Skip to content

Commit 36fb28a

Browse files
committed
feat: calculate unnagregated fees even if the rav fails
1 parent df94692 commit 36fb28a

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

tap-agent/src/agent/sender_account.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use eventuals::{Eventual, EventualExt, PipeHandle};
2525
use indexer_common::{escrow_accounts::EscrowAccounts, prelude::SubgraphClient};
2626
use ractor::{Actor, ActorProcessingErr, ActorRef, MessagingErr, SupervisionEvent};
2727
use sqlx::PgPool;
28-
use tap_core::rav::SignedRAV;
28+
use tap_core::rav::{self, SignedRAV};
2929
use tracing::{error, warn, Level};
3030

3131
use super::sender_allocation::{SenderAllocation, SenderAllocationArgs};
@@ -87,7 +87,13 @@ type Balance = U256;
8787
pub enum ReceiptFees {
8888
NewReceipt(u128, u64),
8989
UpdateValue(UnaggregatedReceipts),
90-
RavRequestResponse(anyhow::Result<(UnaggregatedReceipts, Option<SignedRAV>)>),
90+
RavRequestResponse(
91+
(
92+
UnaggregatedReceipts,
93+
Option<SignedRAV>,
94+
Result<(), anyhow::Error>,
95+
),
96+
),
9197
Retry,
9298
}
9399

@@ -285,11 +291,15 @@ impl State {
285291
fn finalize_rav_request(
286292
&mut self,
287293
allocation_id: Address,
288-
rav_result: anyhow::Result<(UnaggregatedReceipts, Option<SignedRAV>)>,
294+
rav_result: (
295+
UnaggregatedReceipts,
296+
Option<SignedRAV>,
297+
Result<(), anyhow::Error>,
298+
),
289299
) {
290300
self.sender_fee_tracker.finish_rav_request(allocation_id);
291301
match rav_result {
292-
Ok((fees, rav)) => {
302+
(fees, rav, Ok(())) => {
293303
self.sender_fee_tracker.ok_rav_request(allocation_id);
294304
self.adaptive_limiter.on_success();
295305

@@ -299,14 +309,14 @@ impl State {
299309
// update sender fee tracker
300310
self.update_sender_fee(allocation_id, fees);
301311
}
302-
Err(err) => {
303-
// TODO we should update the total value too
312+
(fees, _, Err(err)) => {
304313
self.sender_fee_tracker.failed_rav_backoff(allocation_id);
305314
self.adaptive_limiter.on_failure();
306315
error!(
307316
"Error while requesting RAV for sender {} and allocation {}: {}",
308317
self.sender, allocation_id, err
309318
);
319+
self.update_sender_fee(allocation_id, fees);
310320
}
311321
};
312322
}
@@ -1079,8 +1089,18 @@ pub mod tests {
10791089
ReceiptFees::RavRequestResponse(l),
10801090
ReceiptFees::RavRequestResponse(r),
10811091
) => match (l, r) {
1082-
(Ok(l), Ok(r)) => l == r,
1083-
(Err(l), Err(r)) => l.to_string() == r.to_string(),
1092+
(l, r) if l.2.is_ok() && r.2.is_ok() => l.0 == r.0 && l.1 == r.1,
1093+
(l, r) if l.2.is_err() && r.2.is_err() => {
1094+
let l = match &l.2 {
1095+
Ok(()) => "Success".to_string(),
1096+
Err(e) => e.to_string(),
1097+
};
1098+
let r = match &r.2 {
1099+
Ok(()) => "Success".to_string(),
1100+
Err(e) => e.to_string(),
1101+
};
1102+
l == r
1103+
}
10841104
_ => false,
10851105
},
10861106
(ReceiptFees::Retry, ReceiptFees::Retry) => true,
@@ -1483,14 +1503,15 @@ pub mod tests {
14831503
if let Some(sender_account) = self.sender_actor.as_ref() {
14841504
sender_account.cast(SenderAccountMessage::UpdateReceiptFees(
14851505
*ALLOCATION_ID_0,
1486-
ReceiptFees::RavRequestResponse(Ok((
1506+
ReceiptFees::RavRequestResponse((
14871507
UnaggregatedReceipts {
14881508
value: *self.next_unaggregated_fees_value.lock().unwrap(),
14891509
last_id: 0,
14901510
counter: 0,
14911511
},
14921512
Some(signed_rav),
1493-
))),
1513+
Ok(()),
1514+
)),
14941515
))?;
14951516
}
14961517
}

tap-agent/src/agent/sender_allocation.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,21 @@ 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
314311
} else {
315312
Err(anyhow!("Unaggregated fee equals zero"))
316313
};
317-
314+
let ufees_lrav_rav_result = (
315+
state.unaggregated_fees,
316+
state.latest_rav.clone(),
317+
rav_result,
318+
);
319+
// encapsulate inanother okay, unwrap after and send the result over here
318320
state
319321
.sender_account_ref
320322
.cast(SenderAccountMessage::UpdateReceiptFees(
321323
state.allocation_id,
322-
ReceiptFees::RavRequestResponse(rav_result),
324+
ReceiptFees::RavRequestResponse(ufees_lrav_rav_result),
323325
))?;
324326
}
325327
#[cfg(test)]
@@ -501,7 +503,7 @@ impl SenderAllocationState {
501503
})
502504
}
503505

504-
async fn request_rav(&mut self) -> Result<()> {
506+
async fn request_rav(&mut self) -> (Result<()>) {
505507
match self.rav_requester_single().await {
506508
Ok(rav) => {
507509
self.unaggregated_fees = self.calculate_unaggregated_fee().await?;
@@ -1619,7 +1621,7 @@ pub mod tests {
16191621
_,
16201622
ReceiptFees::RavRequestResponse(rav_response),
16211623
) => {
1622-
assert!(rav_response.is_err());
1624+
assert!(rav_response.2.is_err());
16231625
}
16241626
v => panic!("Expecting RavRequestResponse as last message, found: {v:?}"),
16251627
}
@@ -1734,7 +1736,7 @@ pub mod tests {
17341736
_,
17351737
ReceiptFees::RavRequestResponse(rav_response),
17361738
) => {
1737-
assert!(rav_response.is_err());
1739+
assert!(rav_response.2.is_err());
17381740
}
17391741
v => panic!("Expecting RavRequestResponse as last message, found: {v:?}"),
17401742
}

0 commit comments

Comments
 (0)