Skip to content

Commit

Permalink
Discard fee unit in legacy estimate message fee (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop authored Dec 3, 2023
1 parent 27c1ac2 commit 3248d59
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
11 changes: 10 additions & 1 deletion rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,15 @@ func (h *Handler) EstimateMessageFee(msg MsgFromL1, id BlockID) (*FeeEstimate, *
return &estimates[0], nil
}

func (h *Handler) LegacyEstimateMessageFee(msg MsgFromL1, id BlockID) (*FeeEstimate, *jsonrpc.Error) { //nolint:gocritic
estimate, rpcErr := h.EstimateMessageFee(msg, id)
if rpcErr != nil {
return nil, rpcErr
}
estimate.Unit = nil
return estimate, nil
}

// TraceTransaction returns the trace for a given executed transaction, including internal calls
//
// It follows the specification defined here:
Expand Down Expand Up @@ -2007,7 +2016,7 @@ func (h *Handler) LegacyMethods() ([]jsonrpc.Method, string) { //nolint: funlen
{
Name: "starknet_estimateMessageFee",
Params: []jsonrpc.Parameter{{Name: "message"}, {Name: "block_id"}},
Handler: h.EstimateMessageFee,
Handler: h.LegacyEstimateMessageFee,
},
{
Name: "starknet_traceTransaction",
Expand Down
47 changes: 47 additions & 0 deletions rpc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3075,6 +3075,53 @@ func TestEstimateMessageFee(t *testing.T) {
}, *estimateFee)
}

func TestLegacyEstimateMessageFee(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

mockReader := mocks.NewMockReader(mockCtrl)
mockVM := mocks.NewMockVM(mockCtrl)
log := utils.NewNopZapLogger()

handler := rpc.New(mockReader, nil, utils.Mainnet, nil, nil, mockVM, "", log)
msg := rpc.MsgFromL1{
From: common.HexToAddress("0xDEADBEEF"),
To: *new(felt.Felt).SetUint64(1337),
Payload: []felt.Felt{*new(felt.Felt).SetUint64(1), *new(felt.Felt).SetUint64(2)},
Selector: *new(felt.Felt).SetUint64(44),
}

latestHeader := &core.Header{
Number: 123,
Timestamp: 456,
GasPrice: new(felt.Felt).SetUint64(42),
}
mockState := mocks.NewMockStateHistoryReader(mockCtrl)

mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil)
mockReader.EXPECT().HeadsHeader().Return(latestHeader, nil)

expectedGasConsumed := new(felt.Felt).SetUint64(37)
mockVM.EXPECT().Execute(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(),
gomock.Any(), utils.Mainnet, gomock.Any(), gomock.Any(), gomock.Any(), latestHeader.GasPrice, latestHeader.GasPriceSTRK, false).DoAndReturn(
func(txns []core.Transaction, declaredClasses []core.Class, blockNumber, blockTimestamp uint64,
sequencerAddress *felt.Felt, state core.StateReader, network utils.Network, paidFeesOnL1 []*felt.Felt,
skipChargeFee, skipValidate bool, gasPriceWei, gasPriceSTRK *felt.Felt, legacyTraceJson bool,
) ([]*felt.Felt, []json.RawMessage, error) {
actualFee := new(felt.Felt).Mul(expectedGasConsumed, gasPriceWei)
return []*felt.Felt{actualFee}, []json.RawMessage{{}}, nil
},
)

estimateFee, err := handler.LegacyEstimateMessageFee(msg, rpc.BlockID{Latest: true})
require.Nil(t, err)
require.Equal(t, rpc.FeeEstimate{
GasConsumed: expectedGasConsumed,
GasPrice: latestHeader.GasPrice,
OverallFee: new(felt.Felt).Mul(expectedGasConsumed, latestHeader.GasPrice),
}, *estimateFee)
}

func TestTraceTransaction(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)
Expand Down

0 comments on commit 3248d59

Please sign in to comment.