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(batcher, mempool_node): add local storage to the batcher #982

Merged
merged 1 commit into from
Sep 25, 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
2 changes: 2 additions & 0 deletions Cargo.lock

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

56 changes: 53 additions & 3 deletions config/mempool/default_config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,58 @@
{
"batcher_config.batcher_config_param_1": {
"description": "The first batcher configuration parameter",
"batcher_config.storage.db_config.chain_id": {
"description": "The chain to follow. For more details see https://docs.starknet.io/documentation/architecture_and_concepts/Blocks/transactions/#chain-id.",
"pointer_target": "chain_id",
"privacy": "Public"
},
"batcher_config.storage.db_config.enforce_file_exists": {
"description": "Whether to enforce that the path exists. If true, `open_env` fails when the mdbx.dat file does not exist.",
"privacy": "Public",
"value": 1
"value": true
},
"batcher_config.storage.db_config.growth_step": {
"description": "The growth step in bytes, must be greater than zero to allow the database to grow.",
"privacy": "Public",
"value": 4294967296
},
"batcher_config.storage.db_config.max_size": {
"description": "The maximum size of the node's storage in bytes.",
"privacy": "Public",
"value": 1099511627776
},
"batcher_config.storage.db_config.min_size": {
"description": "The minimum size of the node's storage in bytes.",
"privacy": "Public",
"value": 1048576
},
"batcher_config.storage.db_config.path_prefix": {
"description": "Prefix of the path of the node's storage directory, the storage file path will be <path_prefix>/<chain_id>. The path is not created automatically.",
"privacy": "Public",
"value": "."
},
"batcher_config.storage.mmap_file_config.growth_step": {
"description": "The growth step in bytes, must be greater than max_object_size.",
"privacy": "Public",
"value": 1073741824
},
"batcher_config.storage.mmap_file_config.max_object_size": {
"description": "The maximum size of a single object in the file in bytes",
"privacy": "Public",
"value": 268435456
},
"batcher_config.storage.mmap_file_config.max_size": {
"description": "The maximum size of a memory mapped file in bytes. Must be greater than growth_step.",
"privacy": "Public",
"value": 1099511627776
},
"batcher_config.storage.scope": {
"description": "The categories of data saved in storage.",
"privacy": "Public",
"value": "StateOnly"
},
"chain_id": {
"description": "The chain to follow.",
"privacy": "TemporaryValue",
"value": "SN_MAIN"
},
"compiler_config.max_bytecode_size": {
"description": "Limitation of contract bytecode size.",
Expand Down
1 change: 1 addition & 0 deletions crates/batcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ workspace = true
[dependencies]
async-trait.workspace = true
papyrus_config.workspace = true
papyrus_storage.workspace = true
serde.workspace = true
starknet_api.workspace = true
starknet_batcher_types.workspace = true
Expand Down
22 changes: 19 additions & 3 deletions crates/batcher/src/batcher.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::sync::Arc;

use async_trait::async_trait;
#[cfg(test)]
use mockall::automock;
use starknet_mempool_infra::component_runner::ComponentStarter;
use starknet_mempool_types::communication::SharedMempoolClient;

Expand All @@ -8,17 +12,29 @@ use crate::config::BatcherConfig;
pub struct Batcher {
pub config: BatcherConfig,
pub mempool_client: SharedMempoolClient,
pub storage: Arc<dyn BatcherStorageReaderTrait>,
}

impl Batcher {
pub fn new(config: BatcherConfig, mempool_client: SharedMempoolClient) -> Self {
Self { config, mempool_client }
pub fn new(
config: BatcherConfig,
mempool_client: SharedMempoolClient,
storage: Arc<dyn BatcherStorageReaderTrait>,
) -> Self {
Self { config, mempool_client, storage }
}
}

pub fn create_batcher(config: BatcherConfig, mempool_client: SharedMempoolClient) -> Batcher {
Batcher::new(config, mempool_client)
let (storage_reader, _storage_writer) = papyrus_storage::open_storage(config.storage.clone())
.expect("Failed to open batcher's storage");
Batcher::new(config, mempool_client, Arc::new(storage_reader))
}

#[async_trait]
impl ComponentStarter for Batcher {}

#[cfg_attr(test, automock)]
pub trait BatcherStorageReaderTrait: Send + Sync {}

impl BatcherStorageReaderTrait for papyrus_storage::StorageReader {}
26 changes: 16 additions & 10 deletions crates/batcher/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
use std::collections::BTreeMap;

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

/// The batcher related configuration.
/// TODO(Lev/Tsabary/Yael/Dafna): Define actual configuration.
#[derive(Clone, Debug, Serialize, Deserialize, Validate, PartialEq)]
pub struct BatcherConfig {
pub batcher_config_param_1: usize,
pub storage: papyrus_storage::StorageConfig,
}

impl SerializeConfig for BatcherConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::from_iter([ser_param(
"batcher_config_param_1",
&self.batcher_config_param_1,
"The first batcher configuration parameter",
ParamPrivacyInput::Public,
)])
append_sub_config_name(self.storage.dump(), "storage")
}
}

impl Default for BatcherConfig {
fn default() -> Self {
Self { batcher_config_param_1: 1 }
Self {
storage: papyrus_storage::StorageConfig {
db_config: papyrus_storage::db::DbConfig {
path_prefix: ".".into(),
// By default we don't want to create the DB if it doesn't exist.
enforce_file_exists: true,
..Default::default()
},
scope: papyrus_storage::StorageScope::StateOnly,
..Default::default()
},
}
}
}
1 change: 1 addition & 0 deletions crates/mempool_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ futures.workspace = true
papyrus_config.workspace = true
rstest.workspace = true
serde.workspace = true
starknet_api.workspace = true
starknet_batcher.workspace = true
starknet_batcher_types.workspace = true
starknet_consensus_manager.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/mempool_node/src/bin/mempool_dump_config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use papyrus_config::dumping::SerializeConfig;
use starknet_mempool_node::config::{MempoolNodeConfig, DEFAULT_CONFIG_PATH};
use starknet_mempool_node::config::{MempoolNodeConfig, CONFIG_POINTERS, DEFAULT_CONFIG_PATH};

/// Updates the default config file by:
/// cargo run --bin mempool_dump_config -q
fn main() {
MempoolNodeConfig::default()
.dump_to_file(&vec![], DEFAULT_CONFIG_PATH)
.dump_to_file(&CONFIG_POINTERS, DEFAULT_CONFIG_PATH)
.expect("dump to file error");
}
19 changes: 14 additions & 5 deletions crates/mempool_node/src/config/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::config::{
ComponentExecutionConfig,
LocationType,
MempoolNodeConfig,
CONFIG_POINTERS,
DEFAULT_CONFIG_PATH,
};

Expand Down Expand Up @@ -190,21 +191,29 @@ fn test_valid_components_config(
/// cargo run --bin mempool_dump_config -q
#[test]
fn default_config_file_is_up_to_date() {
let default_config = MempoolNodeConfig::default();
assert_matches!(default_config.validate(), Ok(()));
let from_code: serde_json::Value = serde_json::to_value(default_config.dump()).unwrap();

env::set_current_dir(get_absolute_path("")).expect("Couldn't set working dir.");
let from_default_config_file: serde_json::Value =
serde_json::from_reader(File::open(DEFAULT_CONFIG_PATH).unwrap()).unwrap();

let default_config = MempoolNodeConfig::default();
assert_matches!(default_config.validate(), Ok(()));

// Create a temporary file and dump the default config to it.
let mut tmp_file_path = env::temp_dir();
tmp_file_path.push("cfg.json");
default_config.dump_to_file(&CONFIG_POINTERS, tmp_file_path.to_str().unwrap()).unwrap();

// Read the dumped config from the file.
let from_code: serde_json::Value =
serde_json::from_reader(File::open(tmp_file_path).unwrap()).unwrap();

println!(
"{}",
"Default config file doesn't match the default NodeConfig implementation. Please update \
it using the mempool_dump_config binary."
.purple()
.bold()
);
println!("Diffs shown below.");
println!("Diffs shown below (default config file <<>> dump of MempoolNodeConfig::default()).");
assert_json_eq!(from_default_config_file, from_code)
}
12 changes: 12 additions & 0 deletions crates/mempool_node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ mod config_test;
use std::collections::BTreeMap;
use std::fs::File;
use std::path::Path;
use std::sync::LazyLock;

use clap::Command;
use papyrus_config::dumping::{
append_sub_config_name,
ser_optional_sub_config,
ser_param,
ser_pointer_target_param,
SerializeConfig,
};
use papyrus_config::loading::load_and_process_config;
use papyrus_config::{ConfigError, ParamPath, ParamPrivacyInput, SerializedParam};
use serde::{Deserialize, Serialize};
use starknet_api::core::ChainId;
use starknet_batcher::config::BatcherConfig;
use starknet_consensus_manager::config::ConsensusManagerConfig;
use starknet_gateway::config::{GatewayConfig, RpcStateReaderConfig};
Expand All @@ -31,6 +34,15 @@ use crate::version::VERSION_FULL;
// The path of the default configuration file, provided as part of the crate.
pub const DEFAULT_CONFIG_PATH: &str = "config/mempool/default_config.json";

// Configuration parameters that share the same value across multiple components.
type ConfigPointers = Vec<((ParamPath, SerializedParam), Vec<ParamPath>)>;
pub static CONFIG_POINTERS: LazyLock<ConfigPointers> = LazyLock::new(|| {
vec![(
ser_pointer_target_param("chain_id", &ChainId::Mainnet, "The chain to follow."),
vec!["batcher_config.storage.db_config.chain_id".to_owned()],
)]
});

// The configuration of the components.

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
Expand Down
Loading