Skip to content
Merged
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
22 changes: 6 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ triggered = "0"
futures = "*"
futures-util = "*"
prost = "0.12"
once_cell = "1"
lazy_static = "1"
config = { version = "0", default-features = false, features = ["toml"] }
h3o = { version = "0", features = ["serde"] }
xorf = { version = "0", features = ["serde"] }
Expand Down
2 changes: 0 additions & 2 deletions boost_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ serde_json = { workspace = true }
sqlx = { workspace = true }
base64 = { workspace = true }
sha2 = { workspace = true }
lazy_static = { workspace = true }
triggered = { workspace = true }
futures = { workspace = true }
futures-util = { workspace = true }
prost = { workspace = true }
once_cell = { workspace = true }
tokio = { workspace = true }
tokio-util = { workspace = true }
tracing = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion file_store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ futures-util = { workspace = true }
prost = { workspace = true }
bytes = "*"
regex = "1"
lazy_static = { workspace = true }
tracing = { workspace = true }
chrono = { workspace = true }
helium-proto = { workspace = true }
Expand Down
7 changes: 2 additions & 5 deletions file_store/src/file_info.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{error::DecodeError, traits::TimestampDecode, Error, Result};
use chrono::{DateTime, Utc};
use lazy_static::lazy_static;
use regex::Regex;
use serde::Serialize;
use std::{fmt, io, os::unix::fs::MetadataExt, path::Path, str::FromStr};
use std::{fmt, io, os::unix::fs::MetadataExt, path::Path, str::FromStr, sync::LazyLock};

#[derive(Debug, Clone, Serialize)]
pub struct FileInfo {
Expand All @@ -13,9 +12,7 @@ pub struct FileInfo {
pub size: usize,
}

lazy_static! {
static ref RE: Regex = Regex::new(r"([a-z,\d,_]+)\.(\d+)(\.gz)?").unwrap();
}
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"([a-z,\d,_]+)\.(\d+)(\.gz)?").unwrap());

impl FromStr for FileInfo {
type Err = Error;
Expand Down
2 changes: 0 additions & 2 deletions iot_verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ helium-proto = { workspace = true }
http-serde = { workspace = true }
humantime-serde = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
metrics = { workspace = true }
once_cell = { workspace = true }
prost = { workspace = true }
rand = { workspace = true }
retainer = { workspace = true }
Expand Down
35 changes: 18 additions & 17 deletions iot_verifier/src/hex_density.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use file_store::SCALING_PRECISION;
use h3o::{CellIndex, Resolution};
use itertools::Itertools;
use lazy_static::lazy_static;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use std::{cmp, collections::HashMap, sync::Arc};
use std::{
cmp,
collections::HashMap,
sync::{Arc, LazyLock},
};
use tokio::sync::RwLock;

pub struct HexResConfig {
Expand Down Expand Up @@ -48,21 +51,19 @@ const SCALING_RES: [Resolution; 10] = [
Resolution::Four,
];

lazy_static! {
static ref HIP104_RES_CONFIG: HashMap<Resolution, HexResConfig> = {
// Hex resolutions 0 - 3 and 11 and 12 are currently ignored when calculating density;
// For completeness sake their on-chain settings are N=2, TGT=100_000, MAX=100_000
let mut configs = HashMap::new();
configs.insert(Resolution::Four, HexResConfig::new(2, 500, 1000));
configs.insert(Resolution::Five, HexResConfig::new(4, 100, 200));
configs.insert(Resolution::Six, HexResConfig::new(4, 25, 50));
configs.insert(Resolution::Seven, HexResConfig::new(4, 5, 10));
configs.insert(Resolution::Eight, HexResConfig::new(2, 1, 1));
configs.insert(Resolution::Nine, HexResConfig::new(2, 1, 1));
configs.insert(Resolution::Ten, HexResConfig::new(2, 1, 1));
configs
};
}
static HIP104_RES_CONFIG: LazyLock<HashMap<Resolution, HexResConfig>> = LazyLock::new(|| {
// Hex resolutions 0 - 3 and 11 and 12 are currently ignored when calculating density;
// For completeness sake their on-chain settings are N=2, TGT=100_000, MAX=100_000
let mut configs = HashMap::new();
configs.insert(Resolution::Four, HexResConfig::new(2, 500, 1000));
configs.insert(Resolution::Five, HexResConfig::new(4, 100, 200));
configs.insert(Resolution::Six, HexResConfig::new(4, 25, 50));
configs.insert(Resolution::Seven, HexResConfig::new(4, 5, 10));
configs.insert(Resolution::Eight, HexResConfig::new(2, 1, 1));
configs.insert(Resolution::Nine, HexResConfig::new(2, 1, 1));
configs.insert(Resolution::Ten, HexResConfig::new(2, 1, 1));
configs
});

#[derive(Debug, Clone)]
pub struct HexDensityMap(Arc<RwLock<HashMap<u64, Decimal>>>);
Expand Down
30 changes: 14 additions & 16 deletions iot_verifier/src/poc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ use iot_config::{
client::Gateways,
gateway_info::{GatewayInfo, GatewayMetadata},
};
use lazy_static::lazy_static;
use rust_decimal::Decimal;
use sqlx::PgPool;
use std::{f64::consts::PI, time::Duration};
use std::{f64::consts::PI, sync::LazyLock, time::Duration};

pub type GenericVerifyResult<T = ()> = Result<T, InvalidResponse>;

Expand All @@ -47,18 +46,17 @@ const POC_CELL_DISTANCE_MINIMUM: u32 = 8;
/// the resolution at which parent cell distance is derived
const POC_CELL_PARENT_RES: Resolution = Resolution::Eleven;

lazy_static! {
/// Scaling factor when inactive gateway is not found in the tx scaling map (20%).
/// A default tx scale is required to allow for inactive hotspots to become active
/// again when an inactive hotspot's h3 index would otherwise be garbage-collected
/// from density scaling calculations and not finding a value on subsequent lookups
/// would disqualify the hotspot from validating further beacons
static ref DEFAULT_TX_SCALE: Decimal = Decimal::new(2000, 4);
/// max permitted lag between the first witness and all subsequent witnesses
static ref MAX_WITNESS_LAG: chrono::Duration = chrono::Duration::milliseconds(1500);
/// max permitted lag between the beaconer and a witness
static ref MAX_BEACON_TO_WITNESS_LAG: chrono::Duration = chrono::Duration::milliseconds(4000);
}
/// Scaling factor when inactive gateway is not found in the tx scaling map (20%).
/// A default tx scale is required to allow for inactive hotspots to become active
/// again when an inactive hotspot's h3 index would otherwise be garbage-collected
/// from density scaling calculations and not finding a value on subsequent lookups
/// would disqualify the hotspot from validating further beacons
static DEFAULT_TX_SCALE: LazyLock<Decimal> = LazyLock::new(|| Decimal::new(2000, 4));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this decimal wrapped in a LazyLock, but in the next file there’s a half-dozen or so static decimals declared using the dec! macro without any LazyLock?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing I was attempting to change in this PR is the use of lazy_static. All the constructors were left as untouched as possible.

We could ask a similar question about the difference between
dec!(0.2), dec!(0.20), dec!(0.200), and dec!(0.2000).
Decimal::new(2000, 4), Decimal::new(200, 3), Decimal::new(20, 2), and Decimal::new(2, 1).

They're most likely all the same. I don't believe this PR is the place to find that out for a static that's been in use for 2 years and has no reason to change.

/// max permitted lag between the first witness and all subsequent witnesses
static MAX_WITNESS_LAG: chrono::Duration = chrono::Duration::milliseconds(1500);
/// max permitted lag between the beaconer and a witness
static MAX_BEACON_TO_WITNESS_LAG: chrono::Duration = chrono::Duration::milliseconds(4000);

#[derive(Debug, PartialEq)]
pub struct InvalidResponse {
reason: InvalidReason,
Expand Down Expand Up @@ -722,9 +720,9 @@ fn verify_witness_lag(
received_ts: DateTime<Utc>,
) -> GenericVerifyResult {
let (first_event_ts, max_permitted_lag) = if beacon_received_ts <= first_witness_ts {
(beacon_received_ts, *MAX_BEACON_TO_WITNESS_LAG)
(beacon_received_ts, MAX_BEACON_TO_WITNESS_LAG)
} else {
(first_witness_ts, *MAX_WITNESS_LAG)
(first_witness_ts, MAX_WITNESS_LAG)
};
let this_witness_lag = received_ts - first_event_ts;
if this_witness_lag > max_permitted_lag {
Expand Down
47 changes: 22 additions & 25 deletions iot_verifier/src/reward_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,49 @@ use futures::stream::TryStreamExt;
use helium_crypto::PublicKeyBinary;
use helium_proto::services::poc_lora as proto;
use helium_proto::services::poc_lora::iot_reward_share::Reward as ProtoReward;
use lazy_static::lazy_static;
use rust_decimal::prelude::*;
use rust_decimal_macros::dec;
use sqlx::{Postgres, Transaction};
use std::{collections::HashMap, ops::Range};

const DEFAULT_PREC: u32 = 15;

lazy_static! {
static ref BEACON_REWARDS_PER_DAY_PERCENT: Decimal = dec!(0.06);
static ref WITNESS_REWARDS_PER_DAY_PERCENT: Decimal = dec!(0.24);
// Data transfer is allocated 50% of daily rewards
static ref DATA_TRANSFER_REWARDS_PER_DAY_PERCENT: Decimal = dec!(0.50);
// Operations fund is allocated 7% of daily rewards
static ref OPERATIONS_REWARDS_PER_DAY_PERCENT: Decimal = dec!(0.07);
// Oracles fund is allocated 7% of daily rewards
static ref ORACLES_REWARDS_PER_DAY_PERCENT: Decimal = dec!(0.07);
// dc remainer distributed at ration of 4:1 in favour of witnesses
// ie WITNESS_REWARDS_PER_DAY_PERCENT:BEACON_REWARDS_PER_DAY_PERCENT
static ref WITNESS_DC_REMAINER_PERCENT: Decimal = dec!(0.80);
static ref BEACON_DC_REMAINER_PERCENT: Decimal = dec!(0.20);
static ref DC_USD_PRICE: Decimal = dec!(0.00001);
}
static BEACON_REWARDS_PER_DAY_PERCENT: Decimal = dec!(0.06);
static WITNESS_REWARDS_PER_DAY_PERCENT: Decimal = dec!(0.24);
// Data transfer is allocated 50% of daily rewards
static DATA_TRANSFER_REWARDS_PER_DAY_PERCENT: Decimal = dec!(0.50);
// Operations fund is allocated 7% of daily rewards
static OPERATIONS_REWARDS_PER_DAY_PERCENT: Decimal = dec!(0.07);
// Oracles fund is allocated 7% of daily rewards
static ORACLES_REWARDS_PER_DAY_PERCENT: Decimal = dec!(0.07);
// dc remainer distributed at ration of 4:1 in favour of witnesses
// ie WITNESS_REWARDS_PER_DAY_PERCENT:BEACON_REWARDS_PER_DAY_PERCENT
static WITNESS_DC_REMAINER_PERCENT: Decimal = dec!(0.80);
static BEACON_DC_REMAINER_PERCENT: Decimal = dec!(0.20);
static DC_USD_PRICE: Decimal = dec!(0.00001);

pub fn get_scheduled_poc_tokens(
epoch_emissions: Decimal,
dc_transfer_remainder: Decimal,
) -> (Decimal, Decimal) {
(
epoch_emissions * *BEACON_REWARDS_PER_DAY_PERCENT
+ (dc_transfer_remainder * *BEACON_DC_REMAINER_PERCENT),
epoch_emissions * *WITNESS_REWARDS_PER_DAY_PERCENT
+ (dc_transfer_remainder * *WITNESS_DC_REMAINER_PERCENT),
epoch_emissions * BEACON_REWARDS_PER_DAY_PERCENT
+ (dc_transfer_remainder * BEACON_DC_REMAINER_PERCENT),
epoch_emissions * WITNESS_REWARDS_PER_DAY_PERCENT
+ (dc_transfer_remainder * WITNESS_DC_REMAINER_PERCENT),
)
}

pub fn get_scheduled_dc_tokens(epoch_emissions: Decimal) -> Decimal {
epoch_emissions * *DATA_TRANSFER_REWARDS_PER_DAY_PERCENT
epoch_emissions * DATA_TRANSFER_REWARDS_PER_DAY_PERCENT
}

pub fn get_scheduled_ops_fund_tokens(epoch_emissions: Decimal) -> Decimal {
epoch_emissions * *OPERATIONS_REWARDS_PER_DAY_PERCENT
epoch_emissions * OPERATIONS_REWARDS_PER_DAY_PERCENT
}

pub fn get_scheduled_oracle_tokens(epoch_emissions: Decimal) -> Decimal {
epoch_emissions * *ORACLES_REWARDS_PER_DAY_PERCENT
epoch_emissions * ORACLES_REWARDS_PER_DAY_PERCENT
}

#[derive(sqlx::FromRow)]
Expand Down Expand Up @@ -338,7 +335,7 @@ impl GatewayShares {

/// Returns the equivalent amount of Hnt bones for a specified amount of Data Credits
pub fn dc_to_hnt_bones(dc_amount: Decimal, hnt_bone_price: Decimal) -> Decimal {
let dc_in_usd = dc_amount * *DC_USD_PRICE;
let dc_in_usd = dc_amount * DC_USD_PRICE;
(dc_in_usd / hnt_bone_price)
.round_dp_with_strategy(DEFAULT_PREC, RoundingStrategy::ToPositiveInfinity)
}
Expand Down Expand Up @@ -462,7 +459,7 @@ mod test {
/// returns the equiv dc value for a specified hnt bones amount
pub fn hnt_bones_to_dc(hnt_amount: Decimal, hnt_bones_price: Decimal) -> Decimal {
let value = hnt_amount * hnt_bones_price;
(value / (*DC_USD_PRICE)).round_dp_with_strategy(0, RoundingStrategy::ToNegativeInfinity)
(value / (DC_USD_PRICE)).round_dp_with_strategy(0, RoundingStrategy::ToNegativeInfinity)
}

fn rewards_info_1_hour() -> EpochRewardInfo {
Expand Down
Loading