2020-02-13 16:19:46 +00:00
|
|
|
package testing
|
2019-09-26 16:29:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
2020-01-22 05:39:21 +00:00
|
|
|
|
2020-02-13 16:19:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
slasherDB "github.com/prysmaticlabs/prysm/slasher/db"
|
|
|
|
"github.com/prysmaticlabs/prysm/slasher/db/kv"
|
2019-09-26 16:29:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetupSlasherDB instantiates and returns a SlasherDB instance.
|
2020-02-27 17:22:39 +00:00
|
|
|
func SetupSlasherDB(t testing.TB, spanCacheEnabled bool) *kv.Store {
|
2019-09-26 16:29:10 +00:00
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not generate random file path: %v", err)
|
|
|
|
}
|
2020-02-13 16:19:46 +00:00
|
|
|
p := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
|
2019-09-26 16:29:10 +00:00
|
|
|
if err := os.RemoveAll(p); err != nil {
|
|
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
|
|
|
}
|
2020-02-27 17:22:39 +00:00
|
|
|
cfg := &kv.Config{CacheItems: 0, MaxCacheSize: 0, SpanCacheEnabled: spanCacheEnabled}
|
2020-02-13 16:19:46 +00:00
|
|
|
db, err := slasherDB.NewDB(p, cfg)
|
2020-01-22 05:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to instantiate DB: %v", err)
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
2019-09-26 16:29:10 +00:00
|
|
|
|
2020-01-22 05:39:21 +00:00
|
|
|
// SetupSlasherDBDiffCacheSize instantiates and returns a SlasherDB instance with non default cache size.
|
2020-02-13 16:19:46 +00:00
|
|
|
func SetupSlasherDBDiffCacheSize(t testing.TB, cacheItems int64, maxCacheSize int64) *kv.Store {
|
2020-01-22 05:39:21 +00:00
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not generate random file path: %v", err)
|
|
|
|
}
|
2020-02-13 16:19:46 +00:00
|
|
|
p := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
|
2020-01-22 05:39:21 +00:00
|
|
|
if err := os.RemoveAll(p); err != nil {
|
|
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
|
|
|
}
|
2020-02-13 16:19:46 +00:00
|
|
|
cfg := &kv.Config{CacheItems: cacheItems, MaxCacheSize: maxCacheSize, SpanCacheEnabled: true}
|
|
|
|
newDB, err := slasherDB.NewDB(p, cfg)
|
2019-09-26 16:29:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to instantiate DB: %v", err)
|
|
|
|
}
|
2020-02-13 16:19:46 +00:00
|
|
|
return newDB
|
2019-09-26 16:29:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 19:51:30 +00:00
|
|
|
// TeardownSlasherDB cleans up a test SlasherDB instance.
|
2020-02-13 16:19:46 +00:00
|
|
|
func TeardownSlasherDB(t testing.TB, db *kv.Store) {
|
2019-09-26 16:29:10 +00:00
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Fatalf("Failed to close database: %v", err)
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(db.DatabasePath()); err != nil {
|
|
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
|
|
|
}
|
|
|
|
}
|