Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,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

Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 11 additions & 10 deletions x/auth/vesting/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strconv"

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

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -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",
Expand All @@ -32,17 +33,17 @@ func GetTxCmd() *cobra.Command {
}

txCmd.AddCommand(
NewMsgCreateVestingAccountCmd(),
NewMsgCreatePermanentLockedAccountCmd(),
NewMsgCreatePeriodicVestingAccountCmd(),
NewMsgCreateVestingAccountCmd(ac),
NewMsgCreatePermanentLockedAccountCmd(ac),
NewMsgCreatePeriodicVestingAccountCmd(ac),
)

return txCmd
}

// 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.",
Expand All @@ -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
}
Expand Down Expand Up @@ -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.",
Expand All @@ -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
}
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions x/auth/vesting/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
13 changes: 8 additions & 5 deletions x/auth/vesting/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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,
}
Expand Down
12 changes: 6 additions & 6 deletions x/auth/vesting/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion x/crisis/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 5 additions & 1 deletion x/crisis/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"cosmossdk.io/core/address"
"cosmossdk.io/log"

storetypes "cosmossdk.io/store/types"
Expand All @@ -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,
Expand All @@ -42,6 +45,7 @@ func NewKeeper(
supplyKeeper: supplyKeeper,
feeCollectorName: feeCollectorName,
authority: authority,
addressCodec: ac,
}
}

Expand Down
7 changes: 4 additions & 3 deletions x/crisis/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
Expand All @@ -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()
Expand All @@ -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) })
Expand Down
5 changes: 4 additions & 1 deletion x/crisis/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
24 changes: 15 additions & 9 deletions x/crisis/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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)

sender := testutil.CreateKeyringAccounts(s.T(), kr, 1)[0]

s.authKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
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 {
Expand Down Expand Up @@ -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",
},
Expand All @@ -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",
},
Expand Down
Loading