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