mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
16b04699d0
* polling interval * adding proto message * changing proto messages * changing naming * adding slot functionality * initial sync working * new changes * more sync fixes * its working now * finally working * add tests * fix tests * tests * adding tests * lint * log checks * making changes to simulator * update logs * fix tests * get sync to work with crystallized state * fixing race * making requested changes * unexport * documentation * gazelle and fix merge conflicts * adding repeated requests * fix lint * adding new clock , db methods, and util func * revert change to test * gazelle * add in test * gazelle * finally working * save slot * fix lint and constant
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package db
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/boltdb/bolt"
|
|
)
|
|
|
|
// 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 {
|
|
db *bolt.DB
|
|
DatabasePath string
|
|
}
|
|
|
|
// Close closes the underlying leveldb 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) 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, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
db := &BeaconDB{db: boltDB, DatabasePath: dirPath}
|
|
|
|
if err := db.update(func(tx *bolt.Tx) error {
|
|
return createBuckets(tx, blockBucket, attestationBucket, mainChainBucket,
|
|
chainInfoBucket, blockVoteCacheBucket, simulatorBucket)
|
|
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return db, err
|
|
}
|