Skip to content

Commit a079101

Browse files
refactor: improve readability of store_rav_with_options with bon
1 parent af7b5e2 commit a079101

File tree

3 files changed

+94
-36
lines changed

3 files changed

+94
-36
lines changed

crates/tap-agent/src/agent/sender_account.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,7 +1831,13 @@ pub mod tests {
18311831

18321832
// make sure there's a reason to keep denied
18331833
let signed_rav = create_rav(ALLOCATION_ID_0, SIGNER.0.clone(), 4, ESCROW_VALUE);
1834-
store_rav_with_options(&pgpool, signed_rav, SENDER.1, true, false)
1834+
store_rav_with_options()
1835+
.pgpool(&pgpool)
1836+
.signed_rav(signed_rav)
1837+
.sender(SENDER.1)
1838+
.last(true)
1839+
.final_rav(false)
1840+
.call()
18351841
.await
18361842
.unwrap();
18371843

@@ -1981,7 +1987,13 @@ pub mod tests {
19811987
async fn test_initialization_with_pending_ravs_over_the_limit(pgpool: PgPool) {
19821988
// add last non-final ravs
19831989
let signed_rav = create_rav(ALLOCATION_ID_0, SIGNER.0.clone(), 4, ESCROW_VALUE);
1984-
store_rav_with_options(&pgpool, signed_rav, SENDER.1, true, false)
1990+
store_rav_with_options()
1991+
.pgpool(&pgpool)
1992+
.signed_rav(signed_rav)
1993+
.sender(SENDER.1)
1994+
.last(true)
1995+
.final_rav(false)
1996+
.call()
19851997
.await
19861998
.unwrap();
19871999

@@ -1999,13 +2011,25 @@ pub mod tests {
19992011
async fn test_unaggregated_fees_over_balance(pgpool: PgPool) {
20002012
// add last non-final ravs
20012013
let signed_rav = create_rav(ALLOCATION_ID_0, SIGNER.0.clone(), 4, ESCROW_VALUE / 2);
2002-
store_rav_with_options(&pgpool, signed_rav, SENDER.1, true, false)
2014+
store_rav_with_options()
2015+
.pgpool(&pgpool)
2016+
.signed_rav(signed_rav)
2017+
.sender(SENDER.1)
2018+
.last(true)
2019+
.final_rav(false)
2020+
.call()
20032021
.await
20042022
.unwrap();
20052023

20062024
// other rav final, should not be taken into account
20072025
let signed_rav = create_rav(ALLOCATION_ID_1, SIGNER.0.clone(), 4, ESCROW_VALUE / 2);
2008-
store_rav_with_options(&pgpool, signed_rav, SENDER.1, true, true)
2026+
store_rav_with_options()
2027+
.pgpool(&pgpool)
2028+
.signed_rav(signed_rav)
2029+
.sender(SENDER.1)
2030+
.last(true)
2031+
.final_rav(true)
2032+
.call()
20092033
.await
20102034
.unwrap();
20112035

@@ -2176,12 +2200,24 @@ pub mod tests {
21762200

21772201
// redeemed
21782202
let signed_rav = create_rav(ALLOCATION_ID_0, SIGNER.0.clone(), 4, ESCROW_VALUE);
2179-
store_rav_with_options(&pgpool, signed_rav, SENDER.1, true, false)
2203+
store_rav_with_options()
2204+
.pgpool(&pgpool)
2205+
.signed_rav(signed_rav)
2206+
.sender(SENDER.1)
2207+
.last(true)
2208+
.final_rav(false)
2209+
.call()
21802210
.await
21812211
.unwrap();
21822212

21832213
let signed_rav = create_rav(ALLOCATION_ID_1, SIGNER.0.clone(), 4, ESCROW_VALUE - 1);
2184-
store_rav_with_options(&pgpool, signed_rav, SENDER.1, true, false)
2214+
store_rav_with_options()
2215+
.pgpool(&pgpool)
2216+
.signed_rav(signed_rav)
2217+
.sender(SENDER.1)
2218+
.last(true)
2219+
.final_rav(false)
2220+
.call()
21852221
.await
21862222
.unwrap();
21872223

@@ -2233,7 +2269,13 @@ pub mod tests {
22332269
async fn test_thawing_deposit_process(pgpool: PgPool) {
22342270
// add last non-final ravs
22352271
let signed_rav = create_rav(ALLOCATION_ID_0, SIGNER.0.clone(), 4, ESCROW_VALUE / 2);
2236-
store_rav_with_options(&pgpool, signed_rav, SENDER.1, true, false)
2272+
store_rav_with_options()
2273+
.pgpool(&pgpool)
2274+
.signed_rav(signed_rav)
2275+
.sender(SENDER.1)
2276+
.last(true)
2277+
.final_rav(false)
2278+
.call()
22372279
.await
22382280
.unwrap();
22392281

crates/tap-agent/src/agent/sender_accounts_manager.rs

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -283,23 +283,27 @@ impl Actor for SenderAccountsManager {
283283

284284
// Start the new_receipts_watcher task that will consume from the `pglistener`
285285
// after starting all senders
286-
state.new_receipts_watcher_handle_v1 = Some(tokio::spawn(new_receipts_watcher(
287-
myself.get_cell(),
288-
pglistener_v1,
289-
escrow_accounts_v1,
290-
SenderType::Legacy,
291-
prefix.clone(),
292-
)));
286+
state.new_receipts_watcher_handle_v1 = Some(tokio::spawn(
287+
new_receipts_watcher()
288+
.sender_type(SenderType::Legacy)
289+
.actor_cell(myself.get_cell())
290+
.pglistener(pglistener_v1)
291+
.escrow_accounts_rx(escrow_accounts_v1)
292+
.maybe_prefix(prefix.clone())
293+
.call(),
294+
));
293295

294296
// Start the new_receipts_watcher task that will consume from the `pglistener`
295297
// after starting all senders
296-
state.new_receipts_watcher_handle_v2 = Some(tokio::spawn(new_receipts_watcher(
297-
myself.get_cell(),
298-
pglistener_v2,
299-
escrow_accounts_v2,
300-
SenderType::Horizon,
301-
prefix,
302-
)));
298+
state.new_receipts_watcher_handle_v2 = Some(tokio::spawn(
299+
new_receipts_watcher()
300+
.actor_cell(myself.get_cell())
301+
.pglistener(pglistener_v2)
302+
.escrow_accounts_rx(escrow_accounts_v2)
303+
.sender_type(SenderType::Horizon)
304+
.maybe_prefix(prefix)
305+
.call(),
306+
));
303307

304308
tracing::info!("SenderAccountManager created!");
305309
Ok(state)
@@ -757,6 +761,7 @@ impl State {
757761

758762
/// Continuously listens for new receipt notifications from Postgres and forwards them to the
759763
/// corresponding SenderAccount.
764+
#[bon::builder]
760765
async fn new_receipts_watcher(
761766
actor_cell: ActorCell,
762767
mut pglistener: PgListener,
@@ -1169,13 +1174,15 @@ mod tests {
11691174
let dummy_actor = DummyActor::spawn().await;
11701175

11711176
// Start the new_receipts_watcher task that will consume from the `pglistener`
1172-
let new_receipts_watcher_handle = tokio::spawn(new_receipts_watcher(
1173-
dummy_actor.get_cell(),
1174-
pglistener,
1175-
escrow_accounts_rx,
1176-
SenderType::Legacy,
1177-
Some(prefix.clone()),
1178-
));
1177+
let new_receipts_watcher_handle = tokio::spawn(
1178+
new_receipts_watcher()
1179+
.actor_cell(dummy_actor.get_cell())
1180+
.pglistener(pglistener)
1181+
.escrow_accounts_rx(escrow_accounts_rx)
1182+
.sender_type(SenderType::Legacy)
1183+
.prefix(prefix.clone())
1184+
.call(),
1185+
);
11791186

11801187
let receipts_count = 10;
11811188
// add receipts to the database
@@ -1213,13 +1220,14 @@ mod tests {
12131220
let dummy_actor = DummyActor::spawn().await;
12141221

12151222
// Start the new_receipts_watcher task that will consume from the `pglistener`
1216-
let new_receipts_watcher_handle = tokio::spawn(new_receipts_watcher(
1217-
dummy_actor.get_cell(),
1218-
pglistener,
1219-
escrow_accounts_rx,
1220-
SenderType::Legacy,
1221-
None,
1222-
));
1223+
let new_receipts_watcher_handle = tokio::spawn(
1224+
new_receipts_watcher()
1225+
.sender_type(SenderType::Legacy)
1226+
.actor_cell(dummy_actor.get_cell())
1227+
.pglistener(pglistener)
1228+
.escrow_accounts_rx(escrow_accounts_rx)
1229+
.call(),
1230+
);
12231231
pgpool.close().await;
12241232
new_receipts_watcher_handle.await.unwrap();
12251233

crates/tap-agent/src/test.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,14 @@ pub async fn store_rav(
627627
signed_rav: SignedRav,
628628
sender: Address,
629629
) -> anyhow::Result<()> {
630-
store_rav_with_options(pgpool, signed_rav, sender, false, false).await
630+
store_rav_with_options()
631+
.pgpool(pgpool)
632+
.signed_rav(signed_rav)
633+
.sender(sender)
634+
.last(false)
635+
.final_rav(false)
636+
.call()
637+
.await
631638
}
632639

633640
// TODO use static and check for possible errors with connection refused
@@ -659,6 +666,7 @@ async fn create_grpc_aggregator() -> (JoinHandle<()>, SocketAddr) {
659666
.unwrap()
660667
}
661668

669+
#[bon::builder]
662670
pub async fn store_rav_with_options(
663671
pgpool: &PgPool,
664672
signed_rav: SignedRav,

0 commit comments

Comments
 (0)