Fix empty wallet check (#6819)

This commit is contained in:
terence tsao 2020-07-31 17:08:40 -07:00 committed by GitHub
parent 4dfe02d919
commit eb0ad0669f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 6 deletions

View File

@ -63,6 +63,7 @@ go_test(
"//proto/validator/accounts/v2:go_default_library",
"//shared/bls:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/params:go_default_library",
"//shared/petnames:go_default_library",
"//shared/roughtime:go_default_library",
"//shared/testutil:go_default_library",

View File

@ -82,7 +82,7 @@ func NewWallet(
return nil, errors.Wrap(err, "could not check if wallet exists")
}
if walletExists {
isEmptyWallet, err := isEmptyDir(walletDir)
isEmptyWallet, err := isEmptyWallet(walletDir)
if err != nil {
return nil, errors.Wrap(err, "could not check if wallet has files")
}
@ -132,7 +132,7 @@ func OpenWallet(cliCtx *cli.Context) (*Wallet, error) {
return nil, errors.Wrap(err, "could not parse wallet directory")
}
if ok {
isEmptyWallet, err := isEmptyDir(walletDir)
isEmptyWallet, err := isEmptyWallet(walletDir)
if err != nil {
return nil, errors.Wrap(err, "could not check if wallet has files")
}
@ -633,7 +633,7 @@ func createOrOpenWallet(cliCtx *cli.Context, creationFunc func(cliCtx *cli.Conte
return nil, errors.Wrapf(err, "could not check if wallet dir %s exists", directory)
}
if ok {
isEmptyWallet, err := isEmptyDir(directory)
isEmptyWallet, err := isEmptyWallet(directory)
if err != nil {
return nil, errors.Wrap(err, "could not check if wallet has files")
}
@ -671,7 +671,9 @@ func hasDir(dirPath string) (bool, error) {
return info.IsDir(), err
}
func isEmptyDir(name string) (bool, error) {
// isEmptyWallet checks if a folder consists key directory such as `derived`, `remote` or `direct`.
// Returns true if exists, false otherwise.
func isEmptyWallet(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
@ -681,9 +683,18 @@ func isEmptyDir(name string) (bool, error) {
log.Debugf("Could not close directory: %s", name)
}
}()
_, err = f.Readdirnames(1)
names, err := f.Readdirnames(-1)
if err == io.EOF {
return true, nil
}
return false, err // Either not empty or error, suits both cases
for _, n := range names {
// Nil error means input name is `derived`, `remote` or `direct`, the wallet is not empty.
_, err := v2keymanager.ParseKind(n)
if err == nil {
return false, nil
}
}
return true, err
}

View File

@ -13,6 +13,7 @@ import (
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
@ -139,3 +140,19 @@ func TestAccountTimestamp(t *testing.T) {
})
}
}
func Test_IsEmptyWallet_RandomFiles(t *testing.T) {
path := testutil.TempDir()
walletDir := filepath.Join(path, "test")
require.NoError(t, os.MkdirAll(walletDir, params.BeaconIoConfig().ReadWriteExecutePermissions), "Failed to remove directory")
got, err := isEmptyWallet(path)
require.NoError(t, err)
assert.Equal(t, true, got)
walletDir = filepath.Join(path, "direct")
require.NoError(t, os.MkdirAll(walletDir, params.BeaconIoConfig().ReadWriteExecutePermissions), "Failed to remove directory")
got, err = isEmptyWallet(path)
require.NoError(t, err)
assert.Equal(t, false, got)
require.NoError(t, os.RemoveAll(walletDir), "Failed to remove directory")
}