Skip to content

Commit 4d6ee1c

Browse files
authored
refactor: use tokio::watch for allocation id check (#443)
1 parent 99cf66c commit 4d6ee1c

File tree

3 files changed

+136
-71
lines changed

3 files changed

+136
-71
lines changed

tap-agent/src/agent/sender_account.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ pub mod tests {
10591059
use std::time::{Duration, SystemTime, UNIX_EPOCH};
10601060
use tokio::sync::watch;
10611061
use wiremock::matchers::{body_string_contains, method};
1062-
use wiremock::{Mock, MockServer, ResponseTemplate};
1062+
use wiremock::{Mock, MockGuard, MockServer, ResponseTemplate};
10631063

10641064
// we implement the PartialEq and Eq traits for SenderAccountMessage to be able to compare
10651065
impl Eq for SenderAccountMessage {}
@@ -1110,6 +1110,23 @@ pub mod tests {
11101110
const BUFFER_MS: u64 = 100;
11111111
const RECEIPT_LIMIT: u64 = 10000;
11121112

1113+
async fn mock_escrow_subgraph() -> (MockServer, MockGuard) {
1114+
let mock_ecrow_subgraph_server: MockServer = MockServer::start().await;
1115+
let _mock_ecrow_subgraph = mock_ecrow_subgraph_server
1116+
.register_as_scoped(
1117+
Mock::given(method("POST"))
1118+
.and(body_string_contains("TapTransactions"))
1119+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({ "data": {
1120+
"transactions": [{
1121+
"id": "0x00224ee6ad4ae77b817b4e509dc29d644da9004ad0c44005a7f34481d421256409000000"
1122+
}],
1123+
}
1124+
}))),
1125+
)
1126+
.await;
1127+
(mock_ecrow_subgraph_server, _mock_ecrow_subgraph)
1128+
}
1129+
11131130
async fn create_sender_account(
11141131
pgpool: PgPool,
11151132
initial_allocation: HashSet<Address>,
@@ -1201,12 +1218,14 @@ pub mod tests {
12011218
)
12021219
.await;
12031220

1221+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1222+
12041223
let (sender_account, handle, prefix, _) = create_sender_account(
12051224
pgpool,
12061225
HashSet::new(),
12071226
TRIGGER_VALUE,
12081227
TRIGGER_VALUE,
1209-
DUMMY_URL,
1228+
&mock_escrow_subgraph_server.uri(),
12101229
&mock_server.uri(),
12111230
RECEIPT_LIMIT,
12121231
)
@@ -1295,12 +1314,14 @@ pub mod tests {
12951314
)
12961315
.await;
12971316

1317+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1318+
12981319
let (sender_account, handle, prefix, _) = create_sender_account(
12991320
pgpool,
13001321
HashSet::new(),
13011322
TRIGGER_VALUE,
13021323
TRIGGER_VALUE,
1303-
DUMMY_URL,
1324+
&mock_escrow_subgraph_server.uri(),
13041325
&mock_server.uri(),
13051326
RECEIPT_LIMIT,
13061327
)
@@ -1711,12 +1732,13 @@ pub mod tests {
17111732

17121733
#[sqlx::test(migrations = "../migrations")]
17131734
async fn test_remove_sender_account(pgpool: PgPool) {
1735+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
17141736
let (sender_account, handle, prefix, _) = create_sender_account(
17151737
pgpool,
17161738
vec![*ALLOCATION_ID_0].into_iter().collect(),
17171739
TRIGGER_VALUE,
17181740
TRIGGER_VALUE,
1719-
DUMMY_URL,
1741+
&mock_escrow_subgraph_server.uri(),
17201742
DUMMY_URL,
17211743
RECEIPT_LIMIT,
17221744
)

tap-agent/src/agent/sender_allocation.rs

Lines changed: 87 additions & 31 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(),
@@ -904,7 +907,7 @@ pub mod tests {
904907
use tokio::sync::mpsc;
905908
use wiremock::{
906909
matchers::{body_string_contains, method},
907-
Mock, MockServer, Respond, ResponseTemplate,
910+
Mock, MockGuard, MockServer, Respond, ResponseTemplate,
908911
};
909912

910913
const DUMMY_URL: &str = "http://localhost:1234";
@@ -913,6 +916,23 @@ pub mod tests {
913916
pub last_message_emitted: tokio::sync::mpsc::Sender<SenderAccountMessage>,
914917
}
915918

919+
async fn mock_escrow_subgraph() -> (MockServer, MockGuard) {
920+
let mock_ecrow_subgraph_server: MockServer = MockServer::start().await;
921+
let _mock_ecrow_subgraph = mock_ecrow_subgraph_server
922+
.register_as_scoped(
923+
Mock::given(method("POST"))
924+
.and(body_string_contains("TapTransactions"))
925+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({ "data": {
926+
"transactions": [{
927+
"id": "0x00224ee6ad4ae77b817b4e509dc29d644da9004ad0c44005a7f34481d421256409000000"
928+
}],
929+
}
930+
}))),
931+
)
932+
.await;
933+
(mock_ecrow_subgraph_server, _mock_ecrow_subgraph)
934+
}
935+
916936
#[async_trait::async_trait]
917937
impl Actor for MockSenderAccount {
918938
type Msg = SenderAccountMessage;
@@ -1026,6 +1046,7 @@ pub mod tests {
10261046

10271047
#[sqlx::test(migrations = "../migrations")]
10281048
async fn should_update_unaggregated_fees_on_start(pgpool: PgPool) {
1049+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
10291050
let (mut last_message_emitted, sender_account, _join_handle) =
10301051
create_mock_sender_account().await;
10311052
// Add receipts to the database.
@@ -1039,7 +1060,7 @@ pub mod tests {
10391060
let sender_allocation = create_sender_allocation(
10401061
pgpool.clone(),
10411062
DUMMY_URL.to_string(),
1042-
DUMMY_URL,
1063+
&mock_escrow_subgraph_server.uri(),
10431064
Some(sender_account),
10441065
)
10451066
.await;
@@ -1069,6 +1090,7 @@ pub mod tests {
10691090

10701091
#[sqlx::test(migrations = "../migrations")]
10711092
async fn should_return_invalid_receipts_on_startup(pgpool: PgPool) {
1093+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
10721094
let (mut message_receiver, sender_account, _join_handle) =
10731095
create_mock_sender_account().await;
10741096
// Add receipts to the database.
@@ -1082,7 +1104,7 @@ pub mod tests {
10821104
let sender_allocation = create_sender_allocation(
10831105
pgpool.clone(),
10841106
DUMMY_URL.to_string(),
1085-
DUMMY_URL,
1107+
&mock_escrow_subgraph_server.uri(),
10861108
Some(sender_account),
10871109
)
10881110
.await;
@@ -1120,13 +1142,14 @@ pub mod tests {
11201142

11211143
#[sqlx::test(migrations = "../migrations")]
11221144
async fn test_receive_new_receipt(pgpool: PgPool) {
1145+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
11231146
let (mut message_receiver, sender_account, _join_handle) =
11241147
create_mock_sender_account().await;
11251148

11261149
let sender_allocation = create_sender_allocation(
11271150
pgpool.clone(),
11281151
DUMMY_URL.to_string(),
1129-
DUMMY_URL,
1152+
&mock_escrow_subgraph_server.uri(),
11301153
Some(sender_account),
11311154
)
11321155
.await;
@@ -1291,14 +1314,15 @@ pub mod tests {
12911314

12921315
#[sqlx::test(migrations = "../migrations")]
12931316
async fn test_close_allocation_no_pending_fees(pgpool: PgPool) {
1317+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
12941318
let (mut message_receiver, sender_account, _join_handle) =
12951319
create_mock_sender_account().await;
12961320

12971321
// create allocation
12981322
let sender_allocation = create_sender_allocation(
12991323
pgpool.clone(),
13001324
DUMMY_URL.to_string(),
1301-
DUMMY_URL,
1325+
&mock_escrow_subgraph_server.uri(),
13021326
Some(sender_account),
13031327
)
13041328
.await;
@@ -1412,9 +1436,14 @@ pub mod tests {
14121436

14131437
#[sqlx::test(migrations = "../migrations")]
14141438
async fn should_return_unaggregated_fees_without_rav(pgpool: PgPool) {
1415-
let args =
1416-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1417-
.await;
1439+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1440+
let args = create_sender_allocation_args(
1441+
pgpool.clone(),
1442+
DUMMY_URL.to_string(),
1443+
&mock_escrow_subgraph_server.uri(),
1444+
None,
1445+
)
1446+
.await;
14181447
let state = SenderAllocationState::new(args).await.unwrap();
14191448

14201449
// Add receipts to the database.
@@ -1434,9 +1463,14 @@ pub mod tests {
14341463

14351464
#[sqlx::test(migrations = "../migrations")]
14361465
async fn should_calculate_invalid_receipts_fee(pgpool: PgPool) {
1437-
let args =
1438-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1439-
.await;
1466+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1467+
let args = create_sender_allocation_args(
1468+
pgpool.clone(),
1469+
DUMMY_URL.to_string(),
1470+
&mock_escrow_subgraph_server.uri(),
1471+
None,
1472+
)
1473+
.await;
14401474
let state = SenderAllocationState::new(args).await.unwrap();
14411475

14421476
// Add receipts to the database.
@@ -1462,9 +1496,14 @@ pub mod tests {
14621496
/// than the RAV's timestamp.
14631497
#[sqlx::test(migrations = "../migrations")]
14641498
async fn should_return_unaggregated_fees_with_rav(pgpool: PgPool) {
1465-
let args =
1466-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1467-
.await;
1499+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1500+
let args = create_sender_allocation_args(
1501+
pgpool.clone(),
1502+
DUMMY_URL.to_string(),
1503+
&mock_escrow_subgraph_server.uri(),
1504+
None,
1505+
)
1506+
.await;
14681507
let state = SenderAllocationState::new(args).await.unwrap();
14691508

14701509
// Add the RAV to the database.
@@ -1489,9 +1528,14 @@ pub mod tests {
14891528

14901529
#[sqlx::test(migrations = "../migrations")]
14911530
async fn test_store_failed_rav(pgpool: PgPool) {
1492-
let args =
1493-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1494-
.await;
1531+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1532+
let args = create_sender_allocation_args(
1533+
pgpool.clone(),
1534+
DUMMY_URL.to_string(),
1535+
&mock_escrow_subgraph_server.uri(),
1536+
None,
1537+
)
1538+
.await;
14951539
let state = SenderAllocationState::new(args).await.unwrap();
14961540

14971541
let signed_rav = create_rav(*ALLOCATION_ID_0, SIGNER.0.clone(), 4, 10);
@@ -1519,9 +1563,14 @@ pub mod tests {
15191563
}
15201564
}
15211565

1522-
let args =
1523-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1524-
.await;
1566+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1567+
let args = create_sender_allocation_args(
1568+
pgpool.clone(),
1569+
DUMMY_URL.to_string(),
1570+
&mock_escrow_subgraph_server.uri(),
1571+
None,
1572+
)
1573+
.await;
15251574
let mut state = SenderAllocationState::new(args).await.unwrap();
15261575

15271576
let checks = CheckList::new(vec![Arc::new(FailingCheck)]);
@@ -1553,12 +1602,17 @@ pub mod tests {
15531602

15541603
#[sqlx::test(migrations = "../migrations")]
15551604
async fn test_mark_rav_last(pgpool: PgPool) {
1605+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
15561606
let signed_rav = create_rav(*ALLOCATION_ID_0, SIGNER.0.clone(), 4, 10);
15571607
store_rav(&pgpool, signed_rav, SENDER.1).await.unwrap();
15581608

1559-
let args =
1560-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1561-
.await;
1609+
let args = create_sender_allocation_args(
1610+
pgpool.clone(),
1611+
DUMMY_URL.to_string(),
1612+
&mock_escrow_subgraph_server.uri(),
1613+
None,
1614+
)
1615+
.await;
15621616
let state = SenderAllocationState::new(args).await.unwrap();
15631617

15641618
// mark rav as final
@@ -1570,6 +1624,8 @@ pub mod tests {
15701624

15711625
#[sqlx::test(migrations = "../migrations")]
15721626
async fn test_failed_rav_request(pgpool: PgPool) {
1627+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1628+
15731629
// Add receipts to the database.
15741630
for i in 0..10 {
15751631
let receipt =
@@ -1586,7 +1642,7 @@ pub mod tests {
15861642
let sender_allocation = create_sender_allocation(
15871643
pgpool.clone(),
15881644
DUMMY_URL.to_string(),
1589-
DUMMY_URL,
1645+
&mock_escrow_subgraph_server.uri(),
15901646
Some(sender_account),
15911647
)
15921648
.await;

0 commit comments

Comments
 (0)