Skip to content

Commit

Permalink
feat: add network wire byte (#6518)
Browse files Browse the repository at this point in the history
Description
---
Added network wire byte. This is now independent of the network as byte.

Note: This method in favour of #6502.

Motivation and Context
---
Base nodes and wallets maintained numerous connections from which they
could not sync or obtain useful information. This caused base nodes to
try and follow other base nodes that were on a higher proof of work but
on a different genesis block, and caused wallets to query base nodes
that supplied information from a different blockchain. This byte can now
be changed every time the genesis block changes.

How Has This Been Tested?
---
Passed all unit tests.

What process can a PR reviewer use to test or verify this change?
---

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [X] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
  • Loading branch information
hansieodendaal authored Sep 2, 2024
1 parent c1b5c02 commit 78a4803
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 9 deletions.
13 changes: 12 additions & 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 Expand Up @@ -602,3 +602,14 @@ impl ServiceInitializer for P2pInitializer {
Ok(())
}
}

#[cfg(test)]
mod test {
use tari_common::configuration::Network;
use tari_comms::connection_manager::WireMode;
#[test]
fn self_liveness_network_wire_byte_is_consistent() {
let wire_mode = WireMode::Liveness;
assert_eq!(wire_mode.as_byte(), Network::RESERVED_WIRE_BYTE);
}
}
174 changes: 174 additions & 0 deletions common/src/configuration/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub enum Network {
}

impl Network {
/// The reserved wire byte for liveness ('LIVENESS_WIRE_MODE')
pub const RESERVED_WIRE_BYTE: u8 = 0xa7;

pub fn get_current_or_user_setting_or_default() -> Self {
match CURRENT_NETWORK.get() {
Some(&network) => network,
Expand Down Expand Up @@ -86,6 +89,30 @@ 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 reserved wire byte for liveness ('LIVENESS_WIRE_MODE') is defined in another module, which is not
// accessible from here.
debug_assert!(wire_byte != Network::RESERVED_WIRE_BYTE);
wire_byte
}
}

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

// Do not change these ranges
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];

/// Helper function to verify the network wire byte range
pub fn verify_network_wire_byte_range(network_wire_byte: u8, network: Network) -> Result<(), String> {
if network_wire_byte == Network::RESERVED_WIRE_BYTE {
return Err(format!(
"Invalid network wire byte, cannot be '{}', reserved for 'LIVENESS_WIRE_MODE'",
Network::RESERVED_WIRE_BYTE
));
}

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

// Verify binned values
let valid = match network {
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, network
));
}
Ok(())
}

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

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

for val in 0..255 {
match network {
Network::MainNet => {
if val == Network::RESERVED_WIRE_BYTE {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if val == Network::MainNet.as_byte() {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if MAIN_NET_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else {
assert!(verify_network_wire_byte_range(val, network).is_err());
}
},
Network::StageNet => {
if val == Network::RESERVED_WIRE_BYTE {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if val == Network::StageNet.as_byte() {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if STAGE_NET_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else {
assert!(verify_network_wire_byte_range(val, network).is_err());
}
},
Network::NextNet => {
if val == Network::RESERVED_WIRE_BYTE {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if val == Network::NextNet.as_byte() {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if NEXT_NET_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else {
assert!(verify_network_wire_byte_range(val, network).is_err());
}
},
Network::LocalNet => {
if val == Network::RESERVED_WIRE_BYTE {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if val == Network::LocalNet.as_byte() {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if LOCAL_NET_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else {
assert!(verify_network_wire_byte_range(val, network).is_err());
}
},
Network::Igor => {
if val == Network::RESERVED_WIRE_BYTE {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if val == Network::Igor.as_byte() {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if IGOR_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else {
assert!(verify_network_wire_byte_range(val, network).is_err());
}
},
Network::Esmeralda => {
if val == Network::RESERVED_WIRE_BYTE {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if val == Network::Esmeralda.as_byte() {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else if LEGACY_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_err());
} else if ESMERALDA_RANGE.contains(&val) {
assert!(verify_network_wire_byte_range(val, network).is_ok());
} else {
assert!(verify_network_wire_byte_range(val, network).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
1 change: 1 addition & 0 deletions comms/core/src/connection_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub(crate) use self_liveness::SelfLivenessCheck;
pub use self_liveness::SelfLivenessStatus;

mod wire_mode;
pub use wire_mode::WireMode;

#[cfg(test)]
mod tests;
4 changes: 2 additions & 2 deletions comms/core/src/protocol/network_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub struct NodeNetworkInfo {
/// NOT reject the connection if a remote peer advertises a different minor version number.
pub minor_version: u8,
/// 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.
/// be established. This byte cannot be `LIVENESS_WIRE_MODE` (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

0 comments on commit 78a4803

Please sign in to comment.