Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kad/store: Set memory-store on an incoming record for PutRecordTo #88

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/protocol/libp2p/kademlia/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ pub(crate) enum KademliaCommand {

/// Use the following peers for the put request.
peers: Vec<PeerId>,

/// Update local store.
update_local_store: bool,
},

/// Get record from DHT.
Expand Down Expand Up @@ -222,14 +225,20 @@ impl KademliaHandle {
}

/// Store record to DHT to the given peers.
pub async fn put_record_to_peers(&mut self, record: Record, peers: Vec<PeerId>) -> QueryId {
pub async fn put_record_to_peers(
&mut self,
record: Record,
peers: Vec<PeerId>,
update_local_store: bool,
) -> QueryId {
let query_id = self.next_query_id();
let _ = self
.cmd_tx
.send(KademliaCommand::PutRecordToPeers {
record,
query_id,
peers,
update_local_store,
})
.await;

Expand Down Expand Up @@ -282,13 +291,15 @@ impl KademliaHandle {
&mut self,
record: Record,
peers: Vec<PeerId>,
update_local_store: bool,
) -> Result<QueryId, ()> {
let query_id = self.next_query_id();
self.cmd_tx
.try_send(KademliaCommand::PutRecordToPeers {
record,
query_id,
peers,
update_local_store,
})
.map(|_| query_id)
.map_err(|_| ())
Expand Down
10 changes: 9 additions & 1 deletion src/protocol/libp2p/kademlia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,11 +749,19 @@ 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");

if update_local_store {
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 {
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()),
Expand Down