mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
1b5b8a57e0
* Update io_kubernetes_build commit hash to 1246899 * Update dependency build_bazel_rules_nodejs to v0.33.1 * Update dependency com_github_hashicorp_golang_lru to v0.5.1 * Update libp2p * Update io_bazel_rules_k8s commit hash to e68d5d7 * Starting to remove old protos * Bazel build proto passes * Fixing pb version * Cleaned up core package * Fixing tests * 6 tests failing * Update proto bugs * Fixed incorrect validator ordering proto * Sync with master * Update go-ssz commit * Removed bad copies from v1alpha1 folder * add json spec json to pb handler * add nested proto example * proto/testing test works * fix refactoring build failures * use merged ssz * push latest changes * used forked json encoding * used forked json encoding * fix warning * fix build issues * fix test and lint * fix build * lint
107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
package db
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/boltdb/bolt"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var log = logrus.WithField("prefix", "beacondb")
|
|
|
|
// 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 {
|
|
// state objects and caches
|
|
stateLock sync.RWMutex
|
|
serializedState []byte
|
|
stateHash [32]byte
|
|
validatorRegistry []*ethpb.Validator
|
|
validatorBalances []uint64
|
|
db *bolt.DB
|
|
DatabasePath string
|
|
|
|
// Beacon block info in memory.
|
|
highestBlockSlot uint64
|
|
// We keep a map of hashes of blocks which failed processing for blacklisting.
|
|
badBlockHashes map[[32]byte]bool
|
|
badBlocksLock sync.RWMutex
|
|
blocks map[[32]byte]*ethpb.BeaconBlock
|
|
blocksLock sync.RWMutex
|
|
|
|
// Beacon chain deposits in memory.
|
|
pendingDeposits []*DepositContainer
|
|
deposits []*DepositContainer
|
|
depositsLock sync.RWMutex
|
|
chainstartPubkeys map[string]bool
|
|
chainstartPubkeysLock sync.RWMutex
|
|
}
|
|
|
|
// Close closes the underlying boltdb database.
|
|
func (db *BeaconDB) Close() error {
|
|
return db.db.Close()
|
|
}
|
|
|
|
func (db *BeaconDB) update(fn func(*bolt.Tx) error) error {
|
|
return db.db.Update(fn)
|
|
}
|
|
func (db *BeaconDB) batch(fn func(*bolt.Tx) error) error {
|
|
return db.db.Batch(fn)
|
|
}
|
|
func (db *BeaconDB) view(fn func(*bolt.Tx) error) error {
|
|
return db.db.View(fn)
|
|
}
|
|
|
|
func createBuckets(tx *bolt.Tx, buckets ...[]byte) error {
|
|
for _, bucket := range buckets {
|
|
if _, err := tx.CreateBucketIfNotExists(bucket); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// NewDB initializes a new DB. If the genesis block and states do not exist, this method creates it.
|
|
func NewDB(dirPath string) (*BeaconDB, error) {
|
|
if err := os.MkdirAll(dirPath, 0700); err != nil {
|
|
return nil, err
|
|
}
|
|
datafile := path.Join(dirPath, "beaconchain.db")
|
|
boltDB, err := bolt.Open(datafile, 0600, &bolt.Options{Timeout: 1 * time.Second})
|
|
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
|
|
}
|
|
|
|
db := &BeaconDB{db: boltDB, DatabasePath: dirPath}
|
|
db.blocks = make(map[[32]byte]*ethpb.BeaconBlock)
|
|
|
|
if err := db.update(func(tx *bolt.Tx) error {
|
|
return createBuckets(tx, blockBucket, attestationBucket, attestationTargetBucket, mainChainBucket,
|
|
histStateBucket, chainInfoBucket, cleanupHistoryBucket, blockOperationsBucket, validatorBucket)
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return db, err
|
|
}
|
|
|
|
// 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)
|
|
}
|