Skip to content

Commit

Permalink
fix(mempool): update nonce calculation (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 authored Jul 18, 2024
1 parent df1cff6 commit ab3a562
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/mempool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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" }
starknet-types-core.workspace = true
tokio.workspace = true

[dev-dependencies]
Expand Down
18 changes: 13 additions & 5 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use starknet_api::transaction::{Tip, TransactionHash};
use starknet_mempool_types::mempool_types::{
Account, AccountState, MempoolInput, MempoolResult, ThinTransaction,
};
use starknet_types_core::felt::Felt;

use crate::transaction_pool::TransactionPool;
use crate::transaction_queue::TransactionQueue;
Expand Down Expand Up @@ -68,17 +69,24 @@ impl Mempool {
/// updates account balances).
// TODO: the part about resolving nonce gaps is incorrect if we delete txs in get_txs and then
// push back.
// state_changes: a map that associates each account address with the state of the committed
// block.
pub fn commit_block(
&mut self,
state_changes: HashMap<ContractAddress, AccountState>,
) -> MempoolResult<()> {
for (address, AccountState { nonce }) in state_changes {
let next_nonce = Nonce(nonce.0 + Felt::ONE);
// Dequeue transactions from the queue in the following cases:
// 1. Remove a transaction from queue with nonce lower than those committed to the
// block, applicable when the block is from the same leader.
// 2. Remove a transaction from queue with nonce greater than those committed to the
// block, applicable when the block is from a different leader.
if self.tx_queue.get_nonce(address).is_some_and(|queued_nonce| queued_nonce != nonce) {
// 1. Remove a transaction from queue with nonce lower and eq than those committed to
// the block, applicable when the block is from the same leader.
// 2. Remove a transaction from queue with nonce greater than the next nonce block,
// applicable when the block is from a different leader.
if self
.tx_queue
.get_nonce(address)
.is_some_and(|queued_nonce| queued_nonce != next_nonce)
{
self.tx_queue.remove(address);
}
// TODO: remove the transactions from the tx_pool.
Expand Down

0 comments on commit ab3a562

Please sign in to comment.