From d24ebfbb8ec5ea7b88b72e5f67cb4f4268523709 Mon Sep 17 00:00:00 2001 From: Andrew McKenzie Date: Fri, 13 Oct 2023 13:44:44 +0100 Subject: [PATCH] change beacon interval settings from duration to int --- iot_verifier/pkg/settings-template.toml | 7 ++---- iot_verifier/src/poc.rs | 31 +++++++++++++------------ iot_verifier/src/runner.rs | 7 +++--- iot_verifier/src/settings.rs | 12 ++++------ 4 files changed, 27 insertions(+), 30 deletions(-) diff --git a/iot_verifier/pkg/settings-template.toml b/iot_verifier/pkg/settings-template.toml index a0c9d9dab..5f2ecce0d 100644 --- a/iot_verifier/pkg/settings-template.toml +++ b/iot_verifier/pkg/settings-template.toml @@ -11,11 +11,8 @@ cache = "/var/data/iot-verified" # # denylist_url = "https://api.github.com/repos/helium/denylist/releases/latest" -# Default beacon interval ( 6 hours) (in seconds) -beacon_interval = 21600 - -# Default beacon interval tolerance ( 10 minutes) (in seconds) -beacon_interval_tolerance = 600 +# Default beacon interval in hours +beacon_interval = 6 # how often the ingestors write out to s3 # this is used to pad the witness loading `after` and `before` periods diff --git a/iot_verifier/src/poc.rs b/iot_verifier/src/poc.rs index 3ee462080..3240b294b 100644 --- a/iot_verifier/src/poc.rs +++ b/iot_verifier/src/poc.rs @@ -115,7 +115,7 @@ impl Poc { gateway_cache: &GatewayCache, region_cache: &RegionCache, pool: &PgPool, - beacon_interval: Duration, + beacon_interval: u64, deny_list: &DenyList, ) -> Result { let beacon = &self.beacon_report.report; @@ -337,7 +337,7 @@ pub fn do_beacon_verifications( beacon_report: &IotBeaconIngestReport, beaconer_info: &GatewayInfo, beaconer_region_params: &[BlockchainRegionParamV1], - beacon_interval: Duration, + beacon_interval: u64, ) -> GenericVerifyResult { tracing::debug!( "verifying beacon from beaconer: {:?}", @@ -439,27 +439,28 @@ pub fn do_witness_verifications( fn verify_beacon_schedule( last_beacon: &Option, beacon_received_ts: DateTime, - beacon_interval: Duration, + beacon_interval: u64, ) -> GenericVerifyResult { match last_beacon { Some(last_beacon) => { - let beacon_interval_hours = beacon_interval.num_hours(); - // get the time window buckets of the last beacon and the current beacon + let buckets_per_day = 24 / beacon_interval as i64; + // get the time buckets of the last beacon and the current beacon // the cur beacon must be in a later bucket than the last in order to be valid - let last_bucket = last_beacon.timestamp.hour() as i64 / beacon_interval_hours; - let mut cur_bucket = beacon_received_ts.hour() as i64 / beacon_interval_hours; + let last_bucket = last_beacon.timestamp.hour() as i64 / beacon_interval as i64; + let mut cur_bucket = beacon_received_ts.hour() as i64 / beacon_interval as i64; // we then need to take into account the days in which the last and cur beacon are received // as the current beacon could be in an earlier bucket but a later day - // or where the cur beacon is in a later bucket but an earlier day + // or the cur beacon could be in a later bucket but an earlier day // the former should be declared valid, whilst the latter declared invalid // the latter should never happen right ? but we accomodate both in the same step + // based on the number of days diff from cur beacon to the previous let days_diff = beacon_received_ts .date_naive() .signed_duration_since(last_beacon.timestamp.date_naive()) .num_days(); // scale the cur bucket up or down depending on // num of days after or before cur beacon is from last beacon - cur_bucket += days_diff * 24 / beacon_interval_hours; + cur_bucket += days_diff * buckets_per_day; if cur_bucket <= last_bucket { tracing::debug!( "beacon verification failed, reason: @@ -1096,7 +1097,7 @@ mod tests { #[test] fn test_verify_beacon_schedule() { let id: &str = "test_id"; - let beacon_interval = Duration::hours(6); + let beacon_interval = 6_u64; let last_beacon_ts: DateTime = DateTime::parse_from_str("2023 Jan 02 00:00:01 +0000", "%Y %b %d %H:%M:%S %z") .unwrap() @@ -1108,7 +1109,7 @@ mod tests { // beacon is in the next window bucket after last beacon, expectation pass assert!(verify_beacon_schedule( &last_beacon, - last_beacon_ts + beacon_interval + Duration::minutes(1), + last_beacon_ts + Duration::hours(beacon_interval as i64 + 1), beacon_interval, ) .is_ok()); @@ -1116,7 +1117,7 @@ mod tests { // beacon is in 3 time buckets after the last beacon, expectation pass assert!(verify_beacon_schedule( &last_beacon, - last_beacon_ts + beacon_interval * 3, + last_beacon_ts + Duration::hours(beacon_interval as i64 * 3), beacon_interval, ) .is_ok()); @@ -1137,7 +1138,7 @@ mod tests { }), verify_beacon_schedule( &last_beacon, - last_beacon_ts + beacon_interval / 2, + last_beacon_ts + Duration::hours(beacon_interval as i64 / 2), beacon_interval, ) ); @@ -1150,7 +1151,7 @@ mod tests { }), verify_beacon_schedule( &last_beacon, - last_beacon_ts - Duration::hours(24) + beacon_interval, + last_beacon_ts - Duration::hours(24) + Duration::hours(beacon_interval as i64), beacon_interval, ) ); @@ -1421,7 +1422,7 @@ mod tests { let beaconer_info = beaconer_gateway_info(Some(LOC0), ProtoRegion::Eu868, true); let entropy_start = Utc.timestamp_millis_opt(ENTROPY_TIMESTAMP).unwrap(); let entropy_end = entropy_start + Duration::minutes(3); - let beacon_interval = Duration::hours(5); + let beacon_interval = 5; let deny_list: DenyList = vec![PublicKeyBinary::from_str(DENIED_PUBKEY1).unwrap()] .try_into() .unwrap(); diff --git a/iot_verifier/src/runner.rs b/iot_verifier/src/runner.rs index 8414c3bdf..b497b7c8f 100644 --- a/iot_verifier/src/runner.rs +++ b/iot_verifier/src/runner.rs @@ -8,7 +8,7 @@ use crate::{ reward_share::GatewayPocShare, telemetry, Settings, }; -use chrono::{Duration as ChronoDuration, Utc}; +use chrono::Utc; use denylist::DenyList; use file_store::{ file_sink::FileSinkClient, @@ -42,7 +42,7 @@ const HIP15_TX_REWARD_UNIT_CAP: Decimal = Decimal::TWO; pub struct Runner { pool: PgPool, - beacon_interval: ChronoDuration, + beacon_interval: u64, max_witnesses_per_poc: u64, beacon_max_retries: u64, witness_max_retries: u64, @@ -94,7 +94,7 @@ impl Runner { poc_sink: FileSinkClient, hex_density_map: HexDensityMap, ) -> anyhow::Result { - let beacon_interval = settings.beacon_interval(); + let beacon_interval = settings.beacon_interval; let max_witnesses_per_poc = settings.max_witnesses_per_poc; let beacon_max_retries = settings.beacon_max_retries; let witness_max_retries = settings.witness_max_retries; @@ -597,6 +597,7 @@ fn fire_invalid_witness_metric(witnesses: &[IotVerifiedWitnessReport]) { #[cfg(test)] mod tests { use super::*; + use chrono::Duration as ChronoDuration; use file_store::iot_witness_report::IotWitnessReport; use helium_crypto::PublicKeyBinary; use helium_proto::services::poc_lora::InvalidReason; diff --git a/iot_verifier/src/settings.rs b/iot_verifier/src/settings.rs index b12e31bd1..4dd551890 100644 --- a/iot_verifier/src/settings.rs +++ b/iot_verifier/src/settings.rs @@ -37,9 +37,11 @@ pub struct Settings { pub reward_offset_minutes: i64, #[serde(default = "default_max_witnesses_per_poc")] pub max_witnesses_per_poc: u64, - /// The cadence at which hotspots are permitted to beacon (in seconds) + /// The cadence at which hotspots are permitted to beacon (in hours) + /// this should be a factor of 24 so that we can have equal + /// beaconing bucket sizes #[serde(default = "default_beacon_interval")] - pub beacon_interval: i64, + pub beacon_interval: u64, /// Trigger interval for generating a transmit scaling map #[serde(default = "default_transmit_scale_interval")] pub transmit_scale_interval: i64, @@ -134,7 +136,7 @@ pub fn default_poc_loader_poll_time() -> u64 { } // Default: 6 hours -pub fn default_beacon_interval() -> i64 { +pub fn default_beacon_interval() -> u64 { 6 * 60 * 60 } @@ -209,10 +211,6 @@ impl Settings { Duration::minutes(self.reward_offset_minutes) } - pub fn beacon_interval(&self) -> Duration { - Duration::seconds(self.beacon_interval) - } - pub fn poc_loader_window_width(&self) -> Duration { Duration::seconds(self.poc_loader_window_width) }