Skip to content

Commit

Permalink
Add version() to the lighthouse_version crate and make the `enr.r…
Browse files Browse the repository at this point in the history
…s` file use it.
  • Loading branch information
JKincorperated committed Jan 12, 2025
1 parent 8124e5c commit 440cb78
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
19 changes: 9 additions & 10 deletions beacon_node/lighthouse_network/src/discovery/enr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::types::{Enr, EnrAttestationBitfield, EnrSyncCommitteeBitfield};
use crate::NetworkConfig;
use alloy_rlp::bytes::Bytes;
use libp2p::identity::Keypair;
use lighthouse_version::VERSION;
use lighthouse_version::{version, VERSION};
use slog::{debug, warn};
use ssz::{Decode, Encode};
use ssz_types::BitVector;
Expand Down Expand Up @@ -191,15 +191,14 @@ pub fn build_enr<E: EthSpec>(

// Add EIP 7636 client information
if config.send_eip_7636_info {
let version = VERSION.split("/").collect::<Vec<&str>>();
builder.client_info(
version.first().unwrap_or(&"Lighthouse").to_string(),
version
.get(1)
.unwrap_or(&env!("CARGO_PKG_VERSION"))
.to_string(),
None,
);
let name = VERSION
.split("/")
.collect::<Vec<&str>>()
.first()
.unwrap_or(&"Lighthouse")
.to_string();

builder.client_info(name, version().to_string(), None);
}

// Add QUIC fields to the ENR.
Expand Down
29 changes: 29 additions & 0 deletions common/lighthouse_version/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ pub fn version_with_platform() -> String {
format!("{}/{}-{}", VERSION, Target::arch(), Target::os())
}

/// Returns semantic versioning information only.
///
/// ## Example
///
/// `1.5.1`
pub fn version() -> &'static str {
let mut out = "";
let mut start = 0;
for (i, c) in VERSION.chars().enumerate() {
if c == '-' {
break;
} else if c == 'v' {
start = i + 1;
}
out = &VERSION[start..i + 1];
}
out
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -64,4 +83,14 @@ mod test {
VERSION
);
}

#[test]
fn semantic_version_formatting() {
let re = Regex::new(r"^[0-9]+\.[0-9]+\.[0-9]+").unwrap();
assert!(
re.is_match(version()),
"semantic version doesn't match regex: {}",
version()
);
}
}

0 comments on commit 440cb78

Please sign in to comment.