Skip to content

Commit

Permalink
rebase, and update test for sp enum usage
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Dec 14, 2023
1 parent fcf1d2a commit f351d40
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 67 deletions.
4 changes: 2 additions & 2 deletions file_store/src/file_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ impl FileSinkBuilder {

#[derive(Debug, Clone)]
pub struct FileSinkClient {
sender: MessageSender,
metric: &'static str,
pub sender: MessageSender,
pub metric: &'static str,
}

const OK_LABEL: Label = Label::from_static_parts("status", "ok");
Expand Down
111 changes: 46 additions & 65 deletions mobile_verifier/tests/rewarder_sp_rewards.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
use std::collections::HashMap;
use std::string::ToString;

use async_trait::async_trait;
mod common;
use crate::common::MockFileSinkReceiver;
use chrono::{DateTime, Duration as ChronoDuration, Utc};
use common::MockCarrierServiceClient;
use common::ValidSpMap;
use helium_proto::services::poc_mobile::{
ServiceProviderReward, UnallocatedReward, UnallocatedRewardType,
ServiceProvider, ServiceProviderReward, UnallocatedReward, UnallocatedRewardType,
};
use mobile_config::client::{carrier_service_client::CarrierServiceVerifier, ClientError};
use mobile_verifier::{data_session, reward_shares, rewarder};
use rust_decimal::prelude::*;
use rust_decimal_macros::dec;
use sqlx::{PgPool, Postgres, Transaction};
use std::collections::HashMap;
use std::string::ToString;

use common::MockCarrierServiceClient;
use common::ValidSpMap;
use mobile_config::client::{carrier_service_client::CarrierServiceVerifier, ClientError};
use mobile_verifier::{data_session, reward_shares, rewarder};

use crate::common::MockFileSinkReceiver;

mod common;

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_2: &str = "sulfur_mobile";

impl MockCarrierServiceClient {
fn new(valid_sps: ValidSpMap) -> Self {
Expand All @@ -44,7 +47,6 @@ impl CarrierServiceVerifier for MockCarrierServiceClient {
async fn test_service_provider_rewards(pool: PgPool) -> anyhow::Result<()> {
let mut valid_sps = HashMap::<String, String>::new();
valid_sps.insert(PAYER_1.to_string(), SP_1.to_string());
valid_sps.insert(PAYER_2.to_string(), SP_2.to_string());
let carrier_client = MockCarrierServiceClient::new(valid_sps);
let (mobile_rewards_client, mut mobile_rewards) = common::create_file_sink();

Expand All @@ -56,37 +58,38 @@ async fn test_service_provider_rewards(pool: PgPool) -> anyhow::Result<()> {
seed_hotspot_data(epoch.end, &mut txn).await?;
txn.commit().await.expect("db txn failed");

tokio::select!(
tokio::select! {
// run rewards for service providers
_ = rewarder::reward_service_providers(
&pool,
&carrier_client,
&mobile_rewards_client,
&epoch,
dec!(0.0001),
) => {},
Ok((sp_rewards, unallocated_reward)) = receive_expected_rewards(&mut mobile_rewards) => {
assert_eq!(SP_2.to_string(), sp_rewards[0].service_provider_id);
assert_eq!(6000, sp_rewards[0].amount);
assert_eq!(SP_1.to_string(), sp_rewards[1].service_provider_id);
assert_eq!(6000, sp_rewards[1].amount);

assert_eq!(
UnallocatedRewardType::ServiceProvider as i32,
unallocated_reward.reward_type
);
assert_eq!(8_196_721_299_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_rewards[0].amount + sp_rewards[1].amount + unallocated_reward.amount
)
});
&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);
println!("point 1 {:?}", sp_reward);
// 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
);

}
}
Ok(())
}

Expand Down Expand Up @@ -127,22 +130,20 @@ async fn test_service_provider_rewards_invalid_sp(pool: PgPool) -> anyhow::Resul

async fn receive_expected_rewards(
mobile_rewards: &mut MockFileSinkReceiver,
) -> anyhow::Result<(Vec<ServiceProviderReward>, UnallocatedReward)> {
) -> anyhow::Result<(ServiceProviderReward, UnallocatedReward)> {
// get the filestore outputs from rewards run
// we will have 3 radio rewards, 1 wifi radio and 2 cbrs radios
let sp_reward1 = mobile_rewards.receive_service_provider_reward().await;
let sp_reward2 = mobile_rewards.receive_service_provider_reward().await;
// let sp_reward2 = mobile_rewards.receive_service_provider_reward().await;
// dump the sp rewards into a vec and sort to get a deteminstic order
let mut sp_rewards = vec![sp_reward1, sp_reward2];
sp_rewards.sort_by(|a, b| b.service_provider_id.cmp(&a.service_provider_id));

// expect one unallocated reward
let unallocated_reward = mobile_rewards.receive_unallocated_reward().await;

// should be no further msgs
mobile_rewards.assert_no_messages();

Ok((sp_rewards, unallocated_reward))
Ok((sp_reward1, unallocated_reward))
}

async fn seed_hotspot_data(
Expand All @@ -167,28 +168,8 @@ async fn seed_hotspot_data(
received_timestamp: ts - ChronoDuration::hours(2),
};

let data_session_3 = data_session::HotspotDataSession {
pub_key: HOTSPOT_2.parse().unwrap(),
payer: PAYER_2.parse().unwrap(),
upload_bytes: 1024 * 1000,
download_bytes: 1024 * 10000,
num_dcs: 10000,
received_timestamp: ts - ChronoDuration::hours(1),
};

let data_session_4 = data_session::HotspotDataSession {
pub_key: HOTSPOT_2.parse().unwrap(),
payer: PAYER_2.parse().unwrap(),
upload_bytes: 1024 * 1000,
download_bytes: 1024 * 50000,
num_dcs: 50000,
received_timestamp: ts - ChronoDuration::hours(2),
};

data_session_1.save(txn).await?;
data_session_2.save(txn).await?;
data_session_3.save(txn).await?;
data_session_4.save(txn).await?;
Ok(())
}

Expand Down

0 comments on commit f351d40

Please sign in to comment.