2021-04-23 17:06:13 +00:00
|
|
|
// Package slasherkv defines a bolt-db, key-value store implementation
|
|
|
|
// of the slasher database interface for Prysm.
|
|
|
|
package slasherkv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/db/iface"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/io/file"
|
2021-04-23 17:06:13 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ iface.SlasherDatabase = (*Store)(nil)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DatabaseFileName is the name of the beacon node database.
|
|
|
|
DatabaseFileName = "slasher.db"
|
|
|
|
boltAllocSize = 8 * 1024 * 1024
|
2022-08-17 12:22:41 +00:00
|
|
|
// Specifies the initial mmap size of bolt.
|
|
|
|
mmapSize = 536870912
|
2021-04-23 17:06:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Store defines an implementation of the Prysm Database interface
|
2021-06-26 19:00:33 +00:00
|
|
|
// using BoltDB as the underlying persistent kv-store for Ethereum consensus.
|
2021-04-23 17:06:13 +00:00
|
|
|
type Store struct {
|
|
|
|
db *bolt.DB
|
|
|
|
databasePath string
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-08-17 12:22:41 +00:00
|
|
|
func NewKVStore(ctx context.Context, dirPath string) (*Store, error) {
|
2021-09-17 21:55:24 +00:00
|
|
|
hasDir, err := file.HasDir(dirPath)
|
2021-04-23 17:06:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasDir {
|
2021-09-17 21:55:24 +00:00
|
|
|
if err := file.MkdirAll(dirPath); err != nil {
|
2021-04-23 17:06:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
datafile := path.Join(dirPath, DatabaseFileName)
|
|
|
|
boltDB, err := bolt.Open(
|
|
|
|
datafile,
|
|
|
|
params.BeaconIoConfig().ReadWritePermissions,
|
|
|
|
&bolt.Options{
|
|
|
|
Timeout: 1 * time.Second,
|
2022-08-17 12:22:41 +00:00
|
|
|
InitialMmapSize: mmapSize,
|
2021-04-23 17:06:13 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, bolt.ErrTimeout) {
|
|
|
|
return nil, errors.New("cannot obtain database lock, database may be in use by another process")
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
boltDB.AllocSize = boltAllocSize
|
|
|
|
kv := &Store{
|
|
|
|
db: boltDB,
|
|
|
|
databasePath: dirPath,
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := kv.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
return createBuckets(
|
|
|
|
tx,
|
|
|
|
// Slasher buckets.
|
|
|
|
attestedEpochsByValidator,
|
2021-06-23 21:19:22 +00:00
|
|
|
attestationRecordsBucket,
|
2021-04-23 17:06:13 +00:00
|
|
|
attestationDataRootsBucket,
|
|
|
|
proposalRecordsBucket,
|
|
|
|
slasherChunksBucket,
|
|
|
|
)
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return kv, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClearDB removes the previously stored database in the data directory.
|
|
|
|
func (s *Store) ClearDB() error {
|
|
|
|
if _, err := os.Stat(s.databasePath); os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := os.Remove(path.Join(s.databasePath, DatabaseFileName)); err != nil {
|
|
|
|
return errors.Wrap(err, "could not remove database file")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the underlying BoltDB database.
|
|
|
|
func (s *Store) Close() error {
|
|
|
|
return s.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DatabasePath at which this database writes files.
|
|
|
|
func (s *Store) DatabasePath() string {
|
|
|
|
return s.databasePath
|
|
|
|
}
|
|
|
|
|
|
|
|
func createBuckets(tx *bolt.Tx, buckets ...[]byte) error {
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(bucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|