refactor(crisis & vesting): remove global bech32 (#15852)

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
Marko 2023-04-18 16:10:20 +02:00 committed by GitHub
parent 33657b3bf9
commit e9e582aede
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 65 additions and 41 deletions

View File

@ -161,6 +161,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* `InterfaceRegistry` is now a private interface and implements `protodesc.Resolver` plus the `RangeFiles` method
All implementations of `InterfaceRegistry` by other users must now embed the official implementation.
* `AminoCodec` is marked as deprecated.
* (x/crisis) [#15852](https://github.com/cosmos/cosmos-sdk/pull/15852) Crisis keeper now takes a instance of the address codec to be able to decode user addresses
### Client Breaking Changes

View File

@ -311,7 +311,7 @@ func NewSimApp(
invCheckPeriod := cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod))
app.CrisisKeeper = crisiskeeper.NewKeeper(appCodec, keys[crisistypes.StoreKey], invCheckPeriod,
app.BankKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String())
app.BankKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AccountKeeper.GetAddressCodec())
app.FeeGrantKeeper = feegrantkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[feegrant.StoreKey]), app.AccountKeeper)

View File

@ -7,6 +7,7 @@ import (
"os"
"strconv"
"cosmossdk.io/core/address"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
@ -22,7 +23,7 @@ const (
)
// GetTxCmd returns vesting module's transaction commands.
func GetTxCmd() *cobra.Command {
func GetTxCmd(ac address.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Vesting transaction subcommands",
@ -32,9 +33,9 @@ func GetTxCmd() *cobra.Command {
}
txCmd.AddCommand(
NewMsgCreateVestingAccountCmd(),
NewMsgCreatePermanentLockedAccountCmd(),
NewMsgCreatePeriodicVestingAccountCmd(),
NewMsgCreateVestingAccountCmd(ac),
NewMsgCreatePermanentLockedAccountCmd(ac),
NewMsgCreatePeriodicVestingAccountCmd(ac),
)
return txCmd
@ -42,7 +43,7 @@ func GetTxCmd() *cobra.Command {
// NewMsgCreateVestingAccountCmd returns a CLI command handler for creating a
// MsgCreateVestingAccount transaction.
func NewMsgCreateVestingAccountCmd() *cobra.Command {
func NewMsgCreateVestingAccountCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-vesting-account [to_address] [amount] [end_time]",
Short: "Create a new vesting account funded with an allocation of tokens.",
@ -57,7 +58,7 @@ timestamp.`,
if err != nil {
return err
}
toAddr, err := sdk.AccAddressFromBech32(args[0])
toAddr, err := ac.StringToBytes(args[0])
if err != nil {
return err
}
@ -91,7 +92,7 @@ timestamp.`,
// NewMsgCreatePermanentLockedAccountCmd returns a CLI command handler for creating a
// MsgCreatePermanentLockedAccount transaction.
func NewMsgCreatePermanentLockedAccountCmd() *cobra.Command {
func NewMsgCreatePermanentLockedAccountCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-permanent-locked-account [to_address] [amount]",
Short: "Create a new permanently locked account funded with an allocation of tokens.",
@ -104,7 +105,7 @@ tokens.`,
if err != nil {
return err
}
toAddr, err := sdk.AccAddressFromBech32(args[0])
toAddr, err := ac.StringToBytes(args[0])
if err != nil {
return err
}
@ -140,7 +141,7 @@ type InputPeriod struct {
// NewMsgCreatePeriodicVestingAccountCmd returns a CLI command handler for creating a
// MsgCreatePeriodicVestingAccountCmd transaction.
func NewMsgCreatePeriodicVestingAccountCmd() *cobra.Command {
func NewMsgCreatePeriodicVestingAccountCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-periodic-vesting-account [to_address] [periods_json_file]",
Short: "Create a new vesting account funded with an allocation of tokens.",
@ -168,7 +169,7 @@ func NewMsgCreatePeriodicVestingAccountCmd() *cobra.Command {
return err
}
toAddr, err := sdk.AccAddressFromBech32(args[0])
toAddr, err := ac.StringToBytes(args[0])
if err != nil {
return err
}

View File

@ -13,6 +13,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"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/cosmos/cosmos-sdk/testutil"
@ -49,7 +50,7 @@ func (s *CLITestSuite) SetupSuite() {
func (s *CLITestSuite) TestNewMsgCreateVestingAccountCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
cmd := cli.NewMsgCreateVestingAccountCmd()
cmd := cli.NewMsgCreateVestingAccountCmd(address.NewBech32Codec("cosmos"))
cmd.SetOutput(io.Discard)
extraArgs := []string{
@ -138,7 +139,7 @@ func (s *CLITestSuite) TestNewMsgCreateVestingAccountCmd() {
func (s *CLITestSuite) TestNewMsgCreatePermanentLockedAccountCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
cmd := cli.NewMsgCreatePermanentLockedAccountCmd()
cmd := cli.NewMsgCreatePermanentLockedAccountCmd(address.NewBech32Codec("cosmos"))
cmd.SetOutput(io.Discard)
extraArgs := []string{
@ -217,7 +218,7 @@ func (s *CLITestSuite) TestNewMsgCreatePermanentLockedAccountCmd() {
func (s *CLITestSuite) TestNewMsgCreatePeriodicVestingAccountCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
cmd := cli.NewMsgCreatePeriodicVestingAccountCmd()
cmd := cli.NewMsgCreatePeriodicVestingAccountCmd(address.NewBech32Codec("cosmos"))
cmd.SetOutput(io.Discard)
extraArgs := []string{

View File

@ -17,6 +17,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
modulev1 "cosmossdk.io/api/cosmos/vesting/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
@ -32,7 +33,9 @@ var (
// AppModuleBasic defines the basic application module used by the sub-vesting
// module. The module itself contain no special logic or state other than message
// handling.
type AppModuleBasic struct{}
type AppModuleBasic struct {
ac address.Codec
}
// Name returns the module's name.
func (AppModuleBasic) Name() string {
@ -62,11 +65,11 @@ func (AppModuleBasic) ValidateGenesis(_ codec.JSONCodec, _ client.TxEncodingConf
// RegisterGRPCGatewayRoutes registers the module's gRPC Gateway routes. Currently, this
// is a no-op.
func (a AppModuleBasic) RegisterGRPCGatewayRoutes(_ client.Context, _ *gwruntime.ServeMux) {}
func (AppModuleBasic) RegisterGRPCGatewayRoutes(_ client.Context, _ *gwruntime.ServeMux) {}
// GetTxCmd returns the root tx command for the auth module.
func (AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd()
func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd(ab.ac)
}
// GetQueryCmd returns the module's root query command. Currently, this is a no-op.
@ -85,7 +88,7 @@ type AppModule struct {
func NewAppModule(ak keeper.AccountKeeper, bk types.BankKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
AppModuleBasic: AppModuleBasic{ac: ak},
accountKeeper: ak,
bankKeeper: bk,
}

View File

@ -29,12 +29,12 @@ func NewMsgServerImpl(k keeper.AccountKeeper, bk types.BankKeeper) types.MsgServ
var _ types.MsgServer = msgServer{}
func (s msgServer) CreateVestingAccount(goCtx context.Context, msg *types.MsgCreateVestingAccount) (*types.MsgCreateVestingAccountResponse, error) {
from, err := sdk.AccAddressFromBech32(msg.FromAddress)
from, err := s.AccountKeeper.StringToBytes(msg.FromAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'from' address: %s", err)
}
to, err := sdk.AccAddressFromBech32(msg.ToAddress)
to, err := s.AccountKeeper.StringToBytes(msg.ToAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'to' address: %s", err)
}
@ -95,12 +95,12 @@ func (s msgServer) CreateVestingAccount(goCtx context.Context, msg *types.MsgCre
}
func (s msgServer) CreatePermanentLockedAccount(goCtx context.Context, msg *types.MsgCreatePermanentLockedAccount) (*types.MsgCreatePermanentLockedAccountResponse, error) {
from, err := sdk.AccAddressFromBech32(msg.FromAddress)
from, err := s.AccountKeeper.StringToBytes(msg.FromAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'from' address: %s", err)
}
to, err := sdk.AccAddressFromBech32(msg.ToAddress)
to, err := s.AccountKeeper.StringToBytes(msg.ToAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'to' address: %s", err)
}
@ -150,12 +150,12 @@ func (s msgServer) CreatePermanentLockedAccount(goCtx context.Context, msg *type
}
func (s msgServer) CreatePeriodicVestingAccount(goCtx context.Context, msg *types.MsgCreatePeriodicVestingAccount) (*types.MsgCreatePeriodicVestingAccountResponse, error) {
from, err := sdk.AccAddressFromBech32(msg.FromAddress)
from, err := s.AccountKeeper.StringToBytes(msg.FromAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'from' address: %s", err)
}
to, err := sdk.AccAddressFromBech32(msg.ToAddress)
to, err := s.AccountKeeper.StringToBytes(msg.ToAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'to' address: %s", err)
}

View File

@ -10,6 +10,7 @@ import (
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
@ -43,7 +44,7 @@ func (s *GenesisTestSuite) SetupTest() {
supplyKeeper := crisistestutil.NewMockSupplyKeeper(ctrl)
s.keeper = *keeper.NewKeeper(s.cdc, key, 5, supplyKeeper, "", "")
s.keeper = *keeper.NewKeeper(s.cdc, key, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos"))
}
func (s *GenesisTestSuite) TestImportExportGenesis() {

View File

@ -4,6 +4,7 @@ import (
"fmt"
"time"
"cosmossdk.io/core/address"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
@ -27,12 +28,14 @@ type Keeper struct {
supplyKeeper types.SupplyKeeper
feeCollectorName string // name of the FeeCollector ModuleAccount
addressCodec address.Codec
}
// NewKeeper creates a new Keeper object
func NewKeeper(
cdc codec.BinaryCodec, storeKey storetypes.StoreKey, invCheckPeriod uint,
supplyKeeper types.SupplyKeeper, feeCollectorName, authority string,
supplyKeeper types.SupplyKeeper, feeCollectorName, authority string, ac address.Codec,
) *Keeper {
return &Keeper{
storeKey: storeKey,
@ -42,6 +45,7 @@ func NewKeeper(
supplyKeeper: supplyKeeper,
feeCollectorName: feeCollectorName,
authority: authority,
addressCodec: ac,
}
}

View File

@ -8,6 +8,7 @@ import (
storetypes "cosmossdk.io/store/types"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
@ -24,7 +25,7 @@ func TestLogger(t *testing.T) {
key := storetypes.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test"))
encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{})
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "")
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos"))
require.Equal(t,
testCtx.Ctx.Logger().With("module", "x/"+types.ModuleName),
@ -37,7 +38,7 @@ func TestInvariants(t *testing.T) {
key := storetypes.NewKVStoreKey(types.StoreKey)
encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{})
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "")
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos"))
require.Equal(t, keeper.InvCheckPeriod(), uint(5))
orgInvRoutes := keeper.Routes()
@ -53,7 +54,7 @@ func TestAssertInvariants(t *testing.T) {
key := storetypes.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test"))
encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{})
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "")
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", "", addresscodec.NewBech32Codec("cosmos"))
keeper.RegisterRoute("testModule", "testRoute1", func(sdk.Context) (string, bool) { return "", false })
require.NotPanics(t, func() { keeper.AssertInvariants(testCtx.Ctx) })

View File

@ -16,7 +16,10 @@ var _ types.MsgServer = &Keeper{}
// VerifyInvariant implements MsgServer.VerifyInvariant method.
// It defines a method to verify a particular invariant.
func (k *Keeper) VerifyInvariant(goCtx context.Context, msg *types.MsgVerifyInvariant) (*types.MsgVerifyInvariantResponse, error) {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if msg.Sender == "" {
return nil, sdkerrors.ErrInvalidAddress.Wrap("empty address string is not allowed")
}
sender, err := k.addressCodec.StringToBytes(msg.Sender)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid sender address: %s", err)
}

View File

@ -9,6 +9,8 @@ import (
sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
@ -21,9 +23,9 @@ import (
type KeeperTestSuite struct {
suite.Suite
ctx sdk.Context
authKeeper *crisistestutil.MockSupplyKeeper
keeper *keeper.Keeper
ctx sdk.Context
supplyKeeper *crisistestutil.MockSupplyKeeper
keeper *keeper.Keeper
}
func (s *KeeperTestSuite) SetupTest() {
@ -34,11 +36,11 @@ func (s *KeeperTestSuite) SetupTest() {
key := storetypes.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{})
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", sdk.AccAddress([]byte("addr1_______________")).String())
keeper := keeper.NewKeeper(encCfg.Codec, key, 5, supplyKeeper, "", sdk.AccAddress([]byte("addr1_______________")).String(), addresscodec.NewBech32Codec("cosmos"))
s.ctx = testCtx.Ctx
s.keeper = keeper
s.authKeeper = supplyKeeper
s.supplyKeeper = supplyKeeper
}
func (s *KeeperTestSuite) TestMsgVerifyInvariant() {
@ -47,9 +49,13 @@ func (s *KeeperTestSuite) TestMsgVerifyInvariant() {
err := s.keeper.SetConstantFee(s.ctx, constantFee)
s.Require().NoError(err)
sender := sdk.AccAddress([]byte("addr1_______________"))
encCfg := moduletestutil.MakeTestEncodingConfig(crisis.AppModuleBasic{})
kr := keyring.NewInMemory(encCfg.Codec)
testutil.CreateKeyringAccounts(s.T(), kr, 1)
s.authKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
sender := testutil.CreateKeyringAccounts(s.T(), kr, 1)[0]
s.supplyKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
s.keeper.RegisterRoute("bank", "total-supply", func(sdk.Context) (string, bool) { return "", false })
testCases := []struct {
@ -81,7 +87,7 @@ func (s *KeeperTestSuite) TestMsgVerifyInvariant() {
{
name: "unregistered invariant route",
input: &types.MsgVerifyInvariant{
Sender: sender.String(),
Sender: sender.Address.String(),
InvariantModuleName: "module",
InvariantRoute: "invalidroute",
},
@ -91,7 +97,7 @@ func (s *KeeperTestSuite) TestMsgVerifyInvariant() {
{
name: "valid invariant",
input: &types.MsgVerifyInvariant{
Sender: sender.String(),
Sender: sender.Address.String(),
InvariantModuleName: "bank",
InvariantRoute: "total-supply",
},

View File

@ -12,6 +12,7 @@ import (
"github.com/spf13/cobra"
modulev1 "cosmossdk.io/api/cosmos/crisis/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
@ -199,7 +200,8 @@ type ModuleInputs struct {
Cdc codec.Codec
AppOpts servertypes.AppOptions `optional:"true"`
BankKeeper types.SupplyKeeper
BankKeeper types.SupplyKeeper
AddressCodec address.Codec
// LegacySubspace is used solely for migration of x/params managed parameters
LegacySubspace exported.Subspace
@ -236,6 +238,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
in.BankKeeper,
feeCollectorName,
authority.String(),
in.AddressCodec,
)
var skipGenesisInvariants bool