Skip to content

Commit 598ab52

Browse files
committed
sentry - routes - camiang insert events - use CampaignRemaining
1 parent 05ac2b0 commit 598ab52

File tree

1 file changed

+30
-57
lines changed

1 file changed

+30
-57
lines changed

sentry/src/routes/campaign.rs

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub mod insert_events {
370370

371371
use crate::{
372372
access::{self, check_access},
373-
db::{accounting::spend_amount, DbPool, PoolError, RedisError},
373+
db::{accounting::spend_amount, CampaignRemaining, DbPool, PoolError, RedisError},
374374
payout::get_payout,
375375
spender::fee::calculate_fee,
376376
Application, Auth, ResponseError, Session,
@@ -384,12 +384,8 @@ pub mod insert_events {
384384
},
385385
Address, Campaign, CampaignId, DomainError, UnifiedNum, ValidatorDesc,
386386
};
387-
use redis::aio::MultiplexedConnection;
388387
use thiserror::Error;
389388

390-
// TODO AIP#61: Use the Campaign Modify const here
391-
pub const CAMPAIGN_REMAINING_KEY: &str = "campaignRemaining";
392-
393389
#[derive(Debug, Error)]
394390
pub enum Error {
395391
#[error(transparent)]
@@ -492,7 +488,7 @@ pub mod insert_events {
492488
match payout {
493489
Some((earner, payout)) => spend_for_event(
494490
&app.pool,
495-
app.redis.clone(),
491+
&app.campaign_remaining,
496492
campaign,
497493
earner,
498494
leader,
@@ -515,7 +511,7 @@ pub mod insert_events {
515511

516512
pub async fn spend_for_event(
517513
pool: &DbPool,
518-
mut redis: MultiplexedConnection,
514+
campaign_remaining: &CampaignRemaining,
519515
campaign: &Campaign,
520516
earner: Address,
521517
leader: &ValidatorDesc,
@@ -534,14 +530,16 @@ pub mod insert_events {
534530
.sum::<Option<UnifiedNum>>()
535531
.ok_or(EventError::EventPayoutOverflow)?;
536532

537-
if !has_enough_remaining_budget(&mut redis, campaign.id, spending).await? {
533+
if !has_enough_remaining_budget(campaign_remaining, campaign.id, spending).await? {
538534
return Err(Error::Event(
539535
EventError::CampaignRemainingNotEnoughForPayout,
540536
));
541537
}
542538

543539
// The event payout decreases the remaining budget for the Campaign
544-
let remaining = decrease_remaining_budget(&mut redis, campaign.id, spending).await?;
540+
let remaining = campaign_remaining
541+
.decrease_by(campaign.id, spending)
542+
.await?;
545543

546544
// Update the Accounting records accordingly
547545
let channel_id = campaign.channel.id();
@@ -563,40 +561,22 @@ pub mod insert_events {
563561
}
564562

565563
async fn has_enough_remaining_budget(
566-
redis: &mut MultiplexedConnection,
564+
campaign_remaining: &CampaignRemaining,
567565
campaign: CampaignId,
568566
amount: UnifiedNum,
569567
) -> Result<bool, RedisError> {
570-
let key = format!("{}:{}", CAMPAIGN_REMAINING_KEY, campaign);
571-
572-
let remaining = redis::cmd("GET")
573-
.arg(&key)
574-
.query_async::<_, Option<i64>>(redis)
568+
let remaining = campaign_remaining
569+
.get_remaining_opt(campaign)
575570
.await?
576571
.unwrap_or_default();
577572

578573
Ok(remaining > 0 && remaining.unsigned_abs() > amount.to_u64())
579574
}
580575

581-
async fn decrease_remaining_budget(
582-
redis: &mut MultiplexedConnection,
583-
campaign: CampaignId,
584-
amount: UnifiedNum,
585-
) -> Result<i64, RedisError> {
586-
let key = format!("{}:{}", CAMPAIGN_REMAINING_KEY, campaign);
587-
588-
let remaining = redis::cmd("DECRBY")
589-
.arg(&key)
590-
.arg(amount.to_u64())
591-
.query_async::<_, i64>(redis)
592-
.await?;
593-
594-
Ok(remaining)
595-
}
596-
597576
#[cfg(test)]
598577
mod test {
599578
use primitives::util::tests::prep_db::{ADDRESSES, DUMMY_CAMPAIGN};
579+
use redis::aio::MultiplexedConnection;
600580

601581
use crate::db::{
602582
redis_pool::TESTS_POOL,
@@ -605,27 +585,13 @@ pub mod insert_events {
605585

606586
use super::*;
607587

608-
/// Helper function to get the Campaign Remaining budget in Redis for the tests
609-
async fn get_campaign_remaining(
610-
redis: &mut MultiplexedConnection,
611-
campaign: CampaignId,
612-
) -> Option<i64> {
613-
let key = format!("{}:{}", CAMPAIGN_REMAINING_KEY, campaign);
614-
615-
redis::cmd("GET")
616-
.arg(&key)
617-
.query_async(redis)
618-
.await
619-
.expect("Should set Campaign remaining key")
620-
}
621-
622588
/// Helper function to set the Campaign Remaining budget in Redis for the tests
623589
async fn set_campaign_remaining(
624590
redis: &mut MultiplexedConnection,
625591
campaign: CampaignId,
626592
remaining: i64,
627593
) {
628-
let key = format!("{}:{}", CAMPAIGN_REMAINING_KEY, campaign);
594+
let key = CampaignRemaining::get_key(campaign);
629595

630596
redis::cmd("SET")
631597
.arg(&key)
@@ -638,12 +604,14 @@ pub mod insert_events {
638604
#[tokio::test]
639605
async fn test_has_enough_remaining_budget() {
640606
let mut redis = TESTS_POOL.get().await.expect("Should get redis connection");
607+
let campaign_remaining = CampaignRemaining::new(redis.connection.clone());
641608
let campaign = DUMMY_CAMPAIGN.id;
642609
let amount = UnifiedNum::from(10_000);
643610

644-
let no_remaining_budget_set = has_enough_remaining_budget(&mut redis, campaign, amount)
645-
.await
646-
.expect("Should check campaign remaining");
611+
let no_remaining_budget_set =
612+
has_enough_remaining_budget(&campaign_remaining, campaign, amount)
613+
.await
614+
.expect("Should check campaign remaining");
647615
assert!(
648616
!no_remaining_budget_set,
649617
"No remaining budget set, should return false"
@@ -652,7 +620,7 @@ pub mod insert_events {
652620
set_campaign_remaining(&mut redis, campaign, 9_000).await;
653621

654622
let not_enough_remaining_budget =
655-
has_enough_remaining_budget(&mut redis, campaign, amount)
623+
has_enough_remaining_budget(&campaign_remaining, campaign, amount)
656624
.await
657625
.expect("Should check campaign remaining");
658626
assert!(
@@ -663,7 +631,7 @@ pub mod insert_events {
663631
set_campaign_remaining(&mut redis, campaign, 11_000).await;
664632

665633
let has_enough_remaining_budget =
666-
has_enough_remaining_budget(&mut redis, campaign, amount)
634+
has_enough_remaining_budget(&campaign_remaining, campaign, amount)
667635
.await
668636
.expect("Should check campaign remaining");
669637

@@ -677,19 +645,22 @@ pub mod insert_events {
677645
async fn test_decreasing_remaining_budget() {
678646
let mut redis = TESTS_POOL.get().await.expect("Should get redis connection");
679647
let campaign = DUMMY_CAMPAIGN.id;
648+
let campaign_remaining = CampaignRemaining::new(redis.connection.clone());
680649
let amount = UnifiedNum::from(5_000);
681650

682651
set_campaign_remaining(&mut redis, campaign, 9_000).await;
683652

684-
let remaining = decrease_remaining_budget(&mut redis, campaign, amount)
653+
let remaining = campaign_remaining
654+
.decrease_by(campaign, amount)
685655
.await
686656
.expect("Should decrease campaign remaining");
687657
assert_eq!(
688658
4_000, remaining,
689659
"Should decrease remaining budget with amount and be positive"
690660
);
691661

692-
let remaining = decrease_remaining_budget(&mut redis, campaign, amount)
662+
let remaining = campaign_remaining
663+
.decrease_by(campaign, amount)
693664
.await
694665
.expect("Should decrease campaign remaining");
695666
assert_eq!(
@@ -702,6 +673,7 @@ pub mod insert_events {
702673
async fn test_spending_for_events_with_enough_remaining_budget() {
703674
let mut redis = TESTS_POOL.get().await.expect("Should get redis connection");
704675
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
676+
let campaign_remaining = CampaignRemaining::new(redis.connection.clone());
705677

706678
setup_test_migrations(database.pool.clone())
707679
.await
@@ -719,7 +691,7 @@ pub mod insert_events {
719691
{
720692
let spend_event = spend_for_event(
721693
&database.pool,
722-
redis.connection.clone(),
694+
&campaign_remaining,
723695
&campaign,
724696
publisher,
725697
leader,
@@ -745,7 +717,7 @@ pub mod insert_events {
745717

746718
let spend_event = spend_for_event(
747719
&database.pool,
748-
redis.connection.clone(),
720+
&campaign_remaining,
749721
&campaign,
750722
publisher,
751723
leader,
@@ -765,8 +737,9 @@ pub mod insert_events {
765737
// Follower fee: 100
766738
// Follower payout: 300 * 100 / 1000 = 30
767739
assert_eq!(
768-
10_640_i64,
769-
get_campaign_remaining(&mut redis.connection, campaign.id)
740+
Some(10_640_i64),
741+
campaign_remaining
742+
.get_remaining_opt(campaign.id)
770743
.await
771744
.expect("Should have key")
772745
)

0 commit comments

Comments
 (0)