From 36a7be0183060bb185b150dfce3f83a28468adf6 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 3 Apr 2023 21:39:21 +0200 Subject: [PATCH] distribution tests --- x/distribution/client/cli/query.go | 13 ++++---- x/distribution/client/cli/suite_test.go | 5 ++-- x/distribution/client/cli/tx.go | 9 +++--- x/distribution/keeper/genesis.go | 15 ++++++++-- x/distribution/keeper/grpc_query.go | 8 ++--- x/distribution/keeper/msg_server.go | 12 ++++---- x/distribution/module.go | 14 +++++---- .../testutil/expected_keepers_mocks.go | 30 +++++++++++++++++++ x/distribution/types/expected_keepers.go | 3 ++ x/evidence/testutil/expected_keepers_mocks.go | 9 +++--- 10 files changed, 83 insertions(+), 35 deletions(-) diff --git a/x/distribution/client/cli/query.go b/x/distribution/client/cli/query.go index 2ffdd5f1158..5999fee397c 100644 --- a/x/distribution/client/cli/query.go +++ b/x/distribution/client/cli/query.go @@ -5,6 +5,7 @@ import ( "strconv" "strings" + "cosmossdk.io/core/address" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" @@ -15,7 +16,7 @@ import ( ) // GetQueryCmd returns the cli query commands for this module -func GetQueryCmd() *cobra.Command { +func GetQueryCmd(ac address.Codec) *cobra.Command { distQueryCmd := &cobra.Command{ Use: types.ModuleName, Short: "Querying commands for the distribution module", @@ -30,7 +31,7 @@ func GetQueryCmd() *cobra.Command { GetCmdQueryValidatorOutstandingRewards(), GetCmdQueryValidatorCommission(), GetCmdQueryValidatorSlashes(), - GetCmdQueryDelegatorRewards(), + GetCmdQueryDelegatorRewards(ac), GetCmdQueryCommunityPool(), ) @@ -265,7 +266,7 @@ $ %s query distribution slashes %svaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj } // GetCmdQueryDelegatorRewards implements the query delegator rewards command. -func GetCmdQueryDelegatorRewards() *cobra.Command { +func GetCmdQueryDelegatorRewards(ac address.Codec) *cobra.Command { bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix() bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix() @@ -290,7 +291,7 @@ $ %s query distribution rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p %s1ggh } queryClient := types.NewQueryClient(clientCtx) - delegatorAddr, err := sdk.AccAddressFromBech32(args[0]) + _, err = ac.StringToBytes(args[0]) if err != nil { return err } @@ -305,7 +306,7 @@ $ %s query distribution rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p %s1ggh res, err := queryClient.DelegationRewards( ctx, - &types.QueryDelegationRewardsRequest{DelegatorAddress: delegatorAddr.String(), ValidatorAddress: validatorAddr.String()}, + &types.QueryDelegationRewardsRequest{DelegatorAddress: args[0], ValidatorAddress: validatorAddr.String()}, ) if err != nil { return err @@ -316,7 +317,7 @@ $ %s query distribution rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p %s1ggh res, err := queryClient.DelegationTotalRewards( ctx, - &types.QueryDelegationTotalRewardsRequest{DelegatorAddress: delegatorAddr.String()}, + &types.QueryDelegationTotalRewardsRequest{DelegatorAddress: args[0]}, ) if err != nil { return err diff --git a/x/distribution/client/cli/suite_test.go b/x/distribution/client/cli/suite_test.go index 150acf8b97d..576a9f561aa 100644 --- a/x/distribution/client/cli/suite_test.go +++ b/x/distribution/client/cli/suite_test.go @@ -14,6 +14,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/keyring" "github.com/cosmos/cosmos-sdk/testutil" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" @@ -422,7 +423,7 @@ total: []`, tc := tc s.Run(tc.name, func() { - cmd := cli.GetCmdQueryDelegatorRewards() + cmd := cli.GetCmdQueryDelegatorRewards(address.NewBech32Codec("cosmos")) out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args) if tc.expectErr { @@ -616,7 +617,7 @@ func (s *CLITestSuite) TestNewSetWithdrawAddrCmd() { tc := tc s.Run(tc.name, func() { - cmd := cli.NewSetWithdrawAddrCmd() + cmd := cli.NewSetWithdrawAddrCmd(address.NewBech32Codec("cosmos")) out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args) if tc.expectErr { diff --git a/x/distribution/client/cli/tx.go b/x/distribution/client/cli/tx.go index 0985d3dfee4..769f9b1ce4c 100644 --- a/x/distribution/client/cli/tx.go +++ b/x/distribution/client/cli/tx.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "cosmossdk.io/core/address" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -26,7 +27,7 @@ const ( ) // NewTxCmd returns a root CLI command handler for all x/distribution transaction commands. -func NewTxCmd() *cobra.Command { +func NewTxCmd(ac address.Codec) *cobra.Command { distTxCmd := &cobra.Command{ Use: types.ModuleName, Short: "Distribution transactions subcommands", @@ -38,7 +39,7 @@ func NewTxCmd() *cobra.Command { distTxCmd.AddCommand( NewWithdrawRewardsCmd(), NewWithdrawAllRewardsCmd(), - NewSetWithdrawAddrCmd(), + NewSetWithdrawAddrCmd(ac), NewFundCommunityPoolCmd(), NewDepositValidatorRewardsPoolCmd(), ) @@ -181,7 +182,7 @@ $ %[1]s tx distribution withdraw-all-rewards --from mykey } // NewSetWithdrawAddrCmd returns a CLI command handler for creating a MsgSetWithdrawAddress transaction. -func NewSetWithdrawAddrCmd() *cobra.Command { +func NewSetWithdrawAddrCmd(ac address.Codec) *cobra.Command { bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix() cmd := &cobra.Command{ @@ -203,7 +204,7 @@ $ %s tx distribution set-withdraw-addr %s1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p return err } delAddr := clientCtx.GetFromAddress() - withdrawAddr, err := sdk.AccAddressFromBech32(args[0]) + withdrawAddr, err := ac.StringToBytes(args[0]) if err != nil { return err } diff --git a/x/distribution/keeper/genesis.go b/x/distribution/keeper/genesis.go index e615fa71a1f..8c6ac54cf39 100644 --- a/x/distribution/keeper/genesis.go +++ b/x/distribution/keeper/genesis.go @@ -18,8 +18,14 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { } for _, dwi := range data.DelegatorWithdrawInfos { - delegatorAddress := sdk.MustAccAddressFromBech32(dwi.DelegatorAddress) - withdrawAddress := sdk.MustAccAddressFromBech32(dwi.WithdrawAddress) + delegatorAddress, err := k.authKeeper.StringToBytes(dwi.DelegatorAddress) + if err != nil { + panic(err) + } + withdrawAddress, err := k.authKeeper.StringToBytes(dwi.WithdrawAddress) + if err != nil { + panic(err) + } k.SetDelegatorWithdrawAddr(ctx, delegatorAddress, withdrawAddress) } @@ -68,7 +74,10 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { if err != nil { panic(err) } - delegatorAddress := sdk.MustAccAddressFromBech32(del.DelegatorAddress) + delegatorAddress, err := k.authKeeper.StringToBytes(del.DelegatorAddress) + if err != nil { + panic(err) + } k.SetDelegatorStartingInfo(ctx, valAddr, delegatorAddress, del.StartingInfo) } diff --git a/x/distribution/keeper/grpc_query.go b/x/distribution/keeper/grpc_query.go index af275689990..024d1026559 100644 --- a/x/distribution/keeper/grpc_query.go +++ b/x/distribution/keeper/grpc_query.go @@ -188,7 +188,7 @@ func (k Querier) DelegationRewards(c context.Context, req *types.QueryDelegation return nil, errors.Wrap(types.ErrNoValidatorExists, req.ValidatorAddress) } - delAdr, err := sdk.AccAddressFromBech32(req.DelegatorAddress) + delAdr, err := k.authKeeper.StringToBytes(req.DelegatorAddress) if err != nil { return nil, err } @@ -218,7 +218,7 @@ func (k Querier) DelegationTotalRewards(c context.Context, req *types.QueryDeleg total := sdk.DecCoins{} var delRewards []types.DelegationDelegatorReward - delAdr, err := sdk.AccAddressFromBech32(req.DelegatorAddress) + delAdr, err := k.authKeeper.StringToBytes(req.DelegatorAddress) if err != nil { return nil, err } @@ -251,7 +251,7 @@ func (k Querier) DelegatorValidators(c context.Context, req *types.QueryDelegato } ctx := sdk.UnwrapSDKContext(c) - delAdr, err := sdk.AccAddressFromBech32(req.DelegatorAddress) + delAdr, err := k.authKeeper.StringToBytes(req.DelegatorAddress) if err != nil { return nil, err } @@ -277,7 +277,7 @@ func (k Querier) DelegatorWithdrawAddress(c context.Context, req *types.QueryDel if req.DelegatorAddress == "" { return nil, status.Error(codes.InvalidArgument, "empty delegator address") } - delAdr, err := sdk.AccAddressFromBech32(req.DelegatorAddress) + delAdr, err := k.authKeeper.StringToBytes(req.DelegatorAddress) if err != nil { return nil, err } diff --git a/x/distribution/keeper/msg_server.go b/x/distribution/keeper/msg_server.go index 37b66e0145b..a52774d767e 100644 --- a/x/distribution/keeper/msg_server.go +++ b/x/distribution/keeper/msg_server.go @@ -29,11 +29,11 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer { func (k msgServer) SetWithdrawAddress(goCtx context.Context, msg *types.MsgSetWithdrawAddress) (*types.MsgSetWithdrawAddressResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - delegatorAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress) + delegatorAddress, err := k.authKeeper.StringToBytes(msg.DelegatorAddress) if err != nil { return nil, err } - withdrawAddress, err := sdk.AccAddressFromBech32(msg.WithdrawAddress) + withdrawAddress, err := k.authKeeper.StringToBytes(msg.WithdrawAddress) if err != nil { return nil, err } @@ -52,7 +52,7 @@ func (k msgServer) WithdrawDelegatorReward(goCtx context.Context, msg *types.Msg if err != nil { return nil, err } - delegatorAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress) + delegatorAddress, err := k.authKeeper.StringToBytes(msg.DelegatorAddress) if err != nil { return nil, err } @@ -106,7 +106,7 @@ func (k msgServer) WithdrawValidatorCommission(goCtx context.Context, msg *types func (k msgServer) FundCommunityPool(goCtx context.Context, msg *types.MsgFundCommunityPool) (*types.MsgFundCommunityPoolResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - depositer, err := sdk.AccAddressFromBech32(msg.Depositor) + depositer, err := k.authKeeper.StringToBytes(msg.Depositor) if err != nil { return nil, err } @@ -142,7 +142,7 @@ func (k msgServer) CommunityPoolSpend(goCtx context.Context, req *types.MsgCommu ctx := sdk.UnwrapSDKContext(goCtx) - recipient, err := sdk.AccAddressFromBech32(req.Recipient) + recipient, err := k.authKeeper.StringToBytes(req.Recipient) if err != nil { return nil, err } @@ -164,7 +164,7 @@ func (k msgServer) CommunityPoolSpend(goCtx context.Context, req *types.MsgCommu func (k msgServer) DepositValidatorRewardsPool(goCtx context.Context, req *types.MsgDepositValidatorRewardsPool) (*types.MsgDepositValidatorRewardsPoolResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - authority, err := sdk.AccAddressFromBech32(req.Authority) + authority, err := k.authKeeper.StringToBytes(req.Authority) if err != nil { return nil, err } diff --git a/x/distribution/module.go b/x/distribution/module.go index 11cebd12971..4420ffd3492 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/cobra" modulev1 "cosmossdk.io/api/cosmos/distribution/module/v1" + "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" "cosmossdk.io/depinject" @@ -42,6 +43,7 @@ var ( // AppModuleBasic defines the basic application module used by the distribution module. type AppModuleBasic struct { cdc codec.Codec + ac address.Codec } // Name returns the distribution module's name. @@ -78,17 +80,17 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux } // GetTxCmd returns the root tx command for the distribution module. -func (AppModuleBasic) GetTxCmd() *cobra.Command { - return cli.NewTxCmd() +func (ab AppModuleBasic) GetTxCmd() *cobra.Command { + return cli.NewTxCmd(ab.ac) } // GetQueryCmd returns the root query command for the distribution module. -func (AppModuleBasic) GetQueryCmd() *cobra.Command { - return cli.GetQueryCmd() +func (ab AppModuleBasic) GetQueryCmd() *cobra.Command { + return cli.GetQueryCmd(ab.ac) } // RegisterInterfaces implements InterfaceModule -func (b AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) { +func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) { types.RegisterInterfaces(registry) } @@ -111,7 +113,7 @@ func NewAppModule( bankKeeper types.BankKeeper, stakingKeeper types.StakingKeeper, ss exported.Subspace, ) AppModule { return AppModule{ - AppModuleBasic: AppModuleBasic{cdc: cdc}, + AppModuleBasic: AppModuleBasic{cdc: cdc, ac: accountKeeper}, keeper: keeper, accountKeeper: accountKeeper, bankKeeper: bankKeeper, diff --git a/x/distribution/testutil/expected_keepers_mocks.go b/x/distribution/testutil/expected_keepers_mocks.go index 550463aee2c..31471ca6e76 100644 --- a/x/distribution/testutil/expected_keepers_mocks.go +++ b/x/distribution/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(ctx context.Context, addr types.AccAddress) types.AccountI { m.ctrl.T.Helper() @@ -90,6 +105,21 @@ func (mr *MockAccountKeeperMockRecorder) SetModuleAccount(arg0, arg1 interface{} return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetModuleAccount", reflect.TypeOf((*MockAccountKeeper)(nil).SetModuleAccount), 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/distribution/types/expected_keepers.go b/x/distribution/types/expected_keepers.go index ca497e56be2..8556d6e3ff4 100644 --- a/x/distribution/types/expected_keepers.go +++ b/x/distribution/types/expected_keepers.go @@ -3,12 +3,15 @@ package types import ( context "context" + "cosmossdk.io/core/address" sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) // AccountKeeper defines the expected account keeper used for simulations (noalias) type AccountKeeper interface { + address.Codec + GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI GetModuleAddress(name string) sdk.AccAddress diff --git a/x/evidence/testutil/expected_keepers_mocks.go b/x/evidence/testutil/expected_keepers_mocks.go index 845322d2ce1..e6b93cdf7f4 100644 --- a/x/evidence/testutil/expected_keepers_mocks.go +++ b/x/evidence/testutil/expected_keepers_mocks.go @@ -9,6 +9,7 @@ import ( reflect "reflect" time "time" + math "cosmossdk.io/math" types "github.com/cosmos/cosmos-sdk/crypto/types" types0 "github.com/cosmos/cosmos-sdk/types" types1 "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -157,7 +158,7 @@ func (mr *MockSlashingKeeperMockRecorder) JailUntil(arg0, arg1, arg2 interface{} } // Slash mocks base method. -func (m *MockSlashingKeeper) Slash(arg0 types0.Context, arg1 types0.ConsAddress, arg2 types0.Dec, arg3, arg4 int64) { +func (m *MockSlashingKeeper) Slash(arg0 types0.Context, arg1 types0.ConsAddress, arg2 math.LegacyDec, arg3, arg4 int64) { m.ctrl.T.Helper() m.ctrl.Call(m, "Slash", arg0, arg1, arg2, arg3, arg4) } @@ -169,10 +170,10 @@ func (mr *MockSlashingKeeperMockRecorder) Slash(arg0, arg1, arg2, arg3, arg4 int } // SlashFractionDoubleSign mocks base method. -func (m *MockSlashingKeeper) SlashFractionDoubleSign(arg0 types0.Context) types0.Dec { +func (m *MockSlashingKeeper) SlashFractionDoubleSign(arg0 types0.Context) math.LegacyDec { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SlashFractionDoubleSign", arg0) - ret0, _ := ret[0].(types0.Dec) + ret0, _ := ret[0].(math.LegacyDec) return ret0 } @@ -183,7 +184,7 @@ func (mr *MockSlashingKeeperMockRecorder) SlashFractionDoubleSign(arg0 interface } // SlashWithInfractionReason mocks base method. -func (m *MockSlashingKeeper) SlashWithInfractionReason(arg0 types0.Context, arg1 types0.ConsAddress, arg2 types0.Dec, arg3, arg4 int64, arg5 types1.Infraction) { +func (m *MockSlashingKeeper) SlashWithInfractionReason(arg0 types0.Context, arg1 types0.ConsAddress, arg2 math.LegacyDec, arg3, arg4 int64, arg5 types1.Infraction) { m.ctrl.T.Helper() m.ctrl.Call(m, "SlashWithInfractionReason", arg0, arg1, arg2, arg3, arg4, arg5) }