Skip to content

Commit dfeb234

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

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,45 @@ impl AddAssign<&ChargedResources> for ChargedResources {
272272
}
273273
}
274274

275+
// Opcode name constants.
276+
const BLAKE_OPCODE_NAME: &str = "blake";
277+
275278
#[cfg_attr(feature = "transaction_serde", derive(serde::Deserialize))]
276279
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Ord, PartialOrd)]
277280
pub enum OpcodeName {
278281
Blake,
279282
}
280283

284+
impl OpcodeName {
285+
/// Converts an [`OpcodeName`] to its string representation.
286+
pub fn to_str(&self) -> &'static str {
287+
match self {
288+
OpcodeName::Blake => BLAKE_OPCODE_NAME,
289+
}
290+
}
291+
}
292+
293+
impl std::fmt::Display for OpcodeName {
294+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
295+
f.write_str(self.to_str())
296+
}
297+
}
298+
299+
impl std::str::FromStr for OpcodeName {
300+
type Err = ParseCairoPrimitiveNameError;
301+
302+
fn from_str(s: &str) -> Result<Self, Self::Err> {
303+
match s {
304+
BLAKE_OPCODE_NAME => Ok(OpcodeName::Blake),
305+
_ => Err(ParseCairoPrimitiveNameError),
306+
}
307+
}
308+
}
309+
310+
/// Error type for parsing [`OpcodeName`] or [`CairoPrimitiveName`] from a string.
311+
#[derive(Debug, Clone, PartialEq, Eq)]
312+
pub struct ParseCairoPrimitiveNameError;
313+
281314
#[cfg_attr(feature = "transaction_serde", derive(serde::Deserialize))]
282315
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Ord, PartialOrd)]
283316
// Serialize as an untagged enum to avoid a type prefix, for backward compatibility with
@@ -288,6 +321,31 @@ pub enum CairoPrimitiveName {
288321
Opcode(OpcodeName),
289322
}
290323

324+
impl CairoPrimitiveName {
325+
/// Converts a [`CairoPrimitiveName`] to its string representation.
326+
pub fn to_str(&self) -> &'static str {
327+
match self {
328+
CairoPrimitiveName::Builtin(builtin) => builtin.to_str(),
329+
CairoPrimitiveName::Opcode(opcode) => opcode.to_str(),
330+
}
331+
}
332+
}
333+
334+
impl std::str::FromStr for CairoPrimitiveName {
335+
type Err = ParseCairoPrimitiveNameError;
336+
337+
/// Tries to parse as a builtin first, then as an opcode.
338+
fn from_str(s: &str) -> Result<Self, Self::Err> {
339+
if let Some(builtin) = BuiltinName::from_str(s) {
340+
return Ok(CairoPrimitiveName::Builtin(builtin));
341+
}
342+
if let Ok(opcode) = s.parse::<OpcodeName>() {
343+
return Ok(CairoPrimitiveName::Opcode(opcode));
344+
}
345+
Err(ParseCairoPrimitiveNameError)
346+
}
347+
}
348+
291349
impl From<BuiltinName> for CairoPrimitiveName {
292350
fn from(builtin_name: BuiltinName) -> Self {
293351
CairoPrimitiveName::Builtin(builtin_name)
@@ -300,6 +358,12 @@ impl From<OpcodeName> for CairoPrimitiveName {
300358
}
301359
}
302360

361+
impl std::fmt::Display for CairoPrimitiveName {
362+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
363+
f.write_str(self.to_str())
364+
}
365+
}
366+
303367
pub type CairoPrimitiveCounterMap = BTreeMap<CairoPrimitiveName, usize>;
304368

305369
pub type BuiltinCounterMap = BTreeMap<BuiltinName, usize>;

0 commit comments

Comments
 (0)