Skip to content

Commit

Permalink
refactor(feegrant): remove bech32 global (#15347)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
tac0turtle and julienrbrt authored Mar 24, 2023
1 parent 443eb3d commit 572e657
Show file tree
Hide file tree
Showing 36 changed files with 402 additions and 340 deletions.
48 changes: 48 additions & 0 deletions codec/address/bech32_codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package address

import (
"cosmossdk.io/core/address"
errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

type Bech32Codec struct {
Bech32Prefix string
}

var _ address.Codec = &Bech32Codec{}

func NewBech32Codec(prefix string) address.Codec {
return Bech32Codec{prefix}
}

// StringToBytes encodes text to bytes
func (bc Bech32Codec) StringToBytes(text string) ([]byte, error) {
hrp, bz, err := bech32.DecodeAndConvert(text)
if err != nil {
return nil, err
}

if hrp != bc.Bech32Prefix {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "hrp does not match bech32Prefix")
}

if err := sdk.VerifyAddressFormat(bz); err != nil {
return nil, err
}

return bz, nil
}

// BytesToString decodes bytes to text
func (bc Bech32Codec) BytesToString(bz []byte) (string, error) {
text, err := bech32.ConvertAndEncode(bc.Bech32Prefix, bz)
if err != nil {
return "", err
}

return text, nil
}
2 changes: 1 addition & 1 deletion simapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/cometbft/cometbft v0.37.0
github.com/cosmos/cosmos-db v1.0.0-rc.1
// this version is not used as it is always replaced by the latest Cosmos SDK version
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230321173237-fe77d4bca302
github.com/cosmos/cosmos-sdk v0.48.0
github.com/cosmos/gogoproto v1.4.6
github.com/golang/mock v1.6.0
github.com/spf13/cast v1.5.0
Expand Down
28 changes: 15 additions & 13 deletions tests/e2e/feegrant/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"cosmossdk.io/x/feegrant/client/cli"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
codecaddress "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
Expand Down Expand Up @@ -97,7 +98,7 @@ func (s *E2ETestSuite) createGrant(granter, grantee sdk.Address) {
commonFlags...,
)

cmd := cli.NewCmdFeeGrant()
cmd := cli.NewCmdFeeGrant(codecaddress.NewBech32Codec("cosmos"))

_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)
Expand Down Expand Up @@ -171,7 +172,7 @@ func (s *E2ETestSuite) TestCmdGetFeeGrant() {
tc := tc

s.Run(tc.name, func() {
cmd := cli.GetCmdQueryFeeGrant()
cmd := cli.GetCmdQueryFeeGrant(codecaddress.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)

if tc.expectErr {
Expand Down Expand Up @@ -237,7 +238,7 @@ func (s *E2ETestSuite) TestCmdGetFeeGrantsByGrantee() {
tc := tc

s.Run(tc.name, func() {
cmd := cli.GetCmdQueryFeeGrantsByGrantee()
cmd := cli.GetCmdQueryFeeGrantsByGrantee(codecaddress.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)

if tc.expectErr {
Expand Down Expand Up @@ -294,7 +295,7 @@ func (s *E2ETestSuite) TestCmdGetFeeGrantsByGranter() {
tc := tc

s.Run(tc.name, func() {
cmd := cli.GetCmdQueryFeeGrantsByGranter()
cmd := cli.GetCmdQueryFeeGrantsByGranter(codecaddress.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)

if tc.expectErr {
Expand Down Expand Up @@ -587,7 +588,7 @@ func (s *E2ETestSuite) TestNewCmdFeeGrant() {
tc := tc

s.Run(tc.name, func() {
cmd := cli.NewCmdFeeGrant()
cmd := cli.NewCmdFeeGrant(codecaddress.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)

if tc.expectErr {
Expand Down Expand Up @@ -615,10 +616,11 @@ func (s *E2ETestSuite) TestNewCmdRevokeFeegrant() {
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
}

address := "cosmos16ydaqh0fcnh4qt7a3jme4mmztm2qel5axcpw00"
// Create new fee grant specifically to test amino.
aminoGrantee, err := sdk.AccAddressFromBech32("cosmos16ydaqh0fcnh4qt7a3jme4mmztm2qel5axcpw00")
aminoGrantee, err := codecaddress.NewBech32Codec("cosmos").StringToBytes(address)
s.Require().NoError(err)
s.createGrant(granter, aminoGrantee)
s.createGrant(granter, sdk.AccAddress(aminoGrantee))

testCases := []struct {
name string
Expand Down Expand Up @@ -680,7 +682,7 @@ func (s *E2ETestSuite) TestNewCmdRevokeFeegrant() {
append(
[]string{
granter.String(),
aminoGrantee.String(),
address,
fmt.Sprintf("--%s=%s", flags.FlagFrom, granter),
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
},
Expand All @@ -694,7 +696,7 @@ func (s *E2ETestSuite) TestNewCmdRevokeFeegrant() {
tc := tc

s.Run(tc.name, func() {
cmd := cli.NewCmdRevokeFeegrant()
cmd := cli.NewCmdRevokeFeegrant(codecaddress.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)

if tc.expectErr {
Expand Down Expand Up @@ -743,7 +745,7 @@ func (s *E2ETestSuite) TestTxWithFeeGrant() {
commonFlags...,
)

cmd := cli.NewCmdFeeGrant()
cmd := cli.NewCmdFeeGrant(codecaddress.NewBech32Codec("cosmos"))

_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)
Expand Down Expand Up @@ -878,7 +880,7 @@ func (s *E2ETestSuite) TestFilteredFeeAllowance() {
tc := tc

s.Run(tc.name, func() {
cmd := cli.NewCmdFeeGrant()
cmd := cli.NewCmdFeeGrant(codecaddress.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)

if tc.expectErr {
Expand All @@ -900,7 +902,7 @@ func (s *E2ETestSuite) TestFilteredFeeAllowance() {
}

// get filtered fee allowance and check info
cmd := cli.GetCmdQueryFeeGrant()
cmd := cli.GetCmdQueryFeeGrant(codecaddress.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)

Expand Down Expand Up @@ -963,7 +965,7 @@ func (s *E2ETestSuite) TestFilteredFeeAllowance() {
},
commonFlags...,
)
cmd := cli.NewCmdFeeGrant()
cmd := cli.NewCmdFeeGrant(codecaddress.NewBech32Codec("cosmos"))
return clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
},
&sdk.TxResponse{},
Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/cosmos/cosmos-db v1.0.0-rc.1
github.com/cosmos/cosmos-proto v1.0.0-beta.3
// this version is not used as it is always replaced by the latest Cosmos SDK version
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230321173237-fe77d4bca302
github.com/cosmos/cosmos-sdk v0.48.0
github.com/cosmos/gogoproto v1.4.6
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
Expand Down
3 changes: 2 additions & 1 deletion testutil/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testutil

import (
"testing"
"time"

"cosmossdk.io/log"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
Expand Down Expand Up @@ -44,7 +45,7 @@ func DefaultContextWithDB(t *testing.T, key storetypes.StoreKey, tkey storetypes
err := cms.LoadLatestVersion()
assert.NoError(t, err)

ctx := sdk.NewContext(cms, cmtproto.Header{}, false, log.NewNopLogger())
ctx := sdk.NewContext(cms, cmtproto.Header{Time: time.Now()}, false, log.NewNopLogger())

return TestContext{ctx, db, cms}
}
9 changes: 0 additions & 9 deletions types/address/codec.go

This file was deleted.

2 changes: 1 addition & 1 deletion x/auth/keeper/bech32_codec.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package keeper

import (
"cosmossdk.io/core/address"
errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/bech32"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down
3 changes: 1 addition & 2 deletions x/auth/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
"sort"
"testing"

storetypes "cosmossdk.io/store/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/suite"
"pgregory.net/rapid"

storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/testutil"
Expand Down
12 changes: 12 additions & 0 deletions x/auth/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,15 @@ func (ak AccountKeeper) AccountInfo(goCtx context.Context, req *types.QueryAccou
},
}, nil
}

// BytesToString converts an address from bytes to string, using the
// keeper's bech32 prefix.
func (ak AccountKeeper) BytesToString(address []byte) (string, error) {
return ak.addressCdc.BytesToString(address)
}

// StringToBytes converts an address from string to bytes, using the
// keeper's bech32 prefix.
func (ak AccountKeeper) StringToBytes(address string) ([]byte, error) {
return ak.addressCdc.StringToBytes(address)
}
2 changes: 1 addition & 1 deletion x/auth/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"cosmossdk.io/log"
gogotypes "github.com/cosmos/gogoproto/types"

"cosmossdk.io/core/address"
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
Expand Down
5 changes: 3 additions & 2 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import (

"cosmossdk.io/depinject"

"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"github.com/cosmos/cosmos-sdk/types/address"

modulev1 "cosmossdk.io/api/cosmos/auth/module/v1"

store "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codecaddress "github.com/cosmos/cosmos-sdk/codec/address"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand Down Expand Up @@ -206,7 +207,7 @@ func init() {
// ProvideAddressCodec provides an address.Codec to the container for any
// modules that want to do address string <> bytes conversion.
func ProvideAddressCodec(config *modulev1.Module) address.Codec {
return keeper.NewBech32Codec(config.Bech32Prefix)
return codecaddress.NewBech32Codec(config.Bech32Prefix)
}

//nolint:revive
Expand Down
7 changes: 6 additions & 1 deletion x/feegrant/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* (x/feegrant) [14649](https://github.com/cosmos/cosmos-sdk/pull/14649) The `x/feegrant` module is extracted to have a separate go.mod file which allows it to be a standalone module.
* [#14649](https://github.com/cosmos/cosmos-sdk/pull/14649) The `x/feegrant` module is extracted to have a separate go.mod file which allows it to be a standalone module.

### API Breaking Changes

* [#15347](https://github.com/cosmos/cosmos-sdk/pull/15347) Remove global bech32 usage in keeper.
* [#15347](https://github.com/cosmos/cosmos-sdk/pull/15347) `ValidateBasic` is treated as a no op now with with acceptance of RFC001
Loading

0 comments on commit 572e657

Please sign in to comment.