From 62b8414cd7da3bbe3e0d8b25dc9c8bd79c81bee7 Mon Sep 17 00:00:00 2001 From: Gilad Chase Date: Thu, 11 Apr 2024 06:36:11 +0300 Subject: [PATCH] feat: add Mempool and its API --- crates/mempool/src/errors.rs | 2 ++ crates/mempool/src/lib.rs | 2 ++ crates/mempool/src/mempool.rs | 37 +++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 crates/mempool/src/errors.rs create mode 100644 crates/mempool/src/mempool.rs diff --git a/crates/mempool/src/errors.rs b/crates/mempool/src/errors.rs new file mode 100644 index 00000000..75a583f7 --- /dev/null +++ b/crates/mempool/src/errors.rs @@ -0,0 +1,2 @@ +#[derive(Debug, Error)] +pub enum MempoolError {} diff --git a/crates/mempool/src/lib.rs b/crates/mempool/src/lib.rs index b25f4f10..177671c3 100644 --- a/crates/mempool/src/lib.rs +++ b/crates/mempool/src/lib.rs @@ -1,2 +1,4 @@ // TODO: change to pub(crate) once this is used by the (not yet implemented) mempool struct. +pub mod errors; +pub mod mempool; pub mod priority_queue; diff --git a/crates/mempool/src/mempool.rs b/crates/mempool/src/mempool.rs new file mode 100644 index 00000000..03fbde66 --- /dev/null +++ b/crates/mempool/src/mempool.rs @@ -0,0 +1,37 @@ +use starknet_api::{internal_transaction::InternalTransaction, transaction::TransactionHash}; + +use crate::errors::MempoolError; + +pub type MempoolResult = Result; + +pub struct Mempool; + +impl Mempool { + /// Retrieves up to `n_txs` transactions with the highest priority from the mempool. + /// Transactions are guaranteed to be unique across calls until `commit_block` is invoked. + // TODO: the last part about commit_block is incorrect if we delete txs in get_txs and then push back. + pub fn get_txs(n_txs: u8) -> MempoolResult> { + todo!(); + } + + /// Adds a new transaction to the mempool. + /// TODO: support fee escalation and transactions with future nonces. + pub fn add_tx(&mut self, tx: InternalTransaction) -> MempoolResult<()> { + todo!(); + } + + /// Update the mempool's internal state according to the committed block's transactions. + /// This method also resolves nonce gaps and updates account balances. + // TODO: the part about resolving nonce gaps is incorrect if we delete txs in get_txs and then + // push back. + pub fn commit_block( + &mut self, + block_number: u64, + txs_in_block: &[TransactionHash], + state_changes: StateChanges, + ) -> MempoolResult<()> { + todo!() + } +} + +pub struct StateChanges;