Skip to content

Commit 3ae39c0

Browse files
authored
feat: calculate unnagregated fees even if the rav fails (#442)
1 parent 5fa85ac commit 3ae39c0

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

tap-agent/src/agent/sender_account.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ type Balance = U256;
9292
pub enum ReceiptFees {
9393
NewReceipt(u128, u64),
9494
UpdateValue(UnaggregatedReceipts),
95-
RavRequestResponse(anyhow::Result<(UnaggregatedReceipts, Option<SignedRAV>)>),
95+
RavRequestResponse((UnaggregatedReceipts, anyhow::Result<Option<SignedRAV>>)),
9696
Retry,
9797
}
9898

@@ -290,22 +290,18 @@ impl State {
290290
fn finalize_rav_request(
291291
&mut self,
292292
allocation_id: Address,
293-
rav_result: anyhow::Result<(UnaggregatedReceipts, Option<SignedRAV>)>,
293+
rav_response: (UnaggregatedReceipts, anyhow::Result<Option<SignedRAV>>),
294294
) {
295295
self.sender_fee_tracker.finish_rav_request(allocation_id);
296+
let (fees, rav_result) = rav_response;
296297
match rav_result {
297-
Ok((fees, rav)) => {
298+
Ok(signed_rav) => {
298299
self.sender_fee_tracker.ok_rav_request(allocation_id);
299300
self.adaptive_limiter.on_success();
300-
301-
let rav_value = rav.map_or(0, |rav| rav.message.valueAggregate);
301+
let rav_value = signed_rav.map_or(0, |rav| rav.message.valueAggregate);
302302
self.update_rav(allocation_id, rav_value);
303-
304-
// update sender fee tracker
305-
self.update_sender_fee(allocation_id, fees);
306303
}
307304
Err(err) => {
308-
// TODO we should update the total value too
309305
self.sender_fee_tracker.failed_rav_backoff(allocation_id);
310306
self.adaptive_limiter.on_failure();
311307
error!(
@@ -314,6 +310,7 @@ impl State {
314310
);
315311
}
316312
};
313+
self.update_sender_fee(allocation_id, fees);
317314
}
318315

319316
fn update_rav(&mut self, allocation_id: Address, rav_value: u128) {
@@ -1102,8 +1099,10 @@ pub mod tests {
11021099
ReceiptFees::RavRequestResponse(l),
11031100
ReceiptFees::RavRequestResponse(r),
11041101
) => match (l, r) {
1105-
(Ok(l), Ok(r)) => l == r,
1106-
(Err(l), Err(r)) => l.to_string() == r.to_string(),
1102+
((fee, Ok(rav)), (fee1, Ok(rav1))) => fee == fee1 && rav == rav1,
1103+
((fee, Err(error)), (fee1, Err(error1))) => {
1104+
fee == fee1 && error.to_string() == error1.to_string()
1105+
}
11071106
_ => false,
11081107
},
11091108
(ReceiptFees::Retry, ReceiptFees::Retry) => true,
@@ -1534,14 +1533,14 @@ pub mod tests {
15341533
if let Some(sender_account) = self.sender_actor.as_ref() {
15351534
sender_account.cast(SenderAccountMessage::UpdateReceiptFees(
15361535
*ALLOCATION_ID_0,
1537-
ReceiptFees::RavRequestResponse(Ok((
1536+
ReceiptFees::RavRequestResponse((
15381537
UnaggregatedReceipts {
15391538
value: *self.next_unaggregated_fees_value.lock().unwrap(),
15401539
last_id: 0,
15411540
counter: 0,
15421541
},
1543-
Some(signed_rav),
1544-
))),
1542+
Ok(Some(signed_rav)),
1543+
)),
15451544
))?;
15461545
}
15471546
}

tap-agent/src/agent/sender_allocation.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,17 @@ impl Actor for SenderAllocation {
307307
}
308308
SenderAllocationMessage::TriggerRAVRequest => {
309309
let rav_result = if state.unaggregated_fees.value > 0 {
310-
state
311-
.request_rav()
312-
.await
313-
.map(|_| (state.unaggregated_fees, state.latest_rav.clone()))
310+
state.request_rav().await.map(|_| state.latest_rav.clone())
314311
} else {
315312
Err(anyhow!("Unaggregated fee equals zero"))
316313
};
317-
314+
let rav_response = (state.unaggregated_fees, rav_result);
315+
// encapsulate inanother okay, unwrap after and send the result over here
318316
state
319317
.sender_account_ref
320318
.cast(SenderAccountMessage::UpdateReceiptFees(
321319
state.allocation_id,
322-
ReceiptFees::RavRequestResponse(rav_result),
320+
ReceiptFees::RavRequestResponse(rav_response),
323321
))?;
324322
}
325323
#[cfg(test)]
@@ -1678,7 +1676,7 @@ pub mod tests {
16781676
_,
16791677
ReceiptFees::RavRequestResponse(rav_response),
16801678
) => {
1681-
assert!(rav_response.is_err());
1679+
assert!(rav_response.1.is_err());
16821680
}
16831681
v => panic!("Expecting RavRequestResponse as last message, found: {v:?}"),
16841682
}
@@ -1793,7 +1791,7 @@ pub mod tests {
17931791
_,
17941792
ReceiptFees::RavRequestResponse(rav_response),
17951793
) => {
1796-
assert!(rav_response.is_err());
1794+
assert!(rav_response.1.is_err());
17971795
}
17981796
v => panic!("Expecting RavRequestResponse as last message, found: {v:?}"),
17991797
}

0 commit comments

Comments
 (0)