Skip to content

Commit

Permalink
[common] Filter seed by feature supported
Browse files Browse the repository at this point in the history
This include a rework of the PR cloudhead#54, but not include the refactoring in other module.

Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Nov 2, 2021
1 parent 5355f6c commit ee1ca41
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
2 changes: 1 addition & 1 deletion client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ impl<R: Reactor<Publisher>> Client<R> {
.network
.seeds()
.iter()
.map(|s| (*s, self.config.network.port())),
.map(|s| (s.as_str(), self.config.network.port())),
Source::Dns,
)?;
peers.flush()?;
Expand Down
41 changes: 36 additions & 5 deletions common/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Bitcoin peer network. Eg. *Mainnet*.
use std::vec;

use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::consensus::params::Params;
Expand Down Expand Up @@ -99,10 +100,31 @@ impl Network {
}
}

/// Return a list of services that nakamoto need
/// in the DNS resolving
fn supported_services(&self) -> vec::Vec<ServiceFlags> {
vec![ServiceFlags::NETWORK, ServiceFlags::COMPACT_FILTERS]
}

/// Return a list of flag in string slices form
fn service_flags(&self) -> vec::Vec<String> {
let services = self.supported_services();
services
.iter()
.map(|service| match *service {
//FIXME(vincenzopalazzo): The COMPACT_FILTER is it x64? Check in bitcoin core.
ServiceFlags::NETWORK | ServiceFlags::COMPACT_FILTERS => {
format!("x{:x}", service.as_u64())
}
_ => panic!("Unsupported feature!"),
})
.collect()
}

/// DNS seeds. Used to bootstrap the client's address book.
pub fn seeds(&self) -> &[&str] {
match self {
Network::Mainnet => &[
pub fn seeds<'a>(&self) -> Vec<String> {
let seed_list = match self {
Network::Mainnet => vec![
"seed.bitcoin.sipa.be", // Pieter Wuille
"dnsseed.bluematt.me", // Matt Corallo
"dnsseed.bitcoin.dashjr.org", // Luke Dashjr
Expand All @@ -114,14 +136,23 @@ impl Network {
"seed.bitcoin.wiz.biz", // Jason Maurice
"seed.cloudhead.io", // Alexis Sellier
],
Network::Testnet => &[
Network::Testnet => vec![
"testnet-seed.bitcoin.jonasschnelli.ch",
"seed.tbtc.petertodd.org",
"seed.testnet.bitcoin.sprovoost.nl",
"testnet-seed.bluematt.me",
],
Network::Regtest => &[], // No seeds
Network::Regtest => vec![], // No seeds
};

let mut feature_seeds = Vec::new();
for flag in self.service_flags() {
for seed in seed_list.iter() {
let feature_seed = format!("{}.{}", flag, seed);
feature_seeds.push(feature_seed);
}
}
feature_seeds
}
}

Expand Down
22 changes: 5 additions & 17 deletions common/src/p2p/peer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Shared peer types.
use log::debug;
use microserde as serde;
use std::io;
use std::net;

use microserde as serde;

use bitcoin::network::address::Address;
use bitcoin::network::constants::ServiceFlags;

Expand Down Expand Up @@ -40,18 +40,15 @@ pub trait Store {

/// Seed the peer store with addresses.
/// Fails if *none* of the seeds could be resolved to addresses.
fn seed<S: net::ToSocketAddrs>(
fn seed<S: net::ToSocketAddrs + std::fmt::Debug>(
&mut self,
seeds: impl Iterator<Item = S>,
source: Source,
) -> io::Result<()> {
let mut error = None;
let mut success = false;

for seed in seeds {
debug!("Resolving DNS seed {:?}", seed);
match seed.to_socket_addrs() {
Ok(addrs) => {
success = true;
for addr in addrs {
self.insert(
addr.ip(),
Expand All @@ -63,19 +60,10 @@ pub trait Store {
);
}
}
Err(err) => error = Some(err),
Err(err) => debug!("Error received is {}", err),
}
}

if success {
return Ok(());
}
if let Some(err) = error {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("seeds failed to resolve: {}", err),
));
}
Ok(())
}

Expand Down

0 comments on commit ee1ca41

Please sign in to comment.