@@ -317,6 +317,10 @@ pub struct State {
317317 // reset in case of a successful response
318318 backoff_info : BackoffInfo ,
319319
320+ /// Allows the sender to go over escrow balance
321+ /// limited to `max_amount_willing_to_lose_grt`
322+ trusted_sender : bool ,
323+
320324 // Config forwarded to [SenderAllocation]
321325 config : & ' static SenderAccountConfig ,
322326}
@@ -343,6 +347,9 @@ pub struct SenderAccountConfig {
343347 ///
344348 /// This is reached if the database is too slow
345349 pub tap_sender_timeout : Duration ,
350+ /// Senders that are allowed to spend up to `max_amount_willing_to_lose_grt`
351+ /// over the escrow balance
352+ pub trusted_senders : HashSet < Address > ,
346353}
347354
348355impl SenderAccountConfig {
@@ -357,6 +364,7 @@ impl SenderAccountConfig {
357364 trigger_value : config. tap . get_trigger_value ( ) ,
358365 rav_request_timeout : config. tap . rav_request . request_timeout_secs ,
359366 tap_sender_timeout : config. tap . sender_timeout_secs ,
367+ trusted_senders : config. tap . trusted_senders . clone ( ) ,
360368 }
361369 }
362370}
@@ -531,9 +539,16 @@ impl State {
531539 fn deny_condition_reached ( & self ) -> bool {
532540 let pending_ravs = self . rav_tracker . get_total_fee ( ) ;
533541 let unaggregated_fees = self . sender_fee_tracker . get_total_fee ( ) ;
534- let pending_fees_over_balance =
535- U256 :: from ( pending_ravs + unaggregated_fees) >= self . sender_balance ;
536542 let max_amount_willing_to_lose = self . config . max_amount_willing_to_lose_grt ;
543+
544+ // if it's a trusted sender, allow to spend up to max_amount_willing_to_lose
545+ let balance = if self . trusted_sender {
546+ self . sender_balance + U256 :: from ( max_amount_willing_to_lose)
547+ } else {
548+ self . sender_balance
549+ } ;
550+
551+ let pending_fees_over_balance = U256 :: from ( pending_ravs + unaggregated_fees) >= balance;
537552 let invalid_receipt_fees = self . invalid_receipts_tracker . get_total_fee ( ) ;
538553 let total_fee_over_max_value =
539554 unaggregated_fees + invalid_receipt_fees >= max_amount_willing_to_lose;
@@ -841,6 +856,7 @@ impl Actor for SenderAccount {
841856 aggregator_v1,
842857 aggregator_v2,
843858 backoff_info : BackoffInfo :: default ( ) ,
859+ trusted_sender : config. trusted_senders . contains ( & sender_id) ,
844860 config,
845861 } ;
846862
@@ -1294,7 +1310,7 @@ pub mod tests {
12941310 assert_not_triggered, assert_triggered,
12951311 test:: {
12961312 actors:: { create_mock_sender_allocation, MockSenderAllocation } ,
1297- create_rav, create_sender_account, store_rav_with_options, TRIGGER_VALUE ,
1313+ create_rav, create_sender_account, store_rav_with_options, ESCROW_VALUE , TRIGGER_VALUE ,
12981314 } ,
12991315 } ;
13001316
@@ -1343,7 +1359,6 @@ pub mod tests {
13431359 }
13441360
13451361 /// Prefix shared between tests so we don't have conflicts in the global registry
1346- const ESCROW_VALUE : u128 = 1000 ;
13471362 const BUFFER_DURATION : Duration = Duration :: from_millis ( 100 ) ;
13481363 const RETRY_DURATION : Duration = Duration :: from_millis ( 1000 ) ;
13491364
@@ -1986,6 +2001,80 @@ pub mod tests {
19862001 sender_account. stop_and_wait ( None , None ) . await . unwrap ( ) ;
19872002 }
19882003
2004+ #[ sqlx:: test( migrations = "../../migrations" ) ]
2005+ async fn test_trusted_sender ( pgpool : PgPool ) {
2006+ let max_amount_willing_to_lose_grt = ESCROW_VALUE / 10 ;
2007+ // initialize with no trigger value and no max receipt deny
2008+ let ( sender_account, notify, prefix, _) = create_sender_account ( )
2009+ . pgpool ( pgpool)
2010+ . trusted_sender ( true )
2011+ . rav_request_trigger_value ( u128:: MAX )
2012+ . max_amount_willing_to_lose_grt ( max_amount_willing_to_lose_grt)
2013+ . call ( )
2014+ . await ;
2015+
2016+ let ( mock_sender_allocation, _) =
2017+ MockSenderAllocation :: new_with_next_rav_value ( sender_account. clone ( ) ) ;
2018+
2019+ let name = format ! ( "{}:{}:{}" , prefix, SENDER . 1 , ALLOCATION_ID_0 ) ;
2020+ let ( allocation, _) = MockSenderAllocation :: spawn ( Some ( name) , mock_sender_allocation, ( ) )
2021+ . await
2022+ . unwrap ( ) ;
2023+
2024+ async fn get_deny_status ( sender_account : & ActorRef < SenderAccountMessage > ) -> bool {
2025+ call ! ( sender_account, SenderAccountMessage :: GetDeny ) . unwrap ( )
2026+ }
2027+
2028+ macro_rules! update_receipt_fees {
2029+ ( $value: expr) => {
2030+ sender_account
2031+ . cast( SenderAccountMessage :: UpdateReceiptFees (
2032+ ALLOCATION_ID_0 ,
2033+ ReceiptFees :: UpdateValue ( UnaggregatedReceipts {
2034+ value: $value,
2035+ last_id: 11 ,
2036+ counter: 0 ,
2037+ } ) ,
2038+ ) )
2039+ . unwrap( ) ;
2040+
2041+ flush_messages( & notify) . await ;
2042+ } ;
2043+ }
2044+
2045+ let deny = call ! ( sender_account, SenderAccountMessage :: GetDeny ) . unwrap ( ) ;
2046+ assert ! ( !deny) ;
2047+
2048+ update_receipt_fees ! ( ESCROW_VALUE - 1 ) ;
2049+ let deny = get_deny_status ( & sender_account) . await ;
2050+ assert ! ( !deny, "it shouldn't deny a sender below escrow balance" ) ;
2051+
2052+ update_receipt_fees ! ( ESCROW_VALUE ) ;
2053+ let deny = get_deny_status ( & sender_account) . await ;
2054+ assert ! (
2055+ !deny,
2056+ "it shouldn't deny a trusted sender below escrow balance + max willing to lose"
2057+ ) ;
2058+
2059+ update_receipt_fees ! ( ESCROW_VALUE + max_amount_willing_to_lose_grt - 1 ) ;
2060+ let deny = get_deny_status ( & sender_account) . await ;
2061+ assert ! (
2062+ !deny,
2063+ "it shouldn't deny a trusted sender below escrow balance + max willing to lose"
2064+ ) ;
2065+
2066+ update_receipt_fees ! ( ESCROW_VALUE + max_amount_willing_to_lose_grt) ;
2067+ let deny = get_deny_status ( & sender_account) . await ;
2068+ assert ! (
2069+ deny,
2070+ "it should deny a trusted sender over escrow balance + max willing to lose"
2071+ ) ;
2072+
2073+ allocation. stop_and_wait ( None , None ) . await . unwrap ( ) ;
2074+
2075+ sender_account. stop_and_wait ( None , None ) . await . unwrap ( ) ;
2076+ }
2077+
19892078 #[ sqlx:: test( migrations = "../../migrations" ) ]
19902079 async fn test_pending_rav_already_redeemed_and_redeem ( pgpool : PgPool ) {
19912080 // Start a mock graphql server using wiremock
0 commit comments