Skip to content

Commit

Permalink
Merge pull request #2029 from eqlabs/chris/protocol-names2
Browse files Browse the repository at this point in the history
feat: update kad and indentify protocol names
  • Loading branch information
CHr15F0x committed May 22, 2024
2 parents 69cd7c3 + 680ebd3 commit 9332125
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
9 changes: 9 additions & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,19 @@ impl ChainId {
})
}

/// A hex string representation, eg.: `"0x534e5f4d41494e"` stands for
/// Mainnet (`SN_MAIN`)
pub fn to_hex_str(&self) -> std::borrow::Cow<'static, str> {
self.0.to_hex_str()
}

/// A human readable representation, eg.: `"SN_MAIN"` stands for Mainnet
pub fn as_str(&self) -> &str {
std::str::from_utf8(self.0.as_be_bytes())
.expect("valid utf8")
.trim_start_matches(|c| c == '\0')
}

pub const MAINNET: Self = Self::from_slice_unwrap(b"SN_MAIN");
pub const SEPOLIA_TESTNET: Self = Self::from_slice_unwrap(b"SN_SEPOLIA");
pub const SEPOLIA_INTEGRATION: Self = Self::from_slice_unwrap(b"SN_INTEGRATION_SEPOLIA");
Expand Down
8 changes: 2 additions & 6 deletions crates/p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ use crate::secret::Secret;
use crate::sync::codec;
use crate::Config;

// TODO
pub const IDENTIFY_PROTOCOL_NAME: &str = "/starknet/id/1.0.0";

// TODO
pub fn kademlia_protocol_name(chain_id: ChainId) -> String {
format!("/starknet/kad/{}/1.0.0", chain_id.to_hex_str())
format!("/starknet/kad/{}/1.0.0", chain_id.as_str())
}

pub struct Behaviour {
Expand Down Expand Up @@ -498,7 +494,7 @@ impl Behaviour {
ping: ping::Behaviour::new(ping::Config::new()),
identify: identify::Behaviour::new(
identify::Config::new(
IDENTIFY_PROTOCOL_NAME.to_string(),
identify::PROTOCOL_NAME.to_string(),
identity.public(),
)
.with_agent_version(format!("pathfinder/{}", env!("CARGO_PKG_VERSION"))),
Expand Down
4 changes: 2 additions & 2 deletions crates/p2p/src/bin/bootstrap/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use libp2p::kad::store::MemoryStore;
use libp2p::kad::{self};
use libp2p::swarm::NetworkBehaviour;
use libp2p::{autonat, dcutr, identify, identity, ping, relay, StreamProtocol};
use p2p::{kademlia_protocol_name, IDENTIFY_PROTOCOL_NAME};
use p2p::kademlia_protocol_name;
use pathfinder_common::ChainId;

#[derive(NetworkBehaviour)]
Expand Down Expand Up @@ -47,7 +47,7 @@ impl BootstrapBehaviour {
dcutr: dcutr::Behaviour::new(peer_id),
ping: ping::Behaviour::new(ping::Config::new()),
identify: identify::Behaviour::new(
identify::Config::new(IDENTIFY_PROTOCOL_NAME.to_string(), pub_key)
identify::Config::new(identify::PROTOCOL_NAME.to_string(), pub_key)
.with_agent_version(format!("pathfinder/{}", env!("CARGO_PKG_VERSION"))),
),
kademlia,
Expand Down
2 changes: 1 addition & 1 deletion crates/p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod test_utils;
mod tests;
mod transport;

pub use behaviour::{kademlia_protocol_name, IDENTIFY_PROTOCOL_NAME};
pub use behaviour::kademlia_protocol_name;
pub use client::peer_agnostic::PeerData;
use client::peer_aware::Client;
pub use libp2p;
Expand Down

0 comments on commit 9332125

Please sign in to comment.