Skip to content

Commit 8d75cf1

Browse files
refactor: use builder pattern to improve readability of big functions (#644)
* refactor: use builder pattern to simplify `TapAgentContext` creation * refactor: improve readability of `store_rav_with_options` with bon
1 parent 59e5b2c commit 8d75cf1

File tree

7 files changed

+126
-96
lines changed

7 files changed

+126
-96
lines changed

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

+49-7
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

+36-28
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/agent/sender_allocation.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,14 @@ where
468468
escrow_accounts.clone(),
469469
)),
470470
];
471-
let context = TapAgentContext::new(
472-
pgpool.clone(),
473-
allocation_id,
474-
config.indexer_address,
475-
sender,
476-
escrow_accounts.clone(),
477-
);
471+
let context = TapAgentContext::builder()
472+
.pgpool(pgpool.clone())
473+
.allocation_id(allocation_id)
474+
.indexer_address(config.indexer_address)
475+
.sender(sender)
476+
.escrow_accounts(escrow_accounts.clone())
477+
.build();
478+
478479
let latest_rav = context.last_rav().await.unwrap_or_default();
479480
let tap_manager = TapManager::new(
480481
domain_separator.clone(),

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

+5-22
Original file line numberDiff line numberDiff line change
@@ -152,36 +152,19 @@ impl NetworkVersion for Horizon {
152152
/// Context used by [tap_core::manager::Manager] that enables certain helper methods
153153
///
154154
/// This context is implemented for PostgresSQL
155-
#[derive(Clone)]
155+
#[derive(Clone, bon::Builder)]
156156
pub struct TapAgentContext<T> {
157157
pgpool: PgPool,
158+
#[cfg_attr(test, builder(default = crate::test::ALLOCATION_ID_0))]
158159
allocation_id: Address,
160+
#[cfg_attr(test, builder(default = test_assets::TAP_SENDER.1))]
159161
sender: Address,
162+
#[cfg_attr(test, builder(default = crate::test::INDEXER.1))]
160163
indexer_address: Address,
161164
escrow_accounts: Receiver<EscrowAccounts>,
162165
/// We use phantom data as a marker since it's
163166
/// only used to define what methods are available
164167
/// for each type of network
168+
#[builder(default = PhantomData)]
165169
_phantom: PhantomData<T>,
166170
}
167-
168-
/// Allow any [NetworkVersion] to create a new context
169-
impl<T: NetworkVersion> TapAgentContext<T> {
170-
/// Creates a TapContext
171-
pub fn new(
172-
pgpool: PgPool,
173-
allocation_id: Address,
174-
indexer_address: Address,
175-
sender: Address,
176-
escrow_accounts: Receiver<EscrowAccounts>,
177-
) -> Self {
178-
Self {
179-
pgpool,
180-
allocation_id,
181-
indexer_address,
182-
sender,
183-
escrow_accounts,
184-
_phantom: PhantomData,
185-
}
186-
}
187-
}

crates/tap-agent/src/tap/context/rav.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,13 @@ mod test {
326326
use rstest::rstest;
327327
use sqlx::PgPool;
328328
use tap_core::signed_message::Eip712SignedMessage;
329-
use test_assets::{TAP_SENDER as SENDER, TAP_SIGNER as SIGNER};
329+
use test_assets::TAP_SIGNER as SIGNER;
330330
use tokio::sync::watch;
331331

332332
use super::*;
333333
use crate::{
334334
tap::context::NetworkVersion,
335-
test::{CreateRav, ALLOCATION_ID_0, INDEXER},
335+
test::{CreateRav, ALLOCATION_ID_0},
336336
};
337337

338338
#[derive(Debug)]
@@ -351,23 +351,17 @@ mod test {
351351
const VALUE_AGGREGATE: u128 = u128::MAX;
352352

353353
async fn legacy_adapter(pgpool: PgPool) -> TapAgentContext<Legacy> {
354-
TapAgentContext::new(
355-
pgpool,
356-
ALLOCATION_ID_0,
357-
INDEXER.1,
358-
SENDER.1,
359-
watch::channel(EscrowAccounts::default()).1,
360-
)
354+
TapAgentContext::builder()
355+
.pgpool(pgpool)
356+
.escrow_accounts(watch::channel(EscrowAccounts::default()).1)
357+
.build()
361358
}
362359

363360
async fn horizon_adapter(pgpool: PgPool) -> TapAgentContext<Horizon> {
364-
TapAgentContext::new(
365-
pgpool,
366-
ALLOCATION_ID_0,
367-
INDEXER.1,
368-
SENDER.1,
369-
watch::channel(EscrowAccounts::default()).1,
370-
)
361+
TapAgentContext::builder()
362+
.pgpool(pgpool)
363+
.escrow_accounts(watch::channel(EscrowAccounts::default()).1)
364+
.build()
371365
}
372366

373367
/// Insert a single receipt and retrieve it from the database using the adapter.

crates/tap-agent/src/tap/context/receipt.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ mod test {
406406
use tokio::sync::watch::{self, Receiver};
407407

408408
use super::*;
409-
use crate::test::{store_receipt, CreateReceipt, INDEXER, SENDER_2};
409+
use crate::test::{store_receipt, CreateReceipt, SENDER_2};
410410

411411
const ALLOCATION_ID_IRRELEVANT: Address = ALLOCATION_ID_1;
412412

@@ -427,26 +427,20 @@ mod test {
427427
pgpool: PgPool,
428428
escrow_accounts: Receiver<EscrowAccounts>,
429429
) -> TapAgentContext<Legacy> {
430-
TapAgentContext::new(
431-
pgpool.clone(),
432-
ALLOCATION_ID_0,
433-
INDEXER.1,
434-
SENDER.1,
435-
escrow_accounts,
436-
)
430+
TapAgentContext::builder()
431+
.pgpool(pgpool)
432+
.escrow_accounts(escrow_accounts)
433+
.build()
437434
}
438435

439436
async fn horizon_adapter(
440437
pgpool: PgPool,
441438
escrow_accounts: Receiver<EscrowAccounts>,
442439
) -> TapAgentContext<Horizon> {
443-
TapAgentContext::new(
444-
pgpool,
445-
ALLOCATION_ID_0,
446-
INDEXER.1,
447-
SENDER.1,
448-
escrow_accounts,
449-
)
440+
TapAgentContext::builder()
441+
.pgpool(pgpool)
442+
.escrow_accounts(escrow_accounts)
443+
.build()
450444
}
451445

452446
/// Insert a single receipt and retrieve it from the database using the adapter.

crates/tap-agent/src/test.rs

+9-1
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)