Skip to content

Commit

Permalink
Fix EstimateFee returning TxnExecutionErr on legacy endpoint (#1539)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak authored Dec 11, 2023
1 parent 66d5fa5 commit cf7b9a3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1303,8 +1303,8 @@ func (h *Handler) EstimateFee(broadcastedTxns []BroadcastedTransaction,

func (h *Handler) LegacyEstimateFee(broadcastedTxns []BroadcastedTransaction, id BlockID) ([]FeeEstimate, *jsonrpc.Error) {
result, err := h.simulateTransactions(id, broadcastedTxns, []SimulationFlag{SkipFeeChargeFlag}, true, true)
if err != nil {
return nil, err
if err != nil && err.Code == ErrTransactionExecutionError.Code {
return nil, makeContractError(errors.New(err.Data.(TransactionExecutionErrorData).ExecutionError))
}

return utils.Map(result, func(tx SimulatedTransaction) FeeEstimate {
Expand Down
58 changes: 58 additions & 0 deletions rpc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3682,3 +3682,61 @@ func TestSpecVersion(t *testing.T) {
require.Nil(t, rpcErr)
require.Equal(t, "0.5.1", legacyVersion)
}

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

const network = utils.Mainnet

mockReader := mocks.NewMockReader(mockCtrl)
mockVM := mocks.NewMockVM(mockCtrl)
log := utils.NewNopZapLogger()
handler := rpc.New(mockReader, nil, network, nil, nil, mockVM, "", log)

mockState := mocks.NewMockStateHistoryReader(mockCtrl)
mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil).AnyTimes()
mockReader.EXPECT().HeadsHeader().Return(&core.Header{}, nil).AnyTimes()
sequencerAddress := core.NetworkBlockHashMetaInfo(network).FallBackSequencerAddress

t.Run("ok with zero values", func(t *testing.T) {
mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), sequencerAddress, mockState, network, []*felt.Felt{}, true, false, true, nil, nil, false).
Return([]*felt.Felt{}, []json.RawMessage{}, nil)

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

t.Run("ok with zero values, skip validate", func(t *testing.T) {
mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), sequencerAddress, mockState, network, []*felt.Felt{}, true, true, true, nil, nil, false).
Return([]*felt.Felt{}, []json.RawMessage{}, nil)

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

t.Run("transaction execution error", func(t *testing.T) {
mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), sequencerAddress, mockState, network, []*felt.Felt{}, true, true, true, nil, nil, false).
Return(nil, nil, vm.TransactionExecutionError{
Index: 44,
Cause: errors.New("oops"),
})

_, err := handler.EstimateFee([]rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipValidateFlag}, rpc.BlockID{Latest: true})
require.Equal(t, rpc.ErrTransactionExecutionError.CloneWithData(rpc.TransactionExecutionErrorData{
TransactionIndex: 44,
ExecutionError: "oops",
}), err)

mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), sequencerAddress, mockState, network, []*felt.Felt{}, true, false, true, nil, nil, true).
Return(nil, nil, vm.TransactionExecutionError{
Index: 44,
Cause: errors.New("oops"),
})

_, err = handler.LegacyEstimateFee([]rpc.BroadcastedTransaction{}, rpc.BlockID{Latest: true})
require.Equal(t, rpc.ErrContractError.CloneWithData(rpc.ContractErrorData{
RevertError: "oops",
}), err)
})
}

0 comments on commit cf7b9a3

Please sign in to comment.