Skip to content

Commit 585b342

Browse files
committed
feat: add escape hatch to trusted senders
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent 32f30db commit 585b342

File tree

5 files changed

+119
-7
lines changed

5 files changed

+119
-7
lines changed

crates/config/maximal-config-example.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ max_receipt_value_grt = "0.001" # 0.001 GRT. We use strings to prevent rounding
124124
# max_amount_willing_to_lose_grt = "0.1"
125125
max_amount_willing_to_lose_grt = 20
126126

127+
# List of Senders that are allowed to spend up to `max_amount_willing_to_lose_grt`
128+
# over the escrow balance
129+
trusted_senders = ["0xdeadbeefcafebabedeadbeefcafebabedeadbeef"]
130+
131+
127132
# Receipts query timeout
128133
sender_timeout_secs = 30
129134

crates/config/src/config.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use std::{
5-
collections::HashMap,
5+
collections::{HashMap, HashSet},
66
env,
77
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
88
path::PathBuf,
@@ -382,6 +382,10 @@ pub struct TapConfig {
382382
pub sender_timeout_secs: Duration,
383383

384384
pub sender_aggregator_endpoints: HashMap<Address, Url>,
385+
386+
/// Senders that are allowed to spend up to `max_amount_willing_to_lose_grt`
387+
/// over the escrow balance
388+
pub trusted_senders: HashSet<Address>,
385389
}
386390

387391
#[derive(Debug, Deserialize)]

crates/tap-agent/src/agent/sender_account.rs

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

348355
impl 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

crates/tap-agent/src/test.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ lazy_static! {
6464
pub const TRIGGER_VALUE: u128 = 500;
6565
pub const RECEIPT_LIMIT: u64 = 10000;
6666
pub const DUMMY_URL: &str = "http://localhost:1234";
67-
const ESCROW_VALUE: u128 = 1000;
67+
pub const ESCROW_VALUE: u128 = 1000;
6868
const BUFFER_DURATION: Duration = Duration::from_millis(100);
6969
const RETRY_DURATION: Duration = Duration::from_millis(1000);
7070
const RAV_REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
@@ -93,6 +93,7 @@ pub fn get_sender_account_config() -> &'static SenderAccountConfig {
9393
indexer_address: INDEXER.1,
9494
escrow_polling_interval: ESCROW_POLLING_INTERVAL,
9595
tap_sender_timeout: Duration::from_secs(63),
96+
trusted_senders: HashSet::new(),
9697
}))
9798
}
9899

@@ -107,12 +108,18 @@ pub async fn create_sender_account(
107108
network_subgraph_endpoint: Option<&str>,
108109
#[builder(default = RECEIPT_LIMIT)] rav_request_receipt_limit: u64,
109110
aggregator_endpoint: Option<Url>,
111+
#[builder(default = false)] trusted_sender: bool,
110112
) -> (
111113
ActorRef<SenderAccountMessage>,
112114
Arc<Notify>,
113115
String,
114116
Sender<EscrowAccounts>,
115117
) {
118+
let trusted_senders = if trusted_sender {
119+
HashSet::from([SENDER.1])
120+
} else {
121+
HashSet::new()
122+
};
116123
let config = Box::leak(Box::new(SenderAccountConfig {
117124
rav_request_buffer: BUFFER_DURATION,
118125
max_amount_willing_to_lose_grt,
@@ -122,6 +129,7 @@ pub async fn create_sender_account(
122129
indexer_address: INDEXER.1,
123130
escrow_polling_interval: Duration::default(),
124131
tap_sender_timeout: TAP_SENDER_TIMEOUT,
132+
trusted_senders,
125133
}));
126134

127135
let network_subgraph = Box::leak(Box::new(

crates/tap-agent/tests/tap_agent_test.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::{collections::HashMap, str::FromStr, sync::Arc, time::Duration};
4+
use std::{
5+
collections::{HashMap, HashSet},
6+
str::FromStr,
7+
sync::Arc,
8+
time::Duration,
9+
};
510

611
use indexer_monitor::{DeploymentDetails, EscrowAccounts, SubgraphClient};
712
use indexer_tap_agent::{
@@ -87,6 +92,7 @@ pub async fn start_agent(
8792
indexer_address: INDEXER_ADDRESS,
8893
escrow_polling_interval: Duration::from_secs(10),
8994
tap_sender_timeout: Duration::from_secs(30),
95+
trusted_senders: HashSet::new(),
9096
}));
9197

9298
let args = SenderAccountsManagerArgs {

0 commit comments

Comments
 (0)