2019-10-03 09:29:49 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
2020-12-03 22:28:57 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-10-03 09:29:49 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-03-03 15:37:13 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-07-21 21:34:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
|
2020-04-14 20:27:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-08-08 18:39:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2019-10-03 09:29:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore_Backup(t *testing.T) {
|
2021-02-15 20:29:47 +00:00
|
|
|
db, err := NewKVStore(context.Background(), t.TempDir(), &Config{})
|
2020-12-16 16:56:21 +00:00
|
|
|
require.NoError(t, err, "Failed to instantiate DB")
|
2019-10-03 09:29:49 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
head := testutil.NewBeaconBlock()
|
|
|
|
head.Block.Slot = 5000
|
2019-10-03 09:29:49 +00:00
|
|
|
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, db.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(head)))
|
2020-08-27 18:13:32 +00:00
|
|
|
root, err := head.Block.HashTreeRoot()
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-08 20:00:09 +00:00
|
|
|
st, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, db.SaveState(ctx, st, root))
|
|
|
|
require.NoError(t, db.SaveHeadBlockRoot(ctx, root))
|
2019-10-03 09:29:49 +00:00
|
|
|
|
2021-06-18 01:37:58 +00:00
|
|
|
require.NoError(t, db.Backup(ctx, "", false))
|
2019-10-03 09:29:49 +00:00
|
|
|
|
2020-12-03 22:28:57 +00:00
|
|
|
backupsPath := filepath.Join(db.databasePath, backupsDirectoryName)
|
|
|
|
files, err := ioutil.ReadDir(backupsPath)
|
2020-08-08 18:39:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, 0, len(files), "No backups created")
|
2020-12-03 22:28:57 +00:00
|
|
|
require.NoError(t, db.Close(), "Failed to close database")
|
|
|
|
|
|
|
|
oldFilePath := filepath.Join(backupsPath, files[0].Name())
|
2020-12-07 20:36:43 +00:00
|
|
|
newFilePath := filepath.Join(backupsPath, DatabaseFileName)
|
2020-12-03 22:28:57 +00:00
|
|
|
// We rename the file to match the database file name
|
|
|
|
// our NewKVStore function expects when opening a database.
|
|
|
|
require.NoError(t, os.Rename(oldFilePath, newFilePath))
|
|
|
|
|
2021-02-15 20:29:47 +00:00
|
|
|
backedDB, err := NewKVStore(ctx, backupsPath, &Config{})
|
2020-12-03 22:28:57 +00:00
|
|
|
require.NoError(t, err, "Failed to instantiate DB")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, backedDB.Close(), "Failed to close database")
|
|
|
|
})
|
|
|
|
require.Equal(t, true, backedDB.HasState(ctx, root))
|
2019-10-03 09:29:49 +00:00
|
|
|
}
|
2021-03-03 15:37:13 +00:00
|
|
|
|
|
|
|
func TestStore_BackupMultipleBuckets(t *testing.T) {
|
|
|
|
db, err := NewKVStore(context.Background(), t.TempDir(), &Config{})
|
|
|
|
require.NoError(t, err, "Failed to instantiate DB")
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
startSlot := types.Slot(5000)
|
|
|
|
|
|
|
|
for i := startSlot; i < 5200; i++ {
|
|
|
|
head := testutil.NewBeaconBlock()
|
|
|
|
head.Block.Slot = i
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, db.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(head)))
|
2021-03-03 15:37:13 +00:00
|
|
|
root, err := head.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
st, err := testutil.NewBeaconState()
|
|
|
|
require.NoError(t, st.SetSlot(i))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, db.SaveState(ctx, st, root))
|
|
|
|
require.NoError(t, db.SaveHeadBlockRoot(ctx, root))
|
|
|
|
}
|
|
|
|
|
2021-06-18 01:37:58 +00:00
|
|
|
require.NoError(t, db.Backup(ctx, "", false))
|
2021-03-03 15:37:13 +00:00
|
|
|
|
|
|
|
backupsPath := filepath.Join(db.databasePath, backupsDirectoryName)
|
|
|
|
files, err := ioutil.ReadDir(backupsPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, 0, len(files), "No backups created")
|
|
|
|
require.NoError(t, db.Close(), "Failed to close database")
|
|
|
|
|
|
|
|
oldFilePath := filepath.Join(backupsPath, files[0].Name())
|
|
|
|
newFilePath := filepath.Join(backupsPath, DatabaseFileName)
|
|
|
|
// We rename the file to match the database file name
|
|
|
|
// our NewKVStore function expects when opening a database.
|
|
|
|
require.NoError(t, os.Rename(oldFilePath, newFilePath))
|
|
|
|
|
|
|
|
backedDB, err := NewKVStore(ctx, backupsPath, &Config{})
|
|
|
|
require.NoError(t, err, "Failed to instantiate DB")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, backedDB.Close(), "Failed to close database")
|
|
|
|
})
|
|
|
|
for i := startSlot; i < 5200; i++ {
|
|
|
|
head := testutil.NewBeaconBlock()
|
|
|
|
head.Block.Slot = i
|
|
|
|
root, err := head.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
nBlock, err := backedDB.Block(ctx, root)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, nBlock)
|
2021-05-26 16:19:54 +00:00
|
|
|
require.Equal(t, nBlock.Block().Slot(), i)
|
2021-03-03 15:37:13 +00:00
|
|
|
nState, err := backedDB.State(ctx, root)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, nState)
|
|
|
|
require.Equal(t, nState.Slot(), i)
|
|
|
|
}
|
|
|
|
}
|