Skip to content

Commit

Permalink
Merge validator databases (#5968)
Browse files Browse the repository at this point in the history
* merge functionality

* merge testing

* log info when merge completed successfully

* extracted private methods

* fixed linter errors

* fixed compilation errors

* build files clean-up

* fixed failing test

* corrected command description

* close sources inside defer

* corrected documentation of NewKVStore
  • Loading branch information
rkapka committed May 25, 2020
1 parent b810243 commit 277fa33
Show file tree
Hide file tree
Showing 16 changed files with 538 additions and 15 deletions.
5 changes: 5 additions & 0 deletions validator/accounts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
"//shared/cmd:go_default_library",
"//shared/keystore:go_default_library",
"//shared/params:go_default_library",
"//validator/db:go_default_library",
"//validator/flags:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
Expand All @@ -35,13 +36,17 @@ go_test(
],
embed = [":go_default_library"],
deps = [
"//proto/slashing:go_default_library",
"//shared/keystore:go_default_library",
"//shared/mock:go_default_library",
"//shared/params:go_default_library",
"//shared/testutil:go_default_library",
"//validator/db:go_default_library",
"//validator/flags:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
"@in_gopkg_urfave_cli_v2//:go_default_library",
],
)
39 changes: 38 additions & 1 deletion validator/accounts/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accounts

import (
"bufio"
"context"
"encoding/hex"
"fmt"
"io"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/keystore"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/validator/db"
"github.com/prysmaticlabs/prysm/validator/flags"
"github.com/sirupsen/logrus"
"gopkg.in/urfave/cli.v2"
Expand Down Expand Up @@ -118,7 +120,7 @@ func NewValidatorAccount(directory string, password string) error {
}

// Exists checks if a validator account at a given keystore path exists.
// assertNonEmpty is a boolean used to determine whether to check that
// assertNonEmpty is a boolean used to determine whether to check that
// the provided directory exists.
func Exists(keystorePath string, assertNonEmpty bool) (bool, error) {
/* #nosec */
Expand Down Expand Up @@ -219,6 +221,41 @@ func HandleEmptyKeystoreFlags(cliCtx *cli.Context, confirmPassword bool) (string
return path, passphrase, nil
}

// Merge merges data from validator databases in sourceDirectories into a new store, which is created in targetDirectory.
func Merge(ctx context.Context, sourceDirectories []string, targetDirectory string) error {
var sourceStores []*db.Store

for _, dir := range sourceDirectories {
store, err := db.GetKVStore(dir)
if err != nil {
return errors.Wrapf(err, "Failed to prepare the database in %s for merging", dir)
}
if store == nil {
continue
}
sourceStores = append(sourceStores, store)
}

if len(sourceStores) == 0 {
return errors.New("no validator databases found in source directories")
}

err := db.Merge(ctx, sourceStores, targetDirectory)
if err != nil {
return errors.Wrapf(err, "Failed to merge validator databases into %s", targetDirectory)
}

defer func() {
for _, store := range sourceStores {
if err := store.Close(); err != nil {
err = errors.Wrapf(err, "Failed to close the database in %s", store.DatabasePath())
}
}
}()

return nil
}

// ChangePassword changes the password for all keys located in a keystore.
// Password is changed only for keys that can be decrypted using the old password.
func ChangePassword(keystorePath string, oldPassword string, newPassword string) error {
Expand Down

0 comments on commit 277fa33

Please sign in to comment.