mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 21:38:05 -05:00
* beacon block is nil wrapper * gaz Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
696 lines
25 KiB
Go
696 lines
25 KiB
Go
package blockchain
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
|
|
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
coreTime "github.com/prysmaticlabs/prysm/beacon-chain/core/time"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/transition"
|
|
forkchoicetypes "github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/types"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
|
"github.com/prysmaticlabs/prysm/config/features"
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
|
"github.com/prysmaticlabs/prysm/consensus-types/interfaces"
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
|
"github.com/prysmaticlabs/prysm/consensus-types/wrapper"
|
|
"github.com/prysmaticlabs/prysm/crypto/bls"
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/monitoring/tracing"
|
|
ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/attestation"
|
|
"github.com/prysmaticlabs/prysm/runtime/version"
|
|
"github.com/prysmaticlabs/prysm/time/slots"
|
|
"go.opencensus.io/trace"
|
|
)
|
|
|
|
// A custom slot deadline for processing state slots in our cache.
|
|
const slotDeadline = 5 * time.Second
|
|
|
|
// A custom deadline for deposit trie insertion.
|
|
const depositDeadline = 20 * time.Second
|
|
|
|
// This defines size of the upper bound for initial sync block cache.
|
|
var initialSyncBlockCacheSize = uint64(2 * params.BeaconConfig().SlotsPerEpoch)
|
|
|
|
// onBlock is called when a gossip block is received. It runs regular state transition on the block.
|
|
// The block's signing root should be computed before calling this method to avoid redundant
|
|
// computation in this method and methods it calls into.
|
|
//
|
|
// Spec pseudocode definition:
|
|
// def on_block(store: Store, signed_block: SignedBeaconBlock) -> None:
|
|
// block = signed_block.message
|
|
// # Parent block must be known
|
|
// assert block.parent_root in store.block_states
|
|
// # Make a copy of the state to avoid mutability issues
|
|
// pre_state = copy(store.block_states[block.parent_root])
|
|
// # Blocks cannot be in the future. If they are, their consideration must be delayed until the are in the past.
|
|
// 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
|
|
//
|
|
// # Check the block is valid and compute the post-state
|
|
// state = pre_state.copy()
|
|
// state_transition(state, signed_block, True)
|
|
// # Add new block to the store
|
|
// store.blocks[hash_tree_root(block)] = block
|
|
// # Add new state for this block to the store
|
|
// store.block_states[hash_tree_root(block)] = state
|
|
//
|
|
// # 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
|
|
// if should_update_justified_checkpoint(store, state.current_justified_checkpoint):
|
|
// store.justified_checkpoint = state.current_justified_checkpoint
|
|
//
|
|
// # Update finalized checkpoint
|
|
// if state.finalized_checkpoint.epoch > store.finalized_checkpoint.epoch:
|
|
// store.finalized_checkpoint = state.finalized_checkpoint
|
|
//
|
|
// # 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
|
|
func (s *Service) onBlock(ctx context.Context, signed interfaces.SignedBeaconBlock, blockRoot [32]byte) error {
|
|
ctx, span := trace.StartSpan(ctx, "blockChain.onBlock")
|
|
defer span.End()
|
|
if err := wrapper.BeaconBlockIsNil(signed); err != nil {
|
|
return invalidBlock{err}
|
|
}
|
|
b := signed.Block()
|
|
|
|
preState, err := s.getBlockPreState(ctx, b)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
preStateVersion, preStateHeader, err := getStateVersionAndPayload(preState)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
postState, err := transition.ExecuteStateTransition(ctx, preState, signed)
|
|
if err != nil {
|
|
return invalidBlock{err}
|
|
}
|
|
postStateVersion, postStateHeader, err := getStateVersionAndPayload(postState)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
isValidPayload, err := s.notifyNewPayload(ctx, postStateVersion, postStateHeader, signed)
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not verify new payload")
|
|
}
|
|
if isValidPayload {
|
|
if err := s.validateMergeTransitionBlock(ctx, preStateVersion, preStateHeader, signed); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := s.savePostStateInfo(ctx, blockRoot, signed, postState); err != nil {
|
|
return err
|
|
}
|
|
if err := s.insertBlockAndAttestationsToForkChoiceStore(ctx, signed.Block(), blockRoot, postState); err != nil {
|
|
return errors.Wrapf(err, "could not insert block %d to fork choice store", signed.Block().Slot())
|
|
}
|
|
s.insertSlashingsToForkChoiceStore(ctx, signed.Block().Body().AttesterSlashings())
|
|
if isValidPayload {
|
|
if err := s.cfg.ForkChoiceStore.SetOptimisticToValid(ctx, blockRoot); err != nil {
|
|
return errors.Wrap(err, "could not set optimistic block to valid")
|
|
}
|
|
}
|
|
|
|
// We add a proposer score boost to fork choice for the block root if applicable, right after
|
|
// running a successful state transition for the block.
|
|
secondsIntoSlot := uint64(time.Since(s.genesisTime).Seconds()) % params.BeaconConfig().SecondsPerSlot
|
|
if err := s.cfg.ForkChoiceStore.BoostProposerRoot(ctx, &forkchoicetypes.ProposerBoostRootArgs{
|
|
BlockRoot: blockRoot,
|
|
BlockSlot: signed.Block().Slot(),
|
|
CurrentSlot: slots.SinceGenesis(s.genesisTime),
|
|
SecondsIntoSlot: secondsIntoSlot,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
// If slasher is configured, forward the attestations in the block via
|
|
// an event feed for processing.
|
|
if features.Get().EnableSlasher {
|
|
// Feed the indexed attestation to slasher if enabled. This action
|
|
// is done in the background to avoid adding more load to this critical code path.
|
|
go func() {
|
|
// Using a different context to prevent timeouts as this operation can be expensive
|
|
// and we want to avoid affecting the critical code path.
|
|
ctx := context.TODO()
|
|
for _, att := range signed.Block().Body().Attestations() {
|
|
committee, err := helpers.BeaconCommitteeFromState(ctx, preState, att.Data.Slot, att.Data.CommitteeIndex)
|
|
if err != nil {
|
|
log.WithError(err).Error("Could not get attestation committee")
|
|
tracing.AnnotateError(span, err)
|
|
return
|
|
}
|
|
indexedAtt, err := attestation.ConvertToIndexed(ctx, att, committee)
|
|
if err != nil {
|
|
log.WithError(err).Error("Could not convert to indexed attestation")
|
|
tracing.AnnotateError(span, err)
|
|
return
|
|
}
|
|
s.cfg.SlasherAttestationsFeed.Send(indexedAtt)
|
|
}
|
|
}()
|
|
}
|
|
|
|
// Update justified check point.
|
|
justified, err := s.store.JustifiedCheckpt()
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not get justified checkpoint")
|
|
}
|
|
currJustifiedEpoch := justified.Epoch
|
|
if postState.CurrentJustifiedCheckpoint().Epoch > currJustifiedEpoch {
|
|
if err := s.updateJustified(ctx, postState); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
finalized, err := s.store.FinalizedCheckpt()
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not get finalized checkpoint")
|
|
}
|
|
if finalized == nil {
|
|
return errNilFinalizedInStore
|
|
}
|
|
newFinalized := postState.FinalizedCheckpointEpoch() > finalized.Epoch
|
|
if newFinalized {
|
|
s.store.SetPrevFinalizedCheckpt(finalized)
|
|
cp := postState.FinalizedCheckpoint()
|
|
h, err := s.getPayloadHash(ctx, cp.Root)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.store.SetFinalizedCheckptAndPayloadHash(cp, h)
|
|
s.store.SetPrevJustifiedCheckpt(justified)
|
|
cp = postState.CurrentJustifiedCheckpoint()
|
|
h, err = s.getPayloadHash(ctx, cp.Root)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.store.SetJustifiedCheckptAndPayloadHash(postState.CurrentJustifiedCheckpoint(), h)
|
|
}
|
|
|
|
balances, err := s.justifiedBalances.get(ctx, bytesutil.ToBytes32(justified.Root))
|
|
if err != nil {
|
|
msg := fmt.Sprintf("could not read balances for state w/ justified checkpoint %#x", justified.Root)
|
|
return errors.Wrap(err, msg)
|
|
}
|
|
headRoot, err := s.updateHead(ctx, balances)
|
|
if err != nil {
|
|
log.WithError(err).Warn("Could not update head")
|
|
}
|
|
s.notifyEngineIfChangedHead(ctx, headRoot)
|
|
|
|
if err := s.pruneCanonicalAttsFromPool(ctx, blockRoot, signed); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Send notification of the processed block to the state feed.
|
|
s.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
|
Type: statefeed.BlockProcessed,
|
|
Data: &statefeed.BlockProcessedData{
|
|
Slot: signed.Block().Slot(),
|
|
BlockRoot: blockRoot,
|
|
SignedBlock: signed,
|
|
Verified: true,
|
|
},
|
|
})
|
|
|
|
// Updating next slot state cache can happen in the background. It shouldn't block rest of the process.
|
|
go func() {
|
|
// Use a custom deadline here, since this method runs asynchronously.
|
|
// We ignore the parent method's context and instead create a new one
|
|
// with a custom deadline, therefore using the background context instead.
|
|
slotCtx, cancel := context.WithTimeout(context.Background(), slotDeadline)
|
|
defer cancel()
|
|
if err := transition.UpdateNextSlotCache(slotCtx, blockRoot[:], postState); err != nil {
|
|
log.WithError(err).Debug("could not update next slot state cache")
|
|
}
|
|
}()
|
|
|
|
// Save justified check point to db.
|
|
if postState.CurrentJustifiedCheckpoint().Epoch > currJustifiedEpoch {
|
|
if err := s.cfg.BeaconDB.SaveJustifiedCheckpoint(ctx, postState.CurrentJustifiedCheckpoint()); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Update finalized check point.
|
|
if newFinalized {
|
|
if err := s.updateFinalized(ctx, postState.FinalizedCheckpoint()); err != nil {
|
|
return err
|
|
}
|
|
fRoot := bytesutil.ToBytes32(postState.FinalizedCheckpoint().Root)
|
|
if err := s.cfg.ForkChoiceStore.Prune(ctx, fRoot); err != nil {
|
|
return errors.Wrap(err, "could not prune proto array fork choice nodes")
|
|
}
|
|
isOptimistic, err := s.cfg.ForkChoiceStore.IsOptimistic(fRoot)
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not check if node is optimistically synced")
|
|
}
|
|
go func() {
|
|
// Send an event regarding the new finalized checkpoint over a common event feed.
|
|
s.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
|
Type: statefeed.FinalizedCheckpoint,
|
|
Data: ðpbv1.EventFinalizedCheckpoint{
|
|
Epoch: postState.FinalizedCheckpoint().Epoch,
|
|
Block: postState.FinalizedCheckpoint().Root,
|
|
State: signed.Block().StateRoot(),
|
|
ExecutionOptimistic: isOptimistic,
|
|
},
|
|
})
|
|
|
|
// Use a custom deadline here, since this method runs asynchronously.
|
|
// We ignore the parent method's context and instead create a new one
|
|
// with a custom deadline, therefore using the background context instead.
|
|
depCtx, cancel := context.WithTimeout(context.Background(), depositDeadline)
|
|
defer cancel()
|
|
if err := s.insertFinalizedDeposits(depCtx, fRoot); err != nil {
|
|
log.WithError(err).Error("Could not insert finalized deposits.")
|
|
}
|
|
}()
|
|
|
|
}
|
|
|
|
defer reportAttestationInclusion(b)
|
|
|
|
return s.handleEpochBoundary(ctx, postState)
|
|
}
|
|
|
|
func getStateVersionAndPayload(st state.BeaconState) (int, *ethpb.ExecutionPayloadHeader, error) {
|
|
if st == nil {
|
|
return 0, nil, errors.New("nil state")
|
|
}
|
|
var preStateHeader *ethpb.ExecutionPayloadHeader
|
|
var err error
|
|
preStateVersion := st.Version()
|
|
switch preStateVersion {
|
|
case version.Phase0, version.Altair:
|
|
default:
|
|
preStateHeader, err = st.LatestExecutionPayloadHeader()
|
|
if err != nil {
|
|
return 0, nil, err
|
|
}
|
|
}
|
|
return preStateVersion, preStateHeader, nil
|
|
}
|
|
|
|
func (s *Service) onBlockBatch(ctx context.Context, blks []interfaces.SignedBeaconBlock,
|
|
blockRoots [][32]byte) ([]*ethpb.Checkpoint, []*ethpb.Checkpoint, error) {
|
|
ctx, span := trace.StartSpan(ctx, "blockChain.onBlockBatch")
|
|
defer span.End()
|
|
|
|
if len(blks) == 0 || len(blockRoots) == 0 {
|
|
return nil, nil, errors.New("no blocks provided")
|
|
}
|
|
|
|
if len(blks) != len(blockRoots) {
|
|
return nil, nil, errWrongBlockCount
|
|
}
|
|
|
|
if err := wrapper.BeaconBlockIsNil(blks[0]); err != nil {
|
|
return nil, nil, invalidBlock{err}
|
|
}
|
|
b := blks[0].Block()
|
|
|
|
// Retrieve incoming block's pre state.
|
|
if err := s.verifyBlkPreState(ctx, b); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
preState, err := s.cfg.StateGen.StateByRootInitialSync(ctx, bytesutil.ToBytes32(b.ParentRoot()))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if preState == nil || preState.IsNil() {
|
|
return nil, nil, fmt.Errorf("nil pre state for slot %d", b.Slot())
|
|
}
|
|
|
|
jCheckpoints := make([]*ethpb.Checkpoint, len(blks))
|
|
fCheckpoints := make([]*ethpb.Checkpoint, len(blks))
|
|
sigSet := &bls.SignatureBatch{
|
|
Signatures: [][]byte{},
|
|
PublicKeys: []bls.PublicKey{},
|
|
Messages: [][32]byte{},
|
|
}
|
|
type versionAndHeader struct {
|
|
version int
|
|
header *ethpb.ExecutionPayloadHeader
|
|
}
|
|
preVersionAndHeaders := make([]*versionAndHeader, len(blks))
|
|
postVersionAndHeaders := make([]*versionAndHeader, len(blks))
|
|
var set *bls.SignatureBatch
|
|
boundaries := make(map[[32]byte]state.BeaconState)
|
|
for i, b := range blks {
|
|
v, h, err := getStateVersionAndPayload(preState)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
preVersionAndHeaders[i] = &versionAndHeader{
|
|
version: v,
|
|
header: h,
|
|
}
|
|
|
|
set, preState, err = transition.ExecuteStateTransitionNoVerifyAnySig(ctx, preState, b)
|
|
if err != nil {
|
|
return nil, nil, invalidBlock{err}
|
|
}
|
|
// Save potential boundary states.
|
|
if slots.IsEpochStart(preState.Slot()) {
|
|
boundaries[blockRoots[i]] = preState.Copy()
|
|
}
|
|
jCheckpoints[i] = preState.CurrentJustifiedCheckpoint()
|
|
fCheckpoints[i] = preState.FinalizedCheckpoint()
|
|
|
|
v, h, err = getStateVersionAndPayload(preState)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
postVersionAndHeaders[i] = &versionAndHeader{
|
|
version: v,
|
|
header: h,
|
|
}
|
|
sigSet.Join(set)
|
|
}
|
|
verify, err := sigSet.Verify()
|
|
if err != nil {
|
|
return nil, nil, invalidBlock{err}
|
|
}
|
|
if !verify {
|
|
return nil, nil, errors.New("batch block signature verification failed")
|
|
}
|
|
|
|
// blocks have been verified, add them to forkchoice and call the engine
|
|
for i, b := range blks {
|
|
isValidPayload, err := s.notifyNewPayload(ctx,
|
|
postVersionAndHeaders[i].version,
|
|
postVersionAndHeaders[i].header, b)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if isValidPayload {
|
|
if err := s.validateMergeTransitionBlock(ctx, preVersionAndHeaders[i].version,
|
|
preVersionAndHeaders[i].header, b); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
|
|
if err := s.insertBlockToForkChoiceStore(ctx, b.Block(), blockRoots[i], fCheckpoints[i], jCheckpoints[i]); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if isValidPayload {
|
|
if err := s.cfg.ForkChoiceStore.SetOptimisticToValid(ctx, blockRoots[i]); err != nil {
|
|
return nil, nil, errors.Wrap(err, "could not set optimistic block to valid")
|
|
}
|
|
}
|
|
s.saveInitSyncBlock(blockRoots[i], b)
|
|
if err = s.handleBlockAfterBatchVerify(ctx, b, blockRoots[i], fCheckpoints[i], jCheckpoints[i]); err != nil {
|
|
tracing.AnnotateError(span, err)
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
|
|
for r, st := range boundaries {
|
|
if err := s.cfg.StateGen.SaveState(ctx, r, st); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
// Also saves the last post state which to be used as pre state for the next batch.
|
|
lastB := blks[len(blks)-1]
|
|
lastBR := blockRoots[len(blockRoots)-1]
|
|
if err := s.cfg.StateGen.SaveState(ctx, lastBR, preState); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
arg := ¬ifyForkchoiceUpdateArg{
|
|
headState: preState,
|
|
headRoot: lastBR,
|
|
headBlock: lastB.Block(),
|
|
}
|
|
if _, err := s.notifyForkchoiceUpdate(ctx, arg); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if err := s.saveHeadNoDB(ctx, lastB, lastBR, preState); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return fCheckpoints, jCheckpoints, nil
|
|
}
|
|
|
|
// 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 interfaces.SignedBeaconBlock,
|
|
blockRoot [32]byte, fCheckpoint, jCheckpoint *ethpb.Checkpoint) error {
|
|
|
|
if err := s.cfg.BeaconDB.SaveStateSummary(ctx, ðpb.StateSummary{
|
|
Slot: signed.Block().Slot(),
|
|
Root: blockRoot[:],
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 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.cfg.BeaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
|
|
return err
|
|
}
|
|
s.clearInitSyncBlocks()
|
|
}
|
|
|
|
justified, err := s.store.JustifiedCheckpt()
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not get justified checkpoint")
|
|
}
|
|
if jCheckpoint.Epoch > justified.Epoch {
|
|
if err := s.updateJustifiedInitSync(ctx, jCheckpoint); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
finalized, err := s.store.FinalizedCheckpt()
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not get finalized checkpoint")
|
|
}
|
|
if finalized == nil {
|
|
return errNilFinalizedInStore
|
|
}
|
|
// Update finalized check point. Prune the block cache and helper caches on every new finalized epoch.
|
|
if fCheckpoint.Epoch > finalized.Epoch {
|
|
if err := s.updateFinalized(ctx, fCheckpoint); err != nil {
|
|
return err
|
|
}
|
|
s.store.SetPrevFinalizedCheckpt(finalized)
|
|
h, err := s.getPayloadHash(ctx, fCheckpoint.Root)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.store.SetFinalizedCheckptAndPayloadHash(fCheckpoint, h)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Epoch boundary bookkeeping such as logging epoch summaries.
|
|
func (s *Service) handleEpochBoundary(ctx context.Context, postState state.BeaconState) error {
|
|
ctx, span := trace.StartSpan(ctx, "blockChain.handleEpochBoundary")
|
|
defer span.End()
|
|
|
|
if postState.Slot()+1 == s.nextEpochBoundarySlot {
|
|
copied := postState.Copy()
|
|
copied, err := transition.ProcessSlots(ctx, copied, copied.Slot()+1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Update caches for the next epoch at epoch boundary slot - 1.
|
|
if err := helpers.UpdateCommitteeCache(copied, coreTime.CurrentEpoch(copied)); err != nil {
|
|
return err
|
|
}
|
|
if err := helpers.UpdateProposerIndicesInCache(ctx, copied); err != nil {
|
|
return err
|
|
}
|
|
} else if postState.Slot() >= s.nextEpochBoundarySlot {
|
|
if err := reportEpochMetrics(ctx, postState, s.head.state); err != nil {
|
|
return err
|
|
}
|
|
var err error
|
|
s.nextEpochBoundarySlot, err = slots.EpochStart(coreTime.NextEpoch(postState))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Update caches at epoch boundary slot.
|
|
// The following updates have short cut to return nil cheaply if fulfilled during boundary slot - 1.
|
|
if err := helpers.UpdateCommitteeCache(postState, coreTime.CurrentEpoch(postState)); err != nil {
|
|
return err
|
|
}
|
|
if err := helpers.UpdateProposerIndicesInCache(ctx, postState); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// 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.
|
|
func (s *Service) insertBlockAndAttestationsToForkChoiceStore(ctx context.Context, blk interfaces.BeaconBlock, root [32]byte,
|
|
st state.BeaconState) error {
|
|
ctx, span := trace.StartSpan(ctx, "blockChain.insertBlockAndAttestationsToForkChoiceStore")
|
|
defer span.End()
|
|
|
|
fCheckpoint := st.FinalizedCheckpoint()
|
|
jCheckpoint := st.CurrentJustifiedCheckpoint()
|
|
if err := s.insertBlockToForkChoiceStore(ctx, blk, root, fCheckpoint, jCheckpoint); err != nil {
|
|
return err
|
|
}
|
|
// Feed in block's attestations to fork choice store.
|
|
for _, a := range blk.Body().Attestations() {
|
|
committee, err := helpers.BeaconCommitteeFromState(ctx, st, a.Data.Slot, a.Data.CommitteeIndex)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
indices, err := attestation.AttestingIndices(a.AggregationBits, committee)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.cfg.ForkChoiceStore.ProcessAttestation(ctx, indices, bytesutil.ToBytes32(a.Data.BeaconBlockRoot), a.Data.Target.Epoch)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) insertBlockToForkChoiceStore(ctx context.Context, blk interfaces.BeaconBlock,
|
|
root [32]byte, fCheckpoint, jCheckpoint *ethpb.Checkpoint) error {
|
|
if err := s.fillInForkChoiceMissingBlocks(ctx, blk, fCheckpoint, jCheckpoint); err != nil {
|
|
return err
|
|
}
|
|
// Feed in block to fork choice store.
|
|
|
|
payloadHash, err := getBlockPayloadHash(blk)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.cfg.ForkChoiceStore.InsertOptimisticBlock(ctx,
|
|
blk.Slot(), root, bytesutil.ToBytes32(blk.ParentRoot()), payloadHash,
|
|
jCheckpoint.Epoch,
|
|
fCheckpoint.Epoch)
|
|
}
|
|
|
|
// Inserts attester slashing indices to fork choice store.
|
|
// To call this function, it's caller's responsibility to ensure the slashing object is valid.
|
|
func (s *Service) insertSlashingsToForkChoiceStore(ctx context.Context, slashings []*ethpb.AttesterSlashing) {
|
|
for _, slashing := range slashings {
|
|
indices := blocks.SlashableAttesterIndices(slashing)
|
|
for _, index := range indices {
|
|
s.ForkChoicer().InsertSlashedIndex(ctx, types.ValidatorIndex(index))
|
|
}
|
|
}
|
|
}
|
|
|
|
func getBlockPayloadHash(blk interfaces.BeaconBlock) ([32]byte, error) {
|
|
payloadHash := [32]byte{}
|
|
if blocks.IsPreBellatrixVersion(blk.Version()) {
|
|
return payloadHash, nil
|
|
}
|
|
payload, err := blk.Body().ExecutionPayload()
|
|
if err != nil {
|
|
return payloadHash, err
|
|
}
|
|
return bytesutil.ToBytes32(payload.BlockHash), nil
|
|
}
|
|
|
|
// 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.
|
|
func (s *Service) savePostStateInfo(ctx context.Context, r [32]byte, b interfaces.SignedBeaconBlock, st state.BeaconState) error {
|
|
ctx, span := trace.StartSpan(ctx, "blockChain.savePostStateInfo")
|
|
defer span.End()
|
|
if err := s.cfg.BeaconDB.SaveBlock(ctx, b); err != nil {
|
|
return errors.Wrapf(err, "could not save block from slot %d", b.Block().Slot())
|
|
}
|
|
if err := s.cfg.StateGen.SaveState(ctx, r, st); err != nil {
|
|
return errors.Wrap(err, "could not save state")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// This removes the attestations from the mem pool. It will only remove the attestations if input root `r` is canonical,
|
|
// meaning the block `b` is part of the canonical chain.
|
|
func (s *Service) pruneCanonicalAttsFromPool(ctx context.Context, r [32]byte, b interfaces.SignedBeaconBlock) error {
|
|
if !features.Get().CorrectlyPruneCanonicalAtts {
|
|
return nil
|
|
}
|
|
|
|
canonical, err := s.IsCanonical(ctx, r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !canonical {
|
|
return nil
|
|
}
|
|
|
|
atts := b.Block().Body().Attestations()
|
|
for _, att := range atts {
|
|
if helpers.IsAggregated(att) {
|
|
if err := s.cfg.AttPool.DeleteAggregatedAttestation(att); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if err := s.cfg.AttPool.DeleteUnaggregatedAttestation(att); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// validateMergeTransitionBlock validates the merge transition block.
|
|
func (s *Service) validateMergeTransitionBlock(ctx context.Context, stateVersion int, stateHeader *ethpb.ExecutionPayloadHeader, blk interfaces.SignedBeaconBlock) error {
|
|
// Skip validation if block is older than Bellatrix.
|
|
if blocks.IsPreBellatrixVersion(blk.Block().Version()) {
|
|
return nil
|
|
}
|
|
|
|
// Skip validation if block has an empty payload.
|
|
payload, err := blk.Block().Body().ExecutionPayload()
|
|
if err != nil {
|
|
return invalidBlock{err}
|
|
}
|
|
if blocks.IsEmptyPayload(payload) {
|
|
return nil
|
|
}
|
|
|
|
// Handle case where pre-state is Altair but block contains payload.
|
|
// To reach here, the block must have contained a valid payload.
|
|
if blocks.IsPreBellatrixVersion(stateVersion) {
|
|
return s.validateMergeBlock(ctx, blk)
|
|
}
|
|
|
|
// Skip validation if the block is not a merge transition block.
|
|
atTransition, err := blocks.IsMergeTransitionBlockUsingPreStatePayloadHeader(stateHeader, blk.Block().Body())
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not check if merge block is terminal")
|
|
}
|
|
if !atTransition {
|
|
return nil
|
|
}
|
|
return s.validateMergeBlock(ctx, blk)
|
|
}
|