Skip to content

Commit 48bc7b3

Browse files
blockifier: enable getting cairo_primitive gas cost by name
1 parent 5bb4dd5 commit 48bc7b3

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

crates/blockifier/src/blockifier_versioned_constants.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use starknet_api::versioned_constants_logic::VersionedConstantsTrait;
2424
use strum::IntoEnumIterator;
2525
use thiserror::Error;
2626

27+
use crate::execution::call_info::{CairoPrimitiveName, OpcodeName};
2728
use crate::execution::common_hints::ExecutionMode;
2829
use crate::execution::execution_utils::poseidon_hash_many_cost;
2930
use crate::execution::syscalls::vm_syscall_utils::{SyscallSelector, SyscallUsageMap};
@@ -926,6 +927,7 @@ pub struct BaseGasCosts {
926927
}
927928

928929
#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Serialize)]
930+
// TODO(AvivG): Consider renaming to CairoPrimitiveGasCosts to match with its usage in the bouncer.
929931
pub struct BuiltinGasCosts {
930932
// Range check has a hard-coded cost higher than its proof percentage to avoid the overhead of
931933
// retrieving its price from the table.
@@ -965,6 +967,23 @@ impl BuiltinGasCosts {
965967

966968
Ok(gas_cost)
967969
}
970+
971+
pub fn get_opcode_gas_cost(&self, opcode: &OpcodeName) -> u64 {
972+
match opcode {
973+
OpcodeName::Blake => self.blake,
974+
}
975+
}
976+
977+
/// Returns the gas cost for any Cairo primitive (builtin or opcode).
978+
pub fn get_cairo_primitive_gas_cost(
979+
&self,
980+
primitive: &CairoPrimitiveName,
981+
) -> Result<u64, GasCostsError> {
982+
match primitive {
983+
CairoPrimitiveName::Builtin(builtin) => self.get_builtin_gas_cost(builtin),
984+
CairoPrimitiveName::Opcode(opcode) => Ok(self.get_opcode_gas_cost(opcode)),
985+
}
986+
}
968987
}
969988

970989
/// Gas cost constants. For more documentation see in core/os/constants.cairo.

crates/blockifier/src/execution/call_info.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use starknet_api::transaction::{
1818
MessageToL1 as StarknetAPIMessageToL1,
1919
};
2020
use starknet_types_core::felt::Felt;
21+
use strum::{AsRefStr, Display, EnumString};
2122

2223
use crate::blockifier_versioned_constants::VersionedConstants;
2324
use crate::execution::contract_class::TrackedResource;
@@ -273,11 +274,18 @@ impl AddAssign<&ChargedResources> for ChargedResources {
273274
}
274275

275276
#[cfg_attr(feature = "transaction_serde", derive(serde::Deserialize))]
276-
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Ord, PartialOrd)]
277+
#[derive(
278+
Clone, Debug, Eq, Hash, PartialEq, Serialize, Ord, PartialOrd, Display, EnumString, AsRefStr,
279+
)]
280+
#[strum(serialize_all = "snake_case")]
277281
pub enum OpcodeName {
278282
Blake,
279283
}
280284

285+
/// Error type for parsing [`CairoPrimitiveName`] from a string.
286+
#[derive(Debug, Clone, PartialEq, Eq)]
287+
pub struct ParseCairoPrimitiveNameError;
288+
281289
#[cfg_attr(feature = "transaction_serde", derive(serde::Deserialize))]
282290
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Ord, PartialOrd)]
283291
// Serialize as an untagged enum to avoid a type prefix, for backward compatibility with
@@ -288,6 +296,36 @@ pub enum CairoPrimitiveName {
288296
Opcode(OpcodeName),
289297
}
290298

299+
impl CairoPrimitiveName {
300+
pub fn to_str(&self) -> &str {
301+
match self {
302+
Self::Builtin(builtin) => builtin.to_str(),
303+
Self::Opcode(opcode) => opcode.as_ref(),
304+
}
305+
}
306+
}
307+
308+
impl std::fmt::Display for CairoPrimitiveName {
309+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
310+
match self {
311+
Self::Builtin(builtin) => write!(f, "{builtin}"),
312+
Self::Opcode(opcode) => write!(f, "{opcode}"),
313+
}
314+
}
315+
}
316+
317+
impl std::str::FromStr for CairoPrimitiveName {
318+
type Err = ParseCairoPrimitiveNameError;
319+
320+
/// Tries to parse as a builtin first, then as an opcode.
321+
fn from_str(s: &str) -> Result<Self, Self::Err> {
322+
if let Some(builtin) = BuiltinName::from_str(s) {
323+
return Ok(Self::Builtin(builtin));
324+
}
325+
s.parse::<OpcodeName>().map(Self::Opcode).map_err(|_| ParseCairoPrimitiveNameError)
326+
}
327+
}
328+
291329
impl From<BuiltinName> for CairoPrimitiveName {
292330
fn from(builtin_name: BuiltinName) -> Self {
293331
CairoPrimitiveName::Builtin(builtin_name)

0 commit comments

Comments
 (0)