Skip to content

Commit

Permalink
chore(blockifier): add gas cost to bultin price array
Browse files Browse the repository at this point in the history
  • Loading branch information
meship-starkware committed Aug 22, 2024
1 parent aad3541 commit 5a2ada1
Show file tree
Hide file tree
Showing 10 changed files with 479 additions and 393 deletions.
4 changes: 4 additions & 0 deletions crates/blockifier/resources/versioned_constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
"range_check_gas_cost": 70,
"pedersen_gas_cost": 4130,
"bitwise_builtin_gas_cost": 594,
"ecop_gas_cost": 256,
"poseidon_gas_cost": 500,
"add_mod_gas_cost": 234,
"mul_mod_gas_cost": 616,
"replace_class_gas_cost": {
"step_gas_cost": 100,
"range_check_gas_cost": 1
Expand Down
6 changes: 6 additions & 0 deletions crates/blockifier/resources/versioned_constants_13_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"stored_block_hash_buffer": 10,
"step_gas_cost": 100,
"range_check_gas_cost": 70,
"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,
"initial_gas_cost": {
"step_gas_cost": 100000000
Expand Down
6 changes: 6 additions & 0 deletions crates/blockifier/resources/versioned_constants_13_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
"stored_block_hash_buffer": 10,
"step_gas_cost": 100,
"range_check_gas_cost": 70,
"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,
"initial_gas_cost": {
"step_gas_cost": 100000000
Expand Down
6 changes: 6 additions & 0 deletions crates/blockifier/resources/versioned_constants_13_1_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
"stored_block_hash_buffer": 10,
"step_gas_cost": 100,
"range_check_gas_cost": 70,
"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,
"initial_gas_cost": {
"step_gas_cost": 100000000
Expand Down
5 changes: 5 additions & 0 deletions crates/blockifier/resources/versioned_constants_13_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@
"memory_hole_gas_cost": 10,
"nop_entry_point_offset": -1,
"range_check_gas_cost": 70,
"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,
"replace_class_gas_cost": {
"step_gas_cost": 50,
"syscall_base_gas_cost": 1
Expand Down
32 changes: 23 additions & 9 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::execution::execution_utils::{
};
use crate::execution::syscalls::hint_processor::SyscallHintProcessor;
use crate::state::state_api::State;
use crate::versioned_constants::GasCosts;

// TODO(spapini): Try to refactor this file into a StarknetRunner struct.

Expand Down Expand Up @@ -165,8 +166,12 @@ pub fn initialize_execution_context<'a>(

runner.initialize_function_runner_cairo_1(&entry_point.builtins)?;
let mut read_only_segments = ReadOnlySegments::default();
let program_extra_data_length =
prepare_program_extra_data(&mut runner, contract_class, &mut read_only_segments)?;
let program_extra_data_length = prepare_program_extra_data(
&mut runner,
contract_class,
&mut read_only_segments,
&context.versioned_constants().os_constants.gas_costs,
)?;

// Instantiate syscall handler.
let initial_syscall_ptr = runner.vm.add_memory_segment();
Expand All @@ -193,14 +198,23 @@ fn prepare_program_extra_data(
runner: &mut CairoRunner,
contract_class: &ContractClassV1,
read_only_segments: &mut ReadOnlySegments,
gas_costs: &GasCosts,
) -> Result<usize, PreExecutionError> {
// Create the builtin cost segment, with dummy values.
let mut data = vec![];

// TODO(spapini): Put real costs here.
for _i in 0..20 {
data.push(MaybeRelocatable::from(0));
}
// Create the builtin cost segment, the builtins order is set by offset_in_builtin_costs in
// cairo lang sierra in the cairo repo .
let builtin_price_array = [
gas_costs.pedersen_gas_cost,
gas_costs.bitwise_builtin_gas_cost,
gas_costs.ecop_gas_cost,
gas_costs.poseidon_gas_cost,
gas_costs.add_mod_gas_cost,
gas_costs.mul_mod_gas_cost,
];

let data = builtin_price_array
.iter()
.map(|&x| MaybeRelocatable::from(Felt::from(x)))
.collect::<Vec<_>>();
let builtin_cost_segment_start = read_only_segments.allocate(&mut runner.vm, &data)?;

// Put a pointer to the builtin cost segment at the end of the program (after the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, BALANCE};

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 17022270; "VM")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 17041278; "VM")]
fn test_secp256k1(test_contract: FeatureContract, expected_gas: u64) {
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);
Expand Down
9 changes: 8 additions & 1 deletion crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,17 @@ impl<'de> Deserialize<'de> for OsResources {
#[derive(Debug, Default, Deserialize)]
pub struct GasCosts {
pub step_gas_cost: u64,
pub memory_hole_gas_cost: u64,
// 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,
pub memory_hole_gas_cost: u64,
// Priced builtins.
pub pedersen_gas_cost: u64,
pub bitwise_builtin_gas_cost: u64,
pub ecop_gas_cost: u64,
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 initial_gas_cost: u64,
Expand Down
Loading

0 comments on commit 5a2ada1

Please sign in to comment.