Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change mapping rewards so verification mappers can only earn rewards … #874

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 they 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