@@ -68,6 +68,10 @@ func (s *Suite) EthTests() []utesting.Test {
68
68
return []utesting.Test {
69
69
// status
70
70
{Name : "Status" , Fn : s .TestStatus },
71
+ {Name : "MaliciousHandshake" , Fn : s .TestMaliciousHandshake },
72
+ {Name : "BlockRangeUpdateExpired" , Fn : s .TestBlockRangeUpdateHistoryExp },
73
+ {Name : "BlockRangeUpdateFuture" , Fn : s .TestBlockRangeUpdateFuture },
74
+ {Name : "BlockRangeUpdateInvalid" , Fn : s .TestBlockRangeUpdateInvalid },
71
75
// get block headers
72
76
{Name : "GetBlockHeaders" , Fn : s .TestGetBlockHeaders },
73
77
{Name : "GetNonexistentBlockHeaders" , Fn : s .TestGetNonexistentBlockHeaders },
@@ -77,8 +81,6 @@ func (s *Suite) EthTests() []utesting.Test {
77
81
// get history
78
82
{Name : "GetBlockBodies" , Fn : s .TestGetBlockBodies },
79
83
{Name : "GetReceipts" , Fn : s .TestGetReceipts },
80
- // // malicious handshakes + status
81
- {Name : "MaliciousHandshake" , Fn : s .TestMaliciousHandshake },
82
84
// test transactions
83
85
{Name : "LargeTxRequest" , Fn : s .TestLargeTxRequest , Slow : true },
84
86
{Name : "Transaction" , Fn : s .TestTransaction },
@@ -102,15 +104,11 @@ func (s *Suite) SnapTests() []utesting.Test {
102
104
103
105
func (s * Suite ) TestStatus (t * utesting.T ) {
104
106
t .Log (`This test is just a sanity check. It performs an eth protocol handshake.` )
105
-
106
- conn , err := s .dial ()
107
+ conn , err := s .dialAndPeer (nil )
107
108
if err != nil {
108
- t .Fatalf ("dial failed: %v" , err )
109
- }
110
- defer conn .Close ()
111
- if err := conn .peer (s .chain , nil ); err != nil {
112
- t .Fatalf ("peering failed: %v" , err )
109
+ t .Fatal ("peering failed:" , err )
113
110
}
111
+ conn .Close ()
114
112
}
115
113
116
114
// headersMatch returns whether the received headers match the given request
@@ -120,15 +118,12 @@ func headersMatch(expected []*types.Header, headers []*types.Header) bool {
120
118
121
119
func (s * Suite ) TestGetBlockHeaders (t * utesting.T ) {
122
120
t .Log (`This test requests block headers from the node.` )
123
-
124
- conn , err := s .dial ()
121
+ conn , err := s .dialAndPeer (nil )
125
122
if err != nil {
126
- t .Fatalf ("dial failed: %v" , err )
127
- }
128
- defer conn .Close ()
129
- if err = conn .peer (s .chain , nil ); err != nil {
130
123
t .Fatalf ("peering failed: %v" , err )
131
124
}
125
+ defer conn .Close ()
126
+
132
127
// Send headers request.
133
128
req := & eth.GetBlockHeadersPacket {
134
129
RequestId : 33 ,
@@ -161,18 +156,13 @@ func (s *Suite) TestGetBlockHeaders(t *utesting.T) {
161
156
}
162
157
163
158
func (s * Suite ) TestGetNonexistentBlockHeaders (t * utesting.T ) {
164
- t .Log (`This test sends GetBlockHeaders requests for nonexistent blocks (using max uint64 value)
159
+ t .Log (`This test sends GetBlockHeaders requests for nonexistent blocks (using max uint64 value)
165
160
to check if the node disconnects after receiving multiple invalid requests.` )
166
-
167
- conn , err := s .dial ()
161
+ conn , err := s .dialAndPeer (nil )
168
162
if err != nil {
169
- t .Fatalf ("dial failed: %v" , err )
170
- }
171
- defer conn .Close ()
172
-
173
- if err := conn .peer (s .chain , nil ); err != nil {
174
163
t .Fatalf ("peering failed: %v" , err )
175
164
}
165
+ defer conn .Close ()
176
166
177
167
// Create request with max uint64 value for a nonexistent block
178
168
badReq := & eth.GetBlockHeadersPacket {
@@ -205,15 +195,11 @@ to check if the node disconnects after receiving multiple invalid requests.`)
205
195
func (s * Suite ) TestSimultaneousRequests (t * utesting.T ) {
206
196
t .Log (`This test requests blocks headers from the node, performing two requests
207
197
concurrently, with different request IDs.` )
208
-
209
- conn , err := s .dial ()
198
+ conn , err := s .dialAndPeer (nil )
210
199
if err != nil {
211
- t .Fatalf ("dial failed: %v" , err )
212
- }
213
- defer conn .Close ()
214
- if err := conn .peer (s .chain , nil ); err != nil {
215
200
t .Fatalf ("peering failed: %v" , err )
216
201
}
202
+ defer conn .Close ()
217
203
218
204
// Create two different requests.
219
205
req1 := & eth.GetBlockHeadersPacket {
@@ -279,15 +265,11 @@ concurrently, with different request IDs.`)
279
265
func (s * Suite ) TestSameRequestID (t * utesting.T ) {
280
266
t .Log (`This test requests block headers, performing two concurrent requests with the
281
267
same request ID. The node should handle the request by responding to both requests.` )
282
-
283
- conn , err := s .dial ()
268
+ conn , err := s .dialAndPeer (nil )
284
269
if err != nil {
285
- t .Fatalf ("dial failed: %v" , err )
286
- }
287
- defer conn .Close ()
288
- if err := conn .peer (s .chain , nil ); err != nil {
289
270
t .Fatalf ("peering failed: %v" , err )
290
271
}
272
+ defer conn .Close ()
291
273
292
274
// Create two different requests with the same ID.
293
275
reqID := uint64 (1234 )
@@ -350,15 +332,12 @@ same request ID. The node should handle the request by responding to both reques
350
332
func (s * Suite ) TestZeroRequestID (t * utesting.T ) {
351
333
t .Log (`This test sends a GetBlockHeaders message with a request-id of zero,
352
334
and expects a response.` )
353
-
354
- conn , err := s .dial ()
335
+ conn , err := s .dialAndPeer (nil )
355
336
if err != nil {
356
- t .Fatalf ("dial failed: %v" , err )
357
- }
358
- defer conn .Close ()
359
- if err := conn .peer (s .chain , nil ); err != nil {
360
337
t .Fatalf ("peering failed: %v" , err )
361
338
}
339
+ defer conn .Close ()
340
+
362
341
req := & eth.GetBlockHeadersPacket {
363
342
GetBlockHeadersRequest : & eth.GetBlockHeadersRequest {
364
343
Origin : eth.HashOrNumber {Number : 0 },
@@ -385,15 +364,12 @@ and expects a response.`)
385
364
386
365
func (s * Suite ) TestGetBlockBodies (t * utesting.T ) {
387
366
t .Log (`This test sends GetBlockBodies requests to the node for known blocks in the test chain.` )
388
-
389
- conn , err := s .dial ()
367
+ conn , err := s .dialAndPeer (nil )
390
368
if err != nil {
391
- t .Fatalf ("dial failed: %v" , err )
392
- }
393
- defer conn .Close ()
394
- if err := conn .peer (s .chain , nil ); err != nil {
395
369
t .Fatalf ("peering failed: %v" , err )
396
370
}
371
+ defer conn .Close ()
372
+
397
373
// Create block bodies request.
398
374
req := & eth.GetBlockBodiesPacket {
399
375
RequestId : 55 ,
@@ -421,15 +397,11 @@ func (s *Suite) TestGetBlockBodies(t *utesting.T) {
421
397
422
398
func (s * Suite ) TestGetReceipts (t * utesting.T ) {
423
399
t .Log (`This test sends GetReceipts requests to the node for known blocks in the test chain.` )
424
-
425
- conn , err := s .dial ()
400
+ conn , err := s .dialAndPeer (nil )
426
401
if err != nil {
427
- t .Fatalf ("dial failed: %v" , err )
428
- }
429
- defer conn .Close ()
430
- if err := conn .peer (s .chain , nil ); err != nil {
431
402
t .Fatalf ("peering failed: %v" , err )
432
403
}
404
+ defer conn .Close ()
433
405
434
406
// Find some blocks containing receipts.
435
407
var hashes = make ([]common.Hash , 0 , 3 )
@@ -546,17 +518,13 @@ func (s *Suite) TestMaliciousHandshake(t *utesting.T) {
546
518
}
547
519
}
548
520
549
- func (s * Suite ) TestInvalidBlockRangeUpdate (t * utesting.T ) {
521
+ func (s * Suite ) TestBlockRangeUpdateInvalid (t * utesting.T ) {
550
522
t .Log (`This test sends an invalid BlockRangeUpdate message to the node and expects to be disconnected.` )
551
-
552
- conn , err := s .dial ()
523
+ conn , err := s .dialAndPeer (nil )
553
524
if err != nil {
554
- t .Fatalf ( "dial failed: %v" , err )
525
+ t .Fatal ( err )
555
526
}
556
527
defer conn .Close ()
557
- if err := conn .peer (s .chain , nil ); err != nil {
558
- t .Fatalf ("peering failed: %v" , err )
559
- }
560
528
561
529
conn .Write (ethProto , eth .BlockRangeUpdateMsg , & eth.BlockRangeUpdatePacket {
562
530
EarliestBlock : 10 ,
@@ -571,6 +539,76 @@ func (s *Suite) TestInvalidBlockRangeUpdate(t *utesting.T) {
571
539
}
572
540
}
573
541
542
+ func (s * Suite ) TestBlockRangeUpdateFuture (t * utesting.T ) {
543
+ t .Log (`This test sends a BlockRangeUpdate that is beyond the chain head.
544
+ The node should accept the update and should not disonnect.` )
545
+ conn , err := s .dialAndPeer (nil )
546
+ if err != nil {
547
+ t .Fatal (err )
548
+ }
549
+ defer conn .Close ()
550
+
551
+ head := s .chain .Head ().NumberU64 ()
552
+ var hash common.Hash
553
+ rand .Read (hash [:])
554
+ conn .Write (ethProto , eth .BlockRangeUpdateMsg , & eth.BlockRangeUpdatePacket {
555
+ EarliestBlock : head + 10 ,
556
+ LatestBlock : head + 50 ,
557
+ LatestBlockHash : hash ,
558
+ })
559
+
560
+ // Ensure the node does not disconnect us.
561
+ // Just send a few ping messages.
562
+ for range 10 {
563
+ time .Sleep (100 * time .Millisecond )
564
+ if err := conn .Write (baseProto , pingMsg , []any {}); err != nil {
565
+ t .Fatal ("write error:" , err )
566
+ }
567
+ code , _ , err := conn .Read ()
568
+ switch {
569
+ case err != nil :
570
+ t .Fatal ("read error:" , err )
571
+ case code == discMsg :
572
+ t .Fatal ("got disconnect" )
573
+ case code == pongMsg :
574
+ }
575
+ }
576
+ }
577
+
578
+ func (s * Suite ) TestBlockRangeUpdateHistoryExp (t * utesting.T ) {
579
+ t .Log (`This test sends a BlockRangeUpdate announcing incomplete (expired) history.
580
+ The node should accept the update and should not disonnect.` )
581
+ conn , err := s .dialAndPeer (nil )
582
+ if err != nil {
583
+ t .Fatal (err )
584
+ }
585
+ defer conn .Close ()
586
+
587
+ head := s .chain .Head ()
588
+ conn .Write (ethProto , eth .BlockRangeUpdateMsg , & eth.BlockRangeUpdatePacket {
589
+ EarliestBlock : head .NumberU64 () - 10 ,
590
+ LatestBlock : head .NumberU64 (),
591
+ LatestBlockHash : head .Hash (),
592
+ })
593
+
594
+ // Ensure the node does not disconnect us.
595
+ // Just send a few ping messages.
596
+ for range 10 {
597
+ time .Sleep (100 * time .Millisecond )
598
+ if err := conn .Write (baseProto , pingMsg , []any {}); err != nil {
599
+ t .Fatal ("write error:" , err )
600
+ }
601
+ code , _ , err := conn .Read ()
602
+ switch {
603
+ case err != nil :
604
+ t .Fatal ("read error:" , err )
605
+ case code == discMsg :
606
+ t .Fatal ("got disconnect" )
607
+ case code == pongMsg :
608
+ }
609
+ }
610
+ }
611
+
574
612
func (s * Suite ) TestTransaction (t * utesting.T ) {
575
613
t .Log (`This test sends a valid transaction to the node and checks if the
576
614
transaction gets propagated.` )
0 commit comments