@@ -55,6 +55,7 @@ use tracing::{debug, error, trace, warn};
55
55
56
56
mod active_requests;
57
57
mod crypto;
58
+ mod request_call;
58
59
mod session;
59
60
mod tests;
60
61
@@ -64,6 +65,7 @@ use crate::metrics::METRICS;
64
65
65
66
use crate :: lru_time_cache:: LruTimeCache ;
66
67
use active_requests:: ActiveRequests ;
68
+ use request_call:: RequestCall ;
67
69
use session:: Session ;
68
70
69
71
// The time interval to check banned peer timeouts and unban peers when the timeout has elapsed (in
@@ -151,49 +153,6 @@ pub struct Challenge {
151
153
remote_enr : Option < Enr > ,
152
154
}
153
155
154
- /// A request to a node that we are waiting for a response.
155
- #[ derive( Debug ) ]
156
- pub ( crate ) struct RequestCall {
157
- contact : NodeContact ,
158
- /// The raw discv5 packet sent.
159
- packet : Packet ,
160
- /// The unencrypted message. Required if need to re-encrypt and re-send.
161
- request : Request ,
162
- /// Handshakes attempted.
163
- handshake_sent : bool ,
164
- /// The number of times this request has been re-sent.
165
- retries : u8 ,
166
- /// If we receive a Nodes Response with a total greater than 1. This keeps track of the
167
- /// remaining responses expected.
168
- remaining_responses : Option < u64 > ,
169
- /// Signifies if we are initiating the session with a random packet. This is only used to
170
- /// determine the connection direction of the session.
171
- initiating_session : bool ,
172
- }
173
-
174
- impl RequestCall {
175
- fn new (
176
- contact : NodeContact ,
177
- packet : Packet ,
178
- request : Request ,
179
- initiating_session : bool ,
180
- ) -> Self {
181
- RequestCall {
182
- contact,
183
- packet,
184
- request,
185
- handshake_sent : false ,
186
- retries : 1 ,
187
- remaining_responses : None ,
188
- initiating_session,
189
- }
190
- }
191
-
192
- fn id ( & self ) -> & RequestId {
193
- & self . request . id
194
- }
195
- }
196
-
197
156
/// Process to handle handshakes and sessions established from raw RPC communications between nodes.
198
157
pub struct Handler {
199
158
/// Configuration for the discv5 service.
@@ -432,7 +391,7 @@ impl Handler {
432
391
node_address : NodeAddress ,
433
392
mut request_call : RequestCall ,
434
393
) {
435
- if request_call. retries >= self . request_retries {
394
+ if request_call. retries ( ) >= self . request_retries {
436
395
trace ! ( "Request timed out with {}" , node_address) ;
437
396
// Remove the request from the awaiting packet_filter
438
397
self . remove_expected_response ( node_address. socket_addr ) ;
@@ -443,12 +402,12 @@ impl Handler {
443
402
// increment the request retry count and restart the timeout
444
403
trace ! (
445
404
"Resending message: {} to {}" ,
446
- request_call. request,
405
+ request_call. request( ) ,
447
406
node_address
448
407
) ;
449
- self . send ( node_address. clone ( ) , request_call. packet . clone ( ) )
408
+ self . send ( node_address. clone ( ) , request_call. packet ( ) . clone ( ) )
450
409
. await ;
451
- request_call. retries += 1 ;
410
+ request_call. increment_retries ( ) ;
452
411
self . active_requests . insert ( node_address, request_call) ;
453
412
}
454
413
}
@@ -599,26 +558,26 @@ impl Handler {
599
558
} ;
600
559
601
560
// double check the message nonces match
602
- if request_call. packet . message_nonce ( ) != & request_nonce {
561
+ if request_call. packet ( ) . message_nonce ( ) != & request_nonce {
603
562
// This could theoretically happen if a peer uses the same node id across
604
563
// different connections.
605
- warn ! ( "Received a WHOAREYOU from a non expected source. Source: {}, message_nonce {} , expected_nonce: {}" , request_call. contact, hex:: encode( request_call. packet. message_nonce( ) ) , hex:: encode( request_nonce) ) ;
564
+ warn ! ( "Received a WHOAREYOU from a non expected source. Source: {}, message_nonce {} , expected_nonce: {}" , request_call. contact( ) , hex:: encode( request_call. packet( ) . message_nonce( ) ) , hex:: encode( request_nonce) ) ;
606
565
// NOTE: Both mappings are removed in this case.
607
566
return ;
608
567
}
609
568
610
569
trace ! (
611
570
"Received a WHOAREYOU packet response. Source: {}" ,
612
- request_call. contact
571
+ request_call. contact( )
613
572
) ;
614
573
615
574
// We do not allow multiple WHOAREYOU packets for a single challenge request. If we have
616
575
// already sent a WHOAREYOU ourselves, we drop sessions who send us a WHOAREYOU in
617
576
// response.
618
- if request_call. handshake_sent {
577
+ if request_call. handshake_sent ( ) {
619
578
warn ! (
620
579
"Authentication response already sent. Dropping session. Node: {}" ,
621
- request_call. contact
580
+ request_call. contact( )
622
581
) ;
623
582
self . fail_request ( request_call, RequestError :: InvalidRemotePacket , true )
624
583
. await ;
@@ -636,12 +595,12 @@ impl Handler {
636
595
637
596
// Generate a new session and authentication packet
638
597
let ( auth_packet, mut session) = match Session :: encrypt_with_header (
639
- & request_call. contact ,
598
+ request_call. contact ( ) ,
640
599
self . key . clone ( ) ,
641
600
updated_enr,
642
601
& self . node_id ,
643
602
& challenge_data,
644
- & ( request_call. request . clone ( ) . encode ( ) ) ,
603
+ & ( request_call. request ( ) . clone ( ) . encode ( ) ) ,
645
604
) {
646
605
Ok ( v) => v,
647
606
Err ( e) => {
@@ -665,15 +624,18 @@ impl Handler {
665
624
//
666
625
// All sent requests must have an associated node_id. Therefore the following
667
626
// must not panic.
668
- let node_address = request_call. contact . node_address ( ) ;
669
- match request_call. contact . enr ( ) {
627
+ let node_address = request_call. contact ( ) . node_address ( ) ;
628
+ match request_call. contact ( ) . enr ( ) {
670
629
Some ( enr) => {
671
630
// NOTE: Here we decide if the session is outgoing or ingoing. The condition for an
672
631
// outgoing session is that we originally sent a RANDOM packet (signifying we did
673
632
// not have a session for a request) and the packet is not a PING (we are not
674
633
// trying to update an old session that may have expired.
675
634
let connection_direction = {
676
- match ( & request_call. initiating_session , & request_call. request . body ) {
635
+ match (
636
+ request_call. initiating_session ( ) ,
637
+ & request_call. request ( ) . body ,
638
+ ) {
677
639
( true , RequestBody :: Ping { .. } ) => ConnectionDirection :: Incoming ,
678
640
( true , _) => ConnectionDirection :: Outgoing ,
679
641
( false , _) => ConnectionDirection :: Incoming ,
@@ -682,9 +644,9 @@ impl Handler {
682
644
683
645
// We already know the ENR. Send the handshake response packet
684
646
trace ! ( "Sending Authentication response to node: {}" , node_address) ;
685
- request_call. packet = auth_packet. clone ( ) ;
686
- request_call. handshake_sent = true ;
687
- request_call. initiating_session = false ;
647
+ request_call. update_packet ( auth_packet. clone ( ) ) ;
648
+ request_call. set_handshake_sent ( ) ;
649
+ request_call. set_initiating_session ( false ) ;
688
650
// Reinsert the request_call
689
651
self . insert_active_request ( request_call) ;
690
652
// Send the actual packet to the send task.
@@ -704,10 +666,10 @@ impl Handler {
704
666
// Don't know the ENR. Establish the session, but request an ENR also
705
667
706
668
// Send the Auth response
707
- let contact = request_call. contact . clone ( ) ;
669
+ let contact = request_call. contact ( ) . clone ( ) ;
708
670
trace ! ( "Sending Authentication response to node: {}" , node_address) ;
709
- request_call. packet = auth_packet. clone ( ) ;
710
- request_call. handshake_sent = true ;
671
+ request_call. update_packet ( auth_packet. clone ( ) ) ;
672
+ request_call. set_handshake_sent ( ) ;
711
673
// Reinsert the request_call
712
674
self . insert_active_request ( request_call) ;
713
675
self . send ( node_address. clone ( ) , auth_packet) . await ;
@@ -1016,7 +978,7 @@ impl Handler {
1016
978
if let ResponseBody :: Nodes { total, .. } = response. body {
1017
979
if total > 1 {
1018
980
// This is a multi-response Nodes response
1019
- if let Some ( remaining_responses) = request_call. remaining_responses . as_mut ( ) {
981
+ if let Some ( remaining_responses) = request_call. remaining_responses_mut ( ) {
1020
982
* remaining_responses -= 1 ;
1021
983
if remaining_responses != & 0 {
1022
984
// more responses remaining, add back the request and send the response
@@ -1034,7 +996,7 @@ impl Handler {
1034
996
}
1035
997
} else {
1036
998
// This is the first instance
1037
- request_call. remaining_responses = Some ( total - 1 ) ;
999
+ * request_call. remaining_responses_mut ( ) = Some ( total - 1 ) ;
1038
1000
// add back the request and send the response
1039
1001
self . active_requests
1040
1002
. insert ( node_address. clone ( ) , request_call) ;
@@ -1074,7 +1036,7 @@ impl Handler {
1074
1036
1075
1037
/// Inserts a request and associated auth_tag mapping.
1076
1038
fn insert_active_request ( & mut self , request_call : RequestCall ) {
1077
- let node_address = request_call. contact . node_address ( ) ;
1039
+ let node_address = request_call. contact ( ) . node_address ( ) ;
1078
1040
1079
1041
// adds the mapping of message nonce to node address
1080
1042
self . active_requests . insert ( node_address, request_call) ;
@@ -1100,7 +1062,7 @@ impl Handler {
1100
1062
) {
1101
1063
// The Request has expired, remove the session.
1102
1064
// Fail the current request
1103
- let request_id = request_call. request . id ;
1065
+ let request_id = request_call. request ( ) . id . clone ( ) ;
1104
1066
if let Err ( e) = self
1105
1067
. service_send
1106
1068
. send ( HandlerOut :: RequestFailed ( request_id, error. clone ( ) ) )
@@ -1109,7 +1071,7 @@ impl Handler {
1109
1071
warn ! ( "Failed to inform request failure {}" , e)
1110
1072
}
1111
1073
1112
- let node_address = request_call. contact . node_address ( ) ;
1074
+ let node_address = request_call. contact ( ) . node_address ( ) ;
1113
1075
self . fail_session ( & node_address, error, remove_session)
1114
1076
. await ;
1115
1077
}
0 commit comments