6 "github.com/bytom/vapor/database/storage"
7 "github.com/bytom/vapor/protocol/bc"
8 "github.com/bytom/vapor/protocol/bc/types"
9 "github.com/bytom/vapor/protocol/state"
10 "github.com/bytom/vapor/testutil"
14 blockHeaders map[bc.Hash]*types.BlockHeader
17 func (s *mStore) BlockExist(hash *bc.Hash) bool { return false }
18 func (s *mStore) GetBlock(*bc.Hash) (*types.Block, error) { return nil, nil }
19 func (s *mStore) GetBlockHeader(hash *bc.Hash) (*types.BlockHeader, error) {
20 return s.blockHeaders[*hash], nil
22 func (s *mStore) GetStoreStatus() *BlockStoreState { return nil }
23 func (s *mStore) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) { return nil, nil }
24 func (s *mStore) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error { return nil }
25 func (s *mStore) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil }
26 func (s *mStore) GetConsensusResult(uint64) (*state.ConsensusResult, error) { return nil, nil }
27 func (s *mStore) GetMainChainHash(uint64) (*bc.Hash, error) { return nil, nil }
28 func (s *mStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) { return nil, nil }
29 func (s *mStore) DeleteConsensusResult(seq uint64) error { return nil }
30 func (s *mStore) DeleteBlock(*types.Block) error { return nil }
31 func (s *mStore) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
32 func (s *mStore) SaveBlockHeader(blockHeader *types.BlockHeader) error {
33 s.blockHeaders[blockHeader.Hash()] = blockHeader
36 func (s *mStore) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
40 func TestCalcReorganizeChain(t *testing.T) {
43 blockHeaders: make(map[bc.Hash]*types.BlockHeader),
47 initBlockHeader := &types.BlockHeader{
51 c.store.SaveBlockHeader(initBlockHeader)
53 var wantAttachBlockHeaders []*types.BlockHeader
54 var wantDetachBlockHeaders []*types.BlockHeader
55 mainChainBlockHeader := initBlockHeader
56 newChainBlockHeader := initBlockHeader
57 for i := 1; i <= 7; i++ {
58 mainChainBlockHeader = &types.BlockHeader{
59 PreviousBlockHash: mainChainBlockHeader.Hash(),
62 wantDetachBlockHeaders = append([]*types.BlockHeader{mainChainBlockHeader}, wantDetachBlockHeaders...)
63 c.store.SaveBlockHeader(mainChainBlockHeader)
66 for i := 1; i <= 13; i++ {
67 newChainBlockHeader = &types.BlockHeader{
68 PreviousBlockHash: newChainBlockHeader.Hash(),
72 wantAttachBlockHeaders = append(wantAttachBlockHeaders, newChainBlockHeader)
73 c.store.SaveBlockHeader(newChainBlockHeader)
77 getAttachBlockHeaders, getDetachBlockHeaders, _ := c.calcReorganizeChain(newChainBlockHeader, mainChainBlockHeader)
78 if !testutil.DeepEqual(wantAttachBlockHeaders, getAttachBlockHeaders) {
79 t.Errorf("normal test: attach headers want %v but get %v", wantAttachBlockHeaders, getAttachBlockHeaders)
82 if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) {
83 t.Errorf("normal test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders)
86 // detachBlockHeaders is empty
87 forkChainBlockHeader := wantAttachBlockHeaders[7]
88 wantAttachBlockHeaders = wantAttachBlockHeaders[8:]
89 wantDetachBlockHeaders = []*types.BlockHeader{}
90 getAttachBlockHeaders, getDetachBlockHeaders, _ = c.calcReorganizeChain(newChainBlockHeader, forkChainBlockHeader)
91 if !testutil.DeepEqual(wantAttachBlockHeaders, getAttachBlockHeaders) {
92 t.Errorf("detachBlockHeaders is empty test: attach headers want %v but get %v", wantAttachBlockHeaders, getAttachBlockHeaders)
95 if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) {
96 t.Errorf("detachBlockHeaders is empty test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders)