Decrease the size of Node from 48 to 16 (and a map entry from 72 to 36)
[dnsseed-rust] / src / datastore.rs
index 1ae9dd95ad11c301426e2b697cd6af20ecafbdb0..c55e47d93cfb1b59d253a583ebdd611f5b4780d3 100644 (file)
@@ -1,11 +1,12 @@
-use std::{cmp, mem};
+use std::cmp;
+use std::convert::TryInto;
 use std::collections::{HashSet, HashMap, hash_map};
 use std::sync::{Arc, RwLock};
 use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
-use std::time::{Duration, Instant};
+use std::time::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};
@@ -121,24 +122,52 @@ pub enum RegexSetting {
 }
 
 struct Node {
-       last_update: Instant,
-       last_good: Instant, // Ignored unless state is Good or WasGood
-       last_services: u64,
+       // Times in seconds-since-startup
+       last_good: u32, // Ignored unless state is Good or WasGood
+       // Since everything is is 4-byte aligned, using a u64 for services blows up our size
+       // substantially. Instead, use a u32 pair and bit shift as needed.
+       last_services: (u32, u32),
        state: AddressState,
        queued: bool,
 }
+impl Node {
+       #[inline]
+       fn last_services(&self) -> u64 {
+               ((self.last_services.0 as u64) << 32) |
+               ((self.last_services.1 as u64)      )
+       }
+       #[inline]
+       fn services(inp: u64) -> (u32, u32) {
+               (
+                       ((inp & 0xffffffff00000000) >> 32) as u32,
+                       ((inp & 0x00000000ffffffff)      ) as u32
+               )
+       }
+}
+
+#[test]
+fn services_test() {
+       assert_eq!(
+               Node { last_good: 0, state: AddressState::Good, queued: false, last_services: Node::services(0x1badcafedeadbeef) }
+                       .last_services(),
+               0x1badcafedeadbeef);
+}
 
 /// Essentially SocketAddr but without a traffic class or scope
 #[derive(Clone, PartialEq, Eq, Hash)]
 enum SockAddr {
        V4(SocketAddrV4),
-       V6((Ipv6Addr, u16)),
+       V6(([u16; 8], u16)),
+}
+#[inline]
+fn segs_to_ip6(segs: &[u16; 8]) -> Ipv6Addr {
+       Ipv6Addr::new(segs[0], segs[1], segs[2], segs[3], segs[4], segs[5], segs[6], segs[7])
 }
 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())),
+                       SocketAddr::V6(sa) => SockAddr::V6((sa.ip().segments(), sa.port())),
                }
        }
 }
@@ -146,7 +175,7 @@ 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))
+                       &SockAddr::V6(sa) => SocketAddr::V6(SocketAddrV6::new(segs_to_ip6(&sa.0), sa.1, 0, 0))
                }
        }
 }
@@ -166,7 +195,7 @@ impl SockAddr {
        pub fn ip(&self) -> IpAddr {
                match *self {
                        SockAddr::V4(sa) => IpAddr::V4(sa.ip().clone()),
-                       SockAddr::V6((ip, _)) => IpAddr::V6(ip),
+                       SockAddr::V6((ip, _)) => IpAddr::V6(segs_to_ip6(&ip)),
                }
        }
 }
@@ -174,12 +203,12 @@ impl SockAddr {
 struct Nodes {
        good_node_services: [HashSet<SockAddr>; 64],
        nodes_to_state: HashMap<SockAddr, Node>,
-       state_next_scan: Vec<Vec<(Instant, SockAddr)>>,
+       state_next_scan: [Vec<SockAddr>; AddressState::get_count() as usize],
 }
 struct NodesMutRef<'a> {
        good_node_services: &'a mut [HashSet<SockAddr>; 64],
        nodes_to_state: &'a mut HashMap<SockAddr, Node>,
-       state_next_scan: &'a mut Vec<Vec<(Instant, SockAddr)>>,
+       state_next_scan: &'a mut [Vec<SockAddr>; AddressState::get_count() as usize],
 }
 
 impl Nodes {
@@ -196,6 +225,7 @@ pub struct Store {
        u64_settings: RwLock<HashMap<U64Setting, u64>>,
        subver_regex: RwLock<Arc<Regex>>,
        nodes: RwLock<Nodes>,
+       start_time: Instant,
        store: String,
 }
 
@@ -262,10 +292,7 @@ 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 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,
@@ -276,7 +303,6 @@ impl Store {
                }
 
                let nodes_future = File::open(store.clone() + "/nodes").and_then(|f| {
-                       let start_time = Instant::now() - Duration::from_secs(60 * 60 * 24);
                        let mut res = nodes_uninitd!();
                        let l = BufReader::new(f).lines();
                        for line_res in l {
@@ -304,19 +330,18 @@ impl Store {
                                                Some(v) => v,
                                                None => return future::ok(res),
                                        },
-                                       last_services,
-                                       last_update: Instant::now(),
-                                       last_good: Instant::now(),
+                                       last_services: Node::services(last_services),
+                                       last_good: 0,
                                        queued: true,
                                };
                                if node.state == AddressState::Good {
                                        for i in 0..64 {
-                                               if node.last_services & (1 << i) != 0 {
+                                               if node.last_services() & (1 << i) != 0 {
                                                        res.good_node_services[i].insert(sockaddr.into());
                                                }
                                        }
                                }
-                               res.state_next_scan[node.state.to_num() as usize].push((start_time, sockaddr.into()));
+                               res.state_next_scan[node.state.to_num() as usize].push(sockaddr.into());
                                res.nodes_to_state.insert(sockaddr.into(), node);
                        }
                        future::ok(res)
@@ -329,6 +354,7 @@ impl Store {
                                subver_regex: RwLock::new(Arc::new(regex)),
                                nodes: RwLock::new(nodes),
                                store,
+                               start_time: Instant::now(),
                        })
                })
        }
@@ -355,19 +381,18 @@ impl Store {
 
        pub fn add_fresh_addrs<I: Iterator<Item=SocketAddr>>(&self, addresses: I) -> u64 {
                let mut res = 0;
+               let cur_time = (Instant::now() - self.start_time).as_secs().try_into().unwrap();
                let mut nodes = self.nodes.write().unwrap();
-               let cur_time = Instant::now();
                for addr in addresses {
                        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_services: (0, 0),
                                                last_good: cur_time,
                                                queued: true,
                                        });
-                                       nodes.state_next_scan[AddressState::Untested.to_num() as usize].push((cur_time, addr.into()));
+                                       nodes.state_next_scan[AddressState::Untested.to_num() as usize].push(addr.into());
                                        res += 1;
                                },
                                hash_map::Entry::Occupied(_) => {},
@@ -384,55 +409,64 @@ 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();
+
+               let now = (Instant::now() - self.start_time).as_secs().try_into().unwrap();
 
                let mut nodes_lock = self.nodes.write().unwrap();
                let nodes = nodes_lock.borrow_mut();
 
                let state_ref = nodes.nodes_to_state.entry(addr.clone()).or_insert(Node {
                        state: AddressState::Untested,
-                       last_services: 0,
-                       last_update: now,
+                       last_services: (0, 0),
                        last_good: now,
                        queued: false,
                });
                let ret = state_ref.state;
+               let was_good_timeout: u32 = self.get_u64(U64Setting::WasGoodTimeout)
+                       .try_into().expect("Need WasGood timeout that fits in a u32");
                if (state_ref.state == AddressState::Good || state_ref.state == AddressState::WasGood)
                                && state != AddressState::Good
-                               && state_ref.last_good >= now - Duration::from_secs(self.get_u64(U64Setting::WasGoodTimeout)) {
+                               && state_ref.last_good >= now - was_good_timeout {
                        state_ref.state = AddressState::WasGood;
                        for i in 0..64 {
-                               if state_ref.last_services & (1 << i) != 0 {
+                               if state_ref.last_services() & (1 << i) != 0 {
                                        nodes.good_node_services[i].remove(&addr);
                                }
                        }
-                       state_ref.last_services = 0;
+                       state_ref.last_services = (0, 0);
                        if !state_ref.queued {
-                               nodes.state_next_scan[AddressState::WasGood.to_num() as usize].push((now, addr));
+                               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 {
+                                       if services & (1 << i) != 0 && state_ref.last_services() & (1 << i) == 0 {
                                                nodes.good_node_services[i].insert(addr.clone());
-                                       } else if services & (1 << i) == 0 && state_ref.last_services & (1 << i) != 0 {
+                                       } else if services & (1 << i) == 0 && state_ref.last_services() & (1 << i) != 0 {
                                                nodes.good_node_services[i].remove(&addr);
                                        }
                                }
-                               state_ref.last_services = services;
+                               state_ref.last_services = Node::services(services);
                                state_ref.last_good = now;
                        }
                        if !state_ref.queued {
-                               nodes.state_next_scan[state.to_num() as usize].push((now, addr));
+                               nodes.state_next_scan[state.to_num() as usize].push(addr);
                                state_ref.queued = true;
                        }
                }
-               state_ref.last_update = now;
                ret
        }
 
@@ -471,13 +505,13 @@ 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 += ",";
                                        nodes_buff += &node.state.to_num().to_string();
                                        nodes_buff += ",";
-                                       nodes_buff += &node.last_services.to_string();
+                                       nodes_buff += &node.last_services().to_string();
                                        nodes_buff += "\n";
                                }
                        }
@@ -580,16 +614,16 @@ impl Store {
                                        }
                                        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(IpAddr::V4(**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(IpAddr::V6(**a)))).choose_multiple(&mut rng, 10) {
-                                               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{}\n", i, a);
+                                               dns_buff += &format!("x{:x}.dnsseed\tIN\tAAAA\t{} ; Tor Onionv2\n", i, a);
                                        }
                                }
                        }
@@ -603,20 +637,16 @@ impl Store {
 
        pub fn get_next_scan_nodes(&self) -> Vec<SocketAddr> {
                let mut res = Vec::with_capacity(128);
-               let cur_time = Instant::now();
 
                {
                        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 rescan_interval = cmp::max(self.get_u64(U64Setting::RescanInterval(AddressState::from_num(idx as u8).unwrap())), 1);
-                               let cmp_time = cur_time - Duration::from_secs(rescan_interval);
                                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.binary_search_by(|a| a.0.cmp(&cmp_time)).unwrap_or_else(|idx| idx) as u64);
-                               let mut new_nodes = state_nodes.split_off(split_point as usize);
-                               mem::swap(&mut new_nodes, state_nodes);
-                               for (_, node) in new_nodes.drain(..) {
+                                               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());
                                }