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

feat: make validator key injectable by application developers #21608

Merged
merged 12 commits into from
Sep 18, 2024
9 changes: 9 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
cmtcrypto "github.com/cometbft/cometbft/crypto"
cmted25519 "github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/crypto/tmhash"
"github.com/cosmos/gogoproto/proto"
"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -60,6 +62,8 @@ const (

var _ servertypes.ABCI = (*BaseApp)(nil)

type KeyGenF = func() (cmtcrypto.PrivKey, error)

// BaseApp reflects the ABCI application implementation.
type BaseApp struct {
// initialized on creation
Expand Down Expand Up @@ -190,6 +194,8 @@ type BaseApp struct {

// includeNestedMsgsGas holds a set of message types for which gas costs for its nested messages are calculated.
includeNestedMsgsGas map[string]struct{}

validatorKeyProvider KeyGenF
}

// NewBaseApp returns a reference to an initialized BaseApp. It accepts a
Expand All @@ -210,6 +216,9 @@ func NewBaseApp(
fauxMerkleMode: false,
sigverifyTx: true,
queryGasLimit: math.MaxUint64,
validatorKeyProvider: func() (cmtcrypto.PrivKey, error) {
return cmted25519.GenPrivKey(), nil
},
}

for _, option := range options {
Expand Down
11 changes: 11 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,14 @@ func (app *BaseApp) SetMsgServiceRouter(msgServiceRouter *MsgServiceRouter) {
func (app *BaseApp) SetGRPCQueryRouter(grpcQueryRouter *GRPCQueryRouter) {
app.grpcQueryRouter = grpcQueryRouter
}

func (app *BaseApp) SetPrivValidatorKeyGen(keyGenF KeyGenF) {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
if app.sealed {
panic("SetPrivValidatorProvider() on sealed BaseApp")
}
app.validatorKeyProvider = keyGenF
}

func (app *BaseApp) ValidatorKeyProvider() KeyGenF {
return app.validatorKeyProvider
}
4 changes: 4 additions & 0 deletions runtime/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,7 @@ var _ servertypes.Application = &App{}
type hasServicesV1 interface {
RegisterServices(grpc.ServiceRegistrar) error
}

func (a *App) ValidatorKeyPrvoder() baseapp.KeyGenF {
return a.ValidatorKeyProvider()
}
4 changes: 1 addition & 3 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,7 @@ func startCmtNode(
return nil, cleanupFn, err
}

pv, err := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile(), func() (cmtcrypto.PrivKey, error) {
return cmted25519.GenPrivKey(), nil
}) // TODO: make this modular
pv, err := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile(), app.ValidatorKeyProvider())
if err != nil {
return nil, cleanupFn, err
}
Expand Down
4 changes: 4 additions & 0 deletions server/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"

cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
cmtcrypto "github.com/cometbft/cometbft/crypto"
cmttypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/gogoproto/grpc"

Expand Down Expand Up @@ -58,6 +59,9 @@ type (
// SnapshotManager return the snapshot manager
SnapshotManager() *snapshots.Manager

// ValidatorKeyProvider returns a function that generates a validator key
ValidatorKeyProvider() func() (cmtcrypto.PrivKey, error)

// Close is called in start cmd to gracefully cleanup resources.
// Must be safe to be called multiple times.
Close() error
Expand Down
4 changes: 4 additions & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,10 @@ func (app *SimApp) RegisterNodeService(clientCtx client.Context, cfg config.Conf
nodeservice.RegisterNodeService(clientCtx, app.GRPCQueryRouter(), cfg)
}

func (app *SimApp) ValidatorKeyProvider() baseapp.KeyGenF {
return app.ValidatorKeyProvider()
}

// GetMaccPerms returns a copy of the module account permissions
//
// NOTE: This is solely to be used for testing purposes.
Expand Down
Loading