Skip to content

Commit

Permalink
chore(blockifier): add field sierra_gas to BouncerWeights
Browse files Browse the repository at this point in the history
  • Loading branch information
avivg-starkware committed Dec 10, 2024
1 parent 3e5404b commit 290720f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
5 changes: 5 additions & 0 deletions config/sequencer/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
"privacy": "Public",
"value": 2500000
},
"batcher_config.block_builder_config.bouncer_config.block_max_capacity.sierra_gas": {
"description": "An upper bound on the total sierra_gas used in a block.",
"privacy": "Public",
"value": 1000000
},
"batcher_config.block_builder_config.bouncer_config.block_max_capacity.state_diff_size": {
"description": "An upper bound on the total state diff size in a block.",
"privacy": "Public",
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/src/abi/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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;
Expand Down
20 changes: 17 additions & 3 deletions crates/blockifier/src/bouncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -122,6 +125,7 @@ impl BouncerWeights {
state_diff_size: usize::MAX,
n_events: usize::MAX,
builtin_count: BuiltinCount::max(),
sierra_gas: GasAmount::MAX,
}
}

Expand All @@ -133,6 +137,7 @@ impl BouncerWeights {
message_segment_length: 0,
n_steps: 0,
state_diff_size: 0,
sierra_gas: GasAmount::ZERO,
}
}
}
Expand All @@ -147,6 +152,7 @@ impl Default for BouncerWeights {
n_events: 5000,
state_diff_size: 4000,
builtin_count: BuiltinCount::default(),
sierra_gas: GasAmount(250000000),
}
}
}
Expand Down Expand Up @@ -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
}
}
Expand All @@ -193,13 +205,14 @@ impl std::fmt::Display for BouncerWeights {
write!(
f,
"BouncerWeights {{ l1_gas: {}, n_steps: {}, message_segment_length: {}, n_events: {}, \
state_diff_size: {}, builtin_count: {} }}",
state_diff_size: {}, builtin_count: {}, sierra_gas: {} }}",
self.l1_gas,
self.n_steps,
self.message_segment_length,
self.n_events,
self.state_diff_size,
self.builtin_count
self.builtin_count,
self.sierra_gas
)
}
}
Expand Down Expand Up @@ -539,6 +552,7 @@ pub fn get_tx_weights<S: StateReader>(
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,
})
}

Expand Down
8 changes: 8 additions & 0 deletions crates/blockifier/src/bouncer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -42,6 +43,7 @@ fn test_block_weights_has_room() {
n_events: 10,
n_steps: 10,
state_diff_size: 10,
sierra_gas: GasAmount(10),
};

let bouncer_weights = BouncerWeights {
Expand All @@ -62,6 +64,7 @@ fn test_block_weights_has_room() {
n_steps: 0,
n_events: 2,
state_diff_size: 7,
sierra_gas: GasAmount(7),
};

assert!(max_bouncer_weights.has_room(bouncer_weights));
Expand All @@ -84,6 +87,7 @@ fn test_block_weights_has_room() {
n_steps: 5,
n_events: 5,
state_diff_size: 5,
sierra_gas: GasAmount(5),
};

assert!(!max_bouncer_weights.has_room(bouncer_weights_exceeds_max));
Expand Down Expand Up @@ -119,6 +123,7 @@ fn test_block_weights_has_room() {
n_steps: 10,
n_events: 10,
state_diff_size: 10,
sierra_gas: GasAmount(10),
},
})]
fn test_bouncer_update(#[case] initial_bouncer: Bouncer) {
Expand Down Expand Up @@ -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),
};

let state_changes_keys_to_update =
Expand Down Expand Up @@ -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),
};
let bouncer_config = BouncerConfig { block_max_capacity };

Expand All @@ -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),
};

let mut bouncer = Bouncer { accumulated_weights, bouncer_config, ..Bouncer::empty() };
Expand Down
5 changes: 5 additions & 0 deletions crates/native_blockifier/src/py_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -125,13 +126,17 @@ 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 {
l1_gas,
n_steps,
message_segment_length,
state_diff_size,
n_events,
builtin_count: hash_map_into_builtin_count(data)?,
sierra_gas,
})
}

Expand Down
12 changes: 8 additions & 4 deletions crates/starknet_api/src/execution_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use strum_macros::EnumIter;
use crate::block::{GasPrice, GasPriceVector, NonzeroGasPrice};
use crate::transaction::fields::{Fee, Resource};

#[cfg_attr(
any(test, feature = "testing"),
derive(derive_more::Add, derive_more::Sum, derive_more::AddAssign, derive_more::Div)
)]
#[cfg_attr(any(test, feature = "testing"), derive(derive_more::Sum, derive_more::Div))]
#[derive(
derive_more::Display,
derive_more::Sub,
derive_more::Add,
derive_more::AddAssign,
Clone,
Copy,
Debug,
Expand Down Expand Up @@ -55,6 +55,10 @@ impl GasAmount {
self.0.checked_add(rhs.0).map(Self)
}

pub fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}

pub const fn nonzero_saturating_mul(self, rhs: NonzeroGasPrice) -> Fee {
rhs.saturating_mul(self)
}
Expand Down

Large diffs are not rendered by default.

0 comments on commit 290720f

Please sign in to comment.