Skip to content

Commit

Permalink
rename some things
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Nov 9, 2023
1 parent 0f9cf38 commit 90542b5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion mobile_verifier/src/data_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct DataSessionIngestor {

#[derive(sqlx::FromRow, Debug)]
pub struct ServiceProviderSession {
pub sp: PublicKeyBinary,
pub service_provider_id: Vec<u8>,
pub total_dcs: Decimal,
}

Expand Down
57 changes: 26 additions & 31 deletions mobile_verifier/src/reward_shares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,16 @@ impl MapperShares {

#[derive(Default)]
pub struct ServiceProviderShares {
pub service_provider_shares: Vec<ServiceProviderSession>,
pub shares: Vec<ServiceProviderSession>,
}

impl ServiceProviderShares {
pub fn new(service_provider_shares: Vec<ServiceProviderSession>) -> Self {
Self {
service_provider_shares,
}
pub fn new(shares: Vec<ServiceProviderSession>) -> Self {
Self { shares }
}

fn total_data_shares(&self) -> Decimal {
self.service_provider_shares
.iter()
.map(|v| v.total_dcs)
.sum()
self.shares.iter().map(|v| v.total_dcs).sum()
}

pub fn rewards_per_share(
Expand Down Expand Up @@ -243,11 +238,11 @@ impl ServiceProviderShares {
reward_period: &'_ Range<DateTime<Utc>>,
reward_per_share: Decimal,
) -> impl Iterator<Item = proto::MobileRewardShare> + '_ {
self.service_provider_shares
self.shares
.into_iter()
.map(move |sp| proto::ServiceProviderReward {
service_provider: sp.sp.into(),
amount: (sp.total_dcs * reward_per_share)
.map(move |share| proto::ServiceProviderReward {
service_provider_id: share.service_provider_id,
amount: (share.total_dcs * reward_per_share)
.round_dp_with_strategy(0, RoundingStrategy::ToZero)
.to_u64()
.unwrap_or(0),
Expand Down Expand Up @@ -498,24 +493,24 @@ mod test {

#[tokio::test]
async fn service_provider_reward_amount() {
let payer1: PublicKeyBinary = "112NqN2WWMwtK29PMzRby62fDydBJfsCLkCAf392stdok48ovNT6"
.parse()
.expect("failed payer1 parse");
let payer2: PublicKeyBinary = "11sctWiP9r5wDJVuDe1Th4XSL2vaawaLLSQF8f8iokAoMAJHxqp"
.parse()
.expect("failed payer2 parse");
let payer1: Vec<u8> = "112NqN2WWMwtK29PMzRby62fDydBJfsCLkCAf392stdok48ovNT6"
.as_bytes()
.to_vec();
let payer2 = "11sctWiP9r5wDJVuDe1Th4XSL2vaawaLLSQF8f8iokAoMAJHxqp"
.as_bytes()
.to_vec();

let now = Utc::now();
let epoch = (now - Duration::hours(1))..now;
let total_rewards = get_scheduled_tokens_for_service_providers(epoch.end - epoch.start);

let service_provider_sessions = vec![
ServiceProviderSession {
sp: payer1.clone(),
service_provider_id: payer1.clone(),
total_dcs: dec!(1000),
},
ServiceProviderSession {
sp: payer2.clone(),
service_provider_id: payer2.clone(),
total_dcs: dec!(5000),
},
];
Expand All @@ -534,7 +529,7 @@ mod test {
let mut sp_rewards = HashMap::new();
for sp_reward in sp_shares.into_service_provider_rewards(&epoch, rewards_per_share) {
if let Some(MobileReward::ServiceProviderReward(r)) = sp_reward.reward {
sp_rewards.insert(PublicKeyBinary::from(r.service_provider), r.amount);
sp_rewards.insert(r.service_provider_id, r.amount);
}
}
println!("sp_rewards: {:?}", sp_rewards);
Expand All @@ -553,12 +548,12 @@ mod test {
#[tokio::test]
async fn service_provider_reward_amount_capped() {
let mobile_bone_price = dec!(1.0);
let payer1: PublicKeyBinary = "112NqN2WWMwtK29PMzRby62fDydBJfsCLkCAf392stdok48ovNT6"
.parse()
.expect("failed payer1 parse");
let payer2: PublicKeyBinary = "11sctWiP9r5wDJVuDe1Th4XSL2vaawaLLSQF8f8iokAoMAJHxqp"
.parse()
.expect("failed payer2 parse");
let payer1: Vec<u8> = "112NqN2WWMwtK29PMzRby62fDydBJfsCLkCAf392stdok48ovNT6"
.as_bytes()
.to_vec();
let payer2 = "11sctWiP9r5wDJVuDe1Th4XSL2vaawaLLSQF8f8iokAoMAJHxqp"
.as_bytes()
.to_vec();

let now = Utc::now();
let epoch = (now - Duration::hours(1))..now;
Expand All @@ -568,11 +563,11 @@ mod test {
println!("total_rewards_value_in_dc: {total_rewards_value_in_dc}");
let service_provider_sessions = vec![
ServiceProviderSession {
sp: payer1.clone(),
service_provider_id: payer1.clone(),
total_dcs: total_rewards_value_in_dc,
},
ServiceProviderSession {
sp: payer2.clone(),
service_provider_id: payer2.clone(),
total_dcs: total_rewards_value_in_dc
.checked_div(dec!(2))
.expect("failed to assign rewards"),
Expand All @@ -594,7 +589,7 @@ mod test {
let mut sp_rewards = HashMap::new();
for sp_reward in sp_shares.into_service_provider_rewards(&epoch, rewards_per_share) {
if let Some(MobileReward::ServiceProviderReward(r)) = sp_reward.reward {
sp_rewards.insert(PublicKeyBinary::from(r.service_provider), r.amount);
sp_rewards.insert(r.service_provider_id, r.amount);
}
}
let payer1_reward_amount = *sp_rewards
Expand Down
7 changes: 7 additions & 0 deletions reward_index/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ impl Indexer {
},
r.discovery_location_amount,
)),
Some(MobileReward::ServiceProviderReward(r)) => Ok((
RewardKey {
key: bs58::encode(&r.service_provider_id).into_string(),
reward_type: RewardType::MobileSubscriber,
},
r.amount,
)),
_ => bail!("got an invalid reward share"),
}
}
Expand Down

0 comments on commit 90542b5

Please sign in to comment.