Skip to content

Commit 55c4ea1

Browse files
authored
fix(tap-agent): update retry logic (#281)
* fix(tap-agent): update retry logic Signed-off-by: Gustavo Inacio <[email protected]> * test(tap-agent): fix retry test Signed-off-by: Gustavo Inacio <[email protected]> --------- Signed-off-by: Gustavo Inacio <[email protected]>
1 parent feda99e commit 55c4ea1

File tree

2 files changed

+59
-40
lines changed

2 files changed

+59
-40
lines changed

tap-agent/src/agent/sender_account.rs

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ lazy_static! {
7171
type RavMap = HashMap<Address, u128>;
7272
type Balance = U256;
7373

74+
#[derive(Debug, Eq, PartialEq)]
75+
pub enum ReceiptFees {
76+
NewValue(UnaggregatedReceipts),
77+
Retry,
78+
}
79+
7480
#[derive(Debug)]
7581
pub enum SenderAccountMessage {
7682
UpdateBalanceAndLastRavs(Balance, RavMap),
7783
UpdateAllocationIds(HashSet<Address>),
7884
NewAllocationId(Address),
79-
UpdateReceiptFees(Address, UnaggregatedReceipts),
85+
UpdateReceiptFees(Address, ReceiptFees),
8086
UpdateInvalidReceiptFees(Address, UnaggregatedReceipts),
8187
UpdateRav(SignedRAV),
8288
#[cfg(test)]
@@ -551,19 +557,21 @@ impl Actor for SenderAccount {
551557
state.add_to_denylist().await;
552558
}
553559
}
554-
SenderAccountMessage::UpdateReceiptFees(allocation_id, unaggregated_fees) => {
555-
UNAGGREGATED_FEES
556-
.with_label_values(&[&state.sender.to_string(), &allocation_id.to_string()])
557-
.set(unaggregated_fees.value as f64);
558-
560+
SenderAccountMessage::UpdateReceiptFees(allocation_id, receipt_fees) => {
559561
// If we're here because of a new receipt, abort any scheduled UpdateReceiptFees
560562
if let Some(scheduled_rav_request) = state.scheduled_rav_request.take() {
561563
scheduled_rav_request.abort();
562564
}
563565

564-
state
565-
.sender_fee_tracker
566-
.update(allocation_id, unaggregated_fees.value);
566+
if let ReceiptFees::NewValue(unaggregated_fees) = receipt_fees {
567+
state
568+
.sender_fee_tracker
569+
.update(allocation_id, unaggregated_fees.value);
570+
571+
UNAGGREGATED_FEES
572+
.with_label_values(&[&state.sender.to_string(), &allocation_id.to_string()])
573+
.set(unaggregated_fees.value as f64);
574+
}
567575

568576
// Eagerly deny the sender (if needed), before the RAV request. To be sure not to
569577
// delay the denial because of the RAV request, which could take some time.
@@ -602,7 +610,7 @@ impl Actor for SenderAccount {
602610
Some(myself.send_after(state.retry_interval, move || {
603611
SenderAccountMessage::UpdateReceiptFees(
604612
allocation_id,
605-
unaggregated_fees,
613+
ReceiptFees::Retry,
606614
)
607615
}));
608616
}
@@ -760,7 +768,7 @@ impl Actor for SenderAccount {
760768
// update the receipt fees by reseting to 0
761769
myself.cast(SenderAccountMessage::UpdateReceiptFees(
762770
allocation_id,
763-
UnaggregatedReceipts::default(),
771+
ReceiptFees::NewValue(UnaggregatedReceipts::default()),
764772
))?;
765773

766774
// rav tracker is not updated because it's still not redeemed
@@ -805,6 +813,7 @@ impl Actor for SenderAccount {
805813
#[cfg(test)]
806814
pub mod tests {
807815
use super::{SenderAccount, SenderAccountArgs, SenderAccountMessage};
816+
use crate::agent::sender_account::ReceiptFees;
808817
use crate::agent::sender_accounts_manager::NewReceiptNotification;
809818
use crate::agent::sender_allocation::SenderAllocationMessage;
810819
use crate::agent::unaggregated_receipts::UnaggregatedReceipts;
@@ -1014,16 +1023,18 @@ pub mod tests {
10141023
}
10151024

10161025
impl MockSenderAllocation {
1017-
pub fn new_with_triggered_rav_request() -> (Self, Arc<AtomicU32>) {
1026+
pub fn new_with_triggered_rav_request() -> (Self, Arc<AtomicU32>, Arc<Mutex<u128>>) {
10181027
let triggered_rav_request = Arc::new(AtomicU32::new(0));
1028+
let unaggregated_fees = Arc::new(Mutex::new(0));
10191029
(
10201030
Self {
10211031
triggered_rav_request: triggered_rav_request.clone(),
10221032
receipts: Arc::new(Mutex::new(Vec::new())),
10231033
next_rav_value: Arc::new(Mutex::new(0)),
1024-
next_unaggregated_fees_value: Arc::new(Mutex::new(0)),
1034+
next_unaggregated_fees_value: unaggregated_fees.clone(),
10251035
},
10261036
triggered_rav_request,
1037+
unaggregated_fees,
10271038
)
10281039
}
10291040

@@ -1120,18 +1131,24 @@ pub mod tests {
11201131
allocation: Address,
11211132
) -> (
11221133
Arc<AtomicU32>,
1134+
Arc<Mutex<u128>>,
11231135
ActorRef<SenderAllocationMessage>,
11241136
JoinHandle<()>,
11251137
) {
1126-
let (mock_sender_allocation, triggered_rav_request) =
1138+
let (mock_sender_allocation, triggered_rav_request, next_unaggregated_fees) =
11271139
MockSenderAllocation::new_with_triggered_rav_request();
11281140

11291141
let name = format!("{}:{}:{}", prefix, sender, allocation);
11301142
let (sender_account, join_handle) =
11311143
MockSenderAllocation::spawn(Some(name), mock_sender_allocation, ())
11321144
.await
11331145
.unwrap();
1134-
(triggered_rav_request, sender_account, join_handle)
1146+
(
1147+
triggered_rav_request,
1148+
next_unaggregated_fees,
1149+
sender_account,
1150+
join_handle,
1151+
)
11351152
}
11361153

11371154
#[sqlx::test(migrations = "../migrations")]
@@ -1145,17 +1162,17 @@ pub mod tests {
11451162
)
11461163
.await;
11471164

1148-
let (triggered_rav_request, allocation, allocation_handle) =
1165+
let (triggered_rav_request, _, allocation, allocation_handle) =
11491166
create_mock_sender_allocation(prefix, SENDER.1, *ALLOCATION_ID_0).await;
11501167

11511168
// create a fake sender allocation
11521169
sender_account
11531170
.cast(SenderAccountMessage::UpdateReceiptFees(
11541171
*ALLOCATION_ID_0,
1155-
UnaggregatedReceipts {
1172+
ReceiptFees::NewValue(UnaggregatedReceipts {
11561173
value: TRIGGER_VALUE - 1,
11571174
last_id: 10,
1158-
},
1175+
}),
11591176
))
11601177
.unwrap();
11611178

@@ -1184,17 +1201,17 @@ pub mod tests {
11841201
)
11851202
.await;
11861203

1187-
let (triggered_rav_request, allocation, allocation_handle) =
1204+
let (triggered_rav_request, _, allocation, allocation_handle) =
11881205
create_mock_sender_allocation(prefix, SENDER.1, *ALLOCATION_ID_0).await;
11891206

11901207
// create a fake sender allocation
11911208
sender_account
11921209
.cast(SenderAccountMessage::UpdateReceiptFees(
11931210
*ALLOCATION_ID_0,
1194-
UnaggregatedReceipts {
1211+
ReceiptFees::NewValue(UnaggregatedReceipts {
11951212
value: TRIGGER_VALUE,
11961213
last_id: 10,
1197-
},
1214+
}),
11981215
))
11991216
.unwrap();
12001217

@@ -1292,23 +1309,24 @@ pub mod tests {
12921309
)
12931310
.await;
12941311

1295-
let (triggered_rav_request, allocation, allocation_handle) =
1312+
let (triggered_rav_request, next_value, allocation, allocation_handle) =
12961313
create_mock_sender_allocation(prefix, SENDER.1, *ALLOCATION_ID_0).await;
12971314

12981315
assert_eq!(
12991316
triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst),
13001317
0
13011318
);
1319+
*next_value.lock().unwrap() = TRIGGER_VALUE;
13021320
sender_account
13031321
.cast(SenderAccountMessage::UpdateReceiptFees(
13041322
*ALLOCATION_ID_0,
1305-
UnaggregatedReceipts {
1323+
ReceiptFees::NewValue(UnaggregatedReceipts {
13061324
value: TRIGGER_VALUE,
13071325
last_id: 11,
1308-
},
1326+
}),
13091327
))
13101328
.unwrap();
1311-
tokio::time::sleep(Duration::from_millis(100)).await;
1329+
tokio::time::sleep(Duration::from_millis(200)).await;
13121330

13131331
let retry_value = triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst);
13141332
assert!(retry_value > 1, "It didn't retry more than once");
@@ -1348,10 +1366,10 @@ pub mod tests {
13481366
sender_account
13491367
.cast(SenderAccountMessage::UpdateReceiptFees(
13501368
*ALLOCATION_ID_0,
1351-
UnaggregatedReceipts {
1369+
ReceiptFees::NewValue(UnaggregatedReceipts {
13521370
value: $value,
13531371
last_id: 11,
1354-
},
1372+
}),
13551373
))
13561374
.unwrap();
13571375

@@ -1489,10 +1507,10 @@ pub mod tests {
14891507
sender_account
14901508
.cast(SenderAccountMessage::UpdateReceiptFees(
14911509
*ALLOCATION_ID_0,
1492-
UnaggregatedReceipts {
1510+
ReceiptFees::NewValue(UnaggregatedReceipts {
14931511
value: $value,
14941512
last_id: 11,
1495-
},
1513+
}),
14961514
))
14971515
.unwrap();
14981516

@@ -1690,10 +1708,10 @@ pub mod tests {
16901708
sender_account
16911709
.cast(SenderAccountMessage::UpdateReceiptFees(
16921710
*ALLOCATION_ID_0,
1693-
UnaggregatedReceipts {
1711+
ReceiptFees::NewValue(UnaggregatedReceipts {
16941712
value: TRIGGER_VALUE,
16951713
last_id: 11,
1696-
},
1714+
}),
16971715
))
16981716
.unwrap();
16991717
tokio::time::sleep(Duration::from_millis(100)).await;

tap-agent/src/agent/sender_allocation.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use tap_core::{
3030
use thegraph::types::Address;
3131
use tracing::{error, warn};
3232

33-
use crate::lazy_static;
33+
use crate::{agent::sender_account::ReceiptFees, lazy_static};
3434

3535
use crate::agent::sender_account::SenderAccountMessage;
3636
use crate::agent::sender_accounts_manager::NewReceiptNotification;
@@ -140,7 +140,7 @@ impl Actor for SenderAllocation {
140140

141141
sender_account_ref.cast(SenderAccountMessage::UpdateReceiptFees(
142142
allocation_id,
143-
state.unaggregated_fees.clone(),
143+
ReceiptFees::NewValue(state.unaggregated_fees.clone()),
144144
))?;
145145

146146
// update rav tracker for sender account
@@ -227,7 +227,7 @@ impl Actor for SenderAllocation {
227227
.sender_account_ref
228228
.cast(SenderAccountMessage::UpdateReceiptFees(
229229
state.allocation_id,
230-
unaggregated_fees.clone(),
230+
ReceiptFees::NewValue(unaggregated_fees.clone()),
231231
))?;
232232
}
233233
}
@@ -696,7 +696,8 @@ pub mod tests {
696696
};
697697
use crate::{
698698
agent::{
699-
sender_account::SenderAccountMessage, sender_accounts_manager::NewReceiptNotification,
699+
sender_account::{ReceiptFees, SenderAccountMessage},
700+
sender_accounts_manager::NewReceiptNotification,
700701
unaggregated_receipts::UnaggregatedReceipts,
701702
},
702703
config,
@@ -889,10 +890,10 @@ pub mod tests {
889890
// Should emit a message to the sender account with the unaggregated fees.
890891
let expected_message = SenderAccountMessage::UpdateReceiptFees(
891892
*ALLOCATION_ID_0,
892-
UnaggregatedReceipts {
893+
ReceiptFees::NewValue(UnaggregatedReceipts {
893894
last_id: 10,
894895
value: 55u128,
895-
},
896+
}),
896897
);
897898
let last_message_emitted = last_message_emitted.lock().unwrap();
898899
assert_eq!(last_message_emitted.len(), 1);
@@ -988,10 +989,10 @@ pub mod tests {
988989
// should emit update aggregate fees message to sender account
989990
let expected_message = SenderAccountMessage::UpdateReceiptFees(
990991
*ALLOCATION_ID_0,
991-
UnaggregatedReceipts {
992+
ReceiptFees::NewValue(UnaggregatedReceipts {
992993
last_id: 1,
993994
value: 20,
994-
},
995+
}),
995996
);
996997
let last_message_emitted = last_message_emitted.lock().unwrap();
997998
assert_eq!(last_message_emitted.len(), 2);
@@ -1106,7 +1107,7 @@ pub mod tests {
11061107
last_message_emitted.lock().unwrap().last(),
11071108
Some(&SenderAccountMessage::UpdateReceiptFees(
11081109
*ALLOCATION_ID_0,
1109-
UnaggregatedReceipts::default()
1110+
ReceiptFees::NewValue(UnaggregatedReceipts::default())
11101111
))
11111112
);
11121113
}

0 commit comments

Comments
 (0)