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(starknet_mempool): define the mempool config to be a SerializeConfig #4260

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/starknet_mempool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ workspace = true
async-trait.workspace = true
derive_more.workspace = true
mempool_test_utils = { workspace = true, optional = true }
papyrus_config.workspace = true
papyrus_network_types.workspace = true
pretty_assertions = { workspace = true, optional = true }
serde.workspace = true
starknet-types-core = { workspace = true, optional = true }
starknet_api.workspace = true
starknet_sequencer_infra.workspace = true
starknet_mempool_p2p_types.workspace = true
starknet_mempool_types.workspace = true
tokio.workspace = true
tracing.workspace = true
validator.workspace = true

[dev-dependencies]
assert_matches.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/starknet_mempool/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use starknet_mempool_types::mempool_types::{CommitBlockArgs, MempoolResult};
use starknet_sequencer_infra::component_definitions::{ComponentRequestHandler, ComponentStarter};
use starknet_sequencer_infra::component_server::{LocalComponentServer, RemoteComponentServer};

use crate::config::MempoolConfig;
use crate::mempool::Mempool;
use crate::utils::InstantClock;

Expand All @@ -27,7 +28,7 @@ pub fn create_mempool(
mempool_p2p_propagator_client: SharedMempoolP2pPropagatorClient,
) -> MempoolCommunicationWrapper {
MempoolCommunicationWrapper::new(
Mempool::new(Arc::new(InstantClock)),
Mempool::new(MempoolConfig::default(), Arc::new(InstantClock)),
mempool_p2p_propagator_client,
)
}
Expand Down
55 changes: 55 additions & 0 deletions crates/starknet_mempool/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::collections::BTreeMap;
use std::time::Duration;

use papyrus_config::converters::deserialize_seconds_to_duration;
use papyrus_config::dumping::{ser_param, SerializeConfig};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use serde::{Deserialize, Serialize};
use validator::Validate;

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Validate)]
pub struct MempoolConfig {
pub enable_fee_escalation: bool,
// TODO(AlonH): consider adding validations; should be bounded?
// Percentage increase for tip and max gas price to enable transaction replacement.
pub fee_escalation_percentage: u8, // E.g., 10 for a 10% increase.
// Time-to-live for transactions in the mempool, in seconds.
// Transactions older than this value will be lazily removed.
#[serde(deserialize_with = "deserialize_seconds_to_duration")]
pub transaction_ttl: Duration,
}

impl Default for MempoolConfig {
fn default() -> Self {
MempoolConfig {
enable_fee_escalation: true,
fee_escalation_percentage: 10,
transaction_ttl: Duration::from_secs(60), // 1 minute.
}
}
}

impl SerializeConfig for MempoolConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::from_iter([
ser_param(
"enable_fee_escalation",
&self.enable_fee_escalation,
"If true, transactions can be replaced with higher fee transactions.",
ParamPrivacyInput::Public,
),
ser_param(
"fee_escalation_percentage",
&self.fee_escalation_percentage,
"Percentage increase for tip and max gas price to enable transaction replacement.",
ParamPrivacyInput::Public,
),
ser_param(
"transaction_ttl",
&self.transaction_ttl,
"Time-to-live for transactions in the mempool, in seconds.",
ParamPrivacyInput::Public,
),
])
}
}
1 change: 1 addition & 0 deletions crates/starknet_mempool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod communication;
pub mod config;
pub mod mempool;
pub(crate) mod suspended_transaction_pool;
pub(crate) mod transaction_pool;
Expand Down
27 changes: 3 additions & 24 deletions crates/starknet_mempool/src/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use starknet_api::block::NonzeroGasPrice;
use starknet_api::core::{ContractAddress, Nonce};
Expand All @@ -16,6 +15,7 @@ use starknet_mempool_types::mempool_types::{
};
use tracing::{debug, info, instrument};

use crate::config::MempoolConfig;
use crate::transaction_pool::TransactionPool;
use crate::transaction_queue::TransactionQueue;
use crate::utils::{try_increment_nonce, Clock};
Expand All @@ -24,27 +24,6 @@ use crate::utils::{try_increment_nonce, Clock};
#[path = "mempool_test.rs"]
pub mod mempool_test;

#[derive(Debug, Clone)]
pub struct MempoolConfig {
enable_fee_escalation: bool,
// TODO(AlonH): consider adding validations; should be bounded?
// Percentage increase for tip and max gas price to enable transaction replacement.
fee_escalation_percentage: u8, // E.g., 10 for a 10% increase.
// Time-to-live for transactions in the mempool, in seconds.
// Transactions older than this value will be lazily removed.
transaction_ttl: Duration,
}

impl Default for MempoolConfig {
fn default() -> Self {
MempoolConfig {
enable_fee_escalation: true,
fee_escalation_percentage: 10,
transaction_ttl: Duration::from_secs(60), // 1 minute.
}
}
}

type AddressToNonce = HashMap<ContractAddress, Nonce>;

/// Represents the state tracked by the mempool.
Expand Down Expand Up @@ -155,9 +134,9 @@ pub struct Mempool {
}

impl Mempool {
pub fn new(clock: Arc<dyn Clock>) -> Self {
pub fn new(config: MempoolConfig, clock: Arc<dyn Clock>) -> Self {
Mempool {
config: MempoolConfig::default(),
config,
tx_pool: TransactionPool::new(clock.clone()),
tx_queue: TransactionQueue::default(),
state: MempoolState::default(),
Expand Down
12 changes: 8 additions & 4 deletions crates/starknet_mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,10 @@ fn tx_from_address_exists(#[case] state: MempoolState, #[case] expected_result:
fn add_tx_old_transactions_cleanup() {
// Create a mempool with a fake clock.
let fake_clock = Arc::new(FakeClock::default());
let mut mempool = Mempool::new(fake_clock.clone());
mempool.config.transaction_ttl = Duration::from_secs(60);
let mut mempool = Mempool::new(
MempoolConfig { transaction_ttl: Duration::from_secs(60), ..Default::default() },
fake_clock.clone(),
);

// Add a new transaction to the mempool.
let first_tx =
Expand Down Expand Up @@ -968,8 +970,10 @@ fn add_tx_old_transactions_cleanup() {
fn get_txs_old_transactions_cleanup() {
// Create a mempool with a fake clock.
let fake_clock = Arc::new(FakeClock::default());
let mut mempool = Mempool::new(fake_clock.clone());
mempool.config.transaction_ttl = Duration::from_secs(60);
let mut mempool = Mempool::new(
MempoolConfig { transaction_ttl: Duration::from_secs(60), ..Default::default() },
fake_clock.clone(),
);

// Add a new transaction to the mempool.
let first_tx =
Expand Down
Loading