Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions builder/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (e *executor) RunTxns(state *BuildState, txns []mempool.BroadcastedTransact
}

// Execute the transaction
vmResults, err := e.vm.Execute(
vmResults, err := e.vm.BuildBlock(
coreTxns,
declaredClasses,
paidFeesOnL1,
Expand All @@ -89,13 +89,10 @@ func (e *executor) RunTxns(state *BuildState, txns []mempool.BroadcastedTransact
BlockHashToBeRevealed: state.RevealedBlockHash,
},
stateWriter,
e.disableFees,
e.skipValidate,
false,
true,
false,
false,
false,
vm.BuildBlockOptions{
SkipChargeFee: e.disableFees,
SkipValidate: e.skipValidate,
},
)
if err != nil {
return err
Expand Down
13 changes: 11 additions & 2 deletions genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,17 @@ func executeTransactions(
}

blockInfo := vm.BlockInfo{Header: &genesisHeader}
executionResults, err := v.Execute(coreTxns, nil, []*felt.Felt{new(felt.Felt).SetUint64(1)},
&blockInfo, genesisState, true, true, true, true, false, false, false)
executionResults, err := v.BuildBlock(
coreTxns,
nil,
[]*felt.Felt{felt.NewFromUint64[felt.Felt](1)},
&blockInfo,
genesisState,
vm.BuildBlockOptions{
SkipChargeFee: true,
SkipValidate: true,
ErrOnRevert: true,
})
if err != nil {
return fmt.Errorf("execute transactions: %v", err)
}
Expand Down
57 changes: 51 additions & 6 deletions mocks/mock_vm.go

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

80 changes: 54 additions & 26 deletions node/throttled_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,65 @@ func (tvm *ThrottledVM) Call(
})
}

func (tvm *ThrottledVM) Execute(
func (tvm *ThrottledVM) runExec(
fn func(inner vm.VM) (vm.ExecutionResults, error),
) (vm.ExecutionResults, error) {
var result vm.ExecutionResults
return result, tvm.Do(func(inner *vm.VM) error {
var err error
result, err = fn(*inner)
return err
})
}

func (tvm *ThrottledVM) Simulate(
txns []core.Transaction,
declaredClasses []core.ClassDefinition,
paidFeesOnL1 []*felt.Felt,
blockInfo *vm.BlockInfo,
state core.StateReader,
skipChargeFee,
skipValidate,
errOnRevert,
errStack,
allowBinarySearch bool,
isEstimateFee bool,
returnInitialReads bool,
opts vm.SimulateOptions,
) (vm.ExecutionResults, error) {
var executionResult vm.ExecutionResults
return executionResult, tvm.Do(func(vm *vm.VM) error {
var err error
executionResult, err = (*vm).Execute(
txns,
declaredClasses,
paidFeesOnL1,
blockInfo,
state,
skipChargeFee,
skipValidate,
errOnRevert,
errStack,
allowBinarySearch,
isEstimateFee,
returnInitialReads,
)
return err
return tvm.runExec(func(inner vm.VM) (vm.ExecutionResults, error) {
return inner.Simulate(txns, declaredClasses, paidFeesOnL1, blockInfo, state, opts)
})
}

func (tvm *ThrottledVM) EstimateFee(
txns []core.Transaction,
declaredClasses []core.ClassDefinition,
paidFeesOnL1 []*felt.Felt,
blockInfo *vm.BlockInfo,
state core.StateReader,
opts vm.EstimateFeeOptions,
) (vm.ExecutionResults, error) {
return tvm.runExec(func(inner vm.VM) (vm.ExecutionResults, error) {
return inner.EstimateFee(txns, declaredClasses, paidFeesOnL1, blockInfo, state, opts)
})
}

func (tvm *ThrottledVM) Trace(
txns []core.Transaction,
declaredClasses []core.ClassDefinition,
paidFeesOnL1 []*felt.Felt,
blockInfo *vm.BlockInfo,
state core.StateReader,
opts vm.TraceOptions,
) (vm.ExecutionResults, error) {
return tvm.runExec(func(inner vm.VM) (vm.ExecutionResults, error) {
return inner.Trace(txns, declaredClasses, paidFeesOnL1, blockInfo, state, opts)
})
}

func (tvm *ThrottledVM) BuildBlock(
txns []core.Transaction,
declaredClasses []core.ClassDefinition,
paidFeesOnL1 []*felt.Felt,
blockInfo *vm.BlockInfo,
state core.StateReader,
opts vm.BuildBlockOptions,
) (vm.ExecutionResults, error) {
return tvm.runExec(func(inner vm.VM) (vm.ExecutionResults, error) {
return inner.BuildBlock(txns, declaredClasses, paidFeesOnL1, blockInfo, state, opts)
})
}
4 changes: 2 additions & 2 deletions rpc/v10/estimate_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (h *Handler) EstimateFee(
estimateFlags []EstimateFlag,
id *BlockID,
) ([]FeeEstimate, http.Header, *jsonrpc.Error) {
simulationFlags := make([]SimulationFlag, 0, len(estimateFlags)+1)
simulationFlags := make([]SimulationFlag, 0, len(estimateFlags))
for _, flag := range estimateFlags {
simulationFlag, err := flag.ToSimulationFlag()
if err != nil {
Expand All @@ -47,7 +47,7 @@ func (h *Handler) EstimateFee(
ctx,
id,
broadcastedTxns.Data,
append(simulationFlags, SkipFeeChargeFlag),
simulationFlags,
true,
true,
)
Expand Down
22 changes: 10 additions & 12 deletions rpc/v10/estimate_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ func TestEstimateFee(t *testing.T) {

blockInfo := vm.BlockInfo{Header: &core.Header{}}
t.Run("ok with zero values", func(t *testing.T) {
mockVM.EXPECT().Execute(
mockVM.EXPECT().EstimateFee(
[]core.Transaction{},
nil,
[]*felt.Felt{},
&blockInfo,
mockState,
true,
false,
true, true, true, true, false).
vm.EstimateFeeOptions{}).
Return(vm.ExecutionResults{
OverallFees: []*felt.Felt{},
DataAvailability: []core.DataAvailability{},
Expand All @@ -66,15 +64,15 @@ func TestEstimateFee(t *testing.T) {
})

t.Run("ok with zero values, skip validate", func(t *testing.T) {
mockVM.EXPECT().Execute(
mockVM.EXPECT().EstimateFee(
[]core.Transaction{},
nil,
[]*felt.Felt{},
&blockInfo,
mockState,
true,
true,
true, true, true, true, false).
vm.EstimateFeeOptions{
SkipValidate: true,
}).
Return(
vm.ExecutionResults{
OverallFees: []*felt.Felt{},
Expand All @@ -99,15 +97,15 @@ func TestEstimateFee(t *testing.T) {
})

t.Run("transaction execution error", func(t *testing.T) {
mockVM.EXPECT().Execute(
mockVM.EXPECT().EstimateFee(
[]core.Transaction{},
nil,
[]*felt.Felt{},
&blockInfo,
mockState,
true,
true,
true, true, true, true, false).
vm.EstimateFeeOptions{
SkipValidate: true,
}).
Return(vm.ExecutionResults{}, vm.TransactionExecutionError{
Index: 44,
Cause: json.RawMessage("oops"),
Expand Down
42 changes: 28 additions & 14 deletions rpc/v10/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,34 @@ func (h *Handler) simulateTransactions(
BlockHashToBeRevealed: blockHashToBeRevealed,
}

executionResults, err := h.vm.Execute(
txns,
classes,
paidFeesOnL1,
&blockInfo,
state,
skipFeeCharge,
skipValidate,
errOnRevert,
true,
true,
isEstimateFee,
returnInitialReads,
)
var executionResults vm.ExecutionResults
if isEstimateFee {
executionResults, err = h.vm.EstimateFee(
txns,
classes,
paidFeesOnL1,
&blockInfo,
state,
vm.EstimateFeeOptions{
SkipValidate: skipValidate,
ReturnInitialReads: returnInitialReads,
},
)
} else {
executionResults, err = h.vm.Simulate(
txns,
classes,
paidFeesOnL1,
&blockInfo,
state,
vm.SimulateOptions{
SkipChargeFee: skipFeeCharge,
SkipValidate: skipValidate,
ErrOnRevert: errOnRevert,
ReturnInitialReads: returnInitialReads,
},
)
}
if err != nil {
return SimulateTransactionsResponse{}, httpHeader, handleExecutionError(err)
}
Expand Down
20 changes: 10 additions & 10 deletions rpc/v10/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func TestSimulateTransactions(t *testing.T) {
mockState *mocks.MockStateReader,
) {
defaultMockBehavior(mockReader, mockVM, mockState)
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
mockVM.EXPECT().Simulate([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
Header: headsHeader,
}, mockState, true, false, false, true, true, false, false).
}, mockState, vm.SimulateOptions{SkipChargeFee: true}).
Return(vm.ExecutionResults{
OverallFees: []*felt.Felt{},
DataAvailability: []core.DataAvailability{},
Expand All @@ -86,9 +86,9 @@ func TestSimulateTransactions(t *testing.T) {
mockState *mocks.MockStateReader,
) {
defaultMockBehavior(mockReader, mockVM, mockState)
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
mockVM.EXPECT().Simulate([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
Header: headsHeader,
}, mockState, false, true, false, true, true, false, false).
}, mockState, vm.SimulateOptions{SkipValidate: true}).
Return(vm.ExecutionResults{
OverallFees: []*felt.Felt{},
DataAvailability: []core.DataAvailability{},
Expand All @@ -108,9 +108,9 @@ func TestSimulateTransactions(t *testing.T) {
mockState *mocks.MockStateReader,
) {
defaultMockBehavior(mockReader, mockVM, mockState)
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
mockVM.EXPECT().Simulate([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
Header: headsHeader,
}, mockState, false, true, false, true, true, false, false).
}, mockState, vm.SimulateOptions{SkipValidate: true}).
Return(vm.ExecutionResults{}, vm.TransactionExecutionError{
Index: 44,
Cause: json.RawMessage("oops"),
Expand All @@ -130,9 +130,9 @@ func TestSimulateTransactions(t *testing.T) {
mockState *mocks.MockStateReader,
) {
defaultMockBehavior(mockReader, mockVM, mockState)
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
mockVM.EXPECT().Simulate([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
Header: headsHeader,
}, mockState, false, true, false, true, true, false, false).
}, mockState, vm.SimulateOptions{SkipValidate: true}).
Return(vm.ExecutionResults{
OverallFees: []*felt.Felt{&felt.Zero},
DataAvailability: []core.DataAvailability{{L1Gas: 0}, {L1Gas: 0}},
Expand Down Expand Up @@ -454,8 +454,8 @@ func TestSimulateTransactionsWithReturnInitialReads(t *testing.T) {
},
}}

mockVM.EXPECT().Execute(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), mockState,
false, false, false, true, true, false, returnInitialReads,
mockVM.EXPECT().Simulate(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), mockState,
vm.SimulateOptions{ReturnInitialReads: returnInitialReads},
).Return(vm.ExecutionResults{
OverallFees: []*felt.Felt{&felt.Zero},
DataAvailability: []core.DataAvailability{{L1Gas: 0}},
Expand Down
Loading
Loading