mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-04 07:07:13 +00:00
cmd: add support for --key (#5612)
Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
This commit is contained in:
@@ -115,8 +115,12 @@ func startNode(cfg *Config) error {
|
||||
return fmt.Errorf("failed to setup config: %w", err)
|
||||
}
|
||||
|
||||
pval, err := privval.LoadOrGenFilePV(tmcfg.PrivValidatorKeyFile(), tmcfg.PrivValidatorStateFile())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := node.NewNode(tmcfg,
|
||||
privval.LoadOrGenFilePV(tmcfg.PrivValidatorKeyFile(), tmcfg.PrivValidatorStateFile()),
|
||||
pval,
|
||||
nodeKey,
|
||||
proxy.NewLocalClientCreator(app),
|
||||
node.DefaultGenesisDocProviderFunc(tmcfg),
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
e2e "github.com/tendermint/tendermint/test/e2e/pkg"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -22,6 +23,7 @@ var (
|
||||
map[string]string{"initial01": "a", "initial02": "b", "initial03": "c"},
|
||||
},
|
||||
"validators": {"genesis", "initchain"},
|
||||
"keyType": {types.ABCIPubKeyTypeEd25519, types.ABCIPubKeyTypeSecp256k1},
|
||||
}
|
||||
|
||||
// The following specify randomly chosen values for testnet nodes.
|
||||
@@ -74,6 +76,7 @@ func generateTestnet(r *rand.Rand, opt map[string]interface{}) (e2e.Manifest, er
|
||||
Validators: &map[string]int64{},
|
||||
ValidatorUpdates: map[string]map[string]int64{},
|
||||
Nodes: map[string]*e2e.ManifestNode{},
|
||||
KeyType: opt["keyType"].(string),
|
||||
}
|
||||
|
||||
var numSeeds, numValidators, numFulls int
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
|
||||
[node.validator]
|
||||
|
||||
@@ -46,6 +46,10 @@ type Manifest struct {
|
||||
|
||||
// Nodes specifies the network nodes. At least one node must be given.
|
||||
Nodes map[string]*ManifestNode `toml:"node"`
|
||||
|
||||
// KeyType sets the curve that will be used by validators.
|
||||
// Options are ed25519 & secp256k1
|
||||
KeyType string `toml:"key_type"`
|
||||
}
|
||||
|
||||
// ManifestNode represents a node in a testnet manifest.
|
||||
|
||||
+22
-4
@@ -14,8 +14,10 @@ import (
|
||||
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
"github.com/tendermint/tendermint/crypto/secp256k1"
|
||||
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
|
||||
mcs "github.com/tendermint/tendermint/test/maverick/consensus"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -57,6 +59,7 @@ type Testnet struct {
|
||||
Validators map[*Node]int64
|
||||
ValidatorUpdates map[int64]map[*Node]int64
|
||||
Nodes []*Node
|
||||
KeyType string
|
||||
}
|
||||
|
||||
// Node represents a Tendermint node in a testnet.
|
||||
@@ -118,6 +121,10 @@ func LoadTestnet(file string) (*Testnet, error) {
|
||||
Validators: map[*Node]int64{},
|
||||
ValidatorUpdates: map[int64]map[*Node]int64{},
|
||||
Nodes: []*Node{},
|
||||
KeyType: "ed25519",
|
||||
}
|
||||
if len(manifest.KeyType) != 0 {
|
||||
testnet.KeyType = manifest.KeyType
|
||||
}
|
||||
if manifest.InitialHeight > 0 {
|
||||
testnet.InitialHeight = manifest.InitialHeight
|
||||
@@ -135,7 +142,7 @@ func LoadTestnet(file string) (*Testnet, error) {
|
||||
node := &Node{
|
||||
Name: name,
|
||||
Testnet: testnet,
|
||||
Key: keyGen.Generate(),
|
||||
Key: keyGen.Generate(manifest.KeyType),
|
||||
IP: ipGen.Next(),
|
||||
ProxyPort: proxyPortGen.Next(),
|
||||
Mode: ModeValidator,
|
||||
@@ -263,6 +270,11 @@ func (t Testnet) Validate() error {
|
||||
if len(t.Nodes) == 0 {
|
||||
return errors.New("network has no nodes")
|
||||
}
|
||||
switch t.KeyType {
|
||||
case "", types.ABCIPubKeyTypeEd25519, types.ABCIPubKeyTypeSecp256k1:
|
||||
default:
|
||||
return errors.New("unsupported KeyType")
|
||||
}
|
||||
for _, node := range t.Nodes {
|
||||
if err := node.Validate(t); err != nil {
|
||||
return fmt.Errorf("invalid node %q: %w", node.Name, err)
|
||||
@@ -466,15 +478,21 @@ func newKeyGenerator(seed int64) *keyGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
func (g *keyGenerator) Generate() crypto.PrivKey {
|
||||
func (g *keyGenerator) Generate(keyType string) crypto.PrivKey {
|
||||
seed := make([]byte, ed25519.SeedSize)
|
||||
|
||||
_, err := io.ReadFull(g.random, seed)
|
||||
if err != nil {
|
||||
panic(err) // this shouldn't happen
|
||||
}
|
||||
|
||||
return ed25519.GenPrivKeyFromSecret(seed)
|
||||
switch keyType {
|
||||
case "secp256k1":
|
||||
return secp256k1.GenPrivKeySecp256k1(seed)
|
||||
case "", "ed25519":
|
||||
return ed25519.GenPrivKeyFromSecret(seed)
|
||||
default:
|
||||
panic("KeyType not supported") // should not make it this far
|
||||
}
|
||||
}
|
||||
|
||||
// portGenerator generates local Docker proxy ports for each node.
|
||||
|
||||
@@ -191,6 +191,13 @@ func MakeGenesis(testnet *e2e.Testnet) (types.GenesisDoc, error) {
|
||||
ConsensusParams: types.DefaultConsensusParams(),
|
||||
InitialHeight: testnet.InitialHeight,
|
||||
}
|
||||
switch testnet.KeyType {
|
||||
case "", types.ABCIPubKeyTypeEd25519, types.ABCIPubKeyTypeSecp256k1:
|
||||
genesis.ConsensusParams.Validator.PubKeyTypes =
|
||||
append(genesis.ConsensusParams.Validator.PubKeyTypes, types.ABCIPubKeyTypeSecp256k1)
|
||||
default:
|
||||
return genesis, errors.New("unsupported KeyType")
|
||||
}
|
||||
for validator, power := range testnet.Validators {
|
||||
genesis.Validators = append(genesis.Validators, types.GenesisValidator{
|
||||
Name: validator.Name,
|
||||
|
||||
@@ -41,7 +41,10 @@ func WALGenerateNBlocks(t *testing.T, wr io.Writer, numBlocks int) (err error) {
|
||||
// NOTE: we don't do handshake so need to set state.Version.Consensus.App directly.
|
||||
privValidatorKeyFile := config.PrivValidatorKeyFile()
|
||||
privValidatorStateFile := config.PrivValidatorStateFile()
|
||||
privValidator := privval.LoadOrGenFilePV(privValidatorKeyFile, privValidatorStateFile)
|
||||
privValidator, err := privval.LoadOrGenFilePV(privValidatorKeyFile, privValidatorStateFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
genDoc, err := types.GenesisDocFromFile(config.GenesisFile())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read genesis file: %w", err)
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
tmos "github.com/tendermint/tendermint/libs/os"
|
||||
tmrand "github.com/tendermint/tendermint/libs/rand"
|
||||
"github.com/tendermint/tendermint/p2p"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
cs "github.com/tendermint/tendermint/test/maverick/consensus"
|
||||
nd "github.com/tendermint/tendermint/test/maverick/node"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
@@ -156,12 +157,19 @@ func startNode(config *cfg.Config, logger log.Logger, misbehaviorFlag string) er
|
||||
select {}
|
||||
}
|
||||
|
||||
var keyType string
|
||||
|
||||
var InitFilesCmd = &cobra.Command{
|
||||
Use: "init",
|
||||
Short: "Initialize Tendermint",
|
||||
RunE: initFiles,
|
||||
}
|
||||
|
||||
func init() {
|
||||
InitFilesCmd.Flags().StringVar(&keyType, "key", types.ABCIPubKeyTypeEd25519,
|
||||
"Key type to generate privval file with. Options: ed25519, secp256k1")
|
||||
}
|
||||
|
||||
func initFiles(cmd *cobra.Command, args []string) error {
|
||||
return initFilesWithConfig(config)
|
||||
}
|
||||
@@ -202,6 +210,11 @@ func initFilesWithConfig(config *cfg.Config) error {
|
||||
GenesisTime: tmtime.Now(),
|
||||
ConsensusParams: types.DefaultConsensusParams(),
|
||||
}
|
||||
if keyType == "secp256k1" {
|
||||
genDoc.ConsensusParams.Validator = tmproto.ValidatorParams{
|
||||
PubKeyTypes: []string{types.ABCIPubKeyTypeSecp256k1},
|
||||
}
|
||||
}
|
||||
pubKey, err := pv.GetPubKey()
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't get pubkey: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user