From 6afff1c2bc2a6b1852afb11173825aa86223bf9b Mon Sep 17 00:00:00 2001 From: Josh Klopfenstein Date: Fri, 24 Nov 2023 16:49:55 -0600 Subject: [PATCH] Permit serializing execution resources as hex --- vm/rust/src/jsonrpc.rs | 70 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/vm/rust/src/jsonrpc.rs b/vm/rust/src/jsonrpc.rs index e6d48920a8..f377a735e7 100644 --- a/vm/rust/src/jsonrpc.rs +++ b/vm/rust/src/jsonrpc.rs @@ -9,12 +9,14 @@ use cairo_vm::vm::runners::builtin_runner::{ use blockifier::state::cached_state::TransactionalState; use blockifier::state::errors::StateError; use blockifier::state::state_api::{State, StateReader}; -use serde::Serialize; +use serde::{Serialize, Serializer, ser::SerializeStruct}; use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, PatriciaKey, EthAddress}; use starknet_api::deprecated_contract_class::EntryPointType; use starknet_api::hash::StarkFelt; use starknet_api::transaction::{Calldata, EventContent, L2ToL1Payload}; use starknet_api::transaction::{DeclareTransaction, Transaction as StarknetApiTransaction}; +use std::collections::HashMap; +use std::string::String; use crate::juno_state_reader::JunoStateReader; @@ -191,27 +193,66 @@ impl From for OrderedEvent { } } -#[derive(Serialize)] pub struct ExecutionResources { pub steps: usize, - pub memory_holes: usize, - #[serde(skip_serializing_if = "Option::is_none")] + pub memory_holes: Option, pub range_check_builtin_applications: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub pedersen_builtin_applications: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub poseidon_builtin_applications: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub ec_op_builtin_applications: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub ecdsa_builtin_applications: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub bitwise_builtin_applications: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub keccak_builtin_applications: Option, // https://github.com/starkware-libs/starknet-specs/pull/167 - #[serde(skip_serializing_if = "Option::is_none")] pub segment_arena_builtin: Option, + is_legacy: bool, +} + +impl Serialize for ExecutionResources { + fn serialize(&self, serializer: S) -> Result + where S: Serializer, { + let mut map = HashMap::<&str, String>::new(); + let encode = |builtin: usize| -> String { + if self.is_legacy { + return format!("{:x}", builtin); + } + builtin.to_string() + }; + if let Some(memory_holes) = self.memory_holes { + map.insert("memory_holes", encode(memory_holes)); + } + if let Some(range_check) = self.range_check_builtin_applications { + map.insert("range_check_builtin_applications", encode(range_check)); + } + if let Some(pedersen) = self.pedersen_builtin_applications { + map.insert("pedersen_builtin_applications", encode(pedersen)); + } + if let Some(poseidon) = self.poseidon_builtin_applications { + map.insert("poseidon_builtin_applications", encode(poseidon)); + } + if let Some(ec_op) = self.ec_op_builtin_applications { + map.insert("ec_op_builtin_applications", encode(ec_op)); + } + if let Some(ecdsa) = self.ecdsa_builtin_applications { + map.insert("ecdsa_builtin_applications", encode(ecdsa)); + } + if let Some(bitwise) = self.bitwise_builtin_applications { + map.insert("bitwise_builtin_applications", encode(bitwise)); + } + if let Some(keccak) = self.keccak_builtin_applications { + map.insert("keccak_builtin_applications", encode(keccak)); + } + if let Some(segment_arena) = self.segment_arena_builtin { + map.insert("segment_arena_builtin", encode(segment_arena)); + } + + let mut state = serializer.serialize_struct("ExecutionResources", map.len() + 1)?; + state.serialize_field("steps", &self.steps)?; + for (key, val) in map.iter() { + state.serialize_field(key, val)?; + } + state.end() + } } use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources; @@ -219,7 +260,11 @@ impl From for ExecutionResources { fn from(val: VmExecutionResources) -> Self { ExecutionResources { steps: val.n_steps, - memory_holes: val.n_memory_holes, + memory_holes: if val.n_memory_holes == 0 { + None + } else { + Some(val.n_memory_holes) + }, range_check_builtin_applications: val.builtin_instance_counter.get(RANGE_CHECK_BUILTIN_NAME).cloned(), pedersen_builtin_applications: val.builtin_instance_counter.get(HASH_BUILTIN_NAME).cloned(), poseidon_builtin_applications: val.builtin_instance_counter.get(POSEIDON_BUILTIN_NAME).cloned(), @@ -228,6 +273,7 @@ impl From for ExecutionResources { bitwise_builtin_applications: val.builtin_instance_counter.get(BITWISE_BUILTIN_NAME).cloned(), keccak_builtin_applications: val.builtin_instance_counter.get(KECCAK_BUILTIN_NAME).cloned(), segment_arena_builtin: val.builtin_instance_counter.get(SEGMENT_ARENA_BUILTIN_NAME).cloned(), + is_legacy: false, } } }