Skip to content

Commit

Permalink
distribution tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Apr 3, 2023
1 parent 448ffbc commit 36a7be0
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 35 deletions.
13 changes: 7 additions & 6 deletions x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"strings"

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

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -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",
Expand All @@ -30,7 +31,7 @@ func GetQueryCmd() *cobra.Command {
GetCmdQueryValidatorOutstandingRewards(),
GetCmdQueryValidatorCommission(),
GetCmdQueryValidatorSlashes(),
GetCmdQueryDelegatorRewards(),
GetCmdQueryDelegatorRewards(ac),
GetCmdQueryCommunityPool(),
)

Expand Down Expand Up @@ -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()

Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions x/distribution/client/cli/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions x/distribution/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

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

Expand All @@ -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",
Expand All @@ -38,7 +39,7 @@ func NewTxCmd() *cobra.Command {
distTxCmd.AddCommand(
NewWithdrawRewardsCmd(),
NewWithdrawAllRewardsCmd(),
NewSetWithdrawAddrCmd(),
NewSetWithdrawAddrCmd(ac),
NewFundCommunityPoolCmd(),
NewDepositValidatorRewardsPoolCmd(),
)
Expand Down Expand Up @@ -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{
Expand All @@ -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
}
Expand Down
15 changes: 12 additions & 3 deletions x/distribution/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions x/distribution/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions x/distribution/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
14 changes: 8 additions & 6 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand All @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions x/distribution/testutil/expected_keepers_mocks.go

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

3 changes: 3 additions & 0 deletions x/distribution/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions x/evidence/testutil/expected_keepers_mocks.go

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

0 comments on commit 36a7be0

Please sign in to comment.