Skip to content

Commit fea175a

Browse files
feat: make validator key injectable by application developers (backport #21608) (#21802)
Co-authored-by: Marko <marko@baricevic.me> Co-authored-by: marbar3778 <marbar3778@yahoo.com>
1 parent 2174a3d commit fea175a

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

runtime/app.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"slices"
77

88
abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
9+
cmtcrypto "github.com/cometbft/cometbft/crypto"
10+
cmted25519 "github.com/cometbft/cometbft/crypto/ed25519"
911
"google.golang.org/grpc"
1012

1113
runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
@@ -30,6 +32,9 @@ import (
3032
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
3133
)
3234

35+
// KeyGenF is a function that generates a private key for use by comet.
36+
type KeyGenF = func() (cmtcrypto.PrivKey, error)
37+
3338
// App is a wrapper around BaseApp and ModuleManager that can be used in hybrid
3439
// app.go/app config scenarios or directly as a servertypes.Application instance.
3540
// To get an instance of *App, *AppBuilder must be requested as a dependency
@@ -308,3 +313,10 @@ var _ servertypes.Application = &App{}
308313
type hasServicesV1 interface {
309314
RegisterServices(grpc.ServiceRegistrar) error
310315
}
316+
317+
// ValidatorKeyProvider returns a function that generates a private key for use by comet.
318+
func (a *App) ValidatorKeyProvider() KeyGenF {
319+
return func() (cmtcrypto.PrivKey, error) {
320+
return cmted25519.GenPrivKey(), nil
321+
}
322+
}

server/start.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,7 @@ func startCmtNode(
374374
return nil, cleanupFn, err
375375
}
376376

377-
pv, err := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile(), func() (cmtcrypto.PrivKey, error) {
378-
return cmted25519.GenPrivKey(), nil
379-
}) // TODO: make this modular
377+
pv, err := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile(), app.ValidatorKeyProvider())
380378
if err != nil {
381379
return nil, cleanupFn, err
382380
}

server/types/app.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66

77
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
8+
cmtcrypto "github.com/cometbft/cometbft/crypto"
89
cmttypes "github.com/cometbft/cometbft/types"
910
"github.com/cosmos/gogoproto/grpc"
1011

@@ -57,6 +58,9 @@ type (
5758
// SnapshotManager return the snapshot manager
5859
SnapshotManager() *snapshots.Manager
5960

61+
// ValidatorKeyProvider returns a function that generates a validator key
62+
ValidatorKeyProvider() func() (cmtcrypto.PrivKey, error)
63+
6064
// Close is called in start cmd to gracefully cleanup resources.
6165
// Must be safe to be called multiple times.
6266
Close() error

server/v2/cometbft/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
package cometbft
22

33
import (
4+
cmtcrypto "github.com/cometbft/cometbft/crypto"
5+
cmted22519 "github.com/cometbft/cometbft/crypto/ed25519"
6+
47
"cosmossdk.io/core/transaction"
58
"cosmossdk.io/server/v2/cometbft/handlers"
69
"cosmossdk.io/server/v2/cometbft/mempool"
710
"cosmossdk.io/server/v2/cometbft/types"
811
"cosmossdk.io/store/v2/snapshots"
912
)
1013

14+
type keyGenF = func() (cmtcrypto.PrivKey, error)
15+
1116
// ServerOptions defines the options for the CometBFT server.
1217
// When an option takes a map[string]any, it can access the app.tom's cometbft section and the config.toml config.
1318
type ServerOptions[T transaction.Tx] struct {
1419
PrepareProposalHandler handlers.PrepareHandler[T]
1520
ProcessProposalHandler handlers.ProcessHandler[T]
1621
VerifyVoteExtensionHandler handlers.VerifyVoteExtensionhandler
1722
ExtendVoteHandler handlers.ExtendVoteHandler
23+
KeygenF keyGenF
1824

1925
Mempool func(cfg map[string]any) mempool.Mempool[T]
2026
SnapshotOptions func(cfg map[string]any) snapshots.SnapshotOptions
@@ -35,5 +41,6 @@ func DefaultServerOptions[T transaction.Tx]() ServerOptions[T] {
3541
SnapshotOptions: func(cfg map[string]any) snapshots.SnapshotOptions { return snapshots.NewSnapshotOptions(0, 0) },
3642
AddrPeerFilter: nil,
3743
IdPeerFilter: nil,
44+
KeygenF: func() (cmtcrypto.PrivKey, error) { return cmted22519.GenPrivKey(), nil },
3845
}
3946
}

server/v2/cometbft/server.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
abciserver "github.com/cometbft/cometbft/abci/server"
1212
cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
1313
cmtcfg "github.com/cometbft/cometbft/config"
14-
cmtcrypto "github.com/cometbft/cometbft/crypto"
15-
cmted25519 "github.com/cometbft/cometbft/crypto/ed25519"
1614
"github.com/cometbft/cometbft/node"
1715
"github.com/cometbft/cometbft/p2p"
1816
pvm "github.com/cometbft/cometbft/privval"
@@ -159,9 +157,7 @@ func (s *CometBFTServer[T]) Start(ctx context.Context) error {
159157
pv, err := pvm.LoadOrGenFilePV(
160158
s.config.ConfigTomlConfig.PrivValidatorKeyFile(),
161159
s.config.ConfigTomlConfig.PrivValidatorStateFile(),
162-
func() (cmtcrypto.PrivKey, error) {
163-
return cmted25519.GenPrivKey(), nil
164-
},
160+
s.serverOptions.KeygenF,
165161
)
166162
if err != nil {
167163
return err

simapp/app.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"path/filepath"
1212

1313
abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
14+
cmtcrypto "github.com/cometbft/cometbft/crypto"
15+
cmted25519 "github.com/cometbft/cometbft/crypto/ed25519"
1416
"github.com/cosmos/gogoproto/proto"
1517
"github.com/spf13/cast"
1618

@@ -829,6 +831,14 @@ func (app *SimApp) RegisterNodeService(clientCtx client.Context, cfg config.Conf
829831
nodeservice.RegisterNodeService(clientCtx, app.GRPCQueryRouter(), cfg)
830832
}
831833

834+
// ValidatorKeyProvider returns a function that generates a validator key
835+
// Supported key types are those supported by Comet: ed25519, secp256k1, bls12-381
836+
func (app *SimApp) ValidatorKeyProvider() runtime.KeyGenF {
837+
return func() (cmtcrypto.PrivKey, error) {
838+
return cmted25519.GenPrivKey(), nil
839+
}
840+
}
841+
832842
// GetMaccPerms returns a copy of the module account permissions
833843
//
834844
// NOTE: This is solely to be used for testing purposes.

0 commit comments

Comments
 (0)