Skip to content

Commit

Permalink
refactor(mempool): rename account state to account nonce (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeletstarkware authored Sep 25, 2024
1 parent 43a12a3 commit fd791e3
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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 } },
})
}

Expand Down
4 changes: 2 additions & 2 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}))
Expand Down
14 changes: 7 additions & 7 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ContractAddress, AccountState>,
mempool_state: HashMap<ContractAddress, AccountNonce>,
// The most recent account nonces received, for all account in the pool.
account_nonces: AccountToNonce,
}
Expand Down Expand Up @@ -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);
Expand All @@ -93,9 +93,9 @@ impl Mempool {
// block.
pub fn commit_block(
&mut self,
state_changes: HashMap<ContractAddress, AccountState>,
state_changes: HashMap<ContractAddress, AccountNonce>,
) -> 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);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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) =
Expand Down
14 changes: 7 additions & 7 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -165,7 +165,7 @@ fn commit_block(
state_changes: impl IntoIterator<Item = (&'static str, u8)>,
) {
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(()));
Expand Down Expand Up @@ -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 }
}};
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -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()
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions crates/mempool/src/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -99,7 +99,7 @@ impl TransactionPool {
&self,
current_account_state: Account,
) -> MempoolResult<Option<&TransactionReference>> {
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))
Expand Down
4 changes: 2 additions & 2 deletions crates/mempool_types/src/mempool_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand All @@ -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)]
Expand Down

0 comments on commit fd791e3

Please sign in to comment.