2018-10-05 17:14:50 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2019-08-09 19:17:18 +00:00
|
|
|
"context"
|
2018-10-17 06:11:24 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2019-02-04 13:47:17 +00:00
|
|
|
"sync"
|
2018-12-17 09:11:11 +00:00
|
|
|
"time"
|
2018-12-17 18:34:28 +00:00
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
"github.com/boltdb/bolt"
|
2019-08-02 02:27:38 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-08-13 23:13:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache"
|
2019-08-11 00:50:10 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
|
2019-08-14 18:48:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv"
|
2019-08-09 19:17:18 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
2019-08-12 19:33:07 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-10-05 17:14:50 +00:00
|
|
|
)
|
|
|
|
|
2019-02-04 13:47:17 +00:00
|
|
|
var log = logrus.WithField("prefix", "beacondb")
|
|
|
|
|
2019-08-09 19:17:18 +00:00
|
|
|
// Database defines the necessary methods for Prysm's eth2 backend which may
|
|
|
|
// be implemented by any key-value or relational database in practice.
|
|
|
|
type Database interface {
|
|
|
|
ClearDB() error
|
|
|
|
Attestation(ctx context.Context, attRoot [32]byte) (*ethpb.Attestation, error)
|
2019-08-11 00:50:10 +00:00
|
|
|
Attestations(ctx context.Context, f *filters.QueryFilter) ([]*ethpb.Attestation, error)
|
2019-08-09 19:17:18 +00:00
|
|
|
HasAttestation(ctx context.Context, attRoot [32]byte) bool
|
|
|
|
DeleteAttestation(ctx context.Context, attRoot [32]byte) error
|
|
|
|
SaveAttestation(ctx context.Context, att *ethpb.Attestation) error
|
|
|
|
SaveAttestations(ctx context.Context, atts []*ethpb.Attestation) error
|
|
|
|
Block(ctx context.Context, blockRoot [32]byte) (*ethpb.BeaconBlock, error)
|
2019-08-12 16:13:30 +00:00
|
|
|
HeadBlock(ctx context.Context) (*ethpb.BeaconBlock, error)
|
2019-08-11 00:50:10 +00:00
|
|
|
Blocks(ctx context.Context, f *filters.QueryFilter) ([]*ethpb.BeaconBlock, error)
|
|
|
|
BlockRoots(ctx context.Context, f *filters.QueryFilter) ([][]byte, error)
|
2019-08-09 19:17:18 +00:00
|
|
|
HasBlock(ctx context.Context, blockRoot [32]byte) bool
|
|
|
|
DeleteBlock(ctx context.Context, blockRoot [32]byte) error
|
|
|
|
SaveBlock(ctx context.Context, block *ethpb.BeaconBlock) error
|
|
|
|
SaveBlocks(ctx context.Context, blocks []*ethpb.BeaconBlock) error
|
2019-08-12 16:13:30 +00:00
|
|
|
SaveHeadBlockRoot(ctx context.Context, blockRoot [32]byte) error
|
2019-08-09 19:17:18 +00:00
|
|
|
ValidatorLatestVote(ctx context.Context, validatorIdx uint64) (*pb.ValidatorLatestVote, error)
|
|
|
|
HasValidatorLatestVote(ctx context.Context, validatorIdx uint64) bool
|
2019-08-11 00:50:10 +00:00
|
|
|
SaveValidatorLatestVote(ctx context.Context, validatorIdx uint64, vote *pb.ValidatorLatestVote) error
|
2019-08-13 22:33:31 +00:00
|
|
|
State(ctx context.Context, blockRoot [32]byte) (*pb.BeaconState, error)
|
2019-08-09 19:17:18 +00:00
|
|
|
HeadState(ctx context.Context) (*pb.BeaconState, error)
|
|
|
|
SaveState(ctx context.Context, state *pb.BeaconState, blockRoot [32]byte) error
|
2019-08-12 19:33:07 +00:00
|
|
|
ValidatorIndex(ctx context.Context, publicKey [48]byte) (uint64, bool, error)
|
2019-08-09 19:17:18 +00:00
|
|
|
HasValidatorIndex(ctx context.Context, publicKey [48]byte) bool
|
|
|
|
DeleteValidatorIndex(ctx context.Context, publicKey [48]byte) error
|
|
|
|
SaveValidatorIndex(ctx context.Context, publicKey [48]byte, validatorIdx uint64) error
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:14:50 +00:00
|
|
|
// BeaconDB manages the data layer of the beacon chain implementation.
|
|
|
|
// The exposed methods do not have an opinion of the underlying data engine,
|
|
|
|
// but instead reflect the beacon chain logic.
|
|
|
|
// For example, instead of defining get, put, remove
|
|
|
|
// This defines methods such as getBlock, saveBlocksAndAttestations, etc.
|
|
|
|
type BeaconDB struct {
|
2019-04-24 11:03:55 +00:00
|
|
|
// state objects and caches
|
|
|
|
stateLock sync.RWMutex
|
|
|
|
serializedState []byte
|
|
|
|
stateHash [32]byte
|
2019-07-22 14:03:57 +00:00
|
|
|
validatorRegistry []*ethpb.Validator
|
2019-04-24 11:03:55 +00:00
|
|
|
validatorBalances []uint64
|
|
|
|
db *bolt.DB
|
|
|
|
DatabasePath string
|
2019-02-04 13:47:17 +00:00
|
|
|
|
2019-04-18 21:57:19 +00:00
|
|
|
// Beacon block info in memory.
|
2019-04-03 15:32:59 +00:00
|
|
|
highestBlockSlot uint64
|
2019-04-18 21:57:19 +00:00
|
|
|
// We keep a map of hashes of blocks which failed processing for blacklisting.
|
|
|
|
badBlockHashes map[[32]byte]bool
|
|
|
|
badBlocksLock sync.RWMutex
|
2019-07-22 14:03:57 +00:00
|
|
|
blocks map[[32]byte]*ethpb.BeaconBlock
|
2019-05-11 01:19:46 +00:00
|
|
|
blocksLock sync.RWMutex
|
2019-04-03 15:32:59 +00:00
|
|
|
|
2019-02-04 13:47:17 +00:00
|
|
|
// Beacon chain deposits in memory.
|
2019-08-13 23:13:47 +00:00
|
|
|
DepositCache *depositcache.DepositCache
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 03:02:47 +00:00
|
|
|
// Close closes the underlying boltdb database.
|
2018-10-17 06:11:24 +00:00
|
|
|
func (db *BeaconDB) Close() error {
|
|
|
|
return db.db.Close()
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
func (db *BeaconDB) update(fn func(*bolt.Tx) error) error {
|
|
|
|
return db.db.Update(fn)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2019-02-28 05:14:52 +00:00
|
|
|
func (db *BeaconDB) batch(fn func(*bolt.Tx) error) error {
|
|
|
|
return db.db.Batch(fn)
|
|
|
|
}
|
2018-10-17 06:11:24 +00:00
|
|
|
func (db *BeaconDB) view(fn func(*bolt.Tx) error) error {
|
|
|
|
return db.db.View(fn)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
func createBuckets(tx *bolt.Tx, buckets ...[]byte) error {
|
|
|
|
for _, bucket := range buckets {
|
2018-11-19 01:59:11 +00:00
|
|
|
if _, err := tx.CreateBucketIfNotExists(bucket); err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
return nil
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 18:48:28 +00:00
|
|
|
// NewDBDeprecated initializes a new DB. If the genesis block and states do not exist, this method creates it.
|
|
|
|
func NewDBDeprecated(dirPath string) (*BeaconDB, error) {
|
2018-10-17 06:11:24 +00:00
|
|
|
if err := os.MkdirAll(dirPath, 0700); err != nil {
|
2018-10-05 17:14:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-17 06:11:24 +00:00
|
|
|
datafile := path.Join(dirPath, "beaconchain.db")
|
2018-12-17 09:11:11 +00:00
|
|
|
boltDB, err := bolt.Open(datafile, 0600, &bolt.Options{Timeout: 1 * time.Second})
|
2018-10-05 17:14:50 +00:00
|
|
|
if err != nil {
|
2018-12-17 09:11:11 +00:00
|
|
|
if err == bolt.ErrTimeout {
|
|
|
|
return nil, errors.New("cannot obtain database lock, database may be in use by another process")
|
|
|
|
}
|
2018-10-05 17:14:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-13 23:13:47 +00:00
|
|
|
depCache := depositcache.NewDepositCache()
|
|
|
|
|
|
|
|
db := &BeaconDB{db: boltDB, DatabasePath: dirPath, DepositCache: depCache}
|
2019-07-22 14:03:57 +00:00
|
|
|
db.blocks = make(map[[32]byte]*ethpb.BeaconBlock)
|
2018-10-15 13:17:07 +00:00
|
|
|
|
2018-10-30 16:00:20 +00:00
|
|
|
if err := db.update(func(tx *bolt.Tx) error {
|
2019-04-24 17:21:00 +00:00
|
|
|
return createBuckets(tx, blockBucket, attestationBucket, attestationTargetBucket, mainChainBucket,
|
|
|
|
histStateBucket, chainInfoBucket, cleanupHistoryBucket, blockOperationsBucket, validatorBucket)
|
2018-10-30 16:00:20 +00:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2018-10-30 16:00:20 +00:00
|
|
|
return db, err
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2019-03-15 03:20:49 +00:00
|
|
|
|
2019-08-14 18:48:28 +00:00
|
|
|
// NewDB initializes a new DB.
|
|
|
|
func NewDB(dirPath string) (Database, error) {
|
|
|
|
return kv.NewKVStore(dirPath)
|
|
|
|
}
|
|
|
|
|
2019-03-15 03:20:49 +00:00
|
|
|
// ClearDB removes the previously stored directory at the data directory.
|
|
|
|
func ClearDB(dirPath string) error {
|
|
|
|
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return os.RemoveAll(dirPath)
|
|
|
|
}
|