Skip to content

Commit

Permalink
Migrate from x/params for emergencybutton module
Browse files Browse the repository at this point in the history
  • Loading branch information
iKapitonau committed Sep 17, 2024
1 parent 3aa4b10 commit 65e08d4
Show file tree
Hide file tree
Showing 16 changed files with 616 additions and 76 deletions.
4 changes: 3 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,9 @@ func (ak *SecretAppKeepers) InitCustomKeepers(

ibcSwitchKeeper := ibcswitch.NewKeeper(
ak.IbcFeeKeeper,
ak.GetSubspace(ibcswitch.ModuleName),
appCodec,
ak.keys[ibcswitchtypes.StoreKey],
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
ak.IbcSwitchKeeper = &ibcSwitchKeeper

Expand Down
2 changes: 1 addition & 1 deletion app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ func Modules(
ica.NewAppModule(app.AppKeepers.ICAControllerKeeper, app.AppKeepers.ICAHostKeeper),
packetforward.NewAppModule(app.AppKeepers.PacketForwardKeeper, app.AppKeepers.GetSubspace(packetforwardtypes.ModuleName)),
ibcfee.NewAppModule(app.AppKeepers.IbcFeeKeeper),
ibcswitch.NewAppModule(app.AppKeepers.IbcSwitchKeeper),
ibcswitch.NewAppModule(app.AppKeepers.IbcSwitchKeeper, app.AppKeepers.GetSubspace(ibcswitch.ModuleName)),
}
}
3 changes: 3 additions & 0 deletions app/upgrades/v1.15/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
ibcconntypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibcswitchtypes "github.com/scrtlabs/SecretNetwork/x/emergencybutton/types"

"github.com/scrtlabs/SecretNetwork/app/keepers"
"github.com/scrtlabs/SecretNetwork/app/upgrades"
Expand Down Expand Up @@ -78,6 +79,8 @@ func createUpgradeHandler(mm *module.Manager, appKeepers *keepers.SecretAppKeepe
keyTable = slashingtypes.ParamKeyTable() //nolint:staticcheck
case govtypes.ModuleName:
keyTable = govv1.ParamKeyTable() //nolint:staticcheck
case ibcswitchtypes.ModuleName:
keyTable = ibcswitchtypes.ParamKeyTable() //nolint:staticcheck
case crisistypes.ModuleName:
keyTable = crisistypes.ParamKeyTable() //nolint:staticcheck
case ibcexported.ModuleName:
Expand Down
18 changes: 17 additions & 1 deletion proto/secret/emergencybutton/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package secret.emergencybutton.v1beta1;
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/msg/v1/msg.proto";
import "secret/emergencybutton/v1beta1/params.proto";

option go_package = "github.com/scrtlabs/SecretNetwork/x/emergencybutton/types";

Expand All @@ -14,6 +15,7 @@ service Msg {
// ToggleIbcSwitch defines a method for toggling the status of the
// emergencybutton.
rpc ToggleIbcSwitch(MsgToggleIbcSwitch) returns (MsgToggleIbcSwitchResponse);
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
}

// MsgToggleIbcSwitch represents a message to toggle the emergencybutton status
Expand All @@ -25,4 +27,18 @@ message MsgToggleIbcSwitch {
}

// MsgToggleIbcSwitchResponse defines the response type for the toggle.
message MsgToggleIbcSwitchResponse {}
message MsgToggleIbcSwitchResponse {}

message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";

// authority is the address of the governance account.
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// params defines the x/emergencybutton parameters to update.
//
// NOTE: All parameters must be supplied.
Params params = 2 [(gogoproto.nullable) = false];
}

message MsgUpdateParamsResponse {}
18 changes: 18 additions & 0 deletions x/emergencybutton/exported/exported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package exported

import (
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

type (
ParamSet = paramtypes.ParamSet

// Subspace defines an interface that implements the legacy x/params Subspace
// type.
//
// NOTE: This is used solely for migration of x/params managed parameters.
Subspace interface {
GetParamSet(ctx sdk.Context, ps ParamSet)
}
)
28 changes: 18 additions & 10 deletions x/emergencybutton/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ package keeper

import (
"cosmossdk.io/errors"
codec "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
//nolint:staticcheck
storetypes "cosmossdk.io/store/types"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
"github.com/scrtlabs/SecretNetwork/x/emergencybutton/types"
)

type Keeper struct {
channel porttypes.ICS4Wrapper
paramSpace paramtypes.Subspace
cdc codec.BinaryCodec
channel porttypes.ICS4Wrapper
storeKey storetypes.StoreKey
authority string
}

func (i *Keeper) GetAppVersion(ctx sdk.Context, portID, channelID string) (string, bool) {
Expand All @@ -23,15 +26,15 @@ func (i *Keeper) GetAppVersion(ctx sdk.Context, portID, channelID string) (strin

func NewKeeper(
channel porttypes.ICS4Wrapper,
paramSpace paramtypes.Subspace,
cdc codec.BinaryCodec,
key storetypes.StoreKey,
authority string,
) Keeper {
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}

return Keeper{
channel: channel,
paramSpace: paramSpace,
channel: channel,
authority: authority,
cdc: cdc,
storeKey: key,
}
}

Expand All @@ -56,3 +59,8 @@ func (i *Keeper) WriteAcknowledgement(ctx sdk.Context, chanCap *capabilitytypes.
func (i *Keeper) IsHalted(ctx sdk.Context) bool {
return i.GetSwitchStatus(ctx) == types.IbcSwitchStatusOff
}

// GetAuthority returns the x/emergencybutton module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
}
28 changes: 28 additions & 0 deletions x/emergencybutton/keeper/migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/scrtlabs/SecretNetwork/x/emergencybutton/exported"
v2 "github.com/scrtlabs/SecretNetwork/x/emergencybutton/migrations/v2"
)

// Migrator is a struct for handling in-place state migrations.
type Migrator struct {
keeper Keeper
legacySubspace exported.Subspace
}

func NewMigrator(k Keeper, ss exported.Subspace) Migrator {
return Migrator{
keeper: k,
legacySubspace: ss,
}
}

// Migrate1to2 migrates the x/emergencybutton module state from the consensus version 1 to
// version 2. Specifically, it takes the parameters that are currently stored
// and managed by the x/params modules and stores them directly into the x/emergencybutton
// module state.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.Migrate(ctx, ctx.KVStore(m.keeper.storeKey), m.legacySubspace, m.keeper.cdc)
}
14 changes: 14 additions & 0 deletions x/emergencybutton/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/scrtlabs/SecretNetwork/x/emergencybutton/types"
)

Expand Down Expand Up @@ -43,3 +44,16 @@ func (m msgServer) ToggleIbcSwitch(goCtx context.Context, msg *types.MsgToggleIb

return &types.MsgToggleIbcSwitchResponse{}, nil
}

func (m msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if m.keeper.authority != req.Authority {
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", m.keeper.authority, req.Authority)
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := m.keeper.SetParams(ctx, req.Params); err != nil {
return nil, err
}

return &types.MsgUpdateParamsResponse{}, nil
}
39 changes: 21 additions & 18 deletions x/emergencybutton/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,36 @@ func (i *Keeper) GetPauserAddress(ctx sdk.Context) (pauser string) {
return i.GetParams(ctx).PauserAddress
}

//func (i *Keeper) GetSwitchStatus(ctx sdk.Context) (status string) {
// return i.GetParams(ctx).SwitchStatus
//}

func (i *Keeper) GetParams(ctx sdk.Context) (params types.Params) {
// This was previously done via i.paramSpace.GetParamSet(ctx, &params). That will
// panic if the params don't exist. This is a workaround to avoid that panic.
// Params should be refactored to just use a raw kvstore.
empty := types.Params{}
for _, pair := range params.ParamSetPairs() {
i.paramSpace.GetIfExists(ctx, pair.Key, pair.Value)
}
if params == empty {
return types.DefaultParams()
store := ctx.KVStore(i.storeKey)
bz := store.Get(types.ParamsKey)
if bz == nil {
return params
}

i.cdc.MustUnmarshal(bz, &params)
return params
}

func (i *Keeper) GetSwitchStatus(ctx sdk.Context) (status string) {
i.paramSpace.Get(ctx, types.KeySwitchStatus, &status)
return
return i.GetParams(ctx).SwitchStatus
}

func (i *Keeper) SetSwitchStatus(ctx sdk.Context, value string) {
i.paramSpace.Set(ctx, types.KeySwitchStatus, value)
params := i.GetParams(ctx)
params.SwitchStatus = value
i.SetParams(ctx, params)
}

func (i *Keeper) SetParams(ctx sdk.Context, params types.Params) {
i.paramSpace.SetParamSet(ctx, &params)
// SetParams sets the x/emergencybutton module parameters.
func (i *Keeper) SetParams(ctx sdk.Context, p types.Params) error {
if err := p.Validate(); err != nil {
return err
}

store := ctx.KVStore(i.storeKey)
bz := i.cdc.MustMarshal(&p)
store.Set(types.ParamsKey, bz)

return nil
}
40 changes: 40 additions & 0 deletions x/emergencybutton/migrations/v2/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package v2

import (
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/scrtlabs/SecretNetwork/x/emergencybutton/exported"
"github.com/scrtlabs/SecretNetwork/x/emergencybutton/types"
)

const (
ModuleName = "emergencybutton"
)

var (
ParamsKey = []byte{0x01}
)

// Migrate migrates the x/emergencybutton module state from the consensus version 1 to
// version 2. Specifically, it takes the parameters that are currently stored
// and managed by the x/params modules and stores them directly into the x/emergencybutton
// module state.
func Migrate(
ctx sdk.Context,
store storetypes.KVStore,
legacySubspace exported.Subspace,
cdc codec.BinaryCodec,
) error {
var currParams types.Params
legacySubspace.GetParamSet(ctx, &currParams)

if err := currParams.Validate(); err != nil {
return err
}

bz := cdc.MustMarshal(&currParams)
store.Set(ParamsKey, bz)

return nil
}
16 changes: 13 additions & 3 deletions x/emergencybutton/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
ibcswitchclient "github.com/scrtlabs/SecretNetwork/x/emergencybutton/client"

"github.com/scrtlabs/SecretNetwork/x/emergencybutton/exported"
"github.com/scrtlabs/SecretNetwork/x/emergencybutton/keeper"
"github.com/scrtlabs/SecretNetwork/x/emergencybutton/types"
)

Expand Down Expand Up @@ -69,12 +70,15 @@ type AppModule struct {
AppModuleBasic

keeper *Keeper

legacySubspace exported.Subspace
}

func NewAppModule(keeper *Keeper) AppModule {
func NewAppModule(keeper *Keeper, ss exported.Subspace) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
keeper: keeper,
legacySubspace: ss,
}
}

Expand All @@ -88,6 +92,12 @@ func (am AppModule) Name() string {
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), NewMsgServerImpl(*am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), grpc.Querier{Q: ibcswitchclient.Querier{K: *am.keeper}})

m := keeper.NewMigrator(*am.keeper, am.legacySubspace)

if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
}
}

// InitGenesis performs the txfees module's genesis initialization It returns
Expand All @@ -106,7 +116,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }

// IsAppModule implements the appmodule.AppModule interface.
func (AppModule) IsAppModule() {}
Expand Down
2 changes: 2 additions & 0 deletions x/emergencybutton/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
// LegacyAmino codec. These types are used for Amino JSON serialization.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgToggleIbcSwitch{}, "emergencybutton/MsgToggleIbcSwitch", nil)
cdc.RegisterConcrete(&MsgUpdateParams{}, "emergencybutton/MsgUpdateParams", nil)
}

// RegisterInterfaces registers interfaces and implementations of the incentives module.
func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgToggleIbcSwitch{},
&MsgUpdateParams{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
5 changes: 4 additions & 1 deletion x/emergencybutton/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const (

// RouterKey is the message route. Can only contain
// alphanumeric characters.
var RouterKey = QuerierRoute
var (
RouterKey = QuerierRoute
ParamsKey = []byte{0x01}
)

const (
// IbcSwitchStatusOff - IBC messages halted
Expand Down
Loading

0 comments on commit 65e08d4

Please sign in to comment.