From 0dbc5de9ddcc31b31071d4361ddd8890ee8e88ab Mon Sep 17 00:00:00 2001 From: Leonardo Bragagnolo Date: Tue, 6 Jul 2021 15:52:23 +0200 Subject: [PATCH] fix: Added missing validator key save when recovering from mnemonic (#9638) This PR adds a missing save inside the `InitializeNodeValidatorFilesFromMnemonic` method. Without it when using `gaiad init --recover` no `priv_validator_key.json` file is created. Closes: #XXXX --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- CHANGELOG.md | 2 +- x/genutil/utils.go | 3 ++- x/genutil/utils_test.go | 9 +++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d11c6ed9..190615fb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,7 +76,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (types) [\#9627](https://github.com/cosmos/cosmos-sdk/pull/9627) Fix nil pointer panic on `NewBigIntFromInt` * (x/genutil) [\#9574](https://github.com/cosmos/cosmos-sdk/pull/9575) Actually use the `gentx` client tx flags (like `--keyring-dir`) * (x/distribution) [\#9599](https://github.com/cosmos/cosmos-sdk/pull/9599) Withdraw rewards event now includes a value attribute even if there are 0 rewards (due to situations like 100% commission). - +* (x/genutil) [\#9638](https://github.com/cosmos/cosmos-sdk/pull/9638) Added missing validator key save when recovering from mnemonic ## [v0.43.0-rc0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.43.0-rc0) - 2021-06-25 diff --git a/x/genutil/utils.go b/x/genutil/utils.go index 4c46bdb6f..4851a4081 100644 --- a/x/genutil/utils.go +++ b/x/genutil/utils.go @@ -54,7 +54,7 @@ func InitializeNodeValidatorFiles(config *cfg.Config) (nodeID string, valPubKey return InitializeNodeValidatorFilesFromMnemonic(config, "") } -// InitializeNodeValidatorFiles creates private validator and p2p configuration files using the given mnemonic. +// 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 string) (nodeID string, valPubKey cryptotypes.PubKey, err error) { if len(mnemonic) > 0 && !bip39.IsMnemonicValid(mnemonic) { @@ -84,6 +84,7 @@ func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic strin } else { privKey := tmed25519.GenPrivKeyFromSecret([]byte(mnemonic)) filePV = privval.NewFilePV(privKey, pvKeyFile, pvStateFile) + filePV.Save() } tmValPubKey, err := filePV.GetPubKey() diff --git a/x/genutil/utils_test.go b/x/genutil/utils_test.go index a845cc68c..df0984a4a 100644 --- a/x/genutil/utils_test.go +++ b/x/genutil/utils_test.go @@ -2,6 +2,8 @@ package genutil import ( "encoding/json" + tmed25519 "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/privval" "os" "path/filepath" "testing" @@ -57,6 +59,13 @@ func TestInitializeNodeValidatorFilesFromMnemonic(t *testing.T) { require.Error(t, err) } else { require.NoError(t, err) + + if tt.mnemonic != "" { + actualPVFile := privval.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile()) + expectedPrivateKey := tmed25519.GenPrivKeyFromSecret([]byte(tt.mnemonic)) + expectedFile := privval.NewFilePV(expectedPrivateKey, cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile()) + require.Equal(t, expectedFile, actualPVFile) + } } }) }