@@ -45,7 +45,10 @@ use futures::StreamExt;
4545use multiaddr:: Multiaddr ;
4646use tokio:: sync:: mpsc:: { Receiver , Sender } ;
4747
48- use std:: collections:: { hash_map:: Entry , HashMap } ;
48+ use std:: {
49+ collections:: { hash_map:: Entry , HashMap } ,
50+ time:: { Duration , Instant } ,
51+ } ;
4952
5053pub use self :: handle:: RecordsType ;
5154pub use config:: { Config , ConfigBuilder } ;
@@ -115,7 +118,7 @@ pub(crate) struct Kademlia {
115118 service : TransportService ,
116119
117120 /// Local Kademlia key.
118- _local_key : Key < PeerId > ,
121+ local_key : Key < PeerId > ,
119122
120123 /// Connected peers,
121124 peers : HashMap < PeerId , PeerContext > ,
@@ -147,6 +150,9 @@ pub(crate) struct Kademlia {
147150 /// Incoming records validation mode.
148151 validation_mode : IncomingRecordValidationMode ,
149152
153+ /// Default record TTL.
154+ record_ttl : Duration ,
155+
150156 /// Query engine.
151157 engine : QueryEngine ,
152158
@@ -175,12 +181,13 @@ impl Kademlia {
175181 cmd_rx : config. cmd_rx ,
176182 store : MemoryStore :: new ( ) ,
177183 event_tx : config. event_tx ,
178- _local_key : local_key,
184+ local_key,
179185 pending_dials : HashMap :: new ( ) ,
180186 executor : QueryExecutor :: new ( ) ,
181187 pending_substreams : HashMap :: new ( ) ,
182188 update_mode : config. update_mode ,
183189 validation_mode : config. validation_mode ,
190+ record_ttl : config. record_ttl ,
184191 replication_factor : config. replication_factor ,
185192 engine : QueryEngine :: new ( local_peer_id, config. replication_factor , PARALLELISM_FACTOR ) ,
186193 }
@@ -775,9 +782,15 @@ impl Kademlia {
775782 self . routing_table. closest( Key :: from( peer) , self . replication_factor) . into( )
776783 ) ;
777784 }
778- Some ( KademliaCommand :: PutRecord { record, query_id } ) => {
785+ Some ( KademliaCommand :: PutRecord { mut record, query_id } ) => {
779786 tracing:: debug!( target: LOG_TARGET , ?query_id, key = ?record. key, "store record to DHT" ) ;
780787
788+ // For `PUT_VALUE` requests originating locally we are always the publisher.
789+ record. publisher = Some ( self . local_key. clone( ) . into_preimage( ) ) ;
790+
791+ // Make sure TTL is set.
792+ record. expires = record. expires. or_else( || Some ( Instant :: now( ) + self . record_ttl) ) ;
793+
781794 let key = Key :: new( record. key. clone( ) ) ;
782795
783796 self . store. put( record. clone( ) ) ;
@@ -788,9 +801,12 @@ impl Kademlia {
788801 self . routing_table. closest( key, self . replication_factor) . into( ) ,
789802 ) ;
790803 }
791- Some ( KademliaCommand :: PutRecordToPeers { record, query_id, peers, update_local_store } ) => {
804+ Some ( KademliaCommand :: PutRecordToPeers { mut record, query_id, peers, update_local_store } ) => {
792805 tracing:: debug!( target: LOG_TARGET , ?query_id, key = ?record. key, "store record to DHT to specified peers" ) ;
793806
807+ // Make sure TTL is set.
808+ record. expires = record. expires. or_else( || Some ( Instant :: now( ) + self . record_ttl) ) ;
809+
794810 if update_local_store {
795811 self . store. put( record. clone( ) ) ;
796812 }
@@ -854,13 +870,16 @@ impl Kademlia {
854870 self . service. add_known_address( & peer, addresses. into_iter( ) ) ;
855871
856872 }
857- Some ( KademliaCommand :: StoreRecord { record } ) => {
873+ Some ( KademliaCommand :: StoreRecord { mut record } ) => {
858874 tracing:: debug!(
859875 target: LOG_TARGET ,
860876 key = ?record. key,
861877 "store record in local store" ,
862878 ) ;
863879
880+ // Make sure TTL is set.
881+ record. expires = record. expires. or_else( || Some ( Instant :: now( ) + self . record_ttl) ) ;
882+
864883 self . store. put( record) ;
865884 }
866885 None => return Err ( Error :: EssentialTaskClosed ) ,
@@ -914,6 +933,7 @@ mod tests {
914933 replication_factor : 20usize ,
915934 update_mode : RoutingTableUpdateMode :: Automatic ,
916935 validation_mode : IncomingRecordValidationMode :: Automatic ,
936+ record_ttl : Duration :: from_secs ( 36 * 60 * 60 ) ,
917937 event_tx,
918938 cmd_rx,
919939 } ;
0 commit comments