Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilad Chase committed Jan 30, 2024
1 parent 1d9a08c commit 143333b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
14 changes: 7 additions & 7 deletions crates/blockifier/resources/versioned_constants.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"gateway": {
"max_calldata_length": 4000,
"max_contract_bytecode_size": 61440
},
"invoke_tx_max_n_steps": 3000000,
"max_recursion_depth": 50,
"os_resources": {
"execute_syscalls": {
"CallContract": {
Expand Down Expand Up @@ -249,7 +255,7 @@
}
}
},
"starknet_os_constants": {
"os_constants": {
"nop_entry_point_offset": -1,
"entry_point_type_external": 0,
"entry_point_type_l1_handler": 1,
Expand Down Expand Up @@ -393,12 +399,6 @@
"l1_gas_index": 0,
"l2_gas_index": 1
},
"gateway": {
"max_calldata_length": 4000,
"max_contract_bytecode_size": 61440
},
"invoke_tx_max_n_steps": 3000000,
"max_recursion_depth": 50,
"validate_max_n_steps": 1000000,
"vm_resource_fee_cost": {
"bitwise_builtin": 0.32,
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/execution/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl EntryPointExecutionContext {
.map(|call_info| call_info.vm_resources.n_steps)
.unwrap_or_default();

let overhead_steps = self.versioned_constants().resources_for_tx_type(tx_type).n_steps;
let overhead_steps = self.versioned_constants().os_resources_for_tx_type(tx_type).n_steps;
self.subtract_steps(validate_steps + overhead_steps)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/fee/gas_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn estimate_minimal_l1_gas(
tx: &AccountTransaction,
) -> TransactionPreValidationResult<GasAndBlobGasUsages> {
// TODO(Dori, 1/8/2023): Give names to the constant VM step estimates and regression-test them.
let os_steps_for_type = versioned_constants.resources_for_tx_type(&tx.tx_type()).n_steps;
let os_steps_for_type = versioned_constants.os_resources_for_tx_type(&tx.tx_type()).n_steps;
let gas_cost: usize = match tx {
// We consider the following state changes: sender balance update (storage update) + nonce
// increment (contract modification) (we exclude the sequencer balance update and the ERC20
Expand Down
37 changes: 18 additions & 19 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct VersionedConstants {
// Cairo OS constants.
// Note: if loaded from a json file, there are some assumptions made on its structure.
// See the struct's docstring for more details.
starknet_os_constants: Arc<StarknetOSConstants>,
os_constants: Arc<OSConstants>,
}

impl VersionedConstants {
Expand All @@ -61,7 +61,7 @@ impl VersionedConstants {

/// Returns the initial gas of any transaction to run with.
pub fn tx_initial_gas(&self) -> u64 {
let os_consts = &self.starknet_os_constants;
let os_consts = &self.os_constants;

os_consts.gas_costs["initial_gas_cost"] - os_consts.gas_costs["transaction_gas_cost"]
}
Expand All @@ -71,26 +71,26 @@ impl VersionedConstants {
}

pub fn gas_cost(&self, name: &str) -> u64 {
match self.starknet_os_constants.gas_costs.get(name) {
match self.os_constants.gas_costs.get(name) {
Some(&cost) => cost,
None if StarknetOSConstants::ALLOWED_GAS_COST_NAMES.contains(&name) => {
None if OSConstants::ALLOWED_GAS_COST_NAMES.contains(&name) => {
panic!(
"{} is present in `StarknetOSConstants::GAS_COSTS` but not in `self`; was \
validation skipped?",
"{} is present in `OSConstants::GAS_COSTS` but not in `self`; was validation \
skipped?",
name
)
}
None => {
panic!(
"Only gas costs listed in `StarknetOsConstants::GAS_COSTS` should be \
requested, got: {}",
"Only gas costs listed in `OsConstants::GAS_COSTS` should be requested, got: \
{}",
name
)
}
}
}

pub fn resources_for_tx_type(&self, tx_type: &TransactionType) -> &VmExecutionResources {
pub fn os_resources_for_tx_type(&self, tx_type: &TransactionType) -> &VmExecutionResources {
self.os_resources.resources_for_tx_type(tx_type)
}

Expand Down Expand Up @@ -130,7 +130,7 @@ impl TryFrom<&Path> for VersionedConstants {
}
}

#[derive(Debug, Deserialize, Clone, Default)]
#[derive(Clone, Debug, Default, Deserialize)]
// Serde trick for adding validations via a customr deserializer, without forgoing the derive.
// See: https://github.com/serde-rs/serde/issues/1220.
#[serde(remote = "Self")]
Expand Down Expand Up @@ -217,7 +217,6 @@ impl<'de> Deserialize<'de> for OsResources {
]);
let all_resources =
os_resources.execute_syscalls.values().chain(os_resources.execute_txs_inner.values());

for resources in all_resources {
for builtin_name in resources.builtin_instance_counter.keys() {
if !known_builtin_names.contains(builtin_name.as_str()) {
Expand All @@ -233,7 +232,7 @@ impl<'de> Deserialize<'de> for OsResources {
}

// Below, serde first deserializes the json into a regular IndexMap wrapped by the newtype
// `StarknetOSConstantsRawJSON`, then calls the `try_from` of the newtype, which handles the
// `OSConstantsRawJSON`, then calls the `try_from` of the newtype, which handles the
// conversion into actual values.
// Assumption: if the json has a value that contains the expression "FOO * 2", then the key `FOO`
// must appear before this value in the JSON.
Expand All @@ -245,12 +244,12 @@ impl<'de> Deserialize<'de> for OsResources {
// TODO: consider encoding the * and + operations inside the json file, instead of hardcoded below
// in the `try_from`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(try_from = "StarknetOSConstantsRawJSON")]
pub struct StarknetOSConstants {
#[serde(try_from = "OSConstantsRawJSON")]
pub struct OSConstants {
gas_costs: IndexMap<String, u64>,
}

impl StarknetOSConstants {
impl OSConstants {
// See documentation in core/os/constants.cairo.
const ALLOWED_GAS_COST_NAMES: [&'static str; 31] = [
"step_gas_cost",
Expand Down Expand Up @@ -309,10 +308,10 @@ impl StarknetOSConstants {
}
}

impl TryFrom<StarknetOSConstantsRawJSON> for StarknetOSConstants {
impl TryFrom<OSConstantsRawJSON> for OSConstants {
type Error = StarknetConstantsSerdeError;

fn try_from(intermediate: StarknetOSConstantsRawJSON) -> Result<Self, Self::Error> {
fn try_from(intermediate: OSConstantsRawJSON) -> Result<Self, Self::Error> {
let mut gas_costs = IndexMap::new();

let gas_cost_whitelist: IndexSet<_> =
Expand Down Expand Up @@ -356,7 +355,7 @@ impl TryFrom<StarknetOSConstantsRawJSON> for StarknetOSConstants {
}
}

let os_constants = StarknetOSConstants { gas_costs };
let os_constants = OSConstants { gas_costs };

// Skip validation in testing: to test validation run validate manually.
#[cfg(not(any(feature = "testing", test)))]
Expand All @@ -369,7 +368,7 @@ impl TryFrom<StarknetOSConstantsRawJSON> for StarknetOSConstants {
// Intermediate representation of the JSON file in order to make the deserialization easier, using a
// regular the try_from.
#[derive(Debug, Deserialize)]
struct StarknetOSConstantsRawJSON {
struct OSConstantsRawJSON {
#[serde(flatten)]
regular: IndexMap<String, Value>,
}
Expand Down
9 changes: 5 additions & 4 deletions crates/blockifier/src/versioned_constants_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ fn test_successful_parsing() {
"ignore the gas string": "GAS!",
"I look like a gas cost but my name is all wrong": 0
}"#;
let starknet_os_constants: Arc<StarknetOSConstants> = serde_json::from_str(json_data).unwrap();
let versioned_constants = VersionedConstants { starknet_os_constants, ..Default::default() };
let os_constants: Arc<OSConstants> = serde_json::from_str(json_data).unwrap();
let versioned_constants =
VersionedConstants { os_constants, ..Default::default() };

assert_eq!(versioned_constants.gas_cost("step_gas_cost"), 2);
assert_eq!(versioned_constants.gas_cost("entry_point_initial_budget"), 6); // step_gas_cost * 3.
Expand All @@ -29,7 +30,7 @@ fn test_successful_parsing() {
assert_eq!(versioned_constants.gas_cost("entry_point_gas_cost"), 34);

// Only the 3 values asserted against should be present, the rest are ignored.
assert_eq!(versioned_constants.starknet_os_constants.gas_costs.len(), 3);
assert_eq!(versioned_constants.os_constants.gas_costs.len(), 3);
}

#[test]
Expand All @@ -50,7 +51,7 @@ fn test_string_inside_composed_field() {
}

fn check_constants_serde_error(json_data: &str, expected_error_message: &str) {
let error = serde_json::from_str::<StarknetOSConstants>(json_data).unwrap_err();
let error = serde_json::from_str::<OSConstants>(json_data).unwrap_err();
assert_eq!(error.to_string(), expected_error_message);
}

Expand Down

0 comments on commit 143333b

Please sign in to comment.