Skip to content

Commit 676a437

Browse files
authored
feat: add tracker for buffer unaggregated fees (#324)
* feat: add tracker for buffer unaggregated fees Signed-off-by: Gustavo Inacio <[email protected]> * refactor: send just the value Signed-off-by: Gustavo Inacio <[email protected]> * test(tap-agent): buffer window fee Signed-off-by: Gustavo Inacio <[email protected]> * chore: update message for get heaviest allocation Signed-off-by: Gustavo Inacio <[email protected]> * chore: fix typo Signed-off-by: Gustavo Inacio <[email protected]> --------- Signed-off-by: Gustavo Inacio <[email protected]>
1 parent 57c89e2 commit 676a437

File tree

4 files changed

+237
-58
lines changed

4 files changed

+237
-58
lines changed

tap-agent/src/agent/sender_account.rs

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ type Balance = U256;
7979

8080
#[derive(Debug, Eq, PartialEq)]
8181
pub enum ReceiptFees {
82-
NewValue(UnaggregatedReceipts),
82+
NewReceipt(u128),
83+
UpdateValue(UnaggregatedReceipts),
8384
Retry,
8485
}
8586

@@ -198,8 +199,13 @@ impl State {
198199
async fn rav_requester_single(&mut self) -> Result<()> {
199200
let Some(allocation_id) = self.sender_fee_tracker.get_heaviest_allocation_id() else {
200201
anyhow::bail!(
201-
"Error while getting the heaviest allocation because \
202-
no unblocked allocation has enough unaggregated fees tracked"
202+
"Error while getting the heaviest allocation, \
203+
this is due one of the following reasons: \n
204+
1. allocations have too much fees under their buffer\n
205+
2. allocations are blocked to be redeemed due to ongoing last rav. \n
206+
If you keep seeing this message try to increase your `amount_willing_to_lose` \
207+
and restart your `tap-agent`\n
208+
If this doesn't work, open an issue on our Github."
203209
);
204210
};
205211
let sender_allocation_id = self.format_sender_allocation(&allocation_id);
@@ -478,7 +484,9 @@ impl Actor for SenderAccount {
478484
.set(config.tap.rav_request_trigger_value as f64);
479485

480486
let state = State {
481-
sender_fee_tracker: SenderFeeTracker::default(),
487+
sender_fee_tracker: SenderFeeTracker::new(Duration::from_millis(
488+
config.tap.rav_request_timestamp_buffer_ms,
489+
)),
482490
rav_tracker: SenderFeeTracker::default(),
483491
invalid_receipts_tracker: SenderFeeTracker::default(),
484492
allocation_ids: allocation_ids.clone(),
@@ -564,14 +572,30 @@ impl Actor for SenderAccount {
564572
scheduled_rav_request.abort();
565573
}
566574

567-
if let ReceiptFees::NewValue(unaggregated_fees) = receipt_fees {
568-
state
569-
.sender_fee_tracker
570-
.update(allocation_id, unaggregated_fees.value);
575+
match receipt_fees {
576+
ReceiptFees::NewReceipt(value) => {
577+
state.sender_fee_tracker.add(allocation_id, value);
571578

572-
UNAGGREGATED_FEES
573-
.with_label_values(&[&state.sender.to_string(), &allocation_id.to_string()])
574-
.set(unaggregated_fees.value as f64);
579+
UNAGGREGATED_FEES
580+
.with_label_values(&[
581+
&state.sender.to_string(),
582+
&allocation_id.to_string(),
583+
])
584+
.add(value as f64);
585+
}
586+
ReceiptFees::UpdateValue(unaggregated_fees) => {
587+
state
588+
.sender_fee_tracker
589+
.update(allocation_id, unaggregated_fees.value);
590+
591+
UNAGGREGATED_FEES
592+
.with_label_values(&[
593+
&state.sender.to_string(),
594+
&allocation_id.to_string(),
595+
])
596+
.set(unaggregated_fees.value as f64);
597+
}
598+
ReceiptFees::Retry => {}
575599
}
576600

577601
// Eagerly deny the sender (if needed), before the RAV request. To be sure not to
@@ -582,7 +606,7 @@ impl Actor for SenderAccount {
582606
state.add_to_denylist().await;
583607
}
584608

585-
if state.sender_fee_tracker.get_total_fee()
609+
if state.sender_fee_tracker.get_total_fee_outside_buffer()
586610
>= state.config.tap.rav_request_trigger_value
587611
{
588612
tracing::debug!(
@@ -769,7 +793,7 @@ impl Actor for SenderAccount {
769793
// update the receipt fees by reseting to 0
770794
myself.cast(SenderAccountMessage::UpdateReceiptFees(
771795
allocation_id,
772-
ReceiptFees::NewValue(UnaggregatedReceipts::default()),
796+
ReceiptFees::UpdateValue(UnaggregatedReceipts::default()),
773797
))?;
774798

775799
// rav tracker is not updated because it's still not redeemed
@@ -884,6 +908,7 @@ pub mod tests {
884908
const DUMMY_URL: &str = "http://localhost:1234";
885909
const TRIGGER_VALUE: u128 = 500;
886910
const ESCROW_VALUE: u128 = 1000;
911+
const BUFFER_MS: u64 = 100;
887912

888913
async fn create_sender_account(
889914
pgpool: PgPool,
@@ -904,7 +929,7 @@ pub mod tests {
904929
},
905930
tap: config::Tap {
906931
rav_request_trigger_value,
907-
rav_request_timestamp_buffer_ms: 1,
932+
rav_request_timestamp_buffer_ms: BUFFER_MS,
908933
rav_request_timeout_secs: 5,
909934
max_unnaggregated_fees_per_sender,
910935
..Default::default()
@@ -1191,14 +1216,11 @@ pub mod tests {
11911216
sender_account
11921217
.cast(SenderAccountMessage::UpdateReceiptFees(
11931218
*ALLOCATION_ID_0,
1194-
ReceiptFees::NewValue(UnaggregatedReceipts {
1195-
value: TRIGGER_VALUE - 1,
1196-
last_id: 10,
1197-
}),
1219+
ReceiptFees::NewReceipt(TRIGGER_VALUE - 1),
11981220
))
11991221
.unwrap();
12001222

1201-
tokio::time::sleep(Duration::from_millis(10)).await;
1223+
tokio::time::sleep(Duration::from_millis(BUFFER_MS)).await;
12021224

12031225
assert_eq!(
12041226
triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst),
@@ -1230,10 +1252,24 @@ pub mod tests {
12301252
sender_account
12311253
.cast(SenderAccountMessage::UpdateReceiptFees(
12321254
*ALLOCATION_ID_0,
1233-
ReceiptFees::NewValue(UnaggregatedReceipts {
1234-
value: TRIGGER_VALUE,
1235-
last_id: 10,
1236-
}),
1255+
ReceiptFees::NewReceipt(TRIGGER_VALUE),
1256+
))
1257+
.unwrap();
1258+
1259+
tokio::time::sleep(Duration::from_millis(20)).await;
1260+
1261+
assert_eq!(
1262+
triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst),
1263+
0
1264+
);
1265+
1266+
// wait for it to be outside buffer
1267+
tokio::time::sleep(Duration::from_millis(BUFFER_MS)).await;
1268+
1269+
sender_account
1270+
.cast(SenderAccountMessage::UpdateReceiptFees(
1271+
*ALLOCATION_ID_0,
1272+
ReceiptFees::Retry,
12371273
))
12381274
.unwrap();
12391275

@@ -1342,10 +1378,7 @@ pub mod tests {
13421378
sender_account
13431379
.cast(SenderAccountMessage::UpdateReceiptFees(
13441380
*ALLOCATION_ID_0,
1345-
ReceiptFees::NewValue(UnaggregatedReceipts {
1346-
value: TRIGGER_VALUE,
1347-
last_id: 11,
1348-
}),
1381+
ReceiptFees::NewReceipt(TRIGGER_VALUE),
13491382
))
13501383
.unwrap();
13511384
tokio::time::sleep(Duration::from_millis(200)).await;
@@ -1388,7 +1421,7 @@ pub mod tests {
13881421
sender_account
13891422
.cast(SenderAccountMessage::UpdateReceiptFees(
13901423
*ALLOCATION_ID_0,
1391-
ReceiptFees::NewValue(UnaggregatedReceipts {
1424+
ReceiptFees::UpdateValue(UnaggregatedReceipts {
13921425
value: $value,
13931426
last_id: 11,
13941427
}),
@@ -1529,7 +1562,7 @@ pub mod tests {
15291562
sender_account
15301563
.cast(SenderAccountMessage::UpdateReceiptFees(
15311564
*ALLOCATION_ID_0,
1532-
ReceiptFees::NewValue(UnaggregatedReceipts {
1565+
ReceiptFees::UpdateValue(UnaggregatedReceipts {
15331566
value: $value,
15341567
last_id: 11,
15351568
}),
@@ -1730,10 +1763,7 @@ pub mod tests {
17301763
sender_account
17311764
.cast(SenderAccountMessage::UpdateReceiptFees(
17321765
*ALLOCATION_ID_0,
1733-
ReceiptFees::NewValue(UnaggregatedReceipts {
1734-
value: TRIGGER_VALUE,
1735-
last_id: 11,
1736-
}),
1766+
ReceiptFees::NewReceipt(TRIGGER_VALUE),
17371767
))
17381768
.unwrap();
17391769
tokio::time::sleep(Duration::from_millis(100)).await;

tap-agent/src/agent/sender_accounts_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ lazy_static! {
3434
.unwrap();
3535
}
3636

37-
#[derive(Deserialize, Debug)]
37+
#[derive(Deserialize, Debug, PartialEq, Eq)]
3838
pub struct NewReceiptNotification {
3939
pub id: u64,
4040
pub allocation_id: Address,

tap-agent/src/agent/sender_allocation.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl Actor for SenderAllocation {
161161

162162
sender_account_ref.cast(SenderAccountMessage::UpdateReceiptFees(
163163
allocation_id,
164-
ReceiptFees::NewValue(state.unaggregated_fees.clone()),
164+
ReceiptFees::UpdateValue(state.unaggregated_fees.clone()),
165165
))?;
166166

167167
// update rav tracker for sender account
@@ -225,9 +225,10 @@ impl Actor for SenderAllocation {
225225
);
226226
let unaggregated_fees = &mut state.unaggregated_fees;
227227
match message {
228-
SenderAllocationMessage::NewReceipt(NewReceiptNotification {
229-
id, value: fees, ..
230-
}) => {
228+
SenderAllocationMessage::NewReceipt(notification) => {
229+
let NewReceiptNotification {
230+
id, value: fees, ..
231+
} = notification;
231232
if id > unaggregated_fees.last_id {
232233
unaggregated_fees.last_id = id;
233234
unaggregated_fees.value = unaggregated_fees
@@ -248,7 +249,7 @@ impl Actor for SenderAllocation {
248249
.sender_account_ref
249250
.cast(SenderAccountMessage::UpdateReceiptFees(
250251
state.allocation_id,
251-
ReceiptFees::NewValue(unaggregated_fees.clone()),
252+
ReceiptFees::NewReceipt(fees),
252253
))?;
253254
}
254255
}
@@ -1014,7 +1015,7 @@ pub mod tests {
10141015
// Should emit a message to the sender account with the unaggregated fees.
10151016
let expected_message = SenderAccountMessage::UpdateReceiptFees(
10161017
*ALLOCATION_ID_0,
1017-
ReceiptFees::NewValue(UnaggregatedReceipts {
1018+
ReceiptFees::UpdateValue(UnaggregatedReceipts {
10181019
last_id: 10,
10191020
value: 55u128,
10201021
}),
@@ -1113,10 +1114,7 @@ pub mod tests {
11131114
// should emit update aggregate fees message to sender account
11141115
let expected_message = SenderAccountMessage::UpdateReceiptFees(
11151116
*ALLOCATION_ID_0,
1116-
ReceiptFees::NewValue(UnaggregatedReceipts {
1117-
last_id: 1,
1118-
value: 20,
1119-
}),
1117+
ReceiptFees::NewReceipt(20u128),
11201118
);
11211119
let last_message_emitted = last_message_emitted.lock().unwrap();
11221120
assert_eq!(last_message_emitted.len(), 2);
@@ -1231,7 +1229,7 @@ pub mod tests {
12311229
last_message_emitted.lock().unwrap().last(),
12321230
Some(&SenderAccountMessage::UpdateReceiptFees(
12331231
*ALLOCATION_ID_0,
1234-
ReceiptFees::NewValue(UnaggregatedReceipts::default())
1232+
ReceiptFees::UpdateValue(UnaggregatedReceipts::default())
12351233
))
12361234
);
12371235
}

0 commit comments

Comments
 (0)