From f1740a6a5ae4288afd3b547f4064719236aecc37 Mon Sep 17 00:00:00 2001 From: Potuz Date: Thu, 8 Aug 2024 10:51:40 -0300 Subject: [PATCH] Move slasher handling down the pipeline (#14322) --- .../sync/validate_beacon_attestation.go | 80 ++++++++++--------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/beacon-chain/sync/validate_beacon_attestation.go b/beacon-chain/sync/validate_beacon_attestation.go index 2cea8638bbd4..860b0d8a2ba7 100644 --- a/beacon-chain/sync/validate_beacon_attestation.go +++ b/beacon-chain/sync/validate_beacon_attestation.go @@ -113,46 +113,19 @@ func (s *Service) validateCommitteeIndexBeaconAttestation(ctx context.Context, p committeeIndex = data.CommitteeIndex } - 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() - preState, err := s.cfg.chain.AttestationTargetState(ctx, data.Target) - if err != nil { - log.WithError(err).Error("Could not retrieve pre state") - tracing.AnnotateError(span, err) - return - } - committee, err := helpers.BeaconCommitteeFromState(ctx, preState, data.Slot, 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(&types.WrappedIndexedAtt{IndexedAtt: indexedAtt}) - }() - } - - // Verify this the first attestation received for the participating validator for the slot. - if s.hasSeenCommitteeIndicesSlot(data.Slot, data.CommitteeIndex, att.GetAggregationBits()) { - return pubsub.ValidationIgnore, nil - } + if !features.Get().EnableSlasher { + // Verify this the first attestation received for the participating validator for the slot. + if s.hasSeenCommitteeIndicesSlot(data.Slot, data.CommitteeIndex, att.GetAggregationBits()) { + return pubsub.ValidationIgnore, nil + } - // Reject an attestation if it references an invalid block. - if s.hasBadBlock(bytesutil.ToBytes32(data.BeaconBlockRoot)) || - s.hasBadBlock(bytesutil.ToBytes32(data.Target.Root)) || - s.hasBadBlock(bytesutil.ToBytes32(data.Source.Root)) { - attBadBlockCount.Inc() - return pubsub.ValidationReject, errors.New("attestation data references bad block root") + // Reject an attestation if it references an invalid block. + if s.hasBadBlock(bytesutil.ToBytes32(data.BeaconBlockRoot)) || + s.hasBadBlock(bytesutil.ToBytes32(data.Target.Root)) || + s.hasBadBlock(bytesutil.ToBytes32(data.Source.Root)) { + attBadBlockCount.Inc() + return pubsub.ValidationReject, errors.New("attestation data references bad block root") + } } // Verify the block being voted and the processed state is in beaconDB and the block has passed validation if it's in the beaconDB. @@ -203,6 +176,35 @@ func (s *Service) validateCommitteeIndexBeaconAttestation(ctx context.Context, p return validationRes, err } + 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() + preState, err := s.cfg.chain.AttestationTargetState(ctx, data.Target) + if err != nil { + log.WithError(err).Error("Could not retrieve pre state") + tracing.AnnotateError(span, err) + return + } + committee, err := helpers.BeaconCommitteeFromState(ctx, preState, data.Slot, 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(&types.WrappedIndexedAtt{IndexedAtt: indexedAtt}) + }() + } + s.setSeenCommitteeIndicesSlot(data.Slot, data.CommitteeIndex, att.GetAggregationBits()) msg.ValidatorData = att