Skip to content

Commit

Permalink
refactor(batcher): make versioned_constants_overrides field optional …
Browse files Browse the repository at this point in the history
…in BlockBuilderConfig
  • Loading branch information
ayeletstarkware committed Oct 21, 2024
1 parent 6b8ae3a commit b50728a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 38 deletions.
11 changes: 8 additions & 3 deletions config/mempool/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,25 @@
"privacy": "Public",
"value": true
},
"batcher_config.block_builder_config.versioned_constants_overrides.#is_none": {
"description": "Flag for an optional field.",
"privacy": "TemporaryValue",
"value": true
},
"batcher_config.block_builder_config.versioned_constants_overrides.invoke_tx_max_n_steps": {
"description": "Maximum number of steps the invoke function is allowed to run.",
"privacy": "Public",
"value": 10000000
"value": 0
},
"batcher_config.block_builder_config.versioned_constants_overrides.max_recursion_depth": {
"description": "Maximum recursion depth for nested calls during blockifier validation.",
"privacy": "Public",
"value": 50
"value": 0
},
"batcher_config.block_builder_config.versioned_constants_overrides.validate_max_n_steps": {
"description": "Maximum number of steps the validation function is allowed to run.",
"privacy": "Public",
"value": 1000000
"value": 0
},
"batcher_config.outstream_content_buffer_size": {
"description": "Maximum items to add to the outstream buffer before blocking further filling of the stream.",
Expand Down
16 changes: 10 additions & 6 deletions crates/batcher/src/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ use blockifier::versioned_constants::{VersionedConstants, VersionedConstantsOver
use indexmap::IndexMap;
#[cfg(test)]
use mockall::automock;
use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig};
use papyrus_config::dumping::{
append_sub_config_name,
ser_optional_sub_config,
ser_param,
SerializeConfig,
};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use papyrus_storage::StorageReader;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -103,7 +108,7 @@ impl Default for BlockBuilderConfig {
sequencer_address: ContractAddress::default(),
use_kzg_da: true,
tx_chunk_size: 100,
versioned_constants_overrides: VersionedConstantsOverrides::default(),
versioned_constants_overrides: None,
}
}
}
Expand Down Expand Up @@ -131,8 +136,8 @@ impl SerializeConfig for BlockBuilderConfig {
"The size of the transaction chunk.",
ParamPrivacyInput::Public,
)]));
dump.append(&mut append_sub_config_name(
self.versioned_constants_overrides.dump(),
dump.append(&mut ser_optional_sub_config(
&self.versioned_constants_overrides,
"versioned_constants_overrides",
));
dump
Expand Down Expand Up @@ -235,8 +240,7 @@ pub struct BlockBuilderConfig {
pub sequencer_address: ContractAddress,
pub use_kzg_da: bool,
pub tx_chunk_size: usize,
// TODO(Ayelet): Make this field optional.
pub versioned_constants_overrides: VersionedConstantsOverrides,
pub versioned_constants_overrides: Option<VersionedConstantsOverrides>,
}

pub struct BlockBuilderFactory {
Expand Down
40 changes: 17 additions & 23 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,22 @@ impl VersionedConstants {

/// Returns the latest versioned constants after applying the given overrides.
pub fn get_versioned_constants(
versioned_constants_overrides: VersionedConstantsOverrides,
versioned_constants_overrides: Option<VersionedConstantsOverrides>,
) -> Self {
let VersionedConstantsOverrides {
validate_max_n_steps,
max_recursion_depth,
invoke_tx_max_n_steps,
} = versioned_constants_overrides;
Self {
validate_max_n_steps,
max_recursion_depth,
invoke_tx_max_n_steps,
..Self::latest_constants().clone()
if let Some(overrides) = versioned_constants_overrides {
let VersionedConstantsOverrides {
validate_max_n_steps,
max_recursion_depth,
invoke_tx_max_n_steps,
} = overrides;
Self {
validate_max_n_steps,
max_recursion_depth,
invoke_tx_max_n_steps,
..Self::latest_constants().clone()
}
} else {
Self::latest_constants().clone()
}
}

Expand Down Expand Up @@ -824,24 +828,14 @@ pub struct ResourcesByVersion {
pub deprecated_resources: ResourcesParams,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
// TODO(Ayelet): Make fields optional.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct VersionedConstantsOverrides {
pub validate_max_n_steps: u32,
pub max_recursion_depth: usize,
pub invoke_tx_max_n_steps: u32,
}

impl Default for VersionedConstantsOverrides {
// TODO: update the default values once the actual values are known.
fn default() -> Self {
Self {
validate_max_n_steps: 1000000,
max_recursion_depth: 50,
invoke_tx_max_n_steps: 10000000,
}
}
}

impl SerializeConfig for VersionedConstantsOverrides {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::from_iter([
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/versioned_constants_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ fn test_versioned_constants_overrides() {
let updated_max_recursion_depth = versioned_constants.max_recursion_depth + 1;

// Create a versioned constants copy with overriden values.
let result = VersionedConstants::get_versioned_constants(VersionedConstantsOverrides {
let result = VersionedConstants::get_versioned_constants(Some(VersionedConstantsOverrides {
validate_max_n_steps: updated_validate_max_n_steps,
max_recursion_depth: updated_max_recursion_depth,
invoke_tx_max_n_steps: updated_invoke_tx_max_n_steps,
});
}));

// Assert the new values are used.
assert_eq!(result.invoke_tx_max_n_steps, updated_invoke_tx_max_n_steps);
Expand Down
5 changes: 3 additions & 2 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ impl PyBlockExecutor {
log::debug!("Initializing Block Executor...");
let storage =
PapyrusStorage::new(target_storage_config).expect("Failed to initialize storage.");
let versioned_constants =
VersionedConstants::get_versioned_constants(py_versioned_constants_overrides.into());
let versioned_constants = VersionedConstants::get_versioned_constants(Some(
py_versioned_constants_overrides.into(),
));
log::debug!("Initialized Block Executor.");

Self {
Expand Down
5 changes: 3 additions & 2 deletions crates/native_blockifier/src/py_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ impl PyValidator {
let state = CachedState::new(state_reader);

// Create the block context.
let versioned_constants =
VersionedConstants::get_versioned_constants(py_versioned_constants_overrides.into());
let versioned_constants = VersionedConstants::get_versioned_constants(Some(
py_versioned_constants_overrides.into(),
));
let block_context = BlockContext::new(
next_block_info.try_into().expect("Failed to convert block info."),
os_config.into_chain_info(),
Expand Down

0 comments on commit b50728a

Please sign in to comment.