Skip to content

Commit 76873f9

Browse files
authored
fix: calculate unaggregate receipts up to last_id (#385)
1 parent 817703e commit 76873f9

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

.sqlx/query-7a7fb81674a67e9b9dbf82a318d60dd78565765831dc4dd84777dfb751736ce6.json renamed to .sqlx/query-e803de8899de4d54bb7c40be0459fa53d0cd2798d219947cc6fc8b5b98362794.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tap-agent/src/agent/sender_allocation.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl Actor for SenderAllocation {
154154
}
155155

156156
// update unaggregated_fees
157-
state.unaggregated_fees = state.calculate_unaggregated_fee().await?;
157+
state.unaggregated_fees = state.initialize_unaggregated_receipts().await?;
158158

159159
sender_account_ref.cast(SenderAccountMessage::UpdateReceiptFees(
160160
allocation_id,
@@ -226,9 +226,17 @@ impl Actor for SenderAllocation {
226226
let NewReceiptNotification {
227227
id, value: fees, ..
228228
} = notification;
229-
if id > unaggregated_fees.last_id {
230-
unaggregated_fees.last_id = id;
231-
unaggregated_fees.value = unaggregated_fees
229+
if id <= unaggregated_fees.last_id {
230+
// our world assumption is wrong
231+
warn!(
232+
last_id = %id,
233+
"Received a receipt notification that was already calculated."
234+
);
235+
return Ok(());
236+
}
237+
unaggregated_fees.last_id = id;
238+
unaggregated_fees.value =
239+
unaggregated_fees
232240
.value
233241
.checked_add(fees)
234242
.unwrap_or_else(|| {
@@ -241,14 +249,13 @@ impl Actor for SenderAllocation {
241249
);
242250
u128::MAX
243251
});
244-
// it's fine to crash the actor, could not send a message to its parent
245-
state
246-
.sender_account_ref
247-
.cast(SenderAccountMessage::UpdateReceiptFees(
248-
state.allocation_id,
249-
ReceiptFees::NewReceipt(fees),
250-
))?;
251-
}
252+
// it's fine to crash the actor, could not send a message to its parent
253+
state
254+
.sender_account_ref
255+
.cast(SenderAccountMessage::UpdateReceiptFees(
256+
state.allocation_id,
257+
ReceiptFees::NewReceipt(fees),
258+
))?;
252259
}
253260
SenderAllocationMessage::TriggerRAVRequest => {
254261
let rav_result = if state.unaggregated_fees.value > 0 {
@@ -336,9 +343,18 @@ impl SenderAllocationState {
336343
})
337344
}
338345

346+
async fn initialize_unaggregated_receipts(&self) -> Result<UnaggregatedReceipts> {
347+
self.calculate_fee_until_last_id(i64::MAX).await
348+
}
349+
350+
async fn calculate_unaggregated_fee(&self) -> Result<UnaggregatedReceipts> {
351+
self.calculate_fee_until_last_id(self.unaggregated_fees.last_id as i64)
352+
.await
353+
}
354+
339355
/// Delete obsolete receipts in the DB w.r.t. the last RAV in DB, then update the tap manager
340356
/// with the latest unaggregated fees from the database.
341-
async fn calculate_unaggregated_fee(&self) -> Result<UnaggregatedReceipts> {
357+
async fn calculate_fee_until_last_id(&self, last_id: i64) -> Result<UnaggregatedReceipts> {
342358
tracing::trace!("calculate_unaggregated_fee()");
343359
self.tap_manager.remove_obsolete_receipts().await?;
344360

@@ -353,10 +369,12 @@ impl SenderAllocationState {
353369
scalar_tap_receipts
354370
WHERE
355371
allocation_id = $1
356-
AND signer_address IN (SELECT unnest($2::text[]))
357-
AND timestamp_ns > $3
372+
AND id <= $2
373+
AND signer_address IN (SELECT unnest($3::text[]))
374+
AND timestamp_ns > $4
358375
"#,
359376
self.allocation_id.encode_hex(),
377+
last_id,
360378
&signers,
361379
BigDecimal::from(
362380
self.latest_rav
@@ -599,7 +617,7 @@ impl SenderAllocationState {
599617
}
600618
Ok(response.data)
601619
}
602-
(Err(_), true, true) => Err(anyhow!(
620+
(Err(tap_core::Error::NoValidReceiptsForRAVRequest), true, true) => Err(anyhow!(
603621
"It looks like there are no valid receipts for the RAV request.\
604622
This may happen if your `rav_request_trigger_value` is too low \
605623
and no receipts were found outside the `rav_request_timestamp_buffer_ms`.\
@@ -1346,7 +1364,7 @@ pub mod tests {
13461364
}
13471365

13481366
// calculate unaggregated fee
1349-
let total_unaggregated_fees = state.calculate_unaggregated_fee().await.unwrap();
1367+
let total_unaggregated_fees = state.initialize_unaggregated_receipts().await.unwrap();
13501368

13511369
// Check that the unaggregated fees are correct.
13521370
assert_eq!(total_unaggregated_fees.value, 45u128);
@@ -1401,7 +1419,7 @@ pub mod tests {
14011419
.unwrap();
14021420
}
14031421

1404-
let total_unaggregated_fees = state.calculate_unaggregated_fee().await.unwrap();
1422+
let total_unaggregated_fees = state.initialize_unaggregated_receipts().await.unwrap();
14051423

14061424
// Check that the unaggregated fees are correct.
14071425
assert_eq!(total_unaggregated_fees.value, 35u128);

0 commit comments

Comments
 (0)