From ec00c211b1f4fc57d77c17826c39bddbb1935b23 Mon Sep 17 00:00:00 2001 From: Hyoung-yoon Kim Date: Tue, 19 Dec 2023 17:23:40 +0900 Subject: [PATCH] refactor: vrf key refactor --- x/randomness/keeper/abci.go | 10 ++++--- x/staking/module.go | 32 +--------------------- x/staking/msg_server.go | 16 +---------- x/staking/types/codec.go | 7 ----- x/staking/types/msg.go | 50 +++++++---------------------------- x/staking/types/msg_server.go | 22 +++++++-------- 6 files changed, 27 insertions(+), 110 deletions(-) diff --git a/x/randomness/keeper/abci.go b/x/randomness/keeper/abci.go index 8679ea52..88c33f80 100644 --- a/x/randomness/keeper/abci.go +++ b/x/randomness/keeper/abci.go @@ -8,7 +8,6 @@ import ( abci "github.com/cometbft/cometbft/abci/types" "github.com/cosmos/cosmos-sdk/client" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" txsigning "github.com/cosmos/cosmos-sdk/types/tx/signing" @@ -53,8 +52,11 @@ func PrepareProposalHandler( return nil, err } account := authKeeper.GetAccount(ctx, sdk.AccAddress(pubKey.Address().Bytes())) - - newSeedTx, err := generateAndSignNewSeedTx(ctx, txConfig, vrfSigner, pubKey, account, &types.MsgNewSeed{ + err = account.SetPubKey(pubKey) // checked later when signing tx with VRF key + if err != nil { + return nil, err + } + newSeedTx, err := generateAndSignNewSeedTx(ctx, txConfig, vrfSigner, account, &types.MsgNewSeed{ Proposer: sdk.AccAddress(req.ProposerAddress).String(), Pi: hex.EncodeToString(pi), Beta: hex.EncodeToString(beta), @@ -132,7 +134,7 @@ func ProcessProposalHandler( // generateAndSignNewSeedTx generates and signs a transaction containing // a given NewSeed message. It returns a transaction encoded into bytes. -func generateAndSignNewSeedTx(ctx sdk.Context, txConfig client.TxConfig, vrfSigner utils.VRFSigner, pubKey cryptotypes.PubKey, account sdk.AccountI, msg *types.MsgNewSeed) ([]byte, error) { +func generateAndSignNewSeedTx(ctx sdk.Context, txConfig client.TxConfig, vrfSigner utils.VRFSigner, account sdk.AccountI, msg *types.MsgNewSeed) ([]byte, error) { // build a transaction containing the given message txBuilder := txConfig.NewTxBuilder() err := txBuilder.SetMsgs(msg) diff --git a/x/staking/module.go b/x/staking/module.go index c78339ce..430b52a0 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -38,8 +38,6 @@ var ( // AppModuleBasic defines the basic application module used by the staking module. type AppModuleBasic struct { - // staking.AppModuleBasic - cdc codec.Codec ak types.AccountKeeper } @@ -99,14 +97,11 @@ func (amb AppModuleBasic) GetTxCmd() *cobra.Command { // AppModule implements an application module for the staking module. type AppModule struct { AppModuleBasic - // staking.AppModule keeper *sdkkeeper.Keeper accountKeeper types.AccountKeeper bankKeeper sdktypes.BankKeeper randomnessKeeper types.RandomnessKeeper - // legacySubspace is used solely for migration of x/params managed parameters - // legacySubspace exported.Subspace } // NewAppModule creates a new AppModule object @@ -116,12 +111,9 @@ func NewAppModule( ak types.AccountKeeper, bk sdktypes.BankKeeper, rk types.RandomnessKeeper, - // ls exported.Subspace, ) AppModule { - // am := staking.NewAppModule(cdc, keeper, ak, bk, ls) return AppModule{ - AppModuleBasic: AppModuleBasic{cdc: cdc, ak: ak}, - // AppModule: am, + AppModuleBasic: AppModuleBasic{cdc: cdc, ak: ak}, keeper: keeper, accountKeeper: ak, bankKeeper: bk, @@ -132,34 +124,12 @@ func NewAppModule( // RegisterServices registers module services. func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), types.NewMsgServerImpl(am.keeper, am.accountKeeper, am.randomnessKeeper)) - sdktypes.RegisterMsgServer(cfg.MsgServer(), NewMsgServerImpl(am.keeper, am.accountKeeper)) querier := sdkkeeper.Querier{Keeper: am.keeper} sdktypes.RegisterQueryServer(cfg.QueryServer(), querier) } -// // RegisterServices registers module services. -// func (am AppModule) RegisterServices(cfg module.Configurator) { -// types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) -// querier := keeper.Querier{Keeper: am.keeper} -// types.RegisterQueryServer(cfg.QueryServer(), querier) - -// 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)) -// } -// if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil { -// panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err)) -// } -// if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil { -// panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err)) -// } -// if err := cfg.RegisterMigration(types.ModuleName, 4, m.Migrate4to5); err != nil { -// panic(fmt.Sprintf("failed to migrate x/%s from version 4 to 5: %v", types.ModuleName, err)) -// } -// } - // IsOnePerModuleType implements the depinject.OnePerModuleType interface. func (am AppModule) IsOnePerModuleType() {} diff --git a/x/staking/msg_server.go b/x/staking/msg_server.go index 4acd61c6..3021a677 100644 --- a/x/staking/msg_server.go +++ b/x/staking/msg_server.go @@ -3,11 +3,6 @@ package staking import ( "context" - errorsmod "cosmossdk.io/errors" - - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -30,16 +25,7 @@ func NewMsgServerImpl(keeper *stakingkeeper.Keeper, accKeeper types.AccountKeepe } func (k msgServer) CreateValidator(ctx context.Context, msg *stakingtypes.MsgCreateValidator) (*stakingtypes.MsgCreateValidatorResponse, error) { - // create an account based on consensus key to send NewSeed txs when proposing blocks - pk, ok := msg.Pubkey.GetCachedValue().(cryptotypes.PubKey) - if !ok { - return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", pk) - } - - addr := sdk.AccAddress(pk.Address().Bytes()) - acc := k.accountKeeper.NewAccountWithAddress(ctx, addr) - k.accountKeeper.SetAccount(ctx, acc) - + // TO-DO disable? return k.MsgServer.CreateValidator(ctx, msg) } diff --git a/x/staking/types/codec.go b/x/staking/types/codec.go index f7be9294..6ce04411 100644 --- a/x/staking/types/codec.go +++ b/x/staking/types/codec.go @@ -1,18 +1,11 @@ package types import ( - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/msgservice" ) -// RegisterLegacyAminoCodec registers the necessary x/staking interfaces and concrete types -// on the provided LegacyAmino codec. These types are used for Amino JSON serialization. -func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - // legacy.RegisterAminoMsg(cdc, &MsgCreateValidator{}, "cosmos-sdk/MsgCreateValidator") -} - // RegisterInterfaces registers the x/staking interfaces types with the interface registry func RegisterInterfaces(registry types.InterfaceRegistry) { registry.RegisterImplementations((*sdk.Msg)(nil), diff --git a/x/staking/types/msg.go b/x/staking/types/msg.go index 7a659b64..50da914a 100644 --- a/x/staking/types/msg.go +++ b/x/staking/types/msg.go @@ -1,6 +1,8 @@ package types import ( + "fmt" + "cosmossdk.io/math" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -42,47 +44,13 @@ func NewMsgCreateValidatorWithVRF( }, nil } -// // Validate validates the MsgCreateValidator sdk msg. -// func (msg MsgCreateValidatorWithVRF) Validate(ac address.Codec) error { -// // note that unmarshaling from bech32 ensures both non-empty and valid -// _, err := ac.StringToBytes(msg.ValidatorAddress) -// if err != nil { -// return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err) -// } - -// if msg.Pubkey == nil { -// return ErrEmptyValidatorPubKey -// } - -// if !msg.Value.IsValid() || !msg.Value.Amount.IsPositive() { -// return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid delegation amount") -// } - -// if msg.Description == (Description{}) { -// return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty description") -// } - -// if msg.Commission == (CommissionRates{}) { -// return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty commission") -// } - -// if err := msg.Commission.Validate(); err != nil { -// return err -// } - -// if !msg.MinSelfDelegation.IsPositive() { -// return errorsmod.Wrap( -// sdkerrors.ErrInvalidRequest, -// "minimum self delegation must be a positive integer", -// ) -// } - -// if msg.Value.Amount.LT(msg.MinSelfDelegation) { -// return ErrSelfDelegationBelowMinimum -// } - -// return nil -// } +// Validate validates the MsgCreateValidatorWithVRF sdk msg. +func (msg MsgCreateValidatorWithVRF) Validate() error { + if msg.Pubkey == nil || msg.VrfPubkey == nil { + return fmt.Errorf("empty validator public key or VRF public key") + } + return nil +} // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces func (msg MsgCreateValidatorWithVRF) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { diff --git a/x/staking/types/msg_server.go b/x/staking/types/msg_server.go index dc1aa098..c9970244 100644 --- a/x/staking/types/msg_server.go +++ b/x/staking/types/msg_server.go @@ -2,7 +2,6 @@ package types import ( "context" - fmt "fmt" errorsmod "cosmossdk.io/errors" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -32,32 +31,31 @@ func NewMsgServerImpl(keeper *stakingkeeper.Keeper, accKeeper AccountKeeper, ran } func (k msgServer) CreateValidatorWithVRF(ctx context.Context, msg *MsgCreateValidatorWithVRF) (*MsgCreateValidatorWithVRFResponse, error) { + if err := msg.Validate(); err != nil { + return nil, err + } + // create an account based on VRF public key to send NewSeed txs when proposing blocks vrfPubKey, ok := msg.VrfPubkey.GetCachedValue().(cryptotypes.PubKey) if !ok { return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", vrfPubKey) } - // debug - pubKey, ok := msg.Pubkey.GetCachedValue().(cryptotypes.PubKey) - if !ok { - return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", pubKey) - } - consAddr := sdk.ConsAddress(pubKey.Address()) - fmt.Println(consAddr) - addr := sdk.AccAddress(vrfPubKey.Address().Bytes()) acc := k.accountKeeper.NewAccountWithAddress(ctx, addr) k.accountKeeper.SetAccount(ctx, acc) - // register VRF public key - k.randomnessKeeper.SetValidatorVRFPubKey(ctx, consAddr.String(), vrfPubKey) + // register VRF public key to validator consensus address + pubKey, ok := msg.Pubkey.GetCachedValue().(cryptotypes.PubKey) + if !ok { + return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", pubKey) + } + k.randomnessKeeper.SetValidatorVRFPubKey(ctx, sdk.GetConsAddress(pubKey).String(), vrfPubKey) sdkMsg := new(stakingtypes.MsgCreateValidator) sdkMsg.Description = msg.Description sdkMsg.Commission = msg.Commission sdkMsg.MinSelfDelegation = msg.MinSelfDelegation - // sdkMsg.DelegatorAddress = msg.DelegatorAddress sdkMsg.ValidatorAddress = msg.ValidatorAddress sdkMsg.Pubkey = msg.Pubkey sdkMsg.Value = msg.Value