Skip to content

Commit

Permalink
Change mapping rewards so verification mappers can only earn rewards …
Browse files Browse the repository at this point in the history
…if they also qualified for disco mapping rewards
  • Loading branch information
bbalser committed Oct 8, 2024
1 parent 2e3a438 commit 424535f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
8 changes: 7 additions & 1 deletion mobile_verifier/src/rewarder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,14 @@ pub async fn reward_mappers(
let location_shares =
subscriber_location::aggregate_location_shares(pool, reward_period).await?;

// Verification mappers can only earn rewards if the qualified for disco mapping
// rewards during the same reward_period
let vsme_shares =
subscriber_verified_mapping_event::aggregate_verified_mapping_events(pool, reward_period)
.await?;
.await?
.into_iter()
.filter(|vsme| location_shares.contains(&vsme.subscriber_id))
.collect();

// determine mapping shares based on location shares and data transferred
let mapping_shares = MapperShares::new(location_shares, vsme_shares);
Expand Down Expand Up @@ -564,6 +569,7 @@ pub async fn reward_mappers(
reward_period,
)
.await?;

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion mobile_verifier/src/subscriber_verified_mapping_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ where
}
}

async fn save_to_db(
pub async fn save_to_db(
report: &SubscriberVerifiedMappingEventIngestReport,
exec: &mut Transaction<'_, Postgres>,
) -> Result<(), sqlx::Error> {
Expand Down
58 changes: 56 additions & 2 deletions mobile_verifier/tests/integrations/rewarder_mappers.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use crate::common::{self, MockFileSinkReceiver};
use chrono::{DateTime, Duration as ChronoDuration, Utc};
use file_store::mobile_subscriber::{SubscriberLocationIngestReport, SubscriberLocationReq};
use file_store::{
mobile_subscriber::{SubscriberLocationIngestReport, SubscriberLocationReq},
subscriber_verified_mapping_event::SubscriberVerifiedMappingEvent,
subscriber_verified_mapping_event_ingest_report::SubscriberVerifiedMappingEventIngestReport,
};
use helium_crypto::PublicKeyBinary;
use helium_proto::{
services::poc_mobile::{
MobileRewardShare, SubscriberReward, UnallocatedReward, UnallocatedRewardType,
},
Message,
};
use mobile_verifier::{reward_shares, rewarder, subscriber_location};
use mobile_verifier::{
reward_shares, rewarder, subscriber_location, subscriber_verified_mapping_event,
};
use rust_decimal::prelude::*;
use rust_decimal_macros::dec;
use sqlx::{PgPool, Postgres, Transaction};
Expand Down Expand Up @@ -96,6 +102,54 @@ async fn test_mapper_rewards(pool: PgPool) -> anyhow::Result<()> {
Ok(())
}

#[sqlx::test]
async fn test_subscriber_can_only_earn_verification_mapping_if_earned_disco_mapping(
pool: PgPool,
) -> anyhow::Result<()> {
let (mobile_rewards_client, mut mobile_rewards) = common::create_file_sink();
let now = Utc::now();
let epoch = (now - ChronoDuration::hours(24))..now;

let mut txn = pool.begin().await?;
let sub_loc_report = SubscriberLocationIngestReport {
received_timestamp: epoch.end - ChronoDuration::hours(1),
report: SubscriberLocationReq {
subscriber_id: SUBSCRIBER_1.to_string().encode_to_vec(),
timestamp: epoch.end - ChronoDuration::hours(1),
carrier_pub_key: PublicKeyBinary::from_str(HOTSPOT_1).unwrap(),
},
};
subscriber_location::save(&sub_loc_report, &mut txn).await?;

let vme_report = SubscriberVerifiedMappingEventIngestReport {
received_timestamp: epoch.end - ChronoDuration::hours(1),
report: SubscriberVerifiedMappingEvent {
subscriber_id: SUBSCRIBER_2.to_string().encode_to_vec(),
total_reward_points: 50,
timestamp: epoch.end - ChronoDuration::hours(1),
carrier_mapping_key: PublicKeyBinary::from_str(HOTSPOT_1).unwrap(),
},
};
subscriber_verified_mapping_event::save_to_db(&vme_report, &mut txn).await?;

txn.commit().await?;

let (_, rewards) = tokio::join!(
rewarder::reward_mappers(&pool, &mobile_rewards_client, &epoch),
mobile_rewards.receive_subscriber_reward()
);

mobile_rewards.assert_no_messages();

let total_pool = reward_shares::get_scheduled_tokens_for_mappers(epoch.end - epoch.start);
assert_eq!(
rewards.discovery_location_amount + rewards.verification_mapping_amount,
total_pool.to_u64().unwrap()
);

Ok(())
}

async fn receive_expected_rewards(
mobile_rewards: &mut MockFileSinkReceiver<MobileRewardShare>,
) -> anyhow::Result<(Vec<SubscriberReward>, UnallocatedReward)> {
Expand Down

0 comments on commit 424535f

Please sign in to comment.