]> git.bitcoin.ninja Git - dnsseed-rust/commitdiff
Regularly poll other dnsseeds for new addrs
authorMatt Corallo <git@bluematt.me>
Tue, 21 May 2019 19:25:33 +0000 (15:25 -0400)
committerMatt Corallo <git@bluematt.me>
Tue, 21 May 2019 19:34:03 +0000 (15:34 -0400)
src/datastore.rs
src/main.rs

index 18aa0e68d067509a71ed992ca791b7b533c656db..26ed96bcb4feff30017a9942933d4c7c36485cee 100644 (file)
@@ -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<I: Iterator<Item=SocketAddr>>(&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 {
index be4c8fcfd07d32b52dd0f506f9b9f905a39ed914..be27835cf6b0df05fbd637b6f713deac21d76572 100644 (file)
@@ -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 {