Skip to content

Commit

Permalink
feat: create dump_config for mempool node config
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed May 22, 2024
1 parent 0a3b6c1 commit 0385976
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 7 deletions.
61 changes: 61 additions & 0 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"crates/mempool_node",
"crates/mempool_infra",
"crates/task_executor",
"crates/test_utils",
]
resolver = "2"

Expand All @@ -27,16 +28,19 @@ as_conversions = "deny"

[workspace.dependencies]
anyhow = "1.0"
assert-json-diff = "2.0.2"
assert_matches = "1.5.0"
async-trait = "0.1.79"
axum = "0.6.12"
# TODO(YaelD, 1/5/2024): Use a fixed version once the StarkNet API is stable.
blockifier = { git = "https://github.com/starkware-libs/blockifier.git", rev = "fc62b8b8", features = ["testing"] }
cairo-lang-starknet-classes = "2.6.0"
clap = "4.3.10"
derive_more = "0.99"
colored = "2.1.0"
const_format = "0.2.30"
derive_more = "0.99"
hyper = { version = "0.14", features = ["client", "http1"] }
insta = "1.29.0"
papyrus_config = "0.3.0"
pretty_assertions = "1.4.0"
reqwest = { version = "0.11", features = ["blocking", "json"] }
Expand Down
26 changes: 23 additions & 3 deletions config/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,34 @@
"privacy": "Public",
"value": true
},
"gateway_config.ip": {
"gateway_config.network_config.ip": {
"description": "The gateway server ip.",
"privacy": "Public",
"value": "0.0.0.0"
},
"gateway_config.port": {
"gateway_config.network_config.port": {
"description": "The gateway server port.",
"privacy": "Public",
"value": 8080
},
"gateway_config.stateless_transaction_validator_config.max_calldata_length": {
"description": "Validates that a transaction has signature length less than or equal to this value.",
"privacy": "Public",
"value": 0
},
"gateway_config.stateless_transaction_validator_config.max_signature_length": {
"description": "Validates that a transaction has calldata length less than or equal to this value.",
"privacy": "Public",
"value": 0
},
"gateway_config.stateless_transaction_validator_config.validate_non_zero_l1_gas_fee": {
"description": "If true, validates that a transaction has non-zero L1 resource bounds.",
"privacy": "Public",
"value": false
},
"gateway_config.stateless_transaction_validator_config.validate_non_zero_l2_gas_fee": {
"description": "If true, validates that a transaction has non-zero L2 resource bounds.",
"privacy": "Public",
"value": false
}
}
}
4 changes: 4 additions & 0 deletions crates/mempool_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ tokio.workspace = true
validator.workspace = true

[dev-dependencies]
assert-json-diff.workspace = true
assert_matches.workspace = true
colored.workspace = true
insta = { workspace = true, features = ["json"] }
pretty_assertions.workspace = true
test_utils = {path = "../test_utils"}
47 changes: 46 additions & 1 deletion crates/mempool_node/src/config/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ use std::fs::File;
use std::ops::IndexMut;
use std::path::{Path, PathBuf};

use assert_json_diff::assert_json_eq;
use assert_matches::assert_matches;
use colored::Colorize;
use papyrus_config::dumping::SerializeConfig;
use papyrus_config::loading::load_and_process_config;
use papyrus_config::presentation::get_config_presentation;
use papyrus_config::validators::ParsedValidationErrors;
use papyrus_config::{SerializationType, SerializedContent, SerializedParam};
use starknet_gateway::config::{GatewayNetworkConfig, StatelessTransactionValidatorConfig};
use test_utils::get_absolute_path;
use validator::Validate;

use crate::config::{
node_command, ComponentConfig, ComponentExecutionConfig, GatewayConfig, MempoolNodeConfig,
DEFAULT_CONFIG_PATH,
};

const TEST_FILES_FOLDER: &str = "./src/test_files";
const TEST_FILES_FOLDER: &str = "crates/mempool_node/src/test_files";
const CONFIG_FILE: &str = "mempool_node_config.json";

fn get_config_file(file_name: &str) -> Result<MempoolNodeConfig, papyrus_config::ConfigError> {
Expand All @@ -27,6 +31,8 @@ fn get_config_file(file_name: &str) -> Result<MempoolNodeConfig, papyrus_config:

#[test]
fn test_valid_config() {
env::set_current_dir(get_absolute_path("")).expect("Couldn't set working dir.");

// Read the valid config file and validate its content.
let expected_config = MempoolNodeConfig {
components: ComponentConfig {
Expand All @@ -51,6 +57,8 @@ fn test_valid_config() {

#[test]
fn test_components_config() {
env::set_current_dir(get_absolute_path("")).expect("Couldn't set working dir.");

// Read the valid config file and check that the validator finds no errors.
let mut config = get_config_file(CONFIG_FILE).unwrap();
assert!(config.validate().is_ok());
Expand All @@ -74,3 +82,40 @@ fn test_components_config() {
config.components.mempool_component.execute = true;
assert!(config.validate().is_ok());
}

#[test]
fn test_dump_default_config() {
env::set_current_dir(get_absolute_path("")).expect("Couldn't set working dir.");

let default_config = MempoolNodeConfig::default();
let dumped_default_config = default_config.dump();
insta::assert_json_snapshot!(dumped_default_config);

assert!(default_config.validate().is_ok());
}

#[test]
fn default_config_file_is_up_to_date() {
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();

// 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");
MempoolNodeConfig::default().dump_to_file(&vec![], 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 dump_config binary."
.purple()
.bold()
);
println!("Diffs shown below.");
assert_json_eq!(from_default_config_file, from_code)
}
4 changes: 2 additions & 2 deletions crates/mempool_node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Default for ComponentExecutionConfig {
}

/// The components configuration.
#[derive(Clone, Debug, Serialize, Deserialize, Validate, PartialEq, Default)]
#[derive(Clone, Debug, Default, Serialize, Deserialize, Validate, PartialEq)]
#[validate(schema(function = "validate_components_config"))]
pub struct ComponentConfig {
pub gateway_component: ComponentExecutionConfig,
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn validate_components_config(components: &ComponentConfig) -> Result<(), Va
}

/// The configurations of the various components of the node.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Validate, Default)]
#[derive(Debug, Deserialize, Default, Serialize, Clone, PartialEq, Validate)]
pub struct MempoolNodeConfig {
#[validate]
pub components: ComponentConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
source: crates/mempool_node/src/config/config_test.rs
expression: dumped_default_config
---
{
"components.gateway_component.execute": {
"description": "The component execution flag.",
"value": true,
"privacy": "Public"
},
"components.mempool_component.execute": {
"description": "The component execution flag.",
"value": true,
"privacy": "Public"
},
"gateway_config.network_config.ip": {
"description": "The gateway server ip.",
"value": "0.0.0.0",
"privacy": "Public"
},
"gateway_config.network_config.port": {
"description": "The gateway server port.",
"value": {
"$serde_json::private::Number": "8080"
},
"privacy": "Public"
},
"gateway_config.stateless_transaction_validator_config.max_calldata_length": {
"description": "Validates that a transaction has signature length less than or equal to this value.",
"value": {
"$serde_json::private::Number": "0"
},
"privacy": "Public"
},
"gateway_config.stateless_transaction_validator_config.max_signature_length": {
"description": "Validates that a transaction has calldata length less than or equal to this value.",
"value": {
"$serde_json::private::Number": "0"
},
"privacy": "Public"
},
"gateway_config.stateless_transaction_validator_config.validate_non_zero_l1_gas_fee": {
"description": "If true, validates that a transaction has non-zero L1 resource bounds.",
"value": false,
"privacy": "Public"
},
"gateway_config.stateless_transaction_validator_config.validate_non_zero_l2_gas_fee": {
"description": "If true, validates that a transaction has non-zero L2 resource bounds.",
"value": false,
"privacy": "Public"
}
}
12 changes: 12 additions & 0 deletions crates/test_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "test_utils"
version.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true

[features]

[dependencies]


7 changes: 7 additions & 0 deletions crates/test_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::env;
use std::path::{Path, PathBuf};

/// Returns the absolute path from the project root.
pub fn get_absolute_path(relative_path: &str) -> PathBuf {
Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("../..").join(relative_path)
}

0 comments on commit 0385976

Please sign in to comment.