X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fdatastore.rs;h=a078f49cae9c2d9f6959bc639005e651074a84b3;hb=5767a129c52ab94c8b23291a0ce0dda46927eb95;hp=0b608f9f536397d60740f8fc8cbf0fc112b74299;hpb=b6fd38119297bf900c9961fcb60b5898656984f9;p=dnsseed-rust diff --git a/src/datastore.rs b/src/datastore.rs index 0b608f9..a078f49 100644 --- a/src/datastore.rs +++ b/src/datastore.rs @@ -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::io::{BufRead, BufReader}; -use bitcoin::network::address::Address; +use bitcoin::network::address::{Address, AddrV2Message}; use rand::thread_rng; use rand::seq::{SliceRandom, IteratorRandom}; @@ -16,10 +17,11 @@ use tokio::io::write_all; use regex::Regex; +use crate::bloom::RollingBloomFilter; use crate::bgp_client::BGPClient; pub const SECS_PER_SCAN_RESULTS: u64 = 15; -const MAX_CONNS_PER_SEC_PER_STATUS: u64 = 30; +const MAX_CONNS_PER_SEC_PER_STATUS: u64 = 1000; #[derive(Clone, Copy, Hash, PartialEq, Eq)] pub enum AddressState { @@ -121,24 +123,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 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 +176,7 @@ impl Into 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 +196,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 +204,12 @@ impl SockAddr { struct Nodes { good_node_services: [HashSet; 64], nodes_to_state: HashMap, - state_next_scan: Vec>, + state_next_scan: [Vec; AddressState::get_count() as usize], } struct NodesMutRef<'a> { good_node_services: &'a mut [HashSet; 64], nodes_to_state: &'a mut HashMap, - state_next_scan: &'a mut Vec>, + state_next_scan: &'a mut [Vec; AddressState::get_count() as usize], } impl Nodes { @@ -196,6 +226,8 @@ pub struct Store { u64_settings: RwLock>, subver_regex: RwLock>, nodes: RwLock, + timeout_nodes: RollingBloomFilter, + start_time: Instant, store: String, } @@ -241,14 +273,14 @@ impl Store { let mut u64s = HashMap::with_capacity(15); u64s.insert(U64Setting::RunTimeout, 120); u64s.insert(U64Setting::WasGoodTimeout, 21600); - u64s.insert(U64Setting::RescanInterval(AddressState::Untested), 1); + u64s.insert(U64Setting::RescanInterval(AddressState::Untested), 3600); u64s.insert(U64Setting::RescanInterval(AddressState::LowBlockCount), 3600); u64s.insert(U64Setting::RescanInterval(AddressState::HighBlockCount), 7200); u64s.insert(U64Setting::RescanInterval(AddressState::LowVersion), 21600); u64s.insert(U64Setting::RescanInterval(AddressState::BadVersion), 21600); u64s.insert(U64Setting::RescanInterval(AddressState::NotFullNode), 86400); u64s.insert(U64Setting::RescanInterval(AddressState::ProtocolViolation), 86400); - u64s.insert(U64Setting::RescanInterval(AddressState::Timeout), 86400); + u64s.insert(U64Setting::RescanInterval(AddressState::Timeout), 604800); u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutDuringRequest), 21600); u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutAwaitingPong), 3600); u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutAwaitingAddr), 1800); @@ -262,10 +294,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, @@ -303,14 +332,13 @@ 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()); } } @@ -327,7 +355,9 @@ impl Store { u64_settings: RwLock::new(u64_settings), subver_regex: RwLock::new(Arc::new(regex)), nodes: RwLock::new(nodes), + timeout_nodes: RollingBloomFilter::new(), store, + start_time: Instant::now(), }) }) } @@ -343,6 +373,9 @@ impl Store { pub fn get_node_count(&self, state: AddressState) -> usize { self.nodes.read().unwrap().state_next_scan[state.to_num() as usize].len() } + pub fn get_bloom_node_count(&self) -> [usize; crate::bloom::GENERATION_COUNT] { + self.timeout_nodes.get_element_count() + } pub fn get_regex(&self, _setting: RegexSetting) -> Arc { Arc::clone(&*self.subver_regex.read().unwrap()) @@ -354,15 +387,14 @@ impl Store { pub fn add_fresh_addrs>(&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, }); @@ -383,32 +415,62 @@ impl Store { } })); } + pub fn add_fresh_nodes_v2(&self, addresses: &Vec) { + 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(); + + if state == AddressState::Untested && self.timeout_nodes.contains(&addr) { + return AddressState::Timeout; + } + + 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 { + let node_entry = nodes.nodes_to_state.entry(addr.clone()); + match node_entry { + hash_map::Entry::Occupied(entry) + if entry.get().state == AddressState::Untested && + entry.get().last_services() == 0 && + state == AddressState::Timeout => { + entry.remove_entry(); + self.timeout_nodes.insert(&addr, Duration::from_secs(self.get_u64(U64Setting::RescanInterval(AddressState::Timeout)))); + return AddressState::Untested; + }, + hash_map::Entry::Vacant(_) if state == AddressState::Timeout => { + self.timeout_nodes.insert(&addr, Duration::from_secs(self.get_u64(U64Setting::RescanInterval(AddressState::Timeout)))); + return AddressState::Untested; + }, + _ => {}, + } + + let state_ref = node_entry.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; if !state_ref.queued { nodes.state_next_scan[AddressState::WasGood.to_num() as usize].push(addr); state_ref.queued = true; @@ -417,13 +479,13 @@ impl Store { 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 { @@ -431,7 +493,6 @@ impl Store { state_ref.queued = true; } } - state_ref.last_update = now; ret } @@ -476,7 +537,7 @@ impl Store { 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"; } } @@ -611,9 +672,7 @@ impl Store { 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); - 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(..) { + for node in state_nodes.drain(..split_point as usize) { nodes.nodes_to_state.get_mut(&node).unwrap().queued = false; res.push((&node).into()); }