Skip to content

Commit

Permalink
change beacon interval settings from duration to int
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Oct 13, 2023
1 parent bfee0cc commit d24ebfb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
7 changes: 2 additions & 5 deletions iot_verifier/pkg/settings-template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 16 additions & 15 deletions iot_verifier/src/poc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Poc {
gateway_cache: &GatewayCache,
region_cache: &RegionCache,
pool: &PgPool,
beacon_interval: Duration,
beacon_interval: u64,
deny_list: &DenyList,
) -> Result<VerifyBeaconResult, VerificationError> {
let beacon = &self.beacon_report.report;
Expand Down Expand Up @@ -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: {:?}",
Expand Down Expand Up @@ -439,27 +439,28 @@ pub fn do_witness_verifications(
fn verify_beacon_schedule(
last_beacon: &Option<LastBeacon>,
beacon_received_ts: DateTime<Utc>,
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:
Expand Down Expand Up @@ -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<Utc> =
DateTime::parse_from_str("2023 Jan 02 00:00:01 +0000", "%Y %b %d %H:%M:%S %z")
.unwrap()
Expand All @@ -1108,15 +1109,15 @@ 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());

// 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());
Expand All @@ -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,
)
);
Expand All @@ -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,
)
);
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions iot_verifier/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Runner {
poc_sink: FileSinkClient,
hex_density_map: HexDensityMap,
) -> anyhow::Result<Self> {
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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions iot_verifier/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit d24ebfb

Please sign in to comment.