refactor(distribution): remove global bech32 (#15684)

## Description

ref #13140 

remove global bech32 in distribution

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

* [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
* [ ] added `!` to the type prefix if API or client breaking change
* [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
* [ ] provided a link to the relevant issue or specification
* [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules)
* [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
* [ ] added a changelog entry to `CHANGELOG.md`
* [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
* [ ] updated the relevant documentation or specification
* [ ] reviewed "Files changed" and left comments if necessary
* [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

* [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
* [ ] confirmed `!` in the type prefix if API or client breaking change
* [ ] confirmed all author checklist items have been addressed 
* [ ] reviewed state machine logic
* [ ] reviewed API design and naming
* [ ] reviewed documentation is accurate
* [ ] reviewed tests and test coverage
* [ ] manually tested (if applicable)
This commit is contained in:
Marko 2023-04-05 16:14:51 +02:00 committed by GitHub
parent f5eb9d8887
commit e2a8f7ca32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 86 additions and 37 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec/address"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -440,7 +441,7 @@ total:
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryDelegatorRewards()
cmd := cli.GetCmdQueryDelegatorRewards(address.NewBech32Codec("cosmos"))
clientCtx := val.ClientCtx
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
@ -724,7 +725,7 @@ func (s *E2ETestSuite) TestNewSetWithdrawAddrCmd() {
tc := tc
s.Run(tc.name, func() {
cmd := cli.NewSetWithdrawAddrCmd()
cmd := cli.NewSetWithdrawAddrCmd(address.NewBech32Codec("cosmos"))
clientCtx := val.ClientCtx
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)

View File

@ -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

View File

@ -15,6 +15,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"
@ -423,7 +424,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 {
@ -617,7 +618,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 {

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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)
}