Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(feegrant): remove bech32 global #15347

Merged
merged 34 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
539b6fe
feegrant bech32 removal
tac0turtle Mar 10, 2023
683e1f2
remove some global bech32 items in feegrant
tac0turtle Mar 10, 2023
e72f015
few more changes
tac0turtle Mar 10, 2023
d31093a
Merge branch 'main' into marko/bech32_global_feegrant
tac0turtle Mar 14, 2023
fb97ad9
Merge branch 'main' into marko/bech32_global_feegrant
tac0turtle Mar 17, 2023
1144aef
remove global codec
tac0turtle Mar 17, 2023
90e0cac
remove usage of accaddressfrombech32
tac0turtle Mar 17, 2023
2d56e2a
remove failing tests
tac0turtle Mar 17, 2023
cde3607
Merge branch 'main' into marko/bech32_global_feegrant
tac0turtle Mar 17, 2023
f16b0fd
Merge branch 'main' into marko/bech32_global_feegrant
tac0turtle Mar 20, 2023
4285a67
address feedback
tac0turtle Mar 20, 2023
979697e
Merge branch 'main' into marko/bech32_global_feegrant
tac0turtle Mar 20, 2023
523c839
Merge branch 'main' into marko/bech32_global_feegrant
tac0turtle Mar 21, 2023
11f2150
Merge branch 'main' into marko/bech32_global_feegrant
tac0turtle Mar 21, 2023
232c6f4
move to address/codec
tac0turtle Mar 21, 2023
ca828f5
depend on core
tac0turtle Mar 21, 2023
5063def
remove types codec
tac0turtle Mar 21, 2023
96b3845
fix imports
tac0turtle Mar 21, 2023
c9fdbdb
fix imports
tac0turtle Mar 21, 2023
8cea31f
move bech32 back to auth and wrap methods
tac0turtle Mar 21, 2023
600915c
Merge branch 'main' into marko/bech32_global_feegrant
tac0turtle Mar 22, 2023
6a0784e
changelog
tac0turtle Mar 22, 2023
d09177a
fix build
tac0turtle Mar 22, 2023
536d268
mock expect
tac0turtle Mar 22, 2023
aece29f
stuck
tac0turtle Mar 22, 2023
c50474d
Merge branch 'main' into marko/bech32_global_feegrant
tac0turtle Mar 24, 2023
698ec45
fix tests
tac0turtle Mar 24, 2023
dc2ebf1
some fixes
tac0turtle Mar 24, 2023
f41cb66
move higher
tac0turtle Mar 24, 2023
028444b
fix genesis test
julienrbrt Mar 24, 2023
4f7a171
fix all tests
julienrbrt Mar 24, 2023
6d297e1
Merge branch 'main' into marko/bech32_global_feegrant
julienrbrt Mar 24, 2023
0176e31
go mod tidy
julienrbrt Mar 24, 2023
764bd17
remove replace
julienrbrt Mar 24, 2023
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting the Bech32Codec has one small disadvantage: it will make it harder to refactor auth into a go.mod, since it will depend on the root sdk. But maybe it's fine for now, other go-modded sdk modules also depend on root.


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
}
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"))
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved

_, 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
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.
* (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.

### Api Breaking Changes

* (keeper) [15347](https://github.com/cosmos/cosmos-sdk/pull/15347) Remove global bech32 usage in keeper.
* (keeper) [15347](https://github.com/cosmos/cosmos-sdk/pull/15347) `ValidateBasic` is treated as a no op now with with acceptance of RFC001
34 changes: 16 additions & 18 deletions x/feegrant/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"fmt"
"strings"

"cosmossdk.io/core/address"
"github.com/spf13/cobra"

"cosmossdk.io/x/feegrant"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
func GetQueryCmd(ac address.Codec) *cobra.Command {
feegrantQueryCmd := &cobra.Command{
Use: feegrant.ModuleName,
Short: "Querying commands for the feegrant module",
Expand All @@ -24,16 +24,16 @@ func GetQueryCmd() *cobra.Command {
}

feegrantQueryCmd.AddCommand(
GetCmdQueryFeeGrant(),
GetCmdQueryFeeGrantsByGrantee(),
GetCmdQueryFeeGrantsByGranter(),
GetCmdQueryFeeGrant(ac),
GetCmdQueryFeeGrantsByGrantee(ac),
GetCmdQueryFeeGrantsByGranter(ac),
)

return feegrantQueryCmd
}

// GetCmdQueryFeeGrant returns cmd to query for a grant between granter and grantee.
func GetCmdQueryFeeGrant() *cobra.Command {
func GetCmdQueryFeeGrant(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "grant [granter] [grantee]",
Args: cobra.ExactArgs(2),
Expand All @@ -50,21 +50,19 @@ $ %s query feegrant grant [granter] [grantee]
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := feegrant.NewQueryClient(clientCtx)

granterAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
if _, err := ac.StringToBytes(args[0]); err != nil {
return err
}

granteeAddr, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
if _, err := ac.StringToBytes(args[1]); err != nil {
return err
}

res, err := queryClient.Allowance(
cmd.Context(),
&feegrant.QueryAllowanceRequest{
Granter: granterAddr.String(),
Grantee: granteeAddr.String(),
Granter: args[0],
Grantee: args[1],
},
)
if err != nil {
Expand All @@ -81,7 +79,7 @@ $ %s query feegrant grant [granter] [grantee]
}

// GetCmdQueryFeeGrantsByGrantee returns cmd to query for all grants for a grantee.
func GetCmdQueryFeeGrantsByGrantee() *cobra.Command {
func GetCmdQueryFeeGrantsByGrantee(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "grants-by-grantee [grantee]",
Args: cobra.ExactArgs(1),
Expand All @@ -97,7 +95,7 @@ $ %s query feegrant grants-by-grantee [grantee]
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := feegrant.NewQueryClient(clientCtx)

granteeAddr, err := sdk.AccAddressFromBech32(args[0])
_, err := ac.StringToBytes(args[0])
if err != nil {
return err
}
Expand All @@ -110,7 +108,7 @@ $ %s query feegrant grants-by-grantee [grantee]
res, err := queryClient.Allowances(
cmd.Context(),
&feegrant.QueryAllowancesRequest{
Grantee: granteeAddr.String(),
Grantee: args[0],
Pagination: pageReq,
},
)
Expand All @@ -129,7 +127,7 @@ $ %s query feegrant grants-by-grantee [grantee]
}

// GetCmdQueryFeeGrantsByGranter returns cmd to query for all grants by a granter.
func GetCmdQueryFeeGrantsByGranter() *cobra.Command {
func GetCmdQueryFeeGrantsByGranter(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "grants-by-granter [granter]",
Args: cobra.ExactArgs(1),
Expand All @@ -145,7 +143,7 @@ $ %s query feegrant grants-by-granter [granter]
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := feegrant.NewQueryClient(clientCtx)

granterAddr, err := sdk.AccAddressFromBech32(args[0])
_, err := ac.StringToBytes(args[0])
if err != nil {
return err
}
Expand All @@ -158,7 +156,7 @@ $ %s query feegrant grants-by-granter [granter]
res, err := queryClient.AllowancesByGranter(
cmd.Context(),
&feegrant.QueryAllowancesByGranterRequest{
Granter: granterAddr.String(),
Granter: args[0],
Pagination: pageReq,
},
)
Expand Down
Loading