From 71fa70ce40a1f28d40c65bc311316649668808eb Mon Sep 17 00:00:00 2001 From: james-prysm <90280386+james-prysm@users.noreply.github.com> Date: Wed, 18 Oct 2023 09:00:25 -0500 Subject: [PATCH] CLEANUP: validator exit prompt (#13057) * removing check special phrase in url from validator exit * fixing missed tests * fixing unit test and addressing preston/sammy's comments * fixing test * sammy's comments * deepsource recommendation * Update validator/accounts/accounts_helper.go Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com> * sammy's review --------- Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- cmd/validator/accounts/exit_test.go | 4 ++-- io/prompt/validate.go | 12 +++++++----- io/prompt/validate_test.go | 6 ++---- validator/accounts/accounts_exit.go | 7 ++----- validator/accounts/accounts_exit_test.go | 3 +-- validator/accounts/accounts_helper.go | 15 +++++---------- 6 files changed, 19 insertions(+), 28 deletions(-) diff --git a/cmd/validator/accounts/exit_test.go b/cmd/validator/accounts/exit_test.go index c0b04e0cd..214d522aa 100644 --- a/cmd/validator/accounts/exit_test.go +++ b/cmd/validator/accounts/exit_test.go @@ -90,7 +90,7 @@ func TestExitAccountsCli_OK(t *testing.T) { // Prepare user input for final confirmation step var stdin bytes.Buffer - stdin.Write([]byte(accounts.ExitPassphrase)) + stdin.Write([]byte("Y")) rawPubKeys, formattedPubKeys, err := accounts.FilterExitAccountsFromUserInput( cliCtx, &stdin, validatingPublicKeys, false, ) @@ -190,7 +190,7 @@ func TestExitAccountsCli_OK_AllPublicKeys(t *testing.T) { // Prepare user input for final confirmation step var stdin bytes.Buffer - stdin.Write([]byte(accounts.ExitPassphrase)) + stdin.Write([]byte("Y")) rawPubKeys, formattedPubKeys, err := accounts.FilterExitAccountsFromUserInput( cliCtx, &stdin, validatingPublicKeys, false, ) diff --git a/io/prompt/validate.go b/io/prompt/validate.go index 41e2a917b..9bc022d78 100644 --- a/io/prompt/validate.go +++ b/io/prompt/validate.go @@ -79,10 +79,12 @@ func ValidatePasswordInput(input string) error { return nil } -// ValidatePhrase checks whether the user input is equal to the wanted phrase. The verification is case sensitive. -func ValidatePhrase(input, wantedPhrase string) error { - if strings.TrimSpace(input) != wantedPhrase { - return errIncorrectPhrase +// ValidatePhrase checks whether the user input is equal to the wanted phrase(s). +func ValidatePhrase(input string, wantedPhrases ...string) error { + for _, wantedPhrase := range wantedPhrases { + if strings.EqualFold(strings.TrimSpace(input), wantedPhrase) { + return nil + } } - return nil + return errIncorrectPhrase } diff --git a/io/prompt/validate_test.go b/io/prompt/validate_test.go index 090a9d8c3..3bcfa7557 100644 --- a/io/prompt/validate_test.go +++ b/io/prompt/validate_test.go @@ -153,9 +153,7 @@ func TestValidatePhrase(t *testing.T) { assert.NotNil(t, err) assert.ErrorContains(t, errIncorrectPhrase.Error(), err) }) - t.Run("wrong letter case", func(t *testing.T) { - err := ValidatePhrase("Wanted Phrase", wantedPhrase) - assert.NotNil(t, err) - assert.ErrorContains(t, errIncorrectPhrase.Error(), err) + t.Run("any letter case", func(t *testing.T) { + assert.NoError(t, ValidatePhrase("Wanted Phrase", wantedPhrase)) }) } diff --git a/validator/accounts/accounts_exit.go b/validator/accounts/accounts_exit.go index 0bacf2b9d..1bd772963 100644 --- a/validator/accounts/accounts_exit.go +++ b/validator/accounts/accounts_exit.go @@ -33,9 +33,6 @@ type PerformExitCfg struct { OutputDirectory string } -// ExitPassphrase exported for use in test. -const ExitPassphrase = "Exit my validator" - // Exit performs a voluntary exit on one or more accounts. func (acm *AccountsCLIManager) Exit(ctx context.Context) error { // User decided to cancel the voluntary exit. @@ -106,7 +103,7 @@ func PerformVoluntaryExit( } else { log.WithError(err).Errorf("voluntary exit failed for account %s", cfg.FormattedPubKeys[i]) } - } else if err := writeSignedVoluntaryExitJSON(ctx, sve, cfg.OutputDirectory); err != nil { + } else if err := writeSignedVoluntaryExitJSON(sve, cfg.OutputDirectory); err != nil { log.WithError(err).Error("failed to write voluntary exit") } } else if err := client.ProposeExit(ctx, cfg.ValidatorClient, cfg.Keymanager.Sign, key, epoch); err != nil { @@ -180,7 +177,7 @@ func displayExitInfo(rawExitedKeys [][]byte, trimmedExitedKeys []string) { } } -func writeSignedVoluntaryExitJSON(ctx context.Context, sve *eth.SignedVoluntaryExit, outputDirectory string) error { +func writeSignedVoluntaryExitJSON(sve *eth.SignedVoluntaryExit, outputDirectory string) error { if err := file.MkdirAll(outputDirectory); err != nil { return err } diff --git a/validator/accounts/accounts_exit_test.go b/validator/accounts/accounts_exit_test.go index 632cf0525..34f8cb4f6 100644 --- a/validator/accounts/accounts_exit_test.go +++ b/validator/accounts/accounts_exit_test.go @@ -1,7 +1,6 @@ package accounts import ( - "context" "encoding/json" "fmt" "path" @@ -53,7 +52,7 @@ func TestWriteSignedVoluntaryExitJSON(t *testing.T) { } output := path.Join(bazel.TestTmpDir(), "TestWriteSignedVoluntaryExitJSON") - require.NoError(t, writeSignedVoluntaryExitJSON(context.Background(), sve, output)) + require.NoError(t, writeSignedVoluntaryExitJSON(sve, output)) b, err := file.ReadFileAsBytes(path.Join(output, "validator-exit-300.json")) require.NoError(t, err) diff --git a/validator/accounts/accounts_helper.go b/validator/accounts/accounts_helper.go index 3f7395a0a..703193372 100644 --- a/validator/accounts/accounts_helper.go +++ b/validator/accounts/accounts_helper.go @@ -202,21 +202,16 @@ func FilterExitAccountsFromUserInput( return rawPubKeys, formattedPubKeys, nil } - promptHeader := au.Red("===============IMPORTANT===============") - promptDescription := "Please navigate to the following website and make sure you understand the current implications " + - "of a voluntary exit before making the final decision:" - promptURL := au.Blue("https://docs.prylabs.network/docs/wallet/exiting-a-validator") - promptQuestion := "If you still want to continue with the voluntary exit, please input a phrase found at the above URL" - promptText := fmt.Sprintf("%s\n%s\n%s\n%s", promptHeader, promptDescription, promptURL, promptQuestion) - resp, err := prompt.ValidatePrompt(r, promptText, func(input string) error { - return prompt.ValidatePhrase(input, ExitPassphrase) - }) + promptHeader := au.Red("===============CONFIRMATION NEEDED===============") + promptQuestion := "continue with the voluntary exit? (y/n)" + promptText := fmt.Sprintf("%s\n%s", promptHeader, promptQuestion) + resp, err := prompt.ValidatePrompt(r, promptText, prompt.ValidateYesOrNo) if err != nil { return nil, nil, err } if strings.EqualFold(resp, "n") { + log.Info("Voluntary exit aborted") return nil, nil, nil } - return rawPubKeys, formattedPubKeys, nil }