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: Update routing table on kademlia established connections #184

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
kad: Update routing table iff automatic mode
Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv committed Jul 23, 2024

Verified

This commit was signed with the committer’s verified signature.
lexnv Alexandru Vasile
commit 7d36680f45e5f1b4c6bc43dfe84d357f1f722653
36 changes: 21 additions & 15 deletions src/protocol/libp2p/kademlia/mod.rs
Original file line number Diff line number Diff line change
@@ -204,16 +204,23 @@ impl Kademlia {
KBucketEntry::Occupied(entry) => {
entry.connection = ConnectionType::Connected;
}
KBucketEntry::Vacant(old) => {
let (endpoint_address, _) = endpoint.into_parts();

old.key = Key::from(peer);
old.peer = peer;
old.addresses = vec![endpoint_address];
old.connection = ConnectionType::Connected;
mut vacant @ KBucketEntry::Vacant(_) => {
// Can only insert a new peer if the routing table update mode is set to
// automatic.
//
// Otherwise, the user is responsible of adding the peer manually if it
// deems necessary.
if std::matches!(self.update_mode, RoutingTableUpdateMode::Automatic) {
let (endpoint_address, _) = endpoint.into_parts();
vacant.insert(KademliaPeer::new(
peer,
vec![endpoint_address],
ConnectionType::Connected,
));
}
}
entry => {
tracing::warn!(target: LOG_TARGET, ?peer, ?entry, "failed to update routing table on connection");
tracing::debug!(target: LOG_TARGET, ?peer, ?entry, "failed to update routing table on connection");
}
}

@@ -277,13 +284,12 @@ impl Kademlia {
});
}

match self.routing_table.entry(Key::from(peer)) {
KBucketEntry::Occupied(entry) => {
entry.connection = ConnectionType::NotConnected;
}
entry => {
tracing::warn!(target: LOG_TARGET, ?peer, ?entry, "failed to update routing table on disconnect");
}
// Don't add the peer to the routing table into a vacant (or already disconnected) entry.
//
// Update the state if the peer could enter the kbucket during `add_known_peer` or
// `on_connection_established`.
if let KBucketEntry::Occupied(entry) = self.routing_table.entry(Key::from(peer)) {
entry.connection = ConnectionType::NotConnected;
}
}