Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
make kademlia protocol name configurable
  • Loading branch information
syrettaman committed Sep 29, 2023
commit 1b0f4c0fc22f643c4f5a03180d392773ed482a45
20 changes: 11 additions & 9 deletions src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! Library users use [configuration](crate::config) to configure `pchain_network`. In turn, `pchain_network`
//! uses [behaviour](crate::behaviour) to configure libp2p.

use crate::constants::{self, MAX_TRANSMIT_SIZE, MEGABYTES};
use crate::constants::{MAX_TRANSMIT_SIZE, MEGABYTES};
use crate::conversions;
use crate::messages::{Message, Topic};
use libp2p::{
Expand Down Expand Up @@ -41,16 +41,16 @@ pub(crate) struct PeerBehaviour {
}

impl PeerBehaviour {
pub fn new(id: PublicAddress, local_key: &Keypair, heartbeat_secs: u64) -> Self {
pub fn new(id: PublicAddress, local_key: &Keypair, heartbeat_secs: u64, protocol_name: &str) -> Self {
let local_peer_id: PeerId = conversions::PublicAddress::new(id)
.try_into()
.expect("Invalid PublicAddress.");

// Configure Kademlia
let kad = Self::kad_config(local_peer_id);
let kad = Self::kad_config(local_peer_id, protocol_name);

// Configure Identify
let identify = Self::identify_config(local_key.public());
let identify = Self::identify_config(local_key.public(), protocol_name);

// Configure Gossipsub - subscribe to the topic of its own the base64-encoded public address
let mut gossip = Self::gossipsub_config(local_key, heartbeat_secs);
Expand All @@ -67,9 +67,10 @@ impl PeerBehaviour {
}
}

fn kad_config(peer_id: PeerId) -> Kademlia<MemoryStore> {
fn kad_config(peer_id: PeerId, protocol_name: &str) -> Kademlia<MemoryStore> {
let protocol_name = StreamProtocol::try_from_owned(protocol_name.to_string()).unwrap();
let kad_config = KademliaConfig::default()
.set_protocol_names(vec![StreamProtocol::new(constants::PROTOCOL_NAME)])
.set_protocol_names(vec![protocol_name])
.set_record_filtering(KademliaStoreInserts::FilterBoth)
.to_owned();

Expand All @@ -80,8 +81,8 @@ impl PeerBehaviour {
kad
}

fn identify_config(public_key: PublicKey) -> identify::Behaviour {
let config = identify::Config::new(constants::PROTOCOL_NAME.to_string(), public_key);
fn identify_config(public_key: PublicKey, protocol_ver: &str) -> identify::Behaviour {
let config = identify::Config::new(protocol_ver.to_string(), public_key);
identify::Behaviour::new(config)
}

Expand Down Expand Up @@ -200,7 +201,7 @@ mod test {
use std::net::Ipv4Addr;

use super::PeerBehaviour;
use crate::{Config, conversions, messages::{Topic, MessageTopicHash}};
use crate::{Config, conversions, messages::{Topic, MessageTopicHash}, constants};

use libp2p::{
gossipsub,
Expand Down Expand Up @@ -230,6 +231,7 @@ mod test {
peer_public_address,
&peer_config.keypair,
peer_config.peer_discovery_interval,
&constants::PROTOCOL_NAME,
);

PeerInfo {
Expand Down
24 changes: 23 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::net::Ipv4Addr;

use crate::constants::{
BRAODCAST_MSG_BUFFER_SIZE, PEER_DISCOVERY_INTERVAL, PORT_NUMBER, PRIVATE_MSG_BUFFER_SIZE,
SEND_COMMAND_BUFFER_SIZE,
SEND_COMMAND_BUFFER_SIZE, PROTOCOL_NAME
};
use crate::conversions;
use libp2p::identity::{ed25519, Keypair};
Expand All @@ -34,6 +34,7 @@ use pchain_types::cryptography::PublicAddress;
/// - Buffer size of message: 10
/// - Buffer size of broadcast messages: 10
/// - Interval for peer discover: 10 secs
/// - Protocol name: "/pchain_p2p/1.0.0"
#[derive(Clone)]
pub struct Config {
/// keypair used for identification of this network node
Expand All @@ -56,6 +57,9 @@ pub struct Config {

/// interval in seconds for querying networking to discover peers
pub peer_discovery_interval: u64,

/// Protocol name to communicate with other Kademlia nodes
pub protocol_name: String,
}

/// Create default configuration with an automatically generated keypair.
Expand All @@ -69,6 +73,7 @@ impl Default for Config {
private_msg_buffer_size: PRIVATE_MSG_BUFFER_SIZE,
broadcast_msg_buffer_size: BRAODCAST_MSG_BUFFER_SIZE,
peer_discovery_interval: PEER_DISCOVERY_INTERVAL, // secs
protocol_name: PROTOCOL_NAME.to_string(),
}
}
}
Expand Down Expand Up @@ -119,6 +124,12 @@ impl Config {
self.boot_nodes = boot_nodes;
self
}

/// Set the protocol name for Kademlia communication
pub fn set_protocol_name(mut self, protocol_name: String) -> Self {
self.protocol_name = protocol_name;
self
}
}

/// [Peer] consists of required information to identify an entity in the network, such as
Expand Down Expand Up @@ -294,4 +305,15 @@ mod tests {
assert_eq!(peer_info.peer_id, test_keypair.public().to_peer_id());
assert_eq!(peer_info.port, 1);
}

#[test]
fn test_new_protocol_name() {
let new_protocol_name = "/parallelchain_mainnet/v0.5".to_string();
let test_config =
Config::default().set_protocol_name(new_protocol_name.clone());
assert_eq!(
test_config.protocol_name,
new_protocol_name
);
}
}
1 change: 1 addition & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub(crate) async fn start(
local_public_address,
&local_keypair,
constants::HEARTBEAT_SECS,
&config.protocol_name,
);

let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build();
Expand Down