Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

feat(ContractClass): add deserialization #1937

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub mod test;

pub type ContractClassResult<T> = Result<T, ContractClassError>;

#[derive(Clone, Debug, Eq, PartialEq, derive_more::From)]
#[derive(Clone, Debug, Eq, PartialEq, derive_more::From, Deserialize)]
pub enum ContractClass {
V0(ContractClassV0),
V1(ContractClassV1),
Expand Down Expand Up @@ -166,6 +166,24 @@ impl Deref for ContractClassV1 {
}
}

impl<'de> Deserialize<'de> for ContractClassV1 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
// Deserialize into a JSON value
let json_value: serde_json::Value = Deserialize::deserialize(deserializer)?;

// Convert into a JSON string
let json_string = serde_json::to_string(&json_value)
.map_err(|err| DeserializationError::custom(err.to_string()))?;

// Use try_from_json_string to deserialize into ContractClassV1
ContractClassV1::try_from_json_string(&json_string)
.map_err(|err| DeserializationError::custom(err.to_string()))
}
}
Comment on lines +169 to +185
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The true issue here is that cairo_vm::program doesn't implement serde.
So we have to go through all this. It's inefficient.

I opened an issue on the cairo-vm: lambdaclass/cairo-vm#1660

I think you should push this one first, and then update in the blockifier once it is merged

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue looks lambdaclass/cairo-vm#1660 like it'll take a long time to be implemented and merged, how can we not be blocked by it?


impl ContractClassV1 {
fn constructor_selector(&self) -> Option<EntryPointSelector> {
Some(self.0.entry_points_by_type[&EntryPointType::Constructor].first()?.selector)
Expand Down Expand Up @@ -340,7 +358,7 @@ pub struct ContractClassV1Inner {
bytecode_segment_lengths: NestedIntList,
}

#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Deserialize)]
pub struct EntryPointV1 {
pub selector: EntryPointSelector,
pub offset: EntryPointOffset,
Expand Down
21 changes: 20 additions & 1 deletion crates/blockifier/src/execution/contract_class_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::collections::HashSet;
use std::fs;
use std::sync::Arc;

use assert_matches::assert_matches;
use cairo_lang_starknet_classes::NestedIntList;
use rstest::rstest;

use crate::execution::contract_class::{ContractClassV1, ContractClassV1Inner};
use crate::execution::contract_class::{ContractClassV0, ContractClassV1, ContractClassV1Inner};
use crate::transaction::errors::TransactionExecutionError;

#[rstest]
Expand Down Expand Up @@ -42,3 +43,21 @@ fn test_get_visited_segments() {
TransactionExecutionError::InvalidSegmentStructure(907, 807)
);
}

#[test]
fn test_deserialization_of_contract_class_v_0() {
let contract_class: ContractClassV0 =
serde_json::from_slice(&fs::read("./tests/cairo0/counter.json").unwrap())
.expect("failed to deserialize contract class from file");

assert_eq!(contract_class, ContractClassV0::from_file("./tests/cairo0/counter.json"));
}

#[test]
fn test_deserialization_of_contract_class_v_1() {
let contract_class: ContractClassV1 =
serde_json::from_slice(&fs::read("./tests/cairo1/counter.json").unwrap())
.expect("failed to deserialize contract class from file");

assert_eq!(contract_class, ContractClassV1::from_file("./tests/cairo1/counter.json"));
}
Loading
Loading