-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Improve Kademlia DHT usage (#378)
# Description This PR implements the following: - [x] Add Kademlia server mode always on - [x] Add Kademlia table add and remove debug logs - [x] Add Kademlia routing table updated debug log - [x] Remove peers that were discovered from DHT (Known peers remain in the DHT) - [x] Fix check for matching Homestar protocol in Identify events - [x] Add `check_lines_for` utility function to correlate log outputs on a single line - [x] Test that known peers are added to DHT - [x] Test that known peers are not removed from DHT on closed connection - [x] Test that peers are added to DHT on identify event following mDNS discovery - [x] Test that peers that were discovered through mDNS are removed from DHT on closed connection - [x] Add utility function to extract a Peer ID from a Multiaddress ## Link to issue Please add a link to any relevant issues/tickets. ## Type of change - [x] Bug fix (non-breaking change that fixes an issue) - [x] Refactor (non-breaking change that updates existing functionality) ## Test plan (required) We are adding tests for existing functionality. In addition, we added tests to check: - Peers are added to the Kademlia table on connection when they been discovered through mDNS - Peers are removed from the Kademlia table on disconnection when they were discovered through mDNS - Peers are not removed from the Kademlia when they were configured in `node_addresses` - Unit tests to check the Peer ID extraction utility
- Loading branch information
Showing
10 changed files
with
396 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod multiaddr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use libp2p::{multiaddr::Protocol, Multiaddr, PeerId}; | ||
|
||
pub(crate) trait MultiaddrExt { | ||
fn peer_id(&self) -> Option<PeerId>; | ||
} | ||
|
||
impl MultiaddrExt for Multiaddr { | ||
fn peer_id(&self) -> Option<PeerId> { | ||
match self.iter().last() { | ||
Some(Protocol::P2p(peer_id)) => Some(peer_id), | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn contains_peer_id() { | ||
let peer_id = PeerId::random(); | ||
let multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/7001/p2p/{}", peer_id.to_base58()) | ||
.parse() | ||
.unwrap(); | ||
|
||
assert_eq!(Multiaddr::peer_id(&multiaddr).unwrap(), peer_id) | ||
} | ||
|
||
#[test] | ||
fn missing_peer_id() { | ||
let multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/7001").parse().unwrap(); | ||
|
||
assert_eq!(Multiaddr::peer_id(&multiaddr), None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.