mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-03 16:37:39 +00:00
Handles issue with empty dirs on validator accounts create (#5940)
* handles issue with empty dirs on validator accounts create * better comments * Merge refs/heads/master into fix-validator-accounts-create * Merge refs/heads/master into fix-validator-accounts-create * Terence's review
This commit is contained in:
parent
6a9112bfae
commit
c875dd458b
@ -118,7 +118,9 @@ func NewValidatorAccount(directory string, password string) error {
|
||||
}
|
||||
|
||||
// Exists checks if a validator account at a given keystore path exists.
|
||||
func Exists(keystorePath string) (bool, error) {
|
||||
// assertNonEmpty is a boolean used to determine whether to check that
|
||||
// the provided directory exists.
|
||||
func Exists(keystorePath string, assertNonEmpty bool) (bool, error) {
|
||||
/* #nosec */
|
||||
f, err := os.Open(keystorePath)
|
||||
if err != nil {
|
||||
@ -130,10 +132,13 @@ func Exists(keystorePath string) (bool, error) {
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = f.Readdirnames(1) // Or f.Readdir(1)
|
||||
if err == io.EOF {
|
||||
return false, nil
|
||||
if assertNonEmpty {
|
||||
_, err = f.Readdirnames(1) // Or f.Readdir(1)
|
||||
if err == io.EOF {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
return true, err
|
||||
}
|
||||
|
||||
@ -141,7 +146,7 @@ func Exists(keystorePath string) (bool, error) {
|
||||
func CreateValidatorAccount(path string, passphrase string) (string, string, error) {
|
||||
// Forces user to create directory if using non-default path.
|
||||
if path != DefaultValidatorDir() {
|
||||
exists, err := Exists(path)
|
||||
exists, err := Exists(path, false /* assertNonEmpty */)
|
||||
if err != nil {
|
||||
return path, passphrase, err
|
||||
}
|
||||
|
@ -46,12 +46,26 @@ func TestNewValidatorAccount_AccountExists(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewValidatorAccount_CreateValidatorAccount(t *testing.T) {
|
||||
directory := "foobar"
|
||||
_, _, err := CreateValidatorAccount(directory, "foobar")
|
||||
wantErrString := fmt.Sprintf("path %q does not exist", directory)
|
||||
directory := testutil.TempDir() + "/testkeystore"
|
||||
defer func() {
|
||||
if err := os.RemoveAll(directory); err != nil {
|
||||
t.Logf("Could not remove directory: %v", err)
|
||||
}
|
||||
}()
|
||||
_, _, err := CreateValidatorAccount("foobar", "foobar")
|
||||
wantErrString := fmt.Sprintf("path %q does not exist", "foobar")
|
||||
if err == nil || err.Error() != wantErrString {
|
||||
t.Errorf("expected error not thrown, want: %v, got: %v", wantErrString, err)
|
||||
}
|
||||
|
||||
// Make sure that empty existing directory doesn't trigger any errors.
|
||||
if err := os.Mkdir(directory, 0777); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _, err = CreateValidatorAccount(directory, "foobar")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleEmptyFlags_FlagsSet(t *testing.T) {
|
||||
|
@ -49,7 +49,7 @@ func NewKeystore(input string) (KeyManager, string, error) {
|
||||
}
|
||||
log.WithField("keystorePath", opts.Path).Info("Checking validator keys")
|
||||
|
||||
exists, err := accounts.Exists(opts.Path)
|
||||
exists, err := accounts.Exists(opts.Path, true /* assertNonEmpty */)
|
||||
if err != nil {
|
||||
return nil, keystoreOptsHelp, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user