Skip to content

Commit

Permalink
feat(mempool): add account nonces field and adjust mempool content ac…
Browse files Browse the repository at this point in the history
…crodingly (#592)
  • Loading branch information
ayeletstarkware authored Aug 29, 2024
1 parent 6f89ece commit 4b87196
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::transaction_queue::TransactionQueue;
#[path = "mempool_test.rs"]
pub mod mempool_test;

type AccountToNonce = HashMap<ContractAddress, Nonce>;

#[derive(Debug, Default)]
pub struct Mempool {
// TODO: add docstring explaining visibility and coupling of the fields.
Expand All @@ -22,6 +24,8 @@ pub struct Mempool {
tx_queue: TransactionQueue,
// Represents the current state of the mempool during block creation.
mempool_state: HashMap<ContractAddress, AccountState>,
// The most recent account nonces received, for all account in the pool.
_account_nonces: AccountToNonce,
}

impl Mempool {
Expand Down
22 changes: 20 additions & 2 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use starknet_mempool_types::errors::MempoolError;
use starknet_mempool_types::mempool_types::{Account, AccountState};
use starknet_types_core::felt::Felt;

use crate::mempool::{Mempool, MempoolInput, TransactionReference};
use crate::mempool::{AccountToNonce, Mempool, MempoolInput, TransactionReference};
use crate::transaction_pool::TransactionPool;
use crate::transaction_queue::TransactionQueue;

Expand All @@ -29,6 +29,7 @@ use crate::transaction_queue::TransactionQueue;
struct MempoolContent<T> {
tx_pool: Option<TransactionPool>,
tx_queue: Option<TransactionQueue>,
account_nonces: Option<AccountToNonce>,
// Artificially use generic type, for the compiler.
_phantom: std::marker::PhantomData<T>,
}
Expand All @@ -46,6 +47,7 @@ impl MempoolContent<PartialContent> {
Self {
tx_pool: Some(pool_txs.into_iter().collect()),
tx_queue: Some(queue_txs.into_iter().collect()),
account_nonces: None,
_phantom: std::marker::PhantomData,
}
}
Expand All @@ -57,6 +59,7 @@ impl MempoolContent<PartialContent> {
Self {
tx_pool: Some(pool_txs.into_iter().collect()),
tx_queue: None,
account_nonces: None,
_phantom: std::marker::PhantomData,
}
}
Expand All @@ -68,6 +71,16 @@ impl MempoolContent<PartialContent> {
Self {
tx_queue: Some(queue_txs.into_iter().collect()),
tx_pool: None,
account_nonces: None,
_phantom: std::marker::PhantomData,
}
}

fn _with_account_nonces(account_nonce_pairs: Vec<(ContractAddress, Nonce)>) -> Self {
Self {
tx_pool: None,
tx_queue: None,
account_nonces: Some(account_nonce_pairs.into_iter().collect()),
_phantom: std::marker::PhantomData,
}
}
Expand All @@ -86,16 +99,21 @@ impl<T> MempoolContent<T> {
fn assert_eq_queue_content(&self, mempool: &Mempool) {
assert_eq!(self.tx_queue.as_ref().unwrap(), &mempool.tx_queue);
}

fn _assert_eq_account_nonces(&self, mempool: &Mempool) {
assert_eq!(self.account_nonces.as_ref().unwrap(), &mempool._account_nonces);
}
}

impl<T> From<MempoolContent<T>> for Mempool {
fn from(mempool_content: MempoolContent<T>) -> Mempool {
let MempoolContent { tx_pool, tx_queue, _phantom: _ } = mempool_content;
let MempoolContent { tx_pool, tx_queue, account_nonces, _phantom: _ } = mempool_content;
Mempool {
tx_pool: tx_pool.unwrap_or_default(),
tx_queue: tx_queue.unwrap_or_default(),
// TODO: Add implementation when needed.
mempool_state: Default::default(),
_account_nonces: account_nonces.unwrap_or_default(),
}
}
}
Expand Down

0 comments on commit 4b87196

Please sign in to comment.