OSDN Git Service

add log (#373)
[bytom/vapor.git] / protocol / block_test.go
1 package protocol
2
3 import (
4         "testing"
5
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"
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) SaveBlock(*types.Block, *bc.TransactionStatus) error          { return nil }
30 func (s *mStore) SaveBlockHeader(blockHeader *types.BlockHeader) error {
31         s.blockHeaders[blockHeader.Hash()] = blockHeader
32         return nil
33 }
34 func (s *mStore) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
35         return nil
36 }
37
38 func TestCalcReorganizeChain(t *testing.T) {
39         c := &Chain{
40                 store: &mStore{
41                         blockHeaders: make(map[bc.Hash]*types.BlockHeader),
42                 },
43         }
44
45         initBlockHeader := &types.BlockHeader{
46                 Height:  0,
47                 Version: 1,
48         }
49         c.store.SaveBlockHeader(initBlockHeader)
50
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(),
58                         Height:            uint64(i),
59                 }
60                 wantDetachBlockHeaders = append([]*types.BlockHeader{mainChainBlockHeader}, wantDetachBlockHeaders...)
61                 c.store.SaveBlockHeader(mainChainBlockHeader)
62         }
63
64         for i := 1; i <= 13; i++ {
65                 newChainBlockHeader = &types.BlockHeader{
66                         PreviousBlockHash: newChainBlockHeader.Hash(),
67                         Height:            uint64(i),
68                         Version:           1,
69                 }
70                 wantAttachBlockHeaders = append(wantAttachBlockHeaders, newChainBlockHeader)
71                 c.store.SaveBlockHeader(newChainBlockHeader)
72         }
73
74         // normal
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)
78         }
79
80         if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) {
81                 t.Errorf("normal test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders)
82         }
83
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)
91         }
92
93         if !testutil.DeepEqual(wantDetachBlockHeaders, getDetachBlockHeaders) {
94                 t.Errorf("detachBlockHeaders is empty test: detach headers want %v but get %v", wantDetachBlockHeaders, getDetachBlockHeaders)
95         }
96 }