Skip to content

Commit

Permalink
Fixed cyclic dependency in linker
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan committed Nov 30, 2023
1 parent 10e3155 commit d129990
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 56 deletions.
20 changes: 20 additions & 0 deletions adapters/core2sn/class.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
18 changes: 9 additions & 9 deletions core/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check failure on line 66 in core/class.go

View workflow job for this annotation

GitHub Actions / lint

func `flatten` is unused (unused)
Expand Down
6 changes: 6 additions & 0 deletions core/class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"reflect"
"testing"

"github.com/NethermindEth/juno/vm"

Check failure on line 10 in core/class_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default (gci)

"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
Expand All @@ -17,6 +19,10 @@ import (
"github.com/stretchr/testify/require"
)

func init() {

Check failure on line 22 in core/class_test.go

View workflow job for this annotation

GitHub Actions / lint

don't use `init` function (gochecknoinits)
core.Cairo0ClassHashFunc = vm.Cairo0ClassHash
}

func TestClassV0Hash(t *testing.T) {
client := feeder.NewTestClient(t, utils.Goerli)

Expand Down
9 changes: 9 additions & 0 deletions utils/zero_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package utils

func NonNilSlice[T any](sl []T) []T {
if sl == nil {
return []T{}
}

return sl
}
56 changes: 9 additions & 47 deletions vm/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"errors"
"unsafe"

"github.com/NethermindEth/juno/adapters/core2sn"

Check failure on line 19 in vm/class.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default (gci)
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/starknet"
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit d129990

Please sign in to comment.