Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
hansieodendaal committed Sep 2, 2024
1 parent c839e89 commit c79823f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
21 changes: 21 additions & 0 deletions base_layer/p2p/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
31 changes: 19 additions & 12 deletions common/src/configuration/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ 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;
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -316,15 +321,17 @@ 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());

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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
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;
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 @@ -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
Expand Down

0 comments on commit c79823f

Please sign in to comment.