Skip to content

Commit b0e1ba3

Browse files
authored
Feat(test_utils): platform agnostic transfer_tx + mock_tx (#52)
* feat(test_utils): platform agnostic transfer_tx * feat(test_utils): platform agnostic transfer_tx
1 parent 740efa8 commit b0e1ba3

File tree

7 files changed

+78
-64
lines changed

7 files changed

+78
-64
lines changed

examples/checkpoints-eth.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
//! In this example are creating a new [`BlockContext`] instance using the
44
//! test-utils provided helpers for creating mock instances.
55
6-
use {
7-
alloy::{consensus::Transaction, primitives::U256},
8-
rblib::{
9-
alloy,
10-
prelude::*,
11-
test_utils::{BlockContextMocked, FundedAccounts, transfer_tx},
6+
use rblib::{
7+
alloy::{
8+
consensus::{Transaction, transaction::Recovered},
9+
primitives::U256,
10+
signers::local::PrivateKeySigner,
11+
},
12+
prelude::*,
13+
test_utils::{
14+
BlockContextMocked,
15+
FundedAccounts,
16+
transfer_tx as test_transfer_tx,
1217
},
1318
};
1419

@@ -58,3 +63,11 @@ fn main() -> eyre::Result<()> {
5863

5964
Ok(())
6065
}
66+
67+
fn transfer_tx(
68+
signer: &PrivateKeySigner,
69+
nonce: u64,
70+
value: U256,
71+
) -> Recovered<types::Transaction<Ethereum>> {
72+
test_transfer_tx::<Ethereum>(signer, nonce, value)
73+
}

examples/checkpoints-op.rs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33
//! In this example are creating a new [`BlockContext`] instance using the
44
//! test-utils provided helpers for creating mock instances.
55
6-
use {
6+
use rblib::{
77
alloy::{
8-
consensus::Transaction,
9-
network::{TransactionBuilder, TxSignerSync},
10-
optimism::{consensus::OpTxEnvelope, rpc_types::OpTransactionRequest},
11-
primitives::{Address, U256},
8+
consensus::{Transaction, transaction::Recovered},
9+
primitives::U256,
1210
signers::local::PrivateKeySigner,
1311
},
14-
rblib::{
15-
alloy,
16-
prelude::*,
17-
reth,
18-
test_utils::{BlockContextMocked, FundedAccounts},
19-
},
20-
reth::{
21-
ethereum::primitives::SignedTransaction,
22-
optimism::primitives::OpTransactionSigned,
23-
primitives::Recovered,
12+
prelude::*,
13+
test_utils::{
14+
BlockContextMocked,
15+
FundedAccounts,
16+
transfer_tx as test_transfer_tx,
2417
},
2518
};
2619

@@ -76,22 +69,6 @@ fn transfer_tx(
7669
signer: &PrivateKeySigner,
7770
nonce: u64,
7871
value: U256,
79-
) -> Recovered<OpTxEnvelope> {
80-
let mut tx = OpTransactionRequest::default()
81-
.with_nonce(nonce)
82-
.with_to(Address::random())
83-
.value(value)
84-
.with_gas_price(1_000_000_000)
85-
.with_gas_limit(21_000)
86-
.with_max_priority_fee_per_gas(1_000_000)
87-
.with_max_fee_per_gas(2_000_000)
88-
.build_unsigned()
89-
.expect("valid transaction request");
90-
91-
let sig = signer
92-
.sign_transaction_sync(&mut tx)
93-
.expect("signing should succeed");
94-
95-
OpTransactionSigned::new_unhashed(tx, sig) //
96-
.with_signer(signer.address())
72+
) -> Recovered<types::Transaction<Optimism>> {
73+
test_transfer_tx::<Optimism>(signer, nonce, value)
9774
}

src/payload/checkpoint.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,8 @@ mod tests {
608608
let checkpoint2 = checkpoint.barrier();
609609
let checkpoint3 = checkpoint2.barrier();
610610

611-
let tx = transfer_tx(&FundedAccounts::signer(0), 0, U256::from(10u64));
611+
let tx =
612+
transfer_tx::<Ethereum>(&FundedAccounts::signer(0), 0, U256::from(10u64));
612613
let checkpoint4 = checkpoint3.apply(tx).unwrap();
613614

614615
let history: Vec<_> = checkpoint4.into_iter().collect();

src/payload/ext/checkpoint.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,11 @@ mod tests {
404404
let block = BlockContext::<Ethereum>::mocked();
405405
let cp1 = Checkpoint::new_at_block(block);
406406

407-
let tx1 = transfer_tx(&FundedAccounts::signer(0), 0, U256::from(50_000u64));
407+
let tx1 = transfer_tx::<Ethereum>(
408+
&FundedAccounts::signer(0),
409+
0,
410+
U256::from(50_000u64),
411+
);
408412
let tx1_hash = *tx1.hash();
409413
assert!(!cp1.contains(tx1_hash));
410414
let cp2 = cp1.apply(tx1).unwrap();
@@ -456,10 +460,12 @@ mod tests {
456460
let base = Checkpoint::new_at_block(block);
457461

458462
// base -> x -> y
459-
let tx_x = transfer_tx(&FundedAccounts::signer(0), 0, U256::from(10u64));
463+
let tx_x =
464+
transfer_tx::<Ethereum>(&FundedAccounts::signer(0), 0, U256::from(10u64));
460465
let x = base.apply(tx_x).unwrap();
461466

462-
let tx_y = transfer_tx(&FundedAccounts::signer(1), 0, U256::from(20u64));
467+
let tx_y =
468+
transfer_tx::<Ethereum>(&FundedAccounts::signer(1), 0, U256::from(20u64));
463469
let y = x.apply(tx_y).unwrap();
464470

465471
let x_barrier = x.barrier();
@@ -487,7 +493,8 @@ mod tests {
487493
let root1 = Checkpoint::new_at_block(block1);
488494
let root2 = Checkpoint::new_at_block(block2);
489495

490-
let tx = transfer_tx(&FundedAccounts::signer(0), 0, U256::from(5u64));
496+
let tx =
497+
transfer_tx::<Ethereum>(&FundedAccounts::signer(0), 0, U256::from(5u64));
491498
let root1_child = root1.apply(tx).unwrap();
492499

493500
assert!(root1_child.to(&root2).is_err());
@@ -504,7 +511,11 @@ mod tests {
504511
"Empty checkpoint should have zero tip"
505512
);
506513

507-
let tx = transfer_tx(&FundedAccounts::signer(0), 0, U256::from(100u64));
514+
let tx = transfer_tx::<Ethereum>(
515+
&FundedAccounts::signer(0),
516+
0,
517+
U256::from(100u64),
518+
);
508519
let cp2 = cp.apply(tx.clone()).unwrap();
509520

510521
let tip = cp2.effective_tip_per_gas();
@@ -516,10 +527,12 @@ mod tests {
516527
let block = BlockContext::<Ethereum>::mocked();
517528
let base = Checkpoint::new_at_block(block);
518529

519-
let tx1 = transfer_tx(&FundedAccounts::signer(0), 0, U256::from(50u64));
530+
let tx1 =
531+
transfer_tx::<Ethereum>(&FundedAccounts::signer(0), 0, U256::from(50u64));
520532
let cp1 = base.apply(tx1).unwrap();
521533

522-
let tx2 = transfer_tx(&FundedAccounts::signer(1), 0, U256::from(75u64));
534+
let tx2 =
535+
transfer_tx::<Ethereum>(&FundedAccounts::signer(1), 0, U256::from(75u64));
523536
let cp2 = cp1.apply(tx2).unwrap();
524537

525538
let staging = cp2.history_staging();
@@ -537,12 +550,14 @@ mod tests {
537550
let block = BlockContext::<Ethereum>::mocked();
538551
let base = Checkpoint::new_at_block(block);
539552

540-
let tx1 = transfer_tx(&FundedAccounts::signer(0), 0, U256::from(50u64));
553+
let tx1 =
554+
transfer_tx::<Ethereum>(&FundedAccounts::signer(0), 0, U256::from(50u64));
541555
let cp1 = base.apply(tx1).unwrap();
542556

543557
let barrier = cp1.barrier();
544558

545-
let tx2 = transfer_tx(&FundedAccounts::signer(1), 0, U256::from(75u64));
559+
let tx2 =
560+
transfer_tx::<Ethereum>(&FundedAccounts::signer(1), 0, U256::from(75u64));
546561
let cp2 = barrier.apply(tx2).unwrap();
547562

548563
let staging = cp2.history_staging();

src/test_utils/accounts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use {
1515
std::sync::Arc,
1616
};
1717

18-
/// Those accounts are defined in the gensis block of the test local node,
18+
/// Those accounts are defined in the genesis block of the test local node,
1919
/// each prefunded with 100 ETH and nonces starting from 0.
2020
pub struct FundedAccounts;
2121

src/test_utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use {
3333
platform::{TestNodeFactory, TestablePlatform},
3434
rblib_tests_macros::{assert_is_dyn_safe, if_platform, rblib_test},
3535
step::{AlwaysBreakStep, AlwaysFailStep, AlwaysOkStep, OneStep},
36-
transactions::transfer_tx,
36+
transactions::{mock_tx, transfer_tx},
3737
};
3838

3939
#[cfg(feature = "optimism")]

src/test_utils/transactions.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
use {
2-
crate::{alloy, reth},
2+
crate::{alloy, prelude, reth, test_utils},
33
alloy::{
4-
consensus::{EthereumTxEnvelope, TxEip4844},
4+
consensus::SignableTransaction,
55
network::{TransactionBuilder, TxSignerSync},
66
primitives::{Address, U256},
77
signers::local::PrivateKeySigner,
88
},
9-
reth::{
10-
ethereum::{TransactionSigned, primitives::SignedTransaction},
11-
primitives::Recovered,
12-
rpc::types::TransactionRequest,
13-
},
9+
prelude::*,
10+
reth::{ethereum::primitives::SignedTransaction, primitives::Recovered},
11+
test_utils::FundedAccounts,
1412
};
1513

14+
/// A mock tx to easily get a valid tx from `FundedAccounts` with `key` and
15+
/// `nonce`
16+
pub fn mock_tx<P: PlatformWithRpcTypes>(
17+
key: u32,
18+
nonce: u64,
19+
) -> Recovered<types::Transaction<P>> {
20+
transfer_tx::<P>(&FundedAccounts::signer(key), nonce, U256::from(10u64))
21+
}
22+
1623
#[allow(clippy::missing_panics_doc)]
17-
pub fn transfer_tx(
24+
pub fn transfer_tx<P: PlatformWithRpcTypes>(
1825
signer: &PrivateKeySigner,
1926
nonce: u64,
2027
value: U256,
21-
) -> Recovered<EthereumTxEnvelope<TxEip4844>> {
22-
let mut tx = TransactionRequest::default()
28+
) -> Recovered<types::Transaction<P>> {
29+
let mut tx = types::TransactionRequest::<P>::default()
2330
.with_nonce(nonce)
2431
.with_to(Address::random())
25-
.value(value)
32+
.with_value(value)
2633
.with_gas_price(1_000_000_000)
2734
.with_gas_limit(21_000)
2835
.with_max_priority_fee_per_gas(1_000_000)
@@ -34,6 +41,7 @@ pub fn transfer_tx(
3441
.sign_transaction_sync(&mut tx)
3542
.expect("signing should succeed");
3643

37-
TransactionSigned::new_unhashed(tx.into(), sig) //
38-
.with_signer(signer.address())
44+
let signed_tx: types::TxEnvelope<P> = tx.into_signed(sig).into();
45+
let signed_tx: types::Transaction<P> = signed_tx.into();
46+
signed_tx.with_signer(signer.address())
3947
}

0 commit comments

Comments
 (0)