Skip to content

Commit 6e4c943

Browse files
committed
concurrently generate multiple node keys
1 parent d3f9c3b commit 6e4c943

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

local/network.go

+50-13
Original file line numberDiff line numberDiff line change
@@ -415,21 +415,15 @@ func NewDefaultConfigNNodes(binaryPath string, numNodes uint32) (network.Config,
415415
if !ok {
416416
return netConfig, fmt.Errorf("expected float64 for last standard api port, got %T", refStakingPortIntf)
417417
}
418+
nodesKeys, err := getNodesKeys(toAdd)
419+
if err != nil {
420+
return netConfig, err
421+
}
418422
for i := 0; i < toAdd; i++ {
419423
nodeConfig := refNodeConfig
420-
stakingCert, stakingKey, err := staking.NewCertAndKeyBytes()
421-
if err != nil {
422-
return netConfig, fmt.Errorf("couldn't generate staking Cert/Key: %w", err)
423-
}
424-
nodeConfig.StakingKey = string(stakingKey)
425-
nodeConfig.StakingCert = string(stakingCert)
426-
key, err := bls.NewSecretKey()
427-
if err != nil {
428-
return netConfig, fmt.Errorf("couldn't generate new signing key: %w", err)
429-
}
430-
keyBytes := bls.SecretKeyToBytes(key)
431-
encodedKey := base64.StdEncoding.EncodeToString(keyBytes)
432-
nodeConfig.StakingSigningKey = encodedKey
424+
nodeConfig.StakingKey = nodesKeys[i].stakingKey
425+
nodeConfig.StakingCert = nodesKeys[i].stakingCert
426+
nodeConfig.StakingSigningKey = nodesKeys[i].blsKey
433427
// replace ports
434428
nodeConfig.Flags = map[string]interface{}{
435429
config.HTTPPortKey: int(refAPIPort) + (i+1)*2,
@@ -447,6 +441,49 @@ func NewDefaultConfigNNodes(binaryPath string, numNodes uint32) (network.Config,
447441
return netConfig, nil
448442
}
449443

444+
type nodeKeys struct {
445+
stakingKey string
446+
stakingCert string
447+
blsKey string
448+
}
449+
450+
func getNodeKeys() (*nodeKeys, error) {
451+
keys := nodeKeys{}
452+
stakingCertBytes, stakingKeyBytes, err := staking.NewCertAndKeyBytes()
453+
if err != nil {
454+
return nil, fmt.Errorf("couldn't generate staking Cert/Key: %w", err)
455+
}
456+
keys.stakingKey = string(stakingKeyBytes)
457+
keys.stakingCert = string(stakingCertBytes)
458+
key, err := bls.NewSecretKey()
459+
if err != nil {
460+
return nil, fmt.Errorf("couldn't generate new signing key: %w", err)
461+
}
462+
blsKeyBytes := bls.SecretKeyToBytes(key)
463+
keys.blsKey = base64.StdEncoding.EncodeToString(blsKeyBytes)
464+
return &keys, nil
465+
}
466+
467+
func getNodesKeys(num int) ([]*nodeKeys, error) {
468+
nodesKeys := []*nodeKeys{}
469+
lock := sync.Mutex{}
470+
eg := errgroup.Group{}
471+
for i := 0; i < num; i++ {
472+
eg.Go(func() error {
473+
keys, err := getNodeKeys()
474+
if err != nil {
475+
return err
476+
}
477+
lock.Lock()
478+
nodesKeys = append(nodesKeys, keys)
479+
lock.Unlock()
480+
return nil
481+
})
482+
}
483+
err := eg.Wait()
484+
return nodesKeys, err
485+
}
486+
450487
func (ln *localNetwork) loadConfig(ctx context.Context, networkConfig network.Config) error {
451488
if err := networkConfig.Validate(); err != nil {
452489
return fmt.Errorf("config failed validation: %w", err)

server/network.go

-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ func (lc *localNetwork) createConfig() error {
146146
if err != nil {
147147
return err
148148
}
149-
return fmt.Errorf("PEPE")
150149

151150
var globalConfig map[string]interface{}
152151

0 commit comments

Comments
 (0)