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-11-09 20:27:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/fileutil"
|
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
|
|
|
)
|
|
|
|
|
2020-09-08 16:42:06 +00:00
|
|
|
// ProtectionDbFileName Validator slashing protection db file name.
|
|
|
|
var ProtectionDbFileName = "validator.db"
|
2020-01-08 18:16:17 +00:00
|
|
|
|
2020-10-15 19:35:31 +00:00
|
|
|
const proposalExported = "PROPOSALS_IMPORTED"
|
|
|
|
const attestationExported = "ATTESTATIONS_IMPORTED"
|
|
|
|
|
2020-01-08 18:16:17 +00:00
|
|
|
// 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-09-08 16:42:06 +00:00
|
|
|
return os.Remove(filepath.Join(store.databasePath, ProtectionDbFileName))
|
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-11-09 20:27:03 +00:00
|
|
|
hasDir, err := fileutil.HasDir(dirPath)
|
|
|
|
if err != nil {
|
2020-01-08 18:16:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-09 20:27:03 +00:00
|
|
|
if !hasDir {
|
|
|
|
if err := fileutil.MkdirAll(dirPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 16:42:06 +00:00
|
|
|
datafile := filepath.Join(dirPath, ProtectionDbFileName)
|
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 {
|
2020-10-14 17:39:52 +00:00
|
|
|
if errors.Is(err, bolt.ErrTimeout) {
|
2020-01-08 18:16:17 +00:00
|
|
|
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,
|
2020-11-20 18:06:12 +00:00
|
|
|
genesisInfoBucket,
|
2020-01-08 18:16:17 +00:00
|
|
|
historicProposalsBucket,
|
2020-01-19 22:05:48 +00:00
|
|
|
historicAttestationsBucket,
|
2020-10-06 19:59:36 +00:00
|
|
|
newHistoricAttestationsBucket,
|
2020-11-26 02:39:23 +00:00
|
|
|
newHistoricProposalsBucket,
|
|
|
|
highestSignedSourceBucket,
|
|
|
|
highestSignedTargetBucket,
|
2020-11-25 23:58:01 +00:00
|
|
|
lowestSignedProposalsBucket,
|
|
|
|
highestSignedProposalsBucket,
|
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) {
|
2020-09-08 16:42:06 +00:00
|
|
|
fileName := filepath.Join(directory, ProtectionDbFileName)
|
2020-05-25 16:20:35 +00:00
|
|
|
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 {
|
2020-10-14 17:39:52 +00:00
|
|
|
if errors.Is(err, bolt.ErrTimeout) {
|
2020-05-25 16:20:35 +00:00
|
|
|
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
|
|
|
|
}
|