Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get back with COUNTRY_CODE_FINDER #514

Open
wants to merge 1 commit into
base: GH-784-review-three-head
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions ip_country/src/country_block_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ impl CountryBlockSerializer {
}
}

struct VersionedIPSerializer<IPType: Debug, SegmentNumRep: Debug, const SEGMENTS_COUNT: usize> {
struct VersionedIPSerializer<IPType, SegmentNumRep, const SEGMENTS_COUNT: usize>
where
IPType: Debug,
SegmentNumRep: Debug,
{
prev_start: VersionedIP<IPType, SegmentNumRep, SEGMENTS_COUNT>,
prev_end: VersionedIP<IPType, SegmentNumRep, SEGMENTS_COUNT>,
block_count: usize,
Expand Down Expand Up @@ -310,7 +314,7 @@ where

// Rust forces public visibility on traits that come to be used as type boundaries in any public
// interface. This is how we can meet the requirements while the implementations of such
// traits becomes ineffective from farther than this file. It works as a form of prevention to
// traits become ineffective beyond this file. It works as a form of prevention to
// namespace pollution for such kind of trait to be implemented on massively common types,
// here namely Ipv4Addr or Ipv6Addr
mod semi_private_items {
Expand Down Expand Up @@ -424,7 +428,7 @@ pub trait DeserializerPublic {
fn next(&mut self) -> Option<CountryBlock>;
}

type Ipv4CountryBlockDeserializer = CountryBlockDeserializer<Ipv4Addr, u8, 4>;
pub type Ipv4CountryBlockDeserializer = CountryBlockDeserializer<Ipv4Addr, u8, 4>;

impl Ipv4CountryBlockDeserializer {
pub fn new(country_data: (Vec<u64>, usize)) -> Self {
Expand All @@ -439,7 +443,7 @@ impl Iterator for Ipv4CountryBlockDeserializer {
}
}

type Ipv6CountryBlockDeserializer = CountryBlockDeserializer<Ipv6Addr, u16, 8>;
pub type Ipv6CountryBlockDeserializer = CountryBlockDeserializer<Ipv6Addr, u16, 8>;

impl Ipv6CountryBlockDeserializer {
pub fn new(country_data: (Vec<u64>, usize)) -> Self {
Expand Down Expand Up @@ -594,8 +598,11 @@ where
segment_num_rep: PhantomData<SegmentNumRep>,
}

impl<IPType: Debug, SegmentNumRep: Debug, const SEGMENTS_COUNT: usize>
impl<IPType, SegmentNumRep, const SEGMENTS_COUNT: usize>
VersionedIP<IPType, SegmentNumRep, SEGMENTS_COUNT>
where
IPType: Debug,
SegmentNumRep: Debug,
{
fn new(ip: IPType) -> VersionedIP<IPType, SegmentNumRep, SEGMENTS_COUNT> {
let segment_num_rep = Default::default();
Expand All @@ -607,7 +614,11 @@ impl<IPType: Debug, SegmentNumRep: Debug, const SEGMENTS_COUNT: usize>
}

#[derive(Debug)]
struct StreamRecord<IPType: Debug, SegmentNumRep: Debug, const SEGMENTS_COUNT: usize> {
struct StreamRecord<IPType, SegmentNumRep, const SEGMENTS_COUNT: usize>
where
IPType: Debug,
SegmentNumRep: Debug,
{
start: VersionedIP<IPType, SegmentNumRep, SEGMENTS_COUNT>,
country_idx: usize,
}
Expand Down
22 changes: 13 additions & 9 deletions ip_country/src/country_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ lazy_static! {
}

pub struct CountryCodeFinder {
pub ipv4: Vec<CountryBlock>,
pub ipv6: Vec<CountryBlock>,
ipv4: Vec<CountryBlock>,
ipv6: Vec<CountryBlock>,
}

impl CountryCodeFinder {
Expand Down Expand Up @@ -46,13 +46,17 @@ impl CountryCodeFinder {
}
}

pub fn init(&self) {}
pub fn ensure_init(&self) {
//This should provoke lazy_static to perform the value initialization
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::country_block_serde::CountryBlockDeserializer;
use crate::country_block_serde::{
CountryBlockDeserializer, Ipv4CountryBlockDeserializer, Ipv6CountryBlockDeserializer,
};
use crate::dbip_country;
use std::str::FromStr;
use std::time::SystemTime;
Expand All @@ -69,7 +73,7 @@ mod tests {

#[test]
fn does_not_find_ipv4_address_in_zz_block() {
COUNTRY_CODE_FINDER.init();
COUNTRY_CODE_FINDER.ensure_init();
let time_start = SystemTime::now();
let result = CountryCodeFinder::find_country(
&COUNTRY_CODE_FINDER,
Expand All @@ -80,7 +84,7 @@ mod tests {
assert_eq!(result, None);
let duration = time_end.duration_since(time_start).unwrap();
assert!(
duration.as_millis() < 1,
duration.as_millis() <= 1,
"Duration of the search was too long: {} ms",
duration.as_millis()
);
Expand Down Expand Up @@ -179,13 +183,13 @@ mod tests {
}

#[test]
fn country_blocks_for_ipv4_and_ipv6_are_deserialized_filled_into_vecs() {
fn country_blocks_for_ipv4_and_ipv6_are_deserialized_and_inserted_into_vecs() {
let time_start = SystemTime::now();

let deserializer_ipv4 =
CountryBlockDeserializer::<Ipv4Addr, u8, 4>::new(dbip_country::ipv4_country_data());
Ipv4CountryBlockDeserializer::new(dbip_country::ipv4_country_data());
let deserializer_ipv6 =
CountryBlockDeserializer::<Ipv6Addr, u16, 8>::new(dbip_country::ipv6_country_data());
Ipv6CountryBlockDeserializer::new(dbip_country::ipv6_country_data());

let time_end = SystemTime::now();
let time_start_fill = SystemTime::now();
Expand Down
6 changes: 1 addition & 5 deletions node/src/bootstrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use crate::sub_lib::ui_gateway::UiGatewayConfig;
use crate::sub_lib::utils::db_connection_launch_panic;
use crate::sub_lib::wallet::Wallet;
use futures::try_ready;
use ip_country_lib::country_finder::COUNTRY_CODE_FINDER;
use itertools::Itertools;
use log::LevelFilter;
use masq_lib::blockchains::chains::Chain;
Expand Down Expand Up @@ -509,10 +508,7 @@ impl ConfiguredByPrivilege for Bootstrapper {
self.config.blockchain_bridge_config.chain,
);
// initialization od CountryFinder
let _ = get_node_location(
Some(IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8))),
&COUNTRY_CODE_FINDER,
);
let _ = get_node_location(Some(IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8))));
let node_descriptor = Bootstrapper::make_local_descriptor(
cryptdes.main,
self.config.neighborhood_config.mode.node_addr_opt(),
Expand Down
3 changes: 1 addition & 2 deletions node/src/neighborhood/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ use gossip_acceptor::GossipAcceptor;
use gossip_acceptor::GossipAcceptorReal;
use gossip_producer::GossipProducer;
use gossip_producer::GossipProducerReal;
use ip_country_lib::country_finder::COUNTRY_CODE_FINDER;
use masq_lib::blockchains::chains::Chain;
use masq_lib::crash_point::CrashPoint;
use masq_lib::logger::Logger;
Expand Down Expand Up @@ -546,7 +545,7 @@ impl Neighborhood {
}

fn handle_new_ip_location(&mut self, new_public_ip: IpAddr) {
let node_location_opt = get_node_location(Some(new_public_ip), &COUNTRY_CODE_FINDER);
let node_location_opt = get_node_location(Some(new_public_ip));
self.neighborhood_database
.root_mut()
.metadata
Expand Down
3 changes: 1 addition & 2 deletions node/src/neighborhood/neighborhood_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::sub_lib::neighborhood::NeighborhoodMode;
use crate::sub_lib::node_addr::NodeAddr;
use crate::sub_lib::utils::time_t_timestamp;
use crate::sub_lib::wallet::Wallet;
use ip_country_lib::country_finder::COUNTRY_CODE_FINDER;
use itertools::Itertools;
use masq_lib::logger::Logger;
use masq_lib::utils::ExpectValue;
Expand Down Expand Up @@ -52,7 +51,7 @@ impl NeighborhoodDatabase {
logger: Logger::new("NeighborhoodDatabase"),
};
let location_opt = match neighborhood_mode.node_addr_opt() {
Some(node_addr) => get_node_location(Some(node_addr.ip_addr()), &COUNTRY_CODE_FINDER),
Some(node_addr) => get_node_location(Some(node_addr.ip_addr())),
None => None,
};
let node_record_data = NodeRecordInputs {
Expand Down
22 changes: 6 additions & 16 deletions node/src/neighborhood/node_location.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.

use ip_country_lib;
use ip_country_lib::country_finder::CountryCodeFinder;
use ip_country_lib::country_finder::{CountryCodeFinder, COUNTRY_CODE_FINDER};
use std::net::IpAddr;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
Expand All @@ -10,13 +10,10 @@ pub struct NodeLocation {
pub free_world_bit: bool,
}

pub fn get_node_location(
ip_opt: Option<IpAddr>,
country_code_finder: &CountryCodeFinder,
) -> Option<NodeLocation> {
pub fn get_node_location(ip_opt: Option<IpAddr>) -> Option<NodeLocation> {
match ip_opt {
Some(ip_addr) => {
let country_opt = CountryCodeFinder::find_country(country_code_finder, ip_addr);
let country_opt = CountryCodeFinder::find_country(&COUNTRY_CODE_FINDER, ip_addr);
country_opt.map(|country| NodeLocation {
country_code: country.iso3166.clone(),
free_world_bit: country.free_world,
Expand All @@ -30,16 +27,12 @@ pub fn get_node_location(
mod tests {
use crate::neighborhood::node_location::{get_node_location, NodeLocation};
use crate::neighborhood::node_record::NodeRecordMetadata;
use ip_country_lib::country_finder::COUNTRY_CODE_FINDER;
use std::net::{IpAddr, Ipv4Addr};

#[test]
fn test_node_location() {
let node_location = get_node_location(
Some(IpAddr::V4(Ipv4Addr::new(125, 125, 125, 1))),
&COUNTRY_CODE_FINDER,
)
.unwrap();
let node_location =
get_node_location(Some(IpAddr::V4(Ipv4Addr::new(125, 125, 125, 1)))).unwrap();

assert_eq!(node_location.country_code, "CN");
assert_eq!(node_location.free_world_bit, false);
Expand All @@ -48,10 +41,7 @@ mod tests {
#[test]
fn construct_node_record_metadata_with_free_world_bit() {
let mut metadata = NodeRecordMetadata::new();
metadata.node_location_opt = get_node_location(
Some(IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1))),
&COUNTRY_CODE_FINDER,
);
metadata.node_location_opt = get_node_location(Some(IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1))));
assert_eq!(
metadata.node_location_opt.as_ref().unwrap(),
&NodeLocation {
Expand Down
7 changes: 2 additions & 5 deletions node/src/neighborhood/node_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::sub_lib::neighborhood::{NodeDescriptor, RatePack};
use crate::sub_lib::node_addr::NodeAddr;
use crate::sub_lib::utils::time_t_timestamp;
use crate::sub_lib::wallet::Wallet;
use ip_country_lib::country_finder::COUNTRY_CODE_FINDER;
use masq_lib::blockchains::chains::Chain;
use serde_derive::{Deserialize, Serialize};
use std::collections::btree_set::BTreeSet;
Expand Down Expand Up @@ -307,8 +306,7 @@ impl From<AccessibleGossipRecord> for NodeRecord {
signed_gossip: agr.signed_gossip,
signature: agr.signature,
};
node_record.metadata.node_location_opt =
get_node_location(ip_add_opt, &COUNTRY_CODE_FINDER);
node_record.metadata.node_location_opt = get_node_location(ip_add_opt);
node_record.metadata.node_addr_opt = agr.node_addr_opt;
node_record
}
Expand Down Expand Up @@ -336,8 +334,7 @@ impl TryFrom<&GossipNodeRecord> for NodeRecord {
signed_gossip: gnr.signed_data.clone(),
signature: gnr.signature.clone(),
};
node_record.metadata.node_location_opt =
get_node_location(ip_addr_opt, &COUNTRY_CODE_FINDER);
node_record.metadata.node_location_opt = get_node_location(ip_addr_opt);
node_record.metadata.node_addr_opt = gnr.node_addr_opt.clone();
Ok(node_record)
}
Expand Down