prysm-pulse/beacon-chain/db/state.go

135 lines
3.6 KiB
Go
Raw Normal View History

2018-10-05 17:14:50 +00:00
package db
import (
2018-10-17 06:11:24 +00:00
"fmt"
"github.com/boltdb/bolt"
"github.com/gogo/protobuf/proto"
2018-12-01 22:09:12 +00:00
"github.com/prysmaticlabs/prysm/beacon-chain/core/types"
2018-10-17 06:11:24 +00:00
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
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.
func (db *BeaconDB) InitializeState(genesisValidatorRegistry []*pb.ValidatorRecord) error {
beaconState, err := types.NewGenesisBeaconState(genesisValidatorRegistry)
2018-10-17 06:11:24 +00:00
if err != nil {
return err
}
// #nosec G104
2018-12-01 22:09:12 +00:00
stateHash, _ := beaconState.Hash()
genesisBlock := types.NewGenesisBlock(stateHash)
2018-10-17 06:11:24 +00:00
// #nosec G104
2018-12-01 22:09:12 +00:00
blockHash, _ := genesisBlock.Hash()
2018-10-17 06:11:24 +00:00
// #nosec G104
blockEnc, _ := genesisBlock.Marshal()
// #nosec G104
2018-12-01 22:09:12 +00:00
stateEnc, _ := beaconState.Marshal()
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
}
2018-12-01 22:09:12 +00:00
// GetState fetches the canonical beacon chain's state from the DB.
func (db *BeaconDB) GetState() (*types.BeaconState, error) {
var beaconState *types.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.
func (db *BeaconDB) SaveState(beaconState *types.BeaconState) error {
return db.update(func(tx *bolt.Tx) error {
chainInfo := tx.Bucket(chainInfoBucket)
2018-12-01 22:09:12 +00:00
beaconStateEnc, err := beaconState.Marshal()
if err != nil {
return err
}
2018-12-01 22:09:12 +00:00
return chainInfo.Put(stateLookupKey, beaconStateEnc)
})
}
// GetUnfinalizedBlockState fetches an unfinalized block's
// active and crystallized state pair.
2018-12-01 22:09:12 +00:00
func (db *BeaconDB) GetUnfinalizedBlockState(stateRoot [32]byte) (*types.BeaconState, error) {
var beaconState *types.BeaconState
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 {
return nil
}
var err error
2018-12-01 22:09:12 +00:00
beaconState, err = createState(encState)
return err
})
2018-12-01 22:09:12 +00:00
return beaconState, err
}
2018-12-01 22:09:12 +00:00
// SaveUnfinalizedBlockState persists the associated state
// for a given unfinalized block.
func (db *BeaconDB) SaveUnfinalizedBlockState(beaconState *types.BeaconState) error {
stateHash, err := beaconState.Hash()
if err != nil {
2018-12-01 22:09:12 +00:00
return fmt.Errorf("unable to hash the beacon state: %v", err)
}
2018-12-01 22:09:12 +00:00
beaconStateEnc, err := beaconState.Marshal()
if err != nil {
2018-12-01 22:09:12 +00:00
return fmt.Errorf("unable to encode the beacon state: %v", err)
}
return db.update(func(tx *bolt.Tx) error {
chainInfo := tx.Bucket(chainInfoBucket)
2018-12-01 22:09:12 +00:00
if err := chainInfo.Put(stateHash[:], beaconStateEnc); err != nil {
return fmt.Errorf("failed to save beacon state: %v", err)
}
return nil
})
}
2018-12-01 22:09:12 +00:00
func createState(enc []byte) (*types.BeaconState, error) {
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-10-17 06:11:24 +00:00
2018-12-01 22:09:12 +00:00
state := types.NewBeaconState(protoState)
2018-10-17 06:11:24 +00:00
return state, nil
2018-10-05 17:14:50 +00:00
}