X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fdatastore.rs;h=4f59305b30f5cf17e634b493f8742700b75a411f;hb=82ab324e5e800a6dc8b952c3b776ce2f8f827f2f;hp=6255cd4d033520de3a558b02a9260b90059e451d;hpb=fe75877d32ada49d31f7debb3977e8948c17d73b;p=dnsseed-rust diff --git a/src/datastore.rs b/src/datastore.rs index 6255cd4..4f59305 100644 --- a/src/datastore.rs +++ b/src/datastore.rs @@ -27,6 +27,9 @@ pub enum AddressState { ProtocolViolation, Timeout, TimeoutDuringRequest, + TimeoutAwaitingPong, + TimeoutAwaitingAddr, + TimeoutAwaitingBlock, Good, WasGood, } @@ -43,8 +46,11 @@ impl AddressState { 0x6 => Some(AddressState::ProtocolViolation), 0x7 => Some(AddressState::Timeout), 0x8 => Some(AddressState::TimeoutDuringRequest), - 0x9 => Some(AddressState::Good), - 0xa => Some(AddressState::WasGood), + 0x9 => Some(AddressState::TimeoutAwaitingPong), + 0xa => Some(AddressState::TimeoutAwaitingAddr), + 0xb => Some(AddressState::TimeoutAwaitingBlock), + 0xc => Some(AddressState::Good), + 0xd => Some(AddressState::WasGood), _ => None, } } @@ -60,8 +66,11 @@ impl AddressState { AddressState::ProtocolViolation => 6, AddressState::Timeout => 7, AddressState::TimeoutDuringRequest => 8, - AddressState::Good => 9, - AddressState::WasGood => 10, + AddressState::TimeoutAwaitingPong => 9, + AddressState::TimeoutAwaitingAddr => 10, + AddressState::TimeoutAwaitingBlock => 11, + AddressState::Good => 12, + AddressState::WasGood => 13, } } @@ -76,13 +85,16 @@ impl AddressState { AddressState::ProtocolViolation => "Protocol Violation", AddressState::Timeout => "Timeout", AddressState::TimeoutDuringRequest => "Timeout During Request", + AddressState::TimeoutAwaitingPong => "Timeout Awaiting Pong", + AddressState::TimeoutAwaitingAddr => "Timeout Awaiting Addr", + AddressState::TimeoutAwaitingBlock => "Timeout Awaiting Block", AddressState::Good => "Good", AddressState::WasGood => "Was Good", } } - pub fn get_count() -> u8 { - 11 + pub const fn get_count() -> u8 { + 14 } } @@ -108,14 +120,14 @@ struct Node { } struct Nodes { - good_node_services: HashMap>, + good_node_services: Vec>, nodes_to_state: HashMap, - state_next_scan: HashMap>, + state_next_scan: Vec>, } struct NodesMutRef<'a> { - good_node_services: &'a mut HashMap>, + good_node_services: &'a mut Vec>, nodes_to_state: &'a mut HashMap, - state_next_scan: &'a mut HashMap>, + state_next_scan: &'a mut Vec>, } impl Nodes { @@ -153,7 +165,7 @@ impl Store { } } } } - let mut u64s = HashMap::with_capacity(15); + 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)); @@ -167,6 +179,9 @@ impl Store { u64s.insert(U64Setting::RescanInterval(AddressState::ProtocolViolation), try_read!(l, u64)); u64s.insert(U64Setting::RescanInterval(AddressState::Timeout), try_read!(l, u64)); u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutDuringRequest), try_read!(l, u64)); + u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutAwaitingPong), try_read!(l, u64)); + u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutAwaitingAddr), try_read!(l, u64)); + u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutAwaitingBlock), try_read!(l, u64)); u64s.insert(U64Setting::RescanInterval(AddressState::Good), try_read!(l, u64)); u64s.insert(U64Setting::RescanInterval(AddressState::WasGood), try_read!(l, u64)); future::ok((u64s, try_read!(l, Regex))) @@ -184,29 +199,24 @@ impl Store { u64s.insert(U64Setting::RescanInterval(AddressState::ProtocolViolation), 86400); u64s.insert(U64Setting::RescanInterval(AddressState::Timeout), 86400); u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutDuringRequest), 21600); + u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutAwaitingPong), 3600); + u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutAwaitingAddr), 1800); + u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutAwaitingBlock), 3600); u64s.insert(U64Setting::RescanInterval(AddressState::Good), 1800); u64s.insert(U64Setting::RescanInterval(AddressState::WasGood), 1800); - u64s.insert(U64Setting::MinProtocolVersion, 10000); //XXX + u64s.insert(U64Setting::MinProtocolVersion, 70002); future::ok((u64s, Regex::new(".*").unwrap())) }); macro_rules! nodes_uninitd { () => { { - let mut state_vecs = HashMap::with_capacity(11); - state_vecs.insert(AddressState::Untested, Vec::new()); - state_vecs.insert(AddressState::LowBlockCount, Vec::new()); - state_vecs.insert(AddressState::HighBlockCount, Vec::new()); - state_vecs.insert(AddressState::LowVersion, Vec::new()); - state_vecs.insert(AddressState::BadVersion, Vec::new()); - state_vecs.insert(AddressState::NotFullNode, Vec::new()); - state_vecs.insert(AddressState::ProtocolViolation, Vec::new()); - state_vecs.insert(AddressState::Timeout, Vec::new()); - state_vecs.insert(AddressState::TimeoutDuringRequest, Vec::new()); - state_vecs.insert(AddressState::Good, Vec::new()); - state_vecs.insert(AddressState::WasGood, Vec::new()); - let mut good_node_services = HashMap::with_capacity(64); - for i in 0..64 { - good_node_services.insert(i, HashSet::new()); + 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()); } Nodes { good_node_services, @@ -251,11 +261,11 @@ impl Store { if node.state == AddressState::Good { for i in 0..64 { if node.last_services & (1 << i) != 0 { - res.good_node_services.get_mut(&i).unwrap().insert(sockaddr); + res.good_node_services[i].insert(sockaddr); } } } - res.state_next_scan.get_mut(&node.state).unwrap().push((Instant::now(), sockaddr)); + res.state_next_scan[node.state.to_num() as usize].push((Instant::now(), sockaddr)); res.nodes_to_state.insert(sockaddr, node); } future::ok(res) @@ -281,7 +291,7 @@ impl Store { } pub fn get_node_count(&self, state: AddressState) -> usize { - self.nodes.read().unwrap().state_next_scan.get(&state).unwrap().len() + self.nodes.read().unwrap().state_next_scan[state.to_num() as usize].len() } pub fn get_regex(&self, _setting: RegexSetting) -> Arc { @@ -292,60 +302,74 @@ 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: cur_time, + }); + nodes.state_next_scan[AddressState::Untested.to_num() as usize].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 { let mut nodes_lock = self.nodes.write().unwrap(); let nodes = nodes_lock.borrow_mut(); - let state_ref = nodes.nodes_to_state.get_mut(&addr).unwrap(); - let ret = state_ref.state; let now = Instant::now(); + + let state_ref = nodes.nodes_to_state.entry(addr).or_insert(Node { + state: AddressState::Untested, + last_services: 0, + last_update: now, + last_good: now, + }); + let ret = state_ref.state; 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.state = AddressState::WasGood; for i in 0..64 { if state_ref.last_services & (1 << i) != 0 { - nodes.good_node_services.get_mut(&i).unwrap().remove(&addr); + nodes.good_node_services[i].remove(&addr); } } state_ref.last_services = 0; - nodes.state_next_scan.get_mut(&AddressState::WasGood).unwrap().push((now, addr)); + nodes.state_next_scan[AddressState::WasGood.to_num() as usize].push((now, addr)); } 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.get_mut(&i).unwrap().insert(addr); + nodes.good_node_services[i].insert(addr); } else if services & (1 << i) == 0 && state_ref.last_services & (1 << i) != 0 { - nodes.good_node_services.get_mut(&i).unwrap().remove(&addr); + nodes.good_node_services[i].remove(&addr); } } state_ref.last_services = services; state_ref.last_good = now; } - nodes.state_next_scan.get_mut(&state).unwrap().push((now, addr)); + nodes.state_next_scan[state.to_num() as usize].push((now, addr)); } state_ref.last_update = now; ret @@ -354,7 +378,7 @@ impl Store { pub fn save_data(&'static self) -> impl Future { 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{}", + 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::ConnsPerSec), self.get_u64(U64Setting::RunTimeout), self.get_u64(U64Setting::WasGoodTimeout), @@ -368,6 +392,9 @@ impl Store { self.get_u64(U64Setting::RescanInterval(AddressState::ProtocolViolation)), self.get_u64(U64Setting::RescanInterval(AddressState::Timeout)), self.get_u64(U64Setting::RescanInterval(AddressState::TimeoutDuringRequest)), + self.get_u64(U64Setting::RescanInterval(AddressState::TimeoutAwaitingPong)), + self.get_u64(U64Setting::RescanInterval(AddressState::TimeoutAwaitingAddr)), + self.get_u64(U64Setting::RescanInterval(AddressState::TimeoutAwaitingBlock)), self.get_u64(U64Setting::RescanInterval(AddressState::Good)), self.get_u64(U64Setting::RescanInterval(AddressState::WasGood)), self.get_regex(RegexSetting::SubverRegex).as_str()); @@ -412,7 +439,7 @@ impl Store { if i.count_ones() == 1 { for j in 0..64 { if i & (1 << j) != 0 { - let set_ref = nodes.good_node_services.get(&j).unwrap(); + let set_ref = &nodes.good_node_services[j]; v4_set = set_ref.iter().filter(|e| e.is_ipv4() && e.port() == 8333) .choose_multiple(&mut rng, 21).iter().map(|e| e.ip()).collect(); v6_set = set_ref.iter().filter(|e| e.is_ipv6() && e.port() == 8333) @@ -426,17 +453,17 @@ impl Store { for j in 0..64 { if i & (1 << j) != 0 { if first_set == None { - first_set = Some(nodes.good_node_services.get(&j).unwrap()); + first_set = Some(&nodes.good_node_services[j]); } else { - second_set = Some(nodes.good_node_services.get(&j).unwrap()); + second_set = Some(&nodes.good_node_services[j]); break; } } } - v4_set = first_set.unwrap().intersection(second_set.unwrap()) + v4_set = first_set.unwrap().intersection(&second_set.unwrap()) .filter(|e| e.is_ipv4() && e.port() == 8333) .choose_multiple(&mut rng, 21).iter().map(|e| e.ip()).collect(); - v6_set = first_set.unwrap().intersection(second_set.unwrap()) + v6_set = first_set.unwrap().intersection(&second_set.unwrap()) .filter(|e| e.is_ipv6() && e.port() == 8333) .choose_multiple(&mut rng, 12).iter().map(|e| e.ip()).collect(); } else { @@ -446,10 +473,10 @@ impl Store { for j in 0..64 { if i & (1 << j) != 0 { if intersection_set_ref == None { - intersection_set_ref = Some(nodes.good_node_services.get(&j).unwrap()); + intersection_set_ref = Some(&nodes.good_node_services[j]); } else { let new_intersection = intersection_set_ref.unwrap() - .intersection(nodes.good_node_services.get(&j).unwrap()).map(|e| (*e).clone()).collect(); + .intersection(&nodes.good_node_services[j]).map(|e| (*e).clone()).collect(); intersection = Some(new_intersection); intersection_set_ref = Some(intersection.as_ref().unwrap()); } @@ -486,15 +513,17 @@ impl Store { let mut res = Vec::with_capacity(results); let cur_time = Instant::now(); - let mut nodes = self.nodes.write().unwrap(); - for (state, state_nodes) in nodes.state_next_scan.iter_mut() { - let cmp_time = cur_time - Duration::from_secs(self.get_u64(U64Setting::RescanInterval(*state))); - let split_point = cmp::min(cmp::min(results - res.len(), per_bucket_results), - 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 mut nodes = self.nodes.write().unwrap(); + 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); + } } } res.shuffle(&mut thread_rng());