diff --git a/server/mock/store.go b/server/mock/store.go index 31b73f3fd86f..2094e2905d0a 100644 --- a/server/mock/store.go +++ b/server/mock/store.go @@ -168,6 +168,10 @@ func (ms multiStore) LatestVersion() int64 { panic("not implemented") } +func (ms multiStore) WorkingHash() []byte { + panic("not implemented") +} + var _ storetypes.KVStore = kvStore{} type kvStore struct { diff --git a/tests/e2e/group/query.go b/tests/e2e/group/query.go index 27c7b609e692..c4cbc4fbded9 100644 --- a/tests/e2e/group/query.go +++ b/tests/e2e/group/query.go @@ -393,7 +393,7 @@ func (s *E2ETestSuite) TestQueryGroupPolicyInfo() { "group policy not found", []string{val.Address.String(), fmt.Sprintf("--%s=json", flags.FlagOutput)}, true, - "group policy: not found", + "not found", 0, }, { diff --git a/tests/e2e/group/tx.go b/tests/e2e/group/tx.go index 6b6265c6c73b..be371cb9ab93 100644 --- a/tests/e2e/group/tx.go +++ b/tests/e2e/group/tx.go @@ -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" @@ -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 { @@ -1608,7 +1609,7 @@ func (s *E2ETestSuite) TestTxSubmitProposal() { s.commonFlags..., ), false, - "group policy: not found", + "not found", &sdk.TxResponse{}, sdkerrors.ErrNotFound.ABCICode(), }, diff --git a/x/group/client/cli/tx.go b/x/group/client/cli/tx.go index 93dc5627d16b..349e3c747925 100644 --- a/x/group/client/cli/tx.go +++ b/x/group/client/cli/tx.go @@ -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" ) @@ -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", @@ -39,7 +39,7 @@ func TxCmd(name string) *cobra.Command { MsgCreateGroupWithPolicyCmd(), MsgCreateGroupPolicyCmd(), MsgUpdateGroupPolicyAdminCmd(), - MsgUpdateGroupPolicyDecisionPolicyCmd(), + MsgUpdateGroupPolicyDecisionPolicyCmd(ac), MsgUpdateGroupPolicyMetadataCmd(), MsgWithdrawProposalCmd(), MsgSubmitProposalCmd(), @@ -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", @@ -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 } diff --git a/x/group/client/cli/tx_test.go b/x/group/client/cli/tx_test.go index f9377eb67dba..7527b9db6f23 100644 --- a/x/group/client/cli/tx_test.go +++ b/x/group/client/cli/tx_test.go @@ -18,6 +18,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" @@ -1182,7 +1183,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 { diff --git a/x/group/expected_keepers.go b/x/group/expected_keepers.go index 037d639604a3..e346fd513652 100644 --- a/x/group/expected_keepers.go +++ b/x/group/expected_keepers.go @@ -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 diff --git a/x/group/keeper/genesis_test.go b/x/group/keeper/genesis_test.go index 4b3a4107fd44..109cc1c2cf68 100644 --- a/x/group/keeper/genesis_test.go +++ b/x/group/keeper/genesis_test.go @@ -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", diff --git a/x/group/keeper/grpc_query.go b/x/group/keeper/grpc_query.go index a54e3bedf293..aeabe5ca4700 100644 --- a/x/group/keeper/grpc_query.go +++ b/x/group/keeper/grpc_query.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/x/group/keeper/grpc_query_test.go b/x/group/keeper/grpc_query_test.go index a57165751511..ed37c6928a16 100644 --- a/x/group/keeper/grpc_query_test.go +++ b/x/group/keeper/grpc_query_test.go @@ -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()) diff --git a/x/group/keeper/keeper.go b/x/group/keeper/keeper.go index 73df21e9372f..c3b705b2c369 100644 --- a/x/group/keeper/keeper.go +++ b/x/group/keeper/keeper.go @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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()) diff --git a/x/group/keeper/keeper_test.go b/x/group/keeper/keeper_test.go index 40a25f591eba..53d95b84a478 100644 --- a/x/group/keeper/keeper_test.go +++ b/x/group/keeper/keeper_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/binary" + "errors" "fmt" "sort" "strings" @@ -18,6 +19,7 @@ import ( storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/testutil" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" "github.com/cosmos/cosmos-sdk/testutil/testdata" @@ -64,7 +66,13 @@ func (s *TestSuite) SetupTest() { s.accountKeeper = grouptestutil.NewMockAccountKeeper(ctrl) for i := range s.addrs { s.accountKeeper.EXPECT().GetAccount(gomock.Any(), s.addrs[i]).Return(authtypes.NewBaseAccountWithAddress(s.addrs[i])).AnyTimes() + s.accountKeeper.EXPECT().BytesToString(s.addrs[i]).Return(s.addrs[i].String(), nil).AnyTimes() + s.accountKeeper.EXPECT().StringToBytes(s.addrs[i].String()).Return(s.addrs[i], nil).AnyTimes() } + + // add empty string to the list of expected calls + s.accountKeeper.EXPECT().StringToBytes("").Return(nil, errors.New("unable to decode")).AnyTimes() + s.bankKeeper = grouptestutil.NewMockBankKeeper(ctrl) bApp := baseapp.NewBaseApp( @@ -113,10 +121,11 @@ func (s *TestSuite) SetupTest() { policyRes, err := s.groupKeeper.CreateGroupPolicy(s.ctx, policyReq) s.Require().NoError(err) - s.policy = policy - addr, err := sdk.AccAddressFromBech32(policyRes.Address) + + addrbz, err := address.NewBech32Codec("cosmos").StringToBytes(policyRes.Address) s.Require().NoError(err) - s.groupPolicyAddr = addr + s.policy = policy + s.groupPolicyAddr = addrbz s.bankKeeper.EXPECT().MintCoins(s.sdkCtx, minttypes.ModuleName, sdk.Coins{sdk.NewInt64Coin("test", 100000)}).Return(nil).AnyTimes() s.bankKeeper.MintCoins(s.sdkCtx, minttypes.ModuleName, sdk.Coins{sdk.NewInt64Coin("test", 100000)}) @@ -139,9 +148,15 @@ func (s *TestSuite) setNextAccount() { s.Require().NoError(err) groupPolicyAccBumpAccountNumber.SetAccountNumber(nextAccVal) + addrcdc := address.NewBech32Codec("cosmos") + addrst, err := addrcdc.BytesToString(ac.Address()) + s.Require().NoError(err) + s.accountKeeper.EXPECT().GetAccount(gomock.Any(), sdk.AccAddress(ac.Address())).Return(nil).AnyTimes() s.accountKeeper.EXPECT().NewAccount(gomock.Any(), groupPolicyAcc).Return(groupPolicyAccBumpAccountNumber).AnyTimes() s.accountKeeper.EXPECT().SetAccount(gomock.Any(), sdk.AccountI(groupPolicyAccBumpAccountNumber)).Return().AnyTimes() + s.accountKeeper.EXPECT().StringToBytes(addrst).Return(ac.Address().Bytes(), nil).AnyTimes() + s.accountKeeper.EXPECT().BytesToString(ac.Address().Bytes()).Return(addrst, nil).AnyTimes() } func TestKeeperTestSuite(t *testing.T) { @@ -163,6 +178,8 @@ func (s *TestSuite) createGroupAndGetMembers(numMembers int) []*group.GroupMembe Address: addressPool[i].String(), Weight: "1", } + s.accountKeeper.EXPECT().StringToBytes(addressPool[i].String()).Return(addressPool[i].Bytes(), nil).AnyTimes() + s.accountKeeper.EXPECT().BytesToString(addressPool[i].Bytes()).Return(addressPool[i].String(), nil).AnyTimes() } g, err := s.groupKeeper.CreateGroup(s.ctx, &group.MsgCreateGroup{ @@ -308,9 +325,9 @@ func (s *TestSuite) TestCreateGroup() { s.Require().Equal(len(members), len(loadedMembers)) // we reorder members by address to be able to compare them sort.Slice(members, func(i, j int) bool { - addri, err := sdk.AccAddressFromBech32(members[i].Address) + addri, err := s.accountKeeper.StringToBytes(members[i].Address) s.Require().NoError(err) - addrj, err := sdk.AccAddressFromBech32(members[j].Address) + addrj, err := s.accountKeeper.StringToBytes(members[j].Address) s.Require().NoError(err) return bytes.Compare(addri, addrj) < 0 }) @@ -768,9 +785,9 @@ func (s *TestSuite) TestUpdateGroupMembers() { s.Require().Equal(len(spec.expMembers), len(loadedMembers)) // we reorder group members by address to be able to compare them sort.Slice(spec.expMembers, func(i, j int) bool { - addri, err := sdk.AccAddressFromBech32(spec.expMembers[i].Member.Address) + addri, err := s.accountKeeper.StringToBytes(spec.expMembers[i].Member.Address) s.Require().NoError(err) - addrj, err := sdk.AccAddressFromBech32(spec.expMembers[j].Member.Address) + addrj, err := s.accountKeeper.StringToBytes(spec.expMembers[j].Member.Address) s.Require().NoError(err) return bytes.Compare(addri, addrj) < 0 }) @@ -962,9 +979,9 @@ func (s *TestSuite) TestCreateGroupWithPolicy() { s.Require().Equal(len(members), len(loadedMembers)) // we reorder members by address to be able to compare them sort.Slice(members, func(i, j int) bool { - addri, err := sdk.AccAddressFromBech32(members[i].Address) + addri, err := s.accountKeeper.StringToBytes(members[i].Address) s.Require().NoError(err) - addrj, err := sdk.AccAddressFromBech32(members[j].Address) + addrj, err := s.accountKeeper.StringToBytes(members[j].Address) s.Require().NoError(err) return bytes.Compare(addri, addrj) < 0 }) @@ -1594,7 +1611,10 @@ func (s *TestSuite) TestSubmitProposal() { s.setNextAccount() res, err := s.groupKeeper.CreateGroupPolicy(s.ctx, policyReq) s.Require().NoError(err) - noMinExecPeriodPolicyAddr := sdk.MustAccAddressFromBech32(res.Address) + + s.accountKeeper.EXPECT().StringToBytes(res.Address).Return(sdk.MustAccAddressFromBech32(res.Address).Bytes(), nil).AnyTimes() + noMinExecPeriodPolicyAddr, err := s.accountKeeper.StringToBytes(res.Address) + s.Require().NoError(err) // Create a new group policy with super high threshold bigThresholdPolicy := group.NewThresholdDecisionPolicy( @@ -1610,7 +1630,7 @@ func (s *TestSuite) TestSubmitProposal() { bigThresholdAddr := bigThresholdRes.Address msgSend := &banktypes.MsgSend{ - FromAddress: noMinExecPeriodPolicyAddr.String(), + FromAddress: res.Address, ToAddress: addr2.String(), Amount: sdk.Coins{sdk.NewInt64Coin("test", 100)}, } @@ -1732,13 +1752,13 @@ func (s *TestSuite) TestSubmitProposal() { } }, req: &group.MsgSubmitProposal{ - GroupPolicyAddress: noMinExecPeriodPolicyAddr.String(), + GroupPolicyAddress: res.Address, Proposers: []string{addr2.String()}, Exec: group.Exec_EXEC_TRY, }, msgs: []sdk.Msg{msgSend}, expProposal: group.Proposal{ - GroupPolicyAddress: noMinExecPeriodPolicyAddr.String(), + GroupPolicyAddress: res.Address, Status: group.PROPOSAL_STATUS_ACCEPTED, FinalTallyResult: group.TallyResult{ YesCount: "2", @@ -1760,13 +1780,13 @@ func (s *TestSuite) TestSubmitProposal() { }, "with try exec, not enough yes votes for proposal to pass": { req: &group.MsgSubmitProposal{ - GroupPolicyAddress: noMinExecPeriodPolicyAddr.String(), + GroupPolicyAddress: res.Address, Proposers: []string{addr5.String()}, Exec: group.Exec_EXEC_TRY, }, msgs: []sdk.Msg{msgSend}, expProposal: group.Proposal{ - GroupPolicyAddress: noMinExecPeriodPolicyAddr.String(), + GroupPolicyAddress: res.Address, Status: group.PROPOSAL_STATUS_SUBMITTED, FinalTallyResult: group.TallyResult{ YesCount: "0", // Since tally doesn't pass Allow(), we consider the proposal not final @@ -1950,7 +1970,11 @@ func (s *TestSuite) TestVote() { policyRes, err := s.groupKeeper.CreateGroupPolicy(s.ctx, policyReq) s.Require().NoError(err) accountAddr := policyRes.Address - groupPolicy, err := sdk.AccAddressFromBech32(accountAddr) + // module account will be created and returned + addrbz, err := address.NewBech32Codec("cosmos").StringToBytes(accountAddr) + s.Require().NoError(err) + s.accountKeeper.EXPECT().StringToBytes(accountAddr).Return(addrbz, nil).AnyTimes() + groupPolicy, err := s.accountKeeper.StringToBytes(accountAddr) s.Require().NoError(err) s.Require().NotNil(groupPolicy) @@ -2956,6 +2980,11 @@ func (s *TestSuite) TestLeaveGroup() { admin3 := addrs[6] require := s.Require() + for _, addr := range addrs { + s.accountKeeper.EXPECT().StringToBytes(addr.String()).Return(addr.Bytes(), nil).AnyTimes() + s.accountKeeper.EXPECT().BytesToString(addr).Return(addr.String(), nil).AnyTimes() + } + members := []group.MemberRequest{ { Address: member1.String(), @@ -3262,7 +3291,7 @@ func (s *TestSuite) TestTallyProposalsAtVPEnd() { groupRes, err := s.groupKeeper.CreateGroupWithPolicy(s.ctx, groupMsg) s.Require().NoError(err) accountAddr := groupRes.GetGroupPolicyAddress() - groupPolicy, err := sdk.AccAddressFromBech32(accountAddr) + groupPolicy, err := s.accountKeeper.StringToBytes(accountAddr) s.Require().NoError(err) s.Require().NotNil(groupPolicy) diff --git a/x/group/keeper/msg_server.go b/x/group/keeper/msg_server.go index 999fbfe4f3de..4f16f25fc052 100644 --- a/x/group/keeper/msg_server.go +++ b/x/group/keeper/msg_server.go @@ -1,6 +1,7 @@ package keeper import ( + "bytes" "context" "encoding/binary" "fmt" @@ -263,7 +264,7 @@ func (k Keeper) CreateGroupWithPolicy(goCtx context.Context, req *group.MsgCreat } policyAddr := groupPolicyRes.Address - groupPolicyAddr, err = sdk.AccAddressFromBech32(policyAddr) + groupPolicyAddr, err = k.accKeeper.StringToBytes(policyAddr) if err != nil { return nil, errorsmod.Wrap(err, "group policy address") } @@ -296,7 +297,7 @@ func (k Keeper) CreateGroupWithPolicy(goCtx context.Context, req *group.MsgCreat func (k Keeper) CreateGroupPolicy(goCtx context.Context, req *group.MsgCreateGroupPolicy) (*group.MsgCreateGroupPolicyResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - admin, err := sdk.AccAddressFromBech32(req.GetAdmin()) + admin, err := k.accKeeper.StringToBytes(req.GetAdmin()) if err != nil { return nil, errorsmod.Wrap(err, "request admin") } @@ -315,12 +316,12 @@ func (k Keeper) CreateGroupPolicy(goCtx context.Context, req *group.MsgCreateGro if err != nil { return nil, err } - groupAdmin, err := sdk.AccAddressFromBech32(g.Admin) + groupAdmin, err := k.accKeeper.StringToBytes(g.Admin) if err != nil { return nil, errorsmod.Wrap(err, "group admin") } // Only current group admin is authorized to create a group policy for this - if !groupAdmin.Equals(admin) { + if !bytes.Equal(groupAdmin, admin) { return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "not group admin") } @@ -461,7 +462,7 @@ func (k Keeper) UpdateGroupPolicyMetadata(goCtx context.Context, req *group.MsgU func (k Keeper) SubmitProposal(goCtx context.Context, req *group.MsgSubmitProposal) (*group.MsgSubmitProposalResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - groupPolicyAddr, err := sdk.AccAddressFromBech32(req.GroupPolicyAddress) + groupPolicyAddr, err := k.accKeeper.StringToBytes(req.GroupPolicyAddress) if err != nil { return nil, errorsmod.Wrap(err, "request account address of group policy") } @@ -486,7 +487,7 @@ func (k Keeper) SubmitProposal(goCtx context.Context, req *group.MsgSubmitPropos policyAcc, err := k.getGroupPolicyInfo(ctx, req.GroupPolicyAddress) if err != nil { - return nil, errorsmod.Wrap(err, "load group policy") + return nil, errorsmod.Wrapf(err, "load group policy: %s", req.GroupPolicyAddress) } g, err := k.getGroupInfo(ctx, policyAcc.GroupId) @@ -761,7 +762,7 @@ func (k Keeper) Exec(goCtx context.Context, req *group.MsgExec) (*group.MsgExecR // Caching context so that we don't update the store in case of failure. cacheCtx, flush := ctx.CacheContext() - addr, err := sdk.AccAddressFromBech32(policyInfo.Address) + addr, err := k.accKeeper.StringToBytes(policyInfo.Address) if err != nil { return nil, err } @@ -812,7 +813,7 @@ func (k Keeper) Exec(goCtx context.Context, req *group.MsgExec) (*group.MsgExecR // LeaveGroup implements the MsgServer/LeaveGroup method. func (k Keeper) LeaveGroup(goCtx context.Context, req *group.MsgLeaveGroup) (*group.MsgLeaveGroupResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - _, err := sdk.AccAddressFromBech32(req.Address) + _, err := k.accKeeper.StringToBytes(req.Address) if err != nil { return nil, err } @@ -903,18 +904,18 @@ func (k Keeper) doUpdateGroupPolicy(ctx sdk.Context, groupPolicy, admin string, return errorsmod.Wrap(err, "load group policy") } - groupPolicyAddr, err := sdk.AccAddressFromBech32(groupPolicy) + groupPolicyAddr, err := k.accKeeper.StringToBytes(groupPolicy) if err != nil { return errorsmod.Wrap(err, "group policy address") } - groupPolicyAdmin, err := sdk.AccAddressFromBech32(admin) + _, err = k.accKeeper.StringToBytes(admin) if err != nil { return errorsmod.Wrap(err, "group policy admin") } // Only current group policy admin is authorized to update a group policy. - if groupPolicyAdmin.String() != groupPolicyInfo.Admin { + if admin != groupPolicyInfo.Admin { return errorsmod.Wrap(sdkerrors.ErrUnauthorized, "not group policy admin") } @@ -956,15 +957,15 @@ func (k Keeper) doAuthenticated(ctx sdk.Context, req authNGroupReq, action actio if err != nil { return err } - admin, err := sdk.AccAddressFromBech32(group.Admin) + admin, err := k.accKeeper.StringToBytes(group.Admin) if err != nil { return errorsmod.Wrap(err, "group admin") } - reqAdmin, err := sdk.AccAddressFromBech32(req.GetAdmin()) + reqAdmin, err := k.accKeeper.StringToBytes(req.GetAdmin()) if err != nil { return errorsmod.Wrap(err, "request admin") } - if !admin.Equals(reqAdmin) { + if !bytes.Equal(admin, reqAdmin) { return errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "not group admin; got %s, expected %s", req.GetAdmin(), group.Admin) } if err := action(&group); err != nil { diff --git a/x/group/module/abci_test.go b/x/group/module/abci_test.go index 7d09c0ba5d80..c669fb3b4654 100644 --- a/x/group/module/abci_test.go +++ b/x/group/module/abci_test.go @@ -5,10 +5,11 @@ import ( "testing" "time" - sdkmath "cosmossdk.io/math" + "cosmossdk.io/core/address" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" cmttime "github.com/cometbft/cometbft/types/time" + codecaddress "github.com/cosmos/cosmos-sdk/codec/address" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/runtime" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" @@ -34,6 +35,8 @@ type IntegrationTestSuite struct { bankKeeper bankkeeper.Keeper stakingKeeper *stakingkeeper.Keeper interfaceRegistry codectypes.InterfaceRegistry + + addressCodec address.Codec } func TestIntegrationTestSuite(t *testing.T) { @@ -55,7 +58,10 @@ func (s *IntegrationTestSuite) SetupTest() { ctx = ctx.WithBlockHeader(cmtproto.Header{Time: cmttime.Now()}) s.ctx = ctx - s.addrs = simtestutil.AddTestAddrsIncremental(s.bankKeeper, s.stakingKeeper, ctx, 4, sdkmath.NewInt(30000000)) + + s.addrs = simtestutil.AddTestAddrsIncremental(s.bankKeeper, s.stakingKeeper, ctx, 4, sdk.NewInt(30000000)) + + s.addressCodec = codecaddress.NewBech32Codec("cosmos") } func (s *IntegrationTestSuite) TestEndBlockerPruning() { @@ -64,13 +70,16 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() { addr2 := s.addrs[1] addr3 := s.addrs[2] + addr1st, err := s.addressCodec.BytesToString(addr1) + s.Require().NoError(err) + // Initial group, group policy and balance setup members := []group.MemberRequest{ - {Address: addr1.String(), Weight: "1"}, {Address: addr2.String(), Weight: "2"}, + {Address: addr1st, Weight: "1"}, {Address: addr2.String(), Weight: "2"}, } groupRes, err := s.groupKeeper.CreateGroup(ctx, &group.MsgCreateGroup{ - Admin: addr1.String(), + Admin: addr1st, Members: members, }) s.Require().NoError(err) @@ -116,23 +125,23 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() { policyRes2, err := s.groupKeeper.CreateGroupPolicy(ctx, policyReq2) s.Require().NoError(err) - groupPolicyAddr, err := sdk.AccAddressFromBech32(policyRes.Address) + groupPolicyAddr, err := s.addressCodec.StringToBytes(policyRes.Address) s.Require().NoError(err) s.Require().NoError(testutil.FundAccount(s.bankKeeper, ctx, groupPolicyAddr, sdk.Coins{sdk.NewInt64Coin("test", 10000)})) - groupPolicyAddr2, err := sdk.AccAddressFromBech32(policyRes2.Address) + groupPolicyAddr2, err := s.addressCodec.StringToBytes(policyRes2.Address) s.Require().NoError(err) s.Require().NoError(testutil.FundAccount(s.bankKeeper, ctx, groupPolicyAddr2, sdk.Coins{sdk.NewInt64Coin("test", 10000)})) votingPeriod := policy.GetVotingPeriod() msgSend1 := &banktypes.MsgSend{ - FromAddress: groupPolicyAddr.String(), + FromAddress: policyRes.Address, ToAddress: addr2.String(), Amount: sdk.Coins{sdk.NewInt64Coin("test", 100)}, } msgSend2 := &banktypes.MsgSend{ - FromAddress: groupPolicyAddr2.String(), + FromAddress: policyRes2.Address, ToAddress: addr2.String(), Amount: sdk.Coins{sdk.NewInt64Coin("test", 100)}, } @@ -215,7 +224,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() { s.Require().NoError(err) _, err = s.groupKeeper.UpdateGroupPolicyMetadata(ctx, &group.MsgUpdateGroupPolicyMetadata{ Admin: addr1.String(), - GroupPolicyAddress: groupPolicyAddr.String(), + GroupPolicyAddress: policyRes.Address, }) s.Require().NoError(err) _, err = s.groupKeeper.Exec(ctx, &group.MsgExec{Executor: addr3.String(), ProposalId: pID}) @@ -281,7 +290,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() { policy := group.NewThresholdDecisionPolicy("3", time.Second, 0) msg := &group.MsgUpdateGroupPolicyDecisionPolicy{ Admin: s.addrs[1].String(), - GroupPolicyAddress: groupPolicyAddr2.String(), + GroupPolicyAddress: policyRes2.Address, } err = msg.SetDecisionPolicy(policy) s.Require().NoError(err) @@ -303,7 +312,7 @@ func (s *IntegrationTestSuite) TestEndBlockerPruning() { policy := group.NewThresholdDecisionPolicy("3", time.Second, 0) msg := &group.MsgUpdateGroupPolicyDecisionPolicy{ Admin: s.addrs[1].String(), - GroupPolicyAddress: groupPolicyAddr2.String(), + GroupPolicyAddress: policyRes2.Address, } err = msg.SetDecisionPolicy(policy) s.Require().NoError(err) @@ -391,13 +400,13 @@ func (s *IntegrationTestSuite) TestEndBlockerTallying() { policyRes, err := s.groupKeeper.CreateGroupPolicy(ctx, policyReq) s.Require().NoError(err) - groupPolicyAddr, err := sdk.AccAddressFromBech32(policyRes.Address) + groupPolicyAddr, err := s.addressCodec.StringToBytes(policyRes.Address) s.Require().NoError(err) votingPeriod := policy.GetVotingPeriod() msgSend := &banktypes.MsgSend{ - FromAddress: groupPolicyAddr.String(), + FromAddress: policyRes.Address, ToAddress: addrs[3].String(), Amount: sdk.Coins{sdk.NewInt64Coin("test", 100)}, } diff --git a/x/group/module/module.go b/x/group/module/module.go index e95536e70e8d..d073242f2263 100644 --- a/x/group/module/module.go +++ b/x/group/module/module.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/cobra" modulev1 "cosmossdk.io/api/cosmos/group/module/v1" + "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" "cosmossdk.io/depinject" @@ -47,7 +48,7 @@ type AppModule struct { // NewAppModule creates a new AppModule object func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak group.AccountKeeper, bk group.BankKeeper, registry cdctypes.InterfaceRegistry) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{cdc: cdc}, + AppModuleBasic: AppModuleBasic{cdc: cdc, ac: ak}, keeper: keeper, bankKeeper: bk, accKeeper: ak, @@ -68,6 +69,7 @@ func (am AppModule) IsAppModule() {} type AppModuleBasic struct { cdc codec.Codec + ac address.Codec } // Name returns the group module's name. @@ -97,7 +99,7 @@ func (a AppModuleBasic) GetQueryCmd() *cobra.Command { // GetTxCmd returns the transaction commands for the group module func (a AppModuleBasic) GetTxCmd() *cobra.Command { - return cli.TxCmd(a.Name()) + return cli.TxCmd(a.Name(), a.ac) } // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the group module. diff --git a/x/group/testutil/expected_keepers_mocks.go b/x/group/testutil/expected_keepers_mocks.go index a03a662ee176..ae05d4e8e1ff 100644 --- a/x/group/testutil/expected_keepers_mocks.go +++ b/x/group/testutil/expected_keepers_mocks.go @@ -36,6 +36,21 @@ func (m *MockAccountKeeper) EXPECT() *MockAccountKeeperMockRecorder { return m.recorder } +// BytesToString mocks base method. +func (m *MockAccountKeeper) BytesToString(bz []byte) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "BytesToString", bz) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// BytesToString indicates an expected call of BytesToString. +func (mr *MockAccountKeeperMockRecorder) BytesToString(bz interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BytesToString", reflect.TypeOf((*MockAccountKeeper)(nil).BytesToString), bz) +} + // GetAccount mocks base method. func (m *MockAccountKeeper) GetAccount(arg0 context.Context, arg1 types.AccAddress) types.AccountI { m.ctrl.T.Helper() @@ -88,6 +103,21 @@ func (mr *MockAccountKeeperMockRecorder) SetAccount(arg0, arg1 interface{}) *gom return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetAccount", reflect.TypeOf((*MockAccountKeeper)(nil).SetAccount), arg0, arg1) } +// StringToBytes mocks base method. +func (m *MockAccountKeeper) StringToBytes(text string) ([]byte, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "StringToBytes", text) + ret0, _ := ret[0].([]byte) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// StringToBytes indicates an expected call of StringToBytes. +func (mr *MockAccountKeeperMockRecorder) StringToBytes(text interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StringToBytes", reflect.TypeOf((*MockAccountKeeper)(nil).StringToBytes), text) +} + // MockBankKeeper is a mock of BankKeeper interface. type MockBankKeeper struct { ctrl *gomock.Controller diff --git a/x/mint/testutil/expected_keepers_mocks.go b/x/mint/testutil/expected_keepers_mocks.go index 9fb2e88af02e..6504d5122930 100644 --- a/x/mint/testutil/expected_keepers_mocks.go +++ b/x/mint/testutil/expected_keepers_mocks.go @@ -37,10 +37,10 @@ func (m *MockStakingKeeper) EXPECT() *MockStakingKeeperMockRecorder { } // BondedRatio mocks base method. -func (m *MockStakingKeeper) BondedRatio(ctx types.Context) types.Dec { +func (m *MockStakingKeeper) BondedRatio(ctx types.Context) math.LegacyDec { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "BondedRatio", ctx) - ret0, _ := ret[0].(types.Dec) + ret0, _ := ret[0].(math.LegacyDec) return ret0 }