2020-02-02 01:42:29 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
2020-02-12 07:33:15 +00:00
|
|
|
"sync"
|
2020-02-02 01:42:29 +00:00
|
|
|
|
2020-05-31 06:44:34 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/async"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/blockchain"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/helpers"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/crypto/rand"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/time/slots"
|
2020-02-02 01:42:29 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This defines how often a node cleans up and processes pending attestations in the queue.
|
2021-09-15 00:09:04 +00:00
|
|
|
var processPendingAttsPeriod = slots.DivideSlotBy(2 /* twice per slot */)
|
2020-02-02 01:42:29 +00:00
|
|
|
|
|
|
|
// This processes pending attestation queues on every `processPendingAttsPeriod`.
|
|
|
|
func (s *Service) processPendingAttsQueue() {
|
2020-06-23 12:00:29 +00:00
|
|
|
// Prevents multiple queue processing goroutines (invoked by RunEvery) from contending for data.
|
2020-02-12 07:33:15 +00:00
|
|
|
mutex := new(sync.Mutex)
|
2021-09-18 17:26:11 +00:00
|
|
|
async.RunEvery(s.ctx, processPendingAttsPeriod, func() {
|
2020-02-12 07:33:15 +00:00
|
|
|
mutex.Lock()
|
2020-09-09 09:48:52 +00:00
|
|
|
if err := s.processPendingAtts(s.ctx); err != nil {
|
2020-08-04 20:30:40 +00:00
|
|
|
log.WithError(err).Debugf("Could not process pending attestation: %v", err)
|
2020-02-11 20:01:33 +00:00
|
|
|
}
|
2020-02-12 07:33:15 +00:00
|
|
|
mutex.Unlock()
|
2020-02-02 01:42:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// This defines how pending attestations are processed. It contains features:
|
|
|
|
// 1. Clean up invalid pending attestations from the queue.
|
|
|
|
// 2. Check if pending attestations can be processed when the block has arrived.
|
|
|
|
// 3. Request block from a random peer if unable to proceed step 2.
|
|
|
|
func (s *Service) processPendingAtts(ctx context.Context) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "processPendingAtts")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
// Before a node processes pending attestations queue, it verifies
|
|
|
|
// the attestations in the queue are still valid. Attestations will
|
|
|
|
// be deleted from the queue if invalid (ie. getting staled from falling too many slots behind).
|
2021-11-05 19:08:58 +00:00
|
|
|
s.validatePendingAtts(ctx, s.cfg.chain.CurrentSlot())
|
2020-02-02 01:42:29 +00:00
|
|
|
|
2020-02-13 02:33:15 +00:00
|
|
|
s.pendingAttsLock.RLock()
|
2020-10-26 21:17:07 +00:00
|
|
|
roots := make([][32]byte, 0, len(s.blkRootToPendingAtts))
|
2020-02-13 02:33:15 +00:00
|
|
|
for br := range s.blkRootToPendingAtts {
|
|
|
|
roots = append(roots, br)
|
|
|
|
}
|
|
|
|
s.pendingAttsLock.RUnlock()
|
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
var pendingRoots [][32]byte
|
2020-06-26 14:58:47 +00:00
|
|
|
randGen := rand.NewGenerator()
|
2020-02-13 02:33:15 +00:00
|
|
|
for _, bRoot := range roots {
|
|
|
|
s.pendingAttsLock.RLock()
|
|
|
|
attestations := s.blkRootToPendingAtts[bRoot]
|
|
|
|
s.pendingAttsLock.RUnlock()
|
2020-12-16 16:56:21 +00:00
|
|
|
// has the pending attestation's missing block arrived and the node processed block yet?
|
2021-11-05 19:08:58 +00:00
|
|
|
if s.cfg.beaconDB.HasBlock(ctx, bRoot) && (s.cfg.beaconDB.HasState(ctx, bRoot) || s.cfg.beaconDB.HasStateSummary(ctx, bRoot)) {
|
2022-06-18 10:14:43 +00:00
|
|
|
s.processAttestations(ctx, attestations)
|
2020-02-02 01:42:29 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"blockRoot": hex.EncodeToString(bytesutil.Trunc(bRoot[:])),
|
|
|
|
"pendingAttsCount": len(attestations),
|
2020-11-25 22:52:57 +00:00
|
|
|
}).Debug("Verified and saved pending attestations to pool")
|
2020-02-02 01:42:29 +00:00
|
|
|
|
|
|
|
// Delete the missing block root key from pending attestation queue so a node will not request for the block again.
|
2020-02-13 02:33:15 +00:00
|
|
|
s.pendingAttsLock.Lock()
|
2020-02-02 01:42:29 +00:00
|
|
|
delete(s.blkRootToPendingAtts, bRoot)
|
2020-02-13 02:33:15 +00:00
|
|
|
s.pendingAttsLock.Unlock()
|
2020-02-02 01:42:29 +00:00
|
|
|
} else {
|
|
|
|
// Pending attestation's missing block has not arrived yet.
|
2020-02-03 21:11:21 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
2021-11-05 19:08:58 +00:00
|
|
|
"currentSlot": s.cfg.chain.CurrentSlot(),
|
2020-04-14 20:27:03 +00:00
|
|
|
"attSlot": attestations[0].Message.Aggregate.Data.Slot,
|
2020-02-03 21:11:21 +00:00
|
|
|
"attCount": len(attestations),
|
|
|
|
"blockRoot": hex.EncodeToString(bytesutil.Trunc(bRoot[:])),
|
|
|
|
}).Debug("Requesting block for pending attestation")
|
2020-08-20 04:50:14 +00:00
|
|
|
pendingRoots = append(pendingRoots, bRoot)
|
2020-02-02 01:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-20 04:50:14 +00:00
|
|
|
return s.sendBatchRootRequest(ctx, pendingRoots, randGen)
|
2020-02-02 01:42:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 10:14:43 +00:00
|
|
|
func (s *Service) processAttestations(ctx context.Context, attestations []*ethpb.SignedAggregateAttestationAndProof) {
|
|
|
|
for _, signedAtt := range attestations {
|
|
|
|
att := signedAtt.Message
|
|
|
|
// The pending attestations can arrive in both aggregated and unaggregated forms,
|
|
|
|
// each from has distinct validation steps.
|
|
|
|
if helpers.IsAggregated(att.Aggregate) {
|
|
|
|
// Save the pending aggregated attestation to the pool if it passes the aggregated
|
|
|
|
// validation steps.
|
|
|
|
valRes, err := s.validateAggregatedAtt(ctx, signedAtt)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Debug("Pending aggregated attestation failed validation")
|
|
|
|
}
|
|
|
|
aggValid := pubsub.ValidationAccept == valRes
|
|
|
|
if s.validateBlockInAttestation(ctx, signedAtt) && aggValid {
|
|
|
|
if err := s.cfg.attPool.SaveAggregatedAttestation(att.Aggregate); err != nil {
|
|
|
|
log.WithError(err).Debug("Could not save aggregate attestation")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.setAggregatorIndexEpochSeen(att.Aggregate.Data.Target.Epoch, att.AggregatorIndex)
|
|
|
|
|
|
|
|
// Broadcasting the signed attestation again once a node is able to process it.
|
|
|
|
if err := s.cfg.p2p.Broadcast(ctx, signedAtt); err != nil {
|
|
|
|
log.WithError(err).Debug("Could not broadcast")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is an important validation before retrieving attestation pre state to defend against
|
|
|
|
// attestation's target intentionally reference checkpoint that's long ago.
|
|
|
|
// Verify current finalized checkpoint is an ancestor of the block defined by the attestation's beacon block root.
|
2023-02-25 16:39:13 +00:00
|
|
|
if !s.cfg.chain.InForkchoice(bytesutil.ToBytes32(att.Aggregate.Data.BeaconBlockRoot)) {
|
|
|
|
log.WithError(blockchain.ErrNotDescendantOfFinalized).Debug("Could not verify finalized consistency")
|
2022-06-18 10:14:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := s.cfg.chain.VerifyLmdFfgConsistency(ctx, att.Aggregate); err != nil {
|
|
|
|
log.WithError(err).Debug("Could not verify FFG consistency")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
preState, err := s.cfg.chain.AttestationTargetState(ctx, att.Aggregate.Data.Target)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Debug("Could not retrieve attestation prestate")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
valid, err := s.validateUnaggregatedAttWithState(ctx, att.Aggregate, preState)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Debug("Pending unaggregated attestation failed validation")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if valid == pubsub.ValidationAccept {
|
|
|
|
if err := s.cfg.attPool.SaveUnaggregatedAttestation(att.Aggregate); err != nil {
|
|
|
|
log.WithError(err).Debug("Could not save unaggregated attestation")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.setSeenCommitteeIndicesSlot(att.Aggregate.Data.Slot, att.Aggregate.Data.CommitteeIndex, att.Aggregate.AggregationBits)
|
|
|
|
|
|
|
|
valCount, err := helpers.ActiveValidatorCount(ctx, preState, slots.ToEpoch(att.Aggregate.Data.Slot))
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Debug("Could not retrieve active validator count")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Broadcasting the signed attestation again once a node is able to process it.
|
|
|
|
if err := s.cfg.p2p.BroadcastAttestation(ctx, helpers.ComputeSubnetForAttestation(valCount, signedAtt.Message.Aggregate), signedAtt.Message.Aggregate); err != nil {
|
|
|
|
log.WithError(err).Debug("Could not broadcast")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 01:42:29 +00:00
|
|
|
// This defines how pending attestations is saved in the map. The key is the
|
|
|
|
// root of the missing block. The value is the list of pending attestations
|
|
|
|
// that voted for that block root.
|
2020-04-14 20:27:03 +00:00
|
|
|
func (s *Service) savePendingAtt(att *ethpb.SignedAggregateAttestationAndProof) {
|
|
|
|
root := bytesutil.ToBytes32(att.Message.Aggregate.Data.BeaconBlockRoot)
|
2020-02-02 01:42:29 +00:00
|
|
|
|
2020-02-13 02:33:15 +00:00
|
|
|
s.pendingAttsLock.Lock()
|
|
|
|
defer s.pendingAttsLock.Unlock()
|
2020-02-02 01:42:29 +00:00
|
|
|
_, ok := s.blkRootToPendingAtts[root]
|
|
|
|
if !ok {
|
2020-04-14 20:27:03 +00:00
|
|
|
s.blkRootToPendingAtts[root] = []*ethpb.SignedAggregateAttestationAndProof{att}
|
2020-02-02 01:42:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-24 15:47:03 +00:00
|
|
|
// Skip if the attestation from the same aggregator already exists in the pending queue.
|
|
|
|
for _, a := range s.blkRootToPendingAtts[root] {
|
|
|
|
if a.Message.AggregatorIndex == att.Message.AggregatorIndex {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 01:42:29 +00:00
|
|
|
s.blkRootToPendingAtts[root] = append(s.blkRootToPendingAtts[root], att)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This validates the pending attestations in the queue are still valid.
|
|
|
|
// If not valid, a node will remove it in the queue in place. The validity
|
|
|
|
// check specifies the pending attestation could not fall one epoch behind
|
|
|
|
// of the current slot.
|
2023-01-26 14:40:12 +00:00
|
|
|
func (s *Service) validatePendingAtts(ctx context.Context, slot primitives.Slot) {
|
2022-07-14 17:00:33 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "validatePendingAtts")
|
2020-02-02 01:42:29 +00:00
|
|
|
defer span.End()
|
|
|
|
|
2020-02-13 02:33:15 +00:00
|
|
|
s.pendingAttsLock.Lock()
|
|
|
|
defer s.pendingAttsLock.Unlock()
|
|
|
|
|
2020-02-02 01:42:29 +00:00
|
|
|
for bRoot, atts := range s.blkRootToPendingAtts {
|
|
|
|
for i := len(atts) - 1; i >= 0; i-- {
|
2020-04-14 20:27:03 +00:00
|
|
|
if slot >= atts[i].Message.Aggregate.Data.Slot+params.BeaconConfig().SlotsPerEpoch {
|
2020-02-02 01:42:29 +00:00
|
|
|
// Remove the pending attestation from the list in place.
|
|
|
|
atts = append(atts[:i], atts[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.blkRootToPendingAtts[bRoot] = atts
|
|
|
|
|
|
|
|
// If the pending attestations list of a given block root is empty,
|
|
|
|
// a node will remove the key from the map to avoid dangling keys.
|
|
|
|
if len(s.blkRootToPendingAtts[bRoot]) == 0 {
|
|
|
|
delete(s.blkRootToPendingAtts, bRoot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|