Skip to content
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
1 change: 1 addition & 0 deletions accumulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ edition = "2024"
bitcoin = { workspace = true }

[dev-dependencies]
bitcoin = { workspace = true, default-features = true }
rusqlite = { version = "0.36.0", features = ["bundled"] }
3 changes: 1 addition & 2 deletions network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"]
13 changes: 3 additions & 10 deletions network/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
57 changes: 18 additions & 39 deletions network/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use bitcoin::{Network, TestnetVersion};
use std::{
hash::{DefaultHasher, Hash, Hasher},
time::SystemTime,
};

pub mod dns;

Expand All @@ -18,46 +21,22 @@ fn encode_qname<S: AsRef<str>>(hostname: S, filter: Option<S>) -> Vec<u8> {
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();
}
}
1 change: 1 addition & 0 deletions network/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down