Skip to content

Commit

Permalink
test: test trusted gateway config
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <[email protected]>
  • Loading branch information
gusinacio committed Feb 11, 2025
1 parent d735660 commit da7ac0f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
77 changes: 75 additions & 2 deletions crates/tap-agent/src/agent/sender_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ pub mod tests {
assert_not_triggered, assert_triggered,
test::{
actors::{create_mock_sender_allocation, MockSenderAllocation},
create_rav, create_sender_account, store_rav_with_options, TRIGGER_VALUE,
create_rav, create_sender_account, store_rav_with_options, ESCROW_VALUE, TRIGGER_VALUE,
},
};

Expand Down Expand Up @@ -1359,7 +1359,6 @@ pub mod tests {
}

/// Prefix shared between tests so we don't have conflicts in the global registry
const ESCROW_VALUE: u128 = 1000;
const BUFFER_DURATION: Duration = Duration::from_millis(100);
const RETRY_DURATION: Duration = Duration::from_millis(1000);

Expand Down Expand Up @@ -2002,6 +2001,80 @@ pub mod tests {
sender_account.stop_and_wait(None, None).await.unwrap();
}

#[sqlx::test(migrations = "../../migrations")]
async fn test_trusted_gateway(pgpool: PgPool) {
let max_amount_willing_to_lose_grt = ESCROW_VALUE / 10;
// initialize with no trigger value and no max receipt deny
let (sender_account, notify, prefix, _) = create_sender_account()
.pgpool(pgpool)
.trusted_gateway(true)
.rav_request_trigger_value(u128::MAX)
.max_amount_willing_to_lose_grt(max_amount_willing_to_lose_grt)
.call()
.await;

let (mock_sender_allocation, _) =
MockSenderAllocation::new_with_next_rav_value(sender_account.clone());

let name = format!("{}:{}:{}", prefix, SENDER.1, ALLOCATION_ID_0);
let (allocation, _) = MockSenderAllocation::spawn(Some(name), mock_sender_allocation, ())
.await
.unwrap();

async fn get_deny_status(sender_account: &ActorRef<SenderAccountMessage>) -> bool {
call!(sender_account, SenderAccountMessage::GetDeny).unwrap()
}

macro_rules! update_receipt_fees {
($value:expr) => {
sender_account
.cast(SenderAccountMessage::UpdateReceiptFees(
ALLOCATION_ID_0,
ReceiptFees::UpdateValue(UnaggregatedReceipts {
value: $value,
last_id: 11,
counter: 0,
}),
))
.unwrap();

flush_messages(&notify).await;
};
}

let deny = call!(sender_account, SenderAccountMessage::GetDeny).unwrap();
assert!(!deny);

update_receipt_fees!(ESCROW_VALUE - 1);
let deny = get_deny_status(&sender_account).await;
assert!(!deny, "it shouldn't deny a sender below escrow balance");

update_receipt_fees!(ESCROW_VALUE);
let deny = get_deny_status(&sender_account).await;
assert!(
!deny,
"it shouldn't deny a trusted sender below escrow balance + max willing to lose"
);

update_receipt_fees!(ESCROW_VALUE + max_amount_willing_to_lose_grt - 1);
let deny = get_deny_status(&sender_account).await;
assert!(
!deny,
"it shouldn't deny a trusted sender below escrow balance + max willing to lose"
);

update_receipt_fees!(ESCROW_VALUE + max_amount_willing_to_lose_grt);
let deny = get_deny_status(&sender_account).await;
assert!(
deny,
"it should deny a trusted sender over escrow balance + max willing to lose"
);

allocation.stop_and_wait(None, None).await.unwrap();

sender_account.stop_and_wait(None, None).await.unwrap();
}

#[sqlx::test(migrations = "../../migrations")]
async fn test_pending_rav_already_redeemed_and_redeem(pgpool: PgPool) {
// Start a mock graphql server using wiremock
Expand Down
10 changes: 8 additions & 2 deletions crates/tap-agent/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ lazy_static! {
pub const TRIGGER_VALUE: u128 = 500;
pub const RECEIPT_LIMIT: u64 = 10000;
pub const DUMMY_URL: &str = "http://localhost:1234";
const ESCROW_VALUE: u128 = 1000;
pub const ESCROW_VALUE: u128 = 1000;
const BUFFER_DURATION: Duration = Duration::from_millis(100);
const RETRY_DURATION: Duration = Duration::from_millis(1000);
const RAV_REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
Expand Down Expand Up @@ -108,12 +108,18 @@ pub async fn create_sender_account(
network_subgraph_endpoint: Option<&str>,
#[builder(default = RECEIPT_LIMIT)] rav_request_receipt_limit: u64,
aggregator_endpoint: Option<Url>,
#[builder(default = false)] trusted_gateway: bool,
) -> (
ActorRef<SenderAccountMessage>,
Arc<Notify>,
String,
Sender<EscrowAccounts>,
) {
let trusted_gateways = if trusted_gateway {
HashSet::from([SENDER.1])
} else {
HashSet::new()
};
let config = Box::leak(Box::new(SenderAccountConfig {
rav_request_buffer: BUFFER_DURATION,
max_amount_willing_to_lose_grt,
Expand All @@ -123,7 +129,7 @@ pub async fn create_sender_account(
indexer_address: INDEXER.1,
escrow_polling_interval: Duration::default(),
tap_sender_timeout: TAP_SENDER_TIMEOUT,
trusted_gateways: HashSet::new(),
trusted_gateways,
}));

let network_subgraph = Box::leak(Box::new(
Expand Down

0 comments on commit da7ac0f

Please sign in to comment.