Skip to content

Commit

Permalink
chore(blockifier): split builtin gas costs from base gas costs
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonatan-Starkware committed Dec 8, 2024
1 parent cc35539 commit 593d9bc
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 81 deletions.
20 changes: 11 additions & 9 deletions crates/blockifier/resources/versioned_constants_0_13_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@
"default_entry_point_selector": 0,
"stored_block_hash_buffer": 10,
"step_gas_cost": 100,
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 0,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0,
"builtin_gas_costs": {
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 0,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0
},
"memory_hole_gas_cost": 10,
"os_contract_addresses": {
"block_hash_contract_address": 1,
Expand Down Expand Up @@ -619,4 +621,4 @@
1000
]
}
}
}
20 changes: 11 additions & 9 deletions crates/blockifier/resources/versioned_constants_0_13_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@
"default_entry_point_selector": 0,
"stored_block_hash_buffer": 10,
"step_gas_cost": 100,
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 0,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0,
"builtin_gas_costs": {
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 0,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0
},
"os_contract_addresses": {
"block_hash_contract_address": 1,
"alias_contract_address": 2,
Expand Down Expand Up @@ -655,4 +657,4 @@
10000
]
}
}
}
20 changes: 11 additions & 9 deletions crates/blockifier/resources/versioned_constants_0_13_1_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@
"default_entry_point_selector": 0,
"stored_block_hash_buffer": 10,
"step_gas_cost": 100,
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 0,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0,
"builtin_gas_costs": {
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 0,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0
},
"memory_hole_gas_cost": 10,
"os_contract_addresses": {
"block_hash_contract_address": 1,
Expand Down Expand Up @@ -655,4 +657,4 @@
10000
]
}
}
}
20 changes: 11 additions & 9 deletions crates/blockifier/resources/versioned_constants_0_13_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@
"alias_contract_address": 2,
"reserved_contract_address": 3
},
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 594,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0,
"builtin_gas_costs": {
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 594,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0
},
"sierra_array_len_bound": 4294967296,
"step_gas_cost": 100,
"stored_block_hash_buffer": 10,
Expand Down Expand Up @@ -662,4 +664,4 @@
10000
]
}
}
}
20 changes: 11 additions & 9 deletions crates/blockifier/resources/versioned_constants_0_13_2_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@
"alias_contract_address": 2,
"reserved_contract_address": 3
},
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 594,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0,
"builtin_gas_costs": {
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 594,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0
},
"sierra_array_len_bound": 4294967296,
"step_gas_cost": 100,
"stored_block_hash_buffer": 10,
Expand Down Expand Up @@ -662,4 +664,4 @@
10000
]
}
}
}
18 changes: 10 additions & 8 deletions crates/blockifier/resources/versioned_constants_0_13_3.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@
"alias_contract_address": 2,
"reserved_contract_address": 3
},
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 594,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0,
"builtin_gas_costs": {
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 0,
"pedersen_gas_cost": 0,
"bitwise_builtin_gas_cost": 594,
"ecop_gas_cost": 0,
"poseidon_gas_cost": 0,
"add_mod_gas_cost": 0,
"mul_mod_gas_cost": 0
},
"sierra_array_len_bound": 4294967296,
"step_gas_cost": 100,
"stored_block_hash_buffer": 10,
Expand Down
20 changes: 11 additions & 9 deletions crates/blockifier/resources/versioned_constants_0_13_4.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,16 @@
"alias_contract_address": 2,
"reserved_contract_address": 3
},
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 136189,
"pedersen_gas_cost": 4050,
"bitwise_builtin_gas_cost": 583,
"ecop_gas_cost": 4085,
"poseidon_gas_cost": 491,
"add_mod_gas_cost": 230,
"mul_mod_gas_cost": 604,
"builtin_gas_costs": {
"range_check_gas_cost": 70,
"keccak_builtin_gas_cost": 136189,
"pedersen_gas_cost": 4050,
"bitwise_builtin_gas_cost": 583,
"ecop_gas_cost": 4085,
"poseidon_gas_cost": 491,
"add_mod_gas_cost": 230,
"mul_mod_gas_cost": 604
},
"sierra_array_len_bound": 4294967296,
"step_gas_cost": 100,
"stored_block_hash_buffer": 10,
Expand Down Expand Up @@ -665,4 +667,4 @@
10000
]
}
}
}
65 changes: 46 additions & 19 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,18 @@ impl SyscallGasCosts {
pub struct BaseGasCosts {
pub step_gas_cost: u64,
pub memory_hole_gas_cost: u64,
// An estimation of the initial gas for a transaction to run with. This solution is
// temporary and this value will be deduced from the transaction's fields.
pub default_initial_gas_cost: u64,
// Compiler gas costs.
pub entry_point_initial_budget: u64,
pub syscall_base_gas_cost: u64,
// OS gas costs.
pub transaction_gas_cost: u64,
}

#[derive(Debug, Default, Deserialize)]
pub struct BuiltinGasCosts {
// Range check has a hard-coded cost higher than its proof percentage to avoid the overhead of
// retrieving its price from the table.
pub range_check_gas_cost: u64,
Expand All @@ -655,35 +667,28 @@ pub struct BaseGasCosts {
pub poseidon_gas_cost: u64,
pub add_mod_gas_cost: u64,
pub mul_mod_gas_cost: u64,
// An estimation of the initial gas for a transaction to run with. This solution is
// temporary and this value will be deduced from the transaction's fields.
pub default_initial_gas_cost: u64,
// Compiler gas costs.
pub entry_point_initial_budget: u64,
pub syscall_base_gas_cost: u64,
// OS gas costs.
pub transaction_gas_cost: u64,
}

/// Gas cost constants. For more documentation see in core/os/constants.cairo.
#[derive(Debug, Default, Deserialize)]
pub struct GasCosts {
pub base: BaseGasCosts,
pub builtins: BuiltinGasCosts,
pub syscalls: SyscallGasCosts,
}

impl GasCosts {
pub fn get_builtin_gas_cost(&self, builtin: &BuiltinName) -> Result<u64, GasCostsError> {
let gas_cost = match *builtin {
BuiltinName::range_check => self.base.range_check_gas_cost,
BuiltinName::pedersen => self.base.pedersen_gas_cost,
BuiltinName::bitwise => self.base.bitwise_builtin_gas_cost,
BuiltinName::ec_op => self.base.ecop_gas_cost,
BuiltinName::keccak => self.base.keccak_builtin_gas_cost,
BuiltinName::poseidon => self.base.poseidon_gas_cost,
BuiltinName::range_check96 => self.base.range_check_gas_cost,
BuiltinName::add_mod => self.base.add_mod_gas_cost,
BuiltinName::mul_mod => self.base.mul_mod_gas_cost,
BuiltinName::range_check => self.builtins.range_check_gas_cost,
BuiltinName::pedersen => self.builtins.pedersen_gas_cost,
BuiltinName::bitwise => self.builtins.bitwise_builtin_gas_cost,
BuiltinName::ec_op => self.builtins.ecop_gas_cost,
BuiltinName::keccak => self.builtins.keccak_builtin_gas_cost,
BuiltinName::poseidon => self.builtins.poseidon_gas_cost,
BuiltinName::range_check96 => self.builtins.range_check_gas_cost,
BuiltinName::add_mod => self.builtins.add_mod_gas_cost,
BuiltinName::mul_mod => self.builtins.mul_mod_gas_cost,
BuiltinName::segment_arena => return Err(GasCostsError::VirtualBuiltin),
BuiltinName::output | BuiltinName::ecdsa => {
return Err(GasCostsError::UnsupportedBuiltinInCairo1 { builtin: *builtin });
Expand Down Expand Up @@ -712,7 +717,7 @@ impl OsConstants {
// not used by the blockifier but included for transparency. These constanst will be ignored
// during the creation of the struct containing the gas costs.

const ADDITIONAL_FIELDS: [&'static str; 30] = [
const ADDITIONAL_FIELDS: [&'static str; 31] = [
"block_hash_contract_address",
"constructor_entry_point_selector",
"default_entry_point_selector",
Expand Down Expand Up @@ -743,6 +748,7 @@ impl OsConstants {
"validate_rounding_consts",
"validated",
"syscall_gas_costs",
"builtin_gas_costs"
];
}

Expand All @@ -754,7 +760,7 @@ impl TryFrom<&OsConstantsRawJson> for GasCosts {
let base: BaseGasCosts = serde_json::from_value(base_value)?;
let syscalls_value: Value = serde_json::to_value(&raw_json_data.parse_syscalls(&base)?)?;
let syscalls: SyscallGasCosts = serde_json::from_value(syscalls_value)?;
Ok(GasCosts { base, syscalls })
Ok(GasCosts { base, builtins, syscalls })
}
}

Expand Down Expand Up @@ -839,6 +845,27 @@ impl OsConstantsRawJson {
Ok(gas_costs)
}

fn parse_builtin(&self) -> Result<IndexMap<String, u64>, OsConstantsSerdeError> {
let mut gas_costs = IndexMap::new();
let builtins: IndexMap<String, Value> = serde_json::from_value(
(self.raw_json_file_as_dict.get("builtin_gas_costs").unwrap()).clone(),
)?;
for (key, value) in builtins {
let mut cost = 0;
match value{
Value::Number(n) => {
cost = n.as_u64().ok_or_else(|| OsConstantsSerdeError::OutOfRange {
key: key.to_string(),
value: n.clone(),
})?;
gas_costs.insert(key.to_string(), cost);
}
_ => return Err(OsConstantsSerdeError::UnhandledValueType(value.clone())),
}
}
Ok(gas_costs)
}

/// Recursively adds a key to gas costs, calculating its value after processing any nested keys.
// Invariant: there is no circular dependency between key definitions.
fn recursive_add_to_base(
Expand Down

0 comments on commit 593d9bc

Please sign in to comment.