2019-03-13 21:17:32 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-08-02 02:27:38 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-02-16 07:45:34 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-09-03 20:10:31 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core"
|
2019-12-07 17:57:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
|
|
|
|
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
2021-09-14 20:59:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/monitoring/tracing"
|
2021-07-28 21:23:44 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/block"
|
2021-09-15 00:09:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/time"
|
2019-03-13 21:17:32 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
2020-10-23 00:35:30 +00:00
|
|
|
// This defines how many epochs since finality the run time will begin to save hot state on to the DB.
|
2021-02-09 10:05:22 +00:00
|
|
|
var epochsSinceFinalitySaveHotStateDB = types.Epoch(100)
|
2020-10-23 00:35:30 +00:00
|
|
|
|
2019-08-23 17:48:40 +00:00
|
|
|
// BlockReceiver interface defines the methods of chain service receive and processing new blocks.
|
|
|
|
type BlockReceiver interface {
|
2021-07-23 20:10:15 +00:00
|
|
|
ReceiveBlock(ctx context.Context, block block.SignedBeaconBlock, blockRoot [32]byte) error
|
|
|
|
ReceiveBlockBatch(ctx context.Context, blocks []block.SignedBeaconBlock, blkRoots [][32]byte) error
|
2020-03-30 18:04:10 +00:00
|
|
|
HasInitSyncBlock(root [32]byte) bool
|
2019-08-23 17:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 23:50:48 +00:00
|
|
|
// ReceiveBlock is a function that defines the the operations (minus pubsub)
|
2020-06-23 21:11:20 +00:00
|
|
|
// that are performed on blocks that is received from regular sync service. The operations consists of:
|
2019-08-21 17:40:00 +00:00
|
|
|
// 1. Validate block, apply state transition and update check points
|
|
|
|
// 2. Apply fork choice to the processed block
|
|
|
|
// 3. Save latest head info
|
2021-07-23 20:10:15 +00:00
|
|
|
func (s *Service) ReceiveBlock(ctx context.Context, block block.SignedBeaconBlock, blockRoot [32]byte) error {
|
2020-07-09 23:50:48 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "blockChain.ReceiveBlock")
|
2019-08-21 17:40:00 +00:00
|
|
|
defer span.End()
|
2021-09-15 00:09:04 +00:00
|
|
|
receivedTime := time.Now()
|
2021-05-26 16:19:54 +00:00
|
|
|
blockCopy := block.Copy()
|
2019-08-21 17:40:00 +00:00
|
|
|
|
|
|
|
// Apply state transition on the new block.
|
2020-07-09 23:50:48 +00:00
|
|
|
if err := s.onBlock(ctx, blockCopy, blockRoot); err != nil {
|
2020-02-06 19:03:26 +00:00
|
|
|
err := errors.Wrap(err, "could not process block")
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2020-02-06 19:03:26 +00:00
|
|
|
return err
|
2019-08-21 17:40:00 +00:00
|
|
|
}
|
2020-01-25 20:22:25 +00:00
|
|
|
|
2020-07-09 23:50:48 +00:00
|
|
|
// Handle post block operations such as attestations and exits.
|
2021-05-26 16:19:54 +00:00
|
|
|
if err := s.handlePostBlockOperations(blockCopy.Block()); err != nil {
|
2020-07-09 23:50:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-23 00:35:30 +00:00
|
|
|
// Have we been finalizing? Should we start saving hot states to db?
|
|
|
|
if err := s.checkSaveHotStateDB(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-05 17:24:05 +00:00
|
|
|
// Reports on block and fork choice metrics.
|
2021-05-26 16:19:54 +00:00
|
|
|
reportSlotMetrics(blockCopy.Block().Slot(), s.HeadSlot(), s.CurrentSlot(), s.finalizedCheckpt)
|
2020-02-05 17:24:05 +00:00
|
|
|
|
2020-04-29 16:57:04 +00:00
|
|
|
// Log block sync status.
|
2021-05-26 16:19:54 +00:00
|
|
|
if err := logBlockSyncStatus(blockCopy.Block(), blockRoot, s.finalizedCheckpt, receivedTime, uint64(s.genesisTime.Unix())); err != nil {
|
2021-02-10 17:24:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-02-05 17:24:05 +00:00
|
|
|
// Log state transition data.
|
2021-05-26 16:19:54 +00:00
|
|
|
logStateTransitionData(blockCopy.Block())
|
2020-02-05 17:24:05 +00:00
|
|
|
|
2019-08-21 17:40:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 04:16:12 +00:00
|
|
|
// ReceiveBlockBatch processes the whole block batch at once, assuming the block batch is linear ,transitioning
|
|
|
|
// the state, performing batch verification of all collected signatures and then performing the appropriate
|
|
|
|
// actions for a block post-transition.
|
2021-07-23 20:10:15 +00:00
|
|
|
func (s *Service) ReceiveBlockBatch(ctx context.Context, blocks []block.SignedBeaconBlock, blkRoots [][32]byte) error {
|
2020-07-09 23:50:48 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "blockChain.ReceiveBlockBatch")
|
2020-07-07 04:16:12 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
// Apply state transition on the incoming newly received blockCopy without verifying its BLS contents.
|
2020-07-20 19:35:30 +00:00
|
|
|
fCheckpoints, jCheckpoints, err := s.onBlockBatch(ctx, blocks, blkRoots)
|
2020-07-07 04:16:12 +00:00
|
|
|
if err != nil {
|
2020-07-20 19:35:30 +00:00
|
|
|
err := errors.Wrap(err, "could not process block in batch")
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2020-07-07 04:16:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, b := range blocks {
|
2021-05-26 16:19:54 +00:00
|
|
|
blockCopy := b.Copy()
|
2020-07-07 04:16:12 +00:00
|
|
|
if err = s.handleBlockAfterBatchVerify(ctx, blockCopy, blkRoots[i], fCheckpoints[i], jCheckpoints[i]); err != nil {
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2020-07-07 04:16:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Send notification of the processed block to the state feed.
|
2021-03-17 18:36:56 +00:00
|
|
|
s.cfg.StateNotifier.StateFeed().Send(&feed.Event{
|
2020-07-07 04:16:12 +00:00
|
|
|
Type: statefeed.BlockProcessed,
|
|
|
|
Data: &statefeed.BlockProcessedData{
|
2021-05-26 16:19:54 +00:00
|
|
|
Slot: blockCopy.Block().Slot(),
|
2021-01-05 20:40:11 +00:00
|
|
|
BlockRoot: blkRoots[i],
|
|
|
|
SignedBlock: blockCopy,
|
|
|
|
Verified: true,
|
2020-07-07 04:16:12 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Reports on blockCopy and fork choice metrics.
|
2021-05-26 16:19:54 +00:00
|
|
|
reportSlotMetrics(blockCopy.Block().Slot(), s.HeadSlot(), s.CurrentSlot(), s.finalizedCheckpt)
|
2020-09-26 08:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.VerifyWeakSubjectivityRoot(s.ctx); err != nil {
|
2020-10-04 15:03:10 +00:00
|
|
|
// log.Fatalf will prevent defer from being called
|
|
|
|
span.End()
|
2020-09-26 08:04:07 +00:00
|
|
|
// Exit run time if the node failed to verify weak subjectivity checkpoint.
|
|
|
|
log.Fatalf("Could not verify weak subjectivity checkpoint: %v", err)
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 19:35:30 +00:00
|
|
|
return nil
|
2020-07-07 04:16:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 18:04:10 +00:00
|
|
|
// HasInitSyncBlock returns true if the block of the input root exists in initial sync blocks cache.
|
|
|
|
func (s *Service) HasInitSyncBlock(root [32]byte) bool {
|
|
|
|
return s.hasInitSyncBlock(root)
|
|
|
|
}
|
2020-07-09 23:50:48 +00:00
|
|
|
|
2021-07-23 20:10:15 +00:00
|
|
|
func (s *Service) handlePostBlockOperations(b block.BeaconBlock) error {
|
2020-07-09 23:50:48 +00:00
|
|
|
// Delete the processed block attestations from attestation pool.
|
2021-05-26 16:19:54 +00:00
|
|
|
if err := s.deletePoolAtts(b.Body().Attestations()); err != nil {
|
2020-07-09 23:50:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add block attestations to the fork choice pool to compute head.
|
2021-05-26 16:19:54 +00:00
|
|
|
if err := s.cfg.AttPool.SaveBlockAttestations(b.Body().Attestations()); err != nil {
|
2020-07-09 23:50:48 +00:00
|
|
|
log.Errorf("Could not save block attestations for fork choice: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Mark block exits as seen so we don't include same ones in future blocks.
|
2021-05-26 16:19:54 +00:00
|
|
|
for _, e := range b.Body().VoluntaryExits() {
|
2021-03-17 18:36:56 +00:00
|
|
|
s.cfg.ExitPool.MarkIncluded(e)
|
2020-07-09 23:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mark attester slashings as seen so we don't include same ones in future blocks.
|
2021-05-26 16:19:54 +00:00
|
|
|
for _, as := range b.Body().AttesterSlashings() {
|
2021-03-17 18:36:56 +00:00
|
|
|
s.cfg.SlashingPool.MarkIncludedAttesterSlashing(as)
|
2020-07-09 23:50:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-23 00:35:30 +00:00
|
|
|
|
|
|
|
// This checks whether it's time to start saving hot state to DB.
|
|
|
|
// It's time when there's `epochsSinceFinalitySaveHotStateDB` epochs of non-finality.
|
|
|
|
func (s *Service) checkSaveHotStateDB(ctx context.Context) error {
|
2021-09-03 20:10:31 +00:00
|
|
|
currentEpoch := core.SlotToEpoch(s.CurrentSlot())
|
2020-10-23 00:35:30 +00:00
|
|
|
// Prevent `sinceFinality` going underflow.
|
2021-02-09 10:05:22 +00:00
|
|
|
var sinceFinality types.Epoch
|
2020-10-23 00:35:30 +00:00
|
|
|
if currentEpoch > s.finalizedCheckpt.Epoch {
|
|
|
|
sinceFinality = currentEpoch - s.finalizedCheckpt.Epoch
|
|
|
|
}
|
|
|
|
|
2021-02-09 10:05:22 +00:00
|
|
|
if sinceFinality >= epochsSinceFinalitySaveHotStateDB {
|
2021-03-17 18:36:56 +00:00
|
|
|
s.cfg.StateGen.EnableSaveHotStateToDB(ctx)
|
2020-10-23 00:35:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-17 18:36:56 +00:00
|
|
|
return s.cfg.StateGen.DisableSaveHotStateToDB(ctx)
|
2020-10-23 00:35:30 +00:00
|
|
|
}
|