Improve validator import logs (#12429)

* adding small ux improvement

* gaz

* rolling back dir test changes

* Update validator/accounts/accounts_import.go

* adding review suggestion

* missed else part of statement

---------

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
This commit is contained in:
james-prysm
2023-05-23 15:41:41 -05:00
committed by GitHub
parent cfa64ae013
commit 666188dfea
3 changed files with 32 additions and 12 deletions

View File

@@ -71,6 +71,7 @@ go_test(
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_google_uuid//:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@com_github_wealdtech_go_eth2_wallet_encryptor_keystorev4//:go_default_library",
"@org_golang_google_protobuf//types/known/timestamppb:go_default_library",

View File

@@ -2,6 +2,7 @@ package accounts
import (
"flag"
"fmt"
"os"
"path/filepath"
"testing"
@@ -9,15 +10,18 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/prysmaticlabs/prysm/v4/cmd/validator/flags"
"github.com/prysmaticlabs/prysm/v4/testing/assert"
"github.com/prysmaticlabs/prysm/v4/testing/require"
"github.com/prysmaticlabs/prysm/v4/validator/accounts"
"github.com/prysmaticlabs/prysm/v4/validator/keymanager"
"github.com/prysmaticlabs/prysm/v4/validator/keymanager/local"
"github.com/prysmaticlabs/prysm/v4/validator/node"
"github.com/sirupsen/logrus/hooks/test"
"github.com/urfave/cli/v2"
)
func TestWalletWithKeymanager(t *testing.T) {
logHook := test.NewGlobal()
walletDir, passwordsDir, passwordFilePath := setupWalletAndPasswordsDir(t)
keysDir := filepath.Join(t.TempDir(), "keysDir")
require.NoError(t, os.MkdirAll(keysDir, os.ModePerm))
@@ -66,6 +70,9 @@ func TestWalletWithKeymanager(t *testing.T) {
require.NoError(t, err)
require.Equal(t, len(keys), 2)
require.Equal(t, w.KeymanagerKind(), keymanager.Local)
hexKeys := []string{hexutil.Encode(keys[0][:])[2:], hexutil.Encode(keys[1][:])[2:]} // imported keystores don't include the 0x in name
assert.LogsContain(t, logHook, fmt.Sprintf("Imported accounts %v,", hexKeys))
}
func TestWalletWithKeymanager_web3signer(t *testing.T) {

View File

@@ -154,18 +154,26 @@ func (acm *AccountsCLIManager) Import(ctx context.Context) error {
if err != nil {
return err
}
var successfullyImportedAccounts []string
for i, status := range statuses {
switch status.Status {
case ethpbservice.ImportedKeystoreStatus_IMPORTED:
successfullyImportedAccounts = append(successfullyImportedAccounts, keystoresImported[i].Pubkey)
case ethpbservice.ImportedKeystoreStatus_DUPLICATE:
log.Warnf("Duplicate key %s found in import request, skipped", keystoresImported[i].Pubkey)
case ethpbservice.ImportedKeystoreStatus_ERROR:
log.Warnf("Could not import keystore for %s: %s", keystoresImported[i].Pubkey, status.Message)
}
}
fmt.Printf(
"Successfully imported %s accounts, view all of them by running `accounts list`\n",
au.BrightMagenta(strconv.Itoa(len(keystoresImported))),
)
if len(successfullyImportedAccounts) == 0 {
log.Error("no accounts were successfully imported")
} else {
log.Infof(
"Imported accounts %v, view all of them by running `accounts list`",
successfullyImportedAccounts,
)
}
return nil
}
@@ -240,16 +248,20 @@ func importPrivateKeyAsAccount(ctx context.Context, wallet *wallet.Wallet, impor
return errors.Wrap(err, "could not import keystore into wallet")
}
for _, status := range statuses {
if status.Status == ethpbservice.ImportedKeystoreStatus_ERROR {
log.Warnf("Could not import keystore for %s: %s", keystore.Pubkey, status.Message)
} else if status.Status == ethpbservice.ImportedKeystoreStatus_DUPLICATE {
log.Warnf("Duplicate key %s skipped", keystore.Pubkey)
switch status.Status {
case ethpbservice.ImportedKeystoreStatus_IMPORTED:
fmt.Printf(
"Imported account with public key %#x, view all accounts by running `accounts list`\n",
au.BrightMagenta(bytesutil.Trunc(privKey.PublicKey().Marshal())),
)
return nil
case ethpbservice.ImportedKeystoreStatus_ERROR:
return fmt.Errorf("Could not import keystore for %s: %s", keystore.Pubkey, status.Message)
case ethpbservice.ImportedKeystoreStatus_DUPLICATE:
return fmt.Errorf("Duplicate key %s skipped", keystore.Pubkey)
}
}
fmt.Printf(
"Imported account with public key %#x, view all accounts by running `accounts list`\n",
au.BrightMagenta(bytesutil.Trunc(privKey.PublicKey().Marshal())),
)
return nil
}