2018-10-05 17:14:50 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2018-11-19 01:59:11 +00:00
|
|
|
"crypto/rand"
|
2018-11-07 19:07:41 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2018-10-17 06:11:24 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2019-03-15 03:20:49 +00:00
|
|
|
"strings"
|
2018-10-05 17:14:50 +00:00
|
|
|
"testing"
|
2019-02-13 23:49:06 +00:00
|
|
|
|
2019-08-11 00:50:10 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv"
|
2019-07-22 14:03:57 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
2019-02-13 23:49:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2018-10-05 17:14:50 +00:00
|
|
|
)
|
|
|
|
|
2019-08-11 00:50:10 +00:00
|
|
|
var _ = Database(&kv.Store{})
|
|
|
|
|
2018-11-07 19:07:41 +00:00
|
|
|
// setupDB instantiates and returns a BeaconDB instance.
|
2019-03-12 19:30:10 +00:00
|
|
|
func setupDB(t testing.TB) *BeaconDB {
|
2018-11-07 19:07:41 +00:00
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
2018-10-05 17:14:50 +00:00
|
|
|
if err != nil {
|
2018-11-07 19:07:41 +00:00
|
|
|
t.Fatalf("Could not generate random file path: %v", err)
|
|
|
|
}
|
2019-02-13 23:49:06 +00:00
|
|
|
path := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
|
2018-11-07 19:07:41 +00:00
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2019-08-14 18:48:28 +00:00
|
|
|
db, err := NewDBDeprecated(path)
|
2019-07-22 14:03:57 +00:00
|
|
|
db.blocks = make(map[[32]byte]*ethpb.BeaconBlock)
|
2019-05-11 01:19:46 +00:00
|
|
|
|
2018-10-05 17:14:50 +00:00
|
|
|
if err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
t.Fatalf("Failed to instantiate DB: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2018-10-17 06:11:24 +00:00
|
|
|
return db
|
|
|
|
}
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2018-11-07 19:07:41 +00:00
|
|
|
// teardownDB cleans up a test BeaconDB instance.
|
2019-03-12 19:30:10 +00:00
|
|
|
func teardownDB(t testing.TB, db *BeaconDB) {
|
2018-11-07 19:07:41 +00:00
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Fatalf("Failed to close database: %v", err)
|
|
|
|
}
|
2019-08-21 16:04:00 +00:00
|
|
|
if err := os.RemoveAll(db.DatabasePath()); err != nil {
|
2018-11-07 19:07:41 +00:00
|
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
|
|
|
}
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2019-03-15 03:20:49 +00:00
|
|
|
|
|
|
|
func TestClearDB(t *testing.T) {
|
|
|
|
beaconDB := setupDB(t)
|
2019-08-21 16:04:00 +00:00
|
|
|
path := strings.TrimSuffix(beaconDB.DatabasePath(), "beaconchain.db")
|
2019-03-15 03:20:49 +00:00
|
|
|
if err := ClearDB(path); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-21 16:04:00 +00:00
|
|
|
if _, err := os.Stat(beaconDB.DatabasePath()); !os.IsNotExist(err) {
|
2019-03-15 03:20:49 +00:00
|
|
|
t.Fatalf("db wasnt cleared %v", err)
|
|
|
|
}
|
|
|
|
}
|