Skip to content

Commit

Permalink
fix(x/genutil): fix logic error introduced in #20296
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed Jun 17, 2024
1 parent 6d2f6ff commit 314327e
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions x/genutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func ExportGenesisFile(genesis *types.AppGenesis, genFile string) error {

// ExportGenesisFileWithTime creates and writes the genesis configuration to disk.
// An error is returned if building or writing the configuration to file fails.
func ExportGenesisFileWithTime(genFile, chainID string, validators []cmttypes.GenesisValidator, appState json.RawMessage, genTime time.Time) error {
func ExportGenesisFileWithTime(
genFile, chainID string, validators []cmttypes.GenesisValidator, appState json.RawMessage, genTime time.Time,
) error {
appGenesis := types.NewAppGenesisWithVersion(chainID, appState)
appGenesis.GenesisTime = genTime
appGenesis.Consensus.Validators = validators
Expand All @@ -46,13 +48,17 @@ func ExportGenesisFileWithTime(genFile, chainID string, validators []cmttypes.Ge
}

// InitializeNodeValidatorFiles creates private validator and p2p configuration files.
func InitializeNodeValidatorFiles(config *cfg.Config, keyType string) (nodeID string, valPubKey cryptotypes.PubKey, err error) {
func InitializeNodeValidatorFiles(config *cfg.Config, keyType string) (
nodeID string, valPubKey cryptotypes.PubKey, err error,
) {
return InitializeNodeValidatorFilesFromMnemonic(config, "", keyType)
}

// InitializeNodeValidatorFilesFromMnemonic creates private validator and p2p configuration files using the given mnemonic.
// If no valid mnemonic is given, a random one will be used instead.
func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic, keyType string) (nodeID string, valPubKey cryptotypes.PubKey, err error) {
func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic, keyType string) (
nodeID string, valPubKey cryptotypes.PubKey, err error,
) {
if len(mnemonic) > 0 && !bip39.IsMnemonicValid(mnemonic) {
return "", nil, fmt.Errorf("invalid mnemonic")
}
Expand Down Expand Up @@ -81,14 +87,15 @@ func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic, keyT
if len(mnemonic) == 0 {
switch keyType {
case "ed25519":
privKey = tmed25519.GenPrivKey()
filePV = loadOrGenFilePV(tmed25519.GenPrivKey(), pvKeyFile, pvStateFile)
case "bls12_381":
privKey, err = cmtbls12381.GenPrivKey()
if err != nil {
return "", nil, err
}
filePV = loadOrGenFilePV(privKey, pvKeyFile, pvStateFile)
default:
privKey = tmed25519.GenPrivKey()
filePV = loadOrGenFilePV(tmed25519.GenPrivKey(), pvKeyFile, pvStateFile)
}
} else {
switch keyType {
Expand All @@ -100,11 +107,10 @@ func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic, keyT
default:
privKey = tmed25519.GenPrivKeyFromSecret([]byte(mnemonic))
}
filePV = privval.NewFilePV(privKey, pvKeyFile, pvStateFile)
filePV.Save()
}

filePV = privval.NewFilePV(privKey, pvKeyFile, pvStateFile)
filePV.Save()

tmValPubKey, err := filePV.GetPubKey()
if err != nil {
return "", nil, err
Expand All @@ -117,3 +123,19 @@ func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic, keyT

return nodeID, valPubKey, nil
}

// loadOrGenFilePV loads a FilePV from the given filePaths
// or else generates a new one and saves it to the filePaths.
func loadOrGenFilePV(privKey cmtcrypto.PrivKey, keyFilePath, stateFilePath string) *privval.FilePV {
_, err := os.Stat(keyFilePath)
exists := !os.IsNotExist(err)

var pv *privval.FilePV
if exists {
pv = privval.LoadFilePV(keyFilePath, stateFilePath)
} else {
pv = privval.NewFilePV(privKey, keyFilePath, stateFilePath)
pv.Save()
}
return pv
}

0 comments on commit 314327e

Please sign in to comment.