Skip to content

Commit

Permalink
Add test for updating seniority
Browse files Browse the repository at this point in the history
  • Loading branch information
bbalser committed Jul 3, 2024
1 parent c37ff8d commit 61b4e71
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mobile_verifier/src/seniority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Seniority {
exec: &mut Transaction<'_, Postgres>,
) -> Result<Option<Self>, sqlx::Error> {
sqlx::query_as(
"SELECT uuid, seniority_ts, last_heartbeat, inserted_at, update_reason FROM seniority WHERE radio_key = $1 ORDER BY last_heartbeat DESC LIMIT 1",
"SELECT uuid, seniority_ts, last_heartbeat, inserted_at, update_reason FROM seniority WHERE radio_key = $1 ORDER BY last_heartbeat DESC, seniority_ts DESC LIMIT 1",
)
.bind(key)
.fetch_optional(&mut *exec)
Expand Down
50 changes: 50 additions & 0 deletions mobile_verifier/src/sp_boosted_rewards_bans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ mod tests {
use rand::rngs::OsRng;
use tokio::sync::mpsc;

use crate::heartbeats::KeyType;

use super::*;

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -598,6 +600,54 @@ mod tests {
Ok(())
}

#[sqlx::test]
async fn getting_banned_reset_seniority(pool: PgPool) -> anyhow::Result<()> {
let setup = TestSetup::create(pool.clone(), AllVerified);
let keypair = generate_keypair();
let pubkey = PublicKeyBinary::from(keypair.public_key().to_owned());

let last_heartbeat_ts = Utc::now() - Duration::hours(5);
let uuid = uuid::Uuid::new_v4();
let key_type = KeyType::Wifi(&pubkey);

let seniority_update = SeniorityUpdate::new(
key_type,
last_heartbeat_ts,
uuid,
SeniorityUpdateAction::Insert {
new_seniority: last_heartbeat_ts,
update_reason: SeniorityUpdateReason::NewCoverageClaimTime,
},
);

let report = wifi_ban_report(
keypair.public_key(),
Utc::now() + Duration::days(7),
SpBoostedRewardsBannedRadioReason::NoNetworkCorrelation,
);

let mut transaction = pool.begin().await?;
seniority_update.execute(&mut transaction).await?;

setup
.ingestor
.process_ingest_report(&mut transaction, report)
.await?;

let seniority = Seniority::fetch_latest(key_type, &mut transaction)
.await?
.unwrap();
transaction.commit().await?;

assert!(seniority.seniority_ts > last_heartbeat_ts);
assert_eq!(
seniority.update_reason,
SeniorityUpdateReason::ServiceProviderBan as i32
);

Ok(())
}

fn generate_keypair() -> Keypair {
Keypair::generate(KeyTag::default(), &mut OsRng)
}
Expand Down

0 comments on commit 61b4e71

Please sign in to comment.