Skip to content

Commit 863894f

Browse files
committed
refactor: drop eventuals in favor of tokio watch + timers for allocation id
1 parent 99cf66c commit 863894f

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

tap-agent/src/agent/sender_allocation.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,16 @@ impl SenderAllocationState {
350350
}: SenderAllocationArgs,
351351
) -> anyhow::Result<Self> {
352352
let required_checks: Vec<Arc<dyn Check + Send + Sync>> = vec![
353-
Arc::new(AllocationId::new(
354-
config.indexer_address,
355-
config.escrow_polling_interval,
356-
sender,
357-
allocation_id,
358-
escrow_subgraph,
359-
)),
353+
Arc::new(
354+
AllocationId::new(
355+
config.indexer_address,
356+
config.escrow_polling_interval,
357+
sender,
358+
allocation_id,
359+
escrow_subgraph,
360+
)
361+
.await,
362+
),
360363
Arc::new(Signature::new(
361364
domain_separator.clone(),
362365
escrow_accounts.clone(),

tap-agent/src/tap/context/checks/allocation_id.rs

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@ use std::time::Duration;
55

66
use alloy::primitives::Address;
77
use anyhow::anyhow;
8-
use eventuals::{Eventual, EventualExt};
98
use graphql_client::GraphQLQuery;
10-
use indexer_common::subgraph_client::SubgraphClient;
9+
use indexer_common::{subgraph_client::SubgraphClient, watcher::new_watcher};
1110
use tap_core::receipt::{
1211
checks::{Check, CheckError, CheckResult},
1312
state::Checking,
1413
ReceiptWithState,
1514
};
16-
use tokio::time::sleep;
17-
use tracing::error;
15+
use tokio::sync::watch::Receiver;
1816

1917
pub struct AllocationId {
20-
tap_allocation_redeemed: Eventual<bool>,
18+
tap_allocation_redeemed: Receiver<bool>,
2119
allocation_id: Address,
2220
}
2321

2422
impl AllocationId {
25-
pub fn new(
23+
pub async fn new(
2624
indexer_address: Address,
2725
escrow_polling_interval: Duration,
2826
sender_id: Address,
2927
allocation_id: Address,
3028
escrow_subgraph: &'static SubgraphClient,
3129
) -> Self {
32-
let tap_allocation_redeemed = tap_allocation_redeemed_eventual(
30+
let tap_allocation_redeemed = tap_allocation_redeemed_watcher(
3331
allocation_id,
3432
sender_id,
3533
indexer_address,
3634
escrow_subgraph,
3735
escrow_polling_interval,
38-
);
36+
)
37+
.await
38+
.expect("Failed to initialize tap_allocation_redeemed_watcher");
3939

4040
Self {
4141
tap_allocation_redeemed,
@@ -60,46 +60,33 @@ impl Check for AllocationId {
6060
};
6161

6262
// Check that the allocation ID is not redeemed yet for this consumer
63-
match self.tap_allocation_redeemed.value().await {
64-
Ok(false) => Ok(()),
65-
Ok(true) => Err(CheckError::Failed(anyhow!(
63+
match *self.tap_allocation_redeemed.borrow() {
64+
false => Ok(()),
65+
true => Err(CheckError::Failed(anyhow!(
6666
"Allocation {} already redeemed",
6767
allocation_id
6868
))),
69-
Err(e) => Err(CheckError::Retryable(anyhow!(
70-
"Could not get allocation escrow redemption status from eventual: {:?}",
71-
e
72-
))),
7369
}
7470
}
7571
}
7672

77-
fn tap_allocation_redeemed_eventual(
73+
async fn tap_allocation_redeemed_watcher(
7874
allocation_id: Address,
7975
sender_address: Address,
8076
indexer_address: Address,
8177
escrow_subgraph: &'static SubgraphClient,
8278
escrow_polling_interval: Duration,
83-
) -> Eventual<bool> {
84-
eventuals::timer(escrow_polling_interval).map_with_retry(
85-
move |_| async move {
86-
query_escrow_check_transactions(
87-
allocation_id,
88-
sender_address,
89-
indexer_address,
90-
escrow_subgraph,
91-
)
92-
.await
93-
.map_err(|e| e.to_string())
94-
},
95-
move |error: String| {
96-
error!(
97-
"Failed to check the escrow redeem status for allocation {} and sender {}: {}",
98-
allocation_id, sender_address, error
99-
);
100-
sleep(escrow_polling_interval.div_f32(2.))
101-
},
102-
)
79+
) -> anyhow::Result<Receiver<bool>> {
80+
new_watcher(escrow_polling_interval, move || async move {
81+
query_escrow_check_transactions(
82+
allocation_id,
83+
sender_address,
84+
indexer_address,
85+
escrow_subgraph,
86+
)
87+
.await
88+
})
89+
.await
10390
}
10491

10592
#[derive(GraphQLQuery)]

0 commit comments

Comments
 (0)