Skip to content

Commit

Permalink
misc tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Dec 18, 2024
1 parent 5d0e73e commit 9bce823
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 27 deletions.
1 change: 1 addition & 0 deletions iot_config/src/client/sub_dao_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl SubDaoEpochRewardInfoResolver for SubDaoClient {
Err(status) if status.code() == tonic::Code::NotFound => None,
Err(status) => Err(status)?,
};
tracing::debug!(?response, "fetched subdao epoch info");
Ok(response)
}
}
40 changes: 23 additions & 17 deletions iot_verifier/src/reward_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,14 @@ mod test {
use super::*;
use crate::{reward_share, PriceInfo};
use chrono::Duration;
use helium_lib::token::Token;
use iot_config::sub_dao_epoch_reward_info::EpochRewardInfo;

pub const EPOCH_ADDRESS: &str = "112E7TxoNHV46M6tiPA8N1MkeMeQxc9ztb4JQLXBVAAUfq1kJLoF";
pub const SUB_DAO_ADDRESS: &str = "112NqN2WWMwtK29PMzRby62fDydBJfsCLkCAf392stdok48ovNT6";

const EMISSIONS_POOL_IN_BONES_10_MINUTES: u64 = 618_340_943_683;

fn reward_shares_in_dec(
beacon_shares: Decimal,
witness_shares: Decimal,
Expand All @@ -452,7 +455,6 @@ 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 {
// use the price per bones to get the value of our bones in DC
let value = hnt_amount * hnt_bones_price;
(value / (*DC_USD_PRICE)).round_dp_with_strategy(0, RoundingStrategy::ToNegativeInfinity)
}
Expand Down Expand Up @@ -510,6 +512,20 @@ mod test {
assert_eq!(dec!(7_000_000_000_000), v);
}

#[test]
fn test_price_conversion() {
let token = Token::Hnt;
let hnt_dollar_price = dec!(1.0);
let hnt_price_from_pricer = 100000000_u64;
let hnt_dollar_bone_price = dec!(0.00000001);

let hnt_price = PriceInfo::new(hnt_price_from_pricer, token.decimals());

assert_eq!(hnt_dollar_bone_price, hnt_price.price_per_bone);
assert_eq!(hnt_price_from_pricer, hnt_price.price_in_bones);
assert_eq!(hnt_dollar_price, hnt_price.price_per_token);
}

#[tokio::test]
// test reward distribution where there is a fixed dc spend per gateway
// with the total dc spend across all gateways being significantly lower than the
Expand Down Expand Up @@ -537,7 +553,8 @@ mod test {
.parse()
.expect("failed gw6 parse");

let reward_info = default_rewards_info(618_340_943_683, Duration::minutes(10));
let reward_info =
default_rewards_info(EMISSIONS_POOL_IN_BONES_10_MINUTES, Duration::minutes(10));
let total_data_transfer_tokens_for_period =
get_scheduled_dc_tokens(reward_info.epoch_emissions);
println!("total data transfer scheduled tokens: {total_data_transfer_tokens_for_period}");
Expand Down Expand Up @@ -751,7 +768,8 @@ mod test {
.parse()
.expect("failed gw6 parse");

let reward_info = default_rewards_info(618_340_943_683, Duration::minutes(10));
let reward_info =
default_rewards_info(EMISSIONS_POOL_IN_BONES_10_MINUTES, Duration::minutes(10));
let total_data_transfer_tokens_for_period =
get_scheduled_dc_tokens(reward_info.epoch_emissions);
println!("total data transfer scheduled tokens: {total_data_transfer_tokens_for_period}");
Expand Down Expand Up @@ -950,7 +968,8 @@ mod test {
.parse()
.expect("failed gw6 parse");

let reward_info = default_rewards_info(618_340_943_683, Duration::minutes(10));
let reward_info =
default_rewards_info(EMISSIONS_POOL_IN_BONES_10_MINUTES, Duration::minutes(10));
let total_data_transfer_tokens_for_period =
get_scheduled_dc_tokens(reward_info.epoch_emissions);
println!("total_data_transfer_tokens_for_period: {total_data_transfer_tokens_for_period}");
Expand Down Expand Up @@ -1131,17 +1150,4 @@ mod test {
let hnt_dc_amt = hnt_bones_to_dc(dc_hnt_amt, hnt_bone_price);
assert_eq!(hnt_dc_amt, dc_amount);
}

#[test]
fn test_price_conversion() {
let hnt_dollar_price = dec!(1.0);
let hnt_dollar_bone_price = dec!(0.00000001);
let hnt_price_from_pricer = 100000000_u64;
let pricer_decimals = 8;
let hnt_price = PriceInfo::new(hnt_price_from_pricer, pricer_decimals);

assert_eq!(hnt_dollar_bone_price, hnt_price.price_per_bone);
assert_eq!(hnt_price_from_pricer, hnt_price.price_in_bones);
assert_eq!(hnt_dollar_price, hnt_price.price_per_token);
}
}
3 changes: 1 addition & 2 deletions iot_verifier/src/rewarder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use chrono::{DateTime, TimeZone, Utc};
use db_store::meta;
use file_store::{file_sink, traits::TimestampEncode};
use futures::future::LocalBoxFuture;
use helium_lib::keypair::Pubkey;
use helium_lib::token::Token;
use helium_lib::{keypair::Pubkey, token::Token};
use helium_proto::{
reward_manifest::RewardData::IotRewardData,
services::poc_lora::{
Expand Down
12 changes: 12 additions & 0 deletions iot_verifier/tests/integrations/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ use iot_verifier::{
last_beacon_reciprocity::LastBeaconReciprocity,
last_witness::LastWitness,
poc_report::{InsertBindings, IotStatus, Report, ReportType},
PriceInfo,
};

use helium_lib::token::Token;
use iot_config::sub_dao_epoch_reward_info::EpochRewardInfo;
use prost::Message;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use sqlx::{PgPool, Postgres, Transaction};
use std::{self, ops::DerefMut, str::FromStr};
use tokio::{sync::mpsc::error::TryRecvError, sync::Mutex, time::timeout};

pub const EPOCH_ADDRESS: &str = "112E7TxoNHV46M6tiPA8N1MkeMeQxc9ztb4JQLXBVAAUfq1kJLoF";
pub const SUB_DAO_ADDRESS: &str = "112NqN2WWMwtK29PMzRby62fDydBJfsCLkCAf392stdok48ovNT6";
pub const EMISSIONS_POOL_IN_BONES_24_HOURS: u64 = 89_041_095_890_411;

pub fn default_rewards_info(total_emissions: u64, epoch_duration: Duration) -> EpochRewardInfo {
let now = Utc::now();
Expand All @@ -48,6 +52,14 @@ pub fn default_rewards_info(total_emissions: u64, epoch_duration: Duration) -> E
}
}

pub fn default_price_info() -> PriceInfo {
let token = Token::Hnt;
let price_info = PriceInfo::new(1, token.decimals());
assert_eq!(price_info.price_per_token, dec!(0.00000001));
assert_eq!(price_info.price_per_bone, dec!(0.0000000000000001));
price_info
}

pub fn create_file_sink<T: file_store::traits::MsgBytes>(
) -> (FileSinkClient<T>, MockFileSinkReceiver<T>) {
let (tx, rx) = tokio::sync::mpsc::channel(10);
Expand Down
6 changes: 4 additions & 2 deletions iot_verifier/tests/integrations/rewarder_operations.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::common::{self, default_rewards_info, MockFileSinkReceiver};
use crate::common::{
self, default_rewards_info, MockFileSinkReceiver, EMISSIONS_POOL_IN_BONES_24_HOURS,
};
use chrono::Duration;
use helium_proto::services::poc_lora::{IotRewardShare, OperationalReward};
use iot_verifier::{reward_share, rewarder};
Expand All @@ -9,7 +11,7 @@ use rust_decimal_macros::dec;
async fn test_operations() -> anyhow::Result<()> {
let (iot_rewards_client, mut iot_rewards) = common::create_file_sink();

let reward_info = default_rewards_info(89_041_095_890_411, Duration::hours(24));
let reward_info = default_rewards_info(EMISSIONS_POOL_IN_BONES_24_HOURS, Duration::hours(24));

let (_, rewards) = tokio::join!(
rewarder::reward_operational(&iot_rewards_client, &reward_info),
Expand Down
6 changes: 4 additions & 2 deletions iot_verifier/tests/integrations/rewarder_oracles.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::common::{self, default_rewards_info, MockFileSinkReceiver};
use crate::common::{
self, default_rewards_info, MockFileSinkReceiver, EMISSIONS_POOL_IN_BONES_24_HOURS,
};
use chrono::Duration;
use helium_proto::services::poc_lora::{IotRewardShare, UnallocatedReward};
use iot_verifier::{reward_share, rewarder};
Expand All @@ -10,7 +12,7 @@ use sqlx::PgPool;
async fn test_oracles(_pool: PgPool) -> anyhow::Result<()> {
let (iot_rewards_client, mut iot_rewards) = common::create_file_sink();

let reward_info = default_rewards_info(89_041_095_890_411, Duration::hours(24));
let reward_info = default_rewards_info(EMISSIONS_POOL_IN_BONES_24_HOURS, Duration::hours(24));

let (_, rewards) = tokio::join!(
rewarder::reward_oracles(&iot_rewards_client, &reward_info),
Expand Down
11 changes: 7 additions & 4 deletions iot_verifier/tests/integrations/rewarder_poc_dc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::common::{self, default_rewards_info, MockFileSinkReceiver};
use crate::common::{
self, default_price_info, default_rewards_info, MockFileSinkReceiver,
EMISSIONS_POOL_IN_BONES_24_HOURS,
};
use chrono::{DateTime, Duration as ChronoDuration, Duration, Utc};
use helium_crypto::PublicKeyBinary;
use helium_proto::services::poc_lora::{
Expand All @@ -7,7 +10,7 @@ use helium_proto::services::poc_lora::{
use iot_verifier::{
poc_report::ReportType,
reward_share::{self, GatewayDCShare, GatewayPocShare},
rewarder, PriceInfo,
rewarder,
};
use prost::Message;
use rust_decimal::{prelude::ToPrimitive, Decimal, RoundingStrategy};
Expand All @@ -24,9 +27,9 @@ const HOTSPOT_4: &str = "11eX55faMbqZB7jzN4p67m6w7ScPMH6ubnvCjCPLh72J49PaJEL";
async fn test_poc_and_dc_rewards(pool: PgPool) -> anyhow::Result<()> {
let (iot_rewards_client, mut iot_rewards) = common::create_file_sink();

let reward_info = default_rewards_info(89_041_095_890_411, Duration::hours(24));
let reward_info = default_rewards_info(EMISSIONS_POOL_IN_BONES_24_HOURS, Duration::hours(24));

let price_info = PriceInfo::new(1, 8);
let price_info = default_price_info();

// seed all the things
let mut txn = pool.clone().begin().await?;
Expand Down
1 change: 1 addition & 0 deletions mobile_config/src/client/sub_dao_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl SubDaoEpochRewardInfoResolver for SubDaoClient {
Err(status) if status.code() == tonic::Code::NotFound => None,
Err(status) => Err(status)?,
};
tracing::debug!(?response, "fetched subdao epoch info");
Ok(response)
}
}

0 comments on commit 9bce823

Please sign in to comment.