Skip to content

Commit

Permalink
fix(x/auth/tx): avoid panic from newWrapperFromDecodedTx (#23170)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex | Skip <alex@djinntek.world>
Co-authored-by: Alex | Interchain Labs <alex@skip.money>
  • Loading branch information
3 people authored Jan 9, 2025
1 parent 06f5339 commit 517839b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
### Bug Fixes

* (query) [23002](https://github.com/cosmos/cosmos-sdk/pull/23002) Fix collection filtered pagination.
* (x/auth/tx) [#23170](https://github.com/cosmos/cosmos-sdk/pull/23170) Avoid panic from newWrapperFromDecodedTx when AuthInfo.Fee is optional in decodedTx.
* (x/auth/tx) [23144](https://github.com/cosmos/cosmos-sdk/pull/23144) Add missing CacheWithValue for ExtensionOptions.
* (x/auth/tx) [#23148](https://github.com/cosmos/cosmos-sdk/pull/23148) Avoid panic from intoAnyV2 when v1.PublicKey is optional.

Expand Down
64 changes: 33 additions & 31 deletions x/auth/tx/gogotx.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,46 @@ func newWrapperFromDecodedTx(
addrCodec address.Codec, cdc codec.BinaryCodec, decodedTx *decode.DecodedTx,
) (*gogoTxWrapper, error) {
var (
fees = sdk.Coins{} // decodedTx.Tx.AuthInfo.Fee.Amount might be nil
err error
fees = sdk.Coins{} // decodedTx.Tx.AuthInfo.Fee.Amount might be nil
err error
feePayer, feeGranter []byte
)
for i, fee := range decodedTx.Tx.AuthInfo.Fee.Amount {
amtInt, ok := math.NewIntFromString(fee.Amount)
if !ok {
return nil, fmt.Errorf("invalid fee coin amount at index %d: %s", i, fee.Amount)
if decodedTx.Tx.AuthInfo.Fee != nil {
for i, fee := range decodedTx.Tx.AuthInfo.Fee.Amount {
amtInt, ok := math.NewIntFromString(fee.Amount)
if !ok {
return nil, fmt.Errorf("invalid fee coin amount at index %d: %s", i, fee.Amount)
}
if err = sdk.ValidateDenom(fee.Denom); err != nil {
return nil, fmt.Errorf("invalid fee coin denom at index %d: %w", i, err)
}

fees = fees.Add(sdk.Coin{
Denom: fee.Denom,
Amount: amtInt,
})
}
if err = sdk.ValidateDenom(fee.Denom); err != nil {
return nil, fmt.Errorf("invalid fee coin denom at index %d: %w", i, err)
if !fees.IsSorted() {
return nil, fmt.Errorf("invalid not sorted tx fees: %s", fees.String())
}

fees = fees.Add(sdk.Coin{
Denom: fee.Denom,
Amount: amtInt,
})
}
if !fees.IsSorted() {
return nil, fmt.Errorf("invalid not sorted tx fees: %s", fees.String())
}
// set fee payer
var feePayer []byte
if len(decodedTx.Signers) != 0 {
feePayer = decodedTx.Signers[0]
if decodedTx.Tx.AuthInfo.Fee.Payer != "" {
feePayer, err = addrCodec.StringToBytes(decodedTx.Tx.AuthInfo.Fee.Payer)
if err != nil {
return nil, err
// set fee payer
if len(decodedTx.Signers) != 0 {
feePayer = decodedTx.Signers[0]
if decodedTx.Tx.AuthInfo.Fee.Payer != "" {
feePayer, err = addrCodec.StringToBytes(decodedTx.Tx.AuthInfo.Fee.Payer)
if err != nil {
return nil, err
}
}
}
}

// fee granter
var feeGranter []byte
if decodedTx.Tx.AuthInfo.Fee.Granter != "" {
feeGranter, err = addrCodec.StringToBytes(decodedTx.Tx.AuthInfo.Fee.Granter)
if err != nil {
return nil, err
// fee granter
if decodedTx.Tx.AuthInfo.Fee.Granter != "" {
feeGranter, err = addrCodec.StringToBytes(decodedTx.Tx.AuthInfo.Fee.Granter)
if err != nil {
return nil, err
}
}
}

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

import (
"testing"

"github.com/stretchr/testify/require"

v1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1"
"cosmossdk.io/x/tx/decode"

"github.com/cosmos/cosmos-sdk/codec"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestNewWrapperFromDecodedTxAllowsNilFee(t *testing.T) {
addrCodec := addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix())
cdc := codec.NewProtoCodec(codectestutil.CodecOptions{}.NewInterfaceRegistry())
_, err := newWrapperFromDecodedTx(addrCodec, cdc, &decode.DecodedTx{
Tx: &v1beta1.Tx{
AuthInfo: &v1beta1.AuthInfo{},
},
})
require.Nil(t, err)
}

0 comments on commit 517839b

Please sign in to comment.