@@ -121,7 +121,7 @@ enum NodeValidity {
121
121
#[ derive( Debug ) ]
122
122
enum BucketError {
123
123
Ourselves ,
124
- NotInTheBucket { node_entry : NodeEntry , bucket_distance : usize } ,
124
+ NotInTheBucket { node_entry : NodeEntry , bucket_distance : usize } ,
125
125
}
126
126
127
127
struct PingRequest {
@@ -137,22 +137,14 @@ struct PingRequest {
137
137
reason : PingReason
138
138
}
139
139
140
- #[ derive( Debug ) ]
140
+ #[ derive( Debug , Default ) ]
141
141
pub struct NodeBucket {
142
142
nodes : VecDeque < BucketEntry > , //sorted by last active
143
143
}
144
144
145
- impl Default for NodeBucket {
146
- fn default ( ) -> Self {
147
- NodeBucket :: new ( )
148
- }
149
- }
150
-
151
145
impl NodeBucket {
152
146
fn new ( ) -> Self {
153
- NodeBucket {
154
- nodes : VecDeque :: new ( )
155
- }
147
+ Self :: default ( )
156
148
}
157
149
}
158
150
@@ -161,7 +153,7 @@ pub struct Datagram {
161
153
pub address : SocketAddr ,
162
154
}
163
155
164
- pub struct Discovery < ' a > {
156
+ pub struct Discovery {
165
157
id : NodeId ,
166
158
id_hash : H256 ,
167
159
secret : Secret ,
@@ -182,16 +174,16 @@ pub struct Discovery<'a> {
182
174
check_timestamps : bool ,
183
175
adding_nodes : Vec < NodeEntry > ,
184
176
ip_filter : IpFilter ,
185
- request_backoff : & ' a [ Duration ] ,
177
+ request_backoff : & ' static [ Duration ] ,
186
178
}
187
179
188
180
pub struct TableUpdates {
189
181
pub added : HashMap < NodeId , NodeEntry > ,
190
182
pub removed : HashSet < NodeId > ,
191
183
}
192
184
193
- impl < ' a > Discovery < ' a > {
194
- pub fn new ( key : & KeyPair , public : NodeEndpoint , ip_filter : IpFilter ) -> Discovery < ' static > {
185
+ impl Discovery {
186
+ pub fn new ( key : & KeyPair , public : NodeEndpoint , ip_filter : IpFilter ) -> Discovery {
195
187
Discovery {
196
188
id : * key. public ( ) ,
197
189
id_hash : keccak ( key. public ( ) ) ,
@@ -243,7 +235,8 @@ impl<'a> Discovery<'a> {
243
235
} ;
244
236
let bucket = & mut self . node_buckets [ dist] ;
245
237
bucket. nodes . iter_mut ( ) . find ( |n| n. address . id == e. id )
246
- . map_or ( Err ( BucketError :: NotInTheBucket { node_entry : e. clone ( ) , bucket_distance : dist} . into ( ) ) , |entry| {
238
+ . ok_or_else ( || BucketError :: NotInTheBucket { node_entry : e. clone ( ) , bucket_distance : dist } )
239
+ . and_then ( |entry| {
247
240
entry. address = e;
248
241
entry. last_seen = Instant :: now ( ) ;
249
242
entry. backoff_until = Instant :: now ( ) ;
@@ -389,14 +382,14 @@ impl<'a> Discovery<'a> {
389
382
node. endpoint . to_rlp_list ( & mut rlp) ;
390
383
append_expiration ( & mut rlp) ;
391
384
let old_parity_hash = keccak ( rlp. as_raw ( ) ) ;
392
- let hash = self . send_packet ( PACKET_PING , & node. endpoint . udp_address ( ) , & rlp. drain ( ) ) ?;
385
+ let hash = self . send_packet ( PACKET_PING , node. endpoint . udp_address ( ) , rlp. drain ( ) ) ?;
393
386
394
387
self . in_flight_pings . insert ( node. id , PingRequest {
395
388
sent_at : Instant :: now ( ) ,
396
389
node : node. clone ( ) ,
397
390
echo_hash : hash,
398
391
deprecated_echo_hash : old_parity_hash,
399
- reason : reason
392
+ reason,
400
393
} ) ;
401
394
402
395
trace ! ( target: "discovery" , "Sent Ping to {:?} ; node_id={:#x}" , & node. endpoint, node. id) ;
@@ -407,7 +400,7 @@ impl<'a> Discovery<'a> {
407
400
let mut rlp = RlpStream :: new_list ( 2 ) ;
408
401
rlp. append ( target) ;
409
402
append_expiration ( & mut rlp) ;
410
- self . send_packet ( PACKET_FIND_NODE , & node. endpoint . udp_address ( ) , & rlp. drain ( ) ) ?;
403
+ self . send_packet ( PACKET_FIND_NODE , node. endpoint . udp_address ( ) , rlp. drain ( ) ) ?;
411
404
412
405
self . in_flight_find_nodes . insert ( node. id , FindNodeRequest {
413
406
sent_at : Instant :: now ( ) ,
@@ -419,10 +412,10 @@ impl<'a> Discovery<'a> {
419
412
Ok ( ( ) )
420
413
}
421
414
422
- fn send_packet ( & mut self , packet_id : u8 , address : & SocketAddr , payload : & [ u8 ] ) -> Result < H256 , Error > {
415
+ fn send_packet ( & mut self , packet_id : u8 , address : SocketAddr , payload : Bytes ) -> Result < H256 , Error > {
423
416
let packet = assemble_packet ( packet_id, payload, & self . secret ) ?;
424
417
let hash = H256 :: from_slice ( & packet[ 0 ..32 ] ) ;
425
- self . send_to ( packet, address. clone ( ) ) ;
418
+ self . send_to ( packet, address) ;
426
419
Ok ( hash)
427
420
}
428
421
@@ -498,10 +491,10 @@ impl<'a> Discovery<'a> {
498
491
let packet_id = signed[ 0 ] ;
499
492
let rlp = Rlp :: new ( & signed[ 1 ..] ) ;
500
493
match packet_id {
501
- PACKET_PING => self . on_ping ( & rlp, & node_id, & from, hash_signed. as_bytes ( ) ) ,
502
- PACKET_PONG => self . on_pong ( & rlp, & node_id, & from) ,
503
- PACKET_FIND_NODE => self . on_find_node ( & rlp, & node_id, & from) ,
504
- PACKET_NEIGHBOURS => self . on_neighbours ( & rlp, & node_id, & from) ,
494
+ PACKET_PING => self . on_ping ( & rlp, node_id, from, hash_signed. as_bytes ( ) ) ,
495
+ PACKET_PONG => self . on_pong ( & rlp, node_id, from) ,
496
+ PACKET_FIND_NODE => self . on_find_node ( & rlp, node_id, from) ,
497
+ PACKET_NEIGHBOURS => self . on_neighbours ( & rlp, node_id, from) ,
505
498
_ => {
506
499
debug ! ( target: "discovery" , "Unknown UDP packet: {}" , packet_id) ;
507
500
Ok ( None )
@@ -523,12 +516,12 @@ impl<'a> Discovery<'a> {
523
516
entry. endpoint . is_allowed ( & self . ip_filter ) && entry. id != self . id
524
517
}
525
518
526
- fn on_ping ( & mut self , rlp : & Rlp , node_id : & NodeId , from : & SocketAddr , echo_hash : & [ u8 ] ) -> Result < Option < TableUpdates > , Error > {
519
+ fn on_ping ( & mut self , rlp : & Rlp , node_id : NodeId , from : SocketAddr , echo_hash : & [ u8 ] ) -> Result < Option < TableUpdates > , Error > {
527
520
trace ! ( target: "discovery" , "Got Ping from {:?}" , & from) ;
528
521
let ping_from = if let Ok ( node_endpoint) = NodeEndpoint :: from_rlp ( & rlp. at ( 1 ) ?) {
529
522
node_endpoint
530
523
} else {
531
- let mut address = from. clone ( ) ;
524
+ let mut address = from;
532
525
// address here is the node's tcp port. If we are unable to get the `NodeEndpoint` from the `ping_from`
533
526
// rlp field then this is most likely a BootNode, set the tcp port to 0 because it can not be used for syncing.
534
527
address. set_port ( 0 ) ;
@@ -542,7 +535,7 @@ impl<'a> Discovery<'a> {
542
535
self . check_timestamp ( timestamp) ?;
543
536
let mut response = RlpStream :: new_list ( 3 ) ;
544
537
let pong_to = NodeEndpoint {
545
- address : from. clone ( ) ,
538
+ address : from,
546
539
udp_port : ping_from. udp_port
547
540
} ;
548
541
// Here the PONG's `To` field should be the node we are
@@ -555,27 +548,27 @@ impl<'a> Discovery<'a> {
555
548
556
549
response. append ( & echo_hash) ;
557
550
append_expiration ( & mut response) ;
558
- self . send_packet ( PACKET_PONG , from, & response. drain ( ) ) ?;
551
+ self . send_packet ( PACKET_PONG , from, response. drain ( ) ) ?;
559
552
560
- let entry = NodeEntry { id : * node_id, endpoint : pong_to. clone ( ) } ;
553
+ let entry = NodeEntry { id : node_id, endpoint : pong_to } ;
561
554
if !entry. endpoint . is_valid_discovery_node ( ) {
562
555
debug ! ( target: "discovery" , "Got bad address: {:?}" , entry) ;
563
556
} else if !self . is_allowed ( & entry) {
564
557
debug ! ( target: "discovery" , "Address not allowed: {:?}" , entry) ;
565
558
} else {
566
- self . add_node ( entry. clone ( ) ) ;
559
+ self . add_node ( entry) ;
567
560
}
568
561
Ok ( None )
569
562
}
570
563
571
- fn on_pong ( & mut self , rlp : & Rlp , node_id : & NodeId , from : & SocketAddr ) -> Result < Option < TableUpdates > , Error > {
564
+ fn on_pong ( & mut self , rlp : & Rlp , node_id : NodeId , from : SocketAddr ) -> Result < Option < TableUpdates > , Error > {
572
565
trace ! ( target: "discovery" , "Got Pong from {:?} ; node_id={:#x}" , & from, node_id) ;
573
566
let _pong_to = NodeEndpoint :: from_rlp ( & rlp. at ( 0 ) ?) ?;
574
567
let echo_hash: H256 = rlp. val_at ( 1 ) ?;
575
568
let timestamp: u64 = rlp. val_at ( 2 ) ?;
576
569
self . check_timestamp ( timestamp) ?;
577
570
578
- let expected_node = match self . in_flight_pings . entry ( * node_id) {
571
+ let expected_node = match self . in_flight_pings . entry ( node_id) {
579
572
Entry :: Occupied ( entry) => {
580
573
let expected_node = {
581
574
let request = entry. get ( ) ;
@@ -586,7 +579,7 @@ impl<'a> Discovery<'a> {
586
579
if request. deprecated_echo_hash == echo_hash {
587
580
trace ! ( target: "discovery" , "Got Pong from an old open-ethereum version." ) ;
588
581
}
589
- Some ( ( request. node . clone ( ) , request. reason . clone ( ) ) )
582
+ Some ( ( request. node . clone ( ) , request. reason ) )
590
583
}
591
584
} ;
592
585
@@ -629,16 +622,16 @@ impl<'a> Discovery<'a> {
629
622
}
630
623
}
631
624
632
- fn on_find_node ( & mut self , rlp : & Rlp , node_id : & NodeId , from : & SocketAddr ) -> Result < Option < TableUpdates > , Error > {
625
+ fn on_find_node ( & mut self , rlp : & Rlp , node_id : NodeId , from : SocketAddr ) -> Result < Option < TableUpdates > , Error > {
633
626
trace ! ( target: "discovery" , "Got FindNode from {:?}" , & from) ;
634
627
let target: NodeId = rlp. val_at ( 0 ) ?;
635
628
let timestamp: u64 = rlp. val_at ( 1 ) ?;
636
629
self . check_timestamp ( timestamp) ?;
637
630
638
631
let node = NodeEntry {
639
- id : node_id. clone ( ) ,
632
+ id : node_id,
640
633
endpoint : NodeEndpoint {
641
- address : * from,
634
+ address : from,
642
635
udp_port : from. port ( )
643
636
}
644
637
} ;
@@ -688,7 +681,7 @@ impl<'a> Discovery<'a> {
688
681
}
689
682
let mut packets = Discovery :: prepare_neighbours_packets ( & nearest) ;
690
683
for p in packets. drain ( ..) {
691
- self . send_packet ( PACKET_NEIGHBOURS , & node. endpoint . address , & p) ?;
684
+ self . send_packet ( PACKET_NEIGHBOURS , node. endpoint . address , p) ?;
692
685
}
693
686
trace ! ( target: "discovery" , "Sent {} Neighbours to {:?}" , nearest. len( ) , & node. endpoint) ;
694
687
Ok ( ( ) )
@@ -711,10 +704,10 @@ impl<'a> Discovery<'a> {
711
704
packets. collect ( )
712
705
}
713
706
714
- fn on_neighbours ( & mut self , rlp : & Rlp , node_id : & NodeId , from : & SocketAddr ) -> Result < Option < TableUpdates > , Error > {
707
+ fn on_neighbours ( & mut self , rlp : & Rlp , node_id : NodeId , from : SocketAddr ) -> Result < Option < TableUpdates > , Error > {
715
708
let results_count = rlp. at ( 0 ) ?. item_count ( ) ?;
716
709
717
- let is_expected = match self . in_flight_find_nodes . entry ( * node_id) {
710
+ let is_expected = match self . in_flight_find_nodes . entry ( node_id) {
718
711
Entry :: Occupied ( mut entry) => {
719
712
let expected = {
720
713
let request = entry. get_mut ( ) ;
@@ -862,11 +855,11 @@ fn append_expiration(rlp: &mut RlpStream) {
862
855
rlp. append ( & timestamp) ;
863
856
}
864
857
865
- fn assemble_packet ( packet_id : u8 , bytes : & [ u8 ] , secret : & Secret ) -> Result < Bytes , Error > {
866
- let mut packet = Bytes :: with_capacity ( bytes . len ( ) + 32 + 65 + 1 ) ;
858
+ fn assemble_packet ( packet_id : u8 , payload : Bytes , secret : & Secret ) -> Result < Bytes , Error > {
859
+ let mut packet = Bytes :: with_capacity ( payload . len ( ) + 32 + 65 + 1 ) ;
867
860
packet. resize ( 32 + 65 , 0 ) ; // Filled in below
868
861
packet. push ( packet_id) ;
869
- packet. extend_from_slice ( bytes ) ;
862
+ packet. extend ( payload ) ;
870
863
871
864
let hash = keccak ( & packet[ ( 32 + 65 ) ..] ) ;
872
865
let signature = match sign ( secret, & hash) {
@@ -1043,7 +1036,7 @@ mod tests {
1043
1036
let key = Random . generate ( ) ;
1044
1037
discovery. send_find_node ( & node_entries[ 100 ] , key. public ( ) ) . unwrap ( ) ;
1045
1038
for payload in Discovery :: prepare_neighbours_packets ( & node_entries[ 101 ..116 ] ) {
1046
- let packet = assemble_packet ( PACKET_NEIGHBOURS , & payload, & key. secret ( ) ) . unwrap ( ) ;
1039
+ let packet = assemble_packet ( PACKET_NEIGHBOURS , payload, & key. secret ( ) ) . unwrap ( ) ;
1047
1040
discovery. on_packet ( & packet, from. clone ( ) ) . unwrap ( ) ;
1048
1041
}
1049
1042
@@ -1055,7 +1048,7 @@ mod tests {
1055
1048
// FIND_NODE does not time out because it receives k results.
1056
1049
discovery. send_find_node ( & node_entries[ 100 ] , key. public ( ) ) . unwrap ( ) ;
1057
1050
for payload in Discovery :: prepare_neighbours_packets ( & node_entries[ 101 ..117 ] ) {
1058
- let packet = assemble_packet ( PACKET_NEIGHBOURS , & payload, & key. secret ( ) ) . unwrap ( ) ;
1051
+ let packet = assemble_packet ( PACKET_NEIGHBOURS , payload, & key. secret ( ) ) . unwrap ( ) ;
1059
1052
discovery. on_packet ( & packet, from. clone ( ) ) . unwrap ( ) ;
1060
1053
}
1061
1054
@@ -1065,8 +1058,8 @@ mod tests {
1065
1058
assert_eq ! ( removed, 0 ) ;
1066
1059
1067
1060
// Test bucket evictions with retries.
1068
- let request_backoff = [ Duration :: new ( 0 , 0 ) ; 2 ] ;
1069
- let mut discovery = Discovery { request_backoff : & request_backoff , ..discovery } ;
1061
+ const TEST_REQUEST_BACKOFF : [ Duration ; 2 ] = [ Duration :: from_secs ( 0 ) ; 2 ] ;
1062
+ let mut discovery = Discovery { request_backoff : & TEST_REQUEST_BACKOFF , ..discovery } ;
1070
1063
1071
1064
for _ in 0 ..2 {
1072
1065
discovery. ping ( & node_entries[ 101 ] , PingReason :: Default ) . unwrap ( ) ;
@@ -1289,7 +1282,7 @@ mod tests {
1289
1282
incorrect_pong_rlp. append ( & H256 :: zero ( ) ) ;
1290
1283
append_expiration ( & mut incorrect_pong_rlp) ;
1291
1284
let incorrect_pong_data = assemble_packet (
1292
- PACKET_PONG , & incorrect_pong_rlp. drain ( ) , & discovery2. secret
1285
+ PACKET_PONG , incorrect_pong_rlp. drain ( ) , & discovery2. secret
1293
1286
) . unwrap ( ) ;
1294
1287
if let Some ( _) = discovery1. on_packet ( & incorrect_pong_data, ep2. address . clone ( ) ) . unwrap ( ) {
1295
1288
panic ! ( "Expected no changes to discovery1's table because pong hash is incorrect" ) ;
@@ -1318,7 +1311,7 @@ mod tests {
1318
1311
unexpected_pong_rlp. append ( & H256 :: zero ( ) ) ;
1319
1312
append_expiration ( & mut unexpected_pong_rlp) ;
1320
1313
let unexpected_pong = assemble_packet (
1321
- PACKET_PONG , & unexpected_pong_rlp. drain ( ) , key3. secret ( )
1314
+ PACKET_PONG , unexpected_pong_rlp. drain ( ) , key3. secret ( )
1322
1315
) . unwrap ( ) ;
1323
1316
if let Some ( _) = discovery1. on_packet ( & unexpected_pong, ep3. address . clone ( ) ) . unwrap ( ) {
1324
1317
panic ! ( "Expected no changes to discovery1's table for unexpected pong" ) ;
0 commit comments