Report on duplicate validator keys during imports (#7459)

* Fix #7393

* fix go fmt

* Add a test

* Convert to map of strings

* @terencechain fixes

* Update validator/keymanager/v2/direct/import.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
This commit is contained in:
Potuz 2020-10-09 22:45:26 -03:00 committed by GitHub
parent d98a6dda8f
commit c4e64afd07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View File

@ -39,6 +39,14 @@ func (dr *Keymanager) ImportKeystores(
return errors.Wrap(err, "could not add to progress bar")
}
}
foundKey := map[string]bool{}
for i := range pubKeys {
strKey := string(pubKeys[i])
if foundKey[strKey] {
return fmt.Errorf("duplicated key found: %#x", pubKeys[i])
}
foundKey[strKey] = true
}
// Write the accounts to disk into a single keystore.
accountsKeystore, err := dr.createAccountsKeystore(ctx, privKeys, pubKeys)
if err != nil {

View File

@ -100,18 +100,25 @@ func TestDirectKeymanager_ImportKeystores(t *testing.T) {
accountsStore: &AccountStore{},
}
// Create several keystores and attempt to import them.
// Create a duplicate keystore and attempt to import it.
numAccounts := 5
keystores := make([]*v2keymanager.Keystore, numAccounts)
for i := 0; i < numAccounts; i++ {
keystores := make([]*v2keymanager.Keystore, numAccounts+1)
for i := 1; i < numAccounts+1; i++ {
keystores[i] = createRandomKeystore(t, password)
}
keystores[0] = keystores[1]
ctx := context.Background()
require.NoError(t, dr.ImportKeystores(
require.ErrorContains(t, "duplicated key found:", dr.ImportKeystores(
ctx,
keystores,
password,
))
// Import them correctly without the duplicate.
require.NoError(t, dr.ImportKeystores(
ctx,
keystores[1:],
password,
))
// Ensure the single, all-encompassing accounts keystore was written
// to the wallet and ensure we can decrypt it using the EIP-2335 standard.