Skip to content

Commit

Permalink
feat: add staging area
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed Jun 12, 2024
1 parent ec0dae6 commit 2cc0064
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/mempool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ workspace = true

[dependencies]
async-trait.workspace = true
derive_more.workspace = true
starknet_mempool_infra = { path = "../mempool_infra", version = "0.0" }
starknet_api.workspace = true
starknet_mempool_types = { path = "../mempool_types", version = "0.0" }
Expand Down
1 change: 1 addition & 0 deletions crates/mempool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod mempool;
pub(crate) mod priority_queue;
pub mod staging_area;
pub mod transaction_store;
17 changes: 16 additions & 1 deletion crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use starknet_mempool_types::mempool_types::{
};
use tokio::sync::mpsc::Receiver;

use crate::priority_queue::TransactionPriorityQueue;
use crate::priority_queue::{PrioritizedTransaction, TransactionPriorityQueue};
use crate::staging_area::StagingArea;
use crate::transaction_store::TransactionStore;

#[cfg(test)]
Expand All @@ -25,6 +26,7 @@ pub struct Mempool {
txs_queue: TransactionPriorityQueue,
// All transactions currently held in the mempool.
tx_store: TransactionStore,
staging: StagingArea,
state: HashMap<ContractAddress, AccountState>,
}

Expand All @@ -34,6 +36,7 @@ impl Mempool {
txs_queue: TransactionPriorityQueue::default(),
tx_store: TransactionStore::default(),
state: HashMap::default(),
staging: StagingArea::default(),
};

for MempoolInput { tx, account: Account { sender_address, state } } in inputs.into_iter() {
Expand Down Expand Up @@ -74,13 +77,25 @@ impl Mempool {
let mut txs: Vec<ThinTransaction> = Vec::default();
for pq_tx in &pq_txs {
let tx = self.tx_store.remove(&pq_tx.tx_hash)?;
// TODO(Mohammad): remove from store and staging area in `commit_block`.
self.state.remove(&tx.sender_address);
self.staging.insert(tx.tx_hash)?;
txs.push(tx);
}

Ok(txs)
}

// TODO(Ayelet): implement a method that returns the next eligible transaction for the given
// sender address to be added to priority queue.
#[allow(dead_code)]
fn get_next_eligible_tx(
&self,
_sender_address: ContractAddress,
) -> Option<PrioritizedTransaction> {
todo!()
}

/// Adds a new transaction to the mempool.
/// TODO: support fee escalation and transactions with future nonces.
/// TODO: change input type to `MempoolInput`.
Expand Down
21 changes: 21 additions & 0 deletions crates/mempool/src/staging_area.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use starknet_api::transaction::TransactionHash;
use starknet_mempool_types::errors::MempoolError;

#[derive(Clone, Debug, Default, derive_more::Deref, derive_more::DerefMut)]
pub struct StagingArea(Vec<TransactionHash>);
impl StagingArea {
pub fn insert(&mut self, tx_hash: TransactionHash) -> Result<(), MempoolError> {
if self.0.contains(&tx_hash) {
return Err(MempoolError::DuplicateTransaction { tx_hash });
}
self.0.push(tx_hash);

Ok(())
}

pub fn remove(&mut self, n: usize) {
(0..n).for_each(|_| {
self.0.remove(0);
});
}
}

0 comments on commit 2cc0064

Please sign in to comment.