From 17d9cd136338cac4e11587299086e6ca876fe39e 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 --- Cargo.lock | 1 + crates/mempool/Cargo.toml | 1 + crates/mempool/src/errors.rs | 4 ++++ crates/mempool/src/lib.rs | 2 ++ crates/mempool/src/mempool.rs | 45 +++++++++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+) create mode 100644 crates/mempool/src/errors.rs create mode 100644 crates/mempool/src/mempool.rs diff --git a/Cargo.lock b/Cargo.lock index f121524a8..381026570 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2244,6 +2244,7 @@ dependencies = [ "assert_matches", "derive_more", "starknet_api", + "thiserror", "tokio", ] diff --git a/crates/mempool/Cargo.toml b/crates/mempool/Cargo.toml index 4beff2d2a..da1d82fa9 100644 --- a/crates/mempool/Cargo.toml +++ b/crates/mempool/Cargo.toml @@ -11,6 +11,7 @@ workspace = true [dependencies] derive_more.workspace = true starknet_api.workspace = true +thiserror.workspace = true [dev-dependencies] assert_matches.workspace = true diff --git a/crates/mempool/src/errors.rs b/crates/mempool/src/errors.rs new file mode 100644 index 000000000..4bbd78558 --- /dev/null +++ b/crates/mempool/src/errors.rs @@ -0,0 +1,4 @@ +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum MempoolError {} diff --git a/crates/mempool/src/lib.rs b/crates/mempool/src/lib.rs index b25f4f105..177671c3b 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 000000000..2a10885f0 --- /dev/null +++ b/crates/mempool/src/mempool.rs @@ -0,0 +1,45 @@ +use std::collections::HashMap; + +use starknet_api::{ + core::ContractAddress, 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, + _account_state: AccountState, + ) -> MempoolResult<()> { + todo!(); + } + + /// Update the mempool's internal state according to the committed block's transactions. + /// This method also updates internal state (resolves nonce gaps, 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: HashMap, + ) -> MempoolResult<()> { + todo!() + } +} + +pub struct AccountState;