Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewards halvening #577

Merged
merged 4 commits into from
Jul 31, 2023
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
95 changes: 45 additions & 50 deletions iot_verifier/src/reward_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const DEFAULT_PREC: u32 = 15;
lazy_static! {
// TODO: year 1 emissions allocate 30% of total to PoC with 6% to beacons and 24% to witnesses but subsequent years back
// total PoC percentage off 1.5% each year; determine how beacons and witnesses will split the subsequent years' allocations
static ref REWARDS_PER_DAY: Decimal = (Decimal::from(65_000_000_000_u64) / Decimal::from(365)) * Decimal::from(1_000_000); // 178_082_191_780_822
static ref REWARDS_PER_DAY: Decimal = (Decimal::from(32_500_000_000_u64) / Decimal::from(366)) * Decimal::from(1_000_000); // 88_797_814_207_650.273224043715847
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the 366 because 2024 is a leap year? Are rewards not allocated per 365 days?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

correct its due to 2024 being a leap year which is accounted for in the number of days

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
Expand Down Expand Up @@ -473,7 +473,7 @@ mod test {
println!("total_tokens_for_period: {total_tokens_for_period}");

let operation_tokens_for_period = get_scheduled_ops_fund_tokens(epoch_duration);
assert_eq!(519406392694, operation_tokens_for_period);
assert_eq!(258_993_624_772, operation_tokens_for_period);
}

#[test]
Expand Down Expand Up @@ -600,7 +600,6 @@ mod test {
// the sum of rewards distributed should not exceed total allocation
// but due to rounding whilst going to u64 in compute_rewards,
// is permitted to be a few bones less
// tolerance here is 1
assert_eq!(data_transfer_diff, 1);

// assert the expected data transfer rewards amounts per gateway
Expand All @@ -624,16 +623,16 @@ mod test {
// assert the beacon and witness amount, these will now have an allocation
// of any unused data transfer rewards
assert_eq!(rewards.get(&gw4), None); // Validate zero-amount entry filtered out
assert_eq!(gw1_rewards.beacon_amount, 4_341_363_593);
assert_eq!(gw1_rewards.witness_amount, 103_060_196_599);
assert_eq!(gw2_rewards.beacon_amount, 86_827_271_860);
assert_eq!(gw2_rewards.witness_amount, 188_943_693_766);
assert_eq!(gw3_rewards.beacon_amount, 32_560_226_947);
assert_eq!(gw3_rewards.witness_amount, 137_413_595_466);
assert_eq!(gw5_rewards.beacon_amount, 8_682_727_186);
assert_eq!(gw5_rewards.witness_amount, 240_473_792_066);
assert_eq!(gw6_rewards.beacon_amount, 65_120_453_895);
assert_eq!(gw6_rewards.witness_amount, 120_236_896_033);
assert_eq!(gw1_rewards.beacon_amount, 2_161_036_912);
assert_eq!(gw1_rewards.witness_amount, 51_301_137_137);
assert_eq!(gw2_rewards.beacon_amount, 43_220_738_247);
assert_eq!(gw2_rewards.witness_amount, 94_052_084_751);
assert_eq!(gw3_rewards.beacon_amount, 16_207_776_842);
assert_eq!(gw3_rewards.witness_amount, 68_401_516_182);
assert_eq!(gw5_rewards.beacon_amount, 4_322_073_824);
assert_eq!(gw5_rewards.witness_amount, 119_702_653_319);
assert_eq!(gw6_rewards.beacon_amount, 32_415_553_685);
assert_eq!(gw6_rewards.witness_amount, 59_851_326_659);

// assert the total POC rewards allocated equals TOTAL_POC_REWARDS_FOR_PERIOD
// plus the remainder of the total dc transfer rewards for the period
Expand All @@ -657,8 +656,7 @@ mod test {
// the sum of rewards distributed should not exceed the epoch amount
// but due to rounding whilst going to u64 in compute_rewards,
// is permitted to be a few bones less
// tolerance here is 4
assert_eq!(poc_diff, 3);
assert_eq!(poc_diff, 5);
}

#[test]
Expand Down Expand Up @@ -789,30 +787,29 @@ mod test {
// the sum of rewards distributed should not exceed the epoch amount
// but due to rounding whilst going to u64 in compute_rewards,
// is permitted to be a few bones less
// tolerance here is 2
assert_eq!(data_transfer_diff, 3);
assert_eq!(data_transfer_diff, 1);

// assert the expected data transfer rewards amounts per gateway
assert_eq!(gw1_rewards.dc_transfer_amount, 51_528_411_973); // ~8.33% of total rewards
assert_eq!(gw2_rewards.dc_transfer_amount, 51_528_411_973); // ~8.33% of total rewards
assert_eq!(gw3_rewards.dc_transfer_amount, 51_528_411_973); // ~8.33% of total rewards
assert_eq!(gw5_rewards.dc_transfer_amount, 51_528_411_973); // ~8.33% of total rewards
assert_eq!(gw6_rewards.dc_transfer_amount, 412_227_295_788); // ~66.64% of total rewards, or 8x each of the other gateways
assert_eq!(gw1_rewards.dc_transfer_amount, 25_693_811_981); // ~8.33% of total rewards
assert_eq!(gw2_rewards.dc_transfer_amount, 25_693_811_981); // ~8.33% of total rewards
assert_eq!(gw3_rewards.dc_transfer_amount, 25_693_811_981); // ~8.33% of total rewards
assert_eq!(gw5_rewards.dc_transfer_amount, 25_693_811_981); // ~8.33% of total rewards
assert_eq!(gw6_rewards.dc_transfer_amount, 205_550_495_851); // ~66.64% of total rewards, or 8x each of the other gateways

// assert the beacon and witness amount
// these will be rewards solely from POC as there are zero unallocated
// dc transfer rewards
assert_eq!(rewards.get(&gw4), None); // Validate zero-amount entry filtered out
assert_eq!(gw1_rewards.beacon_amount, 1_630_789_302);
assert_eq!(gw1_rewards.witness_amount, 38_713_519_952);
assert_eq!(gw2_rewards.beacon_amount, 32_615_786_040);
assert_eq!(gw2_rewards.witness_amount, 70_974_786_579);
assert_eq!(gw3_rewards.beacon_amount, 12_230_919_765);
assert_eq!(gw3_rewards.witness_amount, 51_618_026_603);
assert_eq!(gw5_rewards.beacon_amount, 3_261_578_604);
assert_eq!(gw5_rewards.witness_amount, 90_331_546_555);
assert_eq!(gw6_rewards.beacon_amount, 24_461_839_530);
assert_eq!(gw6_rewards.witness_amount, 45_165_773_277);
assert_eq!(gw1_rewards.beacon_amount, 813_166_796);
assert_eq!(gw1_rewards.witness_amount, 19_303_872_653);
assert_eq!(gw2_rewards.beacon_amount, 16_263_335_935);
assert_eq!(gw2_rewards.witness_amount, 35_390_433_198);
assert_eq!(gw3_rewards.beacon_amount, 6_098_750_975);
assert_eq!(gw3_rewards.witness_amount, 25_738_496_871);
assert_eq!(gw5_rewards.beacon_amount, 1_626_333_593);
assert_eq!(gw5_rewards.witness_amount, 45_042_369_525);
assert_eq!(gw6_rewards.beacon_amount, 12_197_501_951);
assert_eq!(gw6_rewards.witness_amount, 22_521_184_762);

// assert the total rewards allocated equals TOTAL_POC_REWARDS_FOR_PERIOD
// plus 0% of the total dc transfer rewards for the period
Expand All @@ -835,8 +832,7 @@ mod test {
// the sum of rewards distributed should not exceed the epoch amount
// but due to rounding whilst going to u64 in compute_rewards,
// is permitted to be a few bones less
// tolerance here is 3
assert_eq!(poc_diff, 3);
assert_eq!(poc_diff, 6);
}

#[test]
Expand Down Expand Up @@ -958,25 +954,25 @@ mod test {
assert_eq!(expected_data_transfer_percent.round(), dec!(55));

// assert the expected dc amounts per gateway
assert_eq!(gw1_rewards.dc_transfer_amount, 61_834_091_922); // 10% of total
assert_eq!(gw1_rewards.dc_transfer_amount, 30_832_573_816); // 10% of total
assert_eq!(gw2_rewards.dc_transfer_amount, 0); // 0% of total
assert_eq!(gw3_rewards.dc_transfer_amount, 123_668_183_844); // 20% of total
assert_eq!(gw5_rewards.dc_transfer_amount, 30_917_045_961); // 5% of total
assert_eq!(gw6_rewards.dc_transfer_amount, 123_668_183_844); // 20% of total
assert_eq!(gw3_rewards.dc_transfer_amount, 61_665_147_632); // 20% of total
assert_eq!(gw5_rewards.dc_transfer_amount, 15_416_286_908); // 5% of total
assert_eq!(gw6_rewards.dc_transfer_amount, 61_665_147_632); // 20% of total

// assert the beacon and witness amount, these will now have an allocation
// of any unused data transfer rewards
assert_eq!(rewards.get(&gw4), None); // Validate zero-amount entry filtered out
assert_eq!(gw1_rewards.beacon_amount, 2_853_881_337);
assert_eq!(gw1_rewards.witness_amount, 67_748_661_320);
assert_eq!(gw2_rewards.beacon_amount, 57_077_626_753);
assert_eq!(gw2_rewards.witness_amount, 124_205_879_087);
assert_eq!(gw3_rewards.beacon_amount, 21_404_110_032);
assert_eq!(gw3_rewards.witness_amount, 90_331_548_427);
assert_eq!(gw5_rewards.beacon_amount, 5_707_762_675);
assert_eq!(gw5_rewards.witness_amount, 158_080_209_748);
assert_eq!(gw6_rewards.beacon_amount, 42_808_220_065);
assert_eq!(gw6_rewards.witness_amount, 79_040_104_874);
assert_eq!(gw1_rewards.beacon_amount, 1_423_041_907);
assert_eq!(gw1_rewards.witness_amount, 33_781_777_466);
assert_eq!(gw2_rewards.beacon_amount, 28_460_838_158);
assert_eq!(gw2_rewards.witness_amount, 61_933_258_688);
assert_eq!(gw3_rewards.beacon_amount, 10_672_814_309);
assert_eq!(gw3_rewards.witness_amount, 45_042_369_955);
assert_eq!(gw5_rewards.beacon_amount, 2_846_083_815);
assert_eq!(gw5_rewards.witness_amount, 78_824_147_421);
assert_eq!(gw6_rewards.beacon_amount, 21_345_628_618);
assert_eq!(gw6_rewards.witness_amount, 39_412_073_710);

// assert the total POC rewards allocated equal TOTAL_POC_REWARDS_FOR_PERIOD
// plus 45% of the total dc transfer rewards for the period
Expand All @@ -1002,8 +998,7 @@ mod test {
// the sum of rewards distributed should not exceed the epoch amount
// but due to rounding whilst going to u64 in compute_rewards,
// is permitted to be a few bones less
// tolerance here is 3
assert_eq!(poc_diff, 4);
assert_eq!(poc_diff, 7);
}

#[test]
Expand Down
28 changes: 14 additions & 14 deletions mobile_verifier/src/reward_shares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use rust_decimal_macros::dec;
use std::collections::HashMap;
use std::ops::Range;

/// Total tokens emissions pool per 365 days
const TOTAL_EMISSIONS_POOL: Decimal = dec!(60_000_000_000_000_000);
/// Total tokens emissions pool per 365 days or 366 days for a leap year
const TOTAL_EMISSIONS_POOL: Decimal = dec!(30_000_000_000_000_000);

/// Maximum amount of the total emissions pool allocated for data transfer
/// rewards
Expand Down Expand Up @@ -306,7 +306,7 @@ impl PocShares {
}

pub fn get_total_scheduled_tokens(duration: Duration) -> Decimal {
(TOTAL_EMISSIONS_POOL / dec!(365) / Decimal::from(Duration::hours(24).num_seconds()))
(TOTAL_EMISSIONS_POOL / dec!(366) / Decimal::from(Duration::hours(24).num_seconds()))
* Decimal::from(duration.num_seconds())
}

Expand Down Expand Up @@ -380,14 +380,14 @@ mod test {
.round_dp_with_strategy(0, RoundingStrategy::ToZero)
.to_u64()
.unwrap_or(0);
assert_eq!(164_383_561_643_835, total_epoch_rewards);
assert_eq!(81_967_213_114_754, total_epoch_rewards);

// verify total rewards allocated to mappers the epoch
let total_mapper_rewards = get_scheduled_tokens_for_mappers(epoch.end - epoch.start)
.round_dp_with_strategy(0, RoundingStrategy::ToZero)
.to_u64()
.unwrap_or(0);
assert_eq!(32_876_712_328_767, total_mapper_rewards);
assert_eq!(16_393_442_622_950, total_mapper_rewards);

let expected_reward_per_subscriber = total_mapper_rewards / NUM_SUBSCRIBERS;

Expand All @@ -401,7 +401,7 @@ mod test {
}

// verify the total rewards awared for discovery mapping
assert_eq!(32_876_712_320_000, total_discovery_mapping_rewards);
assert_eq!(16_393_442_620_000, total_discovery_mapping_rewards);

// the sum of rewards distributed should not exceed the epoch amount
// but due to rounding whilst going to u64 for each subscriber,
Expand Down Expand Up @@ -447,7 +447,7 @@ mod test {
// total_rewards will be in bones
assert_eq!(
(total_rewards / dec!(1_000_000) * dec!(24)).trunc(),
dec!(98_630_136)
dec!(49_180_327)
);

let data_transfer_rewards = TransferRewards::from_transfer_sessions(
Expand Down Expand Up @@ -486,7 +486,7 @@ mod test {
payer: payer.clone(),
upload_bytes: 0,
download_bytes: 0,
num_dcs: 4444444444444445,
num_dcs: 2222222222222222,
received_timestamp: DateTime::default(),
}));
}
Expand Down Expand Up @@ -517,11 +517,11 @@ mod test {
// for POC and data transfer (which is 60% of the daily total emissions).
let available_poc_rewards = get_scheduled_tokens_for_poc_and_dc(epoch.end - epoch.start)
- data_transfer_rewards.reward_sum;
assert_eq!(available_poc_rewards.trunc(), dec!(32_876_712_328_767));
assert_eq!(available_poc_rewards.trunc(), dec!(16_393_442_622_950));
assert_eq!(
// Rewards are automatically scaled
data_transfer_rewards.reward(&owner).trunc(),
dec!(65_753_424_657_534)
dec!(32_786_885_245_901)
);
assert_eq!(data_transfer_rewards.reward_scale().round_dp(1), dec!(0.5));
}
Expand Down Expand Up @@ -838,20 +838,20 @@ mod test {
*owner_rewards
.get(&owner1)
.expect("Could not fetch owner1 rewards"),
983_491_394_449
490_402_129_746
);
assert_eq!(
*owner_rewards
.get(&owner2)
.expect("Could not fetch owner2 rewards"),
2_950_474_183_346
1_471_206_389_237
);

assert_eq!(
*owner_rewards
.get(&owner3)
.expect("Could not fetch owner3 rewards"),
175_623_463_294
87_571_808_883
);
assert_eq!(owner_rewards.get(&owner4), None);

Expand All @@ -860,7 +860,7 @@ mod test {
total += *val
}

assert_eq!(total, 4_109_589_041_089); // total emissions for 1 hour
assert_eq!(total, 2_049_180_327_866); // total emissions for 1 hour
}

#[tokio::test]
Expand Down