2020-06-27 02:37:43 +00:00
|
|
|
// Package kv defines a persistent backend for the validator service.
|
|
|
|
package kv
|
2020-01-08 18:16:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-07-08 17:30:22 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-03-24 20:00:54 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2020-01-08 18:16:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var databaseFileName = "validator.db"
|
|
|
|
|
|
|
|
// Store defines an implementation of the Prysm Database interface
|
|
|
|
// using BoltDB as the underlying persistent kv-store for eth2.
|
|
|
|
type Store struct {
|
|
|
|
db *bolt.DB
|
|
|
|
databasePath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the underlying boltdb database.
|
2020-06-24 01:53:01 +00:00
|
|
|
func (store *Store) Close() error {
|
|
|
|
return store.db.Close()
|
2020-01-08 18:16:17 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 01:53:01 +00:00
|
|
|
func (store *Store) update(fn func(*bolt.Tx) error) error {
|
|
|
|
return store.db.Update(fn)
|
2020-01-08 18:16:17 +00:00
|
|
|
}
|
2020-06-24 01:53:01 +00:00
|
|
|
func (store *Store) view(fn func(*bolt.Tx) error) error {
|
|
|
|
return store.db.View(fn)
|
2020-01-08 18:16:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 05:57:20 +00:00
|
|
|
// ClearDB removes any previously stored data at the configured data directory.
|
2020-06-24 01:53:01 +00:00
|
|
|
func (store *Store) ClearDB() error {
|
|
|
|
if _, err := os.Stat(store.databasePath); os.IsNotExist(err) {
|
2020-01-08 18:16:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-24 01:53:01 +00:00
|
|
|
return os.Remove(filepath.Join(store.databasePath, databaseFileName))
|
2020-01-08 18:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DatabasePath at which this database writes files.
|
2020-06-24 01:53:01 +00:00
|
|
|
func (store *Store) DatabasePath() string {
|
|
|
|
return store.databasePath
|
2020-01-08 18:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createBuckets(tx *bolt.Tx, buckets ...[]byte) error {
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(bucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:57:10 +00:00
|
|
|
// NewKVStore initializes a new boltDB key-value store at the directory
|
|
|
|
// path specified, creates the kv-buckets based on the schema, and stores
|
|
|
|
// an open connection db object as a property of the Store struct.
|
|
|
|
func NewKVStore(dirPath string, pubKeys [][48]byte) (*Store, error) {
|
2020-07-09 14:05:43 +00:00
|
|
|
if err := os.MkdirAll(dirPath, params.BeaconIoConfig().ReadWriteExecutePermissions); err != nil {
|
2020-01-08 18:16:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
datafile := filepath.Join(dirPath, databaseFileName)
|
2020-07-11 16:43:26 +00:00
|
|
|
boltDB, err := bolt.Open(datafile, params.BeaconIoConfig().ReadWritePermissions, &bolt.Options{Timeout: params.BeaconIoConfig().BoltTimeout})
|
2020-01-08 18:16:17 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == bolt.ErrTimeout {
|
|
|
|
return nil, errors.New("cannot obtain database lock, database may be in use by another process")
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
kv := &Store{db: boltDB, databasePath: dirPath}
|
|
|
|
|
|
|
|
if err := kv.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
return createBuckets(
|
|
|
|
tx,
|
|
|
|
historicProposalsBucket,
|
2020-01-19 22:05:48 +00:00
|
|
|
historicAttestationsBucket,
|
2020-08-13 20:27:42 +00:00
|
|
|
validatorAPIBucket,
|
2020-01-09 04:49:32 +00:00
|
|
|
)
|
2020-01-08 18:16:17 +00:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:57:10 +00:00
|
|
|
// Initialize the required public keys into the DB to ensure they're not empty.
|
2020-09-03 15:11:17 +00:00
|
|
|
if pubKeys != nil {
|
|
|
|
if err := kv.UpdatePublicKeysBuckets(pubKeys); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-29 15:57:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 16:20:35 +00:00
|
|
|
return kv, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKVStore returns the validator boltDB key-value store from directory. Returns nil if no such store exists.
|
|
|
|
func GetKVStore(directory string) (*Store, error) {
|
|
|
|
fileName := filepath.Join(directory, databaseFileName)
|
|
|
|
if _, err := os.Stat(fileName); os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-07-11 16:43:26 +00:00
|
|
|
boltDb, err := bolt.Open(fileName, params.BeaconIoConfig().ReadWritePermissions, &bolt.Options{Timeout: params.BeaconIoConfig().BoltTimeout})
|
2020-05-25 16:20:35 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == bolt.ErrTimeout {
|
|
|
|
return nil, errors.New("cannot obtain database lock, database may be in use by another process")
|
|
|
|
}
|
2020-04-14 20:27:03 +00:00
|
|
|
return nil, err
|
2020-01-08 18:16:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 16:20:35 +00:00
|
|
|
return &Store{db: boltDb, databasePath: directory}, nil
|
2020-01-08 18:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the db size in bytes.
|
2020-06-24 01:53:01 +00:00
|
|
|
func (store *Store) Size() (int64, error) {
|
2020-01-08 18:16:17 +00:00
|
|
|
var size int64
|
2020-06-24 01:53:01 +00:00
|
|
|
err := store.db.View(func(tx *bolt.Tx) error {
|
2020-01-08 18:16:17 +00:00
|
|
|
size = tx.Size()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return size, err
|
|
|
|
}
|