2020-02-08 02:05:43 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-06 19:15:31 +00:00
|
|
|
"fmt"
|
2020-02-08 02:05:43 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-02-15 18:57:49 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-05-06 19:15:31 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
|
|
|
|
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
2020-06-16 15:10:34 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2020-05-20 19:16:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray"
|
2020-02-09 16:12:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2020-02-14 01:03:51 +00:00
|
|
|
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2020-02-08 02:05:43 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-02-15 18:57:49 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-05-06 19:15:31 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-02-08 02:05:43 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
2020-02-15 18:57:49 +00:00
|
|
|
// This defines the current chain service's view of head.
|
|
|
|
type head struct {
|
|
|
|
slot uint64 // current head slot.
|
|
|
|
root [32]byte // current head root.
|
|
|
|
block *ethpb.SignedBeaconBlock // current head block.
|
|
|
|
state *state.BeaconState // current head state.
|
|
|
|
}
|
|
|
|
|
2020-06-23 21:11:20 +00:00
|
|
|
// Determined the head from the fork choice service and saves its new data
|
|
|
|
// (head root, head block, and head state) to the local service cache.
|
2020-02-08 02:05:43 +00:00
|
|
|
func (s *Service) updateHead(ctx context.Context, balances []uint64) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "blockchain.updateHead")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
// To get the proper head update, a node first checks its best justified
|
|
|
|
// can become justified. This is designed to prevent bounce attack and
|
|
|
|
// ensure head gets its best justified info.
|
|
|
|
if s.bestJustifiedCheckpt.Epoch > s.justifiedCheckpt.Epoch {
|
|
|
|
s.justifiedCheckpt = s.bestJustifiedCheckpt
|
2020-06-16 15:10:34 +00:00
|
|
|
if err := s.cacheJustifiedStateBalances(ctx, bytesutil.ToBytes32(s.justifiedCheckpt.Root)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-08 02:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get head from the fork choice service.
|
|
|
|
f := s.finalizedCheckpt
|
|
|
|
j := s.justifiedCheckpt
|
2020-06-01 03:32:34 +00:00
|
|
|
// To get head before the first justified epoch, the fork choice will start with genesis root
|
|
|
|
// instead of zero hashes.
|
|
|
|
headStartRoot := bytesutil.ToBytes32(j.Root)
|
|
|
|
if headStartRoot == params.BeaconConfig().ZeroHash {
|
|
|
|
headStartRoot = s.genesisRoot
|
|
|
|
}
|
|
|
|
headRoot, err := s.forkChoiceStore.Head(ctx, j.Epoch, headStartRoot, balances, f.Epoch)
|
2020-02-08 02:05:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:16:56 +00:00
|
|
|
if err := s.updateRecentCanonicalBlocks(ctx, headRoot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:05:43 +00:00
|
|
|
// Save head to the local service cache.
|
|
|
|
return s.saveHead(ctx, headRoot)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This saves head info to the local service cache, it also saves the
|
|
|
|
// new head root to the DB.
|
|
|
|
func (s *Service) saveHead(ctx context.Context, headRoot [32]byte) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "blockchain.saveHead")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
// Do nothing if head hasn't changed.
|
2020-02-15 18:57:49 +00:00
|
|
|
if headRoot == s.headRoot() {
|
2020-02-08 02:05:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-09 16:12:06 +00:00
|
|
|
// If the head state is not available, just return nil.
|
|
|
|
// There's nothing to cache
|
2020-06-23 00:19:33 +00:00
|
|
|
if !s.stateGen.StateSummaryExists(ctx, headRoot) {
|
|
|
|
return nil
|
2020-02-09 16:12:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 02:05:43 +00:00
|
|
|
// Get the new head block from DB.
|
2020-02-15 18:57:49 +00:00
|
|
|
newHeadBlock, err := s.beaconDB.Block(ctx, headRoot)
|
2020-02-08 02:05:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-15 18:57:49 +00:00
|
|
|
if newHeadBlock == nil || newHeadBlock.Block == nil {
|
2020-02-08 02:05:43 +00:00
|
|
|
return errors.New("cannot save nil head block")
|
|
|
|
}
|
|
|
|
|
2020-02-09 16:12:06 +00:00
|
|
|
// Get the new head state from cached state or DB.
|
2020-06-23 00:19:33 +00:00
|
|
|
newHeadState, err := s.stateGen.StateByRoot(ctx, headRoot)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not retrieve head state in DB")
|
2020-02-08 02:05:43 +00:00
|
|
|
}
|
2020-02-15 18:57:49 +00:00
|
|
|
if newHeadState == nil {
|
2020-02-09 05:08:21 +00:00
|
|
|
return errors.New("cannot save nil head state")
|
|
|
|
}
|
2020-02-08 02:05:43 +00:00
|
|
|
|
2020-05-06 19:15:31 +00:00
|
|
|
// A chain re-org occurred, so we fire an event notifying the rest of the services.
|
|
|
|
if bytesutil.ToBytes32(newHeadBlock.Block.ParentRoot) != s.headRoot() {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"newSlot": fmt.Sprintf("%d", newHeadBlock.Block.Slot),
|
|
|
|
"oldSlot": fmt.Sprintf("%d", s.headSlot()),
|
|
|
|
}).Debug("Chain reorg occurred")
|
|
|
|
s.stateNotifier.StateFeed().Send(&feed.Event{
|
|
|
|
Type: statefeed.Reorg,
|
|
|
|
Data: &statefeed.ReorgData{
|
|
|
|
NewSlot: newHeadBlock.Block.Slot,
|
|
|
|
OldSlot: s.headSlot(),
|
|
|
|
},
|
|
|
|
})
|
2020-05-12 05:48:16 +00:00
|
|
|
|
|
|
|
reorgCount.Inc()
|
2020-05-06 19:15:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 02:05:43 +00:00
|
|
|
// Cache the new head info.
|
2020-02-15 18:57:49 +00:00
|
|
|
s.setHead(headRoot, newHeadBlock, newHeadState)
|
2020-02-08 02:05:43 +00:00
|
|
|
|
|
|
|
// Save the new head root to DB.
|
|
|
|
if err := s.beaconDB.SaveHeadBlockRoot(ctx, headRoot); err != nil {
|
|
|
|
return errors.Wrap(err, "could not save head root in DB")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-15 18:57:49 +00:00
|
|
|
|
|
|
|
// This gets called to update canonical root mapping. It does not save head block
|
2020-06-23 21:11:20 +00:00
|
|
|
// root in DB. With the inception of initial-sync-cache-state flag, it uses finalized
|
2020-02-15 18:57:49 +00:00
|
|
|
// check point as anchors to resume sync therefore head is no longer needed to be saved on per slot basis.
|
|
|
|
func (s *Service) saveHeadNoDB(ctx context.Context, b *ethpb.SignedBeaconBlock, r [32]byte) error {
|
|
|
|
if b == nil || b.Block == nil {
|
|
|
|
return errors.New("cannot save nil head block")
|
|
|
|
}
|
|
|
|
|
2020-06-23 00:19:33 +00:00
|
|
|
headState, err := s.stateGen.StateByRootInitialSync(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not retrieve head state in DB")
|
2020-02-15 18:57:49 +00:00
|
|
|
}
|
2020-03-17 14:58:16 +00:00
|
|
|
if headState == nil {
|
|
|
|
return errors.New("nil head state")
|
|
|
|
}
|
2020-02-15 18:57:49 +00:00
|
|
|
|
2020-05-06 05:04:02 +00:00
|
|
|
s.setHeadInitialSync(r, stateTrie.CopySignedBeaconBlock(b), headState)
|
2020-02-15 18:57:49 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This sets head view object which is used to track the head slot, root, block and state.
|
|
|
|
func (s *Service) setHead(root [32]byte, block *ethpb.SignedBeaconBlock, state *state.BeaconState) {
|
|
|
|
s.headLock.Lock()
|
|
|
|
defer s.headLock.Unlock()
|
|
|
|
|
|
|
|
// This does a full copy of the block and state.
|
|
|
|
s.head = &head{
|
|
|
|
slot: block.Block.Slot,
|
|
|
|
root: root,
|
|
|
|
block: stateTrie.CopySignedBeaconBlock(block),
|
|
|
|
state: state.Copy(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 05:04:02 +00:00
|
|
|
// This sets head view object which is used to track the head slot, root, block and state. The method
|
|
|
|
// assumes that state being passed into the method will not be modified by any other alternate
|
|
|
|
// caller which holds the state's reference.
|
|
|
|
func (s *Service) setHeadInitialSync(root [32]byte, block *ethpb.SignedBeaconBlock, state *state.BeaconState) {
|
|
|
|
s.headLock.Lock()
|
|
|
|
defer s.headLock.Unlock()
|
|
|
|
|
|
|
|
// This does a full copy of the block only.
|
|
|
|
s.head = &head{
|
|
|
|
slot: block.Block.Slot,
|
|
|
|
root: root,
|
|
|
|
block: stateTrie.CopySignedBeaconBlock(block),
|
|
|
|
state: state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:57:49 +00:00
|
|
|
// This returns the head slot.
|
|
|
|
func (s *Service) headSlot() uint64 {
|
|
|
|
s.headLock.RLock()
|
|
|
|
defer s.headLock.RUnlock()
|
|
|
|
|
|
|
|
return s.head.slot
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns the head root.
|
|
|
|
// It does a full copy on head root for immutability.
|
|
|
|
func (s *Service) headRoot() [32]byte {
|
|
|
|
if s.head == nil {
|
|
|
|
return params.BeaconConfig().ZeroHash
|
|
|
|
}
|
|
|
|
|
|
|
|
s.headLock.RLock()
|
|
|
|
defer s.headLock.RUnlock()
|
|
|
|
|
|
|
|
return s.head.root
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns the head block.
|
|
|
|
// It does a full copy on head block for immutability.
|
|
|
|
func (s *Service) headBlock() *ethpb.SignedBeaconBlock {
|
|
|
|
s.headLock.RLock()
|
|
|
|
defer s.headLock.RUnlock()
|
|
|
|
|
|
|
|
return stateTrie.CopySignedBeaconBlock(s.head.block)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns the head state.
|
|
|
|
// It does a full copy on head state for immutability.
|
2020-06-26 22:52:11 +00:00
|
|
|
func (s *Service) headState(ctx context.Context) *stateTrie.BeaconState {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "blockchain.headState")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-02-15 18:57:49 +00:00
|
|
|
s.headLock.RLock()
|
|
|
|
defer s.headLock.RUnlock()
|
|
|
|
|
|
|
|
return s.head.state.Copy()
|
|
|
|
}
|
|
|
|
|
2020-04-23 03:18:29 +00:00
|
|
|
// This returns the genesis validator root of the head state.
|
|
|
|
func (s *Service) headGenesisValidatorRoot() [32]byte {
|
|
|
|
s.headLock.RLock()
|
|
|
|
defer s.headLock.RUnlock()
|
|
|
|
|
|
|
|
return bytesutil.ToBytes32(s.head.state.GenesisValidatorRoot())
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:57:49 +00:00
|
|
|
// Returns true if head state exists.
|
|
|
|
func (s *Service) hasHeadState() bool {
|
|
|
|
s.headLock.RLock()
|
|
|
|
defer s.headLock.RUnlock()
|
|
|
|
|
|
|
|
return s.head != nil && s.head.state != nil
|
|
|
|
}
|
2020-05-20 19:16:56 +00:00
|
|
|
|
|
|
|
// This updates recent canonical block mapping. It uses input head root and retrieves
|
|
|
|
// all the canonical block roots that are ancestor of the input head block root.
|
|
|
|
func (s *Service) updateRecentCanonicalBlocks(ctx context.Context, headRoot [32]byte) error {
|
2020-06-26 20:08:53 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "blockchain.updateRecentCanonicalBlocks")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-05-20 19:16:56 +00:00
|
|
|
s.recentCanonicalBlocksLock.Lock()
|
|
|
|
defer s.recentCanonicalBlocksLock.Unlock()
|
|
|
|
|
|
|
|
s.recentCanonicalBlocks = make(map[[32]byte]bool)
|
|
|
|
s.recentCanonicalBlocks[headRoot] = true
|
|
|
|
nodes := s.forkChoiceStore.Nodes()
|
|
|
|
node := s.forkChoiceStore.Node(headRoot)
|
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for node.Parent != protoarray.NonExistentNode {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
node = nodes[node.Parent]
|
|
|
|
s.recentCanonicalBlocks[node.Root] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-16 15:10:34 +00:00
|
|
|
|
|
|
|
// This caches justified state balances to be used for fork choice.
|
|
|
|
func (s *Service) cacheJustifiedStateBalances(ctx context.Context, justifiedRoot [32]byte) error {
|
|
|
|
if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-23 00:19:33 +00:00
|
|
|
s.clearInitSyncBlocks()
|
2020-07-06 17:22:12 +00:00
|
|
|
|
|
|
|
var justifiedState *stateTrie.BeaconState
|
|
|
|
var err error
|
|
|
|
if justifiedRoot == s.genesisRoot {
|
|
|
|
justifiedState, err = s.beaconDB.GenesisState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
justifiedState, err = s.stateGen.StateByRoot(ctx, justifiedRoot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if justifiedState == nil {
|
|
|
|
return errors.New("justified state can't be nil")
|
2020-06-16 15:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
epoch := helpers.CurrentEpoch(justifiedState)
|
|
|
|
validators := justifiedState.Validators()
|
|
|
|
justifiedBalances := make([]uint64, len(validators))
|
|
|
|
for i, validator := range validators {
|
|
|
|
if helpers.IsActiveValidator(validator, epoch) {
|
|
|
|
justifiedBalances[i] = validator.EffectiveBalance
|
|
|
|
} else {
|
|
|
|
justifiedBalances[i] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.justifiedBalancesLock.Lock()
|
|
|
|
defer s.justifiedBalancesLock.Unlock()
|
|
|
|
s.justifiedBalances = justifiedBalances
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) getJustifiedBalances() []uint64 {
|
|
|
|
s.justifiedBalancesLock.RLock()
|
|
|
|
defer s.justifiedBalancesLock.RUnlock()
|
|
|
|
return s.justifiedBalances
|
|
|
|
}
|