Skip to content

Commit e7469e9

Browse files
authored
Fix estimations in v0.6 RPC methods (#1740)
1 parent 6026484 commit e7469e9

File tree

6 files changed

+91
-64
lines changed

6 files changed

+91
-64
lines changed

mocks/mock_vm.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/throttled_vm.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ func NewThrottledVM(res vm.VM, concurrenyBudget uint, maxQueueLen int32) *Thrott
2020
}
2121

2222
func (tvm *ThrottledVM) Call(callInfo *vm.CallInfo, blockInfo *vm.BlockInfo, state core.StateReader,
23-
network *utils.Network, maxSteps uint64,
23+
network *utils.Network, maxSteps uint64, useBlobData bool,
2424
) ([]*felt.Felt, error) {
2525
var ret []*felt.Felt
2626
return ret, tvm.Do(func(vm *vm.VM) error {
2727
var err error
28-
ret, err = (*vm).Call(callInfo, blockInfo, state, network, maxSteps)
28+
ret, err = (*vm).Call(callInfo, blockInfo, state, network, maxSteps, useBlobData)
2929
return err
3030
})
3131
}
3232

3333
func (tvm *ThrottledVM) Execute(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt,
34-
blockInfo *vm.BlockInfo, state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert bool,
34+
blockInfo *vm.BlockInfo, state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert, useBlobData bool,
3535
) ([]*felt.Felt, []*felt.Felt, []vm.TransactionTrace, error) {
3636
var ret []*felt.Felt
3737
var traces []vm.TransactionTrace
3838
var dataGasConsumed []*felt.Felt
3939
return ret, dataGasConsumed, traces, tvm.Do(func(vm *vm.VM) error {
4040
var err error
4141
ret, dataGasConsumed, traces, err = (*vm).Execute(txns, declaredClasses, paidFeesOnL1, blockInfo, state, network,
42-
skipChargeFee, skipValidate, errOnRevert)
42+
skipChargeFee, skipValidate, errOnRevert, useBlobData)
4343
return err
4444
})
4545
}

rpc/handlers.go

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,15 @@ func (h *Handler) Version() (string, *jsonrpc.Error) {
13181318
}
13191319

13201320
// https://github.com/starkware-libs/starknet-specs/blob/e0b76ed0d8d8eba405e182371f9edac8b2bcbc5a/api/starknet_api_openrpc.json#L401-L445
1321-
func (h *Handler) Call(call FunctionCall, id BlockID) ([]*felt.Felt, *jsonrpc.Error) { //nolint:gocritic
1321+
func (h *Handler) Call(funcCall FunctionCall, id BlockID) ([]*felt.Felt, *jsonrpc.Error) { //nolint:gocritic
1322+
return h.call(funcCall, id, true)
1323+
}
1324+
1325+
func (h *Handler) CallV0_6(call FunctionCall, id BlockID) ([]*felt.Felt, *jsonrpc.Error) { //nolint:gocritic
1326+
return h.call(call, id, false)
1327+
}
1328+
1329+
func (h *Handler) call(funcCall FunctionCall, id BlockID, useBlobData bool) ([]*felt.Felt, *jsonrpc.Error) { //nolint:gocritic
13221330
state, closer, rpcErr := h.stateByBlockID(&id)
13231331
if rpcErr != nil {
13241332
return nil, rpcErr
@@ -1330,7 +1338,7 @@ func (h *Handler) Call(call FunctionCall, id BlockID) ([]*felt.Felt, *jsonrpc.Er
13301338
return nil, rpcErr
13311339
}
13321340

1333-
classHash, err := state.ContractClassHash(&call.ContractAddress)
1341+
classHash, err := state.ContractClassHash(&funcCall.ContractAddress)
13341342
if err != nil {
13351343
return nil, ErrContractNotFound
13361344
}
@@ -1341,14 +1349,14 @@ func (h *Handler) Call(call FunctionCall, id BlockID) ([]*felt.Felt, *jsonrpc.Er
13411349
}
13421350

13431351
res, err := h.vm.Call(&vm.CallInfo{
1344-
ContractAddress: &call.ContractAddress,
1345-
Selector: &call.EntryPointSelector,
1346-
Calldata: call.Calldata,
1352+
ContractAddress: &funcCall.ContractAddress,
1353+
Selector: &funcCall.EntryPointSelector,
1354+
Calldata: funcCall.Calldata,
13471355
ClassHash: classHash,
13481356
}, &vm.BlockInfo{
13491357
Header: header,
13501358
BlockHashToBeRevealed: blockHashToBeRevealed,
1351-
}, state, h.bcReader.Network(), h.callMaxSteps)
1359+
}, state, h.bcReader.Network(), h.callMaxSteps, useBlobData)
13521360
if err != nil {
13531361
if errors.Is(err, utils.ErrResourceBusy) {
13541362
return nil, ErrInternal.CloneWithData(throttledVMErr)
@@ -1451,6 +1459,27 @@ func (h *Handler) EstimateFeeV0_6(broadcastedTxns []BroadcastedTransaction,
14511459
}
14521460

14531461
func (h *Handler) EstimateMessageFee(msg MsgFromL1, id BlockID) (*FeeEstimate, *jsonrpc.Error) { //nolint:gocritic
1462+
return h.estimateMessageFee(msg, id, h.EstimateFee)
1463+
}
1464+
1465+
func (h *Handler) EstimateMessageFeeV0_6(msg MsgFromL1, id BlockID) (*FeeEstimate, *jsonrpc.Error) { //nolint:gocritic
1466+
feeEstimate, rpcErr := h.estimateMessageFee(msg, id, h.EstimateFeeV0_6)
1467+
if rpcErr != nil {
1468+
return nil, rpcErr
1469+
}
1470+
1471+
feeEstimate.v0_6Response = true
1472+
feeEstimate.DataGasPrice = nil
1473+
feeEstimate.DataGasConsumed = nil
1474+
1475+
return feeEstimate, nil
1476+
}
1477+
1478+
type estimateFeeHandler func(broadcastedTxns []BroadcastedTransaction,
1479+
simulationFlags []SimulationFlag, id BlockID,
1480+
) ([]FeeEstimate, *jsonrpc.Error)
1481+
1482+
func (h *Handler) estimateMessageFee(msg MsgFromL1, id BlockID, f estimateFeeHandler) (*FeeEstimate, *jsonrpc.Error) { //nolint:gocritic
14541483
calldata := make([]*felt.Felt, 0, len(msg.Payload)+1)
14551484
// The order of the calldata parameters matters. msg.From must be prepended.
14561485
calldata = append(calldata, new(felt.Felt).SetBytes(msg.From.Bytes()))
@@ -1470,7 +1499,7 @@ func (h *Handler) EstimateMessageFee(msg MsgFromL1, id BlockID) (*FeeEstimate, *
14701499
// Must be greater than zero to successfully execute transaction.
14711500
PaidFeeOnL1: new(felt.Felt).SetUint64(1),
14721501
}
1473-
estimates, rpcErr := h.EstimateFee([]BroadcastedTransaction{tx}, nil, id)
1502+
estimates, rpcErr := f([]BroadcastedTransaction{tx}, nil, id)
14741503
if rpcErr != nil {
14751504
if rpcErr.Code == ErrTransactionExecutionError.Code {
14761505
data := rpcErr.Data.(TransactionExecutionErrorData)
@@ -1481,19 +1510,6 @@ func (h *Handler) EstimateMessageFee(msg MsgFromL1, id BlockID) (*FeeEstimate, *
14811510
return &estimates[0], nil
14821511
}
14831512

1484-
func (h *Handler) EstimateMessageFeeV0_6(msg MsgFromL1, id BlockID) (*FeeEstimate, *jsonrpc.Error) { //nolint:gocritic
1485-
feeEstimate, err := h.EstimateMessageFee(msg, id)
1486-
if err != nil {
1487-
return nil, err
1488-
}
1489-
1490-
feeEstimate.v0_6Response = true
1491-
feeEstimate.DataGasPrice = nil
1492-
feeEstimate.DataGasConsumed = nil
1493-
1494-
return feeEstimate, nil
1495-
}
1496-
14971513
// TraceTransaction returns the trace for a given executed transaction, including internal calls
14981514
//
14991515
// It follows the specification defined here:
@@ -1591,8 +1607,9 @@ func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTra
15911607
Header: header,
15921608
BlockHashToBeRevealed: blockHashToBeRevealed,
15931609
}
1610+
useBlobData := !v0_6Response
15941611
overallFees, dataGasConsumed, traces, err := h.vm.Execute(txns, classes, paidFeesOnL1, &blockInfo,
1595-
state, h.bcReader.Network(), skipFeeCharge, skipValidate, errOnRevert)
1612+
state, h.bcReader.Network(), skipFeeCharge, skipValidate, errOnRevert, useBlobData)
15961613
if err != nil {
15971614
if errors.Is(err, utils.ErrResourceBusy) {
15981615
return nil, ErrInternal.CloneWithData(throttledVMErr)
@@ -1625,8 +1642,13 @@ func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTra
16251642
}
16261643
}
16271644

1628-
dataGasFee := new(felt.Felt).Mul(dataGasConsumed[i], dataGasPrice)
1629-
gasConsumed := new(felt.Felt).Sub(overallFee, dataGasFee)
1645+
var gasConsumed *felt.Felt
1646+
if !v0_6Response {
1647+
dataGasFee := new(felt.Felt).Mul(dataGasConsumed[i], dataGasPrice)
1648+
gasConsumed = new(felt.Felt).Sub(overallFee, dataGasFee)
1649+
} else {
1650+
gasConsumed = overallFee.Clone()
1651+
}
16301652
gasConsumed = gasConsumed.Div(gasConsumed, gasPrice) // division by zero felt is zero felt
16311653

16321654
estimate := FeeEstimate{
@@ -1758,8 +1780,9 @@ func (h *Handler) traceBlockTransactions(ctx context.Context, block *core.Block,
17581780
BlockHashToBeRevealed: blockHashToBeRevealed,
17591781
}
17601782

1783+
useBlobData := !v0_6Response
17611784
overallFees, dataGasConsumed, traces, err := h.vm.Execute(block.Transactions, classes, paidFeesOnL1, &blockInfo, state, network, false,
1762-
false, false)
1785+
false, false, useBlobData)
17631786
if err != nil {
17641787
if errors.Is(err, utils.ErrResourceBusy) {
17651788
return nil, ErrInternal.CloneWithData(throttledVMErr)
@@ -2197,7 +2220,7 @@ func (h *Handler) MethodsV0_6() ([]jsonrpc.Method, string) { //nolint: funlen
21972220
{
21982221
Name: "starknet_call",
21992222
Params: []jsonrpc.Parameter{{Name: "request"}, {Name: "block_id"}},
2200-
Handler: h.Call,
2223+
Handler: h.CallV0_6,
22012224
},
22022225
{
22032226
Name: "starknet_estimateFee",

rpc/handlers_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2905,7 +2905,7 @@ func TestCall(t *testing.T) {
29052905
ClassHash: classHash,
29062906
Selector: selector,
29072907
Calldata: calldata,
2908-
}, &vm.BlockInfo{Header: headsHeader}, gomock.Any(), &utils.Mainnet, uint64(1337)).Return(expectedRes, nil)
2908+
}, &vm.BlockInfo{Header: headsHeader}, gomock.Any(), &utils.Mainnet, uint64(1337), true).Return(expectedRes, nil)
29092909

29102910
res, rpcErr := handler.Call(rpc.FunctionCall{
29112911
ContractAddress: *contractAddr,
@@ -2953,7 +2953,7 @@ func TestEstimateMessageFee(t *testing.T) {
29532953
expectedGasConsumed := new(felt.Felt).SetUint64(37)
29542954
mockVM.EXPECT().Execute(gomock.Any(), gomock.Any(), gomock.Any(), &vm.BlockInfo{
29552955
Header: latestHeader,
2956-
}, gomock.Any(), &utils.Mainnet, gomock.Any(), false, true).DoAndReturn(
2956+
}, gomock.Any(), &utils.Mainnet, gomock.Any(), false, true, false).DoAndReturn(
29572957
func(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt, blockInfo *vm.BlockInfo,
29582958
state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert bool,
29592959
) ([]*felt.Felt, []*felt.Felt, []vm.TransactionTrace, error) {
@@ -3055,7 +3055,7 @@ func TestTraceTransaction(t *testing.T) {
30553055
vmTrace := new(vm.TransactionTrace)
30563056
require.NoError(t, json.Unmarshal(vmTraceJSON, vmTrace))
30573057
mockVM.EXPECT().Execute([]core.Transaction{tx}, []core.Class{declaredClass.Class}, []*felt.Felt{},
3058-
&vm.BlockInfo{Header: header}, gomock.Any(), &utils.Mainnet, false, false, false).Return(nil, []vm.TransactionTrace{*vmTrace}, nil)
3058+
&vm.BlockInfo{Header: header}, gomock.Any(), &utils.Mainnet, false, false, false, false).Return(nil, []vm.TransactionTrace{*vmTrace}, nil)
30593059

30603060
trace, err := handler.TraceTransaction(context.Background(), *hash)
30613061
require.Nil(t, err)
@@ -3085,7 +3085,7 @@ func TestSimulateTransactions(t *testing.T) {
30853085
t.Run("ok with zero values, skip fee", func(t *testing.T) {
30863086
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &vm.BlockInfo{
30873087
Header: headsHeader,
3088-
}, mockState, &network, true, false, false).
3088+
}, mockState, &network, true, false, false, false).
30893089
Return([]*felt.Felt{}, []vm.TransactionTrace{}, nil)
30903090

30913091
_, err := handler.SimulateTransactions(rpc.BlockID{Latest: true}, []rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipFeeChargeFlag})
@@ -3095,7 +3095,7 @@ func TestSimulateTransactions(t *testing.T) {
30953095
t.Run("ok with zero values, skip validate", func(t *testing.T) {
30963096
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &vm.BlockInfo{
30973097
Header: headsHeader,
3098-
}, mockState, &network, false, false, false).
3098+
}, mockState, &network, false, false, false, false).
30993099
Return([]*felt.Felt{}, []vm.TransactionTrace{}, nil)
31003100

31013101
_, err := handler.SimulateTransactions(rpc.BlockID{Latest: true}, []rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipValidateFlag})
@@ -3105,7 +3105,7 @@ func TestSimulateTransactions(t *testing.T) {
31053105
t.Run("transaction execution error", func(t *testing.T) {
31063106
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &vm.BlockInfo{
31073107
Header: headsHeader,
3108-
}, mockState, &network, false, false, false).
3108+
}, mockState, &network, false, false, false, false).
31093109
Return(nil, nil, vm.TransactionExecutionError{
31103110
Index: 44,
31113111
Cause: errors.New("oops"),
@@ -3119,7 +3119,7 @@ func TestSimulateTransactions(t *testing.T) {
31193119

31203120
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &vm.BlockInfo{
31213121
Header: headsHeader,
3122-
}, mockState, &network, false, true, true).
3122+
}, mockState, &network, false, true, true, false).
31233123
Return(nil, nil, vm.TransactionExecutionError{
31243124
Index: 44,
31253125
Cause: errors.New("oops"),
@@ -3215,7 +3215,7 @@ func TestTraceBlockTransactions(t *testing.T) {
32153215
vmTrace := vm.TransactionTrace{}
32163216
require.NoError(t, json.Unmarshal(vmTraceJSON, &vmTrace))
32173217
mockVM.EXPECT().Execute(block.Transactions, []core.Class{declaredClass.Class}, paidL1Fees, &vm.BlockInfo{Header: header},
3218-
gomock.Any(), &network, false, false, false).Return(nil, []vm.TransactionTrace{vmTrace, vmTrace}, nil)
3218+
gomock.Any(), &network, false, false, false, false).Return(nil, []vm.TransactionTrace{vmTrace, vmTrace}, nil)
32193219

32203220
result, err := handler.TraceBlockTransactions(context.Background(), rpc.BlockID{Hash: blockHash})
32213221
require.Nil(t, err)
@@ -3281,7 +3281,7 @@ func TestTraceBlockTransactions(t *testing.T) {
32813281
vmTrace := vm.TransactionTrace{}
32823282
require.NoError(t, json.Unmarshal(vmTraceJSON, &vmTrace))
32833283
mockVM.EXPECT().Execute([]core.Transaction{tx}, []core.Class{declaredClass.Class}, []*felt.Felt{}, &vm.BlockInfo{Header: header},
3284-
gomock.Any(), &network, false, false, false).Return(nil, []vm.TransactionTrace{vmTrace}, nil)
3284+
gomock.Any(), &network, false, false, false, false).Return(nil, []vm.TransactionTrace{vmTrace}, nil)
32853285

32863286
expectedResult := []rpc.TracedBlockTransaction{
32873287
{
@@ -3651,23 +3651,23 @@ func TestEstimateFee(t *testing.T) {
36513651

36523652
blockInfo := vm.BlockInfo{Header: &core.Header{}}
36533653
t.Run("ok with zero values", func(t *testing.T) {
3654-
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &blockInfo, mockState, &network, true, true, false).
3654+
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &blockInfo, mockState, &network, true, true, false, false).
36553655
Return([]*felt.Felt{}, []vm.TransactionTrace{}, nil)
36563656

36573657
_, err := handler.EstimateFee([]rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{}, rpc.BlockID{Latest: true})
36583658
require.Nil(t, err)
36593659
})
36603660

36613661
t.Run("ok with zero values, skip validate", func(t *testing.T) {
3662-
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &blockInfo, mockState, &network, true, true, false).
3662+
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &blockInfo, mockState, &network, true, true, false, false).
36633663
Return([]*felt.Felt{}, []vm.TransactionTrace{}, nil)
36643664

36653665
_, err := handler.EstimateFee([]rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipValidateFlag}, rpc.BlockID{Latest: true})
36663666
require.Nil(t, err)
36673667
})
36683668

36693669
t.Run("transaction execution error", func(t *testing.T) {
3670-
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &blockInfo, mockState, &network, true, true, false).
3670+
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &blockInfo, mockState, &network, true, true, false, false).
36713671
Return(nil, nil, vm.TransactionExecutionError{
36723672
Index: 44,
36733673
Cause: errors.New("oops"),
@@ -3679,7 +3679,7 @@ func TestEstimateFee(t *testing.T) {
36793679
ExecutionError: "oops",
36803680
}), err)
36813681

3682-
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &blockInfo, mockState, &network, false, true, true).
3682+
mockVM.EXPECT().Execute(nil, nil, []*felt.Felt{}, &blockInfo, mockState, &network, false, true, true, false).
36833683
Return(nil, nil, vm.TransactionExecutionError{
36843684
Index: 44,
36853685
Cause: errors.New("oops"),

0 commit comments

Comments
 (0)