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

build: make log level depend on input #313

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

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 12 additions & 3 deletions crates/committer/src/block_committer/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::Deserialize;
use log::LevelFilter;

use crate::felt::Felt;
use crate::hash::hash_trait::HashOutput;
Expand Down Expand Up @@ -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,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/committer_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion crates/committer_cli/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use committer::block_committer::{
commit::commit_block,
input::{ConfigImpl, Input},
input::{Config, ConfigImpl, Input},
};

use crate::{
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/committer_cli/src/parse_input/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl TryFrom<RawInput> 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(),
})
}
}
Expand Down
41 changes: 38 additions & 3 deletions crates/committer_cli/src/parse_input/raw_input.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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)]
Expand All @@ -21,6 +22,40 @@ pub(crate) struct RawStorageEntry {
pub value: Vec<u8>,
}

#[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<RawConfigImpl> 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,
Expand Down
8 changes: 4 additions & 4 deletions crates/committer_cli/src/parse_input/read_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
]

"#;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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}
]

"#;
Expand Down Expand Up @@ -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}
]

"#;
Expand Down
2 changes: 1 addition & 1 deletion crates/committer_cli/src/tests/flow_test_files_prefix
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6cf59e4
23ffcf5
Loading