Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mempool): impl mempool state #497

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct Mempool {
tx_pool: TransactionPool,
// Transactions eligible for sequencing.
tx_queue: TransactionQueue,
// Represents the current state of the mempool during block creation.
mempool_state: HashMap<ContractAddress, AccountState>,
}

impl Mempool {
Expand Down Expand Up @@ -55,13 +57,19 @@ impl Mempool {
eligible_txs.push(tx);
}

// Update the mempool state with the new nonces.
for tx in &eligible_txs {
self.mempool_state.entry(tx.sender_address).or_default().nonce = tx.nonce;
}

Ok(eligible_txs)
}

/// Adds a new transaction to the mempool.
/// TODO: support fee escalation and transactions with future nonces.
/// TODO: check Account nonce and balance.
pub fn add_tx(&mut self, input: MempoolInput) -> MempoolResult<()> {
self.validate_input(&input)?;
self.insert_tx(input)
}

Expand Down Expand Up @@ -111,6 +119,28 @@ impl Mempool {
Ok(())
}

fn validate_input(&self, input: &MempoolInput) -> MempoolResult<()> {
let MempoolInput { tx, account } = input;
if let Some(AccountState { nonce }) = self.mempool_state.get(&tx.sender_address) {
if nonce >= &tx.nonce {
return Err(MempoolError::DuplicateNonce {
address: tx.sender_address,
nonce: tx.nonce,
});
}
}

let Account { state: AccountState { nonce }, .. } = account;
if nonce > &tx.nonce {
return Err(MempoolError::DuplicateNonce {
address: tx.sender_address,
nonce: tx.nonce,
});
}

Ok(())
}

#[cfg(test)]
pub(crate) fn _tx_pool(&self) -> &TransactionPool {
&self.tx_pool
Expand Down
2 changes: 1 addition & 1 deletion crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl MempoolState {
impl From<MempoolState> for Mempool {
fn from(mempool_state: MempoolState) -> Mempool {
let MempoolState { tx_pool, tx_queue } = mempool_state;
Mempool { tx_pool, tx_queue }
Mempool { tx_pool, tx_queue, ..Default::default() }
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/mempool_types/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::transaction::TransactionHash;
use thiserror::Error;

#[derive(Clone, Debug, Error, PartialEq, Eq)]
pub enum MempoolError {
#[error("Duplicate transaction, sender address: {address}, nonce: {:?}", nonce)]
DuplicateNonce { address: ContractAddress, nonce: Nonce },
#[error("Duplicate transaction, with hash: {tx_hash}")]
DuplicateTransaction { tx_hash: TransactionHash },
#[error("Transaction with hash: {tx_hash} not found")]
Expand Down
Loading