@@ -153,6 +153,22 @@ pub struct Challenge {
153
153
remote_enr : Option < Enr > ,
154
154
}
155
155
156
+ /// Request ID from the handler's perspective.
157
+ #[ derive( Debug , Clone ) ]
158
+ enum HandlerReqId {
159
+ /// Requests made by the handler.
160
+ Internal ( RequestId ) ,
161
+ /// Requests made from outside the handler.
162
+ External ( RequestId ) ,
163
+ }
164
+
165
+ /// A request queued for sending.
166
+ struct PendingRequest {
167
+ contact : NodeContact ,
168
+ request_id : HandlerReqId ,
169
+ request : RequestBody ,
170
+ }
171
+
156
172
/// Process to handle handshakes and sessions established from raw RPC communications between nodes.
157
173
pub struct Handler {
158
174
/// Configuration for the discv5 service.
@@ -169,7 +185,7 @@ pub struct Handler {
169
185
/// The expected responses by SocketAddr which allows packets to pass the underlying filter.
170
186
filter_expected_responses : Arc < RwLock < HashMap < SocketAddr , usize > > > ,
171
187
/// Requests awaiting a handshake completion.
172
- pending_requests : HashMap < NodeAddress , Vec < ( NodeContact , Request ) > > ,
188
+ pending_requests : HashMap < NodeAddress , Vec < PendingRequest > > ,
173
189
/// Currently in-progress outbound handshakes (WHOAREYOU packets) with peers.
174
190
active_challenges : HashMapDelay < NodeAddress , Challenge > ,
175
191
/// Established sessions with peers.
@@ -191,6 +207,7 @@ type HandlerReturn = (
191
207
mpsc:: UnboundedSender < HandlerIn > ,
192
208
mpsc:: Receiver < HandlerOut > ,
193
209
) ;
210
+
194
211
impl Handler {
195
212
/// A new Session service which instantiates the UDP socket send/recv tasks.
196
213
pub async fn spawn (
@@ -274,13 +291,13 @@ impl Handler {
274
291
Some ( handler_request) = self . service_recv. recv( ) => {
275
292
match handler_request {
276
293
HandlerIn :: Request ( contact, request) => {
277
- let id = request. id . clone ( ) ;
278
- if let Err ( request_error) = self . send_request( contact, * request) . await {
279
- // If the sending failed report to the application
280
- if let Err ( e) = self . service_send. send( HandlerOut :: RequestFailed ( id, request_error) ) . await {
281
- warn!( "Failed to inform that request failed {}" , e)
282
- }
283
- }
294
+ let Request { id , body : request } = * request;
295
+ if let Err ( request_error) = self . send_request( contact, HandlerReqId :: External ( id . clone ( ) ) , request) . await {
296
+ // If the sending failed report to the application
297
+ if let Err ( e) = self . service_send. send( HandlerOut :: RequestFailed ( id, request_error) ) . await {
298
+ warn!( "Failed to inform that request failed {}" , e)
299
+ }
300
+ }
284
301
}
285
302
HandlerIn :: Response ( dst, response) => self . send_response( dst, * response) . await ,
286
303
HandlerIn :: WhoAreYou ( wru_ref, enr) => self . send_challenge( wru_ref, enr) . await ,
@@ -395,7 +412,7 @@ impl Handler {
395
412
// increment the request retry count and restart the timeout
396
413
trace ! (
397
414
"Resending message: {} to {}" ,
398
- request_call. request ( ) ,
415
+ request_call. body ( ) ,
399
416
node_address
400
417
) ;
401
418
self . send ( node_address. clone ( ) , request_call. packet ( ) . clone ( ) )
@@ -409,7 +426,8 @@ impl Handler {
409
426
async fn send_request (
410
427
& mut self ,
411
428
contact : NodeContact ,
412
- request : Request ,
429
+ request_id : HandlerReqId ,
430
+ request : RequestBody ,
413
431
) -> Result < ( ) , RequestError > {
414
432
let node_address = contact. node_address ( ) ;
415
433
@@ -426,15 +444,25 @@ impl Handler {
426
444
self . pending_requests
427
445
. entry ( node_address)
428
446
. or_insert_with ( Vec :: new)
429
- . push ( ( contact, request) ) ;
447
+ . push ( PendingRequest {
448
+ contact,
449
+ request_id,
450
+ request,
451
+ } ) ;
430
452
return Ok ( ( ) ) ;
431
453
}
432
454
433
455
let ( packet, initiating_session) = {
434
456
if let Some ( session) = self . sessions . get_mut ( & node_address) {
435
457
// Encrypt the message and send
458
+ let request = match & request_id {
459
+ HandlerReqId :: Internal ( id) | HandlerReqId :: External ( id) => Request {
460
+ id : id. clone ( ) ,
461
+ body : request. clone ( ) ,
462
+ } ,
463
+ } ;
436
464
let packet = session
437
- . encrypt_message ( self . node_id , & request. clone ( ) . encode ( ) )
465
+ . encrypt_message ( self . node_id , & request. encode ( ) )
438
466
. map_err ( |e| RequestError :: EncryptionFailed ( format ! ( "{:?}" , e) ) ) ?;
439
467
( packet, false )
440
468
} else {
@@ -450,7 +478,13 @@ impl Handler {
450
478
}
451
479
} ;
452
480
453
- let call = RequestCall :: new ( contact, packet. clone ( ) , request, initiating_session) ;
481
+ let call = RequestCall :: new (
482
+ contact,
483
+ packet. clone ( ) ,
484
+ request_id,
485
+ request,
486
+ initiating_session,
487
+ ) ;
454
488
// let the filter know we are expecting a response
455
489
self . add_expected_response ( node_address. socket_addr ) ;
456
490
self . send ( node_address. clone ( ) , packet) . await ;
@@ -593,7 +627,7 @@ impl Handler {
593
627
updated_enr,
594
628
& self . node_id ,
595
629
& challenge_data,
596
- & ( request_call. request ( ) . clone ( ) . encode ( ) ) ,
630
+ & request_call. encode ( ) ,
597
631
) {
598
632
Ok ( v) => v,
599
633
Err ( e) => {
@@ -625,10 +659,7 @@ impl Handler {
625
659
// not have a session for a request) and the packet is not a PING (we are not
626
660
// trying to update an old session that may have expired.
627
661
let connection_direction = {
628
- match (
629
- request_call. initiating_session ( ) ,
630
- & request_call. request ( ) . body ,
631
- ) {
662
+ match ( request_call. initiating_session ( ) , & request_call. body ( ) ) {
632
663
( true , RequestBody :: Ping { .. } ) => ConnectionDirection :: Incoming ,
633
664
( true , _) => ConnectionDirection :: Outgoing ,
634
665
( false , _) => ConnectionDirection :: Incoming ,
@@ -668,13 +699,12 @@ impl Handler {
668
699
self . send ( node_address. clone ( ) , auth_packet) . await ;
669
700
670
701
let id = RequestId :: random ( ) ;
671
- let request = Request {
672
- id : id. clone ( ) ,
673
- body : RequestBody :: FindNode { distances : vec ! [ 0 ] } ,
674
- } ;
675
-
676
- session. awaiting_enr = Some ( id) ;
677
- if let Err ( e) = self . send_request ( contact, request) . await {
702
+ let request = RequestBody :: FindNode { distances : vec ! [ 0 ] } ;
703
+ session. awaiting_enr = Some ( id. clone ( ) ) ;
704
+ if let Err ( e) = self
705
+ . send_request ( contact, HandlerReqId :: Internal ( id) , request)
706
+ . await
707
+ {
678
708
warn ! ( "Failed to send Enr request {}" , e)
679
709
}
680
710
}
@@ -802,21 +832,35 @@ impl Handler {
802
832
self . pending_requests . entry ( node_address)
803
833
{
804
834
// If it exists, there must be a request here
805
- let ( contact, request) = entry. get_mut ( ) . remove ( 0 ) ;
835
+ let PendingRequest {
836
+ contact,
837
+ request_id,
838
+ request,
839
+ } = entry. get_mut ( ) . remove ( 0 ) ;
806
840
if entry. get ( ) . is_empty ( ) {
807
841
entry. remove ( ) ;
808
842
}
809
- let id = request. id . clone ( ) ;
810
843
trace ! ( "Sending next awaiting message. Node: {}" , contact) ;
811
- if let Err ( request_error) = self . send_request ( contact, request) . await {
844
+ if let Err ( request_error) = self
845
+ . send_request ( contact, request_id. clone ( ) , request)
846
+ . await
847
+ {
812
848
warn ! ( "Failed to send next awaiting request {}" , request_error) ;
813
849
// Inform the service that the request failed
814
- if let Err ( e) = self
815
- . service_send
816
- . send ( HandlerOut :: RequestFailed ( id, request_error) )
817
- . await
818
- {
819
- warn ! ( "Failed to inform that request failed {}" , e) ;
850
+ match request_id {
851
+ HandlerReqId :: Internal ( _) => {
852
+ // An internal request could not be sent. For now we do nothing about
853
+ // this.
854
+ }
855
+ HandlerReqId :: External ( id) => {
856
+ if let Err ( e) = self
857
+ . service_send
858
+ . send ( HandlerOut :: RequestFailed ( id, request_error) )
859
+ . await
860
+ {
861
+ warn ! ( "Failed to inform that request failed {}" , e) ;
862
+ }
863
+ }
820
864
}
821
865
}
822
866
}
@@ -954,7 +998,10 @@ impl Handler {
954
998
async fn handle_response ( & mut self , node_address : NodeAddress , response : Response ) {
955
999
// Find a matching request, if any
956
1000
if let Some ( mut request_call) = self . active_requests . remove ( & node_address) {
957
- if request_call. id ( ) != & response. id {
1001
+ let id = match request_call. id ( ) {
1002
+ HandlerReqId :: Internal ( id) | HandlerReqId :: External ( id) => id,
1003
+ } ;
1004
+ if id != & response. id {
958
1005
trace ! (
959
1006
"Received an RPC Response to an unknown request. Likely late response. {}" ,
960
1007
node_address
@@ -1055,13 +1102,19 @@ impl Handler {
1055
1102
) {
1056
1103
// The Request has expired, remove the session.
1057
1104
// Fail the current request
1058
- let request_id = request_call. request ( ) . id . clone ( ) ;
1059
- if let Err ( e) = self
1060
- . service_send
1061
- . send ( HandlerOut :: RequestFailed ( request_id, error. clone ( ) ) )
1062
- . await
1063
- {
1064
- warn ! ( "Failed to inform request failure {}" , e)
1105
+ match request_call. id ( ) {
1106
+ HandlerReqId :: Internal ( _) => {
1107
+ // Do not report failures on requests belonging to the handler.
1108
+ }
1109
+ HandlerReqId :: External ( id) => {
1110
+ if let Err ( e) = self
1111
+ . service_send
1112
+ . send ( HandlerOut :: RequestFailed ( id. clone ( ) , error. clone ( ) ) )
1113
+ . await
1114
+ {
1115
+ warn ! ( "Failed to inform request failure {}" , e)
1116
+ }
1117
+ }
1065
1118
}
1066
1119
1067
1120
let node_address = request_call. contact ( ) . node_address ( ) ;
@@ -1083,13 +1136,20 @@ impl Handler {
1083
1136
. store ( self . sessions . len ( ) , Ordering :: Relaxed ) ;
1084
1137
}
1085
1138
if let Some ( to_remove) = self . pending_requests . remove ( node_address) {
1086
- for request in to_remove {
1087
- if let Err ( e) = self
1088
- . service_send
1089
- . send ( HandlerOut :: RequestFailed ( request. 1 . id , error. clone ( ) ) )
1090
- . await
1091
- {
1092
- warn ! ( "Failed to inform request failure {}" , e)
1139
+ for PendingRequest { request_id, .. } in to_remove {
1140
+ match request_id {
1141
+ HandlerReqId :: Internal ( _) => {
1142
+ // Do not report failures on requests belonging to the handler.
1143
+ }
1144
+ HandlerReqId :: External ( id) => {
1145
+ if let Err ( e) = self
1146
+ . service_send
1147
+ . send ( HandlerOut :: RequestFailed ( id, error. clone ( ) ) )
1148
+ . await
1149
+ {
1150
+ warn ! ( "Failed to inform request failure {}" , e)
1151
+ }
1152
+ }
1093
1153
}
1094
1154
}
1095
1155
}
0 commit comments