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 (#1832)
  • Loading branch information
ArniStarkware authored Nov 10, 2024
1 parent 9c61e2f commit 913e137
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 53 deletions.
5 changes: 5 additions & 0 deletions config/mempool/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@
"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",
Expand Down
109 changes: 57 additions & 52 deletions crates/batcher/src/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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 @@ -90,52 +95,6 @@ impl BlockBuilder {
}
}

impl Default for BlockBuilderConfig {
fn default() -> Self {
Self {
// TODO: update the default values once the actual values are known.
chain_info: ChainInfo::default(),
execute_config: TransactionExecutorConfig::default(),
bouncer_config: BouncerConfig::default(),
sequencer_address: ContractAddress::default(),
use_kzg_da: true,
tx_chunk_size: 100,
versioned_constants_overrides: VersionedConstantsOverrides::default(),
}
}
}

impl SerializeConfig for BlockBuilderConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
let mut dump = append_sub_config_name(self.chain_info.dump(), "chain_info");
dump.append(&mut append_sub_config_name(self.execute_config.dump(), "execute_config"));
dump.append(&mut append_sub_config_name(self.bouncer_config.dump(), "bouncer_config"));
dump.append(&mut BTreeMap::from([ser_param(
"sequencer_address",
&self.sequencer_address,
"The address of the sequencer.",
ParamPrivacyInput::Public,
)]));
dump.append(&mut BTreeMap::from([ser_param(
"use_kzg_da",
&self.use_kzg_da,
"Indicates whether the kzg mechanism is used for data availability.",
ParamPrivacyInput::Public,
)]));
dump.append(&mut BTreeMap::from([ser_param(
"tx_chunk_size",
&self.tx_chunk_size,
"The size of the transaction chunk.",
ParamPrivacyInput::Public,
)]));
dump.append(&mut append_sub_config_name(
self.versioned_constants_overrides.dump(),
"versioned_constants_overrides",
));
dump
}
}

#[async_trait]
impl BlockBuilderTrait for BlockBuilder {
async fn build_block(
Expand Down Expand Up @@ -232,8 +191,53 @@ 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>,
}

impl Default for BlockBuilderConfig {
fn default() -> Self {
Self {
// TODO: update the default values once the actual values are known.
chain_info: ChainInfo::default(),
execute_config: TransactionExecutorConfig::default(),
bouncer_config: BouncerConfig::default(),
sequencer_address: ContractAddress::default(),
use_kzg_da: true,
tx_chunk_size: 100,
versioned_constants_overrides: None,
}
}
}

impl SerializeConfig for BlockBuilderConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
let mut dump = append_sub_config_name(self.chain_info.dump(), "chain_info");
dump.append(&mut append_sub_config_name(self.execute_config.dump(), "execute_config"));
dump.append(&mut append_sub_config_name(self.bouncer_config.dump(), "bouncer_config"));
dump.append(&mut BTreeMap::from([ser_param(
"sequencer_address",
&self.sequencer_address,
"The address of the sequencer.",
ParamPrivacyInput::Public,
)]));
dump.append(&mut BTreeMap::from([ser_param(
"use_kzg_da",
&self.use_kzg_da,
"Indicates whether the kzg mechanism is used for data availability.",
ParamPrivacyInput::Public,
)]));
dump.append(&mut BTreeMap::from([ser_param(
"tx_chunk_size",
&self.tx_chunk_size,
"The size of the transaction chunk.",
ParamPrivacyInput::Public,
)]));
dump.append(&mut ser_optional_sub_config(
&self.versioned_constants_overrides,
"versioned_constants_overrides",
));
dump
}
}

pub struct BlockBuilderFactory {
Expand All @@ -260,12 +264,13 @@ impl BlockBuilderFactory {
},
use_kzg_da: block_builder_config.use_kzg_da,
};
let versioned_constants = VersionedConstants::latest_with_overrides(
block_builder_config.versioned_constants_overrides,
);
let block_context = BlockContext::new(
next_block_info,
block_builder_config.chain_info,
VersionedConstants::get_versioned_constants(
block_builder_config.versioned_constants_overrides,
),
versioned_constants,
block_builder_config.bouncer_config,
);

Expand Down
15 changes: 14 additions & 1 deletion crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,17 @@ impl VersionedConstants {
Self { vm_resource_fee_cost, archival_data_gas_costs, ..latest }
}

// TODO(Arni): Share code with latest_with_overrides.
pub fn latest_constants_with_overrides(
validate_max_n_steps: u32,
max_recursion_depth: usize,
) -> Self {
Self { validate_max_n_steps, max_recursion_depth, ..Self::latest_constants().clone() }
}

/// Returns the latest versioned constants after applying the given overrides.
// TODO(Arni): Consider replacing each call to this function with `latest_with_overrides`, and
// squashing the functions together.
/// Returns the latest versioned constants, applying the given overrides.
pub fn get_versioned_constants(
versioned_constants_overrides: VersionedConstantsOverrides,
) -> Self {
Expand All @@ -325,6 +328,16 @@ impl VersionedConstants {
}
}

/// Returns the latest versioned constants, applying the given overrides if provided.
pub fn latest_with_overrides(
versioned_constants_overrides: Option<VersionedConstantsOverrides>,
) -> Self {
match versioned_constants_overrides {
Some(overrides) => Self::get_versioned_constants(overrides),
None => Self::latest_constants().clone(),
}
}

pub fn get_archival_data_gas_costs(
&self,
mode: &GasVectorComputationMode,
Expand Down

0 comments on commit 913e137

Please sign in to comment.