Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix --clear-db on Windows #7474

Merged
merged 9 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions beacon-chain/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ go_test(
embed = [":go_default_library"],
deps = [
"//beacon-chain/core/feed/state:go_default_library",
"//shared/cmd:go_default_library",
"//shared/testutil:go_default_library",
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",
Expand Down
3 changes: 3 additions & 0 deletions beacon-chain/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ func (b *BeaconNode) startDB(cliCtx *cli.Context) error {
}
if clearDBConfirmed || forceClearDB {
log.Warning("Removing database")
if err := d.Close(); err != nil {
return errors.Wrap(err, "could not close db prior to clearing")
}
if err := d.ClearDB(); err != nil {
return errors.Wrap(err, "could not clear database")
}
Expand Down
26 changes: 26 additions & 0 deletions beacon-chain/node/node_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package node

import (
"crypto/rand"
"flag"
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"testing"

statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
Expand Down Expand Up @@ -65,3 +69,25 @@ func TestBootStrapNodeFile(t *testing.T) {
assert.Equal(t, sampleNode1[2:], nodeList[1], "Unexpected nodes")
assert.Equal(t, sampleNode2[2:], nodeList[2], "Unexpected nodes")
}

// TestClearDB tests clearing the database
func TestClearDB(t *testing.T) {
hook := logTest.NewGlobal()

randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random number for file path")
tmp := filepath.Join(testutil.TempDir(), fmt.Sprintf("datadirtest%d", randPath))
require.NoError(t, os.RemoveAll(tmp))

app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.String("datadir", tmp, "node data directory")
set.Bool(cmd.ForceClearDB.Name, true, "force clear db")

context := cli.NewContext(&app, set, nil)
_, err = NewBeaconNode(context)
require.NoError(t, err)

require.LogsContain(t, hook, "Removing database")
require.NoError(t, os.RemoveAll(tmp))
}
1 change: 1 addition & 0 deletions slasher/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ go_test(
srcs = ["node_test.go"],
embed = [":go_default_library"],
deps = [
"//shared/cmd:go_default_library",
"//shared/testutil:go_default_library",
"//shared/testutil/require:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
Expand Down
3 changes: 3 additions & 0 deletions slasher/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func (s *SlasherNode) startDB() error {
}
if clearDBConfirmed || forceClearDB {
log.Warning("Removing database")
if err := d.Close(); err != nil {
return errors.Wrap(err, "could not close db prior to clearing")
}
if err := d.ClearDB(); err != nil {
return err
}
Expand Down
28 changes: 28 additions & 0 deletions slasher/node/node_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package node

import (
"crypto/rand"
"flag"
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"testing"

"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -43,3 +47,27 @@ func TestNodeClose_OK(t *testing.T) {
require.LogsContain(t, hook, "Stopping hash slinging slasher")
require.NoError(t, os.RemoveAll(tmp))
}

// TestClearDB tests clearing the database
func TestClearDB(t *testing.T) {
hook := logTest.NewGlobal()

randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random number for file path")
tmp := filepath.Join(testutil.TempDir(), fmt.Sprintf("datadirtest%d", randPath))
require.NoError(t, os.RemoveAll(tmp))

app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.String("datadir", tmp, "node data directory")
set.Bool(cmd.ForceClearDB.Name, true, "force clear db")

context := cli.NewContext(&app, set, nil)
slasherNode, err := NewSlasherNode(context)
require.NoError(t, err)

require.LogsContain(t, hook, "Removing database")
err = slasherNode.db.Close()
require.NoError(t, err)
require.NoError(t, os.RemoveAll(tmp))
}
1 change: 1 addition & 0 deletions validator/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_test(
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",
"//validator/accounts/v1:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
],
)
Expand Down
3 changes: 3 additions & 0 deletions validator/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,9 @@ func clearDB(dataDir string, force bool) error {
if err != nil {
return errors.Wrapf(err, "Could not create DB in dir %s", dataDir)
}
if err := valDB.Close(); err != nil {
return errors.Wrapf(err, "could not close DB in dir %s", dataDir)
}

log.Warning("Removing database")
if err := valDB.ClearDB(); err != nil {
Expand Down
21 changes: 20 additions & 1 deletion validator/node/node_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package node

import (
"crypto/rand"
"flag"
"fmt"
"math/big"
"os"
"path/filepath"
"testing"

"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
v1 "github.com/prysmaticlabs/prysm/validator/accounts/v1"
logTest "github.com/sirupsen/logrus/hooks/test"
"github.com/urfave/cli/v2"
)

Expand All @@ -31,6 +36,20 @@ func TestNode_Builds(t *testing.T) {
context := cli.NewContext(&app, set, nil)

require.NoError(t, v1.NewValidatorAccount(dir, "1234"), "Could not create validator account")
_, err := NewValidatorClient(context)
valClient, err := NewValidatorClient(context)
require.NoError(t, err, "Failed to create ValidatorClient")
err = valClient.db.Close()
require.NoError(t, err)
}

// TestClearDB tests clearing the database
func TestClearDB(t *testing.T) {
hook := logTest.NewGlobal()
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random number for file path")
tmp := filepath.Join(testutil.TempDir(), fmt.Sprintf("datadirtest%d", randPath))
require.NoError(t, os.RemoveAll(tmp))
err = clearDB(tmp, true)
require.NoError(t, err)
require.LogsContain(t, hook, "Removing database")
}