2018-10-05 17:14:50 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2018-12-17 18:34:28 +00:00
|
|
|
"errors"
|
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-02-04 13:47:17 +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")
|
|
|
|
|
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 {
|
2018-11-07 19:07:41 +00:00
|
|
|
db *bolt.DB
|
|
|
|
DatabasePath string
|
2019-02-04 13:47:17 +00:00
|
|
|
|
|
|
|
// Beacon chain deposits in memory.
|
|
|
|
deposits []*depositContainer
|
|
|
|
depositsLock sync.RWMutex
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// NewDB initializes a new DB. If the genesis block and states do not exist, this method creates it.
|
2018-10-17 06:11:24 +00:00
|
|
|
func NewDB(dirPath string) (*BeaconDB, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-07 19:07:41 +00:00
|
|
|
db := &BeaconDB{db: boltDB, DatabasePath: dirPath}
|
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 {
|
2018-11-21 18:00:36 +00:00
|
|
|
return createBuckets(tx, blockBucket, attestationBucket, mainChainBucket,
|
2019-02-25 17:37:02 +00:00
|
|
|
chainInfoBucket, cleanupHistoryBucket, blockOperationsBucket, validatorBucket)
|
2018-11-21 18:00:36 +00:00
|
|
|
|
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
|
|
|
}
|