From d1299904d8eb8a14a18423b7464770243137f2ad Mon Sep 17 00:00:00 2001 From: Kirill Date: Thu, 30 Nov 2023 13:17:02 +0300 Subject: [PATCH] Fixed cyclic dependency in linker --- adapters/core2sn/class.go | 20 ++++++++++++++ core/class.go | 18 ++++++------- core/class_test.go | 6 +++++ utils/zero_value.go | 9 +++++++ vm/class.go | 56 +++++++-------------------------------- 5 files changed, 53 insertions(+), 56 deletions(-) create mode 100644 adapters/core2sn/class.go create mode 100644 utils/zero_value.go diff --git a/adapters/core2sn/class.go b/adapters/core2sn/class.go new file mode 100644 index 0000000000..a345fc40da --- /dev/null +++ b/adapters/core2sn/class.go @@ -0,0 +1,20 @@ +package core2sn + +import ( + "github.com/NethermindEth/juno/core" + "github.com/NethermindEth/juno/starknet" +) + +func AdaptEntryPoint(ep core.EntryPoint) starknet.EntryPoint { + return starknet.EntryPoint{ + Selector: ep.Selector, + Offset: ep.Offset, + } +} + +func AdaptSierraEntryPoint(ep core.SierraEntryPoint) starknet.SierraEntryPoint { + return starknet.SierraEntryPoint{ + Index: ep.Index, + Selector: ep.Selector, + } +} diff --git a/core/class.go b/core/class.go index 465744bbdc..5496b50430 100644 --- a/core/class.go +++ b/core/class.go @@ -51,16 +51,16 @@ func (c *Cairo0Class) Version() uint64 { return 0 } +var Cairo0ClassHashFunc func(*Cairo0Class) (*felt.Felt, error) + func (c *Cairo0Class) Hash() *felt.Felt { - return crypto.PedersenArray( - &felt.Zero, - crypto.PedersenArray(flatten(c.Externals)...), - crypto.PedersenArray(flatten(c.L1Handlers)...), - crypto.PedersenArray(flatten(c.Constructors)...), - c.BuiltinsHash, - c.ProgramHash, - c.BytecodeHash, - ) + classHash, err := Cairo0ClassHashFunc(c) + if err != nil { + fmt.Println("Error: ", err) + return nil + } + + return classHash } func flatten(entryPoints []EntryPoint) []*felt.Felt { diff --git a/core/class_test.go b/core/class_test.go index 5b2af80e5f..eb8b8ad538 100644 --- a/core/class_test.go +++ b/core/class_test.go @@ -7,6 +7,8 @@ import ( "reflect" "testing" + "github.com/NethermindEth/juno/vm" + "github.com/NethermindEth/juno/clients/feeder" "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" @@ -17,6 +19,10 @@ import ( "github.com/stretchr/testify/require" ) +func init() { + core.Cairo0ClassHashFunc = vm.Cairo0ClassHash +} + func TestClassV0Hash(t *testing.T) { client := feeder.NewTestClient(t, utils.Goerli) diff --git a/utils/zero_value.go b/utils/zero_value.go new file mode 100644 index 0000000000..d55cd55061 --- /dev/null +++ b/utils/zero_value.go @@ -0,0 +1,9 @@ +package utils + +func NonNilSlice[T any](sl []T) []T { + if sl == nil { + return []T{} + } + + return sl +} diff --git a/vm/class.go b/vm/class.go index a2a7ccbd27..203cdee51e 100644 --- a/vm/class.go +++ b/vm/class.go @@ -15,6 +15,8 @@ import ( "errors" "unsafe" + "github.com/NethermindEth/juno/adapters/core2sn" + "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/starknet" @@ -59,35 +61,15 @@ func marshalDeclaredClass(class core.Class) (json.RawMessage, error) { } func makeDeprecatedVMClass(class *core.Cairo0Class) (*starknet.Cairo0Definition, error) { + constructors := utils.Map(utils.NonNilSlice(class.Constructors), core2sn.AdaptEntryPoint) + external := utils.Map(utils.NonNilSlice(class.Externals), core2sn.AdaptEntryPoint) + handlers := utils.Map(utils.NonNilSlice(class.L1Handlers), core2sn.AdaptEntryPoint) + decompressedProgram, err := utils.Gzip64Decode(class.Program) if err != nil { return nil, err } - constructors := make([]starknet.EntryPoint, 0, len(class.Constructors)) - for _, entryPoint := range class.Constructors { - constructors = append(constructors, starknet.EntryPoint{ - Selector: entryPoint.Selector, - Offset: entryPoint.Offset, - }) - } - - external := make([]starknet.EntryPoint, 0, len(class.Externals)) - for _, entryPoint := range class.Externals { - external = append(external, starknet.EntryPoint{ - Selector: entryPoint.Selector, - Offset: entryPoint.Offset, - }) - } - - handlers := make([]starknet.EntryPoint, 0, len(class.L1Handlers)) - for _, entryPoint := range class.L1Handlers { - handlers = append(handlers, starknet.EntryPoint{ - Selector: entryPoint.Selector, - Offset: entryPoint.Offset, - }) - } - return &starknet.Cairo0Definition{ Program: decompressedProgram, Abi: class.Abi, @@ -100,29 +82,9 @@ func makeDeprecatedVMClass(class *core.Cairo0Class) (*starknet.Cairo0Definition, } func makeSierraClass(class *core.Cairo1Class) *starknet.SierraDefinition { - constructors := make([]starknet.SierraEntryPoint, 0, len(class.EntryPoints.Constructor)) - for _, entryPoint := range class.EntryPoints.Constructor { - constructors = append(constructors, starknet.SierraEntryPoint{ - Selector: entryPoint.Selector, - Index: entryPoint.Index, - }) - } - - external := make([]starknet.SierraEntryPoint, 0, len(class.EntryPoints.External)) - for _, entryPoint := range class.EntryPoints.External { - external = append(external, starknet.SierraEntryPoint{ - Selector: entryPoint.Selector, - Index: entryPoint.Index, - }) - } - - handlers := make([]starknet.SierraEntryPoint, 0, len(class.EntryPoints.L1Handler)) - for _, entryPoint := range class.EntryPoints.L1Handler { - handlers = append(handlers, starknet.SierraEntryPoint{ - Selector: entryPoint.Selector, - Index: entryPoint.Index, - }) - } + constructors := utils.Map(utils.NonNilSlice(class.EntryPoints.Constructor), core2sn.AdaptSierraEntryPoint) + external := utils.Map(utils.NonNilSlice(class.EntryPoints.External), core2sn.AdaptSierraEntryPoint) + handlers := utils.Map(utils.NonNilSlice(class.EntryPoints.L1Handler), core2sn.AdaptSierraEntryPoint) return &starknet.SierraDefinition{ Version: class.SemanticVersion,