From 3db8f2a00b0cbb0b714d3d4cd573038c6e1c391d Mon Sep 17 00:00:00 2001 From: Potuz Date: Thu, 29 Aug 2024 09:38:18 -0300 Subject: [PATCH] Receive ptc message (#14394) * Handle incoming ptc attestation messages in the chain package * fix double import * remove unused error --- beacon-chain/blockchain/BUILD.bazel | 1 + beacon-chain/blockchain/error.go | 3 ++ beacon-chain/blockchain/receive_block.go | 6 ++++ .../receive_payload_attestation_message.go | 33 +++++++++++++++++++ beacon-chain/blockchain/testing/mock.go | 5 +++ beacon-chain/sync/payload_attestations.go | 26 +-------------- beacon-chain/sync/service.go | 1 + 7 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 beacon-chain/blockchain/receive_payload_attestation_message.go diff --git a/beacon-chain/blockchain/BUILD.bazel b/beacon-chain/blockchain/BUILD.bazel index be6f3f6ddc8f..99fe2e490ca6 100644 --- a/beacon-chain/blockchain/BUILD.bazel +++ b/beacon-chain/blockchain/BUILD.bazel @@ -27,6 +27,7 @@ go_library( "receive_blob.go", "receive_block.go", "receive_execution_payload_envelope.go", + "receive_payload_attestation_message.go", "service.go", "tracked_proposer.go", "weak_subjectivity_checks.go", diff --git a/beacon-chain/blockchain/error.go b/beacon-chain/blockchain/error.go index 87ed0d2416db..abb218be45de 100644 --- a/beacon-chain/blockchain/error.go +++ b/beacon-chain/blockchain/error.go @@ -30,6 +30,9 @@ var ( ErrNotCheckpoint = errors.New("not a checkpoint in forkchoice") // ErrNilHead is returned when no head is present in the blockchain service. ErrNilHead = errors.New("nil head") + // errInvalidValidatorIndex is returned when a validator index is + // invalid or unexpected + errInvalidValidatorIndex = errors.New("invalid validator index") ) var errMaxBlobsExceeded = errors.New("Expected commitments in block exceeds MAX_BLOBS_PER_BLOCK") diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index 056632e2b970..ba1699273c19 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -47,6 +47,12 @@ type BlockReceiver interface { BlockBeingSynced([32]byte) bool } +// PayloadAttestationReceiver defines methods of the chain service for receiving +// and processing new payload attestations and payload attestation messages +type PayloadAttestationReceiver interface { + ReceivePayloadAttestationMessage(ctx context.Context, a *ethpb.PayloadAttestationMessage) error +} + // BlobReceiver interface defines the methods of chain service for receiving new // blobs type BlobReceiver interface { diff --git a/beacon-chain/blockchain/receive_payload_attestation_message.go b/beacon-chain/blockchain/receive_payload_attestation_message.go new file mode 100644 index 000000000000..4aa5c6d9648a --- /dev/null +++ b/beacon-chain/blockchain/receive_payload_attestation_message.go @@ -0,0 +1,33 @@ +package blockchain + +import ( + "context" + "slices" + + "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers" + "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" + eth "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" +) + +func (s *Service) ReceivePayloadAttestationMessage(ctx context.Context, a *eth.PayloadAttestationMessage) error { + if err := helpers.ValidateNilPayloadAttestationMessage(a); err != nil { + return err + } + root := [32]byte(a.Data.BeaconBlockRoot) + st, err := s.HeadState(ctx) + if err != nil { + return err + } + ptc, err := helpers.GetPayloadTimelinessCommittee(ctx, st, a.Data.Slot) + if err != nil { + return err + } + idx := slices.Index(ptc, a.ValidatorIndex) + if idx == -1 { + return errInvalidValidatorIndex + } + if s.cfg.PayloadAttestationCache.Seen(root, uint64(primitives.ValidatorIndex(idx))) { + return nil + } + return s.cfg.PayloadAttestationCache.Add(a, uint64(idx)) +} diff --git a/beacon-chain/blockchain/testing/mock.go b/beacon-chain/blockchain/testing/mock.go index 2809eb03db6b..0b4b665b0fcd 100644 --- a/beacon-chain/blockchain/testing/mock.go +++ b/beacon-chain/blockchain/testing/mock.go @@ -713,3 +713,8 @@ func (c *ChainService) TargetRootForEpoch(_ [32]byte, _ primitives.Epoch) ([32]b func (c *ChainService) HashInForkchoice([32]byte) bool { return false } + +// ReceivePayloadAttestationMessage mocks the same method in the chain service +func (c *ChainService) ReceivePayloadAttestationMessage(_ context.Context, _ *ethpb.PayloadAttestationMessage) error { + return nil +} diff --git a/beacon-chain/sync/payload_attestations.go b/beacon-chain/sync/payload_attestations.go index b3bff26bc09d..370c7e577674 100644 --- a/beacon-chain/sync/payload_attestations.go +++ b/beacon-chain/sync/payload_attestations.go @@ -2,15 +2,12 @@ package sync import ( "context" - "slices" pubsub "github.com/libp2p/go-libp2p-pubsub" "github.com/libp2p/go-libp2p/core/peer" "github.com/pkg/errors" - "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/v5/beacon-chain/verification" payloadattestation "github.com/prysmaticlabs/prysm/v5/consensus-types/epbs/payload-attestation" - "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" "github.com/prysmaticlabs/prysm/v5/monitoring/tracing" "github.com/prysmaticlabs/prysm/v5/monitoring/tracing/trace" eth "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" @@ -18,7 +15,6 @@ import ( ) var ( - errInvalidValidatorIndex = errors.New("invalid validator index") errAlreadySeenPayloadAttestation = errors.New("payload attestation already seen for validator index") ) @@ -91,25 +87,5 @@ func (s *Service) payloadAttestationSubscriber(ctx context.Context, msg proto.Me if !ok { return errWrongMessage } - if err := helpers.ValidateNilPayloadAttestationMessage(a); err != nil { - return err - } - root := [32]byte(a.Data.BeaconBlockRoot) - st, err := s.cfg.chain.HeadState(ctx) - if err != nil { - return err - } - ptc, err := helpers.GetPayloadTimelinessCommittee(ctx, st, a.Data.Slot) - if err != nil { - return err - } - idx := slices.Index(ptc, a.ValidatorIndex) - if idx == -1 { - return errInvalidValidatorIndex - } - if s.payloadAttestationCache.Seen(root, uint64(primitives.ValidatorIndex(idx))) { - return nil - } - - return s.payloadAttestationCache.Add(a, uint64(idx)) + return s.cfg.chain.ReceivePayloadAttestationMessage(ctx, a) } diff --git a/beacon-chain/sync/service.go b/beacon-chain/sync/service.go index 3001c8bc38d5..c2a5a28867e5 100644 --- a/beacon-chain/sync/service.go +++ b/beacon-chain/sync/service.go @@ -103,6 +103,7 @@ type config struct { // This defines the interface for interacting with block chain service type blockchainService interface { blockchain.BlockReceiver + blockchain.PayloadAttestationReceiver blockchain.BlobReceiver blockchain.HeadFetcher blockchain.FinalizationFetcher