prysm-pulse/validator/accounts/account_test.go

144 lines
4.2 KiB
Go
Raw Normal View History

package accounts
import (
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/prysmaticlabs/prysm/shared/keystore"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/validator/flags"
"gopkg.in/urfave/cli.v2"
)
func TestNewValidatorAccount_AccountExists(t *testing.T) {
directory := testutil.TempDir() + "/testkeystore"
defer func() {
if err := os.RemoveAll(directory); err != nil {
t.Logf("Could not remove directory: %v", err)
}
}()
validatorKey, err := keystore.NewKey()
if err != nil {
t.Fatalf("Cannot create new key: %v", err)
}
ks := keystore.NewKeystore(directory)
if err := ks.StoreKey(directory+params.BeaconConfig().ValidatorPrivkeyFileName, validatorKey, ""); err != nil {
t.Fatalf("Unable to store key %v", err)
}
if err := NewValidatorAccount(directory, ""); err != nil {
t.Errorf("Should support multiple keys: %v", err)
}
files, err := ioutil.ReadDir(directory)
if err != nil {
t.Error(err)
}
if len(files) != 3 {
Validator-multiple key (#2069) * first version - broken * working proto changes * resolve review remarks * fix goimport issues * fix service issues * first logic version-broken * first running version - no new tests * fix validator client test * add wait group to goroutines * remove unused var in function call * fix review remarks and tests * merge master changes and fix conflicts * gazzele fix * fix prestonvanloon requested changes * merge and some of terenc3t remarks addressed * _,pk bug fix in log * fix account file name suffix and filter not active validator out * merge with master and fix missing parameters * run over all public keys in hasvalidators * add test for error when no all the validators has index in the db and hasvalidators is called * fix runner tests fail due to timing issues * goimports * smaller sleep time in proposer tests * fix UpdateAssignments loging * fix goimports * added && false commented TestUpdateAssignments_DoesNothingWhenNotEpochStartAndAlreadyExistingAssignments * hasvalidators without missing publickeys list * fix some of prestone review remarks * fixes for prestone comments * review changes applied * expect context call in TestWaitForActivation_ValidatorOriginallyExists * changed hasvalidators to return true if one validator exists * fix init problem to getkeys * hasvalidators requiers all validators to be in db * validator attest assignments update * fix ap var name * Change name to hasallvalidators * fix tests * update script, fix any vs all validator calls * fix wait for activation * filter validator * reuse the reply block * fix imports * Remove dup * better lookup of active validators * better filter active vlaidators, still need to fix committee assignment tests * lint * use activated keys * fix for postchainstart * fix logging * move state transitions * hasanyvalidator and hasallvalidators * fix tests with updatechainhead missing * add tests * fix TestCommitteeAssignment_OK * fix test * fix validator tests * fix TestCommitteeAssignment_multipleKeys_OK and TestWaitForActivation_ValidatorOriginallyExists * fix goimports * removed unused param from assignment * change string(pk) to hex.EncodeString(pk) fix change requests * add inactive validator status to assignments * fix logging mess due to multi validator setup * set no assignment to debug level * log assignments every epoch * logging fixes * fixed runtime by using the right assignments * correct activation request * fix the validator panic * correct assignment * fix test fail and waitforactivation * performance log issue fix * fix goimports * add log message with truncated pk for attest * add truncated pk to attest and propose logs * Add comment to script, change 9 to 8 * Update assignment log * Add comment, report number of assignments * Use WithError, add validator as field, merge block proposal log * Update validator_propose.go * fix * use entry.String() * fix fmt
2019-04-18 17:23:38 +00:00
t.Errorf("multiple validators were not created only %v files in directory", len(files))
for _, f := range files {
t.Errorf("%v\n", f.Name())
}
}
}
func TestNewValidatorAccount_CreateValidatorAccount(t *testing.T) {
directory := "foobar"
_, _, err := CreateValidatorAccount(directory, "foobar")
wantErrString := fmt.Sprintf("path %q does not exist", directory)
if err == nil || err.Error() != wantErrString {
t.Errorf("expected error not thrown, want: %v, got: %v", wantErrString, err)
}
}
func TestHandleEmptyFlags_FlagsSet(t *testing.T) {
passedPath := "~/path/given"
passedPassword := "password"
app := &cli.App{}
set := flag.NewFlagSet("test", 0)
set.String(flags.KeystorePathFlag.Name, passedPath, "set keystore path")
set.String(flags.PasswordFlag.Name, passedPassword, "set keystore password")
ctx := cli.NewContext(app, set, nil)
path, passphrase, err := HandleEmptyKeystoreFlags(ctx, false)
if err != nil {
t.Fatal(err)
}
if passedPath != path {
t.Fatalf("Expected set path to be unchanged, expected %s, received %s", passedPath, path)
}
if passedPassword != passphrase {
t.Fatalf("Expected set password to be unchanged, expected %s, received %s", passedPassword, passphrase)
}
}
func TestChangePassword_KeyEncryptedWithNewPassword(t *testing.T) {
directory := testutil.TempDir() + "/testkeystore"
defer func() {
if err := os.RemoveAll(directory); err != nil {
t.Logf("Could not remove directory: %v", err)
}
}()
oldPassword := "old"
newPassword := "new"
validatorKey, err := keystore.NewKey()
if err != nil {
t.Fatalf("Cannot create new key: %v", err)
}
ks := keystore.NewKeystore(directory)
if err := ks.StoreKey(directory+params.BeaconConfig().ValidatorPrivkeyFileName, validatorKey, oldPassword); err != nil {
t.Fatalf("Unable to store key %v", err)
}
if err := ChangePassword(directory, oldPassword, newPassword); err != nil {
t.Fatal(err)
}
keys, err := DecryptKeysFromKeystore(directory, params.BeaconConfig().ValidatorPrivkeyFileName, newPassword)
if err != nil {
t.Fatal(err)
}
if _, ok := keys[hex.EncodeToString(validatorKey.PublicKey.Marshal())]; !ok {
t.Error("Key not encrypted using the new password")
}
}
func TestChangePassword_KeyNotMatchingOldPasswordNotEncryptedWithNewPassword(t *testing.T) {
directory := testutil.TempDir() + "/testkeystore"
defer func() {
if err := os.RemoveAll(directory); err != nil {
t.Logf("Could not remove directory: %v", err)
}
}()
oldPassword := "old"
newPassword := "new"
validatorKey, err := keystore.NewKey()
if err != nil {
t.Fatalf("Cannot create new key: %v", err)
}
ks := keystore.NewKeystore(directory)
if err := ks.StoreKey(directory+params.BeaconConfig().ValidatorPrivkeyFileName, validatorKey, "notmatching"); err != nil {
t.Fatalf("Unable to store key %v", err)
}
if err := ChangePassword(directory, oldPassword, newPassword); err != nil {
t.Fatal(err)
}
keys, err := DecryptKeysFromKeystore(directory, params.BeaconConfig().ValidatorPrivkeyFileName, newPassword)
if err != nil {
t.Fatal(err)
}
if _, ok := keys[hex.EncodeToString(validatorKey.PublicKey.Marshal())]; ok {
t.Error("Key incorrectly encrypted using the new password")
}
}