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

feat: add network wire byte #6518

Merged
Merged
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
3 changes: 2 additions & 1 deletion base_layer/p2p/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ use tower::ServiceBuilder;
use crate::{
comms_connector::{InboundDomainConnector, PubsubDomainConnector},
config::{P2pConfig, PeerSeedsConfig},
get_network_wire_byte,
peer_seeds::{DnsSeedResolver, SeedPeer},
transport::{TorTransportConfig, TransportType},
TransportConfig,
Expand Down Expand Up @@ -559,7 +560,7 @@ impl ServiceInitializer for P2pInitializer {
.with_node_info(NodeNetworkInfo {
major_version: MAJOR_NETWORK_VERSION,
minor_version: MINOR_NETWORK_VERSION,
network_byte: self.network.as_byte(),
network_wire_byte: get_network_wire_byte(self.network)?,
user_agent: self.user_agent.clone(),
})
.with_minimize_connections(if self.config.dht.minimize_connections {
Expand Down
41 changes: 41 additions & 0 deletions base_layer/p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,44 @@ pub const MAJOR_NETWORK_VERSION: u8 = 0;
/// Minor network version. This should change with each time the network protocol has changed in a backward-compatible
/// way.
pub const MINOR_NETWORK_VERSION: u8 = 0;

// This function returns the network wire byte for any chosen network. Increase these numbers for any given network when
// network traffic separation is required.
// Note: Do not re-use previous values.
fn get_network_wire_byte(network: Network) -> Result<u8, anyhow::Error> {
let network_wire_byte = match network {
Network::MainNet => 0,
Network::StageNet => 40,
Network::NextNet => 80,
Network::LocalNet => 120,
Network::Igor => 160,
Network::Esmeralda => 200,
};
verify_network_wire_byte_range(network_wire_byte, network)?;
Ok(network_wire_byte)
hansieodendaal marked this conversation as resolved.
Show resolved Hide resolved
}

// This function bins the range of u8 numbers for any chosen network to a valid network_wire_byte_range.
// Note: Do not change these ranges.
fn verify_network_wire_byte_range(network_wire_byte: u8, network: Network) -> Result<(), anyhow::Error> {
if network_wire_byte == 0x46 {
return Err(anyhow::anyhow!("Invalid network wire byte, cannot be 0x46 (E)"));
}

let valid = match network {
Network::MainNet => (0..40).contains(&network_wire_byte),
Network::StageNet => (40..80).contains(&network_wire_byte),
Network::NextNet => (80..120).contains(&network_wire_byte),
Network::LocalNet => (120..160).contains(&network_wire_byte),
Network::Igor => (160..200).contains(&network_wire_byte),
Network::Esmeralda => (200..240).contains(&network_wire_byte),
};
if !valid {
return Err(anyhow::anyhow!(
"Invalid network wire byte `{}` for network `{}`",
network_wire_byte,
network
));
}
Ok(())
}
hansieodendaal marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion comms/core/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl CommsBuilder {

/// Set a network byte as per [RFC-173 Versioning](https://rfc.tari.com/RFC-0173_Versioning.html)
pub fn with_network_byte(mut self, network_byte: u8) -> Self {
self.connection_manager_config.network_info.network_byte = network_byte;
self.connection_manager_config.network_info.network_wire_byte = network_byte;
self
}

Expand Down
2 changes: 1 addition & 1 deletion comms/core/src/connection_manager/dialer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ where
tokio::select! {
_ = delay => {
debug!(target: LOG_TARGET, "[Attempt {}] Connecting to peer '{}'", current_state.num_attempts(), current_state.peer().node_id.short_str());
match Self::dial_peer(current_state, &noise_config, &current_transport, config.network_info.network_byte).await {
match Self::dial_peer(current_state, &noise_config, &current_transport, config.network_info.network_wire_byte).await {
(state, Ok((socket, addr))) => {
debug!(target: LOG_TARGET, "Dial succeeded for peer '{}' after {} attempt(s)", state.peer().node_id.short_str(), state.num_attempts());
break (state, Ok((socket, addr)));
Expand Down
6 changes: 3 additions & 3 deletions comms/core/src/connection_manager/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ where
#[cfg(feature = "metrics")]
metrics::pending_connections(None, ConnectionDirection::Inbound).inc();
match Self::read_wire_format(&mut socket, config.time_to_first_byte).await {
Ok(WireMode::Comms(byte)) if byte == config.network_info.network_byte => {
Ok(WireMode::Comms(byte)) if byte == config.network_info.network_wire_byte => {
let this_node_id_str = node_identity.node_id().short_str();
let result = Self::perform_socket_upgrade_procedure(
&node_identity,
Expand Down Expand Up @@ -290,7 +290,7 @@ where
target: LOG_TARGET,
"Peer at address '{}' sent invalid wire format byte. Expected {:x?} got: {:x?} ",
peer_addr,
config.network_info.network_byte,
config.network_info.network_wire_byte,
byte,
);
let _result = socket.shutdown().await;
Expand Down Expand Up @@ -320,7 +320,7 @@ where
"Peer at address '{}' failed to send its wire format. Expected network byte {:x?} or liveness \
byte {:x?} not received. Error: {}",
peer_addr,
config.network_info.network_byte,
config.network_info.network_wire_byte,
LIVENESS_WIRE_MODE,
err
);
Expand Down
2 changes: 1 addition & 1 deletion comms/core/src/protocol/network_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct NodeNetworkInfo {
/// The byte that MUST be sent (outbound connections) or MUST be received (inbound connections) for a connection to
/// be established. This byte cannot be 0x46 (E) because that is reserved for liveness.
/// Default: 0x00
pub network_byte: u8,
pub network_wire_byte: u8,
/// The user agent string for this node
pub user_agent: String,
}
2 changes: 1 addition & 1 deletion comms/dht/examples/propagation/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub async fn create<P: AsRef<Path>>(
.with_node_info(NodeNetworkInfo {
major_version: 0,
minor_version: 0,
network_byte: 0x25,
network_wire_byte: 0x25,
user_agent: "/tari/propagator/0.0.1".to_string(),
})
.with_node_identity(node_identity.clone())
Expand Down
Loading