aboutsummaryrefslogtreecommitdiffstats
path: root/blockpool/blockpool_test.go
blob: 9bcd72f046823b568390d7a1f0e9b14e3ba364b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package blockpool

import (
    "testing"
    "time"

    "github.com/ethereum/go-ethereum/blockpool/test"
)

// using the mock framework in blockpool_util_test
// we test various scenarios here

func TestPeerWithKnownBlock(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.refBlockChain[0] = nil
    blockPoolTester.blockChain[0] = nil
    blockPool.Start()

    peer0 := blockPoolTester.newPeer("0", 1, 0)
    peer0.AddPeer()

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    // no request on known block
    peer0.checkBlockHashesRequests()
}

func TestPeerWithKnownParentBlock(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.initRefBlockChain(1)
    blockPoolTester.blockChain[0] = nil
    blockPool.Start()

    peer0 := blockPoolTester.newPeer("0", 1, 1)
    peer0.AddPeer()
    peer0.serveBlocks(0, 1)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    peer0.checkBlocksRequests([]int{1})
    peer0.checkBlockHashesRequests()
    blockPoolTester.refBlockChain[1] = []int{}
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
}

func TestSimpleChain(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(2)

    blockPool.Start()

    peer1 := blockPoolTester.newPeer("peer1", 2, 2)
    peer1.AddPeer()
    peer1.serveBlocks(1, 2)
    go peer1.serveBlockHashes(2, 1, 0)
    peer1.serveBlocks(0, 1)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[2] = []int{}
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
}

func TestChainConnectingWithParentHash(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(3)

    blockPool.Start()

    peer1 := blockPoolTester.newPeer("peer1", 3, 3)
    peer1.AddPeer()
    go peer1.serveBlocks(2, 3)
    go peer1.serveBlockHashes(3, 2, 1)
    peer1.serveBlocks(0, 1, 2)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[3] = []int{}
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
}

func TestMultiSectionChain(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(5)

    blockPool.Start()

    peer1 := blockPoolTester.newPeer("peer1", 5, 5)

    peer1.AddPeer()
    go peer1.serveBlocks(4, 5)
    go peer1.serveBlockHashes(5, 4, 3)
    go peer1.serveBlocks(2, 3, 4)
    go peer1.serveBlockHashes(3, 2, 1, 0)
    peer1.serveBlocks(0, 1, 2)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[5] = []int{}
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
}

func TestNewBlocksOnPartialChain(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(7)
    blockPool.Start()

    peer1 := blockPoolTester.newPeer("peer1", 5, 5)
    blockPoolTester.tds = make(map[int]int)
    blockPoolTester.tds[5] = 5

    peer1.AddPeer()
    go peer1.serveBlocks(4, 5) // partially complete section
    go peer1.serveBlockHashes(5, 4, 3)
    peer1.serveBlocks(3, 4) // partially complete section

    // peer1 found new blocks
    peer1.td = 7
    peer1.currentBlock = 7
    peer1.AddPeer()
    peer1.sendBlocks(6, 7)
    go peer1.serveBlockHashes(7, 6, 5)
    go peer1.serveBlocks(2, 3)
    go peer1.serveBlocks(5, 6)
    go peer1.serveBlockHashes(3, 2, 1) // tests that hash request from known chain root is remembered
    peer1.serveBlocks(0, 1, 2)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[7] = []int{}
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
}

func TestPeerSwitchUp(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(7)

    blockPool.Start()

    peer1 := blockPoolTester.newPeer("peer1", 6, 6)
    peer2 := blockPoolTester.newPeer("peer2", 7, 7)

    peer1.AddPeer()
    go peer1.serveBlocks(5, 6)
    go peer1.serveBlockHashes(6, 5, 4, 3) //
    peer1.serveBlocks(2, 3)               // section partially complete, block 3 will be preserved after peer demoted
    peer2.AddPeer()                       // peer2 is promoted as best peer, peer1 is demoted
    go peer2.serveBlocks(6, 7)            //
    go peer2.serveBlocks(4, 5)            // tests that block request for earlier section is remembered
    go peer1.serveBlocks(3, 4)            // tests that connecting section by demoted peer is remembered and blocks are accepted from demoted peer
    go peer2.serveBlockHashes(3, 2, 1, 0) // tests that known chain section is activated, hash requests from 3 is remembered
    peer2.serveBlocks(0, 1, 2)            // final blocks linking to blockchain sent

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[7] = []int{}
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
}

func TestPeerSwitchDownOverlapSectionWithoutRootBlock(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(6)
    blockPool.Start()

    peer1 := blockPoolTester.newPeer("peer1", 4, 4)
    peer2 := blockPoolTester.newPeer("peer2", 6, 6)

    peer2.AddPeer()
    peer2.serveBlocks(5, 6)                  // partially complete, section will be preserved
    peer2.serveBlockHashes(6, 5, 4)          // no go: make sure skeleton is created
    peer1.AddPeer()                          // inferior peer1 is promoted as best peer
    blockPool.RemovePeer("peer2")            // peer2 disconnects
    go peer1.serveBlockHashes(4, 3, 2, 1, 0) //
    go peer1.serveBlocks(3, 4)               //
    go peer1.serveBlocks(4, 5)               // tests that section set by demoted peer is remembered and blocks are accepted from new peer if they have it even if peers original TD is lower
    peer1.serveBlocks(0, 1, 2, 3)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[6] = []int{} // tests that idle sections are not inserted in blockchain
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
}

func TestPeerSwitchDownOverlapSectionWithRootBlock(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(6)
    blockPool.Start()

    peer1 := blockPoolTester.newPeer("peer1", 4, 4)
    peer2 := blockPoolTester.newPeer("peer2", 6, 6)

    peer2.AddPeer()
    peer2.serveBlocks(5, 6)                  // partially complete, section will be preserved
    go peer2.serveBlockHashes(6, 5, 4)       //
    peer2.serveBlocks(3, 4)                  // !incomplete section
    time.Sleep(100 * time.Millisecond)       // make sure block 4 added
    peer1.AddPeer()                          // inferior peer1 is promoted as best peer
    blockPool.RemovePeer("peer2")            // peer2 disconnects
    go peer1.serveBlockHashes(4, 3, 2, 1, 0) // tests that hash request are directly connecting if the head block exists
    go peer1.serveBlocks(4, 5)               // tests that section set by demoted peer is remembered and blocks are accepted from new peer if they have it even if peers original TD is lower
    peer1.serveBlocks(0, 1, 2, 3)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[6] = []int{} // tests that idle sections are not inserted in blockchain
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
}

func TestPeerSwitchDownDisjointSection(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(3)
    blockPool.Start()

    peer1 := blockPoolTester.newPeer("peer1", 3, 3)
    peer2 := blockPoolTester.newPeer("peer2", 6, 6)

    peer2.AddPeer()
    peer2.serveBlocks(5, 6)            // partially complete, section will be preserved
    go peer2.serveBlockHashes(6, 5, 4) //
    peer2.serveBlocks(3, 4, 5)         //
    time.Sleep(100 * time.Millisecond) // make sure blocks are received
    peer1.AddPeer()                    // inferior peer1 is promoted as best peer
    blockPool.RemovePeer("peer2")      // peer2 disconnects
    go peer1.serveBlocks(2, 3)         //
    go peer1.serveBlockHashes(3, 2, 1) //
    peer1.serveBlocks(0, 1, 2)         //

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[3] = []int{} // tests that idle sections are not inserted in blockchain
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
}

func TestPeerSwitchBack(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(8)

    blockPool.Start()

    peer1 := blockPoolTester.newPeer("peer1", 11, 11)
    peer2 := blockPoolTester.newPeer("peer2", 8, 8)

    peer2.AddPeer()
    go peer2.serveBlocks(7, 8)
    go peer2.serveBlockHashes(8, 7, 6)
    go peer2.serveBlockHashes(6, 5, 4)
    peer2.serveBlocks(4, 5)                  // section partially complete
    peer1.AddPeer()                          // peer1 is promoted as best peer
    go peer1.serveBlocks(10, 11)             //
    peer1.serveBlockHashes(11, 10)           // only gives useless results
    blockPool.RemovePeer("peer1")            // peer1 disconnects
    go peer2.serveBlockHashes(4, 3, 2, 1, 0) // tests that asking for hashes from 4 is remembered
    go peer2.serveBlocks(3, 4, 5, 6, 7, 8)   // tests that section 4, 5, 6 and 7, 8 are remembered for missing blocks
    peer2.serveBlocks(0, 1, 2, 3)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[8] = []int{}
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
}

func TestForkSimple(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(9)
    blockPoolTester.refBlockChain[3] = []int{4, 7}
    delete(blockPoolTester.refBlockChain, 6)

    blockPool.Start()
    blockPoolTester.tds = make(map[int]int)
    blockPoolTester.tds[6] = 10
    peer1 := blockPoolTester.newPeer("peer1", 9, 9)
    peer2 := blockPoolTester.newPeer("peer2", 10, 6)

    peer1.AddPeer()
    go peer1.serveBlocks(8, 9)
    go peer1.serveBlockHashes(9, 8, 7, 3, 2)
    peer1.serveBlocks(1, 2, 3, 7, 8)
    peer2.AddPeer()                          // peer2 is promoted as best peer
    go peer2.serveBlocks(5, 6)               //
    go peer2.serveBlockHashes(6, 5, 4, 3, 2) // fork on 3 -> 4 (earlier child: 7)
    go peer2.serveBlocks(1, 2, 3, 4, 5)
    go peer2.serveBlockHashes(2, 1, 0)
    peer2.serveBlocks(0, 1, 2)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[6] = []int{}
    blockPoolTester.refBlockChain[3] = []int{4}
    delete(blockPoolTester.refBlockChain, 7)
    delete(blockPoolTester.refBlockChain, 8)
    delete(blockPoolTester.refBlockChain, 9)
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)

}

func TestForkSwitchBackByNewBlocks(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(11)
    blockPoolTester.refBlockChain[3] = []int{4, 7}
    delete(blockPoolTester.refBlockChain, 6)

    blockPool.Start()
    blockPoolTester.tds = make(map[int]int)
    blockPoolTester.tds[6] = 10
    peer1 := blockPoolTester.newPeer("peer1", 9, 9)
    peer2 := blockPoolTester.newPeer("peer2", 10, 6)

    peer1.AddPeer()
    go peer1.serveBlocks(8, 9)               //
    go peer1.serveBlockHashes(9, 8, 7, 3, 2) //
    peer1.serveBlocks(7, 8)                  // partial section
    // time.Sleep(1 * time.Second)
    peer2.AddPeer()                          //
    go peer2.serveBlocks(5, 6)               //
    go peer2.serveBlockHashes(6, 5, 4, 3, 2) // peer2 forks on block 3
    peer2.serveBlocks(1, 2, 3, 4, 5)         //

    // peer1 finds new blocks
    peer1.td = 11
    peer1.currentBlock = 11
    peer1.AddPeer()
    go peer1.serveBlocks(10, 11)
    go peer1.serveBlockHashes(11, 10, 9)
    go peer1.serveBlocks(9, 10)
    // time.Sleep(1 * time.Second)
    go peer1.serveBlocks(3, 7)      // tests that block requests on earlier fork are remembered
    go peer1.serveBlockHashes(2, 1) // tests that hash request from root of connecting chain section (added by demoted peer) is remembered
    peer1.serveBlocks(0, 1)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[11] = []int{}
    blockPoolTester.refBlockChain[3] = []int{7}
    delete(blockPoolTester.refBlockChain, 6)
    delete(blockPoolTester.refBlockChain, 5)
    delete(blockPoolTester.refBlockChain, 4)
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)

}

func TestForkSwitchBackByPeerSwitchBack(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(9)
    blockPoolTester.refBlockChain[3] = []int{4, 7}
    delete(blockPoolTester.refBlockChain, 6)

    blockPool.Start()

    blockPoolTester.tds = make(map[int]int)
    blockPoolTester.tds[6] = 10

    blockPoolTester.tds = make(map[int]int)
    blockPoolTester.tds[6] = 10

    peer1 := blockPoolTester.newPeer("peer1", 9, 9)
    peer2 := blockPoolTester.newPeer("peer2", 10, 6)

    peer1.AddPeer()
    go peer1.serveBlocks(8, 9)
    go peer1.serveBlockHashes(9, 8, 7, 3, 2)
    peer1.serveBlocks(7, 8)
    peer2.AddPeer()
    go peer2.serveBlocks(5, 6)               //
    go peer2.serveBlockHashes(6, 5, 4, 3, 2) // peer2 forks on block 3
    peer2.serveBlocks(2, 3, 4, 5)            //
    blockPool.RemovePeer("peer2")            // peer2 disconnects, peer1 is promoted again as best peer
    go peer1.serveBlocks(1, 2)               //
    go peer1.serveBlockHashes(2, 1, 0)       //
    go peer1.serveBlocks(3, 7)               // tests that block requests on earlier fork are remembered and orphan section relinks to existing parent block
    peer1.serveBlocks(0, 1)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[9] = []int{}
    blockPoolTester.refBlockChain[3] = []int{7}
    delete(blockPoolTester.refBlockChain, 6)
    delete(blockPoolTester.refBlockChain, 5)
    delete(blockPoolTester.refBlockChain, 4)
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)

}

func TestForkCompleteSectionSwitchBackByPeerSwitchBack(t *testing.T) {
    test.LogInit()
    _, blockPool, blockPoolTester := newTestBlockPool(t)
    blockPoolTester.blockChain[0] = nil
    blockPoolTester.initRefBlockChain(9)
    blockPoolTester.refBlockChain[3] = []int{4, 7}
    delete(blockPoolTester.refBlockChain, 6)

    blockPool.Start()

    blockPoolTester.tds = make(map[int]int)
    blockPoolTester.tds[6] = 10

    peer1 := blockPoolTester.newPeer("peer1", 9, 9)
    peer2 := blockPoolTester.newPeer("peer2", 10, 6)

    peer1.AddPeer()
    go peer1.serveBlocks(8, 9)
    go peer1.serveBlockHashes(9, 8, 7)
    peer1.serveBlocks(3, 7, 8)               // make sure this section is complete
    time.Sleep(1 * time.Second)              //
    go peer1.serveBlockHashes(7, 3, 2)       // block 3/7 is section boundary
    peer1.serveBlocks(2, 3)                  // partially complete sections block 2 missing
    peer2.AddPeer()                          //
    go peer2.serveBlocks(5, 6)               //
    go peer2.serveBlockHashes(6, 5, 4, 3, 2) // peer2 forks on block 3
    peer2.serveBlocks(2, 3, 4, 5)            // block 2 still missing.
    blockPool.RemovePeer("peer2")            // peer2 disconnects, peer1 is promoted again as best peer
    go peer1.serveBlockHashes(2, 1, 0)       //
    peer1.serveBlocks(0, 1, 2)

    blockPool.Wait(waitTimeout)
    blockPool.Stop()
    blockPoolTester.refBlockChain[9] = []int{}
    blockPoolTester.refBlockChain[3] = []int{7}
    delete(blockPoolTester.refBlockChain, 6)
    delete(blockPoolTester.refBlockChain, 5)
    delete(blockPoolTester.refBlockChain, 4)
    blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)

}