Skip to content

Commit e51ec09

Browse files
giladchaseGilad Chase
and
Gilad Chase
authored
refactor: make add_tx_input take kwargs (#341)
Motivation: subsequent PRs will want to use: ```rust add_tx_input!(tx_hash: ..., address: ...) ``` which was impossible under the previous implementation, due to the macro not being able to differentiate between the above and the `add_tx_input(tip,tx_hash)` variant (the args are untyped). Note: only kwargs were added, no changes in tests themselves. Co-Authored-By: Gilad Chase <[email protected]>
1 parent a29c6a0 commit e51ec09

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

crates/mempool/src/mempool_test.rs

+29-29
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ use starknet_mempool_types::mempool_types::{Account, ThinTransaction};
1111

1212
use crate::mempool::{Mempool, MempoolInput, TransactionReference};
1313

14-
/// Creates a valid input for mempool's `add_tx` with optional default value for
15-
/// `sender_address`.
14+
/// Creates a valid input for mempool's `add_tx` with optional default values.
1615
/// Usage:
17-
/// 1. add_tx_input!(tip, tx_hash, address, nonce)
18-
/// 2. add_tx_input!(tip, tx_hash, address)
19-
/// 3. add_tx_input!(tip, tx_hash)
16+
/// 1. add_tx_input!(tip: 1, tx_hash: StarkFelt::TWO, address: "0x3", nonce: 4)
17+
/// 2. add_tx_input!(tip: 1, tx_hash: StarkFelt::TWO, address: "0x3")
18+
/// 3. add_tx_input!(tip:1 , tx_hash: StarkFelt::TWO)
2019
macro_rules! add_tx_input {
21-
// Pattern for all four arguments
22-
($tip:expr, $tx_hash:expr, $sender_address:expr, $nonce:expr) => {{
23-
let account = Account { sender_address: $sender_address, ..Default::default() };
20+
// Pattern for all four arguments with keyword arguments.
21+
(tip: $tip:expr, tx_hash: $tx_hash:expr, sender_address: $sender_address:expr, nonce: $nonce:expr) => {{
22+
let sender_address = contract_address!($sender_address);
23+
let account = Account { sender_address, ..Default::default() };
2424
let tx = ThinTransaction {
25-
tip: $tip,
26-
tx_hash: $tx_hash,
27-
sender_address: $sender_address,
28-
nonce: $nonce,
25+
tip: Tip($tip),
26+
tx_hash: TransactionHash($tx_hash),
27+
sender_address,
28+
nonce: Nonce::from($nonce),
2929
};
3030
MempoolInput { tx, account }
3131
}};
32-
// Pattern for three arguments: tip, tx_hash, address
33-
($tip:expr, $tx_hash:expr, $address:expr) => {
34-
add_tx_input!($tip, $tx_hash, $address, Nonce::default())
32+
// Pattern for three arguments.
33+
(tip: $tip:expr, tx_hash: $tx_hash:expr, sender_address: $sender_address:expr) => {
34+
add_tx_input!(tip: $tip, tx_hash: $tx_hash, sender_address: $sender_address, nonce: Nonce::default())
3535
};
36-
// Pattern for two arguments: tip, tx_hash
37-
($tip:expr, $tx_hash:expr) => {
38-
add_tx_input!($tip, $tx_hash, ContractAddress::default(), Nonce::default())
36+
// Pattern for two arguments.
37+
(tip: $tip:expr, tx_hash: $tx_hash:expr) => {
38+
add_tx_input!(tip: $tip, tx_hash: $tx_hash, sender_address: ContractAddress::default(), nonce: Nonce::default())
3939
};
4040
}
4141

@@ -63,11 +63,11 @@ fn check_mempool_txs_eq(mempool: &Mempool, expected_txs: &[ThinTransaction]) {
6363
#[case(5)] // Requesting more transactions than are in the queue
6464
#[case(2)] // Requesting fewer transactions than are in the queue
6565
fn test_get_txs(#[case] requested_txs: usize) {
66-
let input_tip_50_address_0 = add_tx_input!(Tip(50), TransactionHash(StarkFelt::ONE));
66+
let input_tip_50_address_0 = add_tx_input!(tip: 50, tx_hash: StarkFelt::ONE);
6767
let input_tip_100_address_1 =
68-
add_tx_input!(Tip(100), TransactionHash(StarkFelt::TWO), contract_address!("0x1"));
68+
add_tx_input!(tip: 100, tx_hash: StarkFelt::TWO, sender_address: "0x1");
6969
let input_tip_10_address_2 =
70-
add_tx_input!(Tip(10), TransactionHash(StarkFelt::THREE), contract_address!("0x2"));
70+
add_tx_input!(tip: 10, tx_hash: StarkFelt::THREE, sender_address: "0x2");
7171

7272
let mut mempool = Mempool::new([
7373
input_tip_50_address_0.clone(),
@@ -94,11 +94,11 @@ fn test_get_txs(#[case] requested_txs: usize) {
9494

9595
#[rstest]
9696
fn test_add_tx(mut mempool: Mempool) {
97-
let input_tip_50_address_0 = add_tx_input!(Tip(50), TransactionHash(StarkFelt::ONE));
97+
let input_tip_50_address_0 = add_tx_input!(tip: 50, tx_hash: StarkFelt::ONE);
9898
let input_tip_100_address_1 =
99-
add_tx_input!(Tip(100), TransactionHash(StarkFelt::TWO), contract_address!("0x1"));
99+
add_tx_input!(tip: 100, tx_hash: StarkFelt::TWO, sender_address: "0x1");
100100
let input_tip_80_address_2 =
101-
add_tx_input!(Tip(80), TransactionHash(StarkFelt::THREE), contract_address!("0x2"));
101+
add_tx_input!(tip: 80, tx_hash: StarkFelt::THREE, sender_address: "0x2");
102102

103103
assert_eq!(mempool.add_tx(input_tip_50_address_0.clone()), Ok(()));
104104
assert_eq!(mempool.add_tx(input_tip_100_address_1.clone()), Ok(()));
@@ -111,7 +111,7 @@ fn test_add_tx(mut mempool: Mempool) {
111111

112112
#[rstest]
113113
fn test_add_same_tx(mut mempool: Mempool) {
114-
let input = add_tx_input!(Tip(50), TransactionHash(StarkFelt::ONE));
114+
let input = add_tx_input!(tip: 50, tx_hash: StarkFelt::ONE);
115115
let same_input = input.clone();
116116

117117
assert_eq!(mempool.add_tx(input), Ok(()));
@@ -126,11 +126,11 @@ fn test_add_same_tx(mut mempool: Mempool) {
126126

127127
#[rstest]
128128
fn test_add_tx_with_identical_tip_succeeds(mut mempool: Mempool) {
129-
let input1 = add_tx_input!(Tip(1), TransactionHash(StarkFelt::TWO));
129+
let input1 = add_tx_input!(tip: 1, tx_hash: StarkFelt::TWO);
130130

131131
// Create a transaction with identical tip, it should be allowed through since the priority
132132
// queue tie-breaks identical tips by other tx-unique identifiers (for example tx hash).
133-
let input2 = add_tx_input!(Tip(1), TransactionHash(StarkFelt::ONE), contract_address!("0x1"));
133+
let input2 = add_tx_input!(tip: 1, tx_hash: StarkFelt::ONE, sender_address: "0x1");
134134

135135
assert_eq!(mempool.add_tx(input1.clone()), Ok(()));
136136
assert_eq!(mempool.add_tx(input2.clone()), Ok(()));
@@ -142,12 +142,12 @@ fn test_add_tx_with_identical_tip_succeeds(mut mempool: Mempool) {
142142

143143
#[rstest]
144144
fn test_tip_priority_over_tx_hash(mut mempool: Mempool) {
145-
let input_big_tip_small_hash = add_tx_input!(Tip(2), TransactionHash(StarkFelt::ONE));
145+
let input_big_tip_small_hash = add_tx_input!(tip: 2, tx_hash: StarkFelt::ONE);
146146

147147
// Create a transaction with identical tip, it should be allowed through since the priority
148148
// queue tie-breaks identical tips by other tx-unique identifiers (for example tx hash).
149149
let input_small_tip_big_hash =
150-
add_tx_input!(Tip(1), TransactionHash(StarkFelt::TWO), contract_address!("0x1"));
150+
add_tx_input!(tip: 1, tx_hash: StarkFelt::TWO, sender_address: "0x1");
151151

152152
assert_eq!(mempool.add_tx(input_big_tip_small_hash.clone()), Ok(()));
153153
assert_eq!(mempool.add_tx(input_small_tip_big_hash.clone()), Ok(()));

0 commit comments

Comments
 (0)