Skip to content

Commit

Permalink
testnet setup command to generate non-determinstic private keys and i…
Browse files Browse the repository at this point in the history
…mproved runtime sanity checks for the genesis validators
  • Loading branch information
charithabandi committed Mar 4, 2025
1 parent 4428f7c commit 85bbe65
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
18 changes: 10 additions & 8 deletions app/setup/testnet.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package setup

import (
"crypto/sha256"
"encoding/binary"
"crypto/rand"
"fmt"
"math/rand/v2"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -130,7 +128,6 @@ type ConfigOpts struct {
DnsHost bool
}

// TODO: once changes to the tests are complete, this may not be needed
func GenerateTestnetConfigs(cfg *TestnetConfig, opts *ConfigOpts, gencfg *config.GenesisConfig) error {
if len(cfg.Hostnames) > 0 && len(cfg.Hostnames) != cfg.NumVals+cfg.NumNVals {
return fmt.Errorf("if set, the number of hostnames %d must be equal to number of validators + number of non-validators %d",
Expand All @@ -148,15 +145,18 @@ func GenerateTestnetConfigs(cfg *TestnetConfig, opts *ConfigOpts, gencfg *config
return err
}

var keys []*crypto.Secp256k1PrivateKey
// generate the configuration for the nodes
for i := range cfg.NumVals + cfg.NumNVals {
// generate Keys, so that the connection strings and the validator set can be generated before the node config files are generated

// generate Keys, so that the connection strings and the validator set can be generated before the node config files are generated
var keys []*crypto.Secp256k1PrivateKey
for range cfg.NumVals + cfg.NumNVals {
/* NOTE: this is a deterministic way to generate keys, but it is not used in the code
var seed [32]byte
binary.LittleEndian.PutUint64(seed[:], cfg.StartingPort+uint64(i))
seed = sha256.Sum256(seed[:])
rr := rand.NewChaCha8(seed)
priv := node.NewKey(&deterministicPRNG{ChaCha8: rr})
*/
priv := node.NewKey(rand.Reader) //&deterministicPRNG{ChaCha8: rr})
keys = append(keys, priv)
}

Expand Down Expand Up @@ -291,6 +291,7 @@ func GenerateNodeRoot(ncfg *NodeGenConfig) error {
return GenerateNodeDir(ncfg.RootDir, ncfg.Genesis, cfg, ncfg.NodeKey, "")
}

/*
type deterministicPRNG struct {
readBuf [8]byte
readLen int // 0 <= readLen <= 8
Expand All @@ -317,6 +318,7 @@ func (dr *deterministicPRNG) Read(p []byte) (n int, err error) {
}
return n, nil
}
*/

type TestnetNodeConfig struct {
// Config is the node configuration.
Expand Down
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ func (gc *GenesisConfig) SanityChecks() error {
if val.Power <= 0 {
return fmt.Errorf("Genesis validators should have non-zero power")
}

// ensure that the key type is valid
_, ok := crypto.KeyTypeDefinition(val.KeyType)
if !ok {
return fmt.Errorf("invalid key type: %s", val.KeyType)
}

// ensure that the pubkey is valid
_, err := crypto.UnmarshalPublicKey(val.Identifier, val.KeyType)
if err != nil {
return fmt.Errorf("invalid public key: %s error: %s", val.Identifier, err)
}
}

if gc.InitialHeight < 0 {
Expand Down

0 comments on commit 85bbe65

Please sign in to comment.