Skip to content

Commit

Permalink
Fix compilation of Cairo 2 classes (#1757)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan authored Mar 22, 2024
1 parent 84a853b commit 47aee92
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 11 deletions.
31 changes: 31 additions & 0 deletions starknet/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package starknet_test

import (
"context"
"encoding/json"
"fmt"
"os"
"testing"

"github.com/NethermindEth/juno/adapters/sn2core"
Expand Down Expand Up @@ -37,4 +40,32 @@ func TestCompile(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, expectedCompiled.Hash(), gotCompiled.Hash())
})

t.Run("declare cairo2 class", func(t *testing.T) {
// tests https://github.com/NethermindEth/juno/issues/1748
definition := loadTestData[starknet.SierraDefinition](t, "declare_cairo2_definition.json")

_, err := starknet.Compile(&definition)
require.NoError(t, err)
})
}

// loadTestData loads json file located relative to a test package and unmarshal it to provided type
func loadTestData[T any](t *testing.T, filename string) T {
t.Helper()

file := fmt.Sprintf("./testdata/%s", filename)
buff, err := os.ReadFile(file)
if err != nil {
t.Fatalf("Failed to read file %s: %v", file, err)
}

var v T
err = json.Unmarshal(buff, &v)
if err != nil {
t.Fatalf("Failed to unmarshal json: %v", err)
}

// todo check for zero value
return v
}
5 changes: 1 addition & 4 deletions starknet/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ edition = "2021"
[dependencies]
serde = "1.0.171"
serde_json = { version = "1.0.96", features = ["raw_value"] }
cairo-lang-starknet = "=2.4.3"
cairo-lang-casm = "=2.4.3"
cairo-lang-utils = "=2.4.3"
cairo-lang-compiler = "=2.4.3"
cairo-lang-starknet-classes = "=2.6.0-rc.1"

[lib]
crate-type = ["staticlib"]
4 changes: 2 additions & 2 deletions starknet/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::ffi::{c_char, CStr, CString};
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;

#[no_mangle]
pub extern "C" fn compileSierraToCasm(sierra_json: *const c_char) -> *mut c_char {
Expand All @@ -11,7 +11,7 @@ pub extern "C" fn compileSierraToCasm(sierra_json: *const c_char) -> *mut c_char
return raw_cstr(e)
}

let casm_class = CasmContractClass::from_contract_class(sierra_class.unwrap(), true)
let casm_class = CasmContractClass::from_contract_class(sierra_class.unwrap(), true, usize::MAX)
.map_err(|err| err.to_string());
if let Err(e) = casm_class {
return raw_cstr(e)
Expand Down
246 changes: 246 additions & 0 deletions starknet/testdata/declare_cairo2_definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
{
"entry_points_by_type": {
"CONSTRUCTOR": [],
"EXTERNAL": [
{
"function_idx": 0,
"selector": "0xf61980aeb34c9c7f823d576c10d00648fdab6c03a59b539ed0824be31da466"
},
{
"function_idx": 1,
"selector": "0x31341177714d81ad9ccd0c903211bc056a60e8af988d0fd918cc43874549653"
}
],
"L1_HANDLER": []
},
"sierra_program": [
"0x1",
"0x5",
"0x0",
"0x2",
"0x6",
"0x0",
"0x97",
"0x69",
"0x18",
"0x52616e6765436865636b",
"0x800000000000000100000000000000000000000000000000",
"0x436f6e7374",
"0x800000000000000000000000000000000000000000000002",
"0x1",
"0xc",
"0x2",
"0x4661696c656420746f20646573657269616c697a6520706172616d202331",
"0x4f7574206f6620676173",
"0x4172726179",
"0x800000000000000300000000000000000000000000000001",
"0x536e617073686f74",
"0x800000000000000700000000000000000000000000000001",
"0x3",
"0x537472756374",
"0x800000000000000700000000000000000000000000000002",
"0x0",
"0x1baeba72e79e9db2587cf44fedb2f3700b2075a5e8e39a562584862c4b71f62",
"0x4",
"0x2ee1e2b1b89f8c495f200e4956278a4d47395fe262f27b52e5865c9524c08c3",
"0x5",
"0x8",
"0x753332",
"0x800000000000000700000000000000000000000000000000",
"0x53746f7261676541646472657373",
"0x53746f726167654261736541646472657373",
"0x456e7465722061206e616d65",
"0x66656c74323532",
"0x4e6f6e5a65726f",
"0x4275696c74696e436f737473",
"0x53797374656d",
"0x800000000000000f00000000000000000000000000000001",
"0x16a4c8d7c05909052238a862d8cc3e7975bf05a07b3a69c6b28951083a6d672",
"0x800000000000000300000000000000000000000000000003",
"0x10",
"0x456e756d",
"0x9931c641b913035ae674b400b61a51476d506bbe8bba2ff8a6272790aba9e6",
"0x6",
"0x11",
"0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473",
"0x800000000000000700000000000000000000000000000003",
"0x11c6d8087e00642489f92d2821ad6ebd6532ad1a3b6d12833da6d6810391511",
"0x14",
"0x426f78",
"0x4761734275696c74696e",
"0x36",
"0x7265766f6b655f61705f747261636b696e67",
"0x77697468647261775f676173",
"0x6272616e63685f616c69676e",
"0x7374727563745f6465636f6e737472756374",
"0x656e61626c655f61705f747261636b696e67",
"0x73746f72655f74656d70",
"0x61727261795f736e617073686f745f706f705f66726f6e74",
"0x756e626f78",
"0x72656e616d65",
"0x656e756d5f696e6974",
"0x15",
"0x6a756d70",
"0x7374727563745f636f6e737472756374",
"0x656e756d5f6d61746368",
"0x64697361626c655f61705f747261636b696e67",
"0x64726f70",
"0x16",
"0x61727261795f6e6577",
"0x636f6e73745f61735f696d6d656469617465",
"0x13",
"0x61727261795f617070656e64",
"0x12",
"0x17",
"0xf",
"0x6765745f6275696c74696e5f636f737473",
"0xe",
"0x77697468647261775f6761735f616c6c",
"0x647570",
"0x66656c743235325f69735f7a65726f",
"0xb",
"0xd",
"0x73746f726167655f626173655f616464726573735f636f6e7374",
"0x361458367e696363fbcc70777d07ebbd2394e89fd0adcaf147faccd1d294d60",
"0x73746f726167655f616464726573735f66726f6d5f62617365",
"0x7",
"0x9",
"0x73746f726167655f77726974655f73797363616c6c",
"0x736e617073686f745f74616b65",
"0x73746f726167655f726561645f73797363616c6c",
"0xde",
"0xffffffffffffffff",
"0x7c",
"0xa",
"0x6c",
"0x27",
"0x19",
"0x1a",
"0x1b",
"0x1c",
"0x1d",
"0x1e",
"0x1f",
"0x20",
"0x5e",
"0x21",
"0x22",
"0x23",
"0x3c",
"0x24",
"0x25",
"0x26",
"0x28",
"0x29",
"0x2a",
"0x55",
"0x2b",
"0x2c",
"0x2d",
"0x2e",
"0x2f",
"0x51",
"0x30",
"0x31",
"0x32",
"0x33",
"0x34",
"0x35",
"0x37",
"0x38",
"0x39",
"0x3a",
"0x3b",
"0x3d",
"0x3e",
"0x3f",
"0x40",
"0x41",
"0x42",
"0x43",
"0x44",
"0x45",
"0x46",
"0x47",
"0x48",
"0x49",
"0x4a",
"0x4b",
"0x4c",
"0x4d",
"0xd0",
"0x9f",
"0xc3",
"0xba",
"0x8a",
"0x80e",
"0x100f13051211100f0e050d0c06050b0a090706050403080706050403020100",
"0x1f181e06050d1d181c1b0706050403181a1819181711071605040315051411",
"0x1411200f28070605040327052605251122240e0523051411220f2111200f02",
"0x1105053411331505053211311130112f2e022d06050d2c2b0506052a112924",
"0x5053b113a3905053413050534110739050738060505370605053606050535",
"0x541060505400605053e3f05053e1305053e113d3905053c0507390507382b",
"0x50534450505340507440507382705053b2305053b06050543060505344205",
"0x4d05053e0e0505344c050541114b0605054a11494805053411474405053446",
"0x5053b0e05053e0e050554115352050534160505345105054111504f07054e",
"0x115611551505053e050505412b05053e070505411107440507382605053b15",
"0x57050e050e1111570511071151260758151307570705110705111157051111",
"0x1107114c054859520757071605261113055705130515111157051113111605",
"0x52055911480557054d0552114d055705060516110605570559055111115705",
"0x27055705114d111157051107111142051106112305570548054c1146055705",
"0x55707230546112305570544054c11460557054c0559114405570527054811",
"0x11231111570511071145055b3f39075707460526111157051107112b055a42",
"0x55705112b11115705420542111157053f05441111570539052711115705",
"0x5e0557051100115d0557055c000745115c0557055c053f115c055705113911",
"0x570515055e111305570513051511600557055f055d115f0557055d5e075c11",
"0x1111570511071160071513130560055705600560110705570507055f111505",
"0x130e6311610557056105621161055705116111115705450527111157051123",
"0x42075705420565111157051113111157051107116665076463620757076115",
"0x57054205421111570511071169056811570767056611620557056205151167",
"0x557056b6a0745116b0557056b053f116b0557051167116a055705112b1111",
"0x116f051106116e0557055b0569116d05570507055f116c05570563055e115b",
"0x116c117105570570055b1170055705116b1111570569056a11115705110711",
"0x7570742717207631570117105570571056e117205570572056d1172055705",
"0x57905711179055705112b111157051123111157051107117877760e757473",
"0x7c0576117c0557052e0574112e0557057b0573111157057a0572117b7a0757",
"0x560117405570574055f117305570573055e1162055705620515117d055705",
"0x570577055f116c05570576055e111157051107117d74736213057d0557057d",
"0x557056e7e075c117e0557051100111157051123116e055705780569116d05",
"0x57056d055f116c0557056c055e116205570562051511800557057f055d117f",
"0x2b1111570542054211115705110711806d6c62130580055705800560116d05",
"0x118205570581680745118105570581053f11810557051177116805570511",
"0x1165055705650515118505570584055d11840557058283075c118305570511",
"0x71185076665130585055705850560110705570507055f116605570566055e",
"0x86055705112b11115705460527111157052b05781111570511231111570511",
"0x890557051100118805570587860745118705570587053f1187055705117911",
"0x570515055e1113055705130515118b0557058a055d118a0557058889075c11",
"0x111157051107118b07151313058b0557058b0560110705570507055f111505",
"0x745118d0557058d053f118d0557051177118c055705112b111157050e057a",
"0x119105570590055d11900557058e8f075c118f0557051100118e0557058d8c",
"0x91055705910560110705570507055f115105570551055e1126055705260515",
"0x57051107115126079215130757070511070511115705111111910751261305",
"0x7114c05935952075707160526111305570513051511160557050e050e1111",
"0x5570511391106055705112b11115705590544111157055205271111570511",
"0x54846075c1146055705110011480557054d060745114d0557054d053f114d",
"0x7055f111505570515055e1113055705130515112705570523055d11230557",
"0x1157054c052711115705110711270715131305270557052705601107055705",
"0x113f3907942b420757074415130e6311440557054405621144055705116111",
"0x5c056d115c055705116c110005570545055b1145055705116b111157051107",
"0x5d0e5707005c072b137b1142055705420515110005570500056e115c055705",
"0x745115f0557055f053f1163055705112b111157051107116261600e955f5e",
"0x11690557056705731111570566057211676607570565057111650557055f63",
"0x5d0557055d055e1142055705420515116b0557056a0576116a055705690574",
"0x1100111157051107116b5e5d4213056b0557056b0560115e0557055e055f11",
"0x5e1142055705420515116d0557056c055d116c055705625b075c115b055705",
"0x1107116d61604213056d0557056d0560116105570561055f11600557056005",
"0x5706e0745117005570570053f11700557051177116e055705112b11115705",
"0x390515117405570573055d11730557057172075c1172055705110011710557",
"0x39130574055705740560110705570507055f113f0557053f055e1139055705",
"0x117705570511771176055705112b111157050e057a1111570511071174073f",
"0x557057879075c11790557051100117805570577760745117705570577053f",
"0x570507055f115105570551055e1126055705260515117b0557057a055d117a",
"0xe07051144464511131546451113077b07512613057b0557057b0560110705",
"0x960e0705114446451113154645111311"
],
"contract_class_version": "0.1.0"
}
5 changes: 0 additions & 5 deletions vm/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@ cached = "0.46.1"
once_cell = "1.18.0"
lazy_static = "1.4.0"

cairo-lang-casm = "=2.6.0-rc.1"
cairo-lang-runner = "=2.6.0-rc.1"
cairo-lang-starknet = "=2.6.0-rc.1"
cairo-lang-utils = "=2.6.0-rc.1"

[lib]
crate-type = ["staticlib"]

0 comments on commit 47aee92

Please sign in to comment.