Skip to content
Open
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
14 changes: 14 additions & 0 deletions x/auth/tx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,25 @@ func (w *wrapper) GetPubKeys() ([]cryptotypes.PubKey, error) {
}

func (w *wrapper) GetGas() uint64 {
if w.tx.AuthInfo.Fee == nil {
return 0
}
return w.tx.AuthInfo.Fee.GasLimit
}

func (w *wrapper) GetFee() sdk.Coins {
if w.tx.AuthInfo.Fee == nil {
return nil
}
return w.tx.AuthInfo.Fee.Amount
}

func (w *wrapper) FeePayer() []byte {
// A fee-less decoded tx has no payer; short-circuit before the GetSigners
// fallback, which dereferences AuthInfo.Fee.Payer without a nil guard.
if w.tx.AuthInfo.Fee == nil {
return nil
}
Comment thread
marcello33 marked this conversation as resolved.
feePayer := w.tx.AuthInfo.Fee.Payer
if feePayer != "" {
feePayerAddr, err := w.cdc.InterfaceRegistry().SigningContext().AddressCodec().StringToBytes(feePayer)
Expand All @@ -207,6 +218,9 @@ func (w *wrapper) FeePayer() []byte {
}

func (w *wrapper) FeeGranter() []byte {
if w.tx.AuthInfo.Fee == nil {
return nil
}
return w.tx.FeeGranter(w.cdc)
}

Expand Down
59 changes: 59 additions & 0 deletions x/auth/tx/nilfee_repro_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package tx

import (
"testing"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/stretchr/testify/require"
)

// decodeAuthInfo builds and decodes a tx with the given AuthInfo, leaving the
// body minimal, and returns it as an sdk.FeeTx. It mirrors what a decoded
// wire tx looks like when the fee field is present or omitted.
func decodeAuthInfo(t *testing.T, authInfo *tx.AuthInfo) sdk.FeeTx {
t.Helper()
cdc := codec.NewProtoCodec(codectypes.NewInterfaceRegistry())

bodyBz, err := (&tx.TxBody{Memo: "foo"}).Marshal()
require.NoError(t, err)
authInfoBz, err := authInfo.Marshal()
require.NoError(t, err)
txBz, err := (&tx.TxRaw{BodyBytes: bodyBz, AuthInfoBytes: authInfoBz}).Marshal()
require.NoError(t, err)

decoded, err := DefaultTxDecoder(cdc)(txBz)
require.NoError(t, err)
feeTx, ok := decoded.(sdk.FeeTx)
require.True(t, ok)
return feeTx
}

// A tx whose AuthInfo omits the Fee field (proto field 2) decodes with
// AuthInfo.Fee == nil. The fee accessors — reached first by the ante chain's
// SetUpContextDecorator — must return zero values instead of dereferencing nil.
func TestNilFeeAccessorsDoNotPanic(t *testing.T) {
feeTx := decodeAuthInfo(t, &tx.AuthInfo{})

require.NotPanics(t, func() {
require.Equal(t, uint64(0), feeTx.GetGas())
require.Nil(t, feeTx.GetFee())
require.Nil(t, feeTx.FeePayer())
require.Nil(t, feeTx.FeeGranter())
})
}

// An empty (but present) Fee — what newBuilder always sets — must keep working.
func TestEmptyFeeStillWorks(t *testing.T) {
feeTx := decodeAuthInfo(t, &tx.AuthInfo{Fee: &tx.Fee{}})
require.Equal(t, uint64(0), feeTx.GetGas())
require.Nil(t, feeTx.GetFee())
}

// A populated Fee returns its values unchanged.
func TestPopulatedFeeUnchanged(t *testing.T) {
feeTx := decodeAuthInfo(t, &tx.AuthInfo{Fee: &tx.Fee{GasLimit: 21000}})
require.Equal(t, uint64(21000), feeTx.GetGas())
}
Loading