@@ -8,6 +8,7 @@ use quic_rpc::{
8
8
} ;
9
9
use serde:: { Deserialize , Serialize } ;
10
10
use std:: collections:: BTreeMap ;
11
+ use std:: time:: { Duration , Instant } ;
11
12
12
13
use crate :: { RpcResult , VersionRequest , VersionResponse , WatchRequest , WatchResponse } ;
13
14
@@ -40,6 +41,20 @@ pub struct ListenersResponse {
40
41
pub addrs : Vec < Multiaddr > ,
41
42
}
42
43
44
+ #[ derive( Serialize , Deserialize , Debug ) ]
45
+ pub struct DhtGetRequest {
46
+ pub key : Key ,
47
+ }
48
+
49
+ #[ derive( Serialize , Deserialize , Debug ) ]
50
+ pub struct PeerRecord {
51
+ pub peer_id : Option < Bytes > ,
52
+ pub key : Bytes ,
53
+ pub value : Bytes ,
54
+ pub ttl : u32 ,
55
+ pub publisher : Option < Bytes > ,
56
+ }
57
+
43
58
#[ derive( Serialize , Deserialize , Debug ) ]
44
59
pub struct BitswapRequest {
45
60
pub cid : Cid ,
@@ -219,6 +234,7 @@ pub enum P2pRequest {
219
234
Watch ( WatchRequest ) ,
220
235
Version ( VersionRequest ) ,
221
236
Shutdown ( ShutdownRequest ) ,
237
+ DhtGet ( DhtGetRequest ) ,
222
238
FetchBitswap ( BitswapRequest ) ,
223
239
FetchProviderDht ( FetchProvidersDhtRequest ) ,
224
240
StopSessionBitswap ( StopSessionBitswapRequest ) ,
@@ -250,6 +266,7 @@ pub enum P2pRequest {
250
266
pub enum P2pResponse {
251
267
Watch ( WatchResponse ) ,
252
268
Version ( VersionResponse ) ,
269
+ DhtGet ( RpcResult < PeerRecord > ) ,
253
270
FetchBitswap ( RpcResult < BitswapResponse > ) ,
254
271
FetchProviderDht ( RpcResult < FetchProvidersDhtResponse > ) ,
255
272
GetListeningAddrs ( RpcResult < GetListeningAddrsResponse > ) ,
@@ -291,6 +308,14 @@ impl RpcMsg<P2pService> for ShutdownRequest {
291
308
type Response = RpcResult < ( ) > ;
292
309
}
293
310
311
+ impl Msg < P2pService > for DhtGetRequest {
312
+ type Response = RpcResult < PeerRecord > ;
313
+
314
+ type Update = Self ;
315
+
316
+ type Pattern = ServerStreaming ;
317
+ }
318
+
294
319
impl RpcMsg < P2pService > for BitswapRequest {
295
320
type Response = RpcResult < BitswapResponse > ;
296
321
}
@@ -394,3 +419,53 @@ impl RpcMsg<P2pService> for ExternalAddrsRequest {
394
419
impl RpcMsg < P2pService > for ListenersRequest {
395
420
type Response = RpcResult < ListenersResponse > ;
396
421
}
422
+
423
+ impl From < libp2p:: kad:: PeerRecord > for PeerRecord {
424
+ fn from ( peer_record : libp2p:: kad:: PeerRecord ) -> Self {
425
+ PeerRecord {
426
+ peer_id : peer_record. peer . map ( |peer| Bytes :: from ( peer. to_bytes ( ) ) ) ,
427
+ key : peer_record. record . key . to_vec ( ) . into ( ) ,
428
+ ttl : peer_record
429
+ . record
430
+ . expires
431
+ . map ( |t| {
432
+ let now = Instant :: now ( ) ;
433
+ if t > now {
434
+ ( t - now) . as_secs ( ) as u32
435
+ } else {
436
+ 1 // because 0 means "does not expire"
437
+ }
438
+ } )
439
+ . unwrap_or ( 0 ) ,
440
+ value : peer_record. record . value . into ( ) ,
441
+ publisher : peer_record
442
+ . record
443
+ . publisher
444
+ . map ( |publisher| Bytes :: from ( publisher. to_bytes ( ) ) ) ,
445
+ }
446
+ }
447
+ }
448
+
449
+ impl From < PeerRecord > for libp2p:: kad:: PeerRecord {
450
+ fn from ( peer_record : PeerRecord ) -> Self {
451
+ let peer = peer_record
452
+ . peer_id
453
+ . as_ref ( )
454
+ . map ( |peer_id| libp2p:: PeerId :: from_bytes ( peer_id) . unwrap ( ) ) ;
455
+ let key = libp2p:: kad:: record:: Key :: from ( peer_record. key . to_vec ( ) ) ;
456
+ let value = peer_record. value ;
457
+ let publisher = peer_record
458
+ . publisher
459
+ . as_ref ( )
460
+ . map ( |publisher| libp2p:: PeerId :: from_bytes ( publisher) . unwrap ( ) ) ;
461
+ let expires = ( peer_record. ttl > 0 )
462
+ . then ( || Instant :: now ( ) + Duration :: from_secs ( peer_record. ttl as u64 ) ) ;
463
+ let record = libp2p:: kad:: Record {
464
+ key,
465
+ value : value. to_vec ( ) ,
466
+ publisher,
467
+ expires,
468
+ } ;
469
+ libp2p:: kad:: PeerRecord { peer, record }
470
+ }
471
+ }
0 commit comments