2018-10-05 17:14:50 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2018-10-17 06:11:24 +00:00
|
|
|
"fmt"
|
2018-12-25 06:47:07 +00:00
|
|
|
"time"
|
2018-10-17 06:11:24 +00:00
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-12-20 22:00:38 +00:00
|
|
|
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
2018-12-23 22:51:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
2019-01-09 03:03:57 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
|
2018-10-17 06:11:24 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2018-12-23 22:51:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2018-10-05 17:14:50 +00:00
|
|
|
)
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
// InitializeState creates an initial genesis state for the beacon
|
|
|
|
// node using a set of genesis validators.
|
2019-01-28 11:59:37 +00:00
|
|
|
func (db *BeaconDB) InitializeState(genesisTime uint64) error {
|
2019-01-09 03:03:57 +00:00
|
|
|
// TODO(#1267): Remove initial validators once testnet startup procedure is finalized.
|
|
|
|
genesisValidatorRegistry := validators.InitialValidatorRegistry()
|
2019-01-01 17:17:44 +00:00
|
|
|
deposits := make([]*pb.Deposit, len(genesisValidatorRegistry))
|
|
|
|
for i := 0; i < len(deposits); i++ {
|
2019-01-05 05:39:34 +00:00
|
|
|
depositInput := &pb.DepositInput{
|
|
|
|
Pubkey: genesisValidatorRegistry[i].Pubkey,
|
|
|
|
}
|
|
|
|
balance := genesisValidatorRegistry[i].Balance
|
|
|
|
depositData, err := b.EncodeDepositData(depositInput, balance, time.Now().Unix())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
deposits[i] = &pb.Deposit{DepositData: depositData}
|
2019-01-01 17:17:44 +00:00
|
|
|
}
|
|
|
|
beaconState, err := state.InitialBeaconState(deposits, genesisTime, nil)
|
2018-10-17 06:11:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// #nosec G104
|
2018-12-23 22:51:04 +00:00
|
|
|
stateEnc, _ := proto.Marshal(beaconState)
|
|
|
|
stateHash := hashutil.Hash(stateEnc)
|
2018-12-20 22:00:38 +00:00
|
|
|
genesisBlock := b.NewGenesisBlock(stateHash[:])
|
2018-10-17 06:11:24 +00:00
|
|
|
// #nosec G104
|
2019-01-14 16:41:20 +00:00
|
|
|
blockHash, _ := hashutil.HashBeaconBlock(genesisBlock)
|
2018-10-17 06:11:24 +00:00
|
|
|
// #nosec G104
|
2018-12-20 22:00:38 +00:00
|
|
|
blockEnc, _ := proto.Marshal(genesisBlock)
|
2018-10-17 06:11:24 +00:00
|
|
|
zeroBinary := encodeSlotNumber(0)
|
|
|
|
|
|
|
|
return db.update(func(tx *bolt.Tx) error {
|
|
|
|
blockBkt := tx.Bucket(blockBucket)
|
|
|
|
mainChain := tx.Bucket(mainChainBucket)
|
|
|
|
chainInfo := tx.Bucket(chainInfoBucket)
|
|
|
|
|
|
|
|
if err := chainInfo.Put(mainChainHeightKey, zeroBinary); err != nil {
|
|
|
|
return fmt.Errorf("failed to record block height: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
if err := mainChain.Put(zeroBinary, blockHash[:]); err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
return fmt.Errorf("failed to record block hash: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
if err := blockBkt.Put(blockHash[:], blockEnc); err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
if err := chainInfo.Put(stateLookupKey, stateEnc); err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 09:34:11 +00:00
|
|
|
// State fetches the canonical beacon chain's state from the DB.
|
|
|
|
func (db *BeaconDB) State() (*pb.BeaconState, error) {
|
2018-12-23 22:51:04 +00:00
|
|
|
var beaconState *pb.BeaconState
|
2018-10-17 06:11:24 +00:00
|
|
|
err := db.view(func(tx *bolt.Tx) error {
|
|
|
|
chainInfo := tx.Bucket(chainInfoBucket)
|
2018-12-01 22:09:12 +00:00
|
|
|
enc := chainInfo.Get(stateLookupKey)
|
2018-10-17 06:11:24 +00:00
|
|
|
if enc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2018-12-01 22:09:12 +00:00
|
|
|
beaconState, err = createState(enc)
|
2018-10-05 17:14:50 +00:00
|
|
|
return err
|
2018-10-17 06:11:24 +00:00
|
|
|
})
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
return beaconState, err
|
2018-10-17 06:11:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
// SaveState updates the beacon chain state.
|
2018-12-23 22:51:04 +00:00
|
|
|
func (db *BeaconDB) SaveState(beaconState *pb.BeaconState) error {
|
2018-11-21 18:00:36 +00:00
|
|
|
return db.update(func(tx *bolt.Tx) error {
|
|
|
|
chainInfo := tx.Bucket(chainInfoBucket)
|
2018-12-23 22:51:04 +00:00
|
|
|
beaconStateEnc, err := proto.Marshal(beaconState)
|
2018-11-21 18:00:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-01 22:09:12 +00:00
|
|
|
return chainInfo.Put(stateLookupKey, beaconStateEnc)
|
2018-11-21 18:00:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-21 09:34:11 +00:00
|
|
|
// UnfinalizedBlockState fetches an unfinalized block's
|
2018-10-18 04:23:18 +00:00
|
|
|
// active and crystallized state pair.
|
2019-01-21 09:34:11 +00:00
|
|
|
func (db *BeaconDB) UnfinalizedBlockState(stateRoot [32]byte) (*pb.BeaconState, error) {
|
2018-12-23 22:51:04 +00:00
|
|
|
var beaconState *pb.BeaconState
|
2018-10-18 04:23:18 +00:00
|
|
|
err := db.view(func(tx *bolt.Tx) error {
|
|
|
|
chainInfo := tx.Bucket(chainInfoBucket)
|
2018-12-01 22:09:12 +00:00
|
|
|
encState := chainInfo.Get(stateRoot[:])
|
|
|
|
if encState == nil {
|
2018-10-18 04:23:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2018-12-01 22:09:12 +00:00
|
|
|
beaconState, err = createState(encState)
|
2018-10-18 04:23:18 +00:00
|
|
|
return err
|
|
|
|
})
|
2018-12-01 22:09:12 +00:00
|
|
|
return beaconState, err
|
2018-10-18 04:23:18 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
// SaveUnfinalizedBlockState persists the associated state
|
|
|
|
// for a given unfinalized block.
|
2018-12-23 22:51:04 +00:00
|
|
|
func (db *BeaconDB) SaveUnfinalizedBlockState(beaconState *pb.BeaconState) error {
|
|
|
|
enc, err := proto.Marshal(beaconState)
|
2018-10-18 04:23:18 +00:00
|
|
|
if err != nil {
|
2018-12-23 22:51:04 +00:00
|
|
|
return fmt.Errorf("unable to marshal the beacon state: %v", err)
|
2018-10-18 04:23:18 +00:00
|
|
|
}
|
2018-12-23 22:51:04 +00:00
|
|
|
stateHash := hashutil.Hash(enc)
|
2018-10-18 04:23:18 +00:00
|
|
|
return db.update(func(tx *bolt.Tx) error {
|
|
|
|
chainInfo := tx.Bucket(chainInfoBucket)
|
2018-12-23 22:51:04 +00:00
|
|
|
if err := chainInfo.Put(stateHash[:], enc); err != nil {
|
2018-12-01 22:09:12 +00:00
|
|
|
return fmt.Errorf("failed to save beacon state: %v", err)
|
2018-10-18 04:23:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-23 22:51:04 +00:00
|
|
|
func createState(enc []byte) (*pb.BeaconState, error) {
|
2018-12-01 22:09:12 +00:00
|
|
|
protoState := &pb.BeaconState{}
|
2018-10-17 06:11:24 +00:00
|
|
|
err := proto.Unmarshal(enc, protoState)
|
2018-10-05 17:14:50 +00:00
|
|
|
if err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal encoding: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2018-12-23 22:51:04 +00:00
|
|
|
return protoState, nil
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2018-12-25 06:47:07 +00:00
|
|
|
|
|
|
|
// GenesisTime returns the genesis timestamp for the state.
|
|
|
|
func (db *BeaconDB) GenesisTime() (time.Time, error) {
|
2019-01-21 09:34:11 +00:00
|
|
|
state, err := db.State()
|
2018-12-25 06:47:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, fmt.Errorf("could not retrieve state: %v", err)
|
|
|
|
}
|
|
|
|
if state == nil {
|
|
|
|
return time.Time{}, fmt.Errorf("state not found: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
genesisTime := time.Unix(int64(state.GetGenesisTime()), int64(0))
|
|
|
|
return genesisTime, nil
|
|
|
|
}
|