From dd2d47ab0ba990557b326695a46e00cd9616c6df Mon Sep 17 00:00:00 2001 From: Andrew McKenzie Date: Wed, 10 Jan 2024 13:41:50 +0000 Subject: [PATCH] use tokio join in int tests --- mobile_verifier/tests/rewarder_mappers.rs | 103 +++++++++------- mobile_verifier/tests/rewarder_oracles.rs | 44 ++++--- mobile_verifier/tests/rewarder_poc_dc.rs | 123 +++++++++---------- mobile_verifier/tests/rewarder_sp_rewards.rs | 66 +++++----- 4 files changed, 173 insertions(+), 163 deletions(-) diff --git a/mobile_verifier/tests/rewarder_mappers.rs b/mobile_verifier/tests/rewarder_mappers.rs index b7d95f30a..00e59f674 100644 --- a/mobile_verifier/tests/rewarder_mappers.rs +++ b/mobile_verifier/tests/rewarder_mappers.rs @@ -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(()) } diff --git a/mobile_verifier/tests/rewarder_oracles.rs b/mobile_verifier/tests/rewarder_oracles.rs index 90c493b6b..a9407c0f6 100644 --- a/mobile_verifier/tests/rewarder_oracles.rs +++ b/mobile_verifier/tests/rewarder_oracles.rs @@ -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(()) } diff --git a/mobile_verifier/tests/rewarder_poc_dc.rs b/mobile_verifier/tests/rewarder_poc_dc.rs index 36b508cba..39a0ab9a9 100644 --- a/mobile_verifier/tests/rewarder_poc_dc.rs +++ b/mobile_verifier/tests/rewarder_poc_dc.rs @@ -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(()) } diff --git a/mobile_verifier/tests/rewarder_sp_rewards.rs b/mobile_verifier/tests/rewarder_sp_rewards.rs index d3657d632..65d242a47 100644 --- a/mobile_verifier/tests/rewarder_sp_rewards.rs +++ b/mobile_verifier/tests/rewarder_sp_rewards.rs @@ -4,7 +4,7 @@ use std::string::ToString; use async_trait::async_trait; use chrono::{DateTime, Duration as ChronoDuration, Utc}; use helium_proto::services::poc_mobile::{ - ServiceProvider, ServiceProviderReward, UnallocatedReward, UnallocatedRewardType, + ServiceProviderReward, UnallocatedReward, UnallocatedRewardType, }; use rust_decimal::prelude::*; use rust_decimal_macros::dec; @@ -23,7 +23,7 @@ const HOTSPOT_1: &str = "112NqN2WWMwtK29PMzRby62fDydBJfsCLkCAf392stdok48ovNT6"; const HOTSPOT_2: &str = "11eX55faMbqZB7jzN4p67m6w7ScPMH6ubnvCjCPLh72J49PaJEL"; const PAYER_1: &str = "11uJHS2YaEWJqgqC7yza9uvSmpv5FWoMQXiP8WbxBGgNUmifUJf"; const PAYER_2: &str = "11sctWiP9r5wDJVuDe1Th4XSL2vaawaLLSQF8f8iokAoMAJHxqp"; -const SP_1: &str = "helium_mobile"; +const SP_1: &str = "Helium Mobile"; impl MockCarrierServiceClient { fn new(valid_sps: ValidSpMap) -> Self { @@ -38,6 +38,7 @@ impl CarrierServiceVerifier for MockCarrierServiceClient { async fn key_to_rewardable_entity<'a>(&self, pubkey: &'a str) -> Result { match self.valid_sps.get(pubkey) { Some(v) => Ok(v.clone()), + None => Err(ClientError::UnknownServiceProvider), } } @@ -56,44 +57,43 @@ async fn test_service_provider_rewards(pool: PgPool) -> anyhow::Result<()> { // seed db with test specific data let mut txn = pool.clone().begin().await?; seed_hotspot_data(epoch.end, &mut txn).await?; - txn.commit().await.expect("db txn failed"); + txn.commit().await?; - tokio::select! { - // run rewards for service providers - _ = rewarder::reward_service_providers( + let (_, rewards) = tokio::join!( + rewarder::reward_service_providers( &pool, &carrier_client, &mobile_rewards_client, &epoch, dec!(0.0001), - ) => {}, - Ok((sp_reward, unallocated_reward)) = receive_expected_rewards(&mut mobile_rewards) => - { - assert_eq!(SP_1.to_string(), ServiceProvider::from_i32(sp_reward.service_provider_id).unwrap().as_str_name()); - assert_eq!(6000, sp_reward.amount); - - assert_eq!( - UnallocatedRewardType::ServiceProvider as i32, - unallocated_reward.reward_type - ); - assert_eq!(8_196_721_305_475, unallocated_reward.amount); - // confirm the total rewards allocated matches expectations - let expected_sum = - reward_shares::get_scheduled_tokens_for_service_providers(epoch.end - epoch.start) - .to_u64() - .unwrap(); - assert_eq!( - expected_sum, - sp_reward.amount + 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.1)); - - } + ), + receive_expected_rewards(&mut mobile_rewards) + ); + if let Ok((sp_reward, unallocated_reward)) = rewards { + // assert_eq!(SP_1.to_string(), ServiceProvider::from_i32(sp_reward.service_provider_id).unwrap().as_str_name()); + assert_eq!(6000, sp_reward.amount); + + assert_eq!( + UnallocatedRewardType::ServiceProvider as i32, + unallocated_reward.reward_type + ); + assert_eq!(8_196_721_305_475, unallocated_reward.amount); + // confirm the total rewards allocated matches expectations + let expected_sum = + reward_shares::get_scheduled_tokens_for_service_providers(epoch.end - epoch.start) + .to_u64() + .unwrap(); + assert_eq!(expected_sum, sp_reward.amount + 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.1)); + } else { + panic!("no rewards received"); } + Ok(()) }