@@ -344,23 +344,10 @@ impl<
344
344
OverlayCommand :: Request ( request) => self . process_request( request) ,
345
345
OverlayCommand :: Event ( event) => self . process_event( event) ,
346
346
OverlayCommand :: FindContentQuery { target, callback, config } => {
347
- if let Some ( query_id) = self . init_find_content_query( target. clone( ) , Some ( callback) , config) {
348
- trace!(
349
- query. id = %query_id,
350
- content. id = %hex_encode_compact( target. content_id( ) ) ,
351
- content. key = %target,
352
- "FindContent query initialized"
353
- ) ;
354
- }
347
+ self . init_find_content_query( target. clone( ) , callback, config) ;
355
348
}
356
349
OverlayCommand :: FindNodeQuery { target, callback } => {
357
- if let Some ( query_id) = self . init_find_nodes_query( & target, Some ( callback) ) {
358
- trace!(
359
- query. id = %query_id,
360
- node. id = %hex_encode_compact( target) ,
361
- "FindNode query initialized"
362
- ) ;
363
- }
350
+ self . init_find_nodes_query( & target, Some ( callback) ) ;
364
351
}
365
352
OverlayCommand :: RequestEventStream ( callback) => {
366
353
if callback. send( self . event_stream. subscribe( ) ) . is_err( ) {
@@ -752,40 +739,36 @@ impl<
752
739
} ;
753
740
match query. into_result ( ) {
754
741
FindContentQueryResult :: ValidContent ( valid_content, cancelled_peers) => {
755
- if let Some ( responder) = callback {
756
- let ValidatedContent {
757
- content,
758
- was_utp_transfer,
759
- sending_peer,
760
- } = valid_content;
761
-
762
- let trace = if let Some ( mut trace) = query_info. trace {
763
- trace. content_validated ( sending_peer) ;
764
- trace. cancelled = cancelled_peers;
765
- Some ( trace)
766
- } else {
767
- None
768
- } ;
742
+ let ValidatedContent {
743
+ content,
744
+ was_utp_transfer,
745
+ sending_peer,
746
+ } = valid_content;
769
747
770
- if responder
771
- . send ( Ok ( ( content, was_utp_transfer, trace) ) )
772
- . is_err ( )
773
- {
774
- error ! (
775
- "Failed to send RecursiveFindContent result to the initiator of the query"
776
- ) ;
777
- }
748
+ let trace = if let Some ( mut trace) = query_info. trace {
749
+ trace. content_validated ( sending_peer) ;
750
+ trace. cancelled = cancelled_peers;
751
+ Some ( trace)
752
+ } else {
753
+ None
754
+ } ;
755
+
756
+ if callback
757
+ . send ( Ok ( ( content, was_utp_transfer, trace) ) )
758
+ . is_err ( )
759
+ {
760
+ error ! (
761
+ "Failed to send RecursiveFindContent result to the initiator of the query"
762
+ ) ;
778
763
}
779
764
}
780
765
FindContentQueryResult :: NoneFound => {
781
- if let Some ( responder) = callback {
782
- let _ = responder. send ( Err ( OverlayRequestError :: ContentNotFound {
783
- message : "Unable to locate content on the network before timeout"
784
- . to_string ( ) ,
785
- utp : false ,
786
- trace : query_info. trace ,
787
- } ) ) ;
788
- }
766
+ let _ = callback. send ( Err ( OverlayRequestError :: ContentNotFound {
767
+ message : "Unable to locate content on the network before timeout"
768
+ . to_string ( ) ,
769
+ utp : false ,
770
+ trace : query_info. trace ,
771
+ } ) ) ;
789
772
}
790
773
}
791
774
}
@@ -1017,7 +1000,6 @@ impl<
1017
1000
request. discv5. id = %request_id,
1018
1001
"Handling FindContent message" ,
1019
1002
) ;
1020
-
1021
1003
let content_key = match TContentKey :: try_from_bytes ( & request. content_key ) {
1022
1004
Ok ( key) => key,
1023
1005
Err ( _) => {
@@ -2314,7 +2296,7 @@ impl<
2314
2296
& mut self ,
2315
2297
target : & NodeId ,
2316
2298
callback : Option < oneshot:: Sender < Vec < Enr > > > ,
2317
- ) -> Option < QueryId > {
2299
+ ) {
2318
2300
let closest_enrs = self
2319
2301
. kbuckets
2320
2302
. closest_to_node_id ( * target, self . query_num_results ) ;
@@ -2324,7 +2306,7 @@ impl<
2324
2306
if let Some ( callback) = callback {
2325
2307
let _ = callback. send ( vec ! [ ] ) ;
2326
2308
}
2327
- return None ;
2309
+ return ;
2328
2310
}
2329
2311
2330
2312
let query_config = QueryConfig {
@@ -2352,26 +2334,43 @@ impl<
2352
2334
2353
2335
if known_closest_peers. is_empty ( ) {
2354
2336
warn ! ( "Cannot initialize FindNode query (no known close peers)" ) ;
2355
- None
2356
2337
} else {
2357
2338
let find_nodes_query =
2358
2339
FindNodeQuery :: with_config ( query_config, query_info. key ( ) , known_closest_peers) ;
2359
- Some (
2360
- self . find_node_query_pool
2361
- . add_query ( query_info, find_nodes_query) ,
2362
- )
2340
+ let query_id = self
2341
+ . find_node_query_pool
2342
+ . add_query ( query_info, find_nodes_query) ;
2343
+ trace ! (
2344
+ query. id = %query_id,
2345
+ node. id = %hex_encode_compact( target) ,
2346
+ "FindNode query initialized"
2347
+ ) ;
2363
2348
}
2364
2349
}
2365
2350
2366
2351
/// Starts a `FindContentQuery` for a target content key.
2367
2352
fn init_find_content_query (
2368
2353
& mut self ,
2369
2354
target : TContentKey ,
2370
- callback : Option < oneshot:: Sender < RecursiveFindContentResult > > ,
2355
+ callback : oneshot:: Sender < RecursiveFindContentResult > ,
2371
2356
config : FindContentConfig ,
2372
- ) -> Option < QueryId > {
2357
+ ) {
2373
2358
debug ! ( "Starting query for content key: {}" , target) ;
2374
2359
2360
+ // Lookup content locally before querying the network.
2361
+ if let Ok ( Some ( content) ) = self . store . read ( ) . get ( & target) {
2362
+ let local_enr = self . local_enr ( ) ;
2363
+ let mut query_trace = QueryTrace :: new ( & local_enr, target. content_id ( ) . into ( ) ) ;
2364
+ query_trace. node_responded_with_content ( & local_enr) ;
2365
+ query_trace. content_validated ( local_enr. into ( ) ) ;
2366
+ let _ = callback. send ( Ok ( (
2367
+ RawContentValue :: from ( content) ,
2368
+ false ,
2369
+ Some ( query_trace) ,
2370
+ ) ) ) ;
2371
+ return ;
2372
+ }
2373
+
2375
2374
// Represent the target content ID with a node ID.
2376
2375
let target_node_id = NodeId :: new ( & target. content_id ( ) ) ;
2377
2376
let target_key = Key :: from ( target_node_id) ;
@@ -2389,15 +2388,13 @@ impl<
2389
2388
if closest_enrs. is_empty ( ) {
2390
2389
// If there are no connected nodes in the routing table the query cannot proceed.
2391
2390
warn ! ( "No connected nodes in routing table, find content query cannot proceed." ) ;
2392
- if let Some ( callback) = callback {
2393
- let _ = callback. send ( Err ( OverlayRequestError :: ContentNotFound {
2394
- message : "Unable to locate content on the network: no connected nodes in the routing table"
2395
- . to_string ( ) ,
2396
- utp : false ,
2397
- trace : None ,
2398
- } ) ) ;
2399
- }
2400
- return None ;
2391
+ let _ = callback. send ( Err ( OverlayRequestError :: ContentNotFound {
2392
+ message : "Unable to locate content on the network: no connected nodes in the routing table"
2393
+ . to_string ( ) ,
2394
+ utp : false ,
2395
+ trace : None ,
2396
+ } ) ) ;
2397
+ return ;
2401
2398
}
2402
2399
2403
2400
// Convert ENRs into k-bucket keys.
@@ -2418,13 +2415,22 @@ impl<
2418
2415
} ;
2419
2416
2420
2417
let query_info = QueryInfo {
2421
- query_type : QueryType :: FindContent { target, callback } ,
2418
+ query_type : QueryType :: FindContent {
2419
+ target : target. clone ( ) ,
2420
+ callback,
2421
+ } ,
2422
2422
untrusted_enrs : SmallVec :: from_vec ( closest_enrs) ,
2423
2423
trace,
2424
2424
} ;
2425
2425
2426
2426
let query = FindContentQuery :: with_config ( query_config, target_key, closest_nodes) ;
2427
- Some ( self . find_content_query_pool . add_query ( query_info, query) )
2427
+ let query_id = self . find_content_query_pool . add_query ( query_info, query) ;
2428
+ trace ! (
2429
+ query. id = %query_id,
2430
+ content. id = %hex_encode_compact( target. content_id( ) ) ,
2431
+ content. key = %target,
2432
+ "FindContent query initialized"
2433
+ ) ;
2428
2434
}
2429
2435
2430
2436
/// Returns an ENR if one is known for the given NodeId.
@@ -3563,12 +3569,13 @@ mod tests {
3563
3569
let target_content = NodeId :: random ( ) ;
3564
3570
let target_content_key = IdentityContentKey :: new ( target_content. raw ( ) ) ;
3565
3571
3566
- let query_id = service. init_find_content_query (
3572
+ let ( tx, _rx) = oneshot:: channel ( ) ;
3573
+ service. init_find_content_query (
3567
3574
target_content_key. clone ( ) ,
3568
- None ,
3575
+ tx ,
3569
3576
FindContentConfig :: default ( ) ,
3570
3577
) ;
3571
- let query_id = query_id . expect ( "Query ID for new find content query is `None`" ) ;
3578
+ let query_id = QueryId ( 0 ) ; // default query id for all initial queries
3572
3579
3573
3580
let pool = & mut service. find_content_query_pool ;
3574
3581
let ( query_info, query) = pool
@@ -3580,7 +3587,7 @@ mod tests {
3580
3587
& query_info. query_type,
3581
3588
QueryType :: FindContent {
3582
3589
target: _target_content_key,
3583
- callback: None ,
3590
+ callback: _callback ,
3584
3591
}
3585
3592
) ) ;
3586
3593
@@ -3600,13 +3607,14 @@ mod tests {
3600
3607
let target_content = NodeId :: random ( ) ;
3601
3608
let target_content_key = IdentityContentKey :: new ( target_content. raw ( ) ) ;
3602
3609
let ( tx, rx) = oneshot:: channel ( ) ;
3603
- let query_id = service. init_find_content_query (
3610
+ service. init_find_content_query (
3604
3611
target_content_key. clone ( ) ,
3605
- Some ( tx ) ,
3612
+ tx ,
3606
3613
FindContentConfig :: default ( ) ,
3607
3614
) ;
3608
3615
3609
- assert ! ( query_id. is_none( ) ) ;
3616
+ let query = service. find_content_query_pool . iter ( ) . next ( ) ;
3617
+ assert ! ( query. is_none( ) ) ;
3610
3618
assert ! ( rx. await . unwrap( ) . is_err( ) ) ;
3611
3619
}
3612
3620
@@ -3633,9 +3641,9 @@ mod tests {
3633
3641
let target_content = NodeId :: random ( ) ;
3634
3642
let target_content_key = IdentityContentKey :: new ( target_content. raw ( ) ) ;
3635
3643
3636
- let query_id =
3637
- service. init_find_content_query ( target_content_key, None , FindContentConfig :: default ( ) ) ;
3638
- let query_id = query_id . expect ( "Query ID for new find content query is `None`" ) ;
3644
+ let ( tx , _rx ) = oneshot :: channel ( ) ;
3645
+ service. init_find_content_query ( target_content_key, tx , FindContentConfig :: default ( ) ) ;
3646
+ let query_id = QueryId ( 0 ) ; // default query id for all initial queries
3639
3647
3640
3648
// update query in own span so mut ref is dropped after poll
3641
3649
{
@@ -3694,9 +3702,9 @@ mod tests {
3694
3702
let target_content = NodeId :: random ( ) ;
3695
3703
let target_content_key = IdentityContentKey :: new ( target_content. raw ( ) ) ;
3696
3704
3697
- let query_id =
3698
- service. init_find_content_query ( target_content_key, None , FindContentConfig :: default ( ) ) ;
3699
- let query_id = query_id . expect ( "Query ID for new find content query is `None`" ) ;
3705
+ let ( tx , _rx ) = oneshot :: channel ( ) ;
3706
+ service. init_find_content_query ( target_content_key, tx , FindContentConfig :: default ( ) ) ;
3707
+ let query_id = QueryId ( 0 ) ; // default query id for all initial queries
3700
3708
3701
3709
// update query in own span so mut ref is dropped after poll
3702
3710
{
@@ -3757,9 +3765,9 @@ mod tests {
3757
3765
let target_content = NodeId :: random ( ) ;
3758
3766
let target_content_key = IdentityContentKey :: new ( target_content. raw ( ) ) ;
3759
3767
3760
- let query_id =
3761
- service. init_find_content_query ( target_content_key, None , FindContentConfig :: default ( ) ) ;
3762
- let query_id = query_id . expect ( "Query ID for new find content query is `None`" ) ;
3768
+ let ( tx , _rx ) = oneshot :: channel ( ) ;
3769
+ service. init_find_content_query ( target_content_key, tx , FindContentConfig :: default ( ) ) ;
3770
+ let query_id = QueryId ( 0 ) ; // default query id for all initial queries
3763
3771
3764
3772
// update query in own span so mut ref is dropped after poll
3765
3773
{
@@ -3823,12 +3831,12 @@ mod tests {
3823
3831
let target_content_key = IdentityContentKey :: new ( target_content. raw ( ) ) ;
3824
3832
3825
3833
let ( callback_tx, callback_rx) = oneshot:: channel ( ) ;
3826
- let query_id = service. init_find_content_query (
3834
+ service. init_find_content_query (
3827
3835
target_content_key. clone ( ) ,
3828
- Some ( callback_tx) ,
3836
+ callback_tx,
3829
3837
FindContentConfig :: default ( ) ,
3830
3838
) ;
3831
- let query_id = query_id . expect ( "Query ID for new find content query is `None`" ) ;
3839
+ let query_id = QueryId ( 0 ) ; // default query id for all initial queries
3832
3840
3833
3841
let query_event =
3834
3842
OverlayService :: < _ , XorMetric , MockValidator , MemoryContentStore > :: query_event_poll (
0 commit comments