Skip to content

Commit

Permalink
feat: add transaction store data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed May 30, 2024
1 parent 41cf4a7 commit 6c42d40
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};

use anyhow::Result;
use mempool_infra::network_component::CommunicationInterface;
use starknet_api::core::ContractAddress;
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::transaction::TransactionHash;
use starknet_mempool_types::errors::MempoolError;
use starknet_mempool_types::mempool_types::{
Expand All @@ -23,6 +23,7 @@ pub struct Mempool {
pub gateway_network: MempoolNetworkComponent,
batcher_network: BatcherToMempoolChannels,
txs_queue: TransactionPriorityQueue,
tx_store: HashMap<ContractAddress, BTreeMap<Nonce, ThinTransaction>>,
state: HashMap<ContractAddress, AccountState>,
}

Expand All @@ -34,6 +35,7 @@ impl Mempool {
) -> Self {
let mut mempool = Mempool {
txs_queue: TransactionPriorityQueue::default(),
tx_store: HashMap::default(),
state: HashMap::default(),
gateway_network,
batcher_network,
Expand All @@ -55,6 +57,14 @@ impl Mempool {
input.account.address,
input.tx
);

// Insert the transaction into the tx_store.
mempool
.tx_store
.entry(input.account.address)
.or_default()
.insert(input.tx.nonce, input.tx.clone());

input.tx
})
.collect::<Vec<ThinTransaction>>(),
Expand All @@ -79,6 +89,7 @@ impl Mempool {
let txs = self.txs_queue.pop_last_chunk(n_txs);
for tx in &txs {
self.state.remove(&tx.sender_address);
self.tx_store.remove(&tx.sender_address);
}

Ok(txs)
Expand All @@ -92,7 +103,11 @@ impl Mempool {
Occupied(_) => Err(MempoolError::DuplicateTransaction { tx_hash: tx.tx_hash }),
Vacant(entry) => {
entry.insert(account.state);
self.txs_queue.push(tx);
self.txs_queue.push(tx.clone());

// Insert the transaction into the tx_store
self.tx_store.entry(account.address).or_default().insert(tx.nonce, tx);

Ok(())
}
}
Expand Down

0 comments on commit 6c42d40

Please sign in to comment.