- pub fn new() -> Store {
- let mut u64s = HashMap::with_capacity(15);
- u64s.insert(U64Setting::ConnsPerSec, 50);
- u64s.insert(U64Setting::RunTimeout, 120);
- u64s.insert(U64Setting::WasGoodTimeout, 21600);
- u64s.insert(U64Setting::RescanInterval(AddressState::Untested), 0);
- 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::TimeoutDuringRequest), 21600);
- u64s.insert(U64Setting::RescanInterval(AddressState::Good), 1800);
- u64s.insert(U64Setting::RescanInterval(AddressState::WasGood), 1800);
- u64s.insert(U64Setting::MinProtocolVersion, 10000); //XXX
- 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, Vec::new());
- }
- Store {
- u64_settings: RwLock::new(u64s),
- subver_regex: RwLock::new(".*".to_string()),
- nodes: RwLock::new(Nodes {
- good_node_services,
- nodes_to_state: HashMap::new(),
- state_next_scan: state_vecs,
- }),
+ pub fn new(store: String) -> impl Future<Item=Store, Error=()> {
+ let settings_future = File::open(store.clone() + "/settings").and_then(|f| {
+ let mut l = BufReader::new(f).lines();
+ macro_rules! try_read {
+ ($lines: expr, $ty: ty) => { {
+ match $lines.next() {
+ Some(line) => match line {
+ Ok(line) => match line.parse::<$ty>() {
+ Ok(res) => res,
+ Err(e) => return future::err(std::io::Error::new(std::io::ErrorKind::InvalidData, e)),
+ },
+ Err(e) => return future::err(e),
+ },
+ None => return future::err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "")),
+ }
+ } }
+ }
+ let mut u64s = HashMap::with_capacity(AddressState::get_count() as usize + 4);
+ u64s.insert(U64Setting::RunTimeout, try_read!(l, u64));
+ u64s.insert(U64Setting::WasGoodTimeout, try_read!(l, u64));
+ u64s.insert(U64Setting::MinProtocolVersion, try_read!(l, u64));
+ u64s.insert(U64Setting::RescanInterval(AddressState::Untested), try_read!(l, u64));
+ u64s.insert(U64Setting::RescanInterval(AddressState::LowBlockCount), try_read!(l, u64));
+ u64s.insert(U64Setting::RescanInterval(AddressState::HighBlockCount), try_read!(l, u64));
+ u64s.insert(U64Setting::RescanInterval(AddressState::LowVersion), try_read!(l, u64));
+ u64s.insert(U64Setting::RescanInterval(AddressState::BadVersion), try_read!(l, u64));
+ u64s.insert(U64Setting::RescanInterval(AddressState::NotFullNode), try_read!(l, u64));
+ 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));
+ u64s.insert(U64Setting::RescanInterval(AddressState::EvilNode), try_read!(l, u64));
+ future::ok((u64s, try_read!(l, Regex)))
+ }).or_else(|_| -> future::FutureResult<(HashMap<U64Setting, u64>, Regex), ()> {
+ 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::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::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::RescanInterval(AddressState::EvilNode), 315360000);
+ u64s.insert(U64Setting::MinProtocolVersion, 70002);
+ future::ok((u64s, Regex::new(".*").unwrap()))
+ });
+
+ 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 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,
+ nodes_to_state: HashMap::new(),
+ state_next_scan: state_vecs,
+ }
+ } }