Skip to content

Commit

Permalink
Revert "Implement casm class hash calculation (#1154)" (#1474)
Browse files Browse the repository at this point in the history
This reverts commit 9bac881.
  • Loading branch information
kirugan authored Nov 28, 2023
1 parent 9bac881 commit 9448c9f
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 298 deletions.
1 change: 1 addition & 0 deletions adapters/core2p2p/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func AdaptClass(class core.Class, compiledHash *felt.Felt) *spec.Class {
case *core.Cairo1Class:
return &spec.Class{
CompiledHash: AdaptHash(compiledHash),
Definition: v.Compiled,
}
default:
panic(fmt.Errorf("unsupported cairo class %T (version=%d)", v, class.Version()))
Expand Down
123 changes: 0 additions & 123 deletions adapters/core2sn/core2sn.go

This file was deleted.

48 changes: 8 additions & 40 deletions adapters/sn2core/sn2core.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package sn2core

import (
"encoding/json"
"errors"
"fmt"
"math/big"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/crypto"
Expand Down Expand Up @@ -244,7 +244,7 @@ func AdaptDeployAccountTransaction(t *starknet.Transaction) *core.DeployAccountT
}
}

func AdaptCairo1Class(response *starknet.SierraDefinition, compiledClass *starknet.CompiledClass) (*core.Cairo1Class, error) {
func AdaptCairo1Class(response *starknet.SierraDefinition, compiledClass json.RawMessage) (core.Class, error) {
var err error

class := new(core.Cairo1Class)
Expand Down Expand Up @@ -272,52 +272,28 @@ func AdaptCairo1Class(response *starknet.SierraDefinition, compiledClass *starkn
}

if compiledClass != nil {
adaptedCompiledClass, err := AdaptCompiledClass(compiledClass)
if err != nil {
if err = json.Unmarshal(compiledClass, &class.Compiled); err != nil {
return nil, err
}
class.Compiled = *adaptedCompiledClass
}

return class, nil
}

func AdaptCompiledClass(compiledClass *starknet.CompiledClass) (*core.CompiledClass, error) {
compiled := new(core.CompiledClass)
compiled.Bytecode = compiledClass.Bytecode
compiled.PythonicHints = compiledClass.PythonicHints
compiled.CompilerVersion = compiledClass.CompilerVersion
compiled.Hints = compiledClass.Hints

var ok bool
compiled.Prime, ok = new(big.Int).SetString(compiledClass.Prime, 0)
if !ok {
return nil, fmt.Errorf("couldn't convert prime value to big.Int: %d", compiled.Prime)
}

entryPoints := compiledClass.EntryPoints
compiled.External = utils.Map(entryPoints.External, adaptCompiledEntryPoint)
compiled.L1Handler = utils.Map(entryPoints.L1Handler, adaptCompiledEntryPoint)
compiled.Constructor = utils.Map(entryPoints.Constructor, adaptCompiledEntryPoint)

return compiled, nil
}

func AdaptCairo0Class(response *starknet.Cairo0Definition) (core.Class, error) {
class := new(core.Cairo0Class)
class.Abi = response.Abi

class.Externals = make([]core.EntryPoint, 0, len(response.EntryPoints.External))
class.Externals = []core.EntryPoint{}
for _, v := range response.EntryPoints.External {
class.Externals = append(class.Externals, core.EntryPoint{Selector: v.Selector, Offset: v.Offset})
}

class.L1Handlers = make([]core.EntryPoint, 0, len(response.EntryPoints.L1Handler))
class.L1Handlers = []core.EntryPoint{}
for _, v := range response.EntryPoints.L1Handler {
class.L1Handlers = append(class.L1Handlers, core.EntryPoint{Selector: v.Selector, Offset: v.Offset})
}

class.Constructors = make([]core.EntryPoint, 0, len(response.EntryPoints.Constructor))
class.Constructors = []core.EntryPoint{}
for _, v := range response.EntryPoints.Constructor {
class.Constructors = append(class.Constructors, core.EntryPoint{Selector: v.Selector, Offset: v.Offset})
}
Expand Down Expand Up @@ -359,7 +335,7 @@ func AdaptStateUpdate(response *starknet.StateUpdate) (*core.StateUpdate, error)
}
}

stateDiff.Nonces = make(map[felt.Felt]*felt.Felt, len(response.StateDiff.Nonces))
stateDiff.Nonces = make(map[felt.Felt]*felt.Felt)
for addrStr, nonce := range response.StateDiff.Nonces {
addr, err := new(felt.Felt).SetString(addrStr)
if err != nil {
Expand All @@ -368,7 +344,7 @@ func AdaptStateUpdate(response *starknet.StateUpdate) (*core.StateUpdate, error)
stateDiff.Nonces[*addr] = nonce
}

stateDiff.StorageDiffs = make(map[felt.Felt][]core.StorageDiff, len(response.StateDiff.StorageDiffs))
stateDiff.StorageDiffs = make(map[felt.Felt][]core.StorageDiff)
for addrStr, diffs := range response.StateDiff.StorageDiffs {
addr, err := new(felt.Felt).SetString(addrStr)
if err != nil {
Expand Down Expand Up @@ -398,11 +374,3 @@ func safeFeltToUint64(f *felt.Felt) uint64 {
}
return 0
}

func adaptCompiledEntryPoint(entryPoint starknet.CompiledEntryPoint) core.CompiledEntryPoint {
return core.CompiledEntryPoint{
Offset: entryPoint.Offset,
Selector: entryPoint.Selector,
Builtins: entryPoint.Builtins,
}
}
11 changes: 5 additions & 6 deletions adapters/sn2core/sn2core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,17 +481,16 @@ func TestClassV1(t *testing.T) {
compiled, err := client.CompiledClassDefinition(context.Background(), classHash)
require.NoError(t, err)

v1Class, err := sn2core.AdaptCairo1Class(feederClass.V1, compiled)
class, err := sn2core.AdaptCairo1Class(feederClass.V1, compiled)
require.NoError(t, err)

v1Class, ok := class.(*core.Cairo1Class)
require.True(t, ok)

assert.Equal(t, feederClass.V1.Abi, v1Class.Abi)
assert.Equal(t, feederClass.V1.Program, v1Class.Program)
assert.Equal(t, feederClass.V1.Version, v1Class.SemanticVersion)
assert.Equal(t, compiled.Prime, "0x"+v1Class.Compiled.Prime.Text(felt.Base16))
assert.Equal(t, compiled.Bytecode, v1Class.Compiled.Bytecode)
assert.Equal(t, compiled.Hints, v1Class.Compiled.Hints)
assert.Equal(t, compiled.CompilerVersion, v1Class.Compiled.CompilerVersion)
assert.Equal(t, len(compiled.EntryPoints.External), len(v1Class.Compiled.External))
assert.Equal(t, compiled, v1Class.Compiled)

assert.Equal(t, len(feederClass.V1.EntryPoints.External), len(v1Class.EntryPoints.External))
for i, v := range feederClass.V1.EntryPoints.External {
Expand Down
6 changes: 3 additions & 3 deletions clients/feeder/feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (c *Client) ClassDefinition(ctx context.Context, classHash *felt.Felt) (*st
return class, nil
}

func (c *Client) CompiledClassDefinition(ctx context.Context, classHash *felt.Felt) (*starknet.CompiledClass, error) {
func (c *Client) CompiledClassDefinition(ctx context.Context, classHash *felt.Felt) (json.RawMessage, error) {
queryURL := c.buildQueryString("get_compiled_class_by_class_hash", map[string]string{
"classHash": classHash.String(),
})
Expand All @@ -332,8 +332,8 @@ func (c *Client) CompiledClassDefinition(ctx context.Context, classHash *felt.Fe
}
defer body.Close()

class := new(starknet.CompiledClass)
if err = json.NewDecoder(body).Decode(class); err != nil {
var class json.RawMessage
if err = json.NewDecoder(body).Decode(&class); err != nil {
return nil, err
}
return class, nil
Expand Down
9 changes: 2 additions & 7 deletions clients/feeder/feeder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package feeder_test

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
Expand Down Expand Up @@ -537,13 +538,7 @@ func TestCompiledClassDefinition(t *testing.T) {
classHash := utils.HexToFelt(t, "0x1cd2edfb485241c4403254d550de0a097fa76743cd30696f714a491a454bad5")
class, err := client.CompiledClassDefinition(context.Background(), classHash)
require.NoError(t, err)
assert.Equal(t, "1.0.0", class.CompilerVersion)
assert.Equal(t, "0x800000000000011000000000000000000000000000000000000000000000001", class.Prime)
assert.Equal(t, 3900, len(class.Bytecode))
assert.Equal(t, 10, len(class.EntryPoints.External))
assert.Equal(t, 1, len(class.EntryPoints.External[9].Builtins))
assert.Equal(t, "range_check", class.EntryPoints.External[9].Builtins[0])
assert.Equal(t, "0x3604cea1cdb094a73a31144f14a3e5861613c008e1e879939ebc4827d10cd50", class.EntryPoints.External[9].Selector.String())
require.True(t, json.Valid(class))
}

func TestTransactionStatusRevertError(t *testing.T) {
Expand Down

This file was deleted.

This file was deleted.

49 changes: 1 addition & 48 deletions core/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package core
import (
"encoding/json"
"fmt"
"math/big"

"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
Expand Down Expand Up @@ -57,24 +56,7 @@ type Cairo1Class struct {
Program []*felt.Felt
ProgramHash *felt.Felt
SemanticVersion string
Compiled CompiledClass
}

type CompiledClass struct {
Bytecode []*felt.Felt
PythonicHints json.RawMessage
CompilerVersion string
Hints json.RawMessage
Prime *big.Int
External []CompiledEntryPoint
L1Handler []CompiledEntryPoint
Constructor []CompiledEntryPoint
}

type CompiledEntryPoint struct {
Offset uint64
Builtins []string
Selector *felt.Felt
Compiled json.RawMessage
}

type SierraEntryPoint struct {
Expand All @@ -97,18 +79,6 @@ func (c *Cairo1Class) Hash() *felt.Felt {
)
}

var compiledClassV1Prefix = new(felt.Felt).SetBytes([]byte("COMPILED_CLASS_V1"))

func (c *CompiledClass) Hash() *felt.Felt {
return crypto.PoseidonArray(
compiledClassV1Prefix,
crypto.PoseidonArray(flattenCompiledEntryPoints(c.External)...),
crypto.PoseidonArray(flattenCompiledEntryPoints(c.L1Handler)...),
crypto.PoseidonArray(flattenCompiledEntryPoints(c.Constructor)...),
crypto.PoseidonArray(c.Bytecode...),
)
}

func flattenSierraEntryPoints(entryPoints []SierraEntryPoint) []*felt.Felt {
result := make([]*felt.Felt, len(entryPoints)*2)
for i, entryPoint := range entryPoints {
Expand All @@ -120,23 +90,6 @@ func flattenSierraEntryPoints(entryPoints []SierraEntryPoint) []*felt.Felt {
return result
}

func flattenCompiledEntryPoints(entryPoints []CompiledEntryPoint) []*felt.Felt {
result := make([]*felt.Felt, len(entryPoints)*3)
for i, entryPoint := range entryPoints {
// It is important that Selector is first, then Offset is second because the order
// influences the class hash.
result[3*i] = entryPoint.Selector
result[3*i+1] = new(felt.Felt).SetUint64(entryPoint.Offset)
builtins := make([]*felt.Felt, len(entryPoint.Builtins))
for idx, buil := range entryPoint.Builtins {
builtins[idx] = new(felt.Felt).SetBytes([]byte(buil))
}
result[3*i+2] = crypto.PoseidonArray(builtins...)
}

return result
}

func VerifyClassHashes(classes map[felt.Felt]Class) error {
for hash, class := range classes {
cairo1Class, ok := class.(*Cairo1Class)
Expand Down
Loading

0 comments on commit 9448c9f

Please sign in to comment.