2019-09-20 17:08:32 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"sort"
|
2020-02-04 08:31:31 +00:00
|
|
|
"sync"
|
2019-09-20 17:08:32 +00:00
|
|
|
|
2020-08-18 00:01:32 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-12-03 15:56:04 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-01-02 08:09:28 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-12-03 15:56:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2020-05-05 04:30:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2019-09-20 17:08:32 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-06-26 14:58:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/rand"
|
2019-12-16 17:00:15 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/runutil"
|
2020-05-13 03:28:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/slotutil"
|
2019-10-10 08:44:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/traceutil"
|
2019-09-20 17:08:32 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-10-10 08:44:24 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-09-20 17:08:32 +00:00
|
|
|
)
|
|
|
|
|
2020-05-13 03:28:17 +00:00
|
|
|
var processPendingBlocksPeriod = slotutil.DivideSlotBy(3 /* times per slot */)
|
2019-09-20 17:08:32 +00:00
|
|
|
|
|
|
|
// processes pending blocks queue on every processPendingBlocksPeriod
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) processPendingBlocksQueue() {
|
2019-12-16 17:00:15 +00:00
|
|
|
ctx := context.Background()
|
2020-06-23 12:00:29 +00:00
|
|
|
// Prevents multiple queue processing goroutines (invoked by RunEvery) from contending for data.
|
2020-02-04 08:31:31 +00:00
|
|
|
locker := new(sync.Mutex)
|
2020-06-22 20:37:48 +00:00
|
|
|
runutil.RunEvery(s.ctx, processPendingBlocksPeriod, func() {
|
2020-02-04 08:31:31 +00:00
|
|
|
locker.Lock()
|
2020-06-22 20:37:48 +00:00
|
|
|
if err := s.processPendingBlocks(ctx); err != nil {
|
2020-08-04 20:30:40 +00:00
|
|
|
log.WithError(err).Debug("Failed to process pending blocks")
|
2020-04-13 04:11:09 +00:00
|
|
|
}
|
2020-02-04 08:31:31 +00:00
|
|
|
locker.Unlock()
|
2019-12-16 17:00:15 +00:00
|
|
|
})
|
2019-09-20 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// processes the block tree inside the queue
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) processPendingBlocks(ctx context.Context) error {
|
2019-10-10 08:44:24 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "processPendingBlocks")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-06-22 20:37:48 +00:00
|
|
|
pids := s.p2p.Peers().Connected()
|
|
|
|
if err := s.validatePendingSlots(); err != nil {
|
2019-12-03 15:56:04 +00:00
|
|
|
return errors.Wrap(err, "could not validate pending slots")
|
|
|
|
}
|
2020-06-22 20:37:48 +00:00
|
|
|
slots := s.sortedPendingSlots()
|
2019-09-20 17:08:32 +00:00
|
|
|
|
2019-10-10 08:44:24 +00:00
|
|
|
span.AddAttributes(
|
|
|
|
trace.Int64Attribute("numSlots", int64(len(slots))),
|
|
|
|
trace.Int64Attribute("numPeers", int64(len(pids))),
|
|
|
|
)
|
|
|
|
|
2020-06-26 14:58:47 +00:00
|
|
|
randGen := rand.NewGenerator()
|
2020-06-22 20:37:48 +00:00
|
|
|
for _, slot := range slots {
|
2019-10-10 08:44:24 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "processPendingBlocks.InnerLoop")
|
2020-06-22 20:37:48 +00:00
|
|
|
span.AddAttributes(trace.Int64Attribute("slot", int64(slot)))
|
2019-10-10 08:44:24 +00:00
|
|
|
|
2020-06-22 20:37:48 +00:00
|
|
|
s.pendingQueueLock.RLock()
|
2020-08-18 00:01:32 +00:00
|
|
|
bs := s.slotToPendingBlocks[slot]
|
|
|
|
// Skip if there's no block in the queue.
|
|
|
|
if len(bs) == 0 {
|
2020-06-22 20:37:48 +00:00
|
|
|
s.pendingQueueLock.RUnlock()
|
2020-02-01 22:47:51 +00:00
|
|
|
span.End()
|
2020-01-13 15:09:22 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-07-27 11:57:19 +00:00
|
|
|
s.pendingQueueLock.RUnlock()
|
|
|
|
|
2020-08-18 00:01:32 +00:00
|
|
|
// Loop through the pending queue and mark the potential parent blocks as seen.
|
|
|
|
for _, b := range bs {
|
|
|
|
if b == nil || b.Block == nil {
|
|
|
|
span.End()
|
|
|
|
continue
|
2020-07-27 11:57:19 +00:00
|
|
|
}
|
2019-09-20 17:08:32 +00:00
|
|
|
|
2020-08-18 00:01:32 +00:00
|
|
|
s.pendingQueueLock.RLock()
|
|
|
|
inPendingQueue := s.seenPendingBlocks[bytesutil.ToBytes32(b.Block.ParentRoot)]
|
|
|
|
s.pendingQueueLock.RUnlock()
|
2019-09-20 17:08:32 +00:00
|
|
|
|
2020-08-18 00:01:32 +00:00
|
|
|
blkRoot, err := stateutil.BlockRoot(b.Block)
|
|
|
|
if err != nil {
|
|
|
|
traceutil.AnnotateError(span, err)
|
|
|
|
span.End()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
parentIsBad := s.hasBadBlock(bytesutil.ToBytes32(b.Block.ParentRoot))
|
|
|
|
blockIsBad := s.hasBadBlock(blkRoot)
|
|
|
|
// Check if parent is a bad block.
|
|
|
|
if parentIsBad || blockIsBad {
|
|
|
|
// Set block as bad if its parent block is bad too.
|
|
|
|
if parentIsBad {
|
2020-08-18 01:21:10 +00:00
|
|
|
s.setBadBlock(ctx, blkRoot)
|
2020-01-17 22:43:32 +00:00
|
|
|
}
|
2020-08-18 00:01:32 +00:00
|
|
|
// Remove block from queue.
|
|
|
|
s.pendingQueueLock.Lock()
|
|
|
|
s.deleteBlockFromPendingQueue(slot, b)
|
|
|
|
delete(s.seenPendingBlocks, blkRoot)
|
|
|
|
s.pendingQueueLock.Unlock()
|
|
|
|
span.End()
|
|
|
|
continue
|
2020-01-17 22:43:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 00:01:32 +00:00
|
|
|
inDB := s.db.HasBlock(ctx, bytesutil.ToBytes32(b.Block.ParentRoot))
|
|
|
|
hasPeer := len(pids) != 0
|
|
|
|
|
|
|
|
// Only request for missing parent block if it's not in DB, not in pending cache
|
|
|
|
// and has peer in the peer list.
|
|
|
|
if !inPendingQueue && !inDB && hasPeer {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"currentSlot": b.Block.Slot,
|
|
|
|
"parentRoot": hex.EncodeToString(bytesutil.Trunc(b.Block.ParentRoot)),
|
|
|
|
}).Info("Requesting parent block")
|
|
|
|
req := [][32]byte{bytesutil.ToBytes32(b.Block.ParentRoot)}
|
|
|
|
|
|
|
|
// Start with a random peer to query, but choose the first peer in our unsorted list that claims to
|
|
|
|
// have a head slot newer than the block slot we are requesting.
|
|
|
|
pid := pids[randGen.Int()%len(pids)]
|
|
|
|
for _, p := range pids {
|
|
|
|
cs, err := s.p2p.Peers().ChainState(p)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to read chain state for peer")
|
|
|
|
}
|
|
|
|
if cs != nil && cs.HeadSlot >= slot {
|
|
|
|
pid = p
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.sendRecentBeaconBlocksRequest(ctx, req, pid); err != nil {
|
|
|
|
traceutil.AnnotateError(span, err)
|
|
|
|
log.Debugf("Could not send recent block request: %v", err)
|
|
|
|
}
|
|
|
|
span.End()
|
|
|
|
continue
|
2019-09-20 17:08:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 00:01:32 +00:00
|
|
|
if !inDB {
|
|
|
|
span.End()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := s.chain.ReceiveBlock(ctx, b, blkRoot); err != nil {
|
|
|
|
log.Debugf("Could not process block from slot %d: %v", b.Block.Slot, err)
|
2020-08-18 01:21:10 +00:00
|
|
|
s.setBadBlock(ctx, blkRoot)
|
2020-08-18 00:01:32 +00:00
|
|
|
traceutil.AnnotateError(span, err)
|
|
|
|
}
|
2019-09-20 17:08:32 +00:00
|
|
|
|
2020-08-18 00:01:32 +00:00
|
|
|
// Broadcasting the block again once a node is able to process it.
|
|
|
|
if err := s.p2p.Broadcast(ctx, b); err != nil {
|
|
|
|
log.WithError(err).Debug("Failed to broadcast block")
|
|
|
|
}
|
2020-02-02 02:22:59 +00:00
|
|
|
|
2020-08-18 00:01:32 +00:00
|
|
|
s.pendingQueueLock.Lock()
|
|
|
|
s.deleteBlockFromPendingQueue(slot, b)
|
|
|
|
delete(s.seenPendingBlocks, blkRoot)
|
|
|
|
s.pendingQueueLock.Unlock()
|
2019-09-20 17:08:32 +00:00
|
|
|
|
2020-08-18 00:01:32 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"slot": slot,
|
|
|
|
"blockRoot": hex.EncodeToString(bytesutil.Trunc(blkRoot[:])),
|
|
|
|
}).Debug("Processed pending block and cleared it in cache")
|
2020-02-01 22:47:51 +00:00
|
|
|
|
2020-08-18 00:01:32 +00:00
|
|
|
span.End()
|
|
|
|
}
|
2019-09-20 17:08:32 +00:00
|
|
|
}
|
2020-02-02 01:42:29 +00:00
|
|
|
|
2019-09-20 17:08:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) sortedPendingSlots() []uint64 {
|
|
|
|
s.pendingQueueLock.RLock()
|
|
|
|
defer s.pendingQueueLock.RUnlock()
|
2019-09-20 17:08:32 +00:00
|
|
|
|
2020-06-22 20:37:48 +00:00
|
|
|
slots := make([]uint64, 0, len(s.slotToPendingBlocks))
|
|
|
|
for slot := range s.slotToPendingBlocks {
|
|
|
|
slots = append(slots, slot)
|
2019-09-20 17:08:32 +00:00
|
|
|
}
|
2020-05-29 13:50:41 +00:00
|
|
|
sort.Slice(slots, func(i, j int) bool {
|
|
|
|
return slots[i] < slots[j]
|
|
|
|
})
|
2019-11-25 05:05:51 +00:00
|
|
|
return slots
|
2019-11-21 13:24:50 +00:00
|
|
|
}
|
2019-12-03 15:56:04 +00:00
|
|
|
|
|
|
|
// validatePendingSlots validates the pending blocks
|
|
|
|
// by their slot. If they are before the current finalized
|
|
|
|
// checkpoint, these blocks are removed from the queue.
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) validatePendingSlots() error {
|
|
|
|
s.pendingQueueLock.Lock()
|
|
|
|
defer s.pendingQueueLock.Unlock()
|
2019-12-03 15:56:04 +00:00
|
|
|
oldBlockRoots := make(map[[32]byte]bool)
|
|
|
|
|
2020-06-22 20:37:48 +00:00
|
|
|
finalizedEpoch := s.chain.FinalizedCheckpt().Epoch
|
2020-08-18 00:01:32 +00:00
|
|
|
for slot, blks := range s.slotToPendingBlocks {
|
|
|
|
for _, b := range blks {
|
|
|
|
epoch := helpers.SlotToEpoch(slot)
|
|
|
|
// remove all descendant blocks of old blocks
|
|
|
|
if oldBlockRoots[bytesutil.ToBytes32(b.Block.ParentRoot)] {
|
|
|
|
root, err := stateutil.BlockRoot(b.Block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldBlockRoots[root] = true
|
|
|
|
s.deleteBlockFromPendingQueue(slot, b)
|
|
|
|
delete(s.seenPendingBlocks, root)
|
|
|
|
continue
|
2019-12-03 15:56:04 +00:00
|
|
|
}
|
2020-08-18 00:01:32 +00:00
|
|
|
// don't process old blocks
|
|
|
|
if finalizedEpoch > 0 && epoch <= finalizedEpoch {
|
|
|
|
blkRoot, err := stateutil.BlockRoot(b.Block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldBlockRoots[blkRoot] = true
|
|
|
|
s.deleteBlockFromPendingQueue(slot, b)
|
|
|
|
delete(s.seenPendingBlocks, blkRoot)
|
2019-12-03 15:56:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-02 08:09:28 +00:00
|
|
|
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) clearPendingSlots() {
|
|
|
|
s.pendingQueueLock.Lock()
|
|
|
|
defer s.pendingQueueLock.Unlock()
|
2020-08-18 00:01:32 +00:00
|
|
|
s.slotToPendingBlocks = make(map[uint64][]*ethpb.SignedBeaconBlock)
|
2020-06-22 20:37:48 +00:00
|
|
|
s.seenPendingBlocks = make(map[[32]byte]bool)
|
2020-01-02 08:09:28 +00:00
|
|
|
}
|
2020-08-18 00:01:32 +00:00
|
|
|
|
|
|
|
// Delete block from the list from the pending queue using the slot as key.
|
|
|
|
// Note: this helper is not thread safe.
|
|
|
|
func (s *Service) deleteBlockFromPendingQueue(slot uint64, b *ethpb.SignedBeaconBlock) {
|
|
|
|
blks, ok := s.slotToPendingBlocks[slot]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newBlks := make([]*ethpb.SignedBeaconBlock, 0, len(blks))
|
|
|
|
for _, blk := range blks {
|
|
|
|
if proto.Equal(blk, b) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newBlks = append(newBlks, blk)
|
|
|
|
}
|
|
|
|
if len(newBlks) == 0 {
|
|
|
|
delete(s.slotToPendingBlocks, slot)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.slotToPendingBlocks[slot] = newBlks
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert block to the list in the pending queue using the slot as key.
|
|
|
|
// Note: this helper is not thread safe.
|
|
|
|
func (s *Service) insertBlockToPendingQueue(slot uint64, b *ethpb.SignedBeaconBlock) {
|
|
|
|
_, ok := s.slotToPendingBlocks[slot]
|
|
|
|
if ok {
|
2020-08-18 10:47:44 +00:00
|
|
|
blks := s.slotToPendingBlocks[slot]
|
|
|
|
for _, blk := range blks {
|
|
|
|
if proto.Equal(blk, b) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.slotToPendingBlocks[slot] = append(blks, b)
|
2020-08-18 00:01:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.slotToPendingBlocks[slot] = []*ethpb.SignedBeaconBlock{b}
|
|
|
|
}
|