Skip to content

Commit

Permalink
bump cairo and cairo-vm
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 committed Dec 11, 2023
1 parent e345eec commit e917332
Show file tree
Hide file tree
Showing 8 changed files with 979 additions and 1,322 deletions.
1,816 changes: 651 additions & 1,165 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 7 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# https://doc.rust-lang.org/cargo/reference/resolver.html#feature-resolver-version-2
resolver = "2"

members = ["crates/blockifier", "crates/native_blockifier"]
members = ["crates/blockifier"]

[workspace.package]
version = "0.4.0-rc6"
Expand All @@ -19,12 +19,12 @@ ark-secp256k1 = "0.4.0"
ark-secp256r1 = "0.4.0"
assert_matches = "1.5.0"
cached = "0.44.0"
cairo-felt = "0.8.2"
cairo-lang-casm = "2.4.0-rc2"
cairo-lang-runner = "2.4.0-rc2"
cairo-lang-starknet = "2.4.0-rc2"
cairo-lang-utils = "2.4.0-rc2"
cairo-vm = "=0.8.2"
cairo-felt = { git = "https://github.com/kkrt-labs/cairo-vm.git", branch = "v0.9.1", default-features = false }
cairo-lang-casm = { git = "https://github.com/kkrt-labs/cairo.git", branch = "v2.4.0", default-features = false }
cairo-lang-runner = { git = "https://github.com/kkrt-labs/cairo.git", branch = "v2.4.0", default-features = false }
cairo-lang-starknet = { git = "https://github.com/kkrt-labs/cairo.git", branch = "v2.4.0", default-features = false }
cairo-lang-utils = { git = "https://github.com/kkrt-labs/cairo.git", branch = "v2.4.0", default-features = false }
cairo-vm = { git = "https://github.com/kkrt-labs/cairo-vm.git", branch = "v0.9.1", default-features = false }
criterion = "0.3"
ctor = "0.2.0"
derive_more = "0.99.17"
Expand Down Expand Up @@ -58,7 +58,3 @@ future-incompatible = "deny"
nonstandard-style = "deny"
rust-2018-idioms = "deny"
unused = "deny"

[patch.crates-io]
cairo-felt = { git = "https://github.com/kkrt-labs/cairo-vm.git", branch = "v0.8.2" }
cairo-vm = { git = "https://github.com/kkrt-labs/cairo-vm.git", branch = "v0.8.2" }
3 changes: 0 additions & 3 deletions crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ repository.workspace = true
license-file.workspace = true
description = "The transaction-executing component in the StarkNet sequencer."

[lints]
workspace = true

[features]
testing = []

Expand Down
161 changes: 102 additions & 59 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};
use std::ops::Deref;
use std::sync::Arc;

Expand All @@ -12,12 +12,12 @@ use cairo_vm::serde::deserialize_program::{
InstructionLocation, ReferenceManager,
};
use cairo_vm::types::errors::program_errors::ProgramError;
use cairo_vm::types::program::{Program, SharedProgramData};
use cairo_vm::types::program::{HintsCollection, Program, SharedProgramData};
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::runners::builtin_runner::{HASH_BUILTIN_NAME, POSEIDON_BUILTIN_NAME};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources;
use serde::de::{self, Error as DeserializationError};
use serde::{Deserialize, Deserializer, Serialize};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use starknet_api::core::EntryPointSelector;
use starknet_api::deprecated_contract_class::{
ContractClass as DeprecatedContractClass, EntryPoint, EntryPointOffset, EntryPointType,
Expand Down Expand Up @@ -109,7 +109,7 @@ impl ContractClassV0 {

#[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct ContractClassV0Inner {
#[serde(deserialize_with = "deserialize_program")]
#[serde(with = "serde_program")]
pub program: Program,
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
}
Expand Down Expand Up @@ -285,16 +285,16 @@ impl TryFrom<CasmContractClass> for ContractClassV1 {

// V0 utilities.

/// Converts the program type from SN API into a Cairo VM-compatible type.
pub fn deserialize_program<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Program, D::Error> {
mod serde_program {
use super::*;

// We have this because `hints` field is a hashmap from <uisze, HintParams>
// When deserializing from JSON, for untagged enum it will only match for <string, HintParams>
#[derive(Serialize, Deserialize)]
// When deserializing from JSON, for untagged enum it will only match for <string,
// HintParams>
#[derive(Deserialize, Serialize)]
struct TmpSharedProgram {
data: Vec<MaybeRelocatable>,
hints: HashMap<String, Vec<HintParams>>,
hints: BTreeMap<String, Vec<HintParams>>,
main: Option<usize>,
start: Option<usize>,
end: Option<usize>,
Expand All @@ -304,64 +304,107 @@ pub fn deserialize_program<'de, D: Deserializer<'de>>(
reference_manager: Vec<HintReference>,
}

#[derive(Serialize, Deserialize)]
impl From<SharedProgramData> for TmpSharedProgram {
fn from(shared_program_data: SharedProgramData) -> Self {
Self {
data: shared_program_data.data,
hints: Into::<BTreeMap<usize, Vec<HintParams>>>::into(
&shared_program_data.hints_collection,
)
.into_iter()
.map(|(k, v)| (k.to_string(), v))
.collect(),
main: shared_program_data.main,
start: shared_program_data.start,
end: shared_program_data.end,
error_message_attributes: shared_program_data.error_message_attributes,
instruction_locations: shared_program_data.instruction_locations,
identifiers: shared_program_data.identifiers,
reference_manager: shared_program_data.reference_manager,
}
}
}

#[derive(Deserialize, Serialize)]
struct TmpProgram {
pub shared_program_data: TmpSharedProgram,
pub constants: HashMap<String, Felt252>,
pub builtins: Vec<BuiltinName>,
}

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum Tmp {
/// Box the variant in order to reduce the size of the enum (clippy suggestion).
CairoVM(Box<TmpProgram>),
SNProgram(DeprecatedProgram),
pub(crate) fn serialize<S>(program: &Program, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let shared_program_data = program.shared_program_data.as_ref().clone().into();
let constants = program.constants.clone();
let builtins = program.builtins.clone();

let tmp_program = TmpProgram { shared_program_data, constants, builtins };

tmp_program.serialize(serializer)
}

let program: Tmp = Tmp::deserialize(deserializer)?;
/// Converts the program type from SN API into a Cairo VM-compatible type.
pub(crate) fn deserialize<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Program, D::Error> {
#[derive(Deserialize)]
#[serde(untagged)]
enum Tmp {
/// Box the variant in order to reduce the size of the enum (clippy suggestion).
CairoVM(Box<TmpProgram>),
SNProgram(DeprecatedProgram),
}

match program {
Tmp::CairoVM(tmp_program) => {
let hints: Result<HashMap<usize, Vec<HintParams>>, D::Error> = tmp_program
.shared_program_data
.hints
.into_iter()
.map(|(k, v)| {
let key = k.parse::<usize>().map_err(|error| {
de::Error::custom(format!(
"failed to convert value {} to usize, \n error {}",
k, error
))
})?;

Ok((key, v))
})
.collect();

let hints = hints?;

let shared_program_data = SharedProgramData {
data: tmp_program.shared_program_data.data,
hints,
main: tmp_program.shared_program_data.main,
start: tmp_program.shared_program_data.start,
end: tmp_program.shared_program_data.end,
error_message_attributes: tmp_program.shared_program_data.error_message_attributes,
identifiers: tmp_program.shared_program_data.identifiers,
instruction_locations: tmp_program.shared_program_data.instruction_locations,
reference_manager: tmp_program.shared_program_data.reference_manager,
};

let program = Program {
shared_program_data: Arc::new(shared_program_data),
constants: tmp_program.constants,
builtins: tmp_program.builtins,
};
Ok(program)
let program: Tmp = Tmp::deserialize(deserializer)?;

match program {
Tmp::CairoVM(tmp_program) => {
let hints: BTreeMap<usize, Vec<HintParams>> = tmp_program
.shared_program_data
.hints
.into_iter()
.map(|(k, v)| {
let key = k.parse::<usize>().map_err(|error| {
de::Error::custom(format!(
"failed to convert value {} to usize, \n error {}",
k, error
))
})?;

Ok((key, v))
})
.collect::<Result<_, D::Error>>()?;

let hints_collection =
HintsCollection::new(&hints, tmp_program.shared_program_data.data.len())
.map_err(|err| de::Error::custom(err.to_string()))?;

let shared_program_data = SharedProgramData {
data: tmp_program.shared_program_data.data,
hints_collection,
main: tmp_program.shared_program_data.main,
start: tmp_program.shared_program_data.start,
end: tmp_program.shared_program_data.end,
error_message_attributes: tmp_program
.shared_program_data
.error_message_attributes,
identifiers: tmp_program.shared_program_data.identifiers,
instruction_locations: tmp_program.shared_program_data.instruction_locations,
reference_manager: tmp_program.shared_program_data.reference_manager,
};

let program = Program {
shared_program_data: Arc::new(shared_program_data),
constants: tmp_program.constants,
builtins: tmp_program.builtins,
};
Ok(program)
}
Tmp::SNProgram(deprecated_program) => sn_api_to_cairo_vm_program(deprecated_program)
.map_err(|err| DeserializationError::custom(err.to_string())),
}
Tmp::SNProgram(deprecated_program) => sn_api_to_cairo_vm_program(deprecated_program)
.map_err(|err| DeserializationError::custom(err.to_string())),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub fn finalize_execution(
// Has to happen after marking holes in segments as accessed.
let vm_resources_without_inner_calls = runner
.get_execution_resources(&vm)
.map_err(VirtualMachineError::TracerError)?
.map_err(VirtualMachineError::RunnerError)?
.filter_unused_builtins();
syscall_handler.resources.vm_resources += &vm_resources_without_inner_calls;

Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ pub fn finalize_execution(
// Has to happen after marking holes in segments as accessed.
let vm_resources_without_inner_calls = runner
.get_execution_resources(&vm)
.map_err(VirtualMachineError::TracerError)?
.map_err(VirtualMachineError::RunnerError)?
.filter_unused_builtins();
syscall_handler.resources.vm_resources += &vm_resources_without_inner_calls;

Expand Down
Loading

0 comments on commit e917332

Please sign in to comment.