2020-01-27 21:48:16 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
2020-01-31 20:57:01 +00:00
|
|
|
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2020-12-16 16:56:21 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-02-03 17:41:36 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/attestationutil"
|
2020-07-07 04:16:12 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
2020-01-31 20:57:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-03-16 19:07:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
2020-03-30 18:04:10 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-01-27 21:48:16 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
2020-03-30 18:04:10 +00:00
|
|
|
// This defines size of the upper bound for initial sync block cache.
|
|
|
|
var initialSyncBlockCacheSize = 2 * params.BeaconConfig().SlotsPerEpoch
|
|
|
|
|
2020-01-27 21:48:16 +00:00
|
|
|
// onBlock is called when a gossip block is received. It runs regular state transition on the block.
|
2020-05-09 01:22:59 +00:00
|
|
|
// The block's signing root should be computed before calling this method to avoid redundant
|
|
|
|
// computation in this method and methods it calls into.
|
2020-01-27 21:48:16 +00:00
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
2020-07-25 04:55:42 +00:00
|
|
|
// def on_block(store: Store, signed_block: SignedBeaconBlock) -> None:
|
|
|
|
// block = signed_block.message
|
|
|
|
// # Parent block must be known
|
2020-01-27 21:48:16 +00:00
|
|
|
// assert block.parent_root in store.block_states
|
2020-07-25 04:55:42 +00:00
|
|
|
// # Make a copy of the state to avoid mutability issues
|
|
|
|
// pre_state = copy(store.block_states[block.parent_root])
|
2020-01-27 21:48:16 +00:00
|
|
|
// # Blocks cannot be in the future. If they are, their consideration must be delayed until the are in the past.
|
2020-07-25 04:55:42 +00:00
|
|
|
// assert get_current_slot(store) >= block.slot
|
|
|
|
//
|
|
|
|
// # Check that block is later than the finalized epoch slot (optimization to reduce calls to get_ancestor)
|
|
|
|
// finalized_slot = compute_start_slot_at_epoch(store.finalized_checkpoint.epoch)
|
|
|
|
// assert block.slot > finalized_slot
|
|
|
|
// # Check block is a descendant of the finalized block at the checkpoint finalized slot
|
|
|
|
// assert get_ancestor(store, block.parent_root, finalized_slot) == store.finalized_checkpoint.root
|
|
|
|
//
|
2020-01-27 21:48:16 +00:00
|
|
|
// # Check the block is valid and compute the post-state
|
2020-07-25 04:55:42 +00:00
|
|
|
// state = state_transition(pre_state, signed_block, True)
|
|
|
|
// # Add new block to the store
|
|
|
|
// store.blocks[hash_tree_root(block)] = block
|
2020-01-27 21:48:16 +00:00
|
|
|
// # Add new state for this block to the store
|
2020-07-25 04:55:42 +00:00
|
|
|
// store.block_states[hash_tree_root(block)] = state
|
2020-01-27 21:48:16 +00:00
|
|
|
//
|
|
|
|
// # Update justified checkpoint
|
|
|
|
// if state.current_justified_checkpoint.epoch > store.justified_checkpoint.epoch:
|
|
|
|
// if state.current_justified_checkpoint.epoch > store.best_justified_checkpoint.epoch:
|
|
|
|
// store.best_justified_checkpoint = state.current_justified_checkpoint
|
2020-07-25 04:55:42 +00:00
|
|
|
// if should_update_justified_checkpoint(store, state.current_justified_checkpoint):
|
|
|
|
// store.justified_checkpoint = state.current_justified_checkpoint
|
2020-01-27 21:48:16 +00:00
|
|
|
//
|
|
|
|
// # Update finalized checkpoint
|
|
|
|
// if state.finalized_checkpoint.epoch > store.finalized_checkpoint.epoch:
|
|
|
|
// store.finalized_checkpoint = state.finalized_checkpoint
|
2020-07-25 04:55:42 +00:00
|
|
|
//
|
|
|
|
// # Potentially update justified if different from store
|
|
|
|
// if store.justified_checkpoint != state.current_justified_checkpoint:
|
|
|
|
// # Update justified if new justified is later than store justified
|
|
|
|
// if state.current_justified_checkpoint.epoch > store.justified_checkpoint.epoch:
|
|
|
|
// store.justified_checkpoint = state.current_justified_checkpoint
|
|
|
|
// return
|
|
|
|
//
|
|
|
|
// # Update justified if store justified is not in chain with finalized checkpoint
|
|
|
|
// finalized_slot = compute_start_slot_at_epoch(store.finalized_checkpoint.epoch)
|
|
|
|
// ancestor_at_finalized_slot = get_ancestor(store, store.justified_checkpoint.root, finalized_slot)
|
|
|
|
// if ancestor_at_finalized_slot != store.finalized_checkpoint.root:
|
|
|
|
// store.justified_checkpoint = state.current_justified_checkpoint
|
2020-07-09 23:50:48 +00:00
|
|
|
func (s *Service) onBlock(ctx context.Context, signed *ethpb.SignedBeaconBlock, blockRoot [32]byte) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "blockChain.onBlock")
|
2020-01-27 21:48:16 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
if signed == nil || signed.Block == nil {
|
2020-07-09 23:50:48 +00:00
|
|
|
return errors.New("nil block")
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
b := signed.Block
|
|
|
|
|
|
|
|
preState, err := s.getBlockPreState(ctx, b)
|
|
|
|
if err != nil {
|
2020-07-09 23:50:48 +00:00
|
|
|
return err
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 23:27:29 +00:00
|
|
|
set, postState, err := state.ExecuteStateTransitionNoVerifyAnySig(ctx, preState, signed)
|
2020-01-27 21:48:16 +00:00
|
|
|
if err != nil {
|
2020-07-09 23:50:48 +00:00
|
|
|
return errors.Wrap(err, "could not execute state transition")
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
2020-11-02 23:27:29 +00:00
|
|
|
valid, err := set.Verify()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not batch verify signature")
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
return errors.New("signature in block failed to verify")
|
|
|
|
}
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2020-07-13 03:44:06 +00:00
|
|
|
if err := s.savePostStateInfo(ctx, blockRoot, signed, postState, false /* reg sync */); err != nil {
|
|
|
|
return err
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update justified check point.
|
2020-01-31 20:57:01 +00:00
|
|
|
if postState.CurrentJustifiedCheckpoint().Epoch > s.justifiedCheckpt.Epoch {
|
2020-01-27 21:48:16 +00:00
|
|
|
if err := s.updateJustified(ctx, postState); err != nil {
|
2020-07-09 23:50:48 +00:00
|
|
|
return err
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:50:48 +00:00
|
|
|
// Update finalized check point.
|
2020-02-06 03:26:15 +00:00
|
|
|
if postState.FinalizedCheckpointEpoch() > s.finalizedCheckpt.Epoch {
|
2020-06-25 20:55:43 +00:00
|
|
|
if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
|
2020-07-09 23:50:48 +00:00
|
|
|
return err
|
2020-04-04 23:25:04 +00:00
|
|
|
}
|
2020-06-25 20:55:43 +00:00
|
|
|
s.clearInitSyncBlocks()
|
2020-04-04 23:25:04 +00:00
|
|
|
|
2020-07-09 23:50:48 +00:00
|
|
|
if err := s.updateFinalized(ctx, postState.FinalizedCheckpoint()); err != nil {
|
|
|
|
return err
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 19:07:07 +00:00
|
|
|
fRoot := bytesutil.ToBytes32(postState.FinalizedCheckpoint().Root)
|
2020-04-13 04:11:09 +00:00
|
|
|
if err := s.forkChoiceStore.Prune(ctx, fRoot); err != nil {
|
2020-07-09 23:50:48 +00:00
|
|
|
return errors.Wrap(err, "could not prune proto array fork choice nodes")
|
2020-04-13 04:11:09 +00:00
|
|
|
}
|
2020-01-31 20:57:01 +00:00
|
|
|
|
2020-02-04 17:35:06 +00:00
|
|
|
if err := s.finalizedImpliesNewJustified(ctx, postState); err != nil {
|
2020-07-09 23:50:48 +00:00
|
|
|
return errors.Wrap(err, "could not save new justified")
|
2020-02-04 17:35:06 +00:00
|
|
|
}
|
2020-03-16 19:07:07 +00:00
|
|
|
|
2020-07-09 20:24:40 +00:00
|
|
|
// Update deposit cache.
|
2020-10-08 10:12:21 +00:00
|
|
|
finalizedState, err := s.stateGen.StateByRoot(ctx, fRoot)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch finalized state")
|
|
|
|
}
|
2020-10-12 18:10:49 +00:00
|
|
|
// We update the cache up to the last deposit index in the finalized block's state.
|
|
|
|
// We can be confident that these deposits will be included in some block
|
|
|
|
// because the Eth1 follow distance makes such long-range reorgs extremely unlikely.
|
|
|
|
eth1DepositIndex := int64(finalizedState.Eth1Data().DepositCount - 1)
|
|
|
|
s.depositCache.InsertFinalizedDeposits(ctx, eth1DepositIndex)
|
|
|
|
if featureconfig.Get().EnablePruningDepositProofs {
|
|
|
|
// Deposit proofs are only used during state transition and can be safely removed to save space.
|
|
|
|
if err = s.depositCache.PruneProofs(ctx, eth1DepositIndex); err != nil {
|
|
|
|
return errors.Wrap(err, "could not prune deposit proofs")
|
|
|
|
}
|
|
|
|
}
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 02:00:42 +00:00
|
|
|
defer reportAttestationInclusion(b)
|
|
|
|
|
2020-10-20 19:14:24 +00:00
|
|
|
return s.handleEpochBoundary(ctx, postState)
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// onBlockInitialSyncStateTransition is called when an initial sync block is received.
|
2020-07-13 03:44:06 +00:00
|
|
|
// It runs state transition on the block and without fork choice and post operation pool processes.
|
2020-05-09 01:22:59 +00:00
|
|
|
// The block's signing root should be computed before calling this method to avoid redundant
|
|
|
|
// computation in this method and methods it calls into.
|
|
|
|
func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed *ethpb.SignedBeaconBlock, blockRoot [32]byte) error {
|
2020-07-13 03:44:06 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "blockChain.onBlockInitialSyncStateTransition")
|
2020-01-27 21:48:16 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
if signed == nil || signed.Block == nil {
|
2020-02-06 19:03:26 +00:00
|
|
|
return errors.New("nil block")
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b := signed.Block
|
|
|
|
|
|
|
|
// Retrieve incoming block's pre state.
|
2020-07-08 02:54:55 +00:00
|
|
|
if err := s.verifyBlkPreState(ctx, b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
preState, err := s.stateGen.StateByRootInitialSync(ctx, bytesutil.ToBytes32(signed.Block.ParentRoot))
|
2020-01-27 21:48:16 +00:00
|
|
|
if err != nil {
|
2020-02-06 19:03:26 +00:00
|
|
|
return err
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
2020-07-08 02:54:55 +00:00
|
|
|
if preState == nil {
|
|
|
|
return fmt.Errorf("nil pre state for slot %d", b.Slot)
|
|
|
|
}
|
|
|
|
|
2020-03-16 19:07:07 +00:00
|
|
|
// Exit early if the pre state slot is higher than incoming block's slot.
|
|
|
|
if preState.Slot() >= signed.Block.Slot {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-13 06:08:21 +00:00
|
|
|
postState, err := state.ExecuteStateTransition(ctx, preState, signed)
|
2020-01-27 21:48:16 +00:00
|
|
|
if err != nil {
|
2020-02-06 19:03:26 +00:00
|
|
|
return errors.Wrap(err, "could not execute state transition")
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
2020-07-13 03:44:06 +00:00
|
|
|
|
|
|
|
if err := s.savePostStateInfo(ctx, blockRoot, signed, postState, true /* init sync */); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-27 00:10:35 +00:00
|
|
|
// Save the latest block as head in cache.
|
|
|
|
if err := s.saveHeadNoDB(ctx, signed, blockRoot, postState); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-13 03:44:06 +00:00
|
|
|
|
|
|
|
// Rate limit how many blocks (2 epochs worth of blocks) a node keeps in the memory.
|
|
|
|
if uint64(len(s.getInitSyncBlocks())) > initialSyncBlockCacheSize {
|
|
|
|
if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.clearInitSyncBlocks()
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:20:25 +00:00
|
|
|
if postState.CurrentJustifiedCheckpoint().Epoch > s.justifiedCheckpt.Epoch {
|
|
|
|
if err := s.updateJustifiedInitSync(ctx, postState.CurrentJustifiedCheckpoint()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 03:44:06 +00:00
|
|
|
// Update finalized check point. Prune the block cache and helper caches on every new finalized epoch.
|
|
|
|
if postState.FinalizedCheckpointEpoch() > s.finalizedCheckpt.Epoch {
|
|
|
|
if err := s.updateFinalized(ctx, postState.FinalizedCheckpoint()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 19:14:24 +00:00
|
|
|
return s.handleEpochBoundary(ctx, postState)
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2020-07-07 04:16:12 +00:00
|
|
|
func (s *Service) onBlockBatch(ctx context.Context, blks []*ethpb.SignedBeaconBlock,
|
2020-07-20 19:35:30 +00:00
|
|
|
blockRoots [][32]byte) ([]*ethpb.Checkpoint, []*ethpb.Checkpoint, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "blockChain.onBlockBatch")
|
2020-07-07 04:16:12 +00:00
|
|
|
defer span.End()
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2020-07-07 04:16:12 +00:00
|
|
|
if len(blks) == 0 || len(blockRoots) == 0 {
|
2020-07-20 19:35:30 +00:00
|
|
|
return nil, nil, errors.New("no blocks provided")
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
|
|
|
if blks[0] == nil || blks[0].Block == nil {
|
2020-07-20 19:35:30 +00:00
|
|
|
return nil, nil, errors.New("nil block")
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
|
|
|
b := blks[0].Block
|
|
|
|
|
|
|
|
// Retrieve incoming block's pre state.
|
2020-07-08 02:54:55 +00:00
|
|
|
if err := s.verifyBlkPreState(ctx, b); err != nil {
|
2020-07-20 19:35:30 +00:00
|
|
|
return nil, nil, err
|
2020-07-08 02:54:55 +00:00
|
|
|
}
|
|
|
|
preState, err := s.stateGen.StateByRootInitialSync(ctx, bytesutil.ToBytes32(b.ParentRoot))
|
2020-07-07 04:16:12 +00:00
|
|
|
if err != nil {
|
2020-07-20 19:35:30 +00:00
|
|
|
return nil, nil, err
|
2020-02-03 17:41:36 +00:00
|
|
|
}
|
2020-07-08 02:54:55 +00:00
|
|
|
if preState == nil {
|
2020-07-20 19:35:30 +00:00
|
|
|
return nil, nil, fmt.Errorf("nil pre state for slot %d", b.Slot)
|
2020-07-08 02:54:55 +00:00
|
|
|
}
|
2020-07-07 04:16:12 +00:00
|
|
|
|
|
|
|
jCheckpoints := make([]*ethpb.Checkpoint, len(blks))
|
|
|
|
fCheckpoints := make([]*ethpb.Checkpoint, len(blks))
|
|
|
|
sigSet := &bls.SignatureSet{
|
2020-09-16 13:28:28 +00:00
|
|
|
Signatures: [][]byte{},
|
2020-07-07 04:16:12 +00:00
|
|
|
PublicKeys: []bls.PublicKey{},
|
|
|
|
Messages: [][32]byte{},
|
|
|
|
}
|
2020-10-01 18:53:36 +00:00
|
|
|
var set *bls.SignatureSet
|
2020-07-10 16:00:41 +00:00
|
|
|
boundaries := make(map[[32]byte]*stateTrie.BeaconState)
|
2020-07-07 04:16:12 +00:00
|
|
|
for i, b := range blks {
|
|
|
|
set, preState, err = state.ExecuteStateTransitionNoVerifyAnySig(ctx, preState, b)
|
|
|
|
if err != nil {
|
2020-07-20 19:35:30 +00:00
|
|
|
return nil, nil, err
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
2020-07-10 16:00:41 +00:00
|
|
|
// Save potential boundary states.
|
|
|
|
if helpers.IsEpochStart(preState.Slot()) {
|
|
|
|
boundaries[blockRoots[i]] = preState.Copy()
|
2020-10-20 19:14:24 +00:00
|
|
|
if err := s.handleEpochBoundary(ctx, preState); err != nil {
|
2020-10-07 17:25:08 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "could not handle epoch boundary state")
|
2020-07-20 19:35:30 +00:00
|
|
|
}
|
2020-07-10 16:00:41 +00:00
|
|
|
}
|
2020-07-07 04:16:12 +00:00
|
|
|
jCheckpoints[i] = preState.CurrentJustifiedCheckpoint()
|
|
|
|
fCheckpoints[i] = preState.FinalizedCheckpoint()
|
|
|
|
sigSet.Join(set)
|
|
|
|
}
|
2020-09-16 13:28:28 +00:00
|
|
|
verify, err := sigSet.Verify()
|
2020-07-07 04:16:12 +00:00
|
|
|
if err != nil {
|
2020-07-20 19:35:30 +00:00
|
|
|
return nil, nil, err
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
|
|
|
if !verify {
|
2020-07-20 19:35:30 +00:00
|
|
|
return nil, nil, errors.New("batch block signature verification failed")
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
2020-07-10 16:00:41 +00:00
|
|
|
for r, st := range boundaries {
|
|
|
|
if err := s.stateGen.SaveState(ctx, r, st); err != nil {
|
2020-07-20 19:35:30 +00:00
|
|
|
return nil, nil, err
|
2020-07-10 16:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 19:35:30 +00:00
|
|
|
// Also saves the last post state which to be used as pre state for the next batch.
|
2020-07-27 00:10:35 +00:00
|
|
|
lastB := blks[len(blks)-1]
|
|
|
|
lastBR := blockRoots[len(blockRoots)-1]
|
|
|
|
if err := s.stateGen.SaveState(ctx, lastBR, preState); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := s.saveHeadNoDB(ctx, lastB, lastBR, preState); err != nil {
|
2020-07-20 19:35:30 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return fCheckpoints, jCheckpoints, nil
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handles a block after the block's batch has been verified, where we can save blocks
|
|
|
|
// their state summaries and split them off to relative hot/cold storage.
|
|
|
|
func (s *Service) handleBlockAfterBatchVerify(ctx context.Context, signed *ethpb.SignedBeaconBlock,
|
2020-10-12 15:43:19 +00:00
|
|
|
blockRoot [32]byte, fCheckpoint, jCheckpoint *ethpb.Checkpoint) error {
|
2020-07-07 04:16:12 +00:00
|
|
|
b := signed.Block
|
|
|
|
|
|
|
|
s.saveInitSyncBlock(blockRoot, signed)
|
|
|
|
if err := s.insertBlockToForkChoiceStore(ctx, b, blockRoot, fCheckpoint, jCheckpoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-16 16:56:21 +00:00
|
|
|
if err := s.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
|
|
|
|
Slot: signed.Block.Slot,
|
|
|
|
Root: blockRoot[:],
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-07 04:16:12 +00:00
|
|
|
|
|
|
|
// Rate limit how many blocks (2 epochs worth of blocks) a node keeps in the memory.
|
|
|
|
if uint64(len(s.getInitSyncBlocks())) > initialSyncBlockCacheSize {
|
|
|
|
if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.clearInitSyncBlocks()
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:20:25 +00:00
|
|
|
if jCheckpoint.Epoch > s.justifiedCheckpt.Epoch {
|
|
|
|
if err := s.updateJustifiedInitSync(ctx, jCheckpoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 04:16:12 +00:00
|
|
|
// Update finalized check point. Prune the block cache and helper caches on every new finalized epoch.
|
|
|
|
if fCheckpoint.Epoch > s.finalizedCheckpt.Epoch {
|
2020-07-09 23:50:48 +00:00
|
|
|
if err := s.updateFinalized(ctx, fCheckpoint); err != nil {
|
|
|
|
return err
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Epoch boundary bookkeeping such as logging epoch summaries.
|
2020-10-20 19:14:24 +00:00
|
|
|
func (s *Service) handleEpochBoundary(ctx context.Context, postState *stateTrie.BeaconState) error {
|
2020-12-17 14:31:05 +00:00
|
|
|
if postState.Slot()+1 == s.nextEpochBoundarySlot {
|
2020-12-15 21:19:02 +00:00
|
|
|
// Update caches for the next epoch at epoch boundary slot - 1.
|
|
|
|
if err := helpers.UpdateCommitteeCache(postState, helpers.NextEpoch(postState)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := helpers.UpdateProposerIndicesInCache(postState, helpers.NextEpoch(postState)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if postState.Slot() >= s.nextEpochBoundarySlot {
|
2020-10-20 19:14:24 +00:00
|
|
|
if err := reportEpochMetrics(ctx, postState, s.head.state); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-02 02:52:36 +00:00
|
|
|
var err error
|
|
|
|
s.nextEpochBoundarySlot, err = helpers.StartSlot(helpers.NextEpoch(postState))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-15 21:19:02 +00:00
|
|
|
|
|
|
|
// Update caches at epoch boundary slot.
|
|
|
|
// The following updates have short cut to return nil cheaply if fulfilled during boundary slot - 1.
|
2020-01-27 21:48:16 +00:00
|
|
|
if err := helpers.UpdateCommitteeCache(postState, helpers.CurrentEpoch(postState)); err != nil {
|
2020-02-06 19:03:26 +00:00
|
|
|
return err
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
if err := helpers.UpdateProposerIndicesInCache(postState, helpers.CurrentEpoch(postState)); err != nil {
|
2020-02-06 19:03:26 +00:00
|
|
|
return err
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-15 21:19:02 +00:00
|
|
|
|
2020-02-06 19:03:26 +00:00
|
|
|
return nil
|
2020-01-27 21:48:16 +00:00
|
|
|
}
|
2020-02-03 17:41:36 +00:00
|
|
|
|
|
|
|
// This feeds in the block and block's attestations to fork choice store. It's allows fork choice store
|
|
|
|
// to gain information on the most current chain.
|
2020-07-07 04:16:12 +00:00
|
|
|
func (s *Service) insertBlockAndAttestationsToForkChoiceStore(ctx context.Context, blk *ethpb.BeaconBlock, root [32]byte,
|
2021-01-04 20:07:12 +00:00
|
|
|
st *stateTrie.BeaconState) error {
|
|
|
|
fCheckpoint := st.FinalizedCheckpoint()
|
|
|
|
jCheckpoint := st.CurrentJustifiedCheckpoint()
|
2020-07-07 04:16:12 +00:00
|
|
|
if err := s.insertBlockToForkChoiceStore(ctx, blk, root, fCheckpoint, jCheckpoint); err != nil {
|
2020-02-05 17:05:51 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-02-03 17:41:36 +00:00
|
|
|
// Feed in block's attestations to fork choice store.
|
|
|
|
for _, a := range blk.Body.Attestations {
|
2021-01-04 20:07:12 +00:00
|
|
|
committee, err := helpers.BeaconCommitteeFromState(st, a.Data.Slot, a.Data.CommitteeIndex)
|
2020-02-03 17:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-20 03:00:52 +00:00
|
|
|
indices, err := attestationutil.AttestingIndices(a.AggregationBits, committee)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 17:41:36 +00:00
|
|
|
s.forkChoiceStore.ProcessAttestation(ctx, indices, bytesutil.ToBytes32(a.Data.BeaconBlockRoot), a.Data.Target.Epoch)
|
|
|
|
}
|
2020-07-07 04:16:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-02-03 17:41:36 +00:00
|
|
|
|
2020-07-07 04:16:12 +00:00
|
|
|
func (s *Service) insertBlockToForkChoiceStore(ctx context.Context, blk *ethpb.BeaconBlock,
|
2020-10-12 15:43:19 +00:00
|
|
|
root [32]byte, fCheckpoint, jCheckpoint *ethpb.Checkpoint) error {
|
2020-07-07 04:16:12 +00:00
|
|
|
if err := s.fillInForkChoiceMissingBlocks(ctx, blk, fCheckpoint, jCheckpoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Feed in block to fork choice store.
|
|
|
|
if err := s.forkChoiceStore.ProcessBlock(ctx,
|
|
|
|
blk.Slot, root, bytesutil.ToBytes32(blk.ParentRoot), bytesutil.ToBytes32(blk.Body.Graffiti),
|
|
|
|
jCheckpoint.Epoch,
|
|
|
|
fCheckpoint.Epoch); err != nil {
|
|
|
|
return errors.Wrap(err, "could not process block for proto array fork choice")
|
|
|
|
}
|
2020-02-03 17:41:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-13 03:44:06 +00:00
|
|
|
|
|
|
|
// This saves post state info to DB or cache. This also saves post state info to fork choice store.
|
|
|
|
// Post state info consists of processed block and state. Do not call this method unless the block and state are verified.
|
2021-01-04 20:07:12 +00:00
|
|
|
func (s *Service) savePostStateInfo(ctx context.Context, r [32]byte, b *ethpb.SignedBeaconBlock, st *stateTrie.BeaconState, initSync bool) error {
|
2020-07-13 03:44:06 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "blockChain.savePostStateInfo")
|
|
|
|
defer span.End()
|
|
|
|
if initSync {
|
|
|
|
s.saveInitSyncBlock(r, b)
|
2020-08-10 16:16:45 +00:00
|
|
|
} else if err := s.beaconDB.SaveBlock(ctx, b); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not save block from slot %d", b.Block.Slot)
|
2020-07-13 03:44:06 +00:00
|
|
|
}
|
2021-01-04 20:07:12 +00:00
|
|
|
if err := s.stateGen.SaveState(ctx, r, st); err != nil {
|
2020-07-13 03:44:06 +00:00
|
|
|
return errors.Wrap(err, "could not save state")
|
|
|
|
}
|
2021-01-04 20:07:12 +00:00
|
|
|
if err := s.insertBlockAndAttestationsToForkChoiceStore(ctx, b.Block, r, st); err != nil {
|
2020-07-13 03:44:06 +00:00
|
|
|
return errors.Wrapf(err, "could not insert block %d to fork choice store", b.Block.Slot)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|