From fd791e3e7746942dff7a14b8871d75a848cf3779 Mon Sep 17 00:00:00 2001 From: Ayelet Zilber <138376632+ayeletstarkware@users.noreply.github.com> Date: Wed, 25 Sep 2024 10:25:43 +0300 Subject: [PATCH] refactor(mempool): rename account state to account nonce (#957) --- crates/gateway/src/gateway.rs | 4 ++-- crates/gateway/src/gateway_test.rs | 4 ++-- crates/mempool/src/mempool.rs | 14 +++++++------- crates/mempool/src/mempool_test.rs | 14 +++++++------- crates/mempool/src/transaction_pool.rs | 4 ++-- crates/mempool_types/src/mempool_types.rs | 4 ++-- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index ed96c4532f..23145a83f8 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -8,7 +8,7 @@ use starknet_api::transaction::TransactionHash; use starknet_gateway_types::errors::GatewaySpecError; use starknet_mempool_infra::component_runner::{ComponentStartError, ComponentStarter}; use starknet_mempool_types::communication::{MempoolWrapperInput, SharedMempoolClient}; -use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput}; +use starknet_mempool_types::mempool_types::{Account, AccountNonce, MempoolInput}; use starknet_sierra_compile::config::SierraToCasmCompilationConfig; use tracing::{error, info, instrument}; @@ -138,7 +138,7 @@ fn process_tx( // TODO(Arni): Add the Sierra and the Casm to the mempool input. Ok(MempoolInput { tx: executable_tx, - account: Account { sender_address, state: AccountState { nonce: account_nonce } }, + account: Account { sender_address, state: AccountNonce { nonce: account_nonce } }, }) } diff --git a/crates/gateway/src/gateway_test.rs b/crates/gateway/src/gateway_test.rs index 5228b48294..65ffdff7b3 100644 --- a/crates/gateway/src/gateway_test.rs +++ b/crates/gateway/src/gateway_test.rs @@ -9,7 +9,7 @@ use starknet_api::executable_transaction::{InvokeTransaction, Transaction}; use starknet_api::rpc_transaction::{RpcDeclareTransaction, RpcTransaction}; use starknet_gateway_types::errors::GatewaySpecError; use starknet_mempool_types::communication::{MempoolWrapperInput, MockMempoolClient}; -use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput}; +use starknet_mempool_types::mempool_types::{Account, AccountNonce, MempoolInput}; use starknet_sierra_compile::config::SierraToCasmCompilationConfig; use crate::compilation::GatewayCompiler; @@ -70,7 +70,7 @@ async fn test_add_tx() { .with(eq(MempoolWrapperInput { mempool_input: MempoolInput { tx: executable_tx, - account: Account { sender_address, state: AccountState { nonce: *rpc_tx.nonce() } }, + account: Account { sender_address, state: AccountNonce { nonce: *rpc_tx.nonce() } }, }, message_metadata: None, })) diff --git a/crates/mempool/src/mempool.rs b/crates/mempool/src/mempool.rs index 7153323eee..98f889406e 100644 --- a/crates/mempool/src/mempool.rs +++ b/crates/mempool/src/mempool.rs @@ -4,7 +4,7 @@ use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::executable_transaction::Transaction; use starknet_api::transaction::{Tip, TransactionHash, ValidResourceBounds}; use starknet_mempool_types::errors::MempoolError; -use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput, MempoolResult}; +use starknet_mempool_types::mempool_types::{Account, AccountNonce, MempoolInput, MempoolResult}; use crate::transaction_pool::TransactionPool; use crate::transaction_queue::TransactionQueue; @@ -23,7 +23,7 @@ pub struct Mempool { // Transactions eligible for sequencing. tx_queue: TransactionQueue, // Represents the current state of the mempool during block creation. - mempool_state: HashMap, + mempool_state: HashMap, // The most recent account nonces received, for all account in the pool. account_nonces: AccountToNonce, } @@ -78,7 +78,7 @@ impl Mempool { /// TODO: check Account nonce and balance. pub fn add_tx(&mut self, input: MempoolInput) -> MempoolResult<()> { self.validate_input(&input)?; - let MempoolInput { tx, account: Account { sender_address, state: AccountState { nonce } } } = + let MempoolInput { tx, account: Account { sender_address, state: AccountNonce { nonce } } } = input; self.tx_pool.insert(tx)?; self.align_to_account_state(sender_address, nonce); @@ -93,9 +93,9 @@ impl Mempool { // block. pub fn commit_block( &mut self, - state_changes: HashMap, + state_changes: HashMap, ) -> MempoolResult<()> { - for (&address, AccountState { nonce }) in &state_changes { + for (&address, AccountNonce { nonce }) in &state_changes { let next_nonce = nonce.try_increment().map_err(|_| MempoolError::FeltOutOfRange)?; self.align_to_account_state(address, next_nonce); } @@ -134,7 +134,7 @@ impl Mempool { // Stateful checks. // Check nonce against mempool state. - if let Some(AccountState { nonce: mempool_state_nonce }) = + if let Some(AccountNonce { nonce: mempool_state_nonce }) = self.mempool_state.get(&sender_address) { if mempool_state_nonce >= &tx_nonce { @@ -158,7 +158,7 @@ impl Mempool { for tx in txs { let current_account_state = Account { sender_address: tx.sender_address, - state: AccountState { nonce: tx.nonce }, + state: AccountNonce { nonce: tx.nonce }, }; if let Some(next_tx_reference) = diff --git a/crates/mempool/src/mempool_test.rs b/crates/mempool/src/mempool_test.rs index 97443a7fca..a9702b8b39 100644 --- a/crates/mempool/src/mempool_test.rs +++ b/crates/mempool/src/mempool_test.rs @@ -12,7 +12,7 @@ use starknet_api::test_utils::invoke::executable_invoke_tx; use starknet_api::transaction::{Tip, TransactionHash, ValidResourceBounds}; use starknet_api::{contract_address, felt, invoke_tx_args, patricia_key}; use starknet_mempool_types::errors::MempoolError; -use starknet_mempool_types::mempool_types::{Account, AccountState}; +use starknet_mempool_types::mempool_types::{Account, AccountNonce}; use starknet_types_core::felt::Felt; use crate::mempool::{AccountToNonce, Mempool, MempoolInput, TransactionReference}; @@ -165,7 +165,7 @@ fn commit_block( state_changes: impl IntoIterator, ) { let state_changes = HashMap::from_iter(state_changes.into_iter().map(|(address, nonce)| { - (contract_address!(address), AccountState { nonce: Nonce(felt!(nonce)) }) + (contract_address!(address), AccountNonce { nonce: Nonce(felt!(nonce)) }) })); assert_eq!(mempool.commit_block(state_changes), Ok(())); @@ -209,7 +209,7 @@ macro_rules! add_tx_input { let tx = tx!(tip: $tip, tx_hash: $tx_hash, sender_address: $sender_address, tx_nonce: $tx_nonce, resource_bounds: $resource_bounds); let sender_address = contract_address!($sender_address); let account_nonce = Nonce(felt!($account_nonce)); - let account = Account { sender_address, state: AccountState {nonce: account_nonce}}; + let account = Account { sender_address, state: AccountNonce {nonce: account_nonce}}; MempoolInput { tx, account } }}; @@ -907,7 +907,7 @@ fn test_account_nonce_does_not_decrease_in_add_tx() { fn test_account_nonces_update_in_commit_block() { // Setup. let input = add_tx_input!(tx_nonce: 2_u8, account_nonce: 0_u8); - let Account { sender_address, state: AccountState { nonce } } = input.account; + let Account { sender_address, state: AccountNonce { nonce } } = input.account; let pool_txs = [input.tx]; let mut mempool = MempoolContentBuilder::new() .with_pool(pool_txs) @@ -916,7 +916,7 @@ fn test_account_nonces_update_in_commit_block() { let committed_nonce = Nonce(Felt::ZERO); // Test: update through a commit block. - let state_changes = HashMap::from([(sender_address, AccountState { nonce: committed_nonce })]); + let state_changes = HashMap::from([(sender_address, AccountNonce { nonce: committed_nonce })]); assert_eq!(mempool.commit_block(state_changes), Ok(())); // Assert. @@ -930,7 +930,7 @@ fn test_account_nonces_update_in_commit_block() { fn test_account_nonce_does_not_decrease_in_commit_block() { // Setup. let input_account_nonce_2 = add_tx_input!(tx_nonce: 3_u8, account_nonce: 2_u8); - let Account { sender_address, state: AccountState { nonce } } = input_account_nonce_2.account; + let Account { sender_address, state: AccountNonce { nonce } } = input_account_nonce_2.account; let account_nonces = [(sender_address, nonce)]; let pool_txs = [input_account_nonce_2.tx]; let mut mempool = MempoolContentBuilder::new() @@ -940,7 +940,7 @@ fn test_account_nonce_does_not_decrease_in_commit_block() { // Test: commits state change of a lower account nonce. let state_changes = - HashMap::from([(sender_address, AccountState { nonce: Nonce(felt!(0_u8)) })]); + HashMap::from([(sender_address, AccountNonce { nonce: Nonce(felt!(0_u8)) })]); assert_eq!(mempool.commit_block(state_changes), Ok(())); // Assert: the account nonce is not updated. diff --git a/crates/mempool/src/transaction_pool.rs b/crates/mempool/src/transaction_pool.rs index f10b78b62d..c7a130f0ee 100644 --- a/crates/mempool/src/transaction_pool.rs +++ b/crates/mempool/src/transaction_pool.rs @@ -4,7 +4,7 @@ use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::executable_transaction::Transaction; use starknet_api::transaction::TransactionHash; use starknet_mempool_types::errors::MempoolError; -use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolResult}; +use starknet_mempool_types::mempool_types::{Account, AccountNonce, MempoolResult}; use crate::mempool::TransactionReference; @@ -99,7 +99,7 @@ impl TransactionPool { &self, current_account_state: Account, ) -> MempoolResult> { - let Account { sender_address, state: AccountState { nonce } } = current_account_state; + let Account { sender_address, state: AccountNonce { nonce } } = current_account_state; // TOOD(Ayelet): Change to StarknetApiError. let next_nonce = nonce.try_increment().map_err(|_| MempoolError::FeltOutOfRange)?; Ok(self.get_by_address_and_nonce(sender_address, next_nonce)) diff --git a/crates/mempool_types/src/mempool_types.rs b/crates/mempool_types/src/mempool_types.rs index 91f91a9fff..17646b794d 100644 --- a/crates/mempool_types/src/mempool_types.rs +++ b/crates/mempool_types/src/mempool_types.rs @@ -5,7 +5,7 @@ use starknet_api::executable_transaction::Transaction; use crate::errors::MempoolError; #[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)] -pub struct AccountState { +pub struct AccountNonce { pub nonce: Nonce, // TODO: add balance field when needed. } @@ -14,7 +14,7 @@ pub struct AccountState { pub struct Account { // TODO(Ayelet): Consider removing this field as it is duplicated in ThinTransaction. pub sender_address: ContractAddress, - pub state: AccountState, + pub state: AccountNonce, } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]