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(code/starknet): Add mempool load generator to the Starknet test app #821

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
193 changes: 193 additions & 0 deletions code/crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub struct Config {
/// Mempool configuration options
pub mempool: MempoolConfig,

/// Mempool load configuration options
pub mempool_load: MempoolLoadConfig,

/// Sync configuration options
pub sync: SyncConfig,

Expand Down Expand Up @@ -339,6 +342,196 @@ mod gossipsub {
}
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "load_type", rename_all = "snake_case")]
pub enum MempoolLoadType {
UniformLoad(UniformLoadConfig),
// #[default]
NoLoad,
NonUniformLoad(NonUniformLoadConfig),
}

impl Default for MempoolLoadType {
fn default() -> Self {
Self::UniformLoad(UniformLoadConfig::default())
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(from = "nonuniformload::RawConfig", default)]
pub struct NonUniformLoadConfig {
/// Base transaction count
base_count: usize,

/// Base transaction size
base_size: usize,

/// How much the transaction count can vary
count_variation: std::ops::Range<i32>,

size_variation: std::ops::Range<i32>,

/// Chance of generating a spike.
/// e.g. 0.1 = 10% chance of spike
spike_probability: f64,

/// Multiplier for spike transactions
/// e.g. 10 = 10x more transactions during spike
spike_multiplier: usize,

/// Range of intervals between generating load, in milliseconds
sleep_interval: std::ops::Range<u64>,
}
impl NonUniformLoadConfig {
pub fn new(
base_count: usize,
base_size: usize,
count_variation: std::ops::Range<i32>,
size_variation: std::ops::Range<i32>,
spike_probability: f64,
spike_multiplier: usize,
sleep_interval: std::ops::Range<u64>,
) -> Self {
Self {
base_count,
base_size,
count_variation,
size_variation,
spike_probability,
spike_multiplier,
sleep_interval,
}
}
pub fn base_count(&self) -> usize {
self.base_count
}
pub fn base_size(&self) -> usize {
self.base_size
}
pub fn count_variation(&self) -> std::ops::Range<i32> {
self.count_variation.clone()
}
pub fn size_variation(&self) -> std::ops::Range<i32> {
self.size_variation.clone()
}
pub fn spike_probability(&self) -> f64 {
self.spike_probability
}
pub fn spike_multiplier(&self) -> usize {
self.spike_multiplier
}
pub fn sleep_interval(&self) -> std::ops::Range<u64> {
self.sleep_interval.clone()
}
}

mod nonuniformload {
#[derive(serde::Deserialize)]
pub struct RawConfig {
/// Base transaction count
#[serde(default)]
base_count: usize,

/// Base transaction size
#[serde(default)]
base_size: usize,

/// How much the transaction count can vary
#[serde(default)]
count_variation: std::ops::Range<i32>,

#[serde(default)]
size_variation: std::ops::Range<i32>,

/// Chance of generating a spike.
/// e.g. 0.1 = 10% chance of spike
#[serde(default)]
spike_probability: f64,

/// Multiplier for spike transactions
/// e.g. 10 = 10x more transactions during spike
#[serde(default)]
spike_multiplier: usize,

/// Range of intervals between generating load, in milliseconds
#[serde(default)]
sleep_interval: std::ops::Range<u64>,
}

impl From<RawConfig> for super::NonUniformLoadConfig {
fn from(raw: RawConfig) -> Self {
super::NonUniformLoadConfig::new(
raw.base_count,
raw.base_size,
raw.count_variation,
raw.size_variation,
raw.spike_probability,
raw.spike_multiplier,
raw.sleep_interval,
)
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(from = "uniformload::RawConfig", default)]
pub struct UniformLoadConfig {
#[serde(with = "humantime_serde")]
interval: Duration,
count: usize,
size: usize,
}
impl UniformLoadConfig {
fn new(interval: Duration, count: usize, size: usize) -> Self {
Self {
interval,
count,
size,
}
}
pub fn interval(&self) -> Duration {
self.interval
}

pub fn count(&self) -> usize {
self.count
}

pub fn size(&self) -> usize {
self.size
}
}
impl Default for UniformLoadConfig {
fn default() -> Self {
Self::new(Duration::from_secs(10), 1000, 256)
}
}

mod uniformload {
use std::time::Duration;

#[derive(serde::Deserialize)]
pub struct RawConfig {
#[serde(default)]
#[serde(with = "humantime_serde")]
interval: Duration,
#[serde(default)]
count: usize,
#[serde(default)]
size: usize,
}

impl From<RawConfig> for super::UniformLoadConfig {
fn from(raw: RawConfig) -> Self {
super::UniformLoadConfig::new(raw.interval, raw.count, raw.size)
}
}
}
/// Mempool configuration options
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct MempoolLoadConfig {
/// Mempool loading type
pub load_type: MempoolLoadType,
}

/// Mempool configuration options
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct MempoolConfig {
Expand Down
9 changes: 8 additions & 1 deletion code/crates/starknet/host/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ use crate::host::proposal::compute_proposal_signature;
use crate::host::state::HostState;
use crate::host::{Host as _, StarknetHost};
use crate::mempool::{MempoolMsg, MempoolRef};
use crate::mempool_load::MempoolLoadRef;
use crate::proto::Protobuf;
use crate::types::*;

pub struct Host {
mempool: MempoolRef,
mempool_load: MempoolLoadRef,
network: NetworkRef<MockContext>,
metrics: Metrics,
span: tracing::Span,
Expand All @@ -41,6 +43,7 @@ impl Host {
home_dir: PathBuf,
host: StarknetHost,
mempool: MempoolRef,
mempool_load: MempoolLoadRef,
network: NetworkRef<MockContext>,
metrics: Metrics,
span: tracing::Span,
Expand All @@ -51,7 +54,7 @@ impl Host {

let (actor_ref, _) = Actor::spawn(
None,
Self::new(mempool, network, metrics, span),
Self::new(mempool, mempool_load, network, metrics, span),
HostState::new(host, db_path, &mut StdRng::from_entropy()),
)
.await?;
Expand All @@ -61,12 +64,14 @@ impl Host {

pub fn new(
mempool: MempoolRef,
mempool_load: MempoolLoadRef,
network: NetworkRef<MockContext>,
metrics: Metrics,
span: tracing::Span,
) -> Self {
Self {
mempool,
mempool_load,
network,
metrics,
span,
Expand All @@ -86,6 +91,7 @@ impl Actor for Host {
initial_state: Self::State,
) -> Result<Self::State, ActorProcessingErr> {
self.mempool.link(myself.get_cell());
self.mempool_load.link(myself.get_cell());

Ok(initial_state)
}
Expand Down Expand Up @@ -545,6 +551,7 @@ async fn on_received_proposal_part(
Ok(())
}

//TODO
async fn on_decided(
state: &mut HostState,
consensus: &ConsensusRef<MockContext>,
Expand Down
4 changes: 4 additions & 0 deletions code/crates/starknet/host/src/host/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use malachitebft_core_types::{CommitCertificate, Extension, Round, SignedExtensi

use crate::host::Host;
use crate::mempool::MempoolRef;
use crate::mempool_load::MempoolLoadRef;
use crate::part_store::PartStore;
use crate::types::*;

Expand All @@ -33,6 +34,7 @@ pub struct StarknetParams {
pub struct StarknetHost {
pub params: StarknetParams,
pub mempool: MempoolRef,
pub mempool_load: MempoolLoadRef,
pub address: Address,
pub private_key: PrivateKey,
pub validator_set: ValidatorSet,
Expand All @@ -43,13 +45,15 @@ impl StarknetHost {
pub fn new(
params: StarknetParams,
mempool: MempoolRef,
mempool_load: MempoolLoadRef,
address: Address,
private_key: PrivateKey,
validator_set: ValidatorSet,
) -> Self {
Self {
params,
mempool,
mempool_load,
address,
private_key,
validator_set,
Expand Down
4 changes: 2 additions & 2 deletions code/crates/starknet/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ pub mod block_store;
pub mod codec;
pub mod host;
pub mod mempool;
pub mod mempool_load;
pub mod node;
pub mod spawn;
pub mod streaming;

pub mod utils;
pub use malachitebft_app::part_store;

pub mod proto {
pub use malachitebft_proto::*;
pub use malachitebft_starknet_p2p_proto::*;
Expand Down
Loading
Loading