Skip to content

Commit

Permalink
fix(protocol): evaluate NETWORK_VERSION_MODE at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef authored and RolandSherwin committed Apr 8, 2024
1 parent 54223b4 commit 71ea622
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion sn_peers_acquisition/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ lazy_static! {
// URL containing the multi-addresses of the bootstrap nodes.
pub static ref NETWORK_CONTACTS_URL: String = {
let version = get_network_version();
let version_prefix = if !version.is_empty() { format!("{version}-") } else { version };
let version_prefix = if !version.is_empty() { format!("{version}-") } else { version.to_string() };
format!("https://sn-testnet.s3.eu-west-2.amazonaws.com/{version_prefix}network-contacts")
};
}
Expand Down
18 changes: 8 additions & 10 deletions sn_protocol/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

use lazy_static::lazy_static;

/// Set this env variable to provide custom network versioning. If it is set to 'restricted', then the git branch name
/// is used as the version string. Else we directly use the passed in string as the version.
const NETWORK_VERSION_MODE_ENV_VARIABLE: &str = "NETWORK_VERSION_MODE";

lazy_static! {
/// The node version used during Identify Behaviour.
pub static ref IDENTIFY_NODE_VERSION_STR: String =
Expand Down Expand Up @@ -53,24 +49,26 @@ lazy_static! {
/// If the network version mode env variable is set to `restricted`, then the git branch is used as the version.
/// Else any non empty string is used as the version string.
/// If the env variable is empty or not set, then we do not apply any network versioning.
pub fn get_network_version() -> String {
match std::env::var(NETWORK_VERSION_MODE_ENV_VARIABLE) {
Ok(value) if !value.is_empty() => {
pub fn get_network_version() -> &'static str {
// Set this env variable to provide custom network versioning. If it is set to 'restricted', then the git branch name
// is used as the version string. Else we directly use the passed in string as the version.
match option_env!("NETWORK_VERSION_MODE") {
Some(value) => {
if value == "restricted" {
sn_build_info::git_branch().to_string()
sn_build_info::git_branch()
} else {
value
}
}
_ => "".to_string(),
_ => "",
}
}

/// Helper to write the network version with `/` appended if it is not empty
fn write_network_version_with_slash() -> String {
let version = get_network_version();
if version.is_empty() {
version
version.to_string()
} else {
format!("/{version}")
}
Expand Down

0 comments on commit 71ea622

Please sign in to comment.