Skip to content

Commit

Permalink
fix kademlia version
Browse files Browse the repository at this point in the history
  • Loading branch information
erhant committed Sep 2, 2024
1 parent 7ab68fd commit bd31deb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dkn-compute"
version = "0.1.7"
version = "0.1.8"
edition = "2021"
license = "Apache-2.0"
readme = "README.md"
Expand Down
15 changes: 9 additions & 6 deletions src/p2p/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,14 @@ impl P2PClient {
let addr = info.observed_addr;

// check protocol string
// TODO: take the prefixes from a shared const
let protocol_ok = self.check_version_with_prefix(&info.protocol_version, "dria/");
let protocol_ok =
self.check_version_with_prefix(&info.protocol_version, P2P_IDENTITY_PREFIX!());
if !protocol_ok {
log::warn!(
"Identify: Peer {} has different Identify protocol: (have {}, want {})",
"Identify: Peer {} has different Identify protocol: (have {}, want {}{})",
peer_id,
info.protocol_version,
P2P_IDENTITY_PREFIX!(),
self.version
);
return;
Expand All @@ -271,9 +272,10 @@ impl P2PClient {
if let Some(kad_protocol) = info
.protocols
.iter()
.find(|p| p.to_string().starts_with("/dria/kad/"))
.find(|p| p.to_string().starts_with(P2P_KADEMLIA_PREFIX!()))
{
let protocol_ok = self.check_version_with_prefix(kad_protocol.as_ref(), "/dria/kad/");
let protocol_ok =
self.check_version_with_prefix(kad_protocol.as_ref(), P2P_KADEMLIA_PREFIX!());

// if it matches our protocol, add it to the Kademlia routing table
if protocol_ok {
Expand All @@ -290,9 +292,10 @@ impl P2PClient {
.add_address(&peer_id, addr);
} else {
log::warn!(
"Identify: Peer {} has different Kademlia version: (have {}, want {})",
"Identify: Peer {} has different Kademlia version: (have {}, want {}{})",
peer_id,
kad_protocol,
P2P_KADEMLIA_PREFIX!(),
self.version
);
}
Expand Down
29 changes: 26 additions & 3 deletions src/p2p/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
use libp2p::StreamProtocol;

/// Kademlia protocol prefix, as a macro so that it can be used in const macros.
macro_rules! P2P_KADEMLIA_PREFIX {
() => {
"/dria/kad/"
};
}

/// Kademlia protocol prefix, as a macro so that it can be used in const macros.
macro_rules! P2P_IDENTITY_PREFIX {
() => {
"dria/"
};
}

/// Kademlia protocol version, in the form of `/dria/kad/<version>`.
/// Notice the `/` at the start.
pub(crate) const P2P_KADEMLIA_PROTOCOL: StreamProtocol =
StreamProtocol::new(concat!("/dria/kad/", env!("CARGO_PKG_VERSION")));
pub(crate) const P2P_KADEMLIA_PROTOCOL: StreamProtocol = StreamProtocol::new(concat!(
P2P_KADEMLIA_PREFIX!(),
env!("CARGO_PKG_VERSION_MAJOR"),
".",
env!("CARGO_PKG_VERSION_MINOR")
));

/// Protocol string, checked by Identify protocol
pub(crate) const P2P_PROTOCOL_STRING: &str = concat!("dria/", env!("CARGO_PKG_VERSION"));
pub(crate) const P2P_PROTOCOL_STRING: &str = concat!(
P2P_IDENTITY_PREFIX!(),
env!("CARGO_PKG_VERSION_MAJOR"),
".",
env!("CARGO_PKG_VERSION_MINOR")
);

mod behaviour;
pub use behaviour::{DriaBehaviour, DriaBehaviourEvent};
Expand Down

0 comments on commit bd31deb

Please sign in to comment.