From 181d62993e274fc5ec9fea986830146be2be2e84 Mon Sep 17 00:00:00 2001 From: Nimrod Weiss Date: Wed, 17 Jul 2024 09:47:48 +0300 Subject: [PATCH] build: make log level depend on input --- Cargo.lock | 12 ++++++ Cargo.toml | 1 + crates/committer/src/block_committer/input.rs | 15 +++++-- .../skeleton_forest_test.rs | 4 +- crates/committer_cli/Cargo.toml | 1 + crates/committer_cli/src/commands.rs | 4 +- crates/committer_cli/src/parse_input/cast.rs | 2 +- .../src/parse_input/raw_input.rs | 41 +++++++++++++++++-- .../src/parse_input/read_test.rs | 8 ++-- .../src/tests/flow_test_files_prefix | 2 +- 10 files changed, 75 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 48478384..f2e8e703 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -582,6 +582,7 @@ dependencies = [ "rand_distr", "serde", "serde_json", + "serde_repr", "simplelog", "starknet-types-core", "starknet_api", @@ -1964,6 +1965,17 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_repr" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.59", +] + [[package]] name = "sha2" version = "0.10.8" diff --git a/Cargo.toml b/Cargo.toml index 424370a7..6e8abd2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ rand_distr = "0.4.3" rstest = "0.17.0" serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.116" +serde_repr = "0.1.19" simplelog = "0.12.2" starknet-types-core = { version = "0.1.5", features = ["hash"] } starknet_api = "0.13.0-rc.0" diff --git a/crates/committer/src/block_committer/input.rs b/crates/committer/src/block_committer/input.rs index f25fb987..10f6f630 100644 --- a/crates/committer/src/block_committer/input.rs +++ b/crates/committer/src/block_committer/input.rs @@ -1,4 +1,4 @@ -use serde::Deserialize; +use log::LevelFilter; use crate::felt::Felt; use crate::hash::hash_trait::HashOutput; @@ -35,23 +35,32 @@ pub trait Config: Debug + Eq + PartialEq { /// If the configuration is set, it requires that the storage will contain the original data for /// the modified leaves. Otherwise, it is not required. fn warn_on_trivial_modifications(&self) -> bool; + + /// Indicates from which log level output should be printed out to console. + fn logger_level(&self) -> LevelFilter; } -#[derive(Deserialize, Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq)] pub struct ConfigImpl { warn_on_trivial_modifications: bool, + log_level: LevelFilter, } impl Config for ConfigImpl { fn warn_on_trivial_modifications(&self) -> bool { self.warn_on_trivial_modifications } + + fn logger_level(&self) -> LevelFilter { + self.log_level + } } impl ConfigImpl { - pub fn new(warn_on_trivial_modifications: bool) -> Self { + pub fn new(warn_on_trivial_modifications: bool, log_level: LevelFilter) -> Self { Self { warn_on_trivial_modifications, + log_level, } } } diff --git a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/skeleton_forest_test.rs b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/skeleton_forest_test.rs index 0392dcfc..3e9f019f 100644 --- a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/skeleton_forest_test.rs +++ b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/skeleton_forest_test.rs @@ -165,7 +165,7 @@ macro_rules! compare_skeleton_tree { }, contracts_trie_root_hash: HashOutput(Felt::from(861_u128 + 248_u128)), classes_trie_root_hash: HashOutput(Felt::from(155_u128 + 248_u128)), - config: ConfigImpl::new(true), + config: ConfigImpl::new(true, log::LevelFilter::Debug), }, OriginalSkeletonForest{ classes_trie: OriginalSkeletonTreeImpl { nodes: create_expected_skeleton_nodes( @@ -287,7 +287,7 @@ fn test_create_original_skeleton_forest( &input.state_diff.actual_storage_updates(), &input.state_diff.actual_classes_updates(), &forest_sorted_indices, - &ConfigImpl::new(false), + &ConfigImpl::new(false, log::LevelFilter::Debug), ) .unwrap(); let expected_original_contracts_trie_leaves = expected_original_contracts_trie_leaves diff --git a/crates/committer_cli/Cargo.toml b/crates/committer_cli/Cargo.toml index 7cfa6757..47da4b0e 100644 --- a/crates/committer_cli/Cargo.toml +++ b/crates/committer_cli/Cargo.toml @@ -25,6 +25,7 @@ rand.workspace = true rand_distr.workspace = true serde.workspace = true serde_json.workspace = true +serde_repr.workspace = true simplelog.workspace = true starknet-types-core.workspace = true starknet_api.workspace = true diff --git a/crates/committer_cli/src/commands.rs b/crates/committer_cli/src/commands.rs index d1edda07..93d9b643 100644 --- a/crates/committer_cli/src/commands.rs +++ b/crates/committer_cli/src/commands.rs @@ -1,6 +1,6 @@ use committer::block_committer::{ commit::commit_block, - input::{ConfigImpl, Input}, + input::{Config, ConfigImpl, Input}, }; use crate::{ @@ -10,6 +10,8 @@ use crate::{ pub async fn parse_and_commit(input_string: &str, output_path: String) { let input = parse_input(input_string).expect("Failed to parse the given input."); + // Set the given log level. + log::set_max_level(input.config.logger_level()); commit(input, output_path).await; } diff --git a/crates/committer_cli/src/parse_input/cast.rs b/crates/committer_cli/src/parse_input/cast.rs index 87f9c2d1..0afd0f60 100644 --- a/crates/committer_cli/src/parse_input/cast.rs +++ b/crates/committer_cli/src/parse_input/cast.rs @@ -89,7 +89,7 @@ impl TryFrom for InputImpl { classes_trie_root_hash: HashOutput(Felt::from_bytes_be_slice( &raw_input.classes_trie_root_hash, )), - config: raw_input.config, + config: raw_input.config.into(), }) } } diff --git a/crates/committer_cli/src/parse_input/raw_input.rs b/crates/committer_cli/src/parse_input/raw_input.rs index f873a6b0..d58dc863 100644 --- a/crates/committer_cli/src/parse_input/raw_input.rs +++ b/crates/committer_cli/src/parse_input/raw_input.rs @@ -1,6 +1,7 @@ use committer::block_committer::input::ConfigImpl; -use serde::Deserialize; - +use log::LevelFilter; +use serde::{Deserialize, Serialize}; +use serde_repr::Deserialize_repr; type RawFelt = [u8; 32]; #[derive(Deserialize, Debug)] @@ -11,7 +12,7 @@ pub(crate) struct RawInput { pub state_diff: RawStateDiff, pub contracts_trie_root_hash: RawFelt, pub classes_trie_root_hash: RawFelt, - pub config: ConfigImpl, + pub config: RawConfigImpl, } #[derive(Deserialize, Debug)] @@ -21,6 +22,40 @@ pub(crate) struct RawStorageEntry { pub value: Vec, } +#[derive(Deserialize, Debug)] +pub(crate) struct RawConfigImpl { + warn_on_trivial_modifications: bool, + log_level: PythonLogLevel, +} + +#[derive(Deserialize_repr, Debug, Default, Serialize)] +#[repr(usize)] +/// Describes a log level https://docs.python.org/3/library/logging.html#logging-levels +pub(crate) enum PythonLogLevel { + NotSet = 0, + Info = 20, + Warning = 30, + Error = 40, + Critical = 50, + // If an unknown variant is given, the default log level is Debug. + #[serde(other)] + #[default] + Debug = 10, +} + +impl From for ConfigImpl { + fn from(raw_config: RawConfigImpl) -> Self { + let log_level = match raw_config.log_level { + PythonLogLevel::NotSet => LevelFilter::Trace, + PythonLogLevel::Debug => LevelFilter::Debug, + PythonLogLevel::Info => LevelFilter::Info, + PythonLogLevel::Warning => LevelFilter::Warn, + PythonLogLevel::Error | PythonLogLevel::Critical => LevelFilter::Error, + }; + ConfigImpl::new(raw_config.warn_on_trivial_modifications, log_level) + } +} + #[derive(Deserialize, Debug)] pub(crate) struct RawFeltMapEntry { pub key: RawFelt, diff --git a/crates/committer_cli/src/parse_input/read_test.rs b/crates/committer_cli/src/parse_input/read_test.rs index 6290bc47..8cce55c4 100644 --- a/crates/committer_cli/src/parse_input/read_test.rs +++ b/crates/committer_cli/src/parse_input/read_test.rs @@ -80,7 +80,7 @@ fn test_simple_input_parsing() { ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], - {"warn_on_trivial_modifications": true} + {"warn_on_trivial_modifications": true, "log_level": 5} ] "#; @@ -205,7 +205,7 @@ fn test_simple_input_parsing() { }, contracts_trie_root_hash: expected_contracts_trie_root_hash, classes_trie_root_hash: expected_classes_trie_root_hash, - config: ConfigImpl::new(true), + config: ConfigImpl::new(true, log::LevelFilter::Debug), }; assert_eq!(parse_input(input).unwrap(), expected_input); } @@ -232,7 +232,7 @@ fn test_input_parsing_with_storage_key_duplicate() { ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 222, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 3], - {"warn_on_trivial_modifications": true} + {"warn_on_trivial_modifications": true, "log_level": 20} ] "#; @@ -274,7 +274,7 @@ fn test_input_parsing_with_mapping_key_duplicate() { ], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 144, 0, 0, 0, 0, 0, 0, 0, 0, 5], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 222, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 3], - {"warn_on_trivial_modifications": false} + {"warn_on_trivial_modifications": false, "log_level": 30} ] "#; diff --git a/crates/committer_cli/src/tests/flow_test_files_prefix b/crates/committer_cli/src/tests/flow_test_files_prefix index 07da9a8b..af417ba7 100644 --- a/crates/committer_cli/src/tests/flow_test_files_prefix +++ b/crates/committer_cli/src/tests/flow_test_files_prefix @@ -1 +1 @@ -6cf59e4 +23ffcf5