Skip to content

Commit

Permalink
Add tests and fill data for Cairo0
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan committed Oct 23, 2024
1 parent 990fd64 commit 4788f92
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 20 deletions.
3 changes: 1 addition & 2 deletions adapters/core2sn/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core2sn

import (
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/starknet"
"github.com/NethermindEth/juno/utils"
)
Expand All @@ -20,7 +19,7 @@ func AdaptCompiledClass(coreCompiledClass *core.CompiledClass) starknet.Compiled
feederCompiledClass.PythonicHints = coreCompiledClass.PythonicHints
feederCompiledClass.CompilerVersion = coreCompiledClass.CompilerVersion
feederCompiledClass.Hints = coreCompiledClass.Hints
feederCompiledClass.Prime = "0x" + coreCompiledClass.Prime.Text(felt.Base16)
feederCompiledClass.Prime = utils.ToHex(coreCompiledClass.Prime)
feederCompiledClass.BytecodeSegmentLengths = AdaptSegmentLengths(coreCompiledClass.BytecodeSegmentLengths)

adapt := func(ep core.CompiledEntryPoint) starknet.CompiledEntryPoint {
Expand Down
26 changes: 14 additions & 12 deletions rpc/executables.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ type EntryPointsByType struct {
}

type CasmCompiledContractClass struct {
EntryPointsByType EntryPointsByType `json:"entry_points_by_type"`
Prime *felt.Felt `json:"prime"`
CompilerVersion string `json:"compiler_version"`
Bytecode []*felt.Felt `json:"bytecode"`
Hints json.RawMessage `json:"hints"`
BytecodeSegmentLengths []int `json:"bytecode_segment_lengths,omitempty"`
EntryPointsByType EntryPointsByType `json:"entry_points_by_type"`
// can't use felt.Felt here because prime is larger than felt
Prime string `json:"prime"`
CompilerVersion string `json:"compiler_version"`
Bytecode []*felt.Felt `json:"bytecode"`
Hints json.RawMessage `json:"hints"`
BytecodeSegmentLengths []int `json:"bytecode_segment_lengths,omitempty"`
}

func (h *Handler) CompiledCasm(classHash *felt.Felt) (*CasmCompiledContractClass, *jsonrpc.Error) {
Expand Down Expand Up @@ -63,8 +64,9 @@ func adaptCairo0Class(class *core.Cairo0Class) (*CasmCompiledContractClass, erro
}

Check warning on line 64 in rpc/executables.go

View check run for this annotation

Codecov / codecov/patch

rpc/executables.go#L63-L64

Added lines #L63 - L64 were not covered by tests

var cairo0 struct {
Prime *felt.Felt
Data []*felt.Felt
Prime string `json:"prime"`
CompilerVersion string `json:"compiler_version,omitempty"`
Data []*felt.Felt `json:"data"`
}
err = json.Unmarshal(program, &cairo0)
if err != nil {
Expand All @@ -87,9 +89,9 @@ func adaptCairo0Class(class *core.Cairo0Class) (*CasmCompiledContractClass, erro
},
Prime: cairo0.Prime,
Bytecode: cairo0.Data,
CompilerVersion: "",
Hints: nil,
BytecodeSegmentLengths: nil,
CompilerVersion: cairo0.CompilerVersion,
Hints: nil, // todo fill this field
BytecodeSegmentLengths: nil, // Cairo 0 classes don't have this field (it was introduced since Sierra 1.5.0)
}
return result, nil
}
Expand All @@ -109,7 +111,7 @@ func adaptCompiledClass(class *core.CompiledClass) *CasmCompiledContractClass {
External: utils.Map(class.External, adaptEntryPoint),
L1Handler: utils.Map(class.L1Handler, adaptEntryPoint),
},
Prime: new(felt.Felt).SetBigInt(class.Prime),
Prime: utils.ToHex(class.Prime),
CompilerVersion: class.CompilerVersion,
Bytecode: class.Bytecode,
Hints: class.Hints,
Expand Down
92 changes: 92 additions & 0 deletions rpc/executables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package rpc_test

import (
"context"
"encoding/json"
"fmt"
clientFeeder "github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/jsonrpc"
"github.com/NethermindEth/juno/mocks"
"github.com/NethermindEth/juno/rpc"
"github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"testing"
)

func TestCompiledCasm(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

rd := mocks.NewMockReader(mockCtrl)
handler := rpc.New(rd, nil, nil, "", nil)

t.Run("db failure", func(t *testing.T) {
rd.EXPECT().HeadState().Return(nil, nil, fmt.Errorf("error"))
resp, err := handler.CompiledCasm(utils.HexToFelt(t, "0x000"))
assert.Nil(t, resp)
assert.Equal(t, jsonrpc.InternalError, err.Code)
})
t.Run("class doesn't exist", func(t *testing.T) {
classHash := utils.HexToFelt(t, "0x111")

mockState := mocks.NewMockStateHistoryReader(mockCtrl)
mockState.EXPECT().Class(classHash).Return(nil, db.ErrKeyNotFound)
rd.EXPECT().HeadState().Return(mockState, nopCloser, nil)

resp, err := handler.CompiledCasm(classHash)
assert.Nil(t, resp)
assert.Equal(t, jsonrpc.InternalError, err.Code)
})
t.Run("cairo0", func(t *testing.T) {
classHash := utils.HexToFelt(t, "0x7db5c2c2676c2a5bfc892ee4f596b49514e3056a0eee8ad125870b4fb1dd909")

cl := clientFeeder.NewTestClient(t, &utils.Sepolia)
fd := feeder.New(cl)

class, err := fd.Class(context.Background(), classHash)
require.NoError(t, err)

cairo0, ok := class.(*core.Cairo0Class)
require.True(t, ok)
program, err := utils.Gzip64Decode(cairo0.Program)
require.NoError(t, err)

// only fields that need to be unmarshaled specified
var cairo0Definition struct {
Data []*felt.Felt `json:"data"`
}
err = json.Unmarshal(program, &cairo0Definition)
require.NoError(t, err)

mockState := mocks.NewMockStateHistoryReader(mockCtrl)
mockState.EXPECT().Class(classHash).Return(&core.DeclaredClass{Class: class}, nil)
rd.EXPECT().HeadState().Return(mockState, nopCloser, nil)

resp, rpcErr := handler.CompiledCasm(classHash)
require.Nil(t, rpcErr)
assert.Equal(t, &rpc.CasmCompiledContractClass{
Prime: "0x800000000000011000000000000000000000000000000000000000000000001",
CompilerVersion: "0.10.3",
EntryPointsByType: rpc.EntryPointsByType{
Constructor: utils.Map(cairo0.Constructors, adaptEntryPoint),
External: utils.Map(cairo0.Externals, adaptEntryPoint),
L1Handler: utils.Map(cairo0.L1Handlers, adaptEntryPoint),
},
Bytecode: cairo0Definition.Data,
}, resp)
})
}

func adaptEntryPoint(point core.EntryPoint) rpc.CasmEntryPoint {
return rpc.CasmEntryPoint{
Offset: point.Offset,
Selector: point.Selector,
Builtins: nil,
}
}
11 changes: 5 additions & 6 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ func (h *Handler) Methods() ([]jsonrpc.Method, string) { //nolint: funlen
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: h.BlockWithReceipts,
},
{
Name: "starknet_getCompiledCasm",
Params: []jsonrpc.Parameter{{Name: "class_hash"}},
Handler: h.CompiledCasm,
},
}, "/v0_8"
}

Expand Down Expand Up @@ -483,11 +488,5 @@ func (h *Handler) MethodsV0_7() ([]jsonrpc.Method, string) { //nolint: funlen
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: h.BlockWithReceipts,
},
// temporary, todo change it to 0.8 methods
{
Name: "starknet_getCompiledCasm",
Params: []jsonrpc.Parameter{{Name: "class_hash"}},
Handler: h.CompiledCasm,
},
}, "/v0_7"
}
9 changes: 9 additions & 0 deletions utils/bigint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package utils

import (
"math/big"
)

func ToHex(b *big.Int) string {
return "0x" + b.Text(16) //nolint:mnd
}

0 comments on commit 4788f92

Please sign in to comment.