From b161100d1919c1d4c678d91a1f49ecca1747327e Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 21 May 2019 15:25:33 -0400 Subject: [PATCH] Regularly poll other dnsseeds for new addrs --- src/datastore.rs | 42 +++++++++++++++++++++++++----------------- src/main.rs | 24 +++++++++++++++++++++++- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/datastore.rs b/src/datastore.rs index 18aa0e6..26ed96b 100644 --- a/src/datastore.rs +++ b/src/datastore.rs @@ -292,27 +292,35 @@ impl Store { *self.subver_regex.write().unwrap() = Arc::new(value); } - pub fn add_fresh_nodes(&self, addresses: &Vec<(u32, Address)>) { + pub fn add_fresh_addrs>(&self, addresses: I) -> u64 { + let mut res = 0; let mut nodes = self.nodes.write().unwrap(); let cur_time = Instant::now(); - for &(_, ref addr) in addresses { - if let Ok(socketaddr) = addr.socket_addr() { - match nodes.nodes_to_state.entry(socketaddr.clone()) { - hash_map::Entry::Vacant(e) => { - e.insert(Node { - state: AddressState::Untested, - last_services: 0, - last_update: cur_time, - last_good: Instant::now(), - }); - nodes.state_next_scan.get_mut(&AddressState::Untested).unwrap().push((cur_time, socketaddr)); - }, - hash_map::Entry::Occupied(_) => {}, - } - } else { - //TODO: Handle onions + for addr in addresses { + match nodes.nodes_to_state.entry(addr.clone()) { + hash_map::Entry::Vacant(e) => { + e.insert(Node { + state: AddressState::Untested, + last_services: 0, + last_update: cur_time, + last_good: Instant::now(), + }); + nodes.state_next_scan.get_mut(&AddressState::Untested).unwrap().push((cur_time, addr)); + res += 1; + }, + hash_map::Entry::Occupied(_) => {}, } } + res + } + + pub fn add_fresh_nodes(&self, addresses: &Vec<(u32, Address)>) { + self.add_fresh_addrs(addresses.iter().filter_map(|(_, addr)| { + match addr.socket_addr() { + Ok(socketaddr) => Some(socketaddr), + Err(_) => None, // TODO: Handle onions + } + })); } pub fn set_node_state(&self, addr: SocketAddr, state: AddressState, services: u64) -> AddressState { diff --git a/src/main.rs b/src/main.rs index be4c8fc..be27835 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::sync::atomic::{Ordering, AtomicBool}; use std::time::{Duration, Instant}; -use std::net::SocketAddr; +use std::net::{SocketAddr, ToSocketAddrs}; use bitcoin_hashes::sha256d; @@ -189,9 +189,30 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr) { })); } +fn poll_dnsseeds() { + tokio::spawn(future::lazy(|| { + let printer = unsafe { PRINTER.as_ref().unwrap() }; + let store = unsafe { DATA_STORE.as_ref().unwrap() }; + + let mut new_addrs = 0; + for seed in ["seed.bitcoin.sipa.be", "dnsseed.bitcoin.dashjr.org", "seed.bitcoinstats.com", "seed.bitcoin.jonasschnelli.ch", "seed.btc.petertodd.org", "seed.bitcoin.sprovoost.nl", "dnsseed.emzy.de"].iter() { + new_addrs += store.add_fresh_addrs((*seed, 8333u16).to_socket_addrs().unwrap_or(Vec::new().into_iter())); + new_addrs += store.add_fresh_addrs((("x9.".to_string() + seed).as_str(), 8333u16).to_socket_addrs().unwrap_or(Vec::new().into_iter())); + } + printer.add_line(format!("Added {} new addresses from other DNS seeds", new_addrs), false); + Delay::new(Instant::now() + Duration::from_secs(60)).then(|_| { + if !START_SHUTDOWN.load(Ordering::Relaxed) { + poll_dnsseeds(); + } + future::ok(()) + }) + })); +} + fn scan_net() { tokio::spawn(future::lazy(|| { let store = unsafe { DATA_STORE.as_ref().unwrap() }; + let mut scan_nodes = store.get_next_scan_nodes(); let per_iter_time = Duration::from_millis(1000 / store.get_u64(U64Setting::ConnsPerSec)); let start_time = Instant::now(); @@ -270,6 +291,7 @@ fn make_trusted_conn(trusted_sockaddr: SocketAddr) { if top_height >= starting_height as u64 { if !SCANNING.swap(true, Ordering::SeqCst) { scan_net(); + poll_dnsseeds(); } } } else { -- 2.39.5