Skip to content

Commit

Permalink
refactor(vm): modify some naming and structure
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-pino committed Feb 28, 2025
1 parent b265910 commit 72ac58c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 54 deletions.
18 changes: 9 additions & 9 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type CallResult struct {
//go:generate mockgen -destination=../mocks/mock_vm.go -package=mocks github.com/NethermindEth/juno/vm VM
type VM interface {
Call(callInfo *CallInfo, blockInfo *BlockInfo, state core.StateReader, network *utils.Network,
maxSteps uint64, sierraVersion string, errStack bool) (CallResult, error)
maxSteps uint64, sierraVersion string, structuredErrStack bool) (CallResult, error)
Execute(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt, blockInfo *BlockInfo,
state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert, errStack bool,
) (ExecutionResults, error)
Expand Down Expand Up @@ -213,7 +213,7 @@ func makeCBlockInfo(blockInfo *BlockInfo) C.BlockInfo {
}

func (v *vm) Call(callInfo *CallInfo, blockInfo *BlockInfo, state core.StateReader,
network *utils.Network, maxSteps uint64, sierraVersion string, errStack bool,
network *utils.Network, maxSteps uint64, sierraVersion string, structuredErrStack bool,
) (CallResult, error) {
context := &callContext{
state: state,
Expand All @@ -227,9 +227,9 @@ func (v *vm) Call(callInfo *CallInfo, blockInfo *BlockInfo, state core.StateRead
if v.concurrencyMode {
concurrencyModeByte = 1
}
var errorStackByte byte
if errStack {
errorStackByte = 1
var structuredErrStackByte byte
if structuredErrStack {
structuredErrStackByte = 1
}
C.setVersionedConstants(C.CString("my_json"))

Expand All @@ -242,10 +242,10 @@ func (v *vm) Call(callInfo *CallInfo, blockInfo *BlockInfo, state core.StateRead
&cBlockInfo,
C.uintptr_t(handle),
chainID,
C.ulonglong(maxSteps), //nolint:gocritic
C.uchar(concurrencyModeByte), //nolint:gocritic
cSierraVersion, //nolint:gocritic
C.uchar(errorStackByte), //nolint:gocritic
C.ulonglong(maxSteps), //nolint:gocritic
C.uchar(concurrencyModeByte), //nolint:gocritic
cSierraVersion, //nolint:gocritic
C.uchar(structuredErrStackByte), //nolint:gocritic
)
callInfoPinner.Unpin()
C.free(unsafe.Pointer(chainID))
Expand Down
90 changes: 45 additions & 45 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
"github.com/stretchr/testify/require"
)

func TestV0Call(t *testing.T) {
func TestCallDeprecatedCairo(t *testing.T) {
testDB := pebble.NewMemTest(t)
txn, err := testDB.NewTransaction(true)
require.NoError(t, err)
client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)
t.Cleanup(func() {
require.NoError(t, txn.Discard())
})
client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)

contractAddr := utils.HexToFelt(t, "0xDEADBEEF")
// https://voyager.online/class/0x03297a93c52357144b7da71296d7e8231c3e0959f0a1d37222204f2f7712010e
Expand Down Expand Up @@ -76,15 +76,54 @@ func TestV0Call(t *testing.T) {
assert.Equal(t, []*felt.Felt{new(felt.Felt).SetUint64(1337)}, ret.Result)
}

func TestV1Call(t *testing.T) {
func TestCallDeprecatedCairoMaxSteps(t *testing.T) {
testDB := pebble.NewMemTest(t)
txn, err := testDB.NewTransaction(true)
require.NoError(t, err)
client := feeder.NewTestClient(t, &utils.Goerli)
t.Cleanup(func() {
require.NoError(t, txn.Discard())
})
client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)

contractAddr := utils.HexToFelt(t, "0xDEADBEEF")
// https://voyager.online/class/0x03297a93c52357144b7da71296d7e8231c3e0959f0a1d37222204f2f7712010e
classHash := utils.HexToFelt(t, "0x3297a93c52357144b7da71296d7e8231c3e0959f0a1d37222204f2f7712010e")
simpleClass, err := gw.Class(context.Background(), classHash)
require.NoError(t, err)

testState := core.NewState(txn)
require.NoError(t, testState.Update(0, &core.StateUpdate{
OldRoot: &felt.Zero,
NewRoot: utils.HexToFelt(t, "0x3d452fbb3c3a32fe85b1a3fbbcdec316d5fc940cefc028ee808ad25a15991c8"),
StateDiff: &core.StateDiff{
DeployedContracts: map[felt.Felt]*felt.Felt{
*contractAddr: classHash,
},
},
}, map[felt.Felt]core.Class{
*classHash: simpleClass,
}))

entryPoint := utils.HexToFelt(t, "0x39e11d48192e4333233c7eb19d10ad67c362bb28580c604d67884c85da39695")

_, err = New(false, nil).Call(&CallInfo{
ContractAddress: contractAddr,
ClassHash: classHash,
Selector: entryPoint,
}, &BlockInfo{Header: &core.Header{}}, testState, &utils.Mainnet, 0, simpleClass.SierraVersion(), false)
assert.ErrorContains(t, err, "RunResources has no remaining steps")
}

func TestCallCairo(t *testing.T) {
testDB := pebble.NewMemTest(t)
txn, err := testDB.NewTransaction(true)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, txn.Discard())
})
client := feeder.NewTestClient(t, &utils.Goerli)
gw := adaptfeeder.New(client)

contractAddr := utils.HexToFelt(t, "0xDEADBEEF")
// https://goerli.voyager.online/class/0x01338d85d3e579f6944ba06c005238d145920afeb32f94e3a1e234d21e1e9292
Expand Down Expand Up @@ -145,54 +184,15 @@ func TestV1Call(t *testing.T) {
assert.Equal(t, []*felt.Felt{new(felt.Felt).SetUint64(37)}, ret.Result)
}

func TestCall_MaxSteps(t *testing.T) {
func TestCallInfoErrorHandling(t *testing.T) {
testDB := pebble.NewMemTest(t)
txn, err := testDB.NewTransaction(true)
require.NoError(t, err)
client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)
t.Cleanup(func() {
require.NoError(t, txn.Discard())
})

contractAddr := utils.HexToFelt(t, "0xDEADBEEF")
// https://voyager.online/class/0x03297a93c52357144b7da71296d7e8231c3e0959f0a1d37222204f2f7712010e
classHash := utils.HexToFelt(t, "0x3297a93c52357144b7da71296d7e8231c3e0959f0a1d37222204f2f7712010e")
simpleClass, err := gw.Class(context.Background(), classHash)
require.NoError(t, err)

testState := core.NewState(txn)
require.NoError(t, testState.Update(0, &core.StateUpdate{
OldRoot: &felt.Zero,
NewRoot: utils.HexToFelt(t, "0x3d452fbb3c3a32fe85b1a3fbbcdec316d5fc940cefc028ee808ad25a15991c8"),
StateDiff: &core.StateDiff{
DeployedContracts: map[felt.Felt]*felt.Felt{
*contractAddr: classHash,
},
},
}, map[felt.Felt]core.Class{
*classHash: simpleClass,
}))

entryPoint := utils.HexToFelt(t, "0x39e11d48192e4333233c7eb19d10ad67c362bb28580c604d67884c85da39695")

_, err = New(false, nil).Call(&CallInfo{
ContractAddress: contractAddr,
ClassHash: classHash,
Selector: entryPoint,
}, &BlockInfo{Header: &core.Header{}}, testState, &utils.Mainnet, 0, simpleClass.SierraVersion(), false)
assert.ErrorContains(t, err, "RunResources has no remaining steps")
}

func TestCallInfoErrorHandling(t *testing.T) {
testDB := pebble.NewMemTest(t)
txn, err := testDB.NewTransaction(true)
require.NoError(t, err)
client := feeder.NewTestClient(t, &utils.Sepolia)
gw := adaptfeeder.New(client)
t.Cleanup(func() {
require.NoError(t, txn.Discard())
})

contractAddr := utils.HexToFelt(t, "0x123")
classHash := utils.HexToFelt(t, "0x5f18f9cdc05da87f04e8e7685bd346fc029f977167d5b1b2b59f69a7dacbfc8")
Expand Down

0 comments on commit 72ac58c

Please sign in to comment.