Skip to content

Commit

Permalink
added handlers MsgUpdateParams and migration for legacy parameter upd…
Browse files Browse the repository at this point in the history
…ates
  • Loading branch information
bermuell committed Nov 13, 2023
1 parent b8539bc commit 04cb84e
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 6 deletions.
7 changes: 7 additions & 0 deletions x/ccv/consumer/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,20 @@ const (
// SlashRecordByteKey is the single byte key storing the consumer's slash record.
SlashRecordByteKey

// ParametersKey is the is the single byte key for storing consumer's parameters.
ParametersByteKey

// NOTE: DO NOT ADD NEW BYTE PREFIXES HERE WITHOUT ADDING THEM TO getAllKeyPrefixes() IN keys_test.go
)

//
// Fully defined key func section
//

func ParametersKey() []byte {
return []byte{ParametersByteKey}
}

// PortKey returns the key to the port ID in the store
func PortKey() []byte {
return []byte{PortByteKey}
Expand Down
1 change: 1 addition & 0 deletions x/ccv/consumer/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func getAllKeyPrefixes() []byte {
PrevStandaloneChainByteKey,
PendingPacketsIndexByteKey,
SlashRecordByteKey,
ParametersByteKey,
}
}

Expand Down
41 changes: 39 additions & 2 deletions x/ccv/provider/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewQueryCmd() *cobra.Command {
cmd.AddCommand(CmdThrottleState())
cmd.AddCommand(CmdThrottledConsumerPacketData())
cmd.AddCommand(CmdRegisteredConsumerRewardDenoms())

cmd.AddCommand(CmdProviderParams())
return cmd
}

Expand Down Expand Up @@ -292,7 +292,7 @@ func CmdThrottledConsumerPacketData() *cobra.Command {
Short: "Query pending VSCMatured and slash packet data for a consumer chainId",
Long: strings.TrimSpace(
fmt.Sprintf(`Returns the current pending VSCMatured and slash packet data instances for a consumer chainId.
Queue is ordered by ibc sequence number.
Queue is ordered by ibc sequence number.
Example:
$ %s query provider throttled-consumer-packet-data foochain
`,
Expand Down Expand Up @@ -356,3 +356,40 @@ $ %s query provider registered-consumer-reward-denoms

return cmd
}

// NewQuerySubspaceParamsCmd returns a CLI command handler for querying subspace
// parameters managed by the x/params module.
func CmdProviderParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params [flags]",
Short: "Query values set as provider parameters",
/* Long: strings.TrimSpace(
fmt.Sprintf(`Returns the registered consumer reward denoms.
Example:
$ %s query provider registered-consumer-reward-denoms
`,
version.AppName,
),
), */
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

req := types.QueryParamsRequest{}
res, err := queryClient.QueryParams(cmd.Context(), &req)
if err != nil {
return err
}

return clientCtx.PrintProto(&res.Params)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
21 changes: 17 additions & 4 deletions x/ccv/provider/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ func (k Keeper) QueryConsumerGenesis(c context.Context, req *types.QueryConsumer
return &types.QueryConsumerGenesisResponse{GenesisState: gen}, nil
}

func (k Keeper) QueryParams(goCtx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(goCtx)
params, err := k.GetParams(ctx)
if err != nil {
return nil, status.Error(codes.NotFound, "no parameters found")
}
return &types.QueryParamsResponse{Params: params}, nil
}

func (k Keeper) QueryConsumerChains(goCtx context.Context, req *types.QueryConsumerChainsRequest) (*types.QueryConsumerChainsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
Expand Down Expand Up @@ -220,9 +233,9 @@ func (k Keeper) QueryThrottledConsumerPacketData(goCtx context.Context, req *typ
// getSlashPacketData fetches a slash packet data from the store using consumerChainId and ibcSeqNum (direct access)
// If the returned bytes do not unmarshal to SlashPacketData, the data is considered not found.
func (k Keeper) getSlashPacketData(ctx sdk.Context, consumerChainID string, ibcSeqNum uint64) (ccvtypes.SlashPacketData, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ThrottledPacketDataKey(consumerChainID, ibcSeqNum))
if len(bz) == 0 {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.ThrottledPacketDataKey(consumerChainID, ibcSeqNum))
if err != nil || len(bz) == 0 {
return ccvtypes.SlashPacketData{}, false
}

Expand All @@ -231,7 +244,7 @@ func (k Keeper) getSlashPacketData(ctx sdk.Context, consumerChainID string, ibcS
}

packet := ccvtypes.SlashPacketData{}
err := packet.Unmarshal(bz[1:])
err = packet.Unmarshal(bz[1:])
if err != nil {
// If the data cannot be unmarshaled, it is considered not found
return ccvtypes.SlashPacketData{}, false
Expand Down
28 changes: 28 additions & 0 deletions x/ccv/provider/keeper/migrations.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"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/interchain-security/v3/x/ccv/provider/types"
)

type Migrator struct {
keeper Keeper
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{
keeper: keeper,
}
}

// MigrateParams migrates the provider module's parameters from the x/params to self store.
func (m Migrator) MigrateParams(ctx sdk.Context, paramSpace paramtypes.Subspace) error {
var params types.Params
paramSpace.GetParamSet(ctx, &params)

m.keeper.SetParams(ctx, params)
m.keeper.Logger(ctx).Info("successfully migrated provider parameters")
return nil
}
15 changes: 15 additions & 0 deletions x/ccv/provider/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

tmprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/cosmos/interchain-security/v3/x/ccv/provider/types"
ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types"
Expand All @@ -27,6 +28,20 @@ func NewMsgServerImpl(keeper *Keeper) types.MsgServer {

var _ types.MsgServer = msgServer{}

// UpdateParams updates the params.
func (k msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if k.GetAuthority() != msg.Signer {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Signer)
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.Keeper.SetParams(ctx, msg.Params); err != nil {
return nil, err
}

return &types.MsgUpdateParamsResponse{}, nil
}

// CreateValidator defines a method for creating a new validator
func (k msgServer) AssignConsumerKey(goCtx context.Context, msg *types.MsgAssignConsumerKey) (*types.MsgAssignConsumerKeyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
Expand Down
9 changes: 9 additions & 0 deletions x/ccv/provider/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ func (AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
func (am AppModule) RegisterServices(cfg module.Configurator) {
providertypes.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
providertypes.RegisterQueryServer(cfg.QueryServer(), am.keeper)

migrator := keeper.NewMigrator(*am.keeper)
err := cfg.RegisterMigration(am.Name(), 2,
func(ctx sdk.Context) error {
return migrator.MigrateParams(ctx, am.paramSpace)
})
if err != nil {
panic(err)
}
}

// InitGenesis performs genesis initialization for the provider module. It returns no validator updates.
Expand Down
1 change: 1 addition & 0 deletions x/ccv/provider/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
&MsgConsumerAddition{},
&MsgConsumerRemoval{},
&MsgChangeRewardDenoms{},
&MsgUpdateParams{},
)
registry.RegisterImplementations(
(*govv1beta1.Content)(nil),
Expand Down
7 changes: 7 additions & 0 deletions x/ccv/provider/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,20 @@ const (
// handled in the current block
VSCMaturedHandledThisBlockBytePrefix

// ParametersKey is the is the single byte key for storing consumer's parameters.
ParametersByteKey

// NOTE: DO NOT ADD NEW BYTE PREFIXES HERE WITHOUT ADDING THEM TO getAllKeyPrefixes() IN keys_test.go
)

//
// Fully defined key func section
//

func ParametersKey() []byte {
return []byte{ParametersByteKey}
}

// PortKey returns the key to the port ID in the store
func PortKey() []byte {
return []byte{PortByteKey}
Expand Down
1 change: 1 addition & 0 deletions x/ccv/provider/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func getAllKeyPrefixes() []byte {
providertypes.ConsumerAddrsToPruneBytePrefix,
providertypes.SlashLogBytePrefix,
providertypes.VSCMaturedHandledThisBlockBytePrefix,
providertypes.ParametersByteKey,
}
}

Expand Down

0 comments on commit 04cb84e

Please sign in to comment.