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 2 commits
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
2 changes: 1 addition & 1 deletion base_layer/p2p/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,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: self.network.as_wire_byte(),
user_agent: self.user_agent.clone(),
})
.with_minimize_connections(if self.config.dht.minimize_connections {
Expand Down
169 changes: 169 additions & 0 deletions common/src/configuration/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ use serde::{Deserialize, Serialize};

use crate::ConfigurationError;

const LIVENESS_WIRE_MODE: u8 = 0xa7;
const MAIN_NET_RANGE: std::ops::Range<u8> = 0..40;
const STAGE_NET_RANGE: std::ops::Range<u8> = 40..80;
const NEXT_NET_RANGE: std::ops::Range<u8> = 80..120;
const LOCAL_NET_RANGE: std::ops::Range<u8> = 120..160;
const IGOR_RANGE: std::ops::Range<u8> = 160..200;
const ESMERALDA_RANGE: std::ops::Range<u8> = 200..240;
const LEGACY_RANGE: [u8; 6] = [0x00, 0x01, 0x02, 0x10, 0x24, 0x26];

static CURRENT_NETWORK: OnceLock<Network> = OnceLock::new();

/// Represents the available Tari p2p networks. Only nodes with matching byte values will be able to connect, so these
Expand Down Expand Up @@ -86,6 +95,66 @@ impl Network {
LocalNet => "localnet",
}
}

/// 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.
pub fn as_wire_byte(self) -> u8 {
let wire_byte = match self {
// Choose a value in 'MAIN_NET_RANGE' or assign 'self.as_byte()'
Network::MainNet => self.as_byte(),
// Choose a value in 'STAGE_NET_RANGE' or assign 'self.as_byte()'
Network::StageNet => self.as_byte(),
// Choose a value in 'NEXT_NET_RANGE' or assign 'self.as_byte()'
Network::NextNet => self.as_byte(),
// Choose a value in 'LOCAL_NET_RANGE' or assign 'self.as_byte()'
Network::LocalNet => self.as_byte(),
// Choose a value in 'IGOR_RANGE' or assign 'self.as_byte()'
Network::Igor => self.as_byte(),
// Choose a value in 'ESMERALDA_RANGE' or assign 'self.as_byte()'
Network::Esmeralda => self.as_byte(),
};
// The error is squashed to make this method infallible, and the method is called here to ensure that the
// range constants are used.
let _unused = self.verify_network_wire_byte_range(wire_byte);
hansieodendaal marked this conversation as resolved.
Show resolved Hide resolved
wire_byte
}

// Helper function to verify the network wire byte range
fn verify_network_wire_byte_range(self, network_wire_byte: u8) -> Result<(), String> {
// 'LIVENESS_WIRE_MODE' is reserved for '0xa7'
if network_wire_byte == LIVENESS_WIRE_MODE {
return Err("Invalid network wire byte, cannot be '0x46', reserved for 'LIVENESS_WIRE_MODE'".to_string());
}

// Legacy compatibility
if network_wire_byte == self.as_byte() {
return Ok(());
}
if LEGACY_RANGE.contains(&network_wire_byte) {
return Err(format!(
"Invalid network wire byte `{}` for network `{}`",
network_wire_byte, self
));
}

// Verify binned values
let valid = match self {
Network::MainNet => MAIN_NET_RANGE.contains(&network_wire_byte),
Network::StageNet => STAGE_NET_RANGE.contains(&network_wire_byte),
Network::NextNet => NEXT_NET_RANGE.contains(&network_wire_byte),
Network::LocalNet => LOCAL_NET_RANGE.contains(&network_wire_byte),
Network::Igor => IGOR_RANGE.contains(&network_wire_byte),
Network::Esmeralda => ESMERALDA_RANGE.contains(&network_wire_byte),
};
if !valid {
return Err(format!(
"Invalid network wire byte `{}` for network `{}`",
network_wire_byte, self
));
}
Ok(())
}
}

/// The default network for all applications
Expand Down Expand Up @@ -236,4 +305,104 @@ mod test {
assert_eq!(Network::try_from(0x24).unwrap(), Network::Igor);
assert_eq!(Network::try_from(0x26).unwrap(), Network::Esmeralda);
}

#[test]
fn test_as_wire_byte() {
for network in [
Network::MainNet,
Network::StageNet,
Network::NextNet,
Network::LocalNet,
Network::Igor,
Network::Esmeralda,
] {
assert!(network.verify_network_wire_byte_range(LIVENESS_WIRE_MODE).is_err());

let wire_byte = Network::as_wire_byte(network);
assert!(network.verify_network_wire_byte_range(wire_byte).is_ok());

for val in 0..255 {
match network {
Network::MainNet => {
if val == LIVENESS_WIRE_MODE {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if val == Network::MainNet.as_byte() {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if MAIN_NET_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else {
assert!(network.verify_network_wire_byte_range(val).is_err());
}
},
Network::StageNet => {
if val == LIVENESS_WIRE_MODE {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if val == Network::StageNet.as_byte() {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if STAGE_NET_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else {
assert!(network.verify_network_wire_byte_range(val).is_err());
}
},
Network::NextNet => {
if val == LIVENESS_WIRE_MODE {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if val == Network::NextNet.as_byte() {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if NEXT_NET_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else {
assert!(network.verify_network_wire_byte_range(val).is_err());
}
},
Network::LocalNet => {
if val == LIVENESS_WIRE_MODE {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if val == Network::LocalNet.as_byte() {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if LOCAL_NET_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else {
assert!(network.verify_network_wire_byte_range(val).is_err());
}
},
Network::Igor => {
if val == LIVENESS_WIRE_MODE {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if val == Network::Igor.as_byte() {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if IGOR_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else {
assert!(network.verify_network_wire_byte_range(val).is_err());
}
},
Network::Esmeralda => {
if val == LIVENESS_WIRE_MODE {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if val == Network::Esmeralda.as_byte() {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_err());
} else if ESMERALDA_RANGE.contains(&val) {
assert!(network.verify_network_wire_byte_range(val).is_ok());
} else {
assert!(network.verify_network_wire_byte_range(val).is_err());
}
},
}
}
}
}
}
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