Skip to content

Commit

Permalink
refactor: vrf key refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Dec 19, 2023
1 parent b770de4 commit ec00c21
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 110 deletions.
10 changes: 6 additions & 4 deletions x/randomness/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 1 addition & 31 deletions x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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() {}

Expand Down
16 changes: 1 addition & 15 deletions x/staking/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
}

Expand Down
7 changes: 0 additions & 7 deletions x/staking/types/codec.go
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
50 changes: 9 additions & 41 deletions x/staking/types/msg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"fmt"

"cosmossdk.io/math"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 10 additions & 12 deletions x/staking/types/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"context"
fmt "fmt"

errorsmod "cosmossdk.io/errors"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ec00c21

Please sign in to comment.