Skip to content

Commit 74a6f89

Browse files
carlosvdrgusinacio
andcommitted
feat: trigger rav request either through fees or counter
Co-authored-by: Gustavo Inacio <[email protected]>
1 parent 5df1b7a commit 74a6f89

File tree

1 file changed

+60
-28
lines changed

1 file changed

+60
-28
lines changed

tap-agent/src/agent/sender_account.rs

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
use alloy::hex::ToHexExt;
55
use alloy::primitives::U256;
6+
67
use bigdecimal::num_bigint::ToBigInt;
78
use bigdecimal::ToPrimitive;
9+
810
use graphql_client::GraphQLQuery;
911
use jsonrpsee::http_client::HttpClientBuilder;
1012
use prometheus::{register_gauge_vec, register_int_gauge_vec, GaugeVec, IntGaugeVec};
@@ -197,25 +199,30 @@ impl State {
197199
sender_allocation_id
198200
}
199201

200-
async fn rav_requester_single(&mut self) -> Result<()> {
201-
let Some(allocation_id) = self.sender_fee_tracker.get_heaviest_allocation_id() else {
202-
anyhow::bail!(
203-
"Error while getting the heaviest allocation, \
204-
this is due one of the following reasons: \n
205-
1. allocations have too much fees under their buffer\n
206-
2. allocations are blocked to be redeemed due to ongoing last rav. \n
207-
If you keep seeing this message try to increase your `amount_willing_to_lose` \
208-
and restart your `tap-agent`\n
209-
If this doesn't work, open an issue on our Github."
210-
);
211-
};
202+
async fn rav_request_for_heaviest_allocation(&mut self) -> Result<()> {
203+
let allocation_id = self
204+
.sender_fee_tracker
205+
.get_heaviest_allocation_id()
206+
.ok_or_else(|| {
207+
anyhow::anyhow!(
208+
"Error while getting the heaviest allocation, \
209+
this is due one of the following reasons: \n
210+
1. allocations have too much fees under their buffer\n
211+
2. allocations are blocked to be redeemed due to ongoing last rav. \n
212+
If you keep seeing this message try to increase your `amount_willing_to_lose` \
213+
and restart your `tap-agent`\n
214+
If this doesn't work, open an issue on our Github."
215+
)
216+
})?;
217+
self.rav_request_for_allocation(allocation_id).await
218+
}
219+
220+
async fn rav_request_for_allocation(&mut self, allocation_id: Address) -> Result<()> {
212221
let sender_allocation_id = self.format_sender_allocation(&allocation_id);
213222
let allocation = ActorRef::<SenderAllocationMessage>::where_is(sender_allocation_id);
214223

215224
let Some(allocation) = allocation else {
216-
anyhow::bail!(
217-
"Error while getting allocation actor {allocation_id} with most unaggregated fees"
218-
);
225+
anyhow::bail!("Error while getting allocation actor {allocation_id}");
219226
};
220227

221228
allocation
@@ -630,22 +637,47 @@ impl Actor for SenderAccount {
630637
if should_deny {
631638
state.add_to_denylist().await;
632639
}
640+
let total_counter_for_allocation = state
641+
.sender_fee_tracker
642+
.get_total_counter_outside_buffer_for_allocation(&allocation_id)
643+
as u64;
644+
let counter_greater_receipt_limit =
645+
total_counter_for_allocation >= state.config.tap.rav_request_receipt_limit;
646+
let total_fee_outside_buffer =
647+
state.sender_fee_tracker.get_total_fee_outside_buffer();
648+
let total_fee_greater_trigger_value =
649+
total_fee_outside_buffer >= state.config.tap.rav_request_trigger_value;
650+
651+
let rav_result = match (
652+
counter_greater_receipt_limit,
653+
total_fee_greater_trigger_value,
654+
) {
655+
(true, _) => {
656+
tracing::debug!(
657+
total_counter_for_allocation,
658+
rav_request_receipt_limit = state.config.tap.rav_request_receipt_limit,
659+
%allocation_id,
660+
"Total counter greater than the receipt limit per rav. Triggering RAV request"
661+
);
633662

634-
if state.sender_fee_tracker.get_total_fee_outside_buffer()
635-
>= state.config.tap.rav_request_trigger_value
636-
{
637-
tracing::debug!(
638-
total_fee = state.sender_fee_tracker.get_total_fee(),
639-
trigger_value = state.config.tap.rav_request_trigger_value,
640-
"Total fee greater than the trigger value. Triggering RAV request"
641-
);
642-
// In case we fail, we want our actor to keep running
643-
if let Err(err) = state.rav_requester_single().await {
644-
tracing::error!(
645-
error = %err,
646-
"There was an error while requesting a RAV."
663+
state.rav_request_for_allocation(allocation_id).await
664+
}
665+
(_, true) => {
666+
tracing::debug!(
667+
total_fee_outside_buffer,
668+
trigger_value = state.config.tap.rav_request_trigger_value,
669+
"Total fee greater than the trigger value. Triggering RAV request"
647670
);
671+
state.rav_request_for_heaviest_allocation().await
648672
}
673+
_ => Ok(()),
674+
};
675+
// In case we fail, we want our actor to keep running
676+
if let Err(err) = rav_result {
677+
tracing::error!(
678+
error = %err,
679+
"There was an error while requesting a RAV."
680+
);
649681
}
650682

651683
match (state.denied, state.deny_condition_reached()) {

0 commit comments

Comments
 (0)