2020-12-07 20:36:43 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/db/kv"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/cmd"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/consensus-types/blocks"
|
2023-01-26 14:40:12 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/util"
|
2020-12-07 20:36:43 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRestore(t *testing.T) {
|
|
|
|
logHook := logTest.NewGlobal()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-08-17 12:22:41 +00:00
|
|
|
backupDb, err := kv.NewKVStore(context.Background(), t.TempDir())
|
2020-12-07 20:36:43 +00:00
|
|
|
require.NoError(t, err)
|
2021-09-23 18:53:46 +00:00
|
|
|
head := util.NewBeaconBlock()
|
2020-12-07 20:36:43 +00:00
|
|
|
head.Block.Slot = 5000
|
2022-08-02 15:30:46 +00:00
|
|
|
wsb, err := blocks.NewSignedBeaconBlock(head)
|
2022-03-25 23:00:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, backupDb.SaveBlock(ctx, wsb))
|
2020-12-07 20:36:43 +00:00
|
|
|
root, err := head.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
2021-09-23 18:53:46 +00:00
|
|
|
st, err := util.NewBeaconState()
|
2021-02-08 20:00:09 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-07 20:36:43 +00:00
|
|
|
require.NoError(t, backupDb.SaveState(ctx, st, root))
|
|
|
|
require.NoError(t, backupDb.SaveHeadBlockRoot(ctx, root))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, backupDb.Close())
|
|
|
|
// We rename the backup file so that we can later verify
|
|
|
|
// whether the restored db has been renamed correctly.
|
|
|
|
require.NoError(t, os.Rename(
|
|
|
|
path.Join(backupDb.DatabasePath(), kv.DatabaseFileName),
|
|
|
|
path.Join(backupDb.DatabasePath(), "backup.db")))
|
|
|
|
|
|
|
|
restoreDir := t.TempDir()
|
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.String(cmd.RestoreSourceFileFlag.Name, "", "")
|
|
|
|
set.String(cmd.RestoreTargetDirFlag.Name, "", "")
|
|
|
|
require.NoError(t, set.Set(cmd.RestoreSourceFileFlag.Name, path.Join(backupDb.DatabasePath(), "backup.db")))
|
|
|
|
require.NoError(t, set.Set(cmd.RestoreTargetDirFlag.Name, restoreDir))
|
|
|
|
cliCtx := cli.NewContext(&app, set, nil)
|
|
|
|
|
2021-03-04 21:01:32 +00:00
|
|
|
assert.NoError(t, Restore(cliCtx))
|
2020-12-07 20:36:43 +00:00
|
|
|
|
2022-04-18 20:42:07 +00:00
|
|
|
files, err := os.ReadDir(path.Join(restoreDir, kv.BeaconNodeDbDirName))
|
2020-12-07 20:36:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(files))
|
|
|
|
assert.Equal(t, kv.DatabaseFileName, files[0].Name())
|
2022-08-17 12:22:41 +00:00
|
|
|
restoredDb, err := kv.NewKVStore(context.Background(), path.Join(restoreDir, kv.BeaconNodeDbDirName))
|
2020-12-07 20:36:43 +00:00
|
|
|
defer func() {
|
|
|
|
require.NoError(t, restoredDb.Close())
|
|
|
|
}()
|
|
|
|
require.NoError(t, err)
|
|
|
|
headBlock, err := restoredDb.HeadBlock(ctx)
|
|
|
|
require.NoError(t, err)
|
2023-01-26 14:40:12 +00:00
|
|
|
assert.Equal(t, primitives.Slot(5000), headBlock.Block().Slot(), "Restored database has incorrect data")
|
2020-12-07 20:36:43 +00:00
|
|
|
assert.LogsContain(t, logHook, "Restore completed successfully")
|
|
|
|
|
|
|
|
}
|