Skip to content

Commit

Permalink
Merge pull request #574 from helium/jg/allow-subscriber-rewards
Browse files Browse the repository at this point in the history
allowing for subscriber mapping rewards without radiopoc rewards
  • Loading branch information
jeffgrunewald authored Jul 20, 2023
2 parents 0f6dc3f + d60620b commit e82216d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 48 deletions.
5 changes: 4 additions & 1 deletion mobile_verifier/src/cli/reward_from_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ impl Cmd {

let mut total_rewards = 0_u64;
let mut owner_rewards = HashMap::<_, u64>::new();
for reward in reward_shares.into_rewards(Decimal::ZERO, &epoch) {
let radio_rewards = reward_shares
.into_rewards(Decimal::ZERO, &epoch)
.ok_or(anyhow::anyhow!("no rewardable events"))?;
for reward in radio_rewards {
if let Some(proto::mobile_reward_share::Reward::RadioReward(proto::RadioReward {
hotspot_key,
poc_reward,
Expand Down
86 changes: 52 additions & 34 deletions mobile_verifier/src/reward_shares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,42 +261,47 @@ impl PocShares {
self,
transfer_rewards_sum: Decimal,
epoch: &'_ Range<DateTime<Utc>>,
) -> impl Iterator<Item = proto::MobileRewardShare> + '_ {
) -> Option<impl Iterator<Item = proto::MobileRewardShare> + '_> {
let total_shares = self.total_shares();
let available_poc_rewards =
get_scheduled_tokens_for_poc_and_dc(epoch.end - epoch.start) - transfer_rewards_sum;
let poc_rewards_per_share = available_poc_rewards / total_shares;
let start_period = epoch.start.encode_timestamp();
let end_period = epoch.end.encode_timestamp();
self.hotspot_shares
.into_iter()
.flat_map(move |(hotspot_key, RadioShares { radio_shares })| {
radio_shares.into_iter().map(move |(cbsd_id, amount)| {
let poc_reward = poc_rewards_per_share * amount;
let hotspot_key: Vec<u8> = hotspot_key.clone().into();
proto::MobileRewardShare {
start_period,
end_period,
reward: Some(proto::mobile_reward_share::Reward::RadioReward(
proto::RadioReward {
hotspot_key,
cbsd_id,
poc_reward: poc_reward
.round_dp_with_strategy(0, RoundingStrategy::ToZero)
.to_u64()
.unwrap_or(0),
..Default::default()
},
)),
}
})
})
.filter(|mobile_reward| match mobile_reward.reward {
Some(proto::mobile_reward_share::Reward::RadioReward(ref radio_reward)) => {
radio_reward.poc_reward > 0
}
_ => false,
})
if let Some(poc_rewards_per_share) = available_poc_rewards.checked_div(total_shares) {
let start_period = epoch.start.encode_timestamp();
let end_period = epoch.end.encode_timestamp();
Some(
self.hotspot_shares
.into_iter()
.flat_map(move |(hotspot_key, RadioShares { radio_shares })| {
radio_shares.into_iter().map(move |(cbsd_id, amount)| {
let poc_reward = poc_rewards_per_share * amount;
let hotspot_key: Vec<u8> = hotspot_key.clone().into();
proto::MobileRewardShare {
start_period,
end_period,
reward: Some(proto::mobile_reward_share::Reward::RadioReward(
proto::RadioReward {
hotspot_key,
cbsd_id,
poc_reward: poc_reward
.round_dp_with_strategy(0, RoundingStrategy::ToZero)
.to_u64()
.unwrap_or(0),
..Default::default()
},
)),
}
})
})
.filter(|mobile_reward| match mobile_reward.reward {
Some(proto::mobile_reward_share::Reward::RadioReward(ref radio_reward)) => {
radio_reward.poc_reward > 0
}
_ => false,
}),
)
} else {
None
}
}
}

Expand Down Expand Up @@ -815,6 +820,7 @@ mod test {
.await
.unwrap()
.into_rewards(Decimal::ZERO, &epoch)
.unwrap()
{
let radio_reward = match mobile_reward.reward {
Some(proto::mobile_reward_share::Reward::RadioReward(radio_reward)) => radio_reward,
Expand Down Expand Up @@ -895,7 +901,7 @@ mod test {
let owner_shares = PocShares { hotspot_shares };
let epoch = now - Duration::hours(1)..now;
let expected_hotspot = gw1;
for mobile_reward in owner_shares.into_rewards(Decimal::ZERO, &epoch) {
for mobile_reward in owner_shares.into_rewards(Decimal::ZERO, &epoch).unwrap() {
let radio_reward = match mobile_reward.reward {
Some(proto::mobile_reward_share::Reward::RadioReward(radio_reward)) => radio_reward,
_ => unreachable!(),
Expand All @@ -904,4 +910,16 @@ mod test {
assert_eq!(actual_hotspot, expected_hotspot);
}
}

#[tokio::test]
async fn skip_empty_radio_rewards() {
let owner_shares = PocShares {
hotspot_shares: HashMap::new(),
};

let now = Utc::now();
let epoch = now - Duration::hours(1)..now;

assert!(owner_shares.into_rewards(Decimal::ZERO, &epoch).is_none());
}
}
28 changes: 15 additions & 13 deletions mobile_verifier/src/rewarder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,24 @@ impl Rewarder {
};
telemetry::data_transfer_rewards_scale(scale);

for mobile_reward_share in
if let Some(mobile_reward_shares) =
poc_rewards.into_rewards(transfer_rewards.reward_sum(), reward_period)
{
self.mobile_rewards
.write(mobile_reward_share, [])
.await?
// Await the returned one shot to ensure that we wrote the file
.await??;
}
for mobile_reward_share in mobile_reward_shares {
self.mobile_rewards
.write(mobile_reward_share, [])
.await?
// Await the returned one shot to ensure that we wrote the file
.await??;
}

for mobile_reward_share in transfer_rewards.into_rewards(reward_period) {
self.mobile_rewards
.write(mobile_reward_share, [])
.await?
// Await the returned one shot to ensure that we wrote the file
.await??;
for mobile_reward_share in transfer_rewards.into_rewards(reward_period) {
self.mobile_rewards
.write(mobile_reward_share, [])
.await?
// Await the returned one shot to ensure that we wrote the file
.await??;
}
}

// Mapper rewards currently include rewards for discovery mapping only.
Expand Down

0 comments on commit e82216d

Please sign in to comment.