Skip to content

Commit

Permalink
test(sequencing): serialize compiled_contract_class to the central ob…
Browse files Browse the repository at this point in the history
…ject (#3115)
  • Loading branch information
Yael-Starkware authored Jan 16, 2025
1 parent 15fcf9f commit 1e9cbaf
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/sequencing/papyrus_consensus_orchestrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description = "Implements the consensus context and orchestrates the node's comp
[dependencies]
assert_matches.workspace = true
async-trait.workspace = true
cairo-lang-starknet-classes.workspace = true
chrono.workspace = true
futures.workspace = true
indexmap.workspace = true
Expand All @@ -34,9 +35,12 @@ url = { workspace = true, features = ["serde"] }

[dev-dependencies]
blockifier.workspace = true
cairo-lang-casm.workspace = true
cairo-lang-utils.workspace = true
lazy_static.workspace = true
mockall.workspace = true
mockito.workspace = true
num-bigint.workspace = true
papyrus_network = { workspace = true, features = ["testing"] }
papyrus_storage = { workspace = true, features = ["testing"] }
papyrus_test_utils.workspace = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"prime": "0x1",
"compiler_version": "dummy version",
"bytecode": [
"0x1"
],
"bytecode_segment_lengths": [1, 2],
"hints": [
[4, [
{
"AllocSegment": {
"dst": {
"offset": 1,
"register": "AP"
}
}
}
]
]
],
"pythonic_hints": [
[5, [
"dummy pythonic hint"
]
]
],
"entry_points_by_type": {
"CONSTRUCTOR": [
{
"selector": "0x1",
"offset": 1,
"builtins": ["dummy builtin"]
}
],
"EXTERNAL": [
{
"selector": "0x1",
"offset": 1,
"builtins": ["dummy builtin"]
}
],
"L1_HANDLER": [
{
"selector": "0x1",
"offset": 1,
"builtins": ["dummy builtin"]
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"prime": "0x1",
"compiler_version": "dummy version",
"bytecode": [
"0x1"
],
"bytecode_segment_lengths": [],
"hints": [
[4, [
{
"AllocSegment": {
"dst": {
"offset": 1,
"register": "AP"
}
}
}
]
]
],
"pythonic_hints": [],
"entry_points_by_type": {
"CONSTRUCTOR": [
{
"selector": "0x1",
"offset": 1,
"builtins": ["dummy builtin"]
}
],
"EXTERNAL": [
{
"selector": "0x1",
"offset": 1,
"builtins": ["dummy builtin"]
}
],
"L1_HANDLER": [
{
"selector": "0x1",
"offset": 1,
"builtins": ["dummy builtin"]
}
]
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use assert_matches::assert_matches;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_lang_starknet_classes::NestedIntList;
use indexmap::{indexmap, IndexMap};
use serde::Serialize;
use starknet_api::block::{
Expand Down Expand Up @@ -352,3 +354,19 @@ impl From<(Transaction, u64)> for CentralTransactionWritten {
}
}
}

// Converts the CasmContractClass into a format that serializes into the python object.
// TODO(Yael): remove allow dead code once used
#[allow(dead_code)]
pub fn casm_contract_class_central_format(
compiled_class_hash: CasmContractClass,
) -> CasmContractClass {
CasmContractClass {
// The rust object allows these fields to be none, while in python they are mandatory.
bytecode_segment_lengths: Some(
compiled_class_hash.bytecode_segment_lengths.unwrap_or(NestedIntList::Node(vec![])),
),
pythonic_hints: Some(compiled_class_hash.pythonic_hints.unwrap_or_default()),
..compiled_class_hash
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
use std::sync::Arc;
use std::vec;

use blockifier::bouncer::BouncerWeights;
use cairo_lang_casm::hints::{CoreHint, CoreHintBase, Hint};
use cairo_lang_casm::operand::{CellRef, Register};
use cairo_lang_starknet_classes::casm_contract_class::{
CasmContractClass,
CasmContractEntryPoint,
CasmContractEntryPoints,
};
use cairo_lang_starknet_classes::NestedIntList;
use cairo_lang_utils::bigint::BigUintAsHex;
use indexmap::indexmap;
use num_bigint::BigUint;
use rstest::rstest;
use serde_json::Value;
use starknet_api::block::{
Expand Down Expand Up @@ -57,14 +68,18 @@ use super::{
CentralTransaction,
CentralTransactionWritten,
};
use crate::cende::central_objects::casm_contract_class_central_format;

pub const CENTRAL_STATE_DIFF_JSON_PATH: &str = "central_state_diff.json";
pub const CENTRAL_INVOKE_TX_JSON_PATH: &str = "central_invoke_tx.json";
pub const CENTRAL_DEPLOY_ACCOUNT_TX_JSON_PATH: &str = "central_deploy_account_tx.json";
pub const CENTRAL_DECLARE_TX_JSON_PATH: &str = "central_declare_tx.json";
pub const CENTRAL_L1_HANDLER_TX_JSON_PATH: &str = "central_l1_handler_tx.json";
pub const CENTRAL_BOUNCER_WEIGHTS_JSON_PATH: &str = "central_bouncer_weights.json";
pub const CENTRAL_CONTRACT_CLASS_JSON_PATH: &str = "central_sierra_contract_class.json";
pub const CENTRAL_SIERRA_CONTRACT_CLASS_JSON_PATH: &str = "central_sierra_contract_class.json";
pub const CENTRAL_CASM_CONTRACT_CLASS_JSON_PATH: &str = "central_contract_class.casm.json";
pub const CENTRAL_CASM_CONTRACT_CLASS_DEFAULT_OPTIONALS_JSON_PATH: &str =
"central_contract_class_default_optionals.casm.json";

fn resource_bounds() -> ValidResourceBounds {
ValidResourceBounds::AllResources(AllResourceBounds {
Expand Down Expand Up @@ -263,6 +278,54 @@ fn central_sierra_contract_class_json() -> Value {
serde_json::to_value(sierra_contract_class).unwrap()
}

fn casm_contract_entry_points() -> Vec<CasmContractEntryPoint> {
vec![CasmContractEntryPoint {
selector: BigUint::from(1_u8),
offset: 1,
builtins: vec!["dummy builtin".to_string()],
}]
}

fn casm_contract_class() -> CasmContractClass {
CasmContractClass {
prime: BigUint::from(1_u8),
compiler_version: "dummy version".to_string(),
bytecode: vec![BigUintAsHex { value: BigUint::from(1_u8) }],
bytecode_segment_lengths: Some(NestedIntList::Node(vec![
NestedIntList::Leaf(1),
NestedIntList::Leaf(2),
])),
hints: vec![(
4,
vec![Hint::Core(CoreHintBase::Core(CoreHint::AllocSegment {
dst: CellRef { register: Register::AP, offset: 1 },
}))],
)],
pythonic_hints: Some(vec![(5, vec!["dummy pythonic hint".to_string()])]),
entry_points_by_type: CasmContractEntryPoints {
external: casm_contract_entry_points(),
l1_handler: casm_contract_entry_points(),
constructor: casm_contract_entry_points(),
},
}
}

fn central_casm_contract_class_json() -> Value {
let casm_contract_class = casm_contract_class();
let central_casm_contract_class = casm_contract_class_central_format(casm_contract_class);
serde_json::to_value(central_casm_contract_class).unwrap()
}

fn central_casm_contract_class_default_optional_fields_json() -> Value {
let casm_contract_class = CasmContractClass {
bytecode_segment_lengths: None,
pythonic_hints: None,
..casm_contract_class()
};
let central_casm_contract_class = casm_contract_class_central_format(casm_contract_class);
serde_json::to_value(central_casm_contract_class).unwrap()
}

#[rstest]
#[case::state_diff(central_state_diff_json(), CENTRAL_STATE_DIFF_JSON_PATH)]
#[case::invoke_tx(central_invoke_tx_json(), CENTRAL_INVOKE_TX_JSON_PATH)]
Expand All @@ -272,7 +335,15 @@ fn central_sierra_contract_class_json() -> Value {
#[case::bouncer_weights(central_bouncer_weights_json(), CENTRAL_BOUNCER_WEIGHTS_JSON_PATH)]
#[case::sierra_contract_class(
central_sierra_contract_class_json(),
CENTRAL_CONTRACT_CLASS_JSON_PATH
CENTRAL_SIERRA_CONTRACT_CLASS_JSON_PATH
)]
#[case::optionals_are_some(
central_casm_contract_class_json(),
CENTRAL_CASM_CONTRACT_CLASS_JSON_PATH
)]
#[case::optionals_are_none(
central_casm_contract_class_default_optional_fields_json(),
CENTRAL_CASM_CONTRACT_CLASS_DEFAULT_OPTIONALS_JSON_PATH
)]
fn serialize_central_objects(#[case] rust_json: Value, #[case] python_json_path: &str) {
let python_json = read_json_file(python_json_path);
Expand Down

0 comments on commit 1e9cbaf

Please sign in to comment.