Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Mempool and its API #43

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -11,6 +11,7 @@ workspace = true
[dependencies]
derive_more.workspace = true
starknet_api.workspace = true
thiserror.workspace = true

[dev-dependencies]
assert_matches.workspace = true
Expand Down
4 changes: 4 additions & 0 deletions crates/mempool/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum MempoolError {}
2 changes: 2 additions & 0 deletions crates/mempool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
45 changes: 45 additions & 0 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
@@ -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<T> = Result<T, MempoolError>;

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<Vec<InternalTransaction>> {
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<ContractAddress, AccountState>,
) -> MempoolResult<()> {
todo!()
}
}

pub struct AccountState;
Loading