2019-10-03 09:29:49 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-03 22:28:57 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-10-03 09:29:49 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/consensus-types/blocks"
|
|
|
|
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/util"
|
2019-10-03 09:29:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore_Backup(t *testing.T) {
|
2022-08-17 12:22:41 +00:00
|
|
|
db, err := NewKVStore(context.Background(), t.TempDir())
|
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()
|
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
head := util.NewBeaconBlock()
|
2020-08-27 18:13:32 +00:00
|
|
|
head.Block.Slot = 5000
|
2019-10-03 09:29:49 +00:00
|
|
|
|
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, db.SaveBlock(ctx, wsb))
|
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-09-23 18:53:46 +00:00
|
|
|
st, err := util.NewBeaconState()
|
2021-02-08 20:00:09 +00:00
|
|
|
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)
|
2022-04-18 20:42:07 +00:00
|
|
|
files, err := os.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))
|
|
|
|
|
2022-08-17 12:22:41 +00:00
|
|
|
backedDB, err := NewKVStore(ctx, backupsPath)
|
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) {
|
2022-08-17 12:22:41 +00:00
|
|
|
db, err := NewKVStore(context.Background(), t.TempDir())
|
2021-03-03 15:37:13 +00:00
|
|
|
require.NoError(t, err, "Failed to instantiate DB")
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
startSlot := types.Slot(5000)
|
|
|
|
|
|
|
|
for i := startSlot; i < 5200; i++ {
|
2021-09-23 18:53:46 +00:00
|
|
|
head := util.NewBeaconBlock()
|
2021-03-03 15:37:13 +00:00
|
|
|
head.Block.Slot = i
|
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, db.SaveBlock(ctx, wsb))
|
2021-03-03 15:37:13 +00:00
|
|
|
root, err := head.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
2021-09-23 18:53:46 +00:00
|
|
|
st, err := util.NewBeaconState()
|
2021-03-03 15:37:13 +00:00
|
|
|
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)
|
2022-04-18 20:42:07 +00:00
|
|
|
files, err := os.ReadDir(backupsPath)
|
2021-03-03 15:37:13 +00:00
|
|
|
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))
|
|
|
|
|
2022-08-17 12:22:41 +00:00
|
|
|
backedDB, err := NewKVStore(ctx, backupsPath)
|
2021-03-03 15:37:13 +00:00
|
|
|
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++ {
|
2021-09-23 18:53:46 +00:00
|
|
|
head := util.NewBeaconBlock()
|
2021-03-03 15:37:13 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|