Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Aug 14, 2023
1 parent ced6879 commit 7617883
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 46 deletions.
43 changes: 19 additions & 24 deletions file_store/src/emissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ pub struct EmissionsSchedule {
}

impl EmissionsSchedule {
pub fn default() -> Result<Self> {
let data = schedule();
let emissions: Vec<Emission> = serde_json::from_str(&data)?;
pub fn from_json(json: &str) -> Result<Self> {
let emissions: Vec<Emission> = serde_json::from_str(json)?;
Ok(EmissionsSchedule {
schedule: emissions,
})
Expand All @@ -44,6 +43,7 @@ impl EmissionsSchedule {
.find(|emission| datetime >= emission.start_time)
.ok_or("failed to find emissions for specified date {datetime}")
.unwrap();
// return yearly emissions in bones
Ok(res.yearly_emissions * Decimal::from(1_000_000))
}

Expand Down Expand Up @@ -72,23 +72,7 @@ fn is_leap_epoch(cur_year: i32, cur_month: u32) -> bool {
}

fn is_year_a_leap(year: i32) -> bool {
year % 4 == 0 && (year % 100 != 0 || (year % 100 == 0 && year % 400 == 0))
}

fn schedule() -> String {
"[{
\"start_time\": \"2022-08-01T00:00:01Z\",
\"yearly_emissions\": 65000000000
},
{
\"start_time\": \"2023-08-01T00:00:01Z\",
\"yearly_emissions\": 32500000000
},
{
\"start_time\": \"2024-08-01T00:00:01Z\",
\"yearly_emissions\": 16250000000
}]"
.to_string()
!(year % 4 != 0 || year % 100 == 0 && year % 400 != 0)
}

#[cfg(test)]
Expand All @@ -97,14 +81,25 @@ mod test {

#[test]
fn test_leap_year() {
assert_eq!(false, is_leap_epoch(2023, 7));
assert!(!is_leap_epoch(2023, 7));
assert!(is_leap_epoch(2023, 8));
assert!(is_leap_epoch(2024, 7));
assert_eq!(false, is_leap_epoch(2024, 8));
assert!(!is_leap_epoch(2024, 8));

assert_eq!(false, is_leap_epoch(2027, 7));
assert!(!is_leap_epoch(2025, 1));
assert!(!is_leap_epoch(2025, 7));
assert!(!is_leap_epoch(2025, 8));
assert!(!is_leap_epoch(2026, 1));
assert!(!is_leap_epoch(2026, 7));
assert!(!is_leap_epoch(2026, 8));

assert!(!is_leap_epoch(2027, 7));
assert!(is_leap_epoch(2027, 8));
assert!(is_leap_epoch(2028, 7));
assert_eq!(false, is_leap_epoch(2028, 8));
assert!(!is_leap_epoch(2028, 8));

assert!(!is_leap_epoch(2029, 1));
assert!(!is_leap_epoch(2029, 7));
assert!(!is_leap_epoch(2029, 8));
}
}
40 changes: 19 additions & 21 deletions iot_verifier/src/reward_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,19 +485,11 @@ mod test {
}

#[tokio::test]
async fn test_default_emission_schedule() {
let emissions_schedule = EmissionsSchedule::default().unwrap();
let yearly_emissions = emissions_schedule.yearly_emissions(Utc::now()).unwrap();
let daily_emissions = emissions_schedule.daily_emissions(Utc::now()).unwrap();
assert_eq!(dec!(32_500_000_000_000_000), yearly_emissions);
assert_eq!(dec!(88_797_814_207_650.27322404371585), daily_emissions);
// todo assert rest of years
}

#[test]
fn test_non_gateway_reward_shares() {
async fn test_non_gateway_reward_shares() {
let epoch_duration = Duration::hours(1);
let emissions_schedule = EmissionsSchedule::default().unwrap();
let emissions_schedule = EmissionsSchedule::from_file("./src/iot-ex1.json".to_string())
.await
.unwrap();
let daily_emissions = emissions_schedule.daily_emissions(Utc::now()).unwrap();

let total_tokens_for_period = daily_emissions / dec!(24);
Expand All @@ -508,13 +500,15 @@ mod test {
assert_eq!(258_993_624_772, operation_tokens_for_period);
}

#[test]
#[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
// total epoch dc rewards amount
// this results in a significant redistribution of dc rewards to POC
fn test_reward_share_calculation_fixed_dc_spend_with_transfer_distribution() {
let emissions_schedule = EmissionsSchedule::default().unwrap();
async fn test_reward_share_calculation_fixed_dc_spend_with_transfer_distribution() {
let emissions_schedule = EmissionsSchedule::from_file("./src/iot-ex1.json".to_string())
.await
.unwrap();
let daily_emissions = emissions_schedule.daily_emissions(Utc::now()).unwrap();

let iot_price = dec!(359);
Expand Down Expand Up @@ -698,10 +692,12 @@ mod test {
assert_eq!(poc_diff, 5);
}

#[test]
#[tokio::test]
// test reward distribution where there is zero transfer of dc rewards to poc
fn test_reward_share_calculation_without_data_transfer_distribution() {
let emissions_schedule = EmissionsSchedule::default().unwrap();
async fn test_reward_share_calculation_without_data_transfer_distribution() {
let emissions_schedule = EmissionsSchedule::from_file("./src/iot-ex1.json".to_string())
.await
.unwrap();
let daily_emissions = emissions_schedule.daily_emissions(Utc::now()).unwrap();
let iot_price = dec!(359);

Expand Down Expand Up @@ -878,10 +874,12 @@ mod test {
assert_eq!(poc_diff, 6);
}

#[test]
#[tokio::test]
// test reward distribution where there is transfer of dc rewards to poc
fn test_reward_share_calculation_with_data_transfer_distribution() {
let emissions_schedule = EmissionsSchedule::default().unwrap();
async fn test_reward_share_calculation_with_data_transfer_distribution() {
let emissions_schedule = EmissionsSchedule::from_file("./src/iot-ex1.json".to_string())
.await
.unwrap();
let daily_emissions = emissions_schedule.daily_emissions(Utc::now()).unwrap();
let iot_price = dec!(359);

Expand Down
2 changes: 1 addition & 1 deletion iot_verifier/src/rewarder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Rewarder {
scheduler: &Scheduler,
iot_price: Decimal,
) -> anyhow::Result<()> {
let emissions_schedule = EmissionsSchedule::default()?;
let emissions_schedule = EmissionsSchedule::from_file("./iot-ex1.json".to_string()).await?;
let daily_emissions = emissions_schedule.daily_emissions(Utc::now())?;

let gateway_reward_shares =
Expand Down

0 comments on commit 7617883

Please sign in to comment.