From c9583ddd14e7919a64a119ff13e1e01fc4349196 Mon Sep 17 00:00:00 2001 From: Davidson Souza Date: Wed, 7 Aug 2024 19:44:20 -0300 Subject: [PATCH] add build.rs to make versioning --- crates/floresta-common/src/constants.rs | 10 ----- crates/floresta-common/src/lib.rs | 1 - .../src/electrum_protocol.rs | 9 +++-- crates/floresta-wire/src/p2p_wire/mod.rs | 3 ++ crates/floresta-wire/src/p2p_wire/node.rs | 6 +++ crates/floresta-wire/src/p2p_wire/peer.rs | 19 ++++----- .../floresta-wire/src/p2p_wire/tests/utils.rs | 1 + florestad/build.rs | 40 +++++++++++++++++++ florestad/src/cli.rs | 5 ++- florestad/src/florestad.rs | 6 ++- florestad/src/main.rs | 1 + 11 files changed, 72 insertions(+), 29 deletions(-) delete mode 100644 crates/floresta-common/src/constants.rs create mode 100644 florestad/build.rs diff --git a/crates/floresta-common/src/constants.rs b/crates/floresta-common/src/constants.rs deleted file mode 100644 index 7be95e84..00000000 --- a/crates/floresta-common/src/constants.rs +++ /dev/null @@ -1,10 +0,0 @@ -//! This module contains some common strings for versioning - -/// The default name of our data dir -pub const DIR_NAME: &str = ".floresta"; -/// Version of rust-bitcoin we are using. If we bump it on Cargo.toml, should change here too -pub const RUST_BITCOIN_VERSION: &str = "0.29.3"; -/// Version of rustreexo we use -pub const RUSTREEXO_VERSION: &str = "0.1.0"; -/// Our own version -pub const FLORESTA_VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/crates/floresta-common/src/lib.rs b/crates/floresta-common/src/lib.rs index 3557b5dc..11690093 100644 --- a/crates/floresta-common/src/lib.rs +++ b/crates/floresta-common/src/lib.rs @@ -7,7 +7,6 @@ use miniscript::Descriptor; #[cfg(feature = "descriptors")] use miniscript::DescriptorPublicKey; use sha2::Digest; -pub mod constants; pub mod spsc; use prelude::*; diff --git a/crates/floresta-electrum/src/electrum_protocol.rs b/crates/floresta-electrum/src/electrum_protocol.rs index 8e5fdce4..604ad590 100644 --- a/crates/floresta-electrum/src/electrum_protocol.rs +++ b/crates/floresta-electrum/src/electrum_protocol.rs @@ -952,8 +952,7 @@ mod test { get_spk_hash(&transaction.output[0].script_pubkey), ); - let cache = Arc::new(RwLock::new(cache)); - cache + Arc::new(RwLock::new(cache)) } fn get_test_address() -> (Address, sha256::Hash) { @@ -1019,6 +1018,7 @@ mod test { assume_utreexo: None, backfill: false, filter_start_height: None, + user_agent: "floresta".to_string(), }; let chain_provider: UtreexoNode>> = @@ -1365,6 +1365,9 @@ mod test { ); assert!(batch_response[4]["result"].as_array().unwrap().is_empty()); assert!(batch_response[5]["result"].is_null()); - assert_eq!(batch_response[6]["result"][0], "Floresta 0.1.0".to_string()); + assert_eq!( + batch_response[6]["result"][0], + format!("Floresta {}", env!("CARGO_PKG_VERSION")) + ); } } diff --git a/crates/floresta-wire/src/p2p_wire/mod.rs b/crates/floresta-wire/src/p2p_wire/mod.rs index d490d277..986b0b56 100644 --- a/crates/floresta-wire/src/p2p_wire/mod.rs +++ b/crates/floresta-wire/src/p2p_wire/mod.rs @@ -64,6 +64,8 @@ pub struct UtreexoNodeConfig { /// tip is at height 1000, and we set this value to -100, we will start downloading filters /// from height 900. pub filter_start_height: Option, + /// The user agent that we will advertise to our peers. Defaults to `floresta:`. + pub user_agent: String, } impl Default for UtreexoNodeConfig { @@ -81,6 +83,7 @@ impl Default for UtreexoNodeConfig { backfill: false, assume_utreexo: None, filter_start_height: None, + user_agent: format!("floresta:{}", env!("CARGO_PKG_VERSION")), } } } diff --git a/crates/floresta-wire/src/p2p_wire/node.rs b/crates/floresta-wire/src/p2p_wire/node.rs index df8614b7..a8a1bc30 100644 --- a/crates/floresta-wire/src/p2p_wire/node.rs +++ b/crates/floresta-wire/src/p2p_wire/node.rs @@ -724,6 +724,7 @@ where mempool: Arc>, network: bitcoin::Network, node_tx: Sender, + user_agent: String, ) -> impl Future + Send + 'static { Peer::::create_outbound_connection( peer_id_count, @@ -734,6 +735,7 @@ where requests_rx, peer_id, feeler, + user_agent, ) } /// Opens a connection through a socks5 interface @@ -748,6 +750,7 @@ where address: LocalAddress, requests_rx: Receiver, peer_id_count: u32, + user_agent: String, ) -> Result<(), Socks5Error> { let addr = match address.get_address() { AddrV2::Cjdns(addr) => Socks5Addr::Ipv6(addr), @@ -772,6 +775,7 @@ where requests_rx, peer_id, feeler, + user_agent, ); Ok(()) } @@ -799,6 +803,7 @@ where address.clone(), requests_rx, self.peer_id_count, + self.config.user_agent.clone(), ), )); } else { @@ -811,6 +816,7 @@ where self.mempool.clone(), self.network.into(), self.node_tx.clone(), + self.config.user_agent.clone(), )); } diff --git a/crates/floresta-wire/src/p2p_wire/peer.rs b/crates/floresta-wire/src/p2p_wire/peer.rs index d67ef773..6f6b16fb 100644 --- a/crates/floresta-wire/src/p2p_wire/peer.rs +++ b/crates/floresta-wire/src/p2p_wire/peer.rs @@ -92,7 +92,9 @@ pub struct Peer { feeler: bool, wants_addrv2: bool, shutdown: bool, + our_user_agent: String, } + #[derive(Debug, Error)] pub enum PeerError { #[error("Error while sending to peer")] @@ -137,7 +139,7 @@ impl Peer { } async fn peer_loop_inner(&mut self) -> Result<()> { // send a version - let version = peer_utils::build_version_message(); + let version = peer_utils::build_version_message(self.our_user_agent.clone()); self.write(version).await?; self.state = State::SentVersion(Instant::now()); let read_stream = BufReader::new(self.stream.clone()); @@ -481,6 +483,7 @@ impl Peer { node_requests: Receiver, address_id: usize, feeler: bool, + our_user_agent: String, ) { let peer = Peer { address_id, @@ -503,6 +506,7 @@ impl Peer { feeler, wants_addrv2: false, shutdown: false, + our_user_agent, }; spawn(peer.read_loop()); } @@ -517,6 +521,7 @@ impl Peer { node_requests: Receiver, address_id: usize, feeler: bool, + our_user_agent: String, ) { let stream = async_std::future::timeout(Duration::from_secs(10), TcpStream::connect(address)).await; @@ -551,6 +556,7 @@ impl Peer { feeler, wants_addrv2: false, shutdown: false, + our_user_agent, }; spawn(peer.read_loop()); } @@ -587,9 +593,6 @@ pub(super) mod peer_utils { use bitcoin::p2p::message::NetworkMessage; use bitcoin::p2p::message::{self}; use bitcoin::p2p::message_network; - use floresta_common::constants::FLORESTA_VERSION; - use floresta_common::constants::RUSTREEXO_VERSION; - use floresta_common::constants::RUST_BITCOIN_VERSION; /// Protocol version we speak pub const PROTOCOL_VERSION: u32 = 70016; @@ -598,7 +601,7 @@ pub(super) mod peer_utils { NetworkMessage::Pong(nonce) } - pub(crate) fn build_version_message() -> message::NetworkMessage { + pub(crate) fn build_version_message(user_agent: String) -> message::NetworkMessage { use bitcoin::p2p::ServiceFlags; // Building version message, see https://en.bitcoin.it/wiki/Protocol_documentation#version @@ -622,12 +625,6 @@ pub(super) mod peer_utils { // "Node random nonce, randomly generated every time a version packet is sent. This nonce is used to detect connections to self." let nonce: u64 = 1; - // "User Agent (0x00 if string is 0 bytes long)" - let user_agent = format!( - "/rust-bitcoin:{}/rustreexo:{}/Floresta:{}/", - RUST_BITCOIN_VERSION, RUSTREEXO_VERSION, FLORESTA_VERSION - ); - // "The last block received by the emitting node" let start_height: i32 = 0; diff --git a/crates/floresta-wire/src/p2p_wire/tests/utils.rs b/crates/floresta-wire/src/p2p_wire/tests/utils.rs index 1a581ee8..d23a2d7d 100644 --- a/crates/floresta-wire/src/p2p_wire/tests/utils.rs +++ b/crates/floresta-wire/src/p2p_wire/tests/utils.rs @@ -172,6 +172,7 @@ pub fn get_node_config( assume_utreexo: None, backfill: false, filter_start_height: None, + user_agent: "node_test".to_string(), } } diff --git a/florestad/build.rs b/florestad/build.rs new file mode 100644 index 00000000..f4fc71de --- /dev/null +++ b/florestad/build.rs @@ -0,0 +1,40 @@ +fn main() { + let git_description = std::process::Command::new("git") + .args(["describe", "--tags", "--always", "--dirty"]) + .output() + .map(|output| { + assert!( + output.status.success(), + "Failed to run git describe. Is git installed?" + ); + let mut git_description = String::from_utf8(output.stdout).unwrap(); + git_description.pop(); // remove the trailing newline + git_description + }) + .expect("Failed to run git describe. Is git installed?"); + + let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(); + let os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); + let runtime = std::env::var("CARGO_CFG_TARGET_ENV").unwrap(); + + let rustc = std::process::Command::new("rustc") + .args(["--version"]) + .output() + .map(|output| { + assert!( + output.status.success(), + "Failed to run rustc --version. Is rustc installed?" + ); + String::from_utf8(output.stdout).expect("Failed to parse rustc version") + }) + .expect("Failed to run rustc --version. Is rustc installed?"); + + let version = + format!("version {git_description} compiled for {arch}-{os}-{runtime} with {rustc}"); + println!("cargo:rustc-env=LONG_VERSION={}", version); + println!("cargo:rustc-env=GIT_DESCRIBE={}", git_description); + + // re-run if either the build script or the git HEAD changes + println!("cargo:rerun-if-changed=../.git/HEAD"); + println!("cargo:rerun-if-changed=build.rs"); +} diff --git a/florestad/src/cli.rs b/florestad/src/cli.rs index c9e1bd85..8526edc4 100644 --- a/florestad/src/cli.rs +++ b/florestad/src/cli.rs @@ -49,8 +49,9 @@ impl std::fmt::Display for Network { #[derive(Parser)] #[command( author = "Davidson Souza", - version = env!("CARGO_PKG_VERSION"), - about = "florestad - a lightweight Bitcoin full node powered by Utreexo", + version = env!("GIT_DESCRIBE"), + about = "florestad - a lightweight Bitcoin client", + long_about = env!("LONG_VERSION"), )] pub struct Cli { #[arg(short, long, value_name = "FILE")] diff --git a/florestad/src/florestad.rs b/florestad/src/florestad.rs index fbcdeedd..90ee17ff 100644 --- a/florestad/src/florestad.rs +++ b/florestad/src/florestad.rs @@ -19,7 +19,6 @@ use floresta_chain::AssumeValidArg; use floresta_chain::BlockchainError; use floresta_chain::ChainState; use floresta_chain::KvChainStore; -use floresta_common::constants::DIR_NAME; use floresta_compact_filters::flat_filters_store::FlatFiltersStore; use floresta_compact_filters::network_filters::NetworkFilters; use floresta_electrum::electrum_protocol::client_accept_loop; @@ -135,6 +134,8 @@ pub struct Config { pub assume_utreexo: bool, /// Whether we should post debug information to the console pub debug: bool, + /// The user agent that we will advertise to our peers + pub user_agent: String, } pub struct Florestad { @@ -201,7 +202,7 @@ impl Florestad { format!( "{}/{}/", x.to_str().unwrap_or_default().to_owned(), - DIR_NAME, + ".floresta", ) }) }) @@ -344,6 +345,7 @@ impl Florestad { assume_utreexo, backfill: false, filter_start_height: self.config.filters_start_height, + user_agent: self.config.user_agent.clone(), }; // Chain Provider (p2p) diff --git a/florestad/src/main.rs b/florestad/src/main.rs index b2df4f23..35c7d69e 100644 --- a/florestad/src/main.rs +++ b/florestad/src/main.rs @@ -48,6 +48,7 @@ async fn main() { electrum_address: params.electrum_address, wallet_descriptor: params.wallet_descriptor, filters_start_height: params.filters_start_height, + user_agent: format!("floresta:{}", env!("GIT_DESCRIBE")), }; let florestad = Florestad::from(config);