diff --git a/crates/blockifier/src/abi/constants.rs b/crates/blockifier/src/abi/constants.rs index efbf664b9ab..827443de399 100644 --- a/crates/blockifier/src/abi/constants.rs +++ b/crates/blockifier/src/abi/constants.rs @@ -33,6 +33,7 @@ pub const N_EVENTS: &str = "n_events"; pub const MESSAGE_SEGMENT_LENGTH: &str = "message_segment_length"; pub const STATE_DIFF_SIZE: &str = "state_diff_size"; pub const N_MEMORY_HOLES: &str = "n_memory_holes"; +pub const SIERRA_GAS: &str = "sierra_gas"; // Casm hash calculation-related constants. pub const CAIRO0_ENTRY_POINT_STRUCT_SIZE: usize = 2; diff --git a/crates/blockifier/src/bouncer.rs b/crates/blockifier/src/bouncer.rs index d70c0095020..ae698690fef 100644 --- a/crates/blockifier/src/bouncer.rs +++ b/crates/blockifier/src/bouncer.rs @@ -6,6 +6,7 @@ use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam}; use serde::{Deserialize, Serialize}; use starknet_api::core::ClassHash; +use starknet_api::execution_resources::GasAmount; use crate::blockifier::transaction_executor::{ TransactionExecutorError, @@ -98,6 +99,7 @@ pub struct BouncerWeights { pub n_events: usize, pub n_steps: usize, pub state_diff_size: usize, + pub sierra_gas: GasAmount, } impl BouncerWeights { @@ -107,7 +109,8 @@ impl BouncerWeights { message_segment_length, n_events, n_steps, - state_diff_size + state_diff_size, + sierra_gas ); pub fn has_room(&self, other: Self) -> bool { @@ -122,6 +125,7 @@ impl BouncerWeights { state_diff_size: usize::MAX, n_events: usize::MAX, builtin_count: BuiltinCount::max(), + sierra_gas: GasAmount::MAX, } } @@ -133,6 +137,7 @@ impl BouncerWeights { message_segment_length: 0, n_steps: 0, state_diff_size: 0, + sierra_gas: GasAmount::ZERO, } } } @@ -147,6 +152,7 @@ impl Default for BouncerWeights { n_events: 5000, state_diff_size: 4000, builtin_count: BuiltinCount::default(), + sierra_gas: GasAmount(1000000), // TODO(AvivG): replace w desired val. } } } @@ -184,6 +190,12 @@ impl SerializeConfig for BouncerWeights { "An upper bound on the total state diff size in a block.", ParamPrivacyInput::Public, )])); + dump.append(&mut BTreeMap::from([ser_param( + "sierra_gas", + &self.sierra_gas, + "An upper bound on the total sierra_gas used in a block.", + ParamPrivacyInput::Public, + )])); dump } } @@ -193,13 +205,14 @@ impl std::fmt::Display for BouncerWeights { write!( f, "BouncerWeights {{ l1gas: {}, n_steps: {}, message_segment_length: {}, n_events: {}, \ - state_diff_size: {}, builtin_count: {} }}", + state_diff_size: {}, builtin_count: {}, sierra_gas: {} }}", self.l1gas, self.n_steps, self.message_segment_length, self.n_events, self.state_diff_size, - self.builtin_count + self.builtin_count, + self.sierra_gas ) } } @@ -539,6 +552,7 @@ pub fn get_tx_weights( n_steps: vm_resources.total_n_steps(), builtin_count: BuiltinCount::from(vm_resources.prover_builtins()), state_diff_size: get_onchain_data_segment_length(&state_changes_keys.count()), + sierra_gas: tx_resources.computation.sierra_gas, }) } diff --git a/crates/blockifier/src/bouncer_test.rs b/crates/blockifier/src/bouncer_test.rs index 1826583c52b..bff064a95df 100644 --- a/crates/blockifier/src/bouncer_test.rs +++ b/crates/blockifier/src/bouncer_test.rs @@ -4,6 +4,7 @@ use assert_matches::assert_matches; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use rstest::rstest; +use starknet_api::execution_resources::GasAmount; use starknet_api::transaction::fields::Fee; use starknet_api::{class_hash, contract_address, storage_key}; @@ -42,6 +43,7 @@ fn test_block_weights_has_room() { n_events: 10, n_steps: 10, state_diff_size: 10, + sierra_gas: GasAmount(10), // TODO(AvivG): replace w desired val. }; let bouncer_weights = BouncerWeights { @@ -62,6 +64,7 @@ fn test_block_weights_has_room() { n_steps: 0, n_events: 2, state_diff_size: 7, + sierra_gas: GasAmount::ZERO, // TODO(AvivG): replace w desired val. }; assert!(max_bouncer_weights.has_room(bouncer_weights)); @@ -84,6 +87,7 @@ fn test_block_weights_has_room() { n_steps: 5, n_events: 5, state_diff_size: 5, + sierra_gas: GasAmount(5), // TODO(AvivG): replace w desired val. }; assert!(!max_bouncer_weights.has_room(bouncer_weights_exceeds_max)); @@ -119,6 +123,7 @@ fn test_block_weights_has_room() { n_steps: 10, n_events: 10, state_diff_size: 10, + sierra_gas: GasAmount(10), // TODO(AvivG): replace w desired val. }, })] fn test_bouncer_update(#[case] initial_bouncer: Bouncer) { @@ -149,6 +154,7 @@ fn test_bouncer_update(#[case] initial_bouncer: Bouncer) { n_steps: 0, n_events: 1, state_diff_size: 2, + sierra_gas: GasAmount(9), // TODO(AvivG): replace w desired val. }; let state_changes_keys_to_update = @@ -202,6 +208,7 @@ fn test_bouncer_try_update(#[case] added_ecdsa: usize, #[case] scenario: &'stati n_steps: 20, n_events: 20, state_diff_size: 20, + sierra_gas: GasAmount(20), // TODO(AvivG): replace w desired val. }; let bouncer_config = BouncerConfig { block_max_capacity }; @@ -223,6 +230,7 @@ fn test_bouncer_try_update(#[case] added_ecdsa: usize, #[case] scenario: &'stati n_steps: 10, n_events: 10, state_diff_size: 10, + sierra_gas: GasAmount(10), // TODO(AvivG): replace w desired val. }; let mut bouncer = Bouncer { accumulated_weights, bouncer_config, ..Bouncer::empty() }; diff --git a/crates/native_blockifier/src/py_objects.rs b/crates/native_blockifier/src/py_objects.rs index 43e03f05e7b..9a3d88258bc 100644 --- a/crates/native_blockifier/src/py_objects.rs +++ b/crates/native_blockifier/src/py_objects.rs @@ -10,6 +10,7 @@ use blockifier::versioned_constants::VersionedConstantsOverrides; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use pyo3::prelude::*; +use starknet_api::execution_resources::GasAmount; use crate::errors::{ InvalidNativeBlockifierInputError, @@ -125,6 +126,9 @@ fn hash_map_into_bouncer_weights( let state_diff_size = data.remove(constants::STATE_DIFF_SIZE).expect("state_diff_size must be present"); let n_events = data.remove(constants::N_EVENTS).expect("n_events must be present"); + let sierra_gas = GasAmount( + data.remove(constants::SIERRA_GAS).expect("sierra_gas must be present").try_into().unwrap(), + ); Ok(BouncerWeights { l1gas, n_steps, @@ -132,6 +136,7 @@ fn hash_map_into_bouncer_weights( state_diff_size, n_events, builtin_count: hash_map_into_builtin_count(data)?, + sierra_gas, }) } diff --git a/crates/starknet_api/src/execution_resources.rs b/crates/starknet_api/src/execution_resources.rs index e3c14d1dbb7..bbc6c8c91dd 100644 --- a/crates/starknet_api/src/execution_resources.rs +++ b/crates/starknet_api/src/execution_resources.rs @@ -13,6 +13,7 @@ use crate::transaction::fields::{Fee, Resource}; )] #[derive( derive_more::Display, + derive_more::Sub, Clone, Copy, Debug, @@ -55,6 +56,10 @@ impl GasAmount { self.0.checked_add(rhs.0).map(Self) } + pub fn checked_sub(self, rhs: Self) -> Option { + self.0.checked_sub(rhs.0).map(Self) + } + pub const fn nonzero_saturating_mul(self, rhs: NonzeroGasPrice) -> Fee { rhs.saturating_mul(self) }