mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
38 lines
932 B
Go
38 lines
932 B
Go
|
package backend
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math/big"
|
||
|
"os"
|
||
|
"path"
|
||
|
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
|
||
|
"crypto/rand"
|
||
|
|
||
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
||
|
)
|
||
|
|
||
|
// setupDB instantiates and returns a simulated backend BeaconDB instance.
|
||
|
func setupDB() (*db.BeaconDB, error) {
|
||
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not generate random file path: %v", err)
|
||
|
}
|
||
|
path := path.Join(os.TempDir(), fmt.Sprintf("/%d", randPath))
|
||
|
if err := os.RemoveAll(path); err != nil {
|
||
|
return nil, fmt.Errorf("failed to remove directory: %v", err)
|
||
|
}
|
||
|
return db.NewDB(path)
|
||
|
}
|
||
|
|
||
|
// teardownDB cleans up a simulated backend BeaconDB instance.
|
||
|
func teardownDB(db *db.BeaconDB) {
|
||
|
if err := db.Close(); err != nil {
|
||
|
log.Fatalf("failed to close database: %v", err)
|
||
|
}
|
||
|
if err := os.RemoveAll(db.DatabasePath); err != nil {
|
||
|
log.Fatalf("could not remove tmp db dir: %v", err)
|
||
|
}
|
||
|
}
|