Skip to content

Commit

Permalink
use tokio join in int tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Jan 10, 2024
1 parent 4dc1777 commit dd2d47a
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 163 deletions.
103 changes: 56 additions & 47 deletions mobile_verifier/tests/rewarder_mappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,58 +30,67 @@ async fn test_mapper_rewards(pool: PgPool) -> anyhow::Result<()> {
seed_mapping_data(epoch.end, &mut txn).await?;
txn.commit().await.expect("db txn failed");

tokio::select!(
// run rewards for mappers
_ = rewarder::reward_mappers(&pool, &mobile_rewards_client, &epoch) => {},
Ok((subscriber_rewards, unallocated_reward)) = receive_expected_rewards(&mut mobile_rewards) => {
// assert the mapper rewards
// all 3 subscribers will have an equal share,
// requirement is 1 event per epoch
// subscriber 1 has two events, other two subscribers one event
assert_eq!(
SUBSCRIBER_1.to_string().encode_to_vec(),
subscriber_rewards[0].subscriber_id
);
assert_eq!(5_464_480_874_316, subscriber_rewards[0].discovery_location_amount);
let (_, rewards) = tokio::join!(
rewarder::reward_mappers(&pool, &mobile_rewards_client, &epoch),
receive_expected_rewards(&mut mobile_rewards)
);
if let Ok((subscriber_rewards, unallocated_reward)) = rewards {
// assert the mapper rewards
// all 3 subscribers will have an equal share,
// requirement is 1 event per epoch
// subscriber 1 has two events, other two subscribers one event
assert_eq!(
SUBSCRIBER_1.to_string().encode_to_vec(),
subscriber_rewards[0].subscriber_id
);
assert_eq!(
5_464_480_874_316,
subscriber_rewards[0].discovery_location_amount
);

assert_eq!(
SUBSCRIBER_2.to_string().encode_to_vec(),
subscriber_rewards[1].subscriber_id
);
assert_eq!(5_464_480_874_316, subscriber_rewards[2].discovery_location_amount);
assert_eq!(
SUBSCRIBER_2.to_string().encode_to_vec(),
subscriber_rewards[1].subscriber_id
);
assert_eq!(
5_464_480_874_316,
subscriber_rewards[2].discovery_location_amount
);

assert_eq!(
SUBSCRIBER_3.to_string().encode_to_vec(),
subscriber_rewards[2].subscriber_id
);
assert_eq!(5_464_480_874_316, subscriber_rewards[2].discovery_location_amount);
assert_eq!(
SUBSCRIBER_3.to_string().encode_to_vec(),
subscriber_rewards[2].subscriber_id
);
assert_eq!(
5_464_480_874_316,
subscriber_rewards[2].discovery_location_amount
);

// confirm our unallocated amount
assert_eq!(
UnallocatedRewardType::Mapper as i32,
unallocated_reward.reward_type
);
assert_eq!(2, unallocated_reward.amount);
// confirm our unallocated amount
assert_eq!(
UnallocatedRewardType::Mapper as i32,
unallocated_reward.reward_type
);
assert_eq!(2, unallocated_reward.amount);

// confirm the total rewards allocated matches expectations
let expected_sum = reward_shares::get_scheduled_tokens_for_mappers(epoch.end - epoch.start)
.to_u64()
.unwrap();
let subscriber_sum = subscriber_rewards[0].discovery_location_amount
+ subscriber_rewards[1].discovery_location_amount
+ subscriber_rewards[2].discovery_location_amount
+ unallocated_reward.amount;
assert_eq!(
expected_sum,
subscriber_sum
);
// confirm the total rewards allocated matches expectations
let expected_sum = reward_shares::get_scheduled_tokens_for_mappers(epoch.end - epoch.start)
.to_u64()
.unwrap();
let subscriber_sum = subscriber_rewards[0].discovery_location_amount
+ subscriber_rewards[1].discovery_location_amount
+ subscriber_rewards[2].discovery_location_amount
+ unallocated_reward.amount;
assert_eq!(expected_sum, subscriber_sum);

// confirm the rewarded percentage amount matches expectations
let daily_total = reward_shares::get_total_scheduled_tokens(epoch.end - epoch.start);
let percent = (Decimal::from(subscriber_sum) / daily_total).round_dp_with_strategy(2, RoundingStrategy::MidpointNearestEven);
assert_eq!(percent, dec!(0.2));
}
);
// confirm the rewarded percentage amount matches expectations
let daily_total = reward_shares::get_total_scheduled_tokens(epoch.end - epoch.start);
let percent = (Decimal::from(subscriber_sum) / daily_total)
.round_dp_with_strategy(2, RoundingStrategy::MidpointNearestEven);
assert_eq!(percent, dec!(0.2));
} else {
panic!("no rewards received");
};
Ok(())
}

Expand Down
44 changes: 24 additions & 20 deletions mobile_verifier/tests/rewarder_oracles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,33 @@ async fn test_oracle_rewards(_pool: PgPool) -> anyhow::Result<()> {
let now = Utc::now();
let epoch = (now - ChronoDuration::hours(24))..now;

tokio::select!(
let (_, rewards) = tokio::join!(
// run rewards for oracles
_ = rewarder::reward_oracles(&mobile_rewards_client, &epoch) => {},
Ok(unallocated_reward) = receive_expected_rewards(&mut mobile_rewards) => {
assert_eq!(
UnallocatedRewardType::Oracle as i32,
unallocated_reward.reward_type
);
// confirm our unallocated amount
assert_eq!(3_278_688_524_590, unallocated_reward.amount);
rewarder::reward_oracles(&mobile_rewards_client, &epoch),
receive_expected_rewards(&mut mobile_rewards)
);
if let Ok(unallocated_reward) = rewards {
assert_eq!(
UnallocatedRewardType::Oracle as i32,
unallocated_reward.reward_type
);
// confirm our unallocated amount
assert_eq!(3_278_688_524_590, unallocated_reward.amount);

// confirm the total rewards allocated matches expectations
let expected_sum = reward_shares::get_scheduled_tokens_for_oracles(epoch.end - epoch.start)
.to_u64()
.unwrap();
assert_eq!(expected_sum, unallocated_reward.amount);
// confirm the total rewards allocated matches expectations
let expected_sum = reward_shares::get_scheduled_tokens_for_oracles(epoch.end - epoch.start)
.to_u64()
.unwrap();
assert_eq!(expected_sum, unallocated_reward.amount);

// confirm the rewarded percentage amount matches expectations
let daily_total = reward_shares::get_total_scheduled_tokens(epoch.end - epoch.start);
let percent = (Decimal::from(unallocated_reward.amount) / daily_total).round_dp_with_strategy(2, RoundingStrategy::MidpointNearestEven);
assert_eq!(percent, dec!(0.04));
}
);
// confirm the rewarded percentage amount matches expectations
let daily_total = reward_shares::get_total_scheduled_tokens(epoch.end - epoch.start);
let percent = (Decimal::from(unallocated_reward.amount) / daily_total)
.round_dp_with_strategy(2, RoundingStrategy::MidpointNearestEven);
assert_eq!(percent, dec!(0.04));
} else {
panic!("no rewards received");
};
Ok(())
}

Expand Down
123 changes: 60 additions & 63 deletions mobile_verifier/tests/rewarder_poc_dc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,75 +40,72 @@ async fn test_poc_and_dc_rewards(pool: PgPool) -> anyhow::Result<()> {
seed_data_sessions(epoch.start, &mut txn).await?;
txn.commit().await?;

tokio::select!(
let (_, rewards) = tokio::join!(
// run rewards for poc and dc
_ = rewarder::reward_poc_and_dc(
&pool,
&mobile_rewards_client,
&epoch,
dec!(0.0001),
100,
) => {},
Ok((poc_rewards, dc_rewards, unallocated_poc_reward)) = receive_expected_rewards(&mut mobile_rewards) => {

// assert poc reward outputs
assert_eq!(24_108_003_121_986, poc_rewards[0].poc_reward);
assert_eq!(
HOTSPOT_1.to_string(),
PublicKeyBinary::from(poc_rewards[0].hotspot_key.clone()).to_string()
);
assert_eq!(964_320_124_879, poc_rewards[1].poc_reward);
assert_eq!(
HOTSPOT_2.to_string(),
PublicKeyBinary::from(poc_rewards[1].hotspot_key.clone()).to_string()
);
assert_eq!(24_108_003_121_986, poc_rewards[2].poc_reward);
assert_eq!(
HOTSPOT_3.to_string(),
PublicKeyBinary::from(poc_rewards[2].hotspot_key.clone()).to_string()
);
rewarder::reward_poc_and_dc(&pool, &mobile_rewards_client, &epoch, dec!(0.0001), 100,),
receive_expected_rewards(&mut mobile_rewards)
);
if let Ok((poc_rewards, dc_rewards, unallocated_poc_reward)) = rewards {
// assert poc reward outputs
assert_eq!(24_108_003_121_986, poc_rewards[0].poc_reward);
assert_eq!(
HOTSPOT_1.to_string(),
PublicKeyBinary::from(poc_rewards[0].hotspot_key.clone()).to_string()
);
assert_eq!(964_320_124_879, poc_rewards[1].poc_reward);
assert_eq!(
HOTSPOT_2.to_string(),
PublicKeyBinary::from(poc_rewards[1].hotspot_key.clone()).to_string()
);
assert_eq!(24_108_003_121_986, poc_rewards[2].poc_reward);
assert_eq!(
HOTSPOT_3.to_string(),
PublicKeyBinary::from(poc_rewards[2].hotspot_key.clone()).to_string()
);

// assert unallocated amount
assert_eq!(
UnallocatedRewardType::Poc as i32,
unallocated_poc_reward.reward_type
);
assert_eq!(1, unallocated_poc_reward.amount);
// assert unallocated amount
assert_eq!(
UnallocatedRewardType::Poc as i32,
unallocated_poc_reward.reward_type
);
assert_eq!(1, unallocated_poc_reward.amount);

// assert the dc reward outputs
assert_eq!(500_000, dc_rewards[0].dc_transfer_reward);
assert_eq!(
HOTSPOT_1.to_string(),
PublicKeyBinary::from(dc_rewards[0].hotspot_key.clone()).to_string()
);
assert_eq!(500_000, dc_rewards[1].dc_transfer_reward);
assert_eq!(
HOTSPOT_2.to_string(),
PublicKeyBinary::from(dc_rewards[1].hotspot_key.clone()).to_string()
);
assert_eq!(500_000, dc_rewards[2].dc_transfer_reward);
assert_eq!(
HOTSPOT_3.to_string(),
PublicKeyBinary::from(dc_rewards[2].hotspot_key.clone()).to_string()
);
// assert the dc reward outputs
assert_eq!(500_000, dc_rewards[0].dc_transfer_reward);
assert_eq!(
HOTSPOT_1.to_string(),
PublicKeyBinary::from(dc_rewards[0].hotspot_key.clone()).to_string()
);
assert_eq!(500_000, dc_rewards[1].dc_transfer_reward);
assert_eq!(
HOTSPOT_2.to_string(),
PublicKeyBinary::from(dc_rewards[1].hotspot_key.clone()).to_string()
);
assert_eq!(500_000, dc_rewards[2].dc_transfer_reward);
assert_eq!(
HOTSPOT_3.to_string(),
PublicKeyBinary::from(dc_rewards[2].hotspot_key.clone()).to_string()
);

// confirm the total rewards allocated matches expectations
let poc_sum: u64 = poc_rewards.iter().map(|r| r.poc_reward).sum();
let dc_sum: u64 = dc_rewards.iter().map(|r| r.dc_transfer_reward).sum();
let unallocated_sum: u64 = unallocated_poc_reward.amount;
let total = poc_sum + dc_sum + unallocated_sum;
// confirm the total rewards allocated matches expectations
let poc_sum: u64 = poc_rewards.iter().map(|r| r.poc_reward).sum();
let dc_sum: u64 = dc_rewards.iter().map(|r| r.dc_transfer_reward).sum();
let unallocated_sum: u64 = unallocated_poc_reward.amount;
let total = poc_sum + dc_sum + unallocated_sum;

let expected_sum = reward_shares::get_scheduled_tokens_for_poc(epoch.end - epoch.start)
.to_u64()
.unwrap();
assert_eq!(expected_sum, total);
let expected_sum = reward_shares::get_scheduled_tokens_for_poc(epoch.end - epoch.start)
.to_u64()
.unwrap();
assert_eq!(expected_sum, total);

// confirm the rewarded percentage amount matches expectations
let daily_total = reward_shares::get_total_scheduled_tokens(epoch.end - epoch.start);
let percent = (Decimal::from(total) / daily_total).round_dp_with_strategy(2, RoundingStrategy::MidpointNearestEven);
assert_eq!(percent, dec!(0.6));
}
);
// confirm the rewarded percentage amount matches expectations
let daily_total = reward_shares::get_total_scheduled_tokens(epoch.end - epoch.start);
let percent = (Decimal::from(total) / daily_total)
.round_dp_with_strategy(2, RoundingStrategy::MidpointNearestEven);
assert_eq!(percent, dec!(0.6));
} else {
panic!("no rewards received");
};
Ok(())
}

Expand Down
Loading

0 comments on commit dd2d47a

Please sign in to comment.