From bbfa256038f7f7e2e51059fc86085d1c0c7bdaaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojte=CC=8Cch=20Parka=CC=81n?= Date: Mon, 16 Sep 2024 17:28:45 +0200 Subject: [PATCH] fixed comments from review --- ip_country/src/country_finder.rs | 83 +++++++------------ ip_country/src/lib.rs | 2 - ip_country/src/test_dbip_country.rs | 30 ------- .../src/neighborhood/neighborhood_database.rs | 14 +--- node/src/neighborhood/node_location.rs | 24 +----- node/src/neighborhood/node_record.rs | 13 +++ 6 files changed, 49 insertions(+), 117 deletions(-) delete mode 100644 ip_country/src/test_dbip_country.rs diff --git a/ip_country/src/country_finder.rs b/ip_country/src/country_finder.rs index d816aeb8c..71ddc91a3 100644 --- a/ip_country/src/country_finder.rs +++ b/ip_country/src/country_finder.rs @@ -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() ); } @@ -48,31 +49,6 @@ 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> = Arc::new(Mutex::new(CCFInitializer { initialized: false })); - } - - pub struct CCFInitializer { - pub initialized: bool, //Mutex - } - - 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::*; @@ -80,11 +56,9 @@ mod tests { 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(), @@ -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, @@ -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(), @@ -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(), @@ -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()); @@ -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 @@ -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( @@ -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", @@ -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( @@ -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", @@ -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::::new( - dbip_country::ipv4_country_data(), - ); - let deserializer_ipv6 = CountryBlockDeserializer::::new( - dbip_country::ipv6_country_data(), - ); + let deserializer_ipv4 = + CountryBlockDeserializer::::new(dbip_country::ipv4_country_data()); + let deserializer_ipv6 = + CountryBlockDeserializer::::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", @@ -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(); diff --git a/ip_country/src/lib.rs b/ip_country/src/lib.rs index 5b8e8d26e..3aee5318c 100644 --- a/ip_country/src/lib.rs +++ b/ip_country/src/lib.rs @@ -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; diff --git a/ip_country/src/test_dbip_country.rs b/ip_country/src/test_dbip_country.rs deleted file mode 100644 index a01fed570..000000000 --- a/ip_country/src/test_dbip_country.rs +++ /dev/null @@ -1,30 +0,0 @@ - -// GENERATED CODE: REGENERATE, DO NOT MODIFY! - -pub fn ipv4_country_data() -> (Vec, usize) { - ( - vec![ - 0xA690000300801003, 0xAA103C48826A0496, 0x1DF5F4C718016207, 0xD001E04BB7C3053C, - 0x0128BAA60060F124, 0x804A4ACB80875806, 0x0000000000053437, - ], - 413 - ) -} - -pub fn ipv4_country_data_blocks() -> usize { - 7 -} - -pub fn ipv6_country_data() -> (Vec, usize) { - ( - vec![ - 0x3000040000400007, 0x00C0001400020000, 0xB400150000000700, 0xA004AF8B024C0E23, - 0x0000000400BC2051, - ], - 302 - ) -} - -pub fn ipv6_country_data_blocks() -> usize { - 5 -} diff --git a/node/src/neighborhood/neighborhood_database.rs b/node/src/neighborhood/neighborhood_database.rs index 7fca82961..139aaa2cd 100644 --- a/node/src/neighborhood/neighborhood_database.rs +++ b/node/src/neighborhood/neighborhood_database.rs @@ -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(), diff --git a/node/src/neighborhood/node_location.rs b/node/src/neighborhood/node_location.rs index 9674e6bdc..f895e9557 100644 --- a/node/src/neighborhood/node_location.rs +++ b/node/src/neighborhood/node_location.rs @@ -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() { @@ -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) - } } diff --git a/node/src/neighborhood/node_record.rs b/node/src/neighborhood/node_record.rs index 6a867e902..e87d70f1a 100644 --- a/node/src/neighborhood/node_record.rs +++ b/node/src/neighborhood/node_record.rs @@ -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); }