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

Change prefix to pica #272

Closed
wants to merge 14 commits into from
Closed
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
9 changes: 9 additions & 0 deletions proto/centauri/keyrotation/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";
package centauri.keyrotation.v1beta1;

import "gogoproto/gogo.proto";

option go_package = "x/keyrotation/types";

// GenesisState defines the mint module's genesis state.
message GenesisState {}
16 changes: 16 additions & 0 deletions proto/centauri/keyrotation/v1beta1/rotation.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";
package centauri.keyrotation.v1beta1;

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "google/protobuf/any.proto";

option go_package = "x/keyrotation/types";

// GenesisState defines the mint module's genesis state.
message ConsPubKeyRotationHistory {
string operator_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
google.protobuf.Any old_key = 2 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
google.protobuf.Any new_key = 3 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
uint64 block_height = 4;
}
25 changes: 25 additions & 0 deletions proto/centauri/keyrotation/v1beta1/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";
package centauri.keyrotation.v1beta1;

import "cosmos/msg/v1/msg.proto";
import "amino/amino.proto";
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "google/protobuf/any.proto";

option go_package = "x/keyrotation/types";

// Msg defines the x/keyrotation Msg service.
service Msg {
option (cosmos.msg.v1.service) = true;
// CreateValidator defines a method for creating a new validator.
rpc RotateConsPubKey(MsgRotateConsPubKey) returns (MsgRotateConsPubKeyResponse);
}

message MsgRotateConsPubKey {
string validator_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
google.protobuf.Any pubkey = 2 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
}

message MsgRotateConsPubKeyResponse {
}
10 changes: 10 additions & 0 deletions x/keyrotation/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package keeper

import (
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) EndBlock(ctx sdk.Context) []abci.ValidatorUpdate {
return nil
}
13 changes: 13 additions & 0 deletions x/keyrotation/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/notional-labs/composable/v6/x/keyrotation/types"
)

func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {}

func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
return nil
}
104 changes: 104 additions & 0 deletions x/keyrotation/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package keeper

import (
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/notional-labs/composable/v6/x/keyrotation/types"
)

type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
sk types.StakingKeeper

// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string
}

func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
sk types.StakingKeeper,
authority string,
) Keeper {
return Keeper{
cdc: cdc,
storeKey: key,
sk: sk,
authority: authority,
}
}

func (k Keeper) handleMsgRotateConsPubKey(ctx sdk.Context, valAddress sdk.ValAddress, pubKey cryptotypes.PubKey) error {
var (
validator stakingtypes.Validator
found bool
)
// check to see if the validator not exist
if validator, found = k.sk.GetValidator(ctx, valAddress); !found {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "validator not exists")
}

// check if pubkey is exists
if _, found := k.sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pubKey)); found {
return stakingtypes.ErrValidatorPubKeyExists
}

// check if new pubkey is allowed
cp := ctx.ConsensusParams()
if cp != nil && cp.Validator != nil {
pkType := pubKey.Type()
hasKeyType := false
for _, keyType := range cp.Validator.PubKeyTypes {
if pkType == keyType {
hasKeyType = true
break
}
}
if !hasKeyType {
return errorsmod.Wrapf(
stakingtypes.ErrValidatorPubKeyTypeNotSupported,
"got: %s, expected: %s", pubKey.Type(), cp.Validator.PubKeyTypes,
)
}
}

// wrap pubkey to types any
newPkAny, err := codectypes.NewAnyWithValue(pubKey)
if err != nil {
return err
}

oldPkAny := validator.ConsensusPubkey

// replace pubkey
validator.ConsensusPubkey = newPkAny

// NOTE: staking module do not support DeleteValidatorByConsAddr method for Validator types
// we need to record all the rotated pubkey in keyrotation store so when we can delete later
// by upgrade handler

// set validator
k.sk.SetValidator(ctx, validator)
k.sk.SetValidatorByConsAddr(ctx, validator)

// Set rotation history
consPubKeyRotationHistory := types.ConsPubKeyRotationHistory{
OperatorAddress: valAddress.String(),
OldKey: oldPkAny,
NewKey: newPkAny,
BlockHeight: uint64(ctx.BlockHeight()),
}

k.SetKeyRotationHistory(ctx, consPubKeyRotationHistory)

return nil
}
47 changes: 47 additions & 0 deletions x/keyrotation/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package keeper

import (
"context"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/notional-labs/composable/v6/x/keyrotation/types"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

var _ types.MsgServer = msgServer{}

// NewMsgServerImpl returns an implementation of the MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{
Keeper: keeper,
}
}

type msgServer struct {
Keeper
}

func (k Keeper) RotateConsPubKey(goCtx context.Context, msg *types.MsgRotateConsPubKey) (*types.MsgRotateConsPubKeyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return nil, err
}

// get pubkey
pk, ok := msg.Pubkey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", pk)

Check failure on line 38 in x/keyrotation/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: sdkerrors.Wrapf is deprecated: functionality of this package has been moved to it's own module: (staticcheck)
}

err = k.handleMsgRotateConsPubKey(ctx, valAddr, pk)
if err != nil {
return nil, err
}

return &types.MsgRotateConsPubKeyResponse{}, nil
}
26 changes: 26 additions & 0 deletions x/keyrotation/keeper/pubkey_rotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/notional-labs/composable/v6/x/keyrotation/types"
)

func (k Keeper) SetKeyRotationHistory(ctx sdk.Context, consRotationHistory types.ConsPubKeyRotationHistory) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&consRotationHistory)
store.Set(types.GetKeyRotationHistory(consRotationHistory.OperatorAddress), bz)
}

func (k Keeper) IterateRotationHistory(ctx sdk.Context, cb func(types.ConsPubKeyRotationHistory) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyRotation)

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var rotationHistory types.ConsPubKeyRotationHistory
k.cdc.MustUnmarshal(iterator.Value(), &rotationHistory)
if cb(rotationHistory) {
break
}
}
}
131 changes: 131 additions & 0 deletions x/keyrotation/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package keyrotation
dzmitry-lahoda marked this conversation as resolved.
Show resolved Hide resolved

import (
"encoding/json"
"fmt"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/notional-labs/composable/v6/x/keyrotation/keeper"
"github.com/notional-labs/composable/v6/x/keyrotation/types"
"github.com/spf13/cobra"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)

// AppModuleBasic defines the basic application module used by the ibc-hooks module.
type AppModuleBasic struct{}

// Name returns the ibc-hooks module's name.
func (AppModuleBasic) Name() string {
return types.ModuleName
}

// RegisterLegacyAminoCodec registers the ibc-hooks module's types on the given LegacyAmino codec.
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterLegacyAminoCodec(cdc)
}

// RegisterInterfaces registers module concrete types into protobuf Any.
func (AppModuleBasic) RegisterInterfaces(reg codectypes.InterfaceRegistry) {
types.RegisterInterfaces(reg)
}

// DefaultGenesis returns default genesis state as raw bytes for the ibc
// router module.
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesisState())
}

// RegisterRESTRoutes implements AppModuleBasic interface
func (AppModuleBasic) RegisterRESTRoutes(_ client.Context, _ *mux.Router) {}

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module.
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
}

// GetTxCmd implements AppModuleBasic interface
func (AppModuleBasic) GetTxCmd() *cobra.Command {
return nil
}

// GetQueryCmd implements AppModuleBasic interface
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return nil
}

// AppModule represents the AppModule for this module
type AppModule struct {
AppModuleBasic
keeper *keeper.Keeper
}

// NewAppModule creates a new router module
func NewAppModule(k *keeper.Keeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
keeper: k,
}
}

// Name returns the capability module's name.
func (am AppModule) Name() string {
return am.AppModuleBasic.Name()
}

// QuerierRoute implements the AppModule interface
func (AppModule) QuerierRoute() string {
return types.QuerierRoute
}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
}

// InitGenesis performs genesis initialization for the ibc-router module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
am.keeper.InitGenesis(ctx, genesisState)
return []abci.ValidatorUpdate{}
}

// ValidateGenesis performs genesis state validation for the mint module.
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error {
var data types.GenesisState
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}
// TODO: Add types.ValidateGenesis(data)
return types.ValidateGenesis(data)
}

// ExportGenesis returns the exported genesis state as raw bytes for the ibc-router
// module.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
gs := am.keeper.ExportGenesis(ctx)
return cdc.MustMarshalJSON(gs)
}

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

// BeginBlock implements the AppModule interface
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
}

// EndBlock implements the AppModule interface
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return am.keeper.EndBlock(ctx)
}
Loading
Loading