Skip to content

Commit 916d8c4

Browse files
refactor: use InternalRpcTransaction in gateway and mempool
1 parent 59b9bb4 commit 916d8c4

File tree

7 files changed

+58
-104
lines changed

7 files changed

+58
-104
lines changed

crates/starknet_gateway/src/gateway.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ impl ProcessTxBlockingTask {
154154
)?;
155155

156156
// TODO(Arni): Add the Sierra and the Casm to the mempool input.
157-
// TODO(noamsp): put internal_tx instead of executable_tx
158-
Ok(AddTransactionArgs { tx: executable_tx, account_state: AccountState { address, nonce } })
157+
Ok(AddTransactionArgs { tx: internal_tx, account_state: AccountState { address, nonce } })
159158
}
160159
}
161160

crates/starknet_mempool/src/mempool.rs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::collections::HashMap;
22

33
use starknet_api::block::GasPrice;
44
use starknet_api::core::{ContractAddress, Nonce};
5-
use starknet_api::executable_transaction::AccountTransaction;
65
use starknet_api::rpc_transaction::InternalRpcTransaction;
76
use starknet_api::transaction::fields::Tip;
87
use starknet_api::transaction::TransactionHash;
@@ -157,7 +156,7 @@ impl Mempool {
157156
/// created.
158157
// TODO: Consider renaming to `pop_txs` to be more consistent with the standard library.
159158
#[instrument(skip(self), err)]
160-
pub fn get_txs(&mut self, n_txs: usize) -> MempoolResult<Vec<AccountTransaction>> {
159+
pub fn get_txs(&mut self, n_txs: usize) -> MempoolResult<Vec<InternalRpcTransaction>> {
161160
let mut eligible_tx_references: Vec<TransactionReference> = Vec::with_capacity(n_txs);
162161
let mut n_remaining_txs = n_txs;
163162

@@ -194,17 +193,17 @@ impl Mempool {
194193
skip(self, args),
195194
fields( // Log subset of (informative) fields.
196195
tx_nonce = %args.tx.nonce(),
197-
tx_hash = %args.tx.tx_hash(),
198-
tx_tip = %tip(&args.tx),
199-
tx_max_l2_gas_price = %max_l2_gas_price(&args.tx),
196+
tx_hash = %args.tx.tx_hash,
197+
tx_tip = %args.tx.tip(),
198+
tx_max_l2_gas_price = %args.tx.resource_bounds().l2_gas.max_price_per_unit,
200199
account_state = %args.account_state
201200
),
202201
err
203202
)]
204203
pub fn add_tx(&mut self, args: AddTransactionArgs) -> MempoolResult<()> {
205204
let AddTransactionArgs { tx, account_state } = args;
206205
debug!("Adding transaction to mempool: {tx:#?}.");
207-
let tx_reference = TransactionReference::deprecated_new(&tx);
206+
let tx_reference = TransactionReference::new(&tx);
208207
self.validate_incoming_tx(tx_reference)?;
209208

210209
self.handle_fee_escalation(&tx)?;
@@ -319,8 +318,8 @@ impl Mempool {
319318
}
320319

321320
#[instrument(level = "debug", skip(self, incoming_tx), err)]
322-
fn handle_fee_escalation(&mut self, incoming_tx: &AccountTransaction) -> MempoolResult<()> {
323-
let incoming_tx_reference = TransactionReference::deprecated_new(incoming_tx);
321+
fn handle_fee_escalation(&mut self, incoming_tx: &InternalRpcTransaction) -> MempoolResult<()> {
322+
let incoming_tx_reference = TransactionReference::new(incoming_tx);
324323
let TransactionReference { address, nonce, .. } = incoming_tx_reference;
325324

326325
if !self.config.enable_fee_escalation {
@@ -386,15 +385,6 @@ impl Mempool {
386385
}
387386
}
388387

389-
// TODO(Elin): move to a shared location with other next-gen node crates.
390-
fn tip(tx: &AccountTransaction) -> Tip {
391-
tx.tip()
392-
}
393-
394-
fn max_l2_gas_price(tx: &AccountTransaction) -> GasPrice {
395-
tx.resource_bounds().get_l2_bounds().max_price_per_unit
396-
}
397-
398388
/// Provides a lightweight representation of a transaction for mempool usage (e.g., excluding
399389
/// execution fields).
400390
/// TODO(Mohammad): rename this struct to `ThinTransaction` once that name
@@ -409,17 +399,6 @@ pub struct TransactionReference {
409399
}
410400

411401
impl TransactionReference {
412-
// TODO(noamsp): remove this once class manager client integration is complete.
413-
pub fn deprecated_new(tx: &AccountTransaction) -> Self {
414-
TransactionReference {
415-
address: tx.contract_address(),
416-
nonce: tx.nonce(),
417-
tx_hash: tx.tx_hash(),
418-
tip: tip(tx),
419-
max_l2_gas_price: max_l2_gas_price(tx),
420-
}
421-
}
422-
423402
pub fn new(tx: &InternalRpcTransaction) -> Self {
424403
TransactionReference {
425404
address: tx.contract_address(),

crates/starknet_mempool/src/mempool_test.rs

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ use pretty_assertions::assert_eq;
77
use rstest::{fixture, rstest};
88
use starknet_api::block::GasPrice;
99
use starknet_api::core::ContractAddress;
10-
use starknet_api::executable_transaction::AccountTransaction;
11-
use starknet_api::rpc_transaction::{
12-
RpcDeployAccountTransaction,
13-
RpcInvokeTransaction,
14-
RpcTransaction,
15-
};
10+
use starknet_api::rpc_transaction::InternalRpcTransaction;
1611
use starknet_api::{contract_address, nonce};
1712
use starknet_mempool_p2p_types::communication::MockMempoolP2pPropagatorClient;
1813
use starknet_mempool_types::communication::AddTransactionArgsWrapper;
@@ -88,7 +83,7 @@ impl MempoolContentBuilder {
8883

8984
fn with_pool<P>(mut self, pool_txs: P) -> Self
9085
where
91-
P: IntoIterator<Item = AccountTransaction>,
86+
P: IntoIterator<Item = InternalRpcTransaction>,
9287
{
9388
self.tx_pool = Some(pool_txs.into_iter().collect());
9489
self
@@ -140,8 +135,8 @@ impl MempoolContentBuilder {
140135
}
141136
}
142137

143-
impl FromIterator<AccountTransaction> for TransactionPool {
144-
fn from_iter<T: IntoIterator<Item = AccountTransaction>>(txs: T) -> Self {
138+
impl FromIterator<InternalRpcTransaction> for TransactionPool {
139+
fn from_iter<T: IntoIterator<Item = InternalRpcTransaction>>(txs: T) -> Self {
145140
let mut pool = Self::default();
146141
for tx in txs {
147142
pool.insert(tx).unwrap();
@@ -154,7 +149,7 @@ impl FromIterator<AccountTransaction> for TransactionPool {
154149
fn builder_with_queue(
155150
in_priority_queue: bool,
156151
in_pending_queue: bool,
157-
tx: &AccountTransaction,
152+
tx: &InternalRpcTransaction,
158153
) -> MempoolContentBuilder {
159154
assert!(
160155
!(in_priority_queue && in_pending_queue),
@@ -164,11 +159,11 @@ fn builder_with_queue(
164159
let mut builder = MempoolContentBuilder::new();
165160

166161
if in_priority_queue {
167-
builder = builder.with_priority_queue([TransactionReference::deprecated_new(tx)]);
162+
builder = builder.with_priority_queue([TransactionReference::new(tx)]);
168163
}
169164

170165
if in_pending_queue {
171-
builder = builder.with_pending_queue([TransactionReference::deprecated_new(tx)]);
166+
builder = builder.with_pending_queue([TransactionReference::new(tx)]);
172167
}
173168

174169
builder
@@ -209,7 +204,7 @@ fn add_tx_and_verify_replacement_in_pool(
209204
#[track_caller]
210205
fn add_txs_and_verify_no_replacement(
211206
mut mempool: Mempool,
212-
existing_tx: AccountTransaction,
207+
existing_tx: InternalRpcTransaction,
213208
invalid_replacement_inputs: impl IntoIterator<Item = AddTransactionArgs>,
214209
in_priority_queue: bool,
215210
in_pending_queue: bool,
@@ -235,7 +230,7 @@ fn add_txs_and_verify_no_replacement(
235230
#[track_caller]
236231
fn add_txs_and_verify_no_replacement_in_pool(
237232
mempool: Mempool,
238-
existing_tx: AccountTransaction,
233+
existing_tx: InternalRpcTransaction,
239234
invalid_replacement_inputs: impl IntoIterator<Item = AddTransactionArgs>,
240235
) {
241236
let in_priority_queue = false;
@@ -276,7 +271,7 @@ fn test_get_txs_returns_by_priority(#[case] n_requested_txs: usize) {
276271
let tx_tip_30 = tx!(tx_hash: 2, address: "0x1", tip: 30);
277272
let tx_tip_10 = tx!(tx_hash: 3, address: "0x2", tip: 10);
278273

279-
let queue_txs = [&tx_tip_20, &tx_tip_30, &tx_tip_10].map(TransactionReference::deprecated_new);
274+
let queue_txs = [&tx_tip_20, &tx_tip_30, &tx_tip_10].map(TransactionReference::new);
280275
let pool_txs = [&tx_tip_20, &tx_tip_30, &tx_tip_10].map(|tx| tx.clone());
281276
let mut mempool = MempoolContentBuilder::new()
282277
.with_pool(pool_txs)
@@ -292,7 +287,7 @@ fn test_get_txs_returns_by_priority(#[case] n_requested_txs: usize) {
292287
assert_eq!(fetched_txs, expected_fetched_txs);
293288

294289
// Assert: non-returned transactions are still in the mempool.
295-
let remaining_tx_references = remaining_txs.iter().map(TransactionReference::deprecated_new);
290+
let remaining_tx_references = remaining_txs.iter().map(TransactionReference::new);
296291
let expected_mempool_content =
297292
MempoolContentBuilder::new().with_priority_queue(remaining_tx_references).build();
298293
expected_mempool_content.assert_eq(&mempool);
@@ -306,9 +301,7 @@ fn test_get_txs_returns_by_secondary_priority_on_tie() {
306301

307302
let mut mempool = MempoolContentBuilder::new()
308303
.with_pool([&tx_tip_10_hash_9, &tx_tip_10_hash_15].map(|tx| tx.clone()))
309-
.with_priority_queue(
310-
[&tx_tip_10_hash_9, &tx_tip_10_hash_15].map(TransactionReference::deprecated_new),
311-
)
304+
.with_priority_queue([&tx_tip_10_hash_9, &tx_tip_10_hash_15].map(TransactionReference::new))
312305
.build_into_mempool();
313306

314307
// Test and assert.
@@ -321,7 +314,7 @@ fn test_get_txs_does_not_return_pending_txs() {
321314
let tx = tx!();
322315

323316
let mut mempool = MempoolContentBuilder::new()
324-
.with_pending_queue([TransactionReference::deprecated_new(&tx)])
317+
.with_pending_queue([TransactionReference::new(&tx)])
325318
.with_pool([tx])
326319
.build_into_mempool();
327320

@@ -334,7 +327,7 @@ fn test_get_txs_does_not_remove_returned_txs_from_pool() {
334327
// Setup.
335328
let tx = tx!();
336329

337-
let queue_txs = [TransactionReference::deprecated_new(&tx)];
330+
let queue_txs = [TransactionReference::new(&tx)];
338331
let pool_txs = [tx];
339332
let mut mempool = MempoolContentBuilder::new()
340333
.with_pool(pool_txs.clone())
@@ -355,8 +348,7 @@ fn test_get_txs_replenishes_queue_only_between_chunks() {
355348
let tx_address_0_nonce_1 = tx!(tx_hash: 2, address: "0x0", tx_nonce: 1, tip: 20);
356349
let tx_address_1_nonce_0 = tx!(tx_hash: 3, address: "0x1", tx_nonce: 0, tip: 10);
357350

358-
let queue_txs =
359-
[&tx_address_0_nonce_0, &tx_address_1_nonce_0].map(TransactionReference::deprecated_new);
351+
let queue_txs = [&tx_address_0_nonce_0, &tx_address_1_nonce_0].map(TransactionReference::new);
360352
let pool_txs =
361353
[&tx_address_0_nonce_0, &tx_address_0_nonce_1, &tx_address_1_nonce_0].map(|tx| tx.clone());
362354
let mut mempool = MempoolContentBuilder::new()
@@ -382,7 +374,7 @@ fn test_get_txs_with_nonce_gap() {
382374
let tx_address_0_nonce_1 = tx!(tx_hash: 2, address: "0x0", tx_nonce: 1);
383375
let tx_address_1_nonce_0 = tx!(tx_hash: 3, address: "0x1", tx_nonce: 0);
384376

385-
let queue_txs = [TransactionReference::deprecated_new(&tx_address_1_nonce_0)];
377+
let queue_txs = [TransactionReference::new(&tx_address_1_nonce_0)];
386378
let pool_txs = [tx_address_0_nonce_1, tx_address_1_nonce_0.clone()];
387379
let mut mempool = MempoolContentBuilder::new()
388380
.with_pool(pool_txs)
@@ -415,8 +407,8 @@ fn test_add_tx_insertion_sorted_by_priority(mut mempool: Mempool) {
415407
}
416408

417409
// Assert: transactions are ordered by priority.
418-
let expected_queue_txs = [&input_tip_100.tx, &input_tip_80.tx, &input_tip_50.tx]
419-
.map(TransactionReference::deprecated_new);
410+
let expected_queue_txs =
411+
[&input_tip_100.tx, &input_tip_80.tx, &input_tip_50.tx].map(TransactionReference::new);
420412
let expected_mempool_content =
421413
MempoolContentBuilder::new().with_priority_queue(expected_queue_txs).build();
422414
expected_mempool_content.assert_eq(&mempool);
@@ -438,8 +430,8 @@ fn test_add_tx_correctly_places_txs_in_queue_and_pool(mut mempool: Mempool) {
438430
}
439431

440432
// Assert: only the eligible transactions appear in the queue.
441-
let expected_queue_txs = [&input_address_1_nonce_0.tx, &input_address_0_nonce_0.tx]
442-
.map(TransactionReference::deprecated_new);
433+
let expected_queue_txs =
434+
[&input_address_1_nonce_0.tx, &input_address_0_nonce_0.tx].map(TransactionReference::new);
443435
let expected_pool_txs =
444436
[input_address_0_nonce_0.tx, input_address_1_nonce_0.tx, input_address_0_nonce_1.tx];
445437
let expected_mempool_content = MempoolContentBuilder::new()
@@ -504,7 +496,7 @@ fn test_add_tx_with_identical_tip_succeeds(mut mempool: Mempool) {
504496
}
505497

506498
// Assert: both transactions are in the mempool.
507-
let expected_queue_txs = [&input1.tx, &input2.tx].map(TransactionReference::deprecated_new);
499+
let expected_queue_txs = [&input1.tx, &input2.tx].map(TransactionReference::new);
508500
let expected_pool_txs = [input1.tx, input2.tx];
509501
let expected_mempool_content = MempoolContentBuilder::new()
510502
.with_pool(expected_pool_txs)
@@ -535,7 +527,7 @@ fn test_add_tx_fills_nonce_gap(mut mempool: Mempool) {
535527
add_tx(&mut mempool, &input_nonce_0);
536528

537529
// Assert: only the eligible transaction appears in the queue.
538-
let expected_queue_txs = [TransactionReference::deprecated_new(&input_nonce_0.tx)];
530+
let expected_queue_txs = [TransactionReference::new(&input_nonce_0.tx)];
539531
let expected_pool_txs = [input_nonce_1.tx, input_nonce_0.tx];
540532
let expected_mempool_content = MempoolContentBuilder::new()
541533
.with_pool(expected_pool_txs)
@@ -576,7 +568,7 @@ fn test_commit_block_includes_all_proposed_txs() {
576568
let tx_address_2_nonce_1 = tx!(tx_hash: 6, address: "0x2", tx_nonce: 1);
577569

578570
let queue_txs = [&tx_address_2_nonce_1, &tx_address_1_nonce_3, &tx_address_0_nonce_4]
579-
.map(TransactionReference::deprecated_new);
571+
.map(TransactionReference::new);
580572
let pool_txs = [
581573
tx_address_0_nonce_3,
582574
tx_address_0_nonce_4.clone(),
@@ -762,7 +754,7 @@ fn test_update_gas_price_threshold_increases_threshold() {
762754
&tx!(tx_hash: 0, address: "0x0", max_l2_gas_price: 100),
763755
&tx!(tx_hash: 1, address: "0x1", max_l2_gas_price: 101),
764756
]
765-
.map(TransactionReference::deprecated_new);
757+
.map(TransactionReference::new);
766758

767759
let mut mempool: Mempool = MempoolContentBuilder::new()
768760
.with_priority_queue([tx_low_gas, tx_high_gas])
@@ -788,7 +780,7 @@ fn test_update_gas_price_threshold_decreases_threshold() {
788780
&tx!(tx_hash: 0, address: "0x0", max_l2_gas_price: 89),
789781
&tx!(tx_hash: 1, address: "0x1", max_l2_gas_price: 90),
790782
]
791-
.map(TransactionReference::deprecated_new);
783+
.map(TransactionReference::new);
792784

793785
let mut mempool: Mempool = MempoolContentBuilder::new()
794786
.with_pending_queue([tx_low_gas, tx_high_gas])
@@ -814,26 +806,11 @@ async fn test_new_tx_sent_to_p2p(mempool: Mempool) {
814806
let tx_args = add_tx_input!(tx_hash: 1, address: "0x0", tx_nonce: 2, account_nonce: 2);
815807
let propagateor_args =
816808
AddTransactionArgsWrapper { args: tx_args.clone(), p2p_message_metadata: None };
817-
// TODO: use regular conversion once we have a compiler component
818-
let rpc_tx = match tx_args.tx {
819-
AccountTransaction::Declare(_declare_tx) => {
820-
panic!("No implementation for converting DeclareTransaction to an RpcTransaction")
821-
}
822-
AccountTransaction::DeployAccount(deploy_account_transaction) => {
823-
RpcTransaction::DeployAccount(RpcDeployAccountTransaction::V3(
824-
deploy_account_transaction.clone().into(),
825-
))
826-
}
827-
AccountTransaction::Invoke(invoke_transaction) => {
828-
RpcTransaction::Invoke(RpcInvokeTransaction::V3(invoke_transaction.clone().into()))
829-
}
830-
};
831-
832809
let mut mock_mempool_p2p_propagator_client = MockMempoolP2pPropagatorClient::new();
833810
mock_mempool_p2p_propagator_client
834811
.expect_add_transaction()
835812
.times(1)
836-
.with(predicate::eq(rpc_tx))
813+
.with(predicate::eq(tx_args.tx))
837814
.returning(|_| Ok(()));
838815
let mut mempool_wrapper =
839816
MempoolCommunicationWrapper::new(mempool, Arc::new(mock_mempool_p2p_propagator_client));

crates/starknet_mempool/src/transaction_pool.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use std::collections::{hash_map, BTreeMap, HashMap};
22

33
use starknet_api::core::{ContractAddress, Nonce};
4-
use starknet_api::executable_transaction::AccountTransaction;
4+
use starknet_api::rpc_transaction::InternalRpcTransaction;
55
use starknet_api::transaction::TransactionHash;
66
use starknet_mempool_types::errors::MempoolError;
77
use starknet_mempool_types::mempool_types::{AccountState, MempoolResult};
88

99
use crate::mempool::TransactionReference;
1010
use crate::utils::try_increment_nonce;
1111

12-
type HashToTransaction = HashMap<TransactionHash, AccountTransaction>;
12+
type HashToTransaction = HashMap<TransactionHash, InternalRpcTransaction>;
1313

1414
/// Contains all transactions currently held in the mempool.
1515
/// Invariant: both data structures are consistent regarding the existence of transactions:
@@ -26,8 +26,8 @@ pub struct TransactionPool {
2626
}
2727

2828
impl TransactionPool {
29-
pub fn insert(&mut self, tx: AccountTransaction) -> MempoolResult<()> {
30-
let tx_reference = TransactionReference::deprecated_new(&tx);
29+
pub fn insert(&mut self, tx: InternalRpcTransaction) -> MempoolResult<()> {
30+
let tx_reference = TransactionReference::new(&tx);
3131
let tx_hash = tx_reference.tx_hash;
3232

3333
// Insert to pool.
@@ -52,21 +52,18 @@ impl TransactionPool {
5252
Ok(())
5353
}
5454

55-
pub fn remove(&mut self, tx_hash: TransactionHash) -> MempoolResult<AccountTransaction> {
55+
pub fn remove(&mut self, tx_hash: TransactionHash) -> MempoolResult<InternalRpcTransaction> {
5656
// Remove from pool.
5757
let tx =
5858
self.tx_pool.remove(&tx_hash).ok_or(MempoolError::TransactionNotFound { tx_hash })?;
5959

6060
// Remove from account mapping.
61-
self.txs_by_account.remove(TransactionReference::deprecated_new(&tx)).unwrap_or_else(
62-
|| {
63-
panic!(
64-
"Transaction pool consistency error: transaction with hash {tx_hash} appears \
65-
in
61+
self.txs_by_account.remove(TransactionReference::new(&tx)).unwrap_or_else(|| {
62+
panic!(
63+
"Transaction pool consistency error: transaction with hash {tx_hash} appears in
6664
main mapping, but does not appear in the account mapping"
67-
)
68-
},
69-
);
65+
)
66+
});
7067

7168
self.capacity.remove();
7269

@@ -95,7 +92,10 @@ impl TransactionPool {
9592
self.txs_by_account.account_txs_sorted_by_nonce(address)
9693
}
9794

98-
pub fn get_by_tx_hash(&self, tx_hash: TransactionHash) -> MempoolResult<&AccountTransaction> {
95+
pub fn get_by_tx_hash(
96+
&self,
97+
tx_hash: TransactionHash,
98+
) -> MempoolResult<&InternalRpcTransaction> {
9999
self.tx_pool.get(&tx_hash).ok_or(MempoolError::TransactionNotFound { tx_hash })
100100
}
101101

0 commit comments

Comments
 (0)