7 "github.com/vapor/consensus"
8 "github.com/vapor/errors"
9 "github.com/vapor/protocol/bc"
10 "github.com/vapor/protocol/bc/types"
11 "github.com/vapor/test/mock"
12 "github.com/vapor/testutil"
15 func TestBlockLocator(t *testing.T) {
16 blocks := mockBlocks(nil, 500)
23 wantHeight: []uint64{0},
27 wantHeight: []uint64{1, 0},
31 wantHeight: []uint64{7, 6, 5, 4, 3, 2, 1, 0},
35 wantHeight: []uint64{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
39 wantHeight: []uint64{100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 89, 85, 77, 61, 29, 0},
43 wantHeight: []uint64{500, 499, 498, 497, 496, 495, 494, 493, 492, 491, 489, 485, 477, 461, 429, 365, 237, 0},
47 for i, c := range cases {
48 mockChain := mock.NewChain(nil)
49 fs := &fastSync{chain: mockChain}
50 mockChain.SetBestBlockHeader(&blocks[c.bestHeight].BlockHeader)
51 for i := uint64(0); i <= c.bestHeight; i++ {
52 mockChain.SetBlockByHeight(i, blocks[i])
56 for _, i := range c.wantHeight {
57 hash := blocks[i].Hash()
58 want = append(want, &hash)
61 if got := fs.blockLocator(); !testutil.DeepEqual(got, want) {
62 t.Errorf("case %d: got %v want %v", i, got, want)
67 func TestFastBlockSync(t *testing.T) {
70 maxFastSyncBlocksNum = 200
71 baseChain := mockBlocks(nil, 300)
74 syncTimeout time.Duration
75 aBlocks []*types.Block
76 bBlocks []*types.Block
81 syncTimeout: 30 * time.Second,
82 aBlocks: baseChain[:50],
83 bBlocks: baseChain[:301],
84 want: baseChain[:237],
88 syncTimeout: 30 * time.Second,
89 aBlocks: baseChain[:2],
90 bBlocks: baseChain[:300],
91 want: baseChain[:202],
96 for i, c := range cases {
97 syncTimeout = c.syncTimeout
98 a := mockSync(c.aBlocks, nil)
99 b := mockSync(c.bBlocks, nil)
100 netWork := NewNetWork()
101 netWork.Register(a, "192.168.0.1", "test node A", consensus.SFFullNode|consensus.SFFastSync)
102 netWork.Register(b, "192.168.0.2", "test node B", consensus.SFFullNode|consensus.SFFastSync)
103 if B2A, A2B, err := netWork.HandsShake(a, b); err != nil {
104 t.Errorf("fail on peer hands shake %v", err)
109 a.blockKeeper.syncPeer = a.peers.GetPeer("test node B")
110 a.blockKeeper.fastSync.setSyncPeer(a.blockKeeper.syncPeer)
112 if err := a.blockKeeper.fastSync.process(); errors.Root(err) != c.err {
113 t.Errorf("case %d: got %v want %v", i, err, c.err)
116 got := []*types.Block{}
117 for i := uint64(0); i <= a.chain.BestBlockHeight(); i++ {
118 block, err := a.chain.GetBlockByHeight(i)
120 t.Errorf("case %d got err %v", i, err)
122 got = append(got, block)
124 if !testutil.DeepEqual(got, c.want) {
125 t.Errorf("case %d: got %v want %v", i, got, c.want)
130 func TestLocateBlocks(t *testing.T) {
132 blocks := mockBlocks(nil, 100)
139 locator: []uint64{20},
140 stopHash: blocks[100].Hash(),
141 wantHeight: []uint64{20, 21, 22, 23, 24},
145 mockChain := mock.NewChain(nil)
146 fs := &fastSync{chain: mockChain}
147 for _, block := range blocks {
148 mockChain.SetBlockByHeight(block.Height, block)
151 for i, c := range cases {
152 locator := []*bc.Hash{}
153 for _, i := range c.locator {
154 hash := blocks[i].Hash()
155 locator = append(locator, &hash)
158 want := []*types.Block{}
159 for _, i := range c.wantHeight {
160 want = append(want, blocks[i])
163 got, _ := fs.locateBlocks(locator, &c.stopHash)
164 if !testutil.DeepEqual(got, want) {
165 t.Errorf("case %d: got %v want %v", i, got, want)
170 func TestLocateHeaders(t *testing.T) {
171 maxHeadersPerMsg = 10
172 blocks := mockBlocks(nil, 150)
173 blocksHash := []bc.Hash{}
174 for _, block := range blocks {
175 blocksHash = append(blocksHash, block.Hash())
188 locator: []uint64{90},
189 stopHash: &blocksHash[100],
191 wantHeight: []uint64{90, 91, 92, 93, 94, 95, 96, 97, 98, 99},
196 locator: []uint64{20},
197 stopHash: &blocksHash[24],
199 wantHeight: []uint64{20, 21, 22, 23, 24},
204 locator: []uint64{20},
205 stopHash: &blocksHash[20],
206 wantHeight: []uint64{20},
211 locator: []uint64{20},
212 stopHash: &blocksHash[120],
213 wantHeight: []uint64{},
218 locator: []uint64{120, 70},
219 stopHash: &blocksHash[78],
220 wantHeight: []uint64{70, 71, 72, 73, 74, 75, 76, 77, 78},
225 locator: []uint64{15},
226 stopHash: &blocksHash[10],
228 wantHeight: []uint64{},
233 locator: []uint64{15},
234 stopHash: &blocksHash[80],
236 wantHeight: []uint64{15, 26, 37, 48, 59, 70},
241 locator: []uint64{0},
242 stopHash: &blocksHash[100],
244 wantHeight: []uint64{0, 10, 20, 30, 40, 50, 60, 70, 80, 90},
249 for i, c := range cases {
250 mockChain := mock.NewChain(nil)
251 fs := &fastSync{chain: mockChain}
252 for i := uint64(0); i <= c.chainHeight; i++ {
253 mockChain.SetBlockByHeight(i, blocks[i])
256 locator := []*bc.Hash{}
257 for _, i := range c.locator {
258 hash := blocks[i].Hash()
259 locator = append(locator, &hash)
262 want := []*types.BlockHeader{}
263 for _, i := range c.wantHeight {
264 want = append(want, &blocks[i].BlockHeader)
267 got, err := fs.locateHeaders(locator, c.stopHash, c.skip, maxHeadersPerMsg)
268 if err != nil != c.err {
269 t.Errorf("case %d: got %v want err = %v", i, err, c.err)
271 if !testutil.DeepEqual(got, want) {
272 t.Errorf("case %d: got %v want %v", i, got, want)