Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add build.rs to make versioning #216

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions crates/floresta-common/src/constants.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/floresta-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use miniscript::Descriptor;
#[cfg(feature = "descriptors")]
use miniscript::DescriptorPublicKey;
use sha2::Digest;
pub mod constants;
pub mod spsc;

use prelude::*;
Expand Down
9 changes: 6 additions & 3 deletions crates/floresta-electrum/src/electrum_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NetworkUnchecked>, sha256::Hash) {
Expand Down Expand Up @@ -1019,6 +1018,7 @@ mod test {
assume_utreexo: None,
backfill: false,
filter_start_height: None,
user_agent: "floresta".to_string(),
};

let chain_provider: UtreexoNode<RunningNode, Arc<ChainState<KvChainStore>>> =
Expand Down Expand Up @@ -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"))
);
}
}
3 changes: 3 additions & 0 deletions crates/floresta-wire/src/p2p_wire/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>,
/// The user agent that we will advertise to our peers. Defaults to `floresta:<version>`.
pub user_agent: String,
}

impl Default for UtreexoNodeConfig {
Expand All @@ -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")),
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/floresta-wire/src/p2p_wire/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ where
mempool: Arc<RwLock<Mempool>>,
network: bitcoin::Network,
node_tx: Sender<NodeNotification>,
user_agent: String,
) -> impl Future<Output = ()> + Send + 'static {
Peer::<TcpStream>::create_outbound_connection(
peer_id_count,
Expand All @@ -734,6 +735,7 @@ where
requests_rx,
peer_id,
feeler,
user_agent,
)
}
/// Opens a connection through a socks5 interface
Expand All @@ -748,6 +750,7 @@ where
address: LocalAddress,
requests_rx: Receiver<NodeRequest>,
peer_id_count: u32,
user_agent: String,
) -> Result<(), Socks5Error> {
let addr = match address.get_address() {
AddrV2::Cjdns(addr) => Socks5Addr::Ipv6(addr),
Expand All @@ -772,6 +775,7 @@ where
requests_rx,
peer_id,
feeler,
user_agent,
);
Ok(())
}
Expand Down Expand Up @@ -799,6 +803,7 @@ where
address.clone(),
requests_rx,
self.peer_id_count,
self.config.user_agent.clone(),
),
));
} else {
Expand All @@ -811,6 +816,7 @@ where
self.mempool.clone(),
self.network.into(),
self.node_tx.clone(),
self.config.user_agent.clone(),
));
}

Expand Down
19 changes: 8 additions & 11 deletions crates/floresta-wire/src/p2p_wire/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ pub struct Peer<T: Transport> {
feeler: bool,
wants_addrv2: bool,
shutdown: bool,
our_user_agent: String,
}

#[derive(Debug, Error)]
pub enum PeerError {
#[error("Error while sending to peer")]
Expand Down Expand Up @@ -137,7 +139,7 @@ impl<T: Transport> Peer<T> {
}
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());
Expand Down Expand Up @@ -481,6 +483,7 @@ impl<T: Transport> Peer<T> {
node_requests: Receiver<NodeRequest>,
address_id: usize,
feeler: bool,
our_user_agent: String,
) {
let peer = Peer {
address_id,
Expand All @@ -503,6 +506,7 @@ impl<T: Transport> Peer<T> {
feeler,
wants_addrv2: false,
shutdown: false,
our_user_agent,
};
spawn(peer.read_loop());
}
Expand All @@ -517,6 +521,7 @@ impl<T: Transport> Peer<T> {
node_requests: Receiver<NodeRequest>,
address_id: usize,
feeler: bool,
our_user_agent: String,
) {
let stream =
async_std::future::timeout(Duration::from_secs(10), TcpStream::connect(address)).await;
Expand Down Expand Up @@ -551,6 +556,7 @@ impl<T: Transport> Peer<T> {
feeler,
wants_addrv2: false,
shutdown: false,
our_user_agent,
};
spawn(peer.read_loop());
}
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;

Expand Down
1 change: 1 addition & 0 deletions crates/floresta-wire/src/p2p_wire/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ pub fn get_node_config(
assume_utreexo: None,
backfill: false,
filter_start_height: None,
user_agent: "node_test".to_string(),
}
}

Expand Down
40 changes: 40 additions & 0 deletions florestad/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
5 changes: 3 additions & 2 deletions florestad/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
6 changes: 4 additions & 2 deletions florestad/src/florestad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -201,7 +202,7 @@ impl Florestad {
format!(
"{}/{}/",
x.to_str().unwrap_or_default().to_owned(),
DIR_NAME,
".floresta",
)
})
})
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions florestad/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading