From 2be9208caedcec6f26fbfa07950bd5934453001c Mon Sep 17 00:00:00 2001 From: rustaceanrob Date: Wed, 13 Aug 2025 12:54:54 +0100 Subject: [PATCH] Remove `bitcoin` from `network` The plan for `network` is to hold networking related tools that do not have anything to do with bitcoin itself, but are very useful for bitcoin in general. Removes `bitcoin` as a dependency and makes `tokio` optional. --- accumulator/Cargo.toml | 1 + network/Cargo.toml | 3 +-- network/src/dns.rs | 13 +++------- network/src/lib.rs | 57 +++++++++++++----------------------------- network/tests/test.rs | 1 + 5 files changed, 24 insertions(+), 51 deletions(-) diff --git a/accumulator/Cargo.toml b/accumulator/Cargo.toml index 75991ba..825ee15 100644 --- a/accumulator/Cargo.toml +++ b/accumulator/Cargo.toml @@ -7,4 +7,5 @@ edition = "2024" bitcoin = { workspace = true } [dev-dependencies] +bitcoin = { workspace = true, default-features = true } rusqlite = { version = "0.36.0", features = ["bundled"] } diff --git a/network/Cargo.toml b/network/Cargo.toml index bf791a0..bd127d3 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -4,7 +4,6 @@ version = "0.1.0" edition = "2024" [dependencies] -bitcoin = { workspace = true, features = ["rand-std"] } tokio = { version = "1", default-features = false, optional = true, features = [ "io-util", "net", @@ -14,5 +13,5 @@ tokio = { version = "1", default-features = false, optional = true, features = [ tokio = { version = "1", default-features = false, features = ["full"] } [features] -default = ["tokio"] +default = [] tokio = ["dep:tokio"] diff --git a/network/src/dns.rs b/network/src/dns.rs index eb4d71f..e190d8b 100644 --- a/network/src/dns.rs +++ b/network/src/dns.rs @@ -4,10 +4,7 @@ use std::{ net::{IpAddr, Ipv4Addr, SocketAddr}, }; -use bitcoin::secp256k1::rand::RngCore; -use bitcoin::secp256k1::rand::thread_rng; - -use crate::encode_qname; +use crate::{encode_qname, rand_bytes}; const CLOUDFLARE: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1)), 53); @@ -43,9 +40,7 @@ pub struct DnsQuery { impl DnsQuery { pub fn new(seed: &str, dns_resolver: SocketAddr) -> Self { // Build a header - let mut rng = thread_rng(); - let mut message_id = [0, 0]; - rng.fill_bytes(&mut message_id); + let message_id = rand_bytes(); let mut message = message_id.to_vec(); message.extend(RECURSIVE_FLAGS); message.push(0x00); // QDCOUNT @@ -63,9 +58,7 @@ impl DnsQuery { } pub fn new_cloudflare(seed: &str) -> Self { - let mut rng = thread_rng(); - let mut message_id = [0, 0]; - rng.fill_bytes(&mut message_id); + let message_id = rand_bytes(); let mut message = message_id.to_vec(); message.extend(RECURSIVE_FLAGS); message.push(0x00); // QDCOUNT diff --git a/network/src/lib.rs b/network/src/lib.rs index 3862af2..e2bca0d 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -1,4 +1,7 @@ -use bitcoin::{Network, TestnetVersion}; +use std::{ + hash::{DefaultHasher, Hash, Hasher}, + time::SystemTime, +}; pub mod dns; @@ -18,46 +21,22 @@ fn encode_qname>(hostname: S, filter: Option) -> Vec { qname } -pub trait PortExt { - fn port(&self) -> u16; +fn rand_bytes() -> [u8; 2] { + let mut hasher = DefaultHasher::new(); + SystemTime::now().hash(&mut hasher); + let mut hash = hasher.finish(); + hash ^= hash << 13; + hash ^= hash >> 17; + hash ^= hash << 5; + hash.to_be_bytes()[..2].try_into().expect("trivial cast") } -impl PortExt for Network { - fn port(&self) -> u16 { - match self { - Self::Signet => 38333, - Self::Bitcoin => 8333, - Self::Regtest => 18444, - Self::Testnet(TestnetVersion::V3) => 48333, - Self::Testnet(TestnetVersion::V4) => 48333, - _ => unreachable!(), - } - } -} - -pub trait SeedsExt { - fn dns_seeds(&self) -> Vec<&str>; -} +#[cfg(test)] +mod test { + use crate::rand_bytes; -impl SeedsExt for Network { - fn dns_seeds(&self) -> Vec<&str> { - match self { - Self::Bitcoin => vec![ - "seed.bitcoin.sipa.be", - "dnsseed.bluematt.me", - "dnsseed.bitcoin.dashjr.org", - "seed.bitcoinstats.com", - "seed.bitcoin.jonasschnelli.ch", - "seed.btc.petertodd.org", - "seed.bitcoin.sprovoost.nl", - "dnsseed.emzy.de", - "seed.bitcoin.wiz.biz", - ], - Self::Signet => vec![ - "seed.signet.bitcoin.sprovoost.nl", - "seed.signet.achownodes.xyz", - ], - _ => unimplemented!(), - } + #[test] + fn test_rand_bytes() { + rand_bytes(); } } diff --git a/network/tests/test.rs b/network/tests/test.rs index 98b152e..887e6dd 100644 --- a/network/tests/test.rs +++ b/network/tests/test.rs @@ -3,6 +3,7 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use peers::dns::DnsQuery; #[tokio::test] +#[cfg(feature = "tokio")] async fn test_tokio_dns_ext() { use peers::dns::TokioDnsExt; let resolver = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(46, 166, 189, 67)), 53);