OSDN Git Service

7ac1edf0b4069a91003dfe67871a87638e3b79d7
[bytom/vapor.git] / netsync / consensusmgr / block_fetcher_test.go
1 package consensusmgr
2
3 import (
4         "testing"
5         "time"
6
7         "github.com/vapor/netsync/peers"
8         "github.com/vapor/protocol/bc"
9         "github.com/vapor/protocol/bc/types"
10 )
11
12 type peerMgr struct {
13 }
14
15 func (pm *peerMgr) IsBanned(peerID string, level byte, reason string) bool {
16         return false
17 }
18
19 func (pm *peerMgr) StopPeerGracefully(string) {
20         return
21 }
22
23 type chain struct {
24         blocks []uint64
25 }
26
27 func newChain() *chain {
28         blocks := make([]uint64, 1, 1)
29         blocks[0] = 99
30         return &chain{
31                 blocks: blocks,
32         }
33 }
34
35 func (c *chain) BestBlockHeight() uint64 {
36         return c.blocks[len(c.blocks)-1]
37 }
38
39 func (c *chain) GetHeaderByHash(*bc.Hash) (*types.BlockHeader, error) {
40         return nil, nil
41 }
42
43 func (c *chain) ProcessBlock(block *types.Block) (bool, error) {
44         c.blocks = append(c.blocks, block.Height)
45         return false, nil
46 }
47
48 func (c *chain) ProcessBlockSignature(signature, pubkey []byte, blockHash *bc.Hash) error {
49         return nil
50 }
51
52 func TestBlockFetcher(t *testing.T) {
53         peers := peers.NewPeerSet(&peerMgr{})
54         testCase := []struct {
55                 blockMsg *blockMsg
56                 height   uint64
57         }{
58                 {
59                         blockMsg: &blockMsg{
60                                 block: &types.Block{
61                                         BlockHeader: types.BlockHeader{
62                                                 Height: 100,
63                                         },
64                                 },
65                         },
66                         height: 100,
67                 },
68                 {
69                         blockMsg: &blockMsg{
70                                 block: &types.Block{
71                                         BlockHeader: types.BlockHeader{
72                                                 Height: 101,
73                                         },
74                                 },
75                         },
76                         height: 101,
77                 },
78                 {
79                         blockMsg: &blockMsg{
80                                 block: &types.Block{
81                                         BlockHeader: types.BlockHeader{
82                                                 Height: 105,
83                                         },
84                                 },
85                         },
86                         height: 101,
87                 },
88                 {
89                         blockMsg: &blockMsg{
90                                 block: &types.Block{
91                                         BlockHeader: types.BlockHeader{
92                                                 Height: 200,
93                                         },
94                                 },
95                         },
96                         height: 101,
97                 },
98                 {
99                         blockMsg: &blockMsg{
100                                 block: &types.Block{
101                                         BlockHeader: types.BlockHeader{
102                                                 Height: 104,
103                                         },
104                                 },
105                         },
106                         height: 101,
107                 },
108                 {
109                         blockMsg: &blockMsg{
110                                 block: &types.Block{
111                                         BlockHeader: types.BlockHeader{
112                                                 Height: 103,
113                                         },
114                                 },
115                         },
116                         height: 101,
117                 },
118                 {
119                         blockMsg: &blockMsg{
120                                 block: &types.Block{
121                                         BlockHeader: types.BlockHeader{
122                                                 Height: 102,
123                                         },
124                                 },
125                         },
126                         height: 105,
127                 },
128         }
129         fetcher := newBlockFetcher(newChain(), peers)
130
131         for i, c := range testCase {
132                 fetcher.processNewBlock(c.blockMsg)
133                 time.Sleep(10 * time.Millisecond)
134                 chainHeight := fetcher.chain.BestBlockHeight()
135                 if chainHeight != c.height {
136                         t.Fatalf("test block fetcher error. index %d expected chain height %d but got %d", i, chainHeight, c.height)
137                 }
138         }
139 }