Skip to content

Commit

Permalink
Add sierra version
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkushinDaniil committed Feb 5, 2025
1 parent 554f59d commit 28c81ec
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 21 deletions.
8 changes: 4 additions & 4 deletions mocks/mock_vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions node/throttled_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func NewThrottledVM(res vm.VM, concurrenyBudget uint, maxQueueLen int32) *Thrott
}

func (tvm *ThrottledVM) Call(callInfo *vm.CallInfo, blockInfo *vm.BlockInfo, state core.StateReader,
network *utils.Network, maxSteps uint64,
network *utils.Network, maxSteps uint64, sierraVersion string,
) ([]*felt.Felt, error) {
var ret []*felt.Felt
return ret, tvm.Do(func(vm *vm.VM) error {
var err error
ret, err = (*vm).Call(callInfo, blockInfo, state, network, maxSteps)
ret, err = (*vm).Call(callInfo, blockInfo, state, network, maxSteps, sierraVersion)

Check warning on line 28 in node/throttled_vm.go

View check run for this annotation

Codecov / codecov/patch

node/throttled_vm.go#L28

Added line #L28 was not covered by tests
return err
})
}
Expand Down
1 change: 1 addition & 0 deletions rpc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestThrottledVMError(t *testing.T) {
mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil)
mockReader.EXPECT().HeadsHeader().Return(new(core.Header), nil)
mockState.EXPECT().ContractClassHash(&felt.Zero).Return(new(felt.Felt), nil)
mockState.EXPECT().Class(new(felt.Felt)).Return(&core.DeclaredClass{Class: &core.Cairo1Class{}}, nil)
_, rpcErr := handler.Call(rpc.FunctionCall{}, rpc.BlockID{Latest: true})
assert.Equal(t, throttledErr, rpcErr.Data)
})
Expand Down
12 changes: 11 additions & 1 deletion rpc/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,16 @@ func (h *Handler) Call(funcCall FunctionCall, id BlockID) ([]*felt.Felt, *jsonrp
return nil, ErrContractNotFound
}

declaredClass, err := state.Class(classHash)
if err != nil {
return nil, ErrClassHashNotFound
}

Check warning on line 388 in rpc/trace.go

View check run for this annotation

Codecov / codecov/patch

rpc/trace.go#L387-L388

Added lines #L387 - L388 were not covered by tests

var sierraVersion string
if class, ok := declaredClass.Class.(*core.Cairo1Class); ok {
sierraVersion = class.SemanticVersion
}

blockHashToBeRevealed, err := h.getRevealedBlockHash(header.Number)
if err != nil {
return nil, ErrInternal.CloneWithData(err)
Expand All @@ -395,7 +405,7 @@ func (h *Handler) Call(funcCall FunctionCall, id BlockID) ([]*felt.Felt, *jsonrp
}, &vm.BlockInfo{
Header: header,
BlockHashToBeRevealed: blockHashToBeRevealed,
}, state, h.bcReader.Network(), h.callMaxSteps)
}, state, h.bcReader.Network(), h.callMaxSteps, sierraVersion)
if err != nil {
if errors.Is(err, utils.ErrResourceBusy) {
return nil, ErrInternal.CloneWithData(throttledVMErr)
Expand Down
3 changes: 2 additions & 1 deletion rpc/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,14 @@ func TestCall(t *testing.T) {
mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil)
mockReader.EXPECT().HeadsHeader().Return(headsHeader, nil)
mockState.EXPECT().ContractClassHash(contractAddr).Return(classHash, nil)
mockState.EXPECT().Class(classHash).Return(&core.DeclaredClass{Class: &core.Cairo1Class{}}, nil)
mockReader.EXPECT().Network().Return(n)
mockVM.EXPECT().Call(&vm.CallInfo{
ContractAddress: contractAddr,
ClassHash: classHash,
Selector: selector,
Calldata: calldata,
}, &vm.BlockInfo{Header: headsHeader}, gomock.Any(), &utils.Mainnet, uint64(1337)).Return(expectedRes, nil)
}, &vm.BlockInfo{Header: headsHeader}, gomock.Any(), &utils.Mainnet, uint64(1337), "").Return(expectedRes, nil)

res, rpcErr := handler.Call(rpc.FunctionCall{
ContractAddress: *contractAddr,
Expand Down
17 changes: 12 additions & 5 deletions vm/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use juno_state_reader::{class_info_from_json_str, felt_to_byte_array};
use serde::Deserialize;
use starknet_api::{
block::{BlockHash, GasPrice},
contract_class::{ClassInfo, EntryPointType},
contract_class::{ClassInfo, EntryPointType, SierraVersion},
core::PatriciaKey,
executable_transaction::AccountTransaction,
execution_resources::GasVector,
Expand Down Expand Up @@ -116,6 +116,7 @@ pub extern "C" fn cairoVMCall(
chain_id: *const c_char,
max_steps: c_ulonglong,
concurrency_mode: c_uchar,
sierra_version: *const c_char,
) {
let block_info = unsafe { *block_info_ptr };
let call_info = unsafe { *call_info_ptr };
Expand All @@ -140,10 +141,16 @@ pub extern "C" fn cairoVMCall(
}
}

// TODO: initial gas should be different based on the execution method used: vm or native!
// There should be some check.
let initial_gas = get_versioned_constants(block_info.version).infinite_gas_for_vm_mode();

let version_constants = get_versioned_constants(block_info.version);
let sierra_version_str = unsafe { CStr::from_ptr(sierra_version) }.to_str().unwrap();
let sierra_version =
SierraVersion::from_str(sierra_version_str).unwrap_or(SierraVersion::DEPRECATED);
let initial_gas: u64;
if sierra_version < SierraVersion::new(1, 7, 0) {
initial_gas = version_constants.infinite_gas_for_vm_mode();
} else {
initial_gas = version_constants.os_constants.validate_max_sierra_gas.0;
}
let contract_address =
starknet_api::core::ContractAddress(PatriciaKey::try_from(contract_addr_felt).unwrap());

Expand Down
7 changes: 5 additions & 2 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ExecutionResults 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) ([]*felt.Felt, error)
maxSteps uint64, sierraVersion string) ([]*felt.Felt, error)
Execute(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt, blockInfo *BlockInfo,
state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert bool,
) (ExecutionResults, error)
Expand Down Expand Up @@ -206,7 +206,7 @@ func makeCBlockInfo(blockInfo *BlockInfo) C.BlockInfo {
}

func (v *vm) Call(callInfo *CallInfo, blockInfo *BlockInfo, state core.StateReader,
network *utils.Network, maxSteps uint64,
network *utils.Network, maxSteps uint64, sierraVersion string,
) ([]*felt.Felt, error) {
context := &callContext{
state: state,
Expand All @@ -225,17 +225,20 @@ func (v *vm) Call(callInfo *CallInfo, blockInfo *BlockInfo, state core.StateRead
cCallInfo, callInfoPinner := makeCCallInfo(callInfo)
cBlockInfo := makeCBlockInfo(blockInfo)
chainID := C.CString(network.L2ChainID)
cSierraVersion := C.CString(sierraVersion)
C.cairoVMCall(
&cCallInfo,
&cBlockInfo,
C.uintptr_t(handle),
chainID,
C.ulonglong(maxSteps), //nolint:gocritic
C.uchar(concurrencyModeByte), //nolint:gocritic
cSierraVersion, //nolint:gocritic
)
callInfoPinner.Unpin()
C.free(unsafe.Pointer(chainID))
C.free(unsafe.Pointer(cBlockInfo.version))
C.free(unsafe.Pointer(cSierraVersion))

if context.err != "" {
return nil, errors.New(context.err)
Expand Down
2 changes: 1 addition & 1 deletion vm/vm_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ typedef struct BlockInfo {
} BlockInfo;

extern void cairoVMCall(CallInfo* call_info_ptr, BlockInfo* block_info_ptr, uintptr_t readerHandle, char* chain_id,
unsigned long long max_steps, unsigned char concurrency_mode);
unsigned long long max_steps, unsigned char concurrency_mode, char* sierra_version);

extern void cairoVMExecute(char* txns_json, char* classes_json, char* paid_fees_on_l1_json,
BlockInfo* block_info_ptr, uintptr_t readerHandle, char* chain_id,
Expand Down
10 changes: 5 additions & 5 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestV0Call(t *testing.T) {
ContractAddress: contractAddr,
ClassHash: classHash,
Selector: entryPoint,
}, &BlockInfo{Header: &core.Header{}}, testState, &utils.Mainnet, 1_000_000)
}, &BlockInfo{Header: &core.Header{}}, testState, &utils.Mainnet, 1_000_000, "")
require.NoError(t, err)
assert.Equal(t, []*felt.Felt{&felt.Zero}, ret)

Expand All @@ -70,7 +70,7 @@ func TestV0Call(t *testing.T) {
ContractAddress: contractAddr,
ClassHash: classHash,
Selector: entryPoint,
}, &BlockInfo{Header: &core.Header{Number: 1}}, testState, &utils.Mainnet, 1_000_000)
}, &BlockInfo{Header: &core.Header{Number: 1}}, testState, &utils.Mainnet, 1_000_000, "")
require.NoError(t, err)
assert.Equal(t, []*felt.Felt{new(felt.Felt).SetUint64(1337)}, ret)
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestV1Call(t *testing.T) {
Calldata: []felt.Felt{
*storageLocation,
},
}, &BlockInfo{Header: &core.Header{}}, testState, &utils.Goerli, 1_000_000)
}, &BlockInfo{Header: &core.Header{}}, testState, &utils.Goerli, 1_000_000, "")
require.NoError(t, err)
assert.Equal(t, []*felt.Felt{&felt.Zero}, ret)

Expand All @@ -139,7 +139,7 @@ func TestV1Call(t *testing.T) {
Calldata: []felt.Felt{
*storageLocation,
},
}, &BlockInfo{Header: &core.Header{Number: 1}}, testState, &utils.Goerli, 1_000_000)
}, &BlockInfo{Header: &core.Header{Number: 1}}, testState, &utils.Goerli, 1_000_000, "")
require.NoError(t, err)
assert.Equal(t, []*felt.Felt{new(felt.Felt).SetUint64(37)}, ret)
}
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestCall_MaxSteps(t *testing.T) {
ContractAddress: contractAddr,
ClassHash: classHash,
Selector: entryPoint,
}, &BlockInfo{Header: &core.Header{}}, testState, &utils.Mainnet, 0)
}, &BlockInfo{Header: &core.Header{}}, testState, &utils.Mainnet, 0, "")
assert.ErrorContains(t, err, "RunResources has no remaining steps")
}

Expand Down

0 comments on commit 28c81ec

Please sign in to comment.