prysm-pulse/beacon-chain/blockchain/receive_block.go

187 lines
6.7 KiB
Go
Raw Normal View History

package blockchain
import (
"context"
2019-08-02 02:27:38 +00:00
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
"github.com/prysmaticlabs/prysm/consensus-types/interfaces"
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/monitoring/tracing"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/time"
"github.com/prysmaticlabs/prysm/time/slots"
"go.opencensus.io/trace"
)
// This defines how many epochs since finality the run time will begin to save hot state on to the DB.
var epochsSinceFinalitySaveHotStateDB = types.Epoch(100)
// BlockReceiver interface defines the methods of chain service for receiving and processing new blocks.
type BlockReceiver interface {
ReceiveBlock(ctx context.Context, block interfaces.SignedBeaconBlock, blockRoot [32]byte) error
ReceiveBlockBatch(ctx context.Context, blocks []interfaces.SignedBeaconBlock, blkRoots [][32]byte) error
HasBlock(ctx context.Context, root [32]byte) bool
}
// SlashingReceiver interface defines the methods of chain service for receiving validated slashing over the wire.
type SlashingReceiver interface {
ReceiveAttesterSlashing(ctx context.Context, slashings *ethpb.AttesterSlashing)
}
// ReceiveBlock is a function that defines the operations (minus pubsub)
// that are performed on a received block. The operations consist of:
// 1. Validate block, apply state transition and update checkpoints
Part 5 of update fork choice - implement new ReceiveBlock (#3242) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Add benchmark tests * Add yaml driven framework tests * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * Implemented new receive block methods * Comments * Remove mark evil block * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Renamed to receive_block.go * Use time.Time * Preston's feedback, removed OnTick and Store.time * Dont have to cast it to kv * add block caching layer * runlock * lockinggg * Fixed * Avoid 2 fetches of the same data * latest votes map * Gaz * Test passes * Lint * Fixed db set up * Fixed all the tests * Gazelle * Added tests * Remove todo * remove kv * Last clean up * Last clean up Last clean up * Lint * Preston's feedback
2019-08-21 17:40:00 +00:00
// 2. Apply fork choice to the processed block
// 3. Save latest head info
func (s *Service) ReceiveBlock(ctx context.Context, block interfaces.SignedBeaconBlock, blockRoot [32]byte) error {
ctx, span := trace.StartSpan(ctx, "blockChain.ReceiveBlock")
Part 5 of update fork choice - implement new ReceiveBlock (#3242) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Add benchmark tests * Add yaml driven framework tests * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * Implemented new receive block methods * Comments * Remove mark evil block * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Renamed to receive_block.go * Use time.Time * Preston's feedback, removed OnTick and Store.time * Dont have to cast it to kv * add block caching layer * runlock * lockinggg * Fixed * Avoid 2 fetches of the same data * latest votes map * Gaz * Test passes * Lint * Fixed db set up * Fixed all the tests * Gazelle * Added tests * Remove todo * remove kv * Last clean up * Last clean up Last clean up * Lint * Preston's feedback
2019-08-21 17:40:00 +00:00
defer span.End()
receivedTime := time.Now()
blockCopy := block.Copy()
Part 5 of update fork choice - implement new ReceiveBlock (#3242) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Add benchmark tests * Add yaml driven framework tests * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * Implemented new receive block methods * Comments * Remove mark evil block * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Renamed to receive_block.go * Use time.Time * Preston's feedback, removed OnTick and Store.time * Dont have to cast it to kv * add block caching layer * runlock * lockinggg * Fixed * Avoid 2 fetches of the same data * latest votes map * Gaz * Test passes * Lint * Fixed db set up * Fixed all the tests * Gazelle * Added tests * Remove todo * remove kv * Last clean up * Last clean up Last clean up * Lint * Preston's feedback
2019-08-21 17:40:00 +00:00
// Apply state transition on the new block.
if err := s.onBlock(ctx, blockCopy, blockRoot); err != nil {
err := errors.Wrap(err, "could not process block")
tracing.AnnotateError(span, err)
return err
Part 5 of update fork choice - implement new ReceiveBlock (#3242) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Add benchmark tests * Add yaml driven framework tests * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * Implemented new receive block methods * Comments * Remove mark evil block * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Renamed to receive_block.go * Use time.Time * Preston's feedback, removed OnTick and Store.time * Dont have to cast it to kv * add block caching layer * runlock * lockinggg * Fixed * Avoid 2 fetches of the same data * latest votes map * Gaz * Test passes * Lint * Fixed db set up * Fixed all the tests * Gazelle * Added tests * Remove todo * remove kv * Last clean up * Last clean up Last clean up * Lint * Preston's feedback
2019-08-21 17:40:00 +00:00
}
// Handle post block operations such as attestations and exits.
if err := s.handlePostBlockOperations(blockCopy.Block()); err != nil {
return err
}
// Have we been finalizing? Should we start saving hot states to db?
if err := s.checkSaveHotStateDB(ctx); err != nil {
return err
}
// Reports on block and fork choice metrics.
finalized := s.store.FinalizedCheckpt()
if finalized == nil {
return errNilFinalizedInStore
}
reportSlotMetrics(blockCopy.Block().Slot(), s.HeadSlot(), s.CurrentSlot(), finalized)
// Log block sync status.
if err := logBlockSyncStatus(blockCopy.Block(), blockRoot, finalized, receivedTime, uint64(s.genesisTime.Unix())); err != nil {
return err
}
// Log state transition data.
if err := logStateTransitionData(blockCopy.Block()); err != nil {
return err
}
Part 5 of update fork choice - implement new ReceiveBlock (#3242) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Add benchmark tests * Add yaml driven framework tests * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * Implemented new receive block methods * Comments * Remove mark evil block * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Renamed to receive_block.go * Use time.Time * Preston's feedback, removed OnTick and Store.time * Dont have to cast it to kv * add block caching layer * runlock * lockinggg * Fixed * Avoid 2 fetches of the same data * latest votes map * Gaz * Test passes * Lint * Fixed db set up * Fixed all the tests * Gazelle * Added tests * Remove todo * remove kv * Last clean up * Last clean up Last clean up * Lint * Preston's feedback
2019-08-21 17:40:00 +00:00
return nil
}
// 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.
func (s *Service) ReceiveBlockBatch(ctx context.Context, blocks []interfaces.SignedBeaconBlock, blkRoots [][32]byte) error {
ctx, span := trace.StartSpan(ctx, "blockChain.ReceiveBlockBatch")
defer span.End()
// Apply state transition on the incoming newly received block batches, one by one.
_, _, err := s.onBlockBatch(ctx, blocks, blkRoots)
if err != nil {
2020-07-20 19:35:30 +00:00
err := errors.Wrap(err, "could not process block in batch")
tracing.AnnotateError(span, err)
return err
}
for i, b := range blocks {
blockCopy := b.Copy()
// Send notification of the processed block to the state feed.
s.cfg.StateNotifier.StateFeed().Send(&feed.Event{
Type: statefeed.BlockProcessed,
Data: &statefeed.BlockProcessedData{
Slot: blockCopy.Block().Slot(),
2021-01-05 20:40:11 +00:00
BlockRoot: blkRoots[i],
SignedBlock: blockCopy,
Verified: true,
},
})
// Reports on blockCopy and fork choice metrics.
finalized := s.store.FinalizedCheckpt()
if finalized == nil {
return errNilFinalizedInStore
}
reportSlotMetrics(blockCopy.Block().Slot(), s.HeadSlot(), s.CurrentSlot(), finalized)
}
if err := s.cfg.BeaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
return err
}
finalized := s.store.FinalizedCheckpt()
if finalized == nil {
return errNilFinalizedInStore
}
if err := s.wsVerifier.VerifyWeakSubjectivity(s.ctx, finalized.Epoch); err != nil {
// log.Fatalf will prevent defer from being called
span.End()
// Exit run time if the node failed to verify weak subjectivity checkpoint.
log.Fatalf("Could not verify weak subjectivity checkpoint: %v", err)
}
2020-07-20 19:35:30 +00:00
return nil
}
// HasBlock returns true if the block of the input root exists in initial sync blocks cache or DB.
func (s *Service) HasBlock(ctx context.Context, root [32]byte) bool {
return s.hasBlockInInitSyncOrDB(ctx, root)
}
// ReceiveAttesterSlashing receives an attester slashing and inserts it to forkchoice
func (s *Service) ReceiveAttesterSlashing(ctx context.Context, slashing *ethpb.AttesterSlashing) {
s.insertSlashingsToForkChoiceStore(ctx, []*ethpb.AttesterSlashing{slashing})
}
func (s *Service) handlePostBlockOperations(b interfaces.BeaconBlock) error {
// Delete the processed block attestations from attestation pool.
if err := s.deletePoolAtts(b.Body().Attestations()); err != nil {
return err
}
// Add block attestations to the fork choice pool to compute head.
if err := s.cfg.AttPool.SaveBlockAttestations(b.Body().Attestations()); err != nil {
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.
for _, e := range b.Body().VoluntaryExits() {
s.cfg.ExitPool.MarkIncluded(e)
}
// Mark attester slashings as seen so we don't include same ones in future blocks.
for _, as := range b.Body().AttesterSlashings() {
s.cfg.SlashingPool.MarkIncludedAttesterSlashing(as)
}
return nil
}
// 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 {
currentEpoch := slots.ToEpoch(s.CurrentSlot())
// Prevent `sinceFinality` going underflow.
var sinceFinality types.Epoch
finalized := s.store.FinalizedCheckpt()
if finalized == nil {
return errNilFinalizedInStore
}
if currentEpoch > finalized.Epoch {
sinceFinality = currentEpoch - finalized.Epoch
}
if sinceFinality >= epochsSinceFinalitySaveHotStateDB {
s.cfg.StateGen.EnableSaveHotStateToDB(ctx)
return nil
}
return s.cfg.StateGen.DisableSaveHotStateToDB(ctx)
}