From 7e9652c056e0ef507fe506c146cbf6f5a11cd870 Mon Sep 17 00:00:00 2001 From: Ng Wei Han <47109095+weiihann@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:48:24 +0800 Subject: [PATCH] Move starknet/rust deps to separate package (#2148) Co-authored-by: Mario Apra --- .github/workflows/find-smallest-rust.yml | 4 ++-- .github/workflows/juno-test.yml | 2 +- Makefile | 6 +++--- adapters/p2p2core/class.go | 3 ++- rpc/class.go | 3 ++- starknet/{ => compiler}/compiler.go | 10 ++++++---- starknet/{ => compiler}/compiler_test.go | 11 ++++++----- starknet/{ => compiler}/rust/.gitignore | 0 starknet/{ => compiler}/rust/Cargo.toml | 0 starknet/{ => compiler}/rust/Makefile | 0 starknet/{ => compiler}/rust/src/lib.rs | 0 11 files changed, 22 insertions(+), 17 deletions(-) rename starknet/{ => compiler}/compiler.go (81%) rename starknet/{ => compiler}/compiler_test.go (86%) rename starknet/{ => compiler}/rust/.gitignore (100%) rename starknet/{ => compiler}/rust/Cargo.toml (100%) rename starknet/{ => compiler}/rust/Makefile (100%) rename starknet/{ => compiler}/rust/src/lib.rs (100%) diff --git a/.github/workflows/find-smallest-rust.yml b/.github/workflows/find-smallest-rust.yml index 851758d47a..7ab4b2a148 100644 --- a/.github/workflows/find-smallest-rust.yml +++ b/.github/workflows/find-smallest-rust.yml @@ -8,7 +8,7 @@ on: - .github/workflows/find-smallest-rust.yml - core/rust/* - vm/rust/* - - starknet/rust/* + - starknet/compiler/rust/* workflow_dispatch: concurrency: @@ -24,7 +24,7 @@ jobs: id: rust-version uses: derrix060/detect-rust-minimum-version@v1 with: - paths: core/rust/,vm/rust/,starknet/rust/ + paths: core/rust/,vm/rust/,starknet/compiler/rust/ - name: Send notification on PR uses: actions/github-script@v7 with: diff --git a/.github/workflows/juno-test.yml b/.github/workflows/juno-test.yml index 12ea3486a0..d23f339e0f 100644 --- a/.github/workflows/juno-test.yml +++ b/.github/workflows/juno-test.yml @@ -33,7 +33,7 @@ jobs: workspaces: | vm/rust core/rust - starknet/rust + starknet/compiler/rust - name: Install deps run: make install-deps diff --git a/Makefile b/Makefile index 623b05154a..092be7c2eb 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ core-rust: $(MAKE) -C core/rust $(VM_TARGET) compiler: - $(MAKE) -C starknet/rust $(VM_TARGET) + $(MAKE) -C starknet/compiler/rust $(VM_TARGET) generate: ## generate mkdir -p mocks @@ -94,13 +94,13 @@ tidy: ## add missing and remove unused modules format: ## run go & rust formatters $(MAKE) -C vm/rust format $(MAKE) -C core/rust format - $(MAKE) -C starknet/rust format + $(MAKE) -C starknet/compiler/rust format gofumpt -l -w . clean: ## clean project builds $(MAKE) -C vm/rust clean $(MAKE) -C core/rust clean - $(MAKE) -C starknet/rust clean + $(MAKE) -C starknet/compiler/rust clean @rm -rf ./build help: ## show this help diff --git a/adapters/p2p2core/class.go b/adapters/p2p2core/class.go index 40c5a7bc51..3962787253 100644 --- a/adapters/p2p2core/class.go +++ b/adapters/p2p2core/class.go @@ -10,6 +10,7 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/p2p/starknet/spec" "github.com/NethermindEth/juno/starknet" + "github.com/NethermindEth/juno/starknet/compiler" "github.com/NethermindEth/juno/utils" ) @@ -110,7 +111,7 @@ func createCompiledClass(cairo1 *spec.Cairo1Class) (*core.CompiledClass, error) Version: cairo1.ContractClassVersion, } - compiledClass, err := starknet.Compile(def) + compiledClass, err := compiler.Compile(def) if err != nil { return nil, err } diff --git a/rpc/class.go b/rpc/class.go index b2beee24a2..c51a942500 100644 --- a/rpc/class.go +++ b/rpc/class.go @@ -9,6 +9,7 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/jsonrpc" "github.com/NethermindEth/juno/starknet" + "github.com/NethermindEth/juno/starknet/compiler" "github.com/NethermindEth/juno/utils" ) @@ -49,7 +50,7 @@ func adaptDeclaredClass(declaredClass json.RawMessage) (core.Class, error) { switch { case feederClass.V1 != nil: - compiledClass, cErr := starknet.Compile(feederClass.V1) + compiledClass, cErr := compiler.Compile(feederClass.V1) if cErr != nil { return nil, cErr } diff --git a/starknet/compiler.go b/starknet/compiler/compiler.go similarity index 81% rename from starknet/compiler.go rename to starknet/compiler/compiler.go index 5def4da367..90367788c5 100644 --- a/starknet/compiler.go +++ b/starknet/compiler/compiler.go @@ -1,4 +1,4 @@ -package starknet +package compiler /* #include @@ -19,10 +19,12 @@ import ( "encoding/json" "errors" "unsafe" + + "github.com/NethermindEth/juno/starknet" ) -func Compile(sierra *SierraDefinition) (*CompiledClass, error) { - sierraJSON, err := json.Marshal(SierraDefinition{ +func Compile(sierra *starknet.SierraDefinition) (*starknet.CompiledClass, error) { + sierraJSON, err := json.Marshal(starknet.SierraDefinition{ EntryPoints: sierra.EntryPoints, Program: sierra.Program, Version: sierra.Version, @@ -45,7 +47,7 @@ func Compile(sierra *SierraDefinition) (*CompiledClass, error) { casmJSON := C.GoString(result) - var casmClass CompiledClass + var casmClass starknet.CompiledClass if err := json.Unmarshal([]byte(casmJSON), &casmClass); err != nil { return nil, err } diff --git a/starknet/compiler_test.go b/starknet/compiler/compiler_test.go similarity index 86% rename from starknet/compiler_test.go rename to starknet/compiler/compiler_test.go index 68682d08f2..201150a25f 100644 --- a/starknet/compiler_test.go +++ b/starknet/compiler/compiler_test.go @@ -1,4 +1,4 @@ -package starknet_test +package compiler_test import ( "context" @@ -10,6 +10,7 @@ import ( "github.com/NethermindEth/juno/adapters/sn2core" "github.com/NethermindEth/juno/clients/feeder" "github.com/NethermindEth/juno/starknet" + "github.com/NethermindEth/juno/starknet/compiler" "github.com/NethermindEth/juno/utils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -17,7 +18,7 @@ import ( func TestCompile(t *testing.T) { t.Run("zero sierra", func(t *testing.T) { - _, err := starknet.Compile(&starknet.SierraDefinition{}) + _, err := compiler.Compile(&starknet.SierraDefinition{}) require.Error(t, err) }) @@ -33,7 +34,7 @@ func TestCompile(t *testing.T) { expectedCompiled, err := sn2core.AdaptCompiledClass(compiledDef) require.NoError(t, err) - res, err := starknet.Compile(classDef.V1) + res, err := compiler.Compile(classDef.V1) require.NoError(t, err) gotCompiled, err := sn2core.AdaptCompiledClass(res) @@ -45,7 +46,7 @@ func TestCompile(t *testing.T) { // tests https://github.com/NethermindEth/juno/issues/1748 definition := loadTestData[starknet.SierraDefinition](t, "declare_cairo2_definition.json") - _, err := starknet.Compile(&definition) + _, err := compiler.Compile(&definition) require.NoError(t, err) }) } @@ -54,7 +55,7 @@ func TestCompile(t *testing.T) { func loadTestData[T any](t *testing.T, filename string) T { t.Helper() - file := fmt.Sprintf("./testdata/%s", filename) + file := fmt.Sprintf("../testdata/%s", filename) buff, err := os.ReadFile(file) if err != nil { t.Fatalf("Failed to read file %s: %v", file, err) diff --git a/starknet/rust/.gitignore b/starknet/compiler/rust/.gitignore similarity index 100% rename from starknet/rust/.gitignore rename to starknet/compiler/rust/.gitignore diff --git a/starknet/rust/Cargo.toml b/starknet/compiler/rust/Cargo.toml similarity index 100% rename from starknet/rust/Cargo.toml rename to starknet/compiler/rust/Cargo.toml diff --git a/starknet/rust/Makefile b/starknet/compiler/rust/Makefile similarity index 100% rename from starknet/rust/Makefile rename to starknet/compiler/rust/Makefile diff --git a/starknet/rust/src/lib.rs b/starknet/compiler/rust/src/lib.rs similarity index 100% rename from starknet/rust/src/lib.rs rename to starknet/compiler/rust/src/lib.rs