6 "github.com/vapor/database/storage"
7 "github.com/vapor/protocol/bc"
8 "github.com/vapor/protocol/bc/types"
9 "github.com/vapor/protocol/state"
10 "github.com/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) SaveBlock(*types.Block, *bc.TransactionStatus) error { return nil }
30 func (s *mStore) SaveBlockHeader(blockHeader *types.BlockHeader) error {
31 s.blockHeaders[blockHeader.Hash()] = blockHeader
34 func (s *mStore) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
38 func TestCalcReorganizeChain(t *testing.T) {
41 blockHeaders: make(map[bc.Hash]*types.BlockHeader),
45 initBlockHeader := &types.BlockHeader{
49 c.store.SaveBlockHeader(initBlockHeader)
51 var wantAttachBlockHeaders []*types.BlockHeader
52 var wantDetachBlockHeaders []*types.BlockHeader
53 mainChainBlockHeader := initBlockHeader
54 newChainBlockHeader := initBlockHeader
55 for i := 1; i <= 7; i++ {
56 mainChainBlockHeader = &types.BlockHeader{
57 PreviousBlockHash: mainChainBlockHeader.Hash(),
60 wantDetachBlockHeaders = append([]*types.BlockHeader{mainChainBlockHeader}, wantDetachBlockHeaders...)
61 c.store.SaveBlockHeader(mainChainBlockHeader)
64 for i := 1; i <= 13; i++ {
65 newChainBlockHeader = &types.BlockHeader{
66 PreviousBlockHash: newChainBlockHeader.Hash(),
70 wantAttachBlockHeaders = append(wantAttachBlockHeaders, newChainBlockHeader)
71 c.store.SaveBlockHeader(newChainBlockHeader)
75 getAttachBlockHeaders, getDetachBlockHeaders, _ := c.calcReorganizeChain(newChainBlockHeader, mainChainBlockHeader)
76 if !testutil.DeepEqual(wantAttachBlockHeaders, getAttachBlockHeaders) {
77 t.Errorf("normal test: attach headers want %v but get %v", wantAttachBlockHeaders, getAttachBlockHeaders)
80 if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) {
81 t.Errorf("normal test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders)
84 // detachBlockHeaders is empty
85 forkChainBlockHeader := wantAttachBlockHeaders[7]
86 wantAttachBlockHeaders = wantAttachBlockHeaders[8:]
87 wantDetachBlockHeaders = []*types.BlockHeader{}
88 getAttachBlockHeaders, getDetachBlockHeaders, _ = c.calcReorganizeChain(newChainBlockHeader, forkChainBlockHeader)
89 if !testutil.DeepEqual(wantAttachBlockHeaders, getAttachBlockHeaders) {
90 t.Errorf("detachBlockHeaders is empty test: attach headers want %v but get %v", wantAttachBlockHeaders, getAttachBlockHeaders)
93 if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) {
94 t.Errorf("detachBlockHeaders is empty test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders)