From fa2e4612f221bc5cacccf9468eae4286c99326d5 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 24 Apr 2024 15:34:18 +0300 Subject: [PATCH 1/3] kad: Put record query only to non-local peers Signed-off-by: Alexandru Vasile --- src/protocol/libp2p/kademlia/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/protocol/libp2p/kademlia/mod.rs b/src/protocol/libp2p/kademlia/mod.rs index a1c646f0..6661ddf1 100644 --- a/src/protocol/libp2p/kademlia/mod.rs +++ b/src/protocol/libp2p/kademlia/mod.rs @@ -754,6 +754,10 @@ impl Kademlia { // Put the record to the specified peers. let peers = peers.into_iter().filter_map(|peer| { + if peer == self.service.local_peer_id { + return None; + } + match self.routing_table.entry(Key::from(peer)) { KBucketEntry::Occupied(entry) => Some(entry.clone()), KBucketEntry::Vacant(entry) if !entry.addresses.is_empty() => Some(entry.clone()), From 67af687e69466a22c5f3e65fb37d0e2381ca7ae2 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 24 Apr 2024 15:37:04 +0300 Subject: [PATCH 2/3] kad/store: Always set localstore on an incoming record for PutRecordTo Signed-off-by: Alexandru Vasile --- src/protocol/libp2p/kademlia/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/protocol/libp2p/kademlia/mod.rs b/src/protocol/libp2p/kademlia/mod.rs index 6661ddf1..07384751 100644 --- a/src/protocol/libp2p/kademlia/mod.rs +++ b/src/protocol/libp2p/kademlia/mod.rs @@ -752,6 +752,8 @@ impl Kademlia { Some(KademliaCommand::PutRecordToPeers { record, query_id, peers }) => { tracing::debug!(target: LOG_TARGET, ?query_id, key = ?record.key, "store record to DHT to specified peers"); + self.store.put(record.clone()); + // Put the record to the specified peers. let peers = peers.into_iter().filter_map(|peer| { if peer == self.service.local_peer_id { From 9412f3828a3083ef44d09e02f9490e9ca97b9b43 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 24 Apr 2024 18:35:40 +0300 Subject: [PATCH 3/3] kad: Add update local store flag Signed-off-by: Alexandru Vasile --- src/protocol/libp2p/kademlia/handle.rs | 13 ++++++++++++- src/protocol/libp2p/kademlia/mod.rs | 6 ++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/protocol/libp2p/kademlia/handle.rs b/src/protocol/libp2p/kademlia/handle.rs index 6d693cdb..e10c5ad3 100644 --- a/src/protocol/libp2p/kademlia/handle.rs +++ b/src/protocol/libp2p/kademlia/handle.rs @@ -102,6 +102,9 @@ pub(crate) enum KademliaCommand { /// Use the following peers for the put request. peers: Vec, + + /// Update local store. + update_local_store: bool, }, /// Get record from DHT. @@ -222,7 +225,12 @@ impl KademliaHandle { } /// Store record to DHT to the given peers. - pub async fn put_record_to_peers(&mut self, record: Record, peers: Vec) -> QueryId { + pub async fn put_record_to_peers( + &mut self, + record: Record, + peers: Vec, + update_local_store: bool, + ) -> QueryId { let query_id = self.next_query_id(); let _ = self .cmd_tx @@ -230,6 +238,7 @@ impl KademliaHandle { record, query_id, peers, + update_local_store, }) .await; @@ -282,6 +291,7 @@ impl KademliaHandle { &mut self, record: Record, peers: Vec, + update_local_store: bool, ) -> Result { let query_id = self.next_query_id(); self.cmd_tx @@ -289,6 +299,7 @@ impl KademliaHandle { record, query_id, peers, + update_local_store, }) .map(|_| query_id) .map_err(|_| ()) diff --git a/src/protocol/libp2p/kademlia/mod.rs b/src/protocol/libp2p/kademlia/mod.rs index 07384751..256cc62c 100644 --- a/src/protocol/libp2p/kademlia/mod.rs +++ b/src/protocol/libp2p/kademlia/mod.rs @@ -749,10 +749,12 @@ impl Kademlia { self.routing_table.closest(key, self.replication_factor).into(), ); } - Some(KademliaCommand::PutRecordToPeers { record, query_id, peers }) => { + Some(KademliaCommand::PutRecordToPeers { record, query_id, peers, update_local_store }) => { tracing::debug!(target: LOG_TARGET, ?query_id, key = ?record.key, "store record to DHT to specified peers"); - self.store.put(record.clone()); + if update_local_store { + self.store.put(record.clone()); + } // Put the record to the specified peers. let peers = peers.into_iter().filter_map(|peer| {