use std::sync::{LazyLock, RwLock};
pub const MAINNET_ID: u8 = 1;
pub const ALPHANET_ID: u8 = 2;
pub static NETWORK_ID: LazyLock<RwLock<u8>> = LazyLock::new(|| RwLock::new(1));
pub fn construct_node_user_agent(node_version: String) -> String {
format!(
"ant/node/{ant_protocol_version}/{node_version}/{network_id}",
ant_protocol_version = get_truncate_version_str(),
network_id = get_network_id(),
)
}
pub fn construct_client_user_agent(client_version: String) -> String {
format!(
"ant/client/{ant_protocol_version}/{client_version}/{network_id}",
ant_protocol_version = get_truncate_version_str(),
network_id = get_network_id(),
)
}
pub static REQ_RESPONSE_VERSION_STR: LazyLock<RwLock<String>> = LazyLock::new(|| {
RwLock::new(format!(
"/ant/{ant_protocol_version}/{network_id}",
ant_protocol_version = get_truncate_version_str(),
network_id = get_network_id()
))
});
pub static IDENTIFY_PROTOCOL_STR: LazyLock<RwLock<String>> = LazyLock::new(|| {
RwLock::new(format!(
"ant/{ant_protocol_version}/{network_id}",
ant_protocol_version = get_truncate_version_str(),
network_id = get_network_id(),
))
});
pub fn set_network_id(id: u8) {
info!("Setting network id to: {id}");
{
let mut network_id = NETWORK_ID
.write()
.expect("Failed to obtain write lock for NETWORK_ID");
*network_id = id;
}
{
let mut req_response = REQ_RESPONSE_VERSION_STR
.write()
.expect("Failed to obtain write lock for REQ_RESPONSE_VERSION_STR");
*req_response = format!("/ant/{}/{}", get_truncate_version_str(), id);
}
{
let mut identify_protocol = IDENTIFY_PROTOCOL_STR
.write()
.expect("Failed to obtain write lock for IDENTIFY_PROTOCOL_STR");
*identify_protocol = format!("ant/{}/{}", get_truncate_version_str(), id);
}
info!("Network id set to: {id} and all protocol strings updated");
}
pub fn get_network_id() -> u8 {
*NETWORK_ID
.read()
.expect("Failed to obtain read lock for NETWORK_ID")
}
pub fn get_network_id_str() -> String {
format!(
"{}",
*NETWORK_ID
.read()
.expect("Failed to obtain read lock for NETWORK_ID")
)
}
pub fn get_truncate_version_str() -> String {
let version_str = env!("CARGO_PKG_VERSION").to_string();
let parts = version_str.split('.').collect::<Vec<_>>();
if parts.len() >= 2 {
format!("{}.{}", parts[0], parts[1])
} else {
panic!("Cannot obtain truncated version str for {version_str:?}: {parts:?}");
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_print_version_strings() -> Result<(), Box<dyn std::error::Error>> {
set_network_id(3);
println!(
"\nNode user agent: {}",
construct_node_user_agent("1.0.0".to_string())
);
println!(
"Client user agent: {}",
construct_client_user_agent("1.0.0".to_string())
);
println!(
"REQ_RESPONSE_VERSION_STR: {}",
*REQ_RESPONSE_VERSION_STR.read().expect(
"Failed to
obtain read lock for REQ_RESPONSE_VERSION_STR"
)
);
println!(
"IDENTIFY_PROTOCOL_STR: {}",
*IDENTIFY_PROTOCOL_STR
.read()
.expect("Failed to obtain read lock for IDENTIFY_PROTOCOL_STR")
);
let truncated = get_truncate_version_str();
println!("\nTruncated version: {truncated}");
let network_id = get_network_id_str();
println!("Network ID string: {network_id}");
Ok(())
}
}