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 b6948ba commit e744b52
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 93 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
},
"ecdsa_gas_cost": 0,
"memory_hole_gas_cost": 10,
"os_contract_addresses": {
Expand Down Expand Up @@ -620,4 +622,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
},
"ecdsa_gas_cost": 0,
"os_contract_addresses": {
"block_hash_contract_address": 1,
Expand Down Expand Up @@ -656,4 +658,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
},
"ecdsa_gas_cost": 0,
"memory_hole_gas_cost": 10,
"os_contract_addresses": {
Expand Down Expand Up @@ -656,4 +658,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
},
"ecdsa_gas_cost": 0,
"replace_class_gas_cost": {
"step_gas_cost": 50,
Expand Down Expand Up @@ -719,4 +721,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
},
"ecdsa_gas_cost": 0,
"replace_class_gas_cost": {
"step_gas_cost": 50,
Expand Down Expand Up @@ -719,4 +721,4 @@
10000
]
}
}
}
20 changes: 11 additions & 9 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
},
"ecdsa_gas_cost": 0,
"replace_class_gas_cost": {
"step_gas_cost": 50,
Expand Down Expand Up @@ -719,4 +721,4 @@
10000
]
}
}
}
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
},
"ecdsa_gas_cost": 10561,
"replace_class_gas_cost": {
"step_gas_cost": 104,
Expand Down Expand Up @@ -722,4 +724,4 @@
10000
]
}
}
}
12 changes: 6 additions & 6 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ fn prepare_program_extra_data(
// Create the builtin cost segment, the builtin order should be the same as the price builtin
// array in the os in compiled_class.cairo in load_compiled_class_facts.
let builtin_price_array = [
gas_costs.base.pedersen_gas_cost,
gas_costs.base.bitwise_builtin_gas_cost,
gas_costs.base.ecop_gas_cost,
gas_costs.base.poseidon_gas_cost,
gas_costs.base.add_mod_gas_cost,
gas_costs.base.mul_mod_gas_cost,
gas_costs.builtins.pedersen_gas_cost,
gas_costs.builtins.bitwise_builtin_gas_cost,
gas_costs.builtins.ecop_gas_cost,
gas_costs.builtins.poseidon_gas_cost,
gas_costs.builtins.add_mod_gas_cost,
gas_costs.builtins.mul_mod_gas_cost,
];

let data = builtin_price_array
Expand Down
86 changes: 62 additions & 24 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,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 @@ -671,22 +683,23 @@ pub struct BaseGasCosts {
#[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::ecdsa => self.base.ecdsa_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::ecdsa => self.builtins.ecdsa_gas_cost,
BuiltinName::segment_arena => return Err(GasCostsError::VirtualBuiltin),
BuiltinName::output => {
return Err(GasCostsError::UnsupportedBuiltinInCairo1 { builtin: *builtin });
Expand Down Expand Up @@ -715,7 +728,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 @@ -746,6 +759,7 @@ impl OsConstants {
"validate_rounding_consts",
"validated",
"syscall_gas_costs",
"builtin_gas_costs"
];
}

Expand All @@ -755,9 +769,11 @@ impl TryFrom<&OsConstantsRawJson> for GasCosts {
fn try_from(raw_json_data: &OsConstantsRawJson) -> Result<Self, Self::Error> {
let base_value: Value = serde_json::to_value(&raw_json_data.parse_base()?)?;
let base: BaseGasCosts = serde_json::from_value(base_value)?;
let syscalls_value: Value = serde_json::to_value(&raw_json_data.parse_syscalls(&base)?)?;
let builtins_value: Value = serde_json::to_value(&raw_json_data.parse_builtin()?)?;
let builtins: BuiltinGasCosts = serde_json::from_value(builtins_value)?;
let syscalls_value: Value = serde_json::to_value(&raw_json_data.parse_syscalls(&base, &builtins)?)?;
let syscalls: SyscallGasCosts = serde_json::from_value(syscalls_value)?;
Ok(GasCosts { base, syscalls })
Ok(GasCosts { base, builtins, syscalls })
}
}

Expand Down Expand Up @@ -830,14 +846,35 @@ impl OsConstantsRawJson {

fn parse_syscalls(
&self,
base: &BaseGasCosts,
base: &BaseGasCosts, builtins: &BuiltinGasCosts,
) -> Result<IndexMap<String, u64>, OsConstantsSerdeError> {
let mut gas_costs = IndexMap::new();
let syscalls: IndexMap<String, Value> = serde_json::from_value(
(self.raw_json_file_as_dict.get("syscall_gas_costs").unwrap()).clone(),
)?;
for (key, value) in syscalls {
self.add_to_syscalls(&key, &value, &mut gas_costs, base)?;
self.add_to_syscalls(&key, &value, &mut gas_costs, base, builtins)?;
}
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)
}
Expand Down Expand Up @@ -910,6 +947,7 @@ impl OsConstantsRawJson {
value: &Value,
syscalls: &mut IndexMap<String, u64>,
base: &BaseGasCosts,
builtins: &BuiltinGasCosts,
) -> Result<(), OsConstantsSerdeError> {
let mut cost = 0;
match value {
Expand All @@ -918,15 +956,15 @@ impl OsConstantsRawJson {
let inner_value = match inner_key.as_str() {
"step_gas_cost" => base.step_gas_cost,
"memory_hole_gas_cost" => base.memory_hole_gas_cost,
"range_check_gas_cost" => base.range_check_gas_cost,
"keccak_builtin_gas_cost" => base.keccak_builtin_gas_cost,
"pedersen_gas_cost" => base.pedersen_gas_cost,
"bitwise_builtin_gas_cost" => base.bitwise_builtin_gas_cost,
"ecop_gas_cost" => base.ecop_gas_cost,
"poseidon_gas_cost" => base.poseidon_gas_cost,
"add_mod_gas_cost" => base.add_mod_gas_cost,
"mul_mod_gas_cost" => base.mul_mod_gas_cost,
"ecdsa_gas_cost" => base.ecdsa_gas_cost,
"range_check_gas_cost" => builtins.range_check_gas_cost,
"keccak_builtin_gas_cost" => builtins.keccak_builtin_gas_cost,
"pedersen_gas_cost" => builtins.pedersen_gas_cost,
"bitwise_builtin_gas_cost" => builtins.bitwise_builtin_gas_cost,
"ecop_gas_cost" => builtins.ecop_gas_cost,
"poseidon_gas_cost" => builtins.poseidon_gas_cost,
"add_mod_gas_cost" => builtins.add_mod_gas_cost,
"mul_mod_gas_cost" => builtins.mul_mod_gas_cost,
"ecdsa_gas_cost" => builtins.ecdsa_gas_cost,
"default_initial_gas_cost" => base.default_initial_gas_cost,
"entry_point_initial_budget" => base.entry_point_initial_budget,
"syscall_base_gas_cost" => base.syscall_base_gas_cost,
Expand Down

0 comments on commit e744b52

Please sign in to comment.