Bump rust-bitcoin, add addrv2
[dnsseed-rust] / src / datastore.rs
index c00bc0165a71d98ef8ed80d17a0d88a5e970d4a5..27987714d347f566ea79d662f6bc4343b9c314d8 100644 (file)
@@ -1,11 +1,11 @@
-use std::{cmp, mem};
+use std::cmp;
 use std::collections::{HashSet, HashMap, hash_map};
 use std::sync::{Arc, RwLock};
-use std::net::{IpAddr, SocketAddr};
+use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
 use std::time::{Duration, Instant};
 use std::io::{BufRead, BufReader};
 
-use bitcoin::network::address::Address;
+use bitcoin::network::address::{Address, AddrV2Message};
 
 use rand::thread_rng;
 use rand::seq::{SliceRandom, IteratorRandom};
@@ -18,6 +18,9 @@ use regex::Regex;
 
 use crate::bgp_client::BGPClient;
 
+pub const SECS_PER_SCAN_RESULTS: u64 = 15;
+const MAX_CONNS_PER_SEC_PER_STATUS: u64 = 30;
+
 #[derive(Clone, Copy, Hash, PartialEq, Eq)]
 pub enum AddressState {
        Untested,
@@ -106,7 +109,6 @@ impl AddressState {
 
 #[derive(Hash, PartialEq, Eq)]
 pub enum U64Setting {
-       ConnsPerSec,
        RunTimeout,
        WasGoodTimeout,
        RescanInterval(AddressState),
@@ -123,19 +125,63 @@ struct Node {
        last_good: Instant, // Ignored unless state is Good or WasGood
        last_services: u64,
        state: AddressState,
+       queued: bool,
+}
+
+/// Essentially SocketAddr but without a traffic class or scope
+#[derive(Clone, PartialEq, Eq, Hash)]
+enum SockAddr {
+       V4(SocketAddrV4),
+       V6((Ipv6Addr, u16)),
+}
+impl From<SocketAddr> for SockAddr {
+       fn from(addr: SocketAddr) -> SockAddr {
+               match addr {
+                       SocketAddr::V4(sa) => SockAddr::V4(sa),
+                       SocketAddr::V6(sa) => SockAddr::V6((sa.ip().clone(), sa.port())),
+               }
+       }
+}
+impl Into<SocketAddr> for &SockAddr {
+       fn into(self) -> SocketAddr {
+               match self {
+                       &SockAddr::V4(sa) => SocketAddr::V4(sa),
+                       &SockAddr::V6(sa) => SocketAddr::V6(SocketAddrV6::new(sa.0, sa.1, 0, 0))
+               }
+       }
+}
+impl ToString for SockAddr {
+       fn to_string(&self) -> String {
+               let sa: SocketAddr = self.into();
+               sa.to_string()
+       }
+}
+impl SockAddr {
+       pub fn port(&self) -> u16 {
+               match *self {
+                       SockAddr::V4(sa) => sa.port(),
+                       SockAddr::V6((_, port)) => port,
+               }
+       }
+       pub fn ip(&self) -> IpAddr {
+               match *self {
+                       SockAddr::V4(sa) => IpAddr::V4(sa.ip().clone()),
+                       SockAddr::V6((ip, _)) => IpAddr::V6(ip),
+               }
+       }
 }
 
 struct Nodes {
-       good_node_services: Vec<HashSet<SocketAddr>>,
-       nodes_to_state: HashMap<SocketAddr, Node>,
-       state_next_scan: Vec<Vec<(Instant, SocketAddr)>>,
+       good_node_services: [HashSet<SockAddr>; 64],
+       nodes_to_state: HashMap<SockAddr, Node>,
+       state_next_scan: [Vec<SockAddr>; AddressState::get_count() as usize],
 }
 struct NodesMutRef<'a> {
-       good_node_services: &'a mut Vec<HashSet<SocketAddr>>,
-       nodes_to_state: &'a mut HashMap<SocketAddr, Node>,
-       state_next_scan: &'a mut Vec<Vec<(Instant, SocketAddr)>>,
-
+       good_node_services: &'a mut [HashSet<SockAddr>; 64],
+       nodes_to_state: &'a mut HashMap<SockAddr, Node>,
+       state_next_scan: &'a mut [Vec<SockAddr>; AddressState::get_count() as usize],
 }
+
 impl Nodes {
        fn borrow_mut<'a>(&'a mut self) -> NodesMutRef<'a> {
                NodesMutRef {
@@ -172,7 +218,6 @@ impl Store {
                                } }
                        }
                        let mut u64s = HashMap::with_capacity(AddressState::get_count() as usize + 4);
-                       u64s.insert(U64Setting::ConnsPerSec, try_read!(l, u64));
                        u64s.insert(U64Setting::RunTimeout, try_read!(l, u64));
                        u64s.insert(U64Setting::WasGoodTimeout, try_read!(l, u64));
                        u64s.insert(U64Setting::MinProtocolVersion, try_read!(l, u64));
@@ -194,10 +239,9 @@ impl Store {
                        future::ok((u64s, try_read!(l, Regex)))
                }).or_else(|_| -> future::FutureResult<(HashMap<U64Setting, u64>, Regex), ()> {
                        let mut u64s = HashMap::with_capacity(15);
-                       u64s.insert(U64Setting::ConnsPerSec, 10);
                        u64s.insert(U64Setting::RunTimeout, 120);
                        u64s.insert(U64Setting::WasGoodTimeout, 21600);
-                       u64s.insert(U64Setting::RescanInterval(AddressState::Untested), 0);
+                       u64s.insert(U64Setting::RescanInterval(AddressState::Untested), 1);
                        u64s.insert(U64Setting::RescanInterval(AddressState::LowBlockCount), 3600);
                        u64s.insert(U64Setting::RescanInterval(AddressState::HighBlockCount), 7200);
                        u64s.insert(U64Setting::RescanInterval(AddressState::LowVersion), 21600);
@@ -218,14 +262,8 @@ impl Store {
 
                macro_rules! nodes_uninitd {
                        () => { {
-                               let mut state_vecs = Vec::with_capacity(AddressState::get_count() as usize);
-                               for _ in 0..AddressState::get_count() {
-                                       state_vecs.push(Vec::new());
-                               }
-                               let mut good_node_services = Vec::with_capacity(64);
-                               for _ in 0..64 {
-                                       good_node_services.push(HashSet::new());
-                               }
+                               let state_vecs = [Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new()];
+                               let good_node_services = [HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new(), HashSet::new()];
                                Nodes {
                                        good_node_services,
                                        nodes_to_state: HashMap::new(),
@@ -265,16 +303,17 @@ impl Store {
                                        last_services,
                                        last_update: Instant::now(),
                                        last_good: Instant::now(),
+                                       queued: true,
                                };
                                if node.state == AddressState::Good {
                                        for i in 0..64 {
                                                if node.last_services & (1 << i) != 0 {
-                                                       res.good_node_services[i].insert(sockaddr);
+                                                       res.good_node_services[i].insert(sockaddr.into());
                                                }
                                        }
                                }
-                               res.state_next_scan[node.state.to_num() as usize].push((Instant::now(), sockaddr));
-                               res.nodes_to_state.insert(sockaddr, node);
+                               res.state_next_scan[node.state.to_num() as usize].push(sockaddr.into());
+                               res.nodes_to_state.insert(sockaddr.into(), node);
                        }
                        future::ok(res)
                }).or_else(|_| -> future::FutureResult<Nodes, ()> {
@@ -315,15 +354,16 @@ impl Store {
                let mut nodes = self.nodes.write().unwrap();
                let cur_time = Instant::now();
                for addr in addresses {
-                       match nodes.nodes_to_state.entry(addr.clone()) {
+                       match nodes.nodes_to_state.entry(addr.into()) {
                                hash_map::Entry::Vacant(e) => {
                                        e.insert(Node {
                                                state: AddressState::Untested,
                                                last_services: 0,
                                                last_update: cur_time,
                                                last_good: cur_time,
+                                               queued: true,
                                        });
-                                       nodes.state_next_scan[AddressState::Untested.to_num() as usize].push((cur_time, addr));
+                                       nodes.state_next_scan[AddressState::Untested.to_num() as usize].push(addr.into());
                                        res += 1;
                                },
                                hash_map::Entry::Occupied(_) => {},
@@ -340,17 +380,28 @@ impl Store {
                        }
                }));
        }
+       pub fn add_fresh_nodes_v2(&self, addresses: &Vec<AddrV2Message>) {
+               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, sockaddr: SocketAddr, state: AddressState, services: u64) -> AddressState {
+               let addr: SockAddr = sockaddr.into();
+               let now = Instant::now();
 
-       pub fn set_node_state(&self, addr: SocketAddr, state: AddressState, services: u64) -> AddressState {
                let mut nodes_lock = self.nodes.write().unwrap();
                let nodes = nodes_lock.borrow_mut();
-               let now = Instant::now();
 
-               let state_ref = nodes.nodes_to_state.entry(addr).or_insert(Node {
+               let state_ref = nodes.nodes_to_state.entry(addr.clone()).or_insert(Node {
                        state: AddressState::Untested,
                        last_services: 0,
                        last_update: now,
                        last_good: now,
+                       queued: false,
                });
                let ret = state_ref.state;
                if (state_ref.state == AddressState::Good || state_ref.state == AddressState::WasGood)
@@ -363,13 +414,16 @@ impl Store {
                                }
                        }
                        state_ref.last_services = 0;
-                       nodes.state_next_scan[AddressState::WasGood.to_num() as usize].push((now, addr));
+                       if !state_ref.queued {
+                               nodes.state_next_scan[AddressState::WasGood.to_num() as usize].push(addr);
+                               state_ref.queued = true;
+                       }
                } else {
                        state_ref.state = state;
                        if state == AddressState::Good {
                                for i in 0..64 {
                                        if services & (1 << i) != 0 && state_ref.last_services & (1 << i) == 0 {
-                                               nodes.good_node_services[i].insert(addr);
+                                               nodes.good_node_services[i].insert(addr.clone());
                                        } else if services & (1 << i) == 0 && state_ref.last_services & (1 << i) != 0 {
                                                nodes.good_node_services[i].remove(&addr);
                                        }
@@ -377,7 +431,10 @@ impl Store {
                                state_ref.last_services = services;
                                state_ref.last_good = now;
                        }
-                       nodes.state_next_scan[state.to_num() as usize].push((now, addr));
+                       if !state_ref.queued {
+                               nodes.state_next_scan[state.to_num() as usize].push(addr);
+                               state_ref.queued = true;
+                       }
                }
                state_ref.last_update = now;
                ret
@@ -386,8 +443,7 @@ impl Store {
        pub fn save_data(&'static self) -> impl Future<Item=(), Error=()> {
                let settings_file = self.store.clone() + "/settings";
                let settings_future = File::create(settings_file.clone() + ".tmp").and_then(move |f| {
-                       let settings_string = format!("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
-                               self.get_u64(U64Setting::ConnsPerSec),
+                       let settings_string = format!("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
                                self.get_u64(U64Setting::RunTimeout),
                                self.get_u64(U64Setting::WasGoodTimeout),
                                self.get_u64(U64Setting::MinProtocolVersion),
@@ -419,7 +475,7 @@ impl Store {
                        let mut nodes_buff = String::new();
                        {
                                let nodes = self.nodes.read().unwrap();
-                               nodes_buff.reserve(nodes.nodes_to_state.len() * 20);
+                               nodes_buff.reserve(nodes.nodes_to_state.len() * 32);
                                for (ref sockaddr, ref node) in nodes.nodes_to_state.iter() {
                                        nodes_buff += &sockaddr.to_string();
                                        nodes_buff += ",";
@@ -445,17 +501,47 @@ impl Store {
                        let mut dns_buff = String::new();
                        {
                                let mut rng = thread_rng();
-                               for i in &[1u64, 4, 5, 8, 9, 12, 13, 1024, 1025, 1028, 1029, 1032, 1033, 1036, 1037] {
-                                       let mut v6_set: Vec<IpAddr> = Vec::new();
-                                       let mut v4_set: Vec<IpAddr> = Vec::new();
+                               for i in &[ 0b00000000001u64,
+                                           0b00000000100,
+                                           0b00000000101,
+                                           0b00000001000,
+                                           0b00000001001,
+                                           0b00000001100,
+                                           0b00000001101,
+                                           0b00001001001,
+                                           0b10000000000,
+                                           0b10000000001,
+                                           0b10000000100,
+                                           0b10000000101,
+                                           0b10000001000,
+                                           0b10000001001,
+                                           0b10000001100,
+                                           0b10000001101,
+                                           0b10001001000] {
+                               //            ^ NODE_NETWORK_LIIMTED
+                               //COMPACT_FILTERS ^   ^ NODE_BLOOM
+                               //      NODE_WITNESS ^  ^ NODE_NETWORK
+                               // We support all combos of NETWORK, NETWORK_LIMITED, BLOOM, and WITNESS
+                               // We support COMPACT_FILTERS with WITNESS and NETWORK or NETWORK_LIIMTED.
+                                       let mut tor_set: Vec<Ipv6Addr> = Vec::new();
+                                       let mut v6_set: Vec<Ipv6Addr> = Vec::new();
+                                       let mut v4_set: Vec<Ipv4Addr> = Vec::new();
+                                       macro_rules! add_addr { ($addr: expr) => {
+                                               match $addr.ip() {
+                                                       IpAddr::V4(v4addr) => v4_set.push(v4addr),
+                                                       IpAddr::V6(v6addr) if v6addr.octets()[..6] == [0xFD,0x87,0xD8,0x7E,0xEB,0x43][..] => tor_set.push(v6addr),
+                                                       IpAddr::V6(v6addr) => v6_set.push(v6addr),
+                                               }
+                                       } }
                                        {
                                                let nodes = self.nodes.read().unwrap();
                                                if i.count_ones() == 1 {
                                                        for j in 0..64 {
                                                                if i & (1 << j) != 0 {
                                                                        let set_ref = &nodes.good_node_services[j];
-                                                                       v4_set = set_ref.iter().filter(|e| e.is_ipv4() && e.port() == 8333).map(|e| e.ip()).collect();
-                                                                       v6_set = set_ref.iter().filter(|e| e.is_ipv6() && e.port() == 8333).map(|e| e.ip()).collect();
+                                                                       for a in set_ref.iter().filter(|e| e.port() == 8333) {
+                                                                               add_addr!(a);
+                                                                       }
                                                                        break;
                                                                }
                                                        }
@@ -472,10 +558,9 @@ impl Store {
                                                                        }
                                                                }
                                                        }
-                                                       v4_set = first_set.unwrap().intersection(&second_set.unwrap())
-                                                               .filter(|e| e.is_ipv4() && e.port() == 8333).map(|e| e.ip()).collect();
-                                                       v6_set = first_set.unwrap().intersection(&second_set.unwrap())
-                                                               .filter(|e| e.is_ipv6() && e.port() == 8333).map(|e| e.ip()).collect();
+                                                       for a in first_set.unwrap().intersection(&second_set.unwrap()).filter(|e| e.port() == 8333) {
+                                                               add_addr!(a);
+                                                       }
                                                } else {
                                                        //TODO: Could optimize this one a bit
                                                        let mut intersection;
@@ -492,21 +577,23 @@ impl Store {
                                                                        }
                                                                }
                                                        }
-                                                       v4_set = intersection_set_ref.unwrap().iter()
-                                                               .filter(|e| e.is_ipv4() && e.port() == 8333).map(|e| e.ip()).collect();
-                                                       v6_set = intersection_set_ref.unwrap().iter()
-                                                               .filter(|e| e.is_ipv6() && e.port() == 8333).map(|e| e.ip()).collect();
+                                                       for a in intersection_set_ref.unwrap().iter().filter(|e| e.port() == 8333) {
+                                                               add_addr!(a);
+                                                       }
                                                }
                                        }
                                        let mut asn_set = HashSet::with_capacity(cmp::max(v4_set.len(), v6_set.len()));
                                        asn_set.insert(0);
-                                       for a in v4_set.iter().filter(|a| asn_set.insert(bgp_client.get_asn(**a))).choose_multiple(&mut rng, 21) {
-                                               dns_buff += &format!("x{:x}.dnsseed\tIN\tA\t{}\n", i, a);
+                                       for (a, asn) in v4_set.iter().map(|a| (a, bgp_client.get_asn(IpAddr::V4(*a)))).filter(|a| asn_set.insert(a.1)).choose_multiple(&mut rng, 21) {
+                                               dns_buff += &format!("x{:x}.dnsseed\tIN\tA\t{} ; AS{}\n", i, a, asn);
                                        }
                                        asn_set.clear();
                                        asn_set.insert(0);
-                                       for a in v6_set.iter().filter(|a| asn_set.insert(bgp_client.get_asn(**a))).choose_multiple(&mut rng, 12) {
-                                               dns_buff += &format!("x{:x}.dnsseed\tIN\tAAAA\t{}\n", i, a);
+                                       for (a, asn) in v6_set.iter().map(|a| (a, bgp_client.get_asn(IpAddr::V6(*a)))).filter(|a| asn_set.insert(a.1)).choose_multiple(&mut rng, 10) {
+                                               dns_buff += &format!("x{:x}.dnsseed\tIN\tAAAA\t{} ; AS{}\n", i, a, asn);
+                                       }
+                                       for a in tor_set.iter().choose_multiple(&mut rng, 2) {
+                                               dns_buff += &format!("x{:x}.dnsseed\tIN\tAAAA\t{} ; Tor Onionv2\n", i, a);
                                        }
                                }
                        }
@@ -519,21 +606,19 @@ impl Store {
        }
 
        pub fn get_next_scan_nodes(&self) -> Vec<SocketAddr> {
-               let results = 30 * self.get_u64(U64Setting::ConnsPerSec) as usize;
-               let per_bucket_results = results / (AddressState::get_count() as usize);
-               let mut res = Vec::with_capacity(results);
-               let cur_time = Instant::now();
+               let mut res = Vec::with_capacity(128);
 
                {
-                       let mut nodes = self.nodes.write().unwrap();
+                       let mut nodes_lock = self.nodes.write().unwrap();
+                       let nodes = nodes_lock.borrow_mut();
                        for (idx, state_nodes) in nodes.state_next_scan.iter_mut().enumerate() {
-                               let cmp_time = cur_time - Duration::from_secs(self.get_u64(U64Setting::RescanInterval(AddressState::from_num(idx as u8).unwrap())));
-                               let split_point = cmp::min(cmp::min(results - res.len(), (per_bucket_results * (idx + 1)) - res.len()),
-                                               state_nodes.binary_search_by(|a| a.0.cmp(&cmp_time)).unwrap_or_else(|idx| idx));
-                               let mut new_nodes = state_nodes.split_off(split_point);
-                               mem::swap(&mut new_nodes, state_nodes);
-                               for (_, node) in new_nodes.drain(..) {
-                                       res.push(node);
+                               let rescan_interval = cmp::max(self.get_u64(U64Setting::RescanInterval(AddressState::from_num(idx as u8).unwrap())), 1);
+                               let split_point = cmp::min(cmp::min(SECS_PER_SCAN_RESULTS * state_nodes.len() as u64 / rescan_interval,
+                                                       SECS_PER_SCAN_RESULTS * MAX_CONNS_PER_SEC_PER_STATUS),
+                                               state_nodes.len() as u64);
+                               for node in state_nodes.drain(..split_point as usize) {
+                                       nodes.nodes_to_state.get_mut(&node).unwrap().queued = false;
+                                       res.push((&node).into());
                                }
                        }
                }