Add some util functions to AddressState
[dnsseed-rust] / src / datastore.rs
1 use std::{cmp, mem};
2 use std::collections::{HashSet, HashMap, hash_map};
3 use std::sync::{Arc, RwLock};
4 use std::net::SocketAddr;
5 use std::time::{Duration, Instant};
6 use std::io::{BufRead, BufReader};
7
8 use bitcoin::network::address::Address;
9
10 use rand::thread_rng;
11 use rand::seq::{SliceRandom, IteratorRandom};
12
13 use tokio::prelude::*;
14 use tokio::fs::File;
15 use tokio::io::write_all;
16
17 use regex::Regex;
18
19 #[derive(Clone, Copy, Hash, PartialEq, Eq)]
20 pub enum AddressState {
21         Untested,
22         LowBlockCount,
23         HighBlockCount,
24         LowVersion,
25         BadVersion,
26         NotFullNode,
27         ProtocolViolation,
28         Timeout,
29         TimeoutDuringRequest,
30         Good,
31         WasGood,
32 }
33
34 impl AddressState {
35         pub fn from_num(num: u8) -> Option<AddressState> {
36                 match num {
37                         0x0 => Some(AddressState::Untested),
38                         0x1 => Some(AddressState::LowBlockCount),
39                         0x2 => Some(AddressState::HighBlockCount),
40                         0x3 => Some(AddressState::LowVersion),
41                         0x4 => Some(AddressState::BadVersion),
42                         0x5 => Some(AddressState::NotFullNode),
43                         0x6 => Some(AddressState::ProtocolViolation),
44                         0x7 => Some(AddressState::Timeout),
45                         0x8 => Some(AddressState::TimeoutDuringRequest),
46                         0x9 => Some(AddressState::Good),
47                         0xa => Some(AddressState::WasGood),
48                         _   => None,
49                 }
50         }
51
52         pub fn to_num(&self) -> u8 {
53                 match *self {
54                         AddressState::Untested => 0,
55                         AddressState::LowBlockCount => 1,
56                         AddressState::HighBlockCount => 2,
57                         AddressState::LowVersion => 3,
58                         AddressState::BadVersion => 4,
59                         AddressState::NotFullNode => 5,
60                         AddressState::ProtocolViolation => 6,
61                         AddressState::Timeout => 7,
62                         AddressState::TimeoutDuringRequest => 8,
63                         AddressState::Good => 9,
64                         AddressState::WasGood => 10,
65                 }
66         }
67
68         pub fn to_str(&self) -> &'static str {
69                 match *self {
70                         AddressState::Untested => "Untested",
71                         AddressState::LowBlockCount => "Low Block Count",
72                         AddressState::HighBlockCount => "High Block Count",
73                         AddressState::LowVersion => "Low Version",
74                         AddressState::BadVersion => "Bad Version",
75                         AddressState::NotFullNode => "Not Full Node",
76                         AddressState::ProtocolViolation => "Protocol Violation",
77                         AddressState::Timeout => "Timeout",
78                         AddressState::TimeoutDuringRequest => "Timeout During Request",
79                         AddressState::Good => "Good",
80                         AddressState::WasGood => "Was Good",
81                 }
82         }
83 }
84
85 #[derive(Hash, PartialEq, Eq)]
86 pub enum U64Setting {
87         ConnsPerSec,
88         RunTimeout,
89         WasGoodTimeout,
90         RescanInterval(AddressState),
91         MinProtocolVersion,
92 }
93
94 #[derive(Hash, PartialEq, Eq)]
95 pub enum RegexSetting {
96         SubverRegex,
97 }
98
99 struct Node {
100         last_update: Instant,
101         last_good: Instant, // Ignored unless state is Good or WasGood
102         last_services: u64,
103         state: AddressState,
104 }
105
106 struct Nodes {
107         good_node_services: HashMap<u8, HashSet<SocketAddr>>,
108         nodes_to_state: HashMap<SocketAddr, Node>,
109         state_next_scan: HashMap<AddressState, Vec<(Instant, SocketAddr)>>,
110 }
111 struct NodesMutRef<'a> {
112         good_node_services: &'a mut HashMap<u8, HashSet<SocketAddr>>,
113         nodes_to_state: &'a mut HashMap<SocketAddr, Node>,
114         state_next_scan: &'a mut HashMap<AddressState, Vec<(Instant, SocketAddr)>>,
115
116 }
117 impl Nodes {
118         fn borrow_mut<'a>(&'a mut self) -> NodesMutRef<'a> {
119                 NodesMutRef {
120                         good_node_services: &mut self.good_node_services,
121                         nodes_to_state: &mut self.nodes_to_state,
122                         state_next_scan: &mut self.state_next_scan,
123                 }
124         }
125 }
126
127 pub struct Store {
128         u64_settings: RwLock<HashMap<U64Setting, u64>>,
129         subver_regex: RwLock<Arc<Regex>>,
130         nodes: RwLock<Nodes>,
131         store: String,
132 }
133
134 impl Store {
135         pub fn new(store: String) -> impl Future<Item=Store, Error=()> {
136                 let settings_future = File::open(store.clone() + "/settings").and_then(|f| {
137                         let mut l = BufReader::new(f).lines();
138                         macro_rules! try_read {
139                                 ($lines: expr, $ty: ty) => { {
140                                         match $lines.next() {
141                                                 Some(line) => match line {
142                                                         Ok(line) => match line.parse::<$ty>() {
143                                                                 Ok(res) => res,
144                                                                 Err(e) => return future::err(std::io::Error::new(std::io::ErrorKind::InvalidData, e)),
145                                                         },
146                                                         Err(e) => return future::err(e),
147                                                 },
148                                                 None => return future::err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "")),
149                                         }
150                                 } }
151                         }
152                         let mut u64s = HashMap::with_capacity(15);
153                         u64s.insert(U64Setting::ConnsPerSec, try_read!(l, u64));
154                         u64s.insert(U64Setting::RunTimeout, try_read!(l, u64));
155                         u64s.insert(U64Setting::WasGoodTimeout, try_read!(l, u64));
156                         u64s.insert(U64Setting::MinProtocolVersion, try_read!(l, u64));
157                         u64s.insert(U64Setting::RescanInterval(AddressState::Untested), try_read!(l, u64));
158                         u64s.insert(U64Setting::RescanInterval(AddressState::LowBlockCount), try_read!(l, u64));
159                         u64s.insert(U64Setting::RescanInterval(AddressState::HighBlockCount), try_read!(l, u64));
160                         u64s.insert(U64Setting::RescanInterval(AddressState::LowVersion), try_read!(l, u64));
161                         u64s.insert(U64Setting::RescanInterval(AddressState::BadVersion), try_read!(l, u64));
162                         u64s.insert(U64Setting::RescanInterval(AddressState::NotFullNode), try_read!(l, u64));
163                         u64s.insert(U64Setting::RescanInterval(AddressState::ProtocolViolation), try_read!(l, u64));
164                         u64s.insert(U64Setting::RescanInterval(AddressState::Timeout), try_read!(l, u64));
165                         u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutDuringRequest), try_read!(l, u64));
166                         u64s.insert(U64Setting::RescanInterval(AddressState::Good), try_read!(l, u64));
167                         u64s.insert(U64Setting::RescanInterval(AddressState::WasGood), try_read!(l, u64));
168                         future::ok((u64s, try_read!(l, Regex)))
169                 }).or_else(|_| -> future::FutureResult<(HashMap<U64Setting, u64>, Regex), ()> {
170                         let mut u64s = HashMap::with_capacity(15);
171                         u64s.insert(U64Setting::ConnsPerSec, 10);
172                         u64s.insert(U64Setting::RunTimeout, 120);
173                         u64s.insert(U64Setting::WasGoodTimeout, 21600);
174                         u64s.insert(U64Setting::RescanInterval(AddressState::Untested), 0);
175                         u64s.insert(U64Setting::RescanInterval(AddressState::LowBlockCount), 3600);
176                         u64s.insert(U64Setting::RescanInterval(AddressState::HighBlockCount), 7200);
177                         u64s.insert(U64Setting::RescanInterval(AddressState::LowVersion), 21600);
178                         u64s.insert(U64Setting::RescanInterval(AddressState::BadVersion), 21600);
179                         u64s.insert(U64Setting::RescanInterval(AddressState::NotFullNode), 86400);
180                         u64s.insert(U64Setting::RescanInterval(AddressState::ProtocolViolation), 86400);
181                         u64s.insert(U64Setting::RescanInterval(AddressState::Timeout), 86400);
182                         u64s.insert(U64Setting::RescanInterval(AddressState::TimeoutDuringRequest), 21600);
183                         u64s.insert(U64Setting::RescanInterval(AddressState::Good), 1800);
184                         u64s.insert(U64Setting::RescanInterval(AddressState::WasGood), 1800);
185                         u64s.insert(U64Setting::MinProtocolVersion, 10000); //XXX
186                         future::ok((u64s, Regex::new(".*").unwrap()))
187                 });
188
189                 macro_rules! nodes_uninitd {
190                         () => { {
191                                 let mut state_vecs = HashMap::with_capacity(11);
192                                 state_vecs.insert(AddressState::Untested, Vec::new());
193                                 state_vecs.insert(AddressState::LowBlockCount, Vec::new());
194                                 state_vecs.insert(AddressState::HighBlockCount, Vec::new());
195                                 state_vecs.insert(AddressState::LowVersion, Vec::new());
196                                 state_vecs.insert(AddressState::BadVersion, Vec::new());
197                                 state_vecs.insert(AddressState::NotFullNode, Vec::new());
198                                 state_vecs.insert(AddressState::ProtocolViolation, Vec::new());
199                                 state_vecs.insert(AddressState::Timeout, Vec::new());
200                                 state_vecs.insert(AddressState::TimeoutDuringRequest, Vec::new());
201                                 state_vecs.insert(AddressState::Good, Vec::new());
202                                 state_vecs.insert(AddressState::WasGood, Vec::new());
203                                 let mut good_node_services = HashMap::with_capacity(64);
204                                 for i in 0..64 {
205                                         good_node_services.insert(i, HashSet::new());
206                                 }
207                                 Nodes {
208                                         good_node_services,
209                                         nodes_to_state: HashMap::new(),
210                                         state_next_scan: state_vecs,
211                                 }
212                         } }
213                 }
214
215                 let nodes_future = File::open(store.clone() + "/nodes").and_then(|f| {
216                         let mut res = nodes_uninitd!();
217                         let l = BufReader::new(f).lines();
218                         for line_res in l {
219                                 let line = match line_res {
220                                         Ok(l) => l,
221                                         Err(_) => return future::ok(res),
222                                 };
223                                 let mut line_iter = line.split(',');
224                                 macro_rules! try_read {
225                                         ($lines: expr, $ty: ty) => { {
226                                                 match $lines.next() {
227                                                         Some(line) => match line.parse::<$ty>() {
228                                                                 Ok(res) => res,
229                                                                 Err(_) => return future::ok(res),
230                                                         },
231                                                         None => return future::ok(res),
232                                                 }
233                                         } }
234                                 }
235                                 let sockaddr = try_read!(line_iter, SocketAddr);
236                                 let state = try_read!(line_iter, u8);
237                                 let last_services = try_read!(line_iter, u64);
238                                 let node = Node {
239                                         state: match AddressState::from_num(state) {
240                                                 Some(v) => v,
241                                                 None => return future::ok(res),
242                                         },
243                                         last_services,
244                                         last_update: Instant::now(),
245                                         last_good: Instant::now(),
246                                 };
247                                 if node.state == AddressState::Good {
248                                         for i in 0..64 {
249                                                 if node.last_services & (1 << i) != 0 {
250                                                         res.good_node_services.get_mut(&i).unwrap().insert(sockaddr);
251                                                 }
252                                         }
253                                 }
254                                 res.state_next_scan.get_mut(&node.state).unwrap().push((Instant::now(), sockaddr));
255                                 res.nodes_to_state.insert(sockaddr, node);
256                         }
257                         future::ok(res)
258                 }).or_else(|_| -> future::FutureResult<Nodes, ()> {
259                         future::ok(nodes_uninitd!())
260                 });
261                 settings_future.join(nodes_future).and_then(move |((u64_settings, regex), nodes)| {
262                         future::ok(Store {
263                                 u64_settings: RwLock::new(u64_settings),
264                                 subver_regex: RwLock::new(Arc::new(regex)),
265                                 nodes: RwLock::new(nodes),
266                                 store,
267                         })
268                 })
269         }
270
271         pub fn get_u64(&self, setting: U64Setting) -> u64 {
272                 *self.u64_settings.read().unwrap().get(&setting).unwrap()
273         }
274
275         pub fn set_u64(&self, setting: U64Setting, value: u64) {
276                 *self.u64_settings.write().unwrap().get_mut(&setting).unwrap() = value;
277         }
278
279         pub fn get_node_count(&self, state: AddressState) -> usize {
280                 self.nodes.read().unwrap().state_next_scan.get(&state).unwrap().len()
281         }
282
283         pub fn get_regex(&self, _setting: RegexSetting) -> Arc<Regex> {
284                 Arc::clone(&*self.subver_regex.read().unwrap())
285         }
286
287         pub fn set_regex(&self, _setting: RegexSetting, value: Regex) {
288                 *self.subver_regex.write().unwrap() = Arc::new(value);
289         }
290
291         pub fn add_fresh_nodes(&self, addresses: &Vec<(u32, Address)>) {
292                 let mut nodes = self.nodes.write().unwrap();
293                 let cur_time = Instant::now();
294                 for &(_, ref addr) in addresses {
295                         if let Ok(socketaddr) = addr.socket_addr() {
296                                 match nodes.nodes_to_state.entry(socketaddr.clone()) {
297                                         hash_map::Entry::Vacant(e) => {
298                                                 e.insert(Node {
299                                                         state: AddressState::Untested,
300                                                         last_services: 0,
301                                                         last_update: cur_time,
302                                                         last_good: Instant::now(),
303                                                 });
304                                                 nodes.state_next_scan.get_mut(&AddressState::Untested).unwrap().push((cur_time, socketaddr));
305                                         },
306                                         hash_map::Entry::Occupied(_) => {},
307                                 }
308                         } else {
309                                 //TODO: Handle onions
310                         }
311                 }
312         }
313
314         pub fn set_node_state(&self, addr: SocketAddr, state: AddressState, services: u64) -> bool {
315                 let mut nodes_lock = self.nodes.write().unwrap();
316                 let nodes = nodes_lock.borrow_mut();
317                 let state_ref = nodes.nodes_to_state.get_mut(&addr).unwrap();
318                 let ret = state != state_ref.state;
319                 let now = Instant::now();
320                 if (state_ref.state == AddressState::Good || state_ref.state == AddressState::WasGood)
321                                 && state != AddressState::Good
322                                 && state_ref.last_good >= now - Duration::from_secs(self.get_u64(U64Setting::WasGoodTimeout)) {
323                         state_ref.state = AddressState::WasGood;
324                         for i in 0..64 {
325                                 if state_ref.last_services & (1 << i) != 0 {
326                                         nodes.good_node_services.get_mut(&i).unwrap().remove(&addr);
327                                 }
328                         }
329                         state_ref.last_services = 0;
330                         nodes.state_next_scan.get_mut(&AddressState::WasGood).unwrap().push((now, addr));
331                 } else {
332                         state_ref.state = state;
333                         if state == AddressState::Good {
334                                 for i in 0..64 {
335                                         if services & (1 << i) != 0 && state_ref.last_services & (1 << i) == 0 {
336                                                 nodes.good_node_services.get_mut(&i).unwrap().insert(addr);
337                                         } else if services & (1 << i) == 0 && state_ref.last_services & (1 << i) != 0 {
338                                                 nodes.good_node_services.get_mut(&i).unwrap().remove(&addr);
339                                         }
340                                 }
341                                 state_ref.last_services = services;
342                                 state_ref.last_good = now;
343                         }
344                         nodes.state_next_scan.get_mut(&state).unwrap().push((now, addr));
345                 }
346                 state_ref.last_update = now;
347                 ret
348         }
349
350         pub fn save_data(&'static self) -> impl Future<Item=(), Error=()> {
351                 let settings_file = self.store.clone() + "/settings";
352                 let settings_future = File::create(settings_file.clone() + ".tmp").and_then(move |f| {
353                         let settings_string = format!("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
354                                 self.get_u64(U64Setting::ConnsPerSec),
355                                 self.get_u64(U64Setting::RunTimeout),
356                                 self.get_u64(U64Setting::WasGoodTimeout),
357                                 self.get_u64(U64Setting::MinProtocolVersion),
358                                 self.get_u64(U64Setting::RescanInterval(AddressState::Untested)),
359                                 self.get_u64(U64Setting::RescanInterval(AddressState::LowBlockCount)),
360                                 self.get_u64(U64Setting::RescanInterval(AddressState::HighBlockCount)),
361                                 self.get_u64(U64Setting::RescanInterval(AddressState::LowVersion)),
362                                 self.get_u64(U64Setting::RescanInterval(AddressState::BadVersion)),
363                                 self.get_u64(U64Setting::RescanInterval(AddressState::NotFullNode)),
364                                 self.get_u64(U64Setting::RescanInterval(AddressState::ProtocolViolation)),
365                                 self.get_u64(U64Setting::RescanInterval(AddressState::Timeout)),
366                                 self.get_u64(U64Setting::RescanInterval(AddressState::TimeoutDuringRequest)),
367                                 self.get_u64(U64Setting::RescanInterval(AddressState::Good)),
368                                 self.get_u64(U64Setting::RescanInterval(AddressState::WasGood)),
369                                 self.get_regex(RegexSetting::SubverRegex).as_str());
370                         write_all(f, settings_string).and_then(|(mut f, _)| {
371                                 f.poll_sync_all()
372                         }).and_then(|_| {
373                                 tokio::fs::rename(settings_file.clone() + ".tmp", settings_file)
374                         })
375                 });
376
377                 let nodes_file = self.store.clone() + "/nodes";
378                 let nodes_future = File::create(nodes_file.clone() + ".tmp").and_then(move |f| {
379                         let mut nodes_buff = String::new();
380                         {
381                                 let nodes = self.nodes.read().unwrap();
382                                 nodes_buff.reserve(nodes.nodes_to_state.len() * 20);
383                                 for (ref sockaddr, ref node) in nodes.nodes_to_state.iter() {
384                                         nodes_buff += &sockaddr.to_string();
385                                         nodes_buff += ",";
386                                         nodes_buff += &node.state.to_num().to_string();
387                                         nodes_buff += ",";
388                                         nodes_buff += &node.last_services.to_string();
389                                         nodes_buff += "\n";
390                                 }
391                         }
392                         write_all(f, nodes_buff)
393                 }).and_then(|(mut f, _)| {
394                         f.poll_sync_all()
395                 }).and_then(|_| {
396                         tokio::fs::rename(nodes_file.clone() + ".tmp", nodes_file)
397                 });
398
399                 let dns_file = self.store.clone() + "/nodes.dump";
400                 let dns_future = File::create(dns_file.clone() + ".tmp").and_then(move |f| {
401                         let mut dns_buff = String::new();
402                         {
403                                 let nodes = self.nodes.read().unwrap();
404                                 let mut rng = thread_rng();
405                                 for i in &[1u64, 4, 5, 8, 9, 12, 13, 1024, 1025, 1028, 1029, 1032, 1033, 1036, 1037] {
406                                         let mut v6_set = Vec::new();
407                                         let mut v4_set = Vec::new();
408                                         if i.count_ones() == 1 {
409                                                 for j in 0..64 {
410                                                         if i & (1 << j) != 0 {
411                                                                 let set_ref = nodes.good_node_services.get(&j).unwrap();
412                                                                 v4_set = set_ref.iter().filter(|e| e.is_ipv4() && e.port() == 8333)
413                                                                         .choose_multiple(&mut rng, 21).iter().map(|e| e.ip()).collect();
414                                                                 v6_set = set_ref.iter().filter(|e| e.is_ipv6() && e.port() == 8333)
415                                                                         .choose_multiple(&mut rng, 12).iter().map(|e| e.ip()).collect();
416                                                                 break;
417                                                         }
418                                                 }
419                                         } else if i.count_ones() == 2 {
420                                                 let mut first_set = None;
421                                                 let mut second_set = None;
422                                                 for j in 0..64 {
423                                                         if i & (1 << j) != 0 {
424                                                                 if first_set == None {
425                                                                         first_set = Some(nodes.good_node_services.get(&j).unwrap());
426                                                                 } else {
427                                                                         second_set = Some(nodes.good_node_services.get(&j).unwrap());
428                                                                         break;
429                                                                 }
430                                                         }
431                                                 }
432                                                 v4_set = first_set.unwrap().intersection(second_set.unwrap())
433                                                         .filter(|e| e.is_ipv4() && e.port() == 8333)
434                                                         .choose_multiple(&mut rng, 21).iter().map(|e| e.ip()).collect();
435                                                 v6_set = first_set.unwrap().intersection(second_set.unwrap())
436                                                         .filter(|e| e.is_ipv6() && e.port() == 8333)
437                                                         .choose_multiple(&mut rng, 12).iter().map(|e| e.ip()).collect();
438                                         } else {
439                                                 //TODO: Could optimize this one a bit
440                                                 let mut intersection;
441                                                 let mut intersection_set_ref = None;
442                                                 for j in 0..64 {
443                                                         if i & (1 << j) != 0 {
444                                                                 if intersection_set_ref == None {
445                                                                         intersection_set_ref = Some(nodes.good_node_services.get(&j).unwrap());
446                                                                 } else {
447                                                                         let new_intersection = intersection_set_ref.unwrap()
448                                                                                 .intersection(nodes.good_node_services.get(&j).unwrap()).map(|e| (*e).clone()).collect();
449                                                                         intersection = Some(new_intersection);
450                                                                         intersection_set_ref = Some(intersection.as_ref().unwrap());
451                                                                 }
452                                                         }
453                                                 }
454                                                 v4_set = intersection_set_ref.unwrap().iter()
455                                                         .filter(|e| e.is_ipv4() && e.port() == 8333)
456                                                         .choose_multiple(&mut rng, 21).iter().map(|e| e.ip()).collect();
457                                                 v6_set = intersection_set_ref.unwrap().iter()
458                                                         .filter(|e| e.is_ipv6() && e.port() == 8333)
459                                                         .choose_multiple(&mut rng, 12).iter().map(|e| e.ip()).collect();
460                                         }
461                                         for a in v4_set {
462                                                 dns_buff += &format!("x{:x}.dnsseed\tIN\tA\t{}\n", i, a);
463                                         }
464                                         for a in v6_set {
465                                                 dns_buff += &format!("x{:x}.dnsseed\tIN\tAAAA\t{}\n", i, a);
466                                         }
467                                 }
468                         }
469                         write_all(f, dns_buff)
470                 }).and_then(|(mut f, _)| {
471                         f.poll_sync_all()
472                 }).and_then(|_| {
473                         tokio::fs::rename(dns_file.clone() + ".tmp", dns_file)
474                 });
475
476                 settings_future.join3(nodes_future, dns_future).then(|_| { future::ok(()) })
477         }
478
479         pub fn get_next_scan_nodes(&self) -> Vec<SocketAddr> {
480                 let mut res = Vec::with_capacity(600);
481                 let cur_time = Instant::now();
482                 let mut nodes = self.nodes.write().unwrap();
483                 for (state, state_nodes) in nodes.state_next_scan.iter_mut() {
484                         let cmp_time = cur_time - Duration::from_secs(self.get_u64(U64Setting::RescanInterval(*state)));
485                         let split_point = cmp::min(cmp::min(600 - res.len(), 60),
486                                         state_nodes.binary_search_by(|a| a.0.cmp(&cmp_time)).unwrap_or_else(|idx| idx));
487                         let mut new_nodes = state_nodes.split_off(split_point);
488                         mem::swap(&mut new_nodes, state_nodes);
489                         for (_, node) in new_nodes.drain(..) {
490                                 res.push(node);
491                         }
492                 }
493                 res.shuffle(&mut thread_rng());
494                 res
495         }
496 }