OSDN Git Service

versoin1.1.9 (#594)
[bytom/vapor.git] / protocol / block_test.go
1 package protocol
2
3 import (
4         "testing"
5
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"
11 )
12
13 type mStore struct {
14         blockHeaders map[bc.Hash]*types.BlockHeader
15 }
16
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
21 }
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
34         return nil
35 }
36 func (s *mStore) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
37         return nil
38 }
39
40 func TestCalcReorganizeChain(t *testing.T) {
41         c := &Chain{
42                 store: &mStore{
43                         blockHeaders: make(map[bc.Hash]*types.BlockHeader),
44                 },
45         }
46
47         initBlockHeader := &types.BlockHeader{
48                 Height:  0,
49                 Version: 1,
50         }
51         c.store.SaveBlockHeader(initBlockHeader)
52
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(),
60                         Height:            uint64(i),
61                 }
62                 wantDetachBlockHeaders = append([]*types.BlockHeader{mainChainBlockHeader}, wantDetachBlockHeaders...)
63                 c.store.SaveBlockHeader(mainChainBlockHeader)
64         }
65
66         for i := 1; i <= 13; i++ {
67                 newChainBlockHeader = &types.BlockHeader{
68                         PreviousBlockHash: newChainBlockHeader.Hash(),
69                         Height:            uint64(i),
70                         Version:           1,
71                 }
72                 wantAttachBlockHeaders = append(wantAttachBlockHeaders, newChainBlockHeader)
73                 c.store.SaveBlockHeader(newChainBlockHeader)
74         }
75
76         // normal
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)
80         }
81
82         if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) {
83                 t.Errorf("normal test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders)
84         }
85
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)
93         }
94
95         if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) {
96                 t.Errorf("detachBlockHeaders is empty test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders)
97         }
98 }