diff --git a/base_layer/p2p/src/initialization.rs b/base_layer/p2p/src/initialization.rs index f46e5f7410..674b3cbaa0 100644 --- a/base_layer/p2p/src/initialization.rs +++ b/base_layer/p2p/src/initialization.rs @@ -602,3 +602,24 @@ 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); + for network in [ + Network::MainNet, + Network::StageNet, + Network::NextNet, + Network::LocalNet, + Network::Igor, + Network::Esmeralda, + ] { + assert!(network.verify_network_wire_byte_range(wire_mode.as_byte()).is_err()); + } + } +} diff --git a/common/src/configuration/network.rs b/common/src/configuration/network.rs index 5854b8514d..1f38a86e2c 100644 --- a/common/src/configuration/network.rs +++ b/common/src/configuration/network.rs @@ -32,7 +32,6 @@ use serde::{Deserialize, Serialize}; use crate::ConfigurationError; -const LIVENESS_WIRE_MODE: u8 = 0xa7; const MAIN_NET_RANGE: std::ops::Range = 0..40; const STAGE_NET_RANGE: std::ops::Range = 40..80; const NEXT_NET_RANGE: std::ops::Range = 80..120; @@ -58,6 +57,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, @@ -120,11 +122,14 @@ impl Network { wire_byte } - // Helper function to verify the network wire byte range - fn verify_network_wire_byte_range(self, network_wire_byte: u8) -> Result<(), String> { + /// Helper function to verify the network wire byte range + pub 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()); + 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 @@ -316,7 +321,9 @@ mod test { Network::Igor, Network::Esmeralda, ] { - assert!(network.verify_network_wire_byte_range(LIVENESS_WIRE_MODE).is_err()); + assert!(network + .verify_network_wire_byte_range(Network::RESERVED_WIRE_BYTE) + .is_err()); let wire_byte = Network::as_wire_byte(network); assert!(network.verify_network_wire_byte_range(wire_byte).is_ok()); @@ -324,7 +331,7 @@ mod test { for val in 0..255 { match network { Network::MainNet => { - if val == LIVENESS_WIRE_MODE { + if val == Network::RESERVED_WIRE_BYTE { 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()); @@ -337,7 +344,7 @@ mod test { } }, Network::StageNet => { - if val == LIVENESS_WIRE_MODE { + if val == Network::RESERVED_WIRE_BYTE { 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()); @@ -350,7 +357,7 @@ mod test { } }, Network::NextNet => { - if val == LIVENESS_WIRE_MODE { + if val == Network::RESERVED_WIRE_BYTE { 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()); @@ -363,7 +370,7 @@ mod test { } }, Network::LocalNet => { - if val == LIVENESS_WIRE_MODE { + if val == Network::RESERVED_WIRE_BYTE { 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()); @@ -376,7 +383,7 @@ mod test { } }, Network::Igor => { - if val == LIVENESS_WIRE_MODE { + if val == Network::RESERVED_WIRE_BYTE { 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()); @@ -389,7 +396,7 @@ mod test { } }, Network::Esmeralda => { - if val == LIVENESS_WIRE_MODE { + if val == Network::RESERVED_WIRE_BYTE { 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()); diff --git a/comms/core/src/connection_manager/mod.rs b/comms/core/src/connection_manager/mod.rs index 3ca92fe339..beb68a4995 100644 --- a/comms/core/src/connection_manager/mod.rs +++ b/comms/core/src/connection_manager/mod.rs @@ -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; diff --git a/comms/core/src/protocol/network_info.rs b/comms/core/src/protocol/network_info.rs index 2961b62b16..ddcb0551cb 100644 --- a/comms/core/src/protocol/network_info.rs +++ b/comms/core/src/protocol/network_info.rs @@ -30,7 +30,7 @@ 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_wire_byte: u8, /// The user agent string for this node