Skip to content

Commit

Permalink
Permit serializing execution resources as hex
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop committed Nov 24, 2023
1 parent 6f89dfe commit 6afff1c
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 deletions vm/rust/src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -191,35 +193,78 @@ impl From<BlockifierOrderedEvent> 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<usize>,
pub range_check_builtin_applications: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pedersen_builtin_applications: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub poseidon_builtin_applications: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ec_op_builtin_applications: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ecdsa_builtin_applications: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bitwise_builtin_applications: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub keccak_builtin_applications: Option<usize>,
// https://github.com/starkware-libs/starknet-specs/pull/167
#[serde(skip_serializing_if = "Option::is_none")]
pub segment_arena_builtin: Option<usize>,
is_legacy: bool,
}

impl Serialize for ExecutionResources {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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;
impl From<VmExecutionResources> 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(),
Expand All @@ -228,6 +273,7 @@ impl From<VmExecutionResources> 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,
}
}
}
Expand Down

0 comments on commit 6afff1c

Please sign in to comment.