File tree Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Original file line number Diff line number Diff line change 8
8
- Introduce ` KademliaStoreInserts ` option, which allows to filter records (see
9
9
[ PR 2163] ).
10
10
11
+ - Check local store when calling ` Kademlia::get_providers ` (see [ PR 2221] ).
12
+
11
13
[ PR 2163 ] : https://github.com/libp2p/rust-libp2p/pull/2163
14
+ [ PR 2221 ] : https://github.com/libp2p/rust-libp2p/pull/2163
12
15
13
16
# 0.31.0 [ 2021-07-12]
14
17
Original file line number Diff line number Diff line change @@ -912,9 +912,16 @@ where
912
912
/// The result of this operation is delivered in a
913
913
/// reported via [`KademliaEvent::OutboundQueryCompleted{QueryResult::GetProviders}`].
914
914
pub fn get_providers ( & mut self , key : record:: Key ) -> QueryId {
915
+ let providers = self
916
+ . store
917
+ . providers ( & key)
918
+ . into_iter ( )
919
+ . filter ( |p| !p. is_expired ( Instant :: now ( ) ) )
920
+ . map ( |p| p. provider )
921
+ . collect ( ) ;
915
922
let info = QueryInfo :: GetProviders {
916
923
key : key. clone ( ) ,
917
- providers : HashSet :: new ( ) ,
924
+ providers,
918
925
} ;
919
926
let target = kbucket:: Key :: new ( key) ;
920
927
let peers = self . kbuckets . closest_keys ( & target) ;
Original file line number Diff line number Diff line change @@ -1317,3 +1317,51 @@ fn network_behaviour_inject_address_change() {
1317
1317
kademlia. addresses_of_peer( & remote_peer_id) ,
1318
1318
) ;
1319
1319
}
1320
+
1321
+ #[ test]
1322
+ fn get_providers ( ) {
1323
+ fn prop ( key : record:: Key ) {
1324
+ let ( _, mut single_swarm) = build_node ( ) ;
1325
+ single_swarm
1326
+ . behaviour_mut ( )
1327
+ . start_providing ( key. clone ( ) )
1328
+ . expect ( "could not provide" ) ;
1329
+
1330
+ block_on ( async {
1331
+ match single_swarm. next ( ) . await . unwrap ( ) {
1332
+ SwarmEvent :: Behaviour ( KademliaEvent :: OutboundQueryCompleted {
1333
+ result : QueryResult :: StartProviding ( Ok ( _) ) ,
1334
+ ..
1335
+ } ) => { }
1336
+ SwarmEvent :: Behaviour ( e) => panic ! ( "Unexpected event: {:?}" , e) ,
1337
+ _ => { }
1338
+ }
1339
+ } ) ;
1340
+
1341
+ let query_id = single_swarm. behaviour_mut ( ) . get_providers ( key. clone ( ) ) ;
1342
+
1343
+ block_on ( async {
1344
+ match single_swarm. next ( ) . await . unwrap ( ) {
1345
+ SwarmEvent :: Behaviour ( KademliaEvent :: OutboundQueryCompleted {
1346
+ id,
1347
+ result :
1348
+ QueryResult :: GetProviders ( Ok ( GetProvidersOk {
1349
+ key : found_key,
1350
+ providers,
1351
+ ..
1352
+ } ) ) ,
1353
+ ..
1354
+ } ) if id == query_id => {
1355
+ assert_eq ! ( key, found_key) ;
1356
+ assert_eq ! (
1357
+ single_swarm. local_peer_id( ) ,
1358
+ providers. iter( ) . next( ) . unwrap( )
1359
+ ) ;
1360
+ }
1361
+ SwarmEvent :: Behaviour ( e) => panic ! ( "Unexpected event: {:?}" , e) ,
1362
+ _ => { }
1363
+ }
1364
+ } ) ;
1365
+ }
1366
+ QuickCheck :: new ( ) . tests ( 10 ) . quickcheck ( prop as fn ( _) )
1367
+ }
You can’t perform that action at this time.
0 commit comments