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(groups): remove global bech32 #15707

Merged
merged 18 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
3 changes: 2 additions & 1 deletion tests/e2e/group/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/client/flags"
"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 @@ -1309,7 +1310,7 @@ func (s *E2ETestSuite) TestTxUpdateGroupPolicyDecisionPolicy() {
tc := tc

s.Run(tc.name, func() {
cmd := client.MsgUpdateGroupPolicyDecisionPolicyCmd()
cmd := client.MsgUpdateGroupPolicyDecisionPolicyCmd(address.NewBech32Codec("cosmos"))

out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
Expand Down
2 changes: 1 addition & 1 deletion x/feegrant/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions x/group/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"fmt"
"strconv"

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

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/group"
)
Expand All @@ -22,7 +22,7 @@ const (
)

// TxCmd returns a root CLI command handler for all x/group transaction commands.
func TxCmd(name string) *cobra.Command {
func TxCmd(name string, ac address.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: name,
Short: "Group transaction subcommands",
Expand All @@ -39,7 +39,7 @@ func TxCmd(name string) *cobra.Command {
MsgCreateGroupWithPolicyCmd(),
MsgCreateGroupPolicyCmd(),
MsgUpdateGroupPolicyAdminCmd(),
MsgUpdateGroupPolicyDecisionPolicyCmd(),
MsgUpdateGroupPolicyDecisionPolicyCmd(ac),
MsgUpdateGroupPolicyMetadataCmd(),
MsgWithdrawProposalCmd(),
MsgSubmitProposalCmd(),
Expand Down Expand Up @@ -461,7 +461,7 @@ func MsgUpdateGroupPolicyAdminCmd() *cobra.Command {
}

// MsgUpdateGroupPolicyDecisionPolicyCmd creates a CLI command for Msg/UpdateGroupPolicyDecisionPolicy.
func MsgUpdateGroupPolicyDecisionPolicyCmd() *cobra.Command {
func MsgUpdateGroupPolicyDecisionPolicyCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "update-group-policy-decision-policy [admin] [group-policy-account] [decision-policy-json-file]",
Short: "Update a group policy's decision policy",
Expand All @@ -482,7 +482,7 @@ func MsgUpdateGroupPolicyDecisionPolicyCmd() *cobra.Command {
return err
}

accountAddress, err := sdk.AccAddressFromBech32(args[1])
accountAddress, err := ac.StringToBytes(args[1])
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion x/group/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
Expand Down Expand Up @@ -1181,7 +1182,7 @@ func (s *CLITestSuite) TestTxUpdateGroupPolicyDecisionPolicy() {
invalidNegativePercentageDecisionPolicy := testutil.WriteToNewTempFile(s.T(), `{"@type":"/cosmos.group.v1.PercentageDecisionPolicy", "percentage":"-0.5", "windows":{"voting_period":"1s"}}`)
invalidPercentageDecisionPolicy := testutil.WriteToNewTempFile(s.T(), `{"@type":"/cosmos.group.v1.PercentageDecisionPolicy", "percentage":"2", "windows":{"voting_period":"40000s"}}`)

cmd := groupcli.MsgUpdateGroupPolicyDecisionPolicyCmd()
cmd := groupcli.MsgUpdateGroupPolicyDecisionPolicyCmd(address.NewBech32Codec("cosmos"))
cmd.SetOutput(io.Discard)

testCases := []struct {
Expand Down
3 changes: 3 additions & 0 deletions x/group/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package group
import (
context "context"

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

type AccountKeeper interface {
address.Codec

// NewAccount returns a new account with the next account number. Does not save the new account to the store.
NewAccount(context.Context, sdk.AccountI) sdk.AccountI

Expand Down
4 changes: 4 additions & 0 deletions x/group/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (s *GenesisTestSuite) SetupTest() {
accountKeeper := grouptestutil.NewMockAccountKeeper(ctrl)
accountKeeper.EXPECT().GetAccount(gomock.Any(), accAddr).Return(authtypes.NewBaseAccountWithAddress(accAddr)).AnyTimes()
accountKeeper.EXPECT().GetAccount(gomock.Any(), memberAddr).Return(authtypes.NewBaseAccountWithAddress(memberAddr)).AnyTimes()
accountKeeper.EXPECT().BytesToString(accAddr).Return(accAddr.String(), nil).AnyTimes()
accountKeeper.EXPECT().StringToBytes(accAddr.String()).Return(accAddr, nil).AnyTimes()
accountKeeper.EXPECT().BytesToString(memberAddr).Return(memberAddr.String(), nil).AnyTimes()
accountKeeper.EXPECT().StringToBytes(memberAddr.String()).Return(memberAddr, nil).AnyTimes()

bApp := baseapp.NewBaseApp(
"group",
Expand Down
14 changes: 7 additions & 7 deletions x/group/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (k Keeper) getGroupMembers(ctx sdk.Context, id uint64, pageRequest *query.P
// GroupsByAdmin queries all groups where a given address is admin.
func (k Keeper) GroupsByAdmin(goCtx context.Context, request *group.QueryGroupsByAdminRequest) (*group.QueryGroupsByAdminResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
addr, err := sdk.AccAddressFromBech32(request.Admin)
addr, err := k.accKeeper.StringToBytes(request.Admin)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func (k Keeper) getGroupPoliciesByGroup(ctx sdk.Context, id uint64, pageRequest
// admin.
func (k Keeper) GroupPoliciesByAdmin(goCtx context.Context, request *group.QueryGroupPoliciesByAdminRequest) (*group.QueryGroupPoliciesByAdminResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
addr, err := sdk.AccAddressFromBech32(request.Admin)
addr, err := k.accKeeper.StringToBytes(request.Admin)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -180,7 +180,7 @@ func (k Keeper) Proposal(goCtx context.Context, request *group.QueryProposalRequ
// ProposalsByGroupPolicy queries all proposals of a group policy.
func (k Keeper) ProposalsByGroupPolicy(goCtx context.Context, request *group.QueryProposalsByGroupPolicyRequest) (*group.QueryProposalsByGroupPolicyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
addr, err := sdk.AccAddressFromBech32(request.Address)
addr, err := k.accKeeper.StringToBytes(request.Address)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func (k Keeper) getProposal(ctx sdk.Context, proposalID uint64) (group.Proposal,
// VoteByProposalVoter queries a vote given a voter and a proposal ID.
func (k Keeper) VoteByProposalVoter(goCtx context.Context, request *group.QueryVoteByProposalVoterRequest) (*group.QueryVoteByProposalVoterResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
addr, err := sdk.AccAddressFromBech32(request.Voter)
addr, err := k.accKeeper.StringToBytes(request.Voter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func (k Keeper) VotesByProposal(goCtx context.Context, request *group.QueryVotes
// VotesByVoter queries all votes of a voter.
func (k Keeper) VotesByVoter(goCtx context.Context, request *group.QueryVotesByVoterRequest) (*group.QueryVotesByVoterResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
addr, err := sdk.AccAddressFromBech32(request.Voter)
addr, err := k.accKeeper.StringToBytes(request.Voter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -284,12 +284,12 @@ func (k Keeper) GroupsByMember(goCtx context.Context, request *group.QueryGroups
}

ctx := sdk.UnwrapSDKContext(goCtx)
member, err := sdk.AccAddressFromBech32(request.Address)
member, err := k.accKeeper.StringToBytes(request.Address)
if err != nil {
return nil, err
}

iter, err := k.groupMemberByMemberIndex.GetPaginated(ctx.KVStore(k.key), member.Bytes(), request.Pagination)
iter, err := k.groupMemberByMemberIndex.GetPaginated(ctx.KVStore(k.key), member, request.Pagination)
if err != nil {
return nil, err
}
Expand Down
11 changes: 5 additions & 6 deletions x/group/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ func initKeeper(t *testing.T) (types.Context, groupkeeper.Keeper, []types.AccAdd
addrs := simtestutil.CreateIncrementalAccounts(6)
ctrl := gomock.NewController(t)
accountKeeper := grouptestutil.NewMockAccountKeeper(ctrl)
accountKeeper.EXPECT().GetAccount(gomock.Any(), addrs[0]).Return(authtypes.NewBaseAccountWithAddress(addrs[0])).AnyTimes()
accountKeeper.EXPECT().GetAccount(gomock.Any(), addrs[1]).Return(authtypes.NewBaseAccountWithAddress(addrs[1])).AnyTimes()
accountKeeper.EXPECT().GetAccount(gomock.Any(), addrs[2]).Return(authtypes.NewBaseAccountWithAddress(addrs[2])).AnyTimes()
accountKeeper.EXPECT().GetAccount(gomock.Any(), addrs[3]).Return(authtypes.NewBaseAccountWithAddress(addrs[3])).AnyTimes()
accountKeeper.EXPECT().GetAccount(gomock.Any(), addrs[4]).Return(authtypes.NewBaseAccountWithAddress(addrs[4])).AnyTimes()
accountKeeper.EXPECT().GetAccount(gomock.Any(), addrs[5]).Return(authtypes.NewBaseAccountWithAddress(addrs[5])).AnyTimes()
for _, addr := range addrs {
accountKeeper.EXPECT().GetAccount(gomock.Any(), addr).Return(authtypes.NewBaseAccountWithAddress(addr)).AnyTimes()
accountKeeper.EXPECT().BytesToString(addr).Return(addr.String(), nil).AnyTimes()
accountKeeper.EXPECT().StringToBytes(addr.String()).Return(addr, nil).AnyTimes()
}

groupKeeper = groupkeeper.NewKeeper(key, encCfg.Codec, bApp.MsgServiceRouter(), accountKeeper, group.DefaultConfig())

Expand Down
20 changes: 10 additions & 10 deletions x/group/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func NewKeeper(storeKey storetypes.StoreKey, cdc codec.Codec, router baseapp.Mes
panic(err.Error())
}
k.groupByAdminIndex, err = orm.NewIndex(groupTable, GroupByAdminIndexPrefix, func(val interface{}) ([]interface{}, error) {
addr, err := sdk.AccAddressFromBech32(val.(*group.GroupInfo).Admin)
addr, err := accKeeper.StringToBytes(val.(*group.GroupInfo).Admin)
if err != nil {
return nil, err
}
return []interface{}{addr.Bytes()}, nil
return []interface{}{addr}, nil
}, []byte{})
if err != nil {
panic(err.Error())
Expand All @@ -119,11 +119,11 @@ func NewKeeper(storeKey storetypes.StoreKey, cdc codec.Codec, router baseapp.Mes
}
k.groupMemberByMemberIndex, err = orm.NewIndex(groupMemberTable, GroupMemberByMemberIndexPrefix, func(val interface{}) ([]interface{}, error) {
memberAddr := val.(*group.GroupMember).Member.Address
addr, err := sdk.AccAddressFromBech32(memberAddr)
addr, err := accKeeper.StringToBytes(memberAddr)
if err != nil {
return nil, err
}
return []interface{}{addr.Bytes()}, nil
return []interface{}{addr}, nil
}, []byte{})
if err != nil {
panic(err.Error())
Expand All @@ -144,11 +144,11 @@ func NewKeeper(storeKey storetypes.StoreKey, cdc codec.Codec, router baseapp.Mes
}
k.groupPolicyByAdminIndex, err = orm.NewIndex(groupPolicyTable, GroupPolicyByAdminIndexPrefix, func(value interface{}) ([]interface{}, error) {
admin := value.(*group.GroupPolicyInfo).Admin
addr, err := sdk.AccAddressFromBech32(admin)
addr, err := accKeeper.StringToBytes(admin)
if err != nil {
return nil, err
}
return []interface{}{addr.Bytes()}, nil
return []interface{}{addr}, nil
}, []byte{})
if err != nil {
panic(err.Error())
Expand All @@ -162,11 +162,11 @@ func NewKeeper(storeKey storetypes.StoreKey, cdc codec.Codec, router baseapp.Mes
}
k.proposalByGroupPolicyIndex, err = orm.NewIndex(proposalTable, ProposalByGroupPolicyIndexPrefix, func(value interface{}) ([]interface{}, error) {
account := value.(*group.Proposal).GroupPolicyAddress
addr, err := sdk.AccAddressFromBech32(account)
addr, err := accKeeper.StringToBytes(account)
if err != nil {
return nil, err
}
return []interface{}{addr.Bytes()}, nil
return []interface{}{addr}, nil
}, []byte{})
if err != nil {
panic(err.Error())
Expand All @@ -192,11 +192,11 @@ func NewKeeper(storeKey storetypes.StoreKey, cdc codec.Codec, router baseapp.Mes
panic(err.Error())
}
k.voteByVoterIndex, err = orm.NewIndex(voteTable, VoteByVoterIndexPrefix, func(value interface{}) ([]interface{}, error) {
addr, err := sdk.AccAddressFromBech32(value.(*group.Vote).Voter)
addr, err := accKeeper.StringToBytes(value.(*group.Vote).Voter)
if err != nil {
return nil, err
}
return []interface{}{addr.Bytes()}, nil
return []interface{}{addr}, nil
}, []byte{})
if err != nil {
panic(err.Error())
Expand Down
Loading