Skip to content

Commit

Permalink
fixed comments from review
Browse files Browse the repository at this point in the history
  • Loading branch information
czarte committed Sep 16, 2024
1 parent 4259ddf commit bbfa256
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 117 deletions.
83 changes: 30 additions & 53 deletions ip_country/src/country_finder.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::country_block_serde::CountryBlockDeserializer;
use crate::country_block_stream::{Country, CountryBlock};
use crate::dbip_country;
use itertools::Itertools;
use lazy_static::lazy_static;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

lazy_static! {
pub static ref COUNTRY_CODE_FINDER: CountryCodeFinder = CountryCodeFinder::new(
crate::dbip_country::ipv4_country_data(),
crate::dbip_country::ipv6_country_data()
dbip_country::ipv4_country_data(),
dbip_country::ipv6_country_data()
);
}

Expand Down Expand Up @@ -48,43 +49,16 @@ impl CountryCodeFinder {
pub fn init(&self) {}
}

pub mod country_code_static_initializer {
use std::sync::{Arc, Mutex};
use lazy_static::lazy_static;
use crate::country_finder::COUNTRY_CODE_FINDER;

lazy_static! {
pub static ref COUNTRY_CODE_FINDER_INITIALIZED: Arc<Mutex<CCFInitializer>> = Arc::new(Mutex::new(CCFInitializer { initialized: false }));
}

pub struct CCFInitializer {
pub initialized: bool, //Mutex<bool>
}

impl CCFInitializer {
pub fn check_initialized(&mut self) -> bool {
let mut is_initialized = self.initialized;
if !is_initialized {
COUNTRY_CODE_FINDER.init();
is_initialized = true;
}
is_initialized
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::country_block_serde::CountryBlockDeserializer;
use crate::dbip_country;
use std::str::FromStr;
use std::time::SystemTime;
use crate::country_finder::country_code_static_initializer::COUNTRY_CODE_FINDER_INITIALIZED;

#[test]
fn finds_ipv4_address_in_fourth_block() {
COUNTRY_CODE_FINDER_INITIALIZED.lock().expect("Mutex poisoned").check_initialized();
let result = CountryCodeFinder::find_country(
&COUNTRY_CODE_FINDER,
IpAddr::from_str("1.0.6.15").unwrap(),
Expand All @@ -95,7 +69,7 @@ mod tests {

#[test]
fn does_not_find_ipv4_address_in_zz_block() {
COUNTRY_CODE_FINDER_INITIALIZED.lock().expect("Mutex poisoned").check_initialized();
COUNTRY_CODE_FINDER.init();
let time_start = SystemTime::now();
let result = CountryCodeFinder::find_country(
&COUNTRY_CODE_FINDER,
Expand All @@ -114,7 +88,6 @@ mod tests {

#[test]
fn finds_ipv6_address_in_fourth_block() {
COUNTRY_CODE_FINDER_INITIALIZED.lock().expect("Mutex poisoned").check_initialized();
let result = CountryCodeFinder::find_country(
&COUNTRY_CODE_FINDER,
IpAddr::from_str("2001:2::").unwrap(),
Expand All @@ -125,7 +98,6 @@ mod tests {

#[test]
fn does_not_find_ipv6_address_in_zz_block() {
COUNTRY_CODE_FINDER_INITIALIZED.lock().expect("Mutex poisoned").check_initialized();
let result = CountryCodeFinder::find_country(
&COUNTRY_CODE_FINDER,
IpAddr::from_str("0:0:5:0:0:0:0:0").unwrap(),
Expand All @@ -136,13 +108,11 @@ mod tests {

#[test]
fn real_test_ipv4_with_google() {
COUNTRY_CODE_FINDER_INITIALIZED.lock().expect("Mutex poisoned").check_initialized();
let result = CountryCodeFinder::
find_country(
&COUNTRY_CODE_FINDER,
IpAddr::from_str("142.250.191.132").unwrap(), // dig www.google.com A
)
.unwrap();
let result = CountryCodeFinder::find_country(
&COUNTRY_CODE_FINDER,
IpAddr::from_str("142.250.191.132").unwrap(), // dig www.google.com A
)
.unwrap();

assert_eq!(result.free_world, true);
assert_eq!(result.iso3166, "US".to_string());
Expand All @@ -151,7 +121,6 @@ mod tests {

#[test]
fn real_test_ipv4_with_cz_ip() {
COUNTRY_CODE_FINDER_INITIALIZED.lock().expect("Mutex poisoned").check_initialized();
let result = CountryCodeFinder::find_country(
&COUNTRY_CODE_FINDER,
IpAddr::from_str("77.75.77.222").unwrap(), // dig www.seznam.cz A
Expand All @@ -165,7 +134,6 @@ mod tests {

#[test]
fn real_test_ipv4_with_sk_ip() {
COUNTRY_CODE_FINDER_INITIALIZED.lock().expect("Mutex poisoned").check_initialized();
let time_start = SystemTime::now();

let result = CountryCodeFinder::find_country(
Expand All @@ -175,10 +143,11 @@ mod tests {
.unwrap();

let time_end = SystemTime::now();
let duration = time_end.duration_since(time_start).unwrap();

assert_eq!(result.free_world, true);
assert_eq!(result.iso3166, "SK".to_string());
assert_eq!(result.name, "Slovakia".to_string());
let duration = time_end.duration_since(time_start).unwrap();
assert!(
duration.as_millis() < 1,
"Duration of the search was too long: {} millisecond",
Expand All @@ -188,7 +157,6 @@ mod tests {

#[test]
fn real_test_ipv6_with_google() {
COUNTRY_CODE_FINDER_INITIALIZED.lock().expect("Mutex poisoned").check_initialized();
let time_start = SystemTime::now();

let result = CountryCodeFinder::find_country(
Expand All @@ -198,10 +166,11 @@ mod tests {
.unwrap();

let time_end = SystemTime::now();
let duration = time_end.duration_since(time_start).unwrap();

assert_eq!(result.free_world, true);
assert_eq!(result.iso3166, "US".to_string());
assert_eq!(result.name, "United States".to_string());
let duration = time_end.duration_since(time_start).unwrap();
assert!(
duration.as_millis() < 1,
"Duration of the search was too long: {} ms",
Expand All @@ -213,20 +182,29 @@ mod tests {
fn country_blocks_for_ipv4_and_ipv6_are_deserialized_filled_into_vecs() {
let time_start = SystemTime::now();

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

let time_end = SystemTime::now();
let time_start_fill = SystemTime::now();
let _ = deserializer_ipv4.collect_vec();
let _ = deserializer_ipv6.collect_vec();

let country_block_finder_ipv4 = deserializer_ipv4.collect_vec();
let country_block_finder_ipv6 = deserializer_ipv6.collect_vec();

let time_end_fill = SystemTime::now();
let duration_deserialize = time_end.duration_since(time_start).unwrap();
let duration_fill = time_end_fill.duration_since(time_start_fill).unwrap();

assert_eq!(
country_block_finder_ipv4.len(),
dbip_country::ipv4_country_block_count()
);
assert_eq!(
country_block_finder_ipv6.len(),
dbip_country::ipv6_country_block_count()
);
assert!(
duration_deserialize.as_secs() < 15,
"Duration of the deserialization was too long: {} ms",
Expand All @@ -241,7 +219,6 @@ mod tests {

#[test]
fn check_ipv4_ipv6_country_blocks_length() {
COUNTRY_CODE_FINDER_INITIALIZED.lock().expect("Mutex poisoned").check_initialized();
let country_block_len_ipv4 = COUNTRY_CODE_FINDER.ipv4.len();
let country_block_len_ipv6 = COUNTRY_CODE_FINDER.ipv6.len();

Expand Down
2 changes: 0 additions & 2 deletions ip_country/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ pub mod country_finder;
pub mod ip_country;
#[rustfmt::skip]
pub mod dbip_country;
#[rustfmt::skip]
pub mod test_dbip_country;
30 changes: 0 additions & 30 deletions ip_country/src/test_dbip_country.rs

This file was deleted.

14 changes: 4 additions & 10 deletions node/src/neighborhood/neighborhood_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,10 @@ impl NeighborhoodDatabase {
by_ip_addr: HashMap::new(),
logger: Logger::new("NeighborhoodDatabase"),
};

let location_opt = get_node_location(
Some(
neighborhood_mode
.node_addr_opt()
.unwrap_or_default()
.ip_addr(),
),
&COUNTRY_CODE_FINDER,
);
let location_opt = match neighborhood_mode.node_addr_opt() {
Some(node_addr) => get_node_location(Some(node_addr.ip_addr()), &COUNTRY_CODE_FINDER),
None => None,
};
let node_record_data = NodeRecordInputs {
earning_wallet,
rate_pack: *neighborhood_mode.rate_pack(),
Expand Down
24 changes: 2 additions & 22 deletions node/src/neighborhood/node_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ pub fn get_node_location(

#[cfg(test)]
mod tests {
use crate::neighborhood::gossip::GossipBuilder;
use crate::neighborhood::node_location::{get_node_location, NodeLocation};
use crate::neighborhood::node_record::{NodeRecord, NodeRecordMetadata};
use crate::test_utils::neighborhood_test_utils::{db_from_node, make_node_record};
use std::net::{IpAddr, Ipv4Addr};
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() {
Expand Down Expand Up @@ -68,22 +66,4 @@ mod tests {
}
);
}

#[test]
fn node_record_from_gossip_with_addr_and_country_is_populated_with_right_addr_and_free_world_bit(
) {
let mut original_node_record = make_node_record(2222, true);

let db = db_from_node(&original_node_record);
let builder = GossipBuilder::new(&db);

let builder = builder.node(original_node_record.public_key(), true);

let mut gossip = builder.build();
let gossip_result = gossip.node_records.remove(0);
let result_node_record = NodeRecord::try_from(&gossip_result).unwrap();

original_node_record.metadata.last_update = result_node_record.last_updated();
assert_eq!(result_node_record, original_node_record)
}
}
13 changes: 13 additions & 0 deletions node/src/neighborhood/node_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,19 @@ mod tests {
before <= actual_node_record.metadata.last_update
&& actual_node_record.metadata.last_update <= after
);
assert_eq!(
actual_node_record.inner.country_code_opt,
Some("AU".to_string())
);
assert_eq!(
actual_node_record
.metadata
.node_location_opt
.as_ref()
.unwrap()
.free_world_bit,
true
);
expected_node_record.metadata.last_update = actual_node_record.metadata.last_update;
assert_eq!(actual_node_record, expected_node_record);
}
Expand Down

0 comments on commit bbfa256

Please sign in to comment.