cosmos-sdk/x/genutil/utils_test.go
Leonardo Bragagnolo 0dbc5de9dd
fix: Added missing validator key save when recovering from mnemonic (#9638)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

This PR adds a missing save inside the `InitializeNodeValidatorFilesFromMnemonic` method. 
Without it when using `gaiad init <name> --recover` no `priv_validator_key.json` file is created.

Closes: #XXXX

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### 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)
2021-07-06 13:52:23 +00:00

73 lines
1.9 KiB
Go

package genutil
import (
"encoding/json"
tmed25519 "github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/privval"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/config"
)
func TestExportGenesisFileWithTime(t *testing.T) {
t.Parallel()
fname := filepath.Join(t.TempDir(), "genesis.json")
require.NoError(t, ExportGenesisFileWithTime(fname, "test", nil, json.RawMessage(`{"account_owner": "Bob"}`), time.Now()))
}
func TestInitializeNodeValidatorFilesFromMnemonic(t *testing.T) {
t.Parallel()
cfg := config.TestConfig()
cfg.RootDir = t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(cfg.RootDir, "config"), 0755))
tests := []struct {
name string
mnemonic string
expError bool
}{
{
name: "invalid mnemonic returns error",
mnemonic: "side video kiss hotel essence",
expError: true,
},
{
name: "empty mnemonic does not return error",
mnemonic: "",
expError: false,
},
{
name: "valid mnemonic does not return error",
mnemonic: "side video kiss hotel essence door angle student degree during vague adjust submit trick globe muscle frozen vacuum artwork million shield bind useful wave",
expError: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
_, _, err := InitializeNodeValidatorFilesFromMnemonic(cfg, tt.mnemonic)
if tt.expError {
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)
}
}
})
}
}