prysm-pulse/validator/accounts/v1/account_test.go

313 lines
13 KiB
Go
Raw Normal View History

package accounts
import (
"context"
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/pkg/errors"
"github.com/prysmaticlabs/go-bitfield"
slashpb "github.com/prysmaticlabs/prysm/proto/slashing"
"github.com/prysmaticlabs/prysm/shared/keystore"
"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"
"github.com/prysmaticlabs/prysm/validator/db/kv"
dbTest "github.com/prysmaticlabs/prysm/validator/db/testing"
"github.com/prysmaticlabs/prysm/validator/flags"
Refactor dependencies, make Prysm "go gettable" (#6053) * Fix a few deps to work with go.mod, check in generated files * Update Gossipsub to 1.1 (#5998) * update libs * add new validators * add new deps * new set of deps * tls * further fix gossip update * get everything to build * clean up * gaz * fix build * fix all tests * add deps to images * imports Co-authored-by: rauljordan <raul@prysmaticlabs.com> * Beacon chain builds with go build * fix bazel * fix dep * lint * Add github action for testing go * on PR for any branch * fix libp2p test failure * Fix TestProcessBlock_PassesProcessingConditions by updating the proposer index in test * Revert "Fix TestProcessBlock_PassesProcessingConditions by updating the proposer index in test" This reverts commit 43676894ab01f03fe90a9b8ee3ecfbc2ec1ec4e4. * Compute and set proposer index instead of hard code * Add back go mod/sum, fix deps * go build ./... * Temporarily skip two tests * Fix kafka confluent patch * Fix kafka confluent patch * fix kafka build * fix kafka * Add info in DEPENDENCIES. Added a stub link for Why Bazel? until https://github.com/prysmaticlabs/documentation/issues/138 * Update fuzz ssz files as well * Update fuzz ssz files as well * getting closer * rollback rules_go and gazelle * fix gogo protobuf * install librdkafka-dev as part of github actions * Update kafka to a recent version where librkafkfa is not required for go modules * clarify comment * fix kafka build * disable go tests * comment * Fix geth dependencies for end to end * rename word * lint * fix docker Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: rauljordan <raul@prysmaticlabs.com> Co-authored-by: terence tsao <terence@prysmaticlabs.com>
2020-05-31 06:44:34 +00:00
"github.com/urfave/cli/v2"
)
type sourceStoresHistory struct {
ProposalEpoch uint64
FirstStorePubKeyProposals bitfield.Bitlist
SecondStorePubKeyProposals bitfield.Bitlist
FirstStorePubKeyAttestations map[uint64]uint64
SecondStorePubKeyAttestations map[uint64]uint64
}
func TestNewValidatorAccount_AccountExists(t *testing.T) {
directory := testutil.TempDir() + "/testkeystore"
defer func() {
if err := os.RemoveAll(directory); err != nil {
t.Errorf("Could not remove directory: %v", err)
}
}()
validatorKey, err := keystore.NewKey()
require.NoError(t, err, "Cannot create new key")
ks := keystore.NewKeystore(directory)
err = ks.StoreKey(directory+params.BeaconConfig().ValidatorPrivkeyFileName, validatorKey, "")
require.NoError(t, err, "Unable to store key")
require.NoError(t, NewValidatorAccount(directory, "passsword123"), "Should support multiple keys")
files, err := ioutil.ReadDir(directory)
assert.NoError(t, 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) {
t.Run("custom non-existent path", func(t *testing.T) {
_, _, err := CreateValidatorAccount("foobar", "foobar")
wantErrString := fmt.Sprintf("path %q does not exist", "foobar")
assert.ErrorContains(t, wantErrString, err)
})
t.Run("empty existing dir", func(t *testing.T) {
directory := testutil.TempDir() + "/testkeystore"
defer func() {
if err := os.RemoveAll(directory); err != nil {
t.Logf("Could not remove directory: %v", err)
}
}()
// Make sure that empty existing directory doesn't trigger any errors.
require.NoError(t, os.Mkdir(directory, 0777))
_, _, err := CreateValidatorAccount(directory, "foobar")
assert.NoError(t, err)
})
t.Run("empty string as password", func(t *testing.T) {
directory := testutil.TempDir() + "/testkeystore"
defer func() {
if err := os.RemoveAll(directory); err != nil {
t.Logf("Could not remove directory: %v", err)
}
}()
require.NoError(t, os.Mkdir(directory, 0777))
_, _, err := CreateValidatorAccount(directory, "")
wantErrString := "empty passphrase is not allowed"
assert.ErrorContains(t, 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)
require.NoError(t, err)
assert.Equal(t, passedPath, path, "Expected set path to be unchanged")
assert.Equal(t, passedPassword, passphrase, "Expected set password to be unchanged")
}
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()
require.NoError(t, err, "Cannot create new key")
ks := keystore.NewKeystore(directory)
err = ks.StoreKey(directory+params.BeaconConfig().ValidatorPrivkeyFileName, validatorKey, oldPassword)
require.NoError(t, err, "Unable to store key")
require.NoError(t, ChangePassword(directory, oldPassword, newPassword))
keys, err := DecryptKeysFromKeystore(directory, params.BeaconConfig().ValidatorPrivkeyFileName, newPassword)
require.NoError(t, err)
_, ok := keys[hex.EncodeToString(validatorKey.PublicKey.Marshal())]
assert.Equal(t, true, ok, "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()
require.NoError(t, err, "Cannot create new key")
ks := keystore.NewKeystore(directory)
err = ks.StoreKey(directory+params.BeaconConfig().ValidatorPrivkeyFileName, validatorKey, "notmatching")
require.NoError(t, err, "Unable to store key")
require.NoError(t, ChangePassword(directory, oldPassword, newPassword))
keys, err := DecryptKeysFromKeystore(directory, params.BeaconConfig().ValidatorPrivkeyFileName, newPassword)
require.NoError(t, err)
_, ok := keys[hex.EncodeToString(validatorKey.PublicKey.Marshal())]
assert.Equal(t, false, ok, "Key incorrectly encrypted using the new password")
}
func TestMerge_SucceedsWhenNoDatabaseExistsInSomeSourceDirectory(t *testing.T) {
firstStorePubKey := [48]byte{1}
firstStore := dbTest.SetupDB(t, [][48]byte{firstStorePubKey})
secondStorePubKey := [48]byte{2}
secondStore := dbTest.SetupDB(t, [][48]byte{secondStorePubKey})
history, err := prepareSourcesForMerging(firstStorePubKey, firstStore.(*kv.Store), secondStorePubKey, secondStore.(*kv.Store))
require.NoError(t, err)
require.NoError(t, firstStore.Close(), "Closing source store failed")
require.NoError(t, secondStore.Close(), "Closing source store failed")
sourceDirectoryWithoutStore := testutil.TempDir() + "/nodb"
require.NoError(t, os.MkdirAll(sourceDirectoryWithoutStore, 0700), "Could not create directory")
targetDirectory := testutil.TempDir() + "/target"
t.Cleanup(func() {
assert.NoError(t, os.RemoveAll(targetDirectory), "Could not remove target directory")
})
err = Merge(
context.Background(),
[]string{firstStore.DatabasePath(), secondStore.DatabasePath(), sourceDirectoryWithoutStore}, targetDirectory)
require.NoError(t, err, "Merging failed")
mergedStore, err := kv.GetKVStore(targetDirectory)
require.NoError(t, err, "Retrieving the merged store failed")
assertMergedStore(t, mergedStore, firstStorePubKey, secondStorePubKey, history)
}
func TestMerge_FailsWhenNoDatabaseExistsInAllSourceDirectories(t *testing.T) {
sourceDirectory1 := testutil.TempDir() + "/source1"
sourceDirectory2 := testutil.TempDir() + "/source2"
targetDirectory := testutil.TempDir() + "/target"
require.NoError(t, os.MkdirAll(sourceDirectory1, 0700), "Could not create directory")
require.NoError(t, os.MkdirAll(sourceDirectory2, 0700), "Could not create directory")
require.NoError(t, os.MkdirAll(targetDirectory, 0700), "Could not create directory")
t.Cleanup(func() {
for _, dir := range []string{sourceDirectory1, sourceDirectory2, targetDirectory} {
assert.NoError(t, os.RemoveAll(dir), "Could not remove directory")
}
})
err := Merge(context.Background(), []string{sourceDirectory1, sourceDirectory2}, targetDirectory)
expected := "no validator databases found in source directories"
assert.ErrorContains(t, expected, err)
}
Split validator databases (#6048) * initial working implementation with a basic test * merge validator enhancements * added test dependency to db's build file * Merge branch 'master' into merge-validators-enhancements * changed formatting of public key * Merge branch 'master' into merge-validators-enhancements * Merge branch 'master' into merge-validators-enhancements * removed unused import * Merge branch 'merge-validators-enhancements' into split-validator-databases # Conflicts: # validator/db/BUILD.bazel # validator/db/manage.go # validator/db/manage_test.go * tests and small fixes * extracted common functionality * Merge branch 'master' into split-validator-databases # Conflicts: # validator/accounts/account_test.go # validator/db/manage.go # validator/db/manage_test.go * added missing test dependency to build file * added missing flags to main.go * applied code review suggestions * renamed flags to avoid duplication * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * removed redundant parenthesis * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * extracted defer errors to package-level variables * Merge branch 'master' into split-validator-databases * comply with error naming convention * removed incorrect import * Merge branch 'master' into split-validator-databases
2020-06-02 22:04:16 +00:00
func TestSplit(t *testing.T) {
pubKeys := [][48]byte{{1}, {2}}
sourceStore := dbTest.SetupDB(t, pubKeys)
Split validator databases (#6048) * initial working implementation with a basic test * merge validator enhancements * added test dependency to db's build file * Merge branch 'master' into merge-validators-enhancements * changed formatting of public key * Merge branch 'master' into merge-validators-enhancements * Merge branch 'master' into merge-validators-enhancements * removed unused import * Merge branch 'merge-validators-enhancements' into split-validator-databases # Conflicts: # validator/db/BUILD.bazel # validator/db/manage.go # validator/db/manage_test.go * tests and small fixes * extracted common functionality * Merge branch 'master' into split-validator-databases # Conflicts: # validator/accounts/account_test.go # validator/db/manage.go # validator/db/manage_test.go * added missing test dependency to build file * added missing flags to main.go * applied code review suggestions * renamed flags to avoid duplication * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * removed redundant parenthesis * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * extracted defer errors to package-level variables * Merge branch 'master' into split-validator-databases * comply with error naming convention * removed incorrect import * Merge branch 'master' into split-validator-databases
2020-06-02 22:04:16 +00:00
proposalEpoch := uint64(0)
proposalHistory1 := bitfield.Bitlist{0x01, 0x00, 0x00, 0x00, 0x01}
err := sourceStore.SaveProposalHistoryForEpoch(context.Background(), pubKeys[0][:], proposalEpoch, proposalHistory1)
require.NoError(t, err, "Saving proposal history failed")
Split validator databases (#6048) * initial working implementation with a basic test * merge validator enhancements * added test dependency to db's build file * Merge branch 'master' into merge-validators-enhancements * changed formatting of public key * Merge branch 'master' into merge-validators-enhancements * Merge branch 'master' into merge-validators-enhancements * removed unused import * Merge branch 'merge-validators-enhancements' into split-validator-databases # Conflicts: # validator/db/BUILD.bazel # validator/db/manage.go # validator/db/manage_test.go * tests and small fixes * extracted common functionality * Merge branch 'master' into split-validator-databases # Conflicts: # validator/accounts/account_test.go # validator/db/manage.go # validator/db/manage_test.go * added missing test dependency to build file * added missing flags to main.go * applied code review suggestions * renamed flags to avoid duplication * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * removed redundant parenthesis * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * extracted defer errors to package-level variables * Merge branch 'master' into split-validator-databases * comply with error naming convention * removed incorrect import * Merge branch 'master' into split-validator-databases
2020-06-02 22:04:16 +00:00
proposalHistory2 := bitfield.Bitlist{0x02, 0x00, 0x00, 0x00, 0x01}
err = sourceStore.SaveProposalHistoryForEpoch(context.Background(), pubKeys[1][:], proposalEpoch, proposalHistory2)
require.NoError(t, err, "Saving proposal history failed")
Split validator databases (#6048) * initial working implementation with a basic test * merge validator enhancements * added test dependency to db's build file * Merge branch 'master' into merge-validators-enhancements * changed formatting of public key * Merge branch 'master' into merge-validators-enhancements * Merge branch 'master' into merge-validators-enhancements * removed unused import * Merge branch 'merge-validators-enhancements' into split-validator-databases # Conflicts: # validator/db/BUILD.bazel # validator/db/manage.go # validator/db/manage_test.go * tests and small fixes * extracted common functionality * Merge branch 'master' into split-validator-databases # Conflicts: # validator/accounts/account_test.go # validator/db/manage.go # validator/db/manage_test.go * added missing test dependency to build file * added missing flags to main.go * applied code review suggestions * renamed flags to avoid duplication * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * removed redundant parenthesis * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * extracted defer errors to package-level variables * Merge branch 'master' into split-validator-databases * comply with error naming convention * removed incorrect import * Merge branch 'master' into split-validator-databases
2020-06-02 22:04:16 +00:00
attestationHistoryMap1 := make(map[uint64]uint64)
attestationHistoryMap1[0] = 0
pubKeyAttestationHistory1 := &slashpb.AttestationHistory{
TargetToSource: attestationHistoryMap1,
LatestEpochWritten: 0,
}
attestationHistoryMap2 := make(map[uint64]uint64)
attestationHistoryMap2[0] = 1
pubKeyAttestationHistory2 := &slashpb.AttestationHistory{
TargetToSource: attestationHistoryMap2,
LatestEpochWritten: 0,
}
dbAttestationHistory := make(map[[48]byte]*slashpb.AttestationHistory)
dbAttestationHistory[pubKeys[0]] = pubKeyAttestationHistory1
dbAttestationHistory[pubKeys[1]] = pubKeyAttestationHistory2
err = sourceStore.SaveAttestationHistoryForPubKeys(context.Background(), dbAttestationHistory)
require.NoError(t, err, "Saving attestation history failed %v")
Split validator databases (#6048) * initial working implementation with a basic test * merge validator enhancements * added test dependency to db's build file * Merge branch 'master' into merge-validators-enhancements * changed formatting of public key * Merge branch 'master' into merge-validators-enhancements * Merge branch 'master' into merge-validators-enhancements * removed unused import * Merge branch 'merge-validators-enhancements' into split-validator-databases # Conflicts: # validator/db/BUILD.bazel # validator/db/manage.go # validator/db/manage_test.go * tests and small fixes * extracted common functionality * Merge branch 'master' into split-validator-databases # Conflicts: # validator/accounts/account_test.go # validator/db/manage.go # validator/db/manage_test.go * added missing test dependency to build file * added missing flags to main.go * applied code review suggestions * renamed flags to avoid duplication * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * removed redundant parenthesis * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * extracted defer errors to package-level variables * Merge branch 'master' into split-validator-databases * comply with error naming convention * removed incorrect import * Merge branch 'master' into split-validator-databases
2020-06-02 22:04:16 +00:00
require.NoError(t, sourceStore.Close(), "Closing source store failed")
Split validator databases (#6048) * initial working implementation with a basic test * merge validator enhancements * added test dependency to db's build file * Merge branch 'master' into merge-validators-enhancements * changed formatting of public key * Merge branch 'master' into merge-validators-enhancements * Merge branch 'master' into merge-validators-enhancements * removed unused import * Merge branch 'merge-validators-enhancements' into split-validator-databases # Conflicts: # validator/db/BUILD.bazel # validator/db/manage.go # validator/db/manage_test.go * tests and small fixes * extracted common functionality * Merge branch 'master' into split-validator-databases # Conflicts: # validator/accounts/account_test.go # validator/db/manage.go # validator/db/manage_test.go * added missing test dependency to build file * added missing flags to main.go * applied code review suggestions * renamed flags to avoid duplication * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * removed redundant parenthesis * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * extracted defer errors to package-level variables * Merge branch 'master' into split-validator-databases * comply with error naming convention * removed incorrect import * Merge branch 'master' into split-validator-databases
2020-06-02 22:04:16 +00:00
targetDirectory := testutil.TempDir() + "/target"
t.Cleanup(func() {
assert.NoError(t, os.RemoveAll(targetDirectory), "Could not remove target directory")
Split validator databases (#6048) * initial working implementation with a basic test * merge validator enhancements * added test dependency to db's build file * Merge branch 'master' into merge-validators-enhancements * changed formatting of public key * Merge branch 'master' into merge-validators-enhancements * Merge branch 'master' into merge-validators-enhancements * removed unused import * Merge branch 'merge-validators-enhancements' into split-validator-databases # Conflicts: # validator/db/BUILD.bazel # validator/db/manage.go # validator/db/manage_test.go * tests and small fixes * extracted common functionality * Merge branch 'master' into split-validator-databases # Conflicts: # validator/accounts/account_test.go # validator/db/manage.go # validator/db/manage_test.go * added missing test dependency to build file * added missing flags to main.go * applied code review suggestions * renamed flags to avoid duplication * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * removed redundant parenthesis * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * extracted defer errors to package-level variables * Merge branch 'master' into split-validator-databases * comply with error naming convention * removed incorrect import * Merge branch 'master' into split-validator-databases
2020-06-02 22:04:16 +00:00
})
require.NoError(t, Split(context.Background(), sourceStore.DatabasePath(), targetDirectory), "Splitting failed")
Split validator databases (#6048) * initial working implementation with a basic test * merge validator enhancements * added test dependency to db's build file * Merge branch 'master' into merge-validators-enhancements * changed formatting of public key * Merge branch 'master' into merge-validators-enhancements * Merge branch 'master' into merge-validators-enhancements * removed unused import * Merge branch 'merge-validators-enhancements' into split-validator-databases # Conflicts: # validator/db/BUILD.bazel # validator/db/manage.go # validator/db/manage_test.go * tests and small fixes * extracted common functionality * Merge branch 'master' into split-validator-databases # Conflicts: # validator/accounts/account_test.go # validator/db/manage.go # validator/db/manage_test.go * added missing test dependency to build file * added missing flags to main.go * applied code review suggestions * renamed flags to avoid duplication * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * removed redundant parenthesis * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * Merge branch 'master' into split-validator-databases * extracted defer errors to package-level variables * Merge branch 'master' into split-validator-databases * comply with error naming convention * removed incorrect import * Merge branch 'master' into split-validator-databases
2020-06-02 22:04:16 +00:00
}
func prepareSourcesForMerging(firstStorePubKey [48]byte, firstStore *kv.Store, secondStorePubKey [48]byte, secondStore *kv.Store) (*sourceStoresHistory, error) {
proposalEpoch := uint64(0)
proposalHistory1 := bitfield.Bitlist{0x01, 0x00, 0x00, 0x00, 0x01}
if err := firstStore.SaveProposalHistoryForEpoch(context.Background(), firstStorePubKey[:], proposalEpoch, proposalHistory1); err != nil {
return nil, errors.Wrapf(err, "Saving proposal history failed")
}
proposalHistory2 := bitfield.Bitlist{0x02, 0x00, 0x00, 0x00, 0x01}
if err := secondStore.SaveProposalHistoryForEpoch(context.Background(), secondStorePubKey[:], proposalEpoch, proposalHistory2); err != nil {
return nil, errors.Wrapf(err, "Saving proposal history failed")
}
attestationHistoryMap1 := make(map[uint64]uint64)
attestationHistoryMap1[0] = 0
pubKeyAttestationHistory1 := &slashpb.AttestationHistory{
TargetToSource: attestationHistoryMap1,
LatestEpochWritten: 0,
}
dbAttestationHistory1 := make(map[[48]byte]*slashpb.AttestationHistory)
dbAttestationHistory1[firstStorePubKey] = pubKeyAttestationHistory1
if err := firstStore.SaveAttestationHistoryForPubKeys(context.Background(), dbAttestationHistory1); err != nil {
return nil, errors.Wrapf(err, "Saving attestation history failed")
}
attestationHistoryMap2 := make(map[uint64]uint64)
attestationHistoryMap2[0] = 1
pubKeyAttestationHistory2 := &slashpb.AttestationHistory{
TargetToSource: attestationHistoryMap2,
LatestEpochWritten: 0,
}
dbAttestationHistory2 := make(map[[48]byte]*slashpb.AttestationHistory)
dbAttestationHistory2[secondStorePubKey] = pubKeyAttestationHistory2
if err := secondStore.SaveAttestationHistoryForPubKeys(context.Background(), dbAttestationHistory2); err != nil {
return nil, errors.Wrapf(err, "Saving attestation history failed")
}
mergeHistory := &sourceStoresHistory{
ProposalEpoch: proposalEpoch,
FirstStorePubKeyProposals: proposalHistory1,
SecondStorePubKeyProposals: proposalHistory2,
FirstStorePubKeyAttestations: attestationHistoryMap1,
SecondStorePubKeyAttestations: attestationHistoryMap2,
}
return mergeHistory, nil
}
func assertMergedStore(
t *testing.T,
mergedStore *kv.Store,
firstStorePubKey [48]byte,
secondStorePubKey [48]byte,
history *sourceStoresHistory) {
mergedProposalHistory1, err := mergedStore.ProposalHistoryForEpoch(
context.Background(), firstStorePubKey[:], history.ProposalEpoch)
require.NoError(t, err, "Retrieving merged proposal history failed for public key %v", firstStorePubKey)
require.DeepEqual(t, history.FirstStorePubKeyProposals, mergedProposalHistory1, "Proposals not merged correctly")
mergedProposalHistory2, err := mergedStore.ProposalHistoryForEpoch(
context.Background(), secondStorePubKey[:], history.ProposalEpoch)
require.NoError(t, err, "Retrieving merged proposal history failed for public key %v", secondStorePubKey)
require.DeepEqual(t, history.SecondStorePubKeyProposals, mergedProposalHistory2, "Proposals not merged correctly")
mergedAttestationHistory, err := mergedStore.AttestationHistoryForPubKeys(
context.Background(),
[][48]byte{firstStorePubKey, secondStorePubKey})
require.NoError(t, err, "Retrieving merged attestation history failed")
assert.Equal(t, history.FirstStorePubKeyAttestations[0], mergedAttestationHistory[firstStorePubKey].TargetToSource[0])
assert.Equal(t, history.SecondStorePubKeyAttestations[0], mergedAttestationHistory[secondStorePubKey].TargetToSource[0])
}