From 440cb78098adbe4ecbc620aa31b6e52ee97d8f50 Mon Sep 17 00:00:00 2001 From: JKincorperated <73645805+JKincorperated@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:43:49 +0000 Subject: [PATCH] Add `version()` to the `lighthouse_version` crate and make the `enr.rs` file use it. --- .../lighthouse_network/src/discovery/enr.rs | 19 ++++++------ common/lighthouse_version/src/lib.rs | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/beacon_node/lighthouse_network/src/discovery/enr.rs b/beacon_node/lighthouse_network/src/discovery/enr.rs index 3e107ac5f4e..7e1022d9164 100644 --- a/beacon_node/lighthouse_network/src/discovery/enr.rs +++ b/beacon_node/lighthouse_network/src/discovery/enr.rs @@ -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; @@ -191,15 +191,14 @@ pub fn build_enr( // Add EIP 7636 client information if config.send_eip_7636_info { - let version = VERSION.split("/").collect::>(); - 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::>() + .first() + .unwrap_or(&"Lighthouse") + .to_string(); + + builder.client_info(name, version().to_string(), None); } // Add QUIC fields to the ENR. diff --git a/common/lighthouse_version/src/lib.rs b/common/lighthouse_version/src/lib.rs index 0751bdadff5..7d466afacbc 100644 --- a/common/lighthouse_version/src/lib.rs +++ b/common/lighthouse_version/src/lib.rs @@ -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::*; @@ -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() + ); + } }