Commit 8a4c2ee0 authored by muscle_boy's avatar muscle_boy Committed by Paladz

add block integration test (#1745)

* add block integration test

* format code

* remove unuse code

* fix tx testcase

* remove debug code
parent 8d564428
......@@ -17,14 +17,25 @@ var (
numOrphanBlockLimit = 256
)
type orphanBlock struct {
type OrphanBlock struct {
*types.Block
expiration time.Time
}
func NewOrphanBlock(block *types.Block, expiration time.Time) *OrphanBlock {
return &OrphanBlock{
Block: block,
expiration: expiration,
}
}
func (o *OrphanBlock) Equals(o1 *OrphanBlock) bool {
return testutil.DeepEqual(o.Block, o1.Block)
}
// OrphanManage is use to handle all the orphan block
type OrphanManage struct {
orphan map[bc.Hash]*orphanBlock
orphan map[bc.Hash]*OrphanBlock
prevOrphans map[bc.Hash][]*bc.Hash
mtx sync.RWMutex
}
......@@ -32,7 +43,7 @@ type OrphanManage struct {
// NewOrphanManage return a new orphan block
func NewOrphanManage() *OrphanManage {
o := &OrphanManage{
orphan: make(map[bc.Hash]*orphanBlock),
orphan: make(map[bc.Hash]*OrphanBlock),
prevOrphans: make(map[bc.Hash][]*bc.Hash),
}
......@@ -40,6 +51,14 @@ func NewOrphanManage() *OrphanManage {
return o
}
// NewOrphanManageWithData return a new orphan manage with specify data
func NewOrphanManageWithData(orphan map[bc.Hash]*OrphanBlock, prevOrphans map[bc.Hash][]*bc.Hash) *OrphanManage {
return &OrphanManage{
orphan: orphan,
prevOrphans: prevOrphans,
}
}
// Add will add the block to OrphanManage
func (o *OrphanManage) Add(block *types.Block) {
blockHash := block.Hash()
......@@ -55,7 +74,7 @@ func (o *OrphanManage) Add(block *types.Block) {
return
}
o.orphan[blockHash] = &orphanBlock{block, time.Now().Add(orphanBlockTTL)}
o.orphan[blockHash] = &OrphanBlock{block, time.Now().Add(orphanBlockTTL)}
o.prevOrphans[block.PreviousBlockHash] = append(o.prevOrphans[block.PreviousBlockHash], &blockHash)
log.WithFields(log.Fields{"module": logModule, "hash": blockHash.String(), "height": block.Height}).Info("add block to orphan")
......@@ -80,7 +99,12 @@ func (o *OrphanManage) Equals(o1 *OrphanManage) bool {
if o1 == nil {
return false
}
return testutil.DeepEqual(o.orphan, o1.orphan) && testutil.DeepEqual(o.prevOrphans, o1.prevOrphans)
for hash, block := range o.orphan {
if block1, ok := o1.orphan[hash]; !ok || !block.Equals(block1) {
return false
}
}
return testutil.DeepEqual(o.prevOrphans, o1.prevOrphans)
}
// Get return the orphan block by hash
......
......@@ -40,12 +40,12 @@ func TestOrphanManageAdd(t *testing.T) {
}{
{
before: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{},
orphan: map[bc.Hash]*OrphanBlock{},
prevOrphans: map[bc.Hash][]*bc.Hash{},
},
after: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
......@@ -55,16 +55,16 @@ func TestOrphanManageAdd(t *testing.T) {
},
{
before: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
},
},
after: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
......@@ -74,17 +74,17 @@ func TestOrphanManageAdd(t *testing.T) {
},
{
before: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
},
},
after: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
blockHashes[1]: &orphanBlock{testBlocks[1], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
blockHashes[1]: &OrphanBlock{testBlocks[1], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0], &blockHashes[1]},
......@@ -94,17 +94,17 @@ func TestOrphanManageAdd(t *testing.T) {
},
{
before: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
},
},
after: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
blockHashes[2]: &orphanBlock{testBlocks[2], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
blockHashes[2]: &OrphanBlock{testBlocks[2], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
......@@ -134,16 +134,16 @@ func TestOrphanManageDelete(t *testing.T) {
}{
{
before: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
},
},
after: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
......@@ -153,32 +153,32 @@ func TestOrphanManageDelete(t *testing.T) {
},
{
before: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
},
},
after: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{},
orphan: map[bc.Hash]*OrphanBlock{},
prevOrphans: map[bc.Hash][]*bc.Hash{},
},
remove: &blockHashes[0],
},
{
before: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
blockHashes[1]: &orphanBlock{testBlocks[1], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
blockHashes[1]: &OrphanBlock{testBlocks[1], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0], &blockHashes[1]},
},
},
after: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{testBlocks[0], time.Time{}},
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{testBlocks[0], time.Time{}},
},
prevOrphans: map[bc.Hash][]*bc.Hash{
bc.Hash{V0: 1}: []*bc.Hash{&blockHashes[0]},
......@@ -203,8 +203,8 @@ func TestOrphanManageExpire(t *testing.T) {
}{
{
before: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{
testBlocks[0],
time.Unix(1633479700, 0),
},
......@@ -214,14 +214,14 @@ func TestOrphanManageExpire(t *testing.T) {
},
},
after: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{},
orphan: map[bc.Hash]*OrphanBlock{},
prevOrphans: map[bc.Hash][]*bc.Hash{},
},
},
{
before: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{
testBlocks[0],
time.Unix(1633479702, 0),
},
......@@ -231,8 +231,8 @@ func TestOrphanManageExpire(t *testing.T) {
},
},
after: &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{
blockHashes[0]: &orphanBlock{
orphan: map[bc.Hash]*OrphanBlock{
blockHashes[0]: &OrphanBlock{
testBlocks[0],
time.Unix(1633479702, 0),
},
......@@ -277,7 +277,7 @@ func TestOrphanManageNumLimit(t *testing.T) {
for i, c := range cases {
orphanManage := &OrphanManage{
orphan: map[bc.Hash]*orphanBlock{},
orphan: map[bc.Hash]*OrphanBlock{},
prevOrphans: map[bc.Hash][]*bc.Hash{},
}
for num := 0; num < c.addOrphanBlockNum; num++ {
......
......@@ -134,7 +134,7 @@ func NewBlockIndex() *BlockIndex {
}
}
func NewBlockIndexWithInitData(index map[bc.Hash]*BlockNode, mainChain []*BlockNode) *BlockIndex {
func NewBlockIndexWithData(index map[bc.Hash]*BlockNode, mainChain []*BlockNode) *BlockIndex {
return &BlockIndex{index: index, mainChain: mainChain}
}
......
......@@ -77,7 +77,7 @@ func TestValidateUglyTx(t *testing.T) {
*consensus.BTMAssetID, 10000000000, 0, nil),
},
Outputs: []*types.TxOutput{
types.NewTxOutput(*consensus.BTMAssetID, 10000000001, testutil.MustDecodeHexString("00145931e1b7b65897f47845ac08fc136e0c0a4ff166")),
types.NewTxOutput(*consensus.BTMAssetID, 10000000000, testutil.MustDecodeHexString("00145931e1b7b65897f47845ac08fc136e0c0a4ff166")),
},
},
err: true,
......
This diff is collapsed.
......@@ -6,6 +6,7 @@ import (
"os"
"reflect"
"strings"
"sort"
"github.com/golang/protobuf/proto"
......@@ -76,6 +77,7 @@ func getDeserialFun(key []byte) (deserialFun, error) {
string(database.BlockPrefix): func(data []byte) (interface{}, error) {
block := &types.Block{}
err := block.UnmarshalText(data)
sortSpendOutputID(block)
return block, err
},
string(database.BlockHeaderPrefix): func(data []byte) (interface{}, error) {
......@@ -228,3 +230,15 @@ func initStore(c *processBlockTestCase) (protocol.Store, dbm.DB, error) {
batch.Write()
return database.NewStore(testDB), testDB, nil
}
func sortSpendOutputID(block *types.Block) {
for _, tx := range block.Transactions {
sort.Sort(HashSlice(tx.SpentOutputIDs))
}
}
type HashSlice []bc.Hash
func (p HashSlice) Len() int { return len(p) }
func (p HashSlice) Less(i, j int) bool { return p[i].String() < p[j].String() }
func (p HashSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment