2021-09-02 02:26:38 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-09-26 01:06:48 +00:00
|
|
|
"errors"
|
2021-09-02 02:26:38 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2022-10-07 07:24:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/altair"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/signing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p"
|
|
|
|
p2ptypes "github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p/types"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/crypto/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/monitoring/tracing"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
2021-09-02 02:26:38 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Sync committee subnets are used to propagate unaggregated sync committee messages to subsections of the network.
|
|
|
|
//
|
|
|
|
// The sync_committee_{subnet_id} topics are used to propagate unaggregated sync committee messages
|
|
|
|
// to the subnet subnet_id to be aggregated before being gossiped to the
|
|
|
|
// global sync_committee_contribution_and_proof topic.
|
|
|
|
//
|
|
|
|
// The following validations MUST pass before forwarding the sync_committee_message on the network:
|
|
|
|
//
|
|
|
|
// [IGNORE] The message's slot is for the current slot (with a MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance),
|
|
|
|
// i.e. sync_committee_message.slot == current_slot.
|
|
|
|
// [REJECT] The subnet_id is valid for the given validator, i.e. subnet_id in
|
|
|
|
// compute_subnets_for_sync_committee(state, sync_committee_message.validator_index).
|
|
|
|
// Note this validation implies the validator is part of the broader current sync
|
|
|
|
// committee along with the correct subcommittee.
|
|
|
|
// [IGNORE] There has been no other valid sync committee message for the declared slot for the
|
|
|
|
// validator referenced by sync_committee_message.validator_index (this requires maintaining
|
|
|
|
// a cache of size SYNC_COMMITTEE_SIZE // SYNC_COMMITTEE_SUBNET_COUNT for each subnet that can be
|
|
|
|
// flushed after each slot). Note this validation is per topic so that for a given slot, multiple
|
|
|
|
// messages could be forwarded with the same validator_index as long as the subnet_ids are distinct.
|
|
|
|
// [REJECT] The signature is valid for the message beacon_block_root for the validator referenced by validator_index.
|
|
|
|
func (s *Service) validateSyncCommitteeMessage(
|
|
|
|
ctx context.Context, pid peer.ID, msg *pubsub.Message,
|
2021-09-26 01:06:48 +00:00
|
|
|
) (pubsub.ValidationResult, error) {
|
2021-09-02 02:26:38 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "sync.validateSyncCommitteeMessage")
|
|
|
|
defer span.End()
|
|
|
|
|
2021-11-05 19:08:58 +00:00
|
|
|
if pid == s.cfg.p2p.PeerID() {
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationAccept, nil
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Basic validations before proceeding.
|
2021-11-05 19:08:58 +00:00
|
|
|
if s.cfg.initialSync.Syncing() {
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationIgnore, nil
|
2022-03-06 08:09:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 02:26:38 +00:00
|
|
|
if msg.Topic == nil {
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationReject, errInvalidTopic
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the data from the pubsub message, and reject if there is an error.
|
|
|
|
m, err := s.readSyncCommitteeMessage(msg)
|
|
|
|
if err != nil {
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationReject, err
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate sync message times before proceeding.
|
|
|
|
// The message's `slot` is for the current slot (with a MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance).
|
|
|
|
if err := altair.ValidateSyncMessageTime(
|
|
|
|
m.Slot,
|
2023-05-03 04:34:01 +00:00
|
|
|
s.cfg.clock.GenesisTime(),
|
2021-09-02 02:26:38 +00:00
|
|
|
params.BeaconNetworkConfig().MaximumGossipClockDisparity,
|
|
|
|
); err != nil {
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationIgnore, err
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 19:08:58 +00:00
|
|
|
committeeIndices, err := s.cfg.chain.HeadSyncCommitteeIndices(ctx, m.ValidatorIndex, m.Slot)
|
2021-09-02 02:26:38 +00:00
|
|
|
if err != nil {
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationIgnore, err
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the message's data according to the p2p specification.
|
2021-09-26 01:06:48 +00:00
|
|
|
if result, err := validationPipeline(
|
2021-09-02 02:26:38 +00:00
|
|
|
ctx,
|
|
|
|
ignoreEmptyCommittee(committeeIndices),
|
|
|
|
s.rejectIncorrectSyncCommittee(committeeIndices, *msg.Topic),
|
2023-05-19 14:47:39 +00:00
|
|
|
s.ignoreHasSeenSyncMsg(ctx, m, committeeIndices),
|
2021-09-02 02:26:38 +00:00
|
|
|
s.rejectInvalidSyncCommitteeSignature(m),
|
|
|
|
); result != pubsub.ValidationAccept {
|
2021-09-26 01:06:48 +00:00
|
|
|
return result, err
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.markSyncCommitteeMessagesSeen(committeeIndices, m)
|
|
|
|
|
|
|
|
msg.ValidatorData = m
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationAccept, nil
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a sync committee message from a pubsub message.
|
|
|
|
func (s *Service) readSyncCommitteeMessage(msg *pubsub.Message) (*ethpb.SyncCommitteeMessage, error) {
|
|
|
|
raw, err := s.decodePubsubMessage(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m, ok := raw.(*ethpb.SyncCommitteeMessage)
|
|
|
|
if !ok {
|
|
|
|
return nil, errWrongMessage
|
|
|
|
}
|
|
|
|
if m == nil {
|
|
|
|
return nil, errNilMessage
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark all a slot and validator index as seen for every index in a committee and subnet.
|
2023-01-26 14:40:12 +00:00
|
|
|
func (s *Service) markSyncCommitteeMessagesSeen(committeeIndices []primitives.CommitteeIndex, m *ethpb.SyncCommitteeMessage) {
|
2021-09-02 02:26:38 +00:00
|
|
|
subCommitteeSize := params.BeaconConfig().SyncCommitteeSize / params.BeaconConfig().SyncCommitteeSubnetCount
|
|
|
|
for _, idx := range committeeIndices {
|
|
|
|
subnet := uint64(idx) / subCommitteeSize
|
2023-05-19 14:47:39 +00:00
|
|
|
s.setSeenSyncMessageIndexSlot(m, subnet)
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the node has received sync committee for the validator with index and slot.
|
2023-05-19 14:47:39 +00:00
|
|
|
func (s *Service) hasSeenSyncMessageIndexSlot(ctx context.Context, m *ethpb.SyncCommitteeMessage, subCommitteeIndex uint64) bool {
|
2021-09-02 02:26:38 +00:00
|
|
|
s.seenSyncMessageLock.RLock()
|
|
|
|
defer s.seenSyncMessageLock.RUnlock()
|
2023-05-19 14:47:39 +00:00
|
|
|
rt, seen := s.seenSyncMessageCache.Get(seenSyncCommitteeKey(m.Slot, m.ValidatorIndex, subCommitteeIndex))
|
|
|
|
if !seen {
|
|
|
|
// return early if this is the first message
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
root, ok := rt.([32]byte)
|
|
|
|
if !ok {
|
|
|
|
return true // Impossible. Return true to be safe
|
|
|
|
}
|
|
|
|
if !s.cfg.chain.InForkchoice(root) && !s.cfg.beaconDB.HasBlock(ctx, root) {
|
2023-05-22 17:43:21 +00:00
|
|
|
syncMessagesForUnknownBlocks.Inc()
|
2023-05-19 14:47:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
msgRoot := [32]byte(m.BlockRoot)
|
|
|
|
if !s.cfg.chain.InForkchoice(msgRoot) && !s.cfg.beaconDB.HasBlock(ctx, msgRoot) {
|
2023-05-22 17:43:21 +00:00
|
|
|
syncMessagesForUnknownBlocks.Inc()
|
2023-05-19 14:47:39 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
headRoot := s.cfg.chain.CachedHeadRoot()
|
|
|
|
if root == headRoot {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return msgRoot != headRoot
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set sync committee message validator index and slot as seen.
|
2023-05-19 14:47:39 +00:00
|
|
|
func (s *Service) setSeenSyncMessageIndexSlot(m *ethpb.SyncCommitteeMessage, subCommitteeIndex uint64) {
|
2021-09-02 02:26:38 +00:00
|
|
|
s.seenSyncMessageLock.Lock()
|
|
|
|
defer s.seenSyncMessageLock.Unlock()
|
2023-05-19 14:47:39 +00:00
|
|
|
key := seenSyncCommitteeKey(m.Slot, m.ValidatorIndex, subCommitteeIndex)
|
|
|
|
s.seenSyncMessageCache.Add(key, [32]byte(m.BlockRoot))
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The `subnet_id` is valid for the given validator. This implies the validator is part of the broader
|
|
|
|
// current sync committee along with the correct subcommittee.
|
|
|
|
// We are trying to validate that whatever committee indices that were retrieved from our state for this
|
|
|
|
// particular validator are indeed valid for this particular topic. Ex: the topic name can be
|
|
|
|
// /eth2/b5303f2a/sync_committee_2/ssz_snappy
|
|
|
|
// This would mean that only messages meant for subnet 2 are valid. If a validator creates this sync
|
|
|
|
// message and broadcasts it into subnet 2, we need to make sure that whatever committee index and
|
|
|
|
// resultant subnet that the validator has is valid for this particular topic.
|
|
|
|
func (s *Service) rejectIncorrectSyncCommittee(
|
2023-01-26 14:40:12 +00:00
|
|
|
committeeIndices []primitives.CommitteeIndex, topic string,
|
2021-09-02 02:26:38 +00:00
|
|
|
) validationFn {
|
2021-09-26 01:06:48 +00:00
|
|
|
return func(ctx context.Context) (pubsub.ValidationResult, error) {
|
2023-12-15 16:49:27 +00:00
|
|
|
_, span := trace.StartSpan(ctx, "sync.rejectIncorrectSyncCommittee")
|
2021-09-02 02:26:38 +00:00
|
|
|
defer span.End()
|
|
|
|
isValid := false
|
2021-09-07 16:58:24 +00:00
|
|
|
digest, err := s.currentForkDigest()
|
2021-09-02 02:26:38 +00:00
|
|
|
if err != nil {
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationIgnore, err
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format := p2p.GossipTypeMapping[reflect.TypeOf(ðpb.SyncCommitteeMessage{})]
|
|
|
|
// Validate that the validator is in the correct committee.
|
|
|
|
subCommitteeSize := params.BeaconConfig().SyncCommitteeSize / params.BeaconConfig().SyncCommitteeSubnetCount
|
|
|
|
for _, idx := range committeeIndices {
|
|
|
|
subnet := uint64(idx) / subCommitteeSize
|
|
|
|
if strings.HasPrefix(topic, fmt.Sprintf(format, digest, subnet)) {
|
|
|
|
isValid = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isValid {
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationReject, errors.New("sync committee message references a different subnet")
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationAccept, nil
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There has been no other valid sync committee signature for the declared `slot`, `validator_index`,
|
|
|
|
// and `subcommittee_index`. In the event of `validator_index` belongs to multiple subnets, as long
|
|
|
|
// as one subnet has not been seen, we should let it in.
|
2023-05-19 14:47:39 +00:00
|
|
|
func (s *Service) ignoreHasSeenSyncMsg(ctx context.Context,
|
2023-01-26 14:40:12 +00:00
|
|
|
m *ethpb.SyncCommitteeMessage, committeeIndices []primitives.CommitteeIndex,
|
2021-09-02 02:26:38 +00:00
|
|
|
) validationFn {
|
2021-09-26 01:06:48 +00:00
|
|
|
return func(ctx context.Context) (pubsub.ValidationResult, error) {
|
2021-09-02 02:26:38 +00:00
|
|
|
var isValid bool
|
|
|
|
subCommitteeSize := params.BeaconConfig().SyncCommitteeSize / params.BeaconConfig().SyncCommitteeSubnetCount
|
|
|
|
for _, idx := range committeeIndices {
|
|
|
|
subnet := uint64(idx) / subCommitteeSize
|
2023-05-19 14:47:39 +00:00
|
|
|
if !s.hasSeenSyncMessageIndexSlot(ctx, m, subnet) {
|
2021-09-02 02:26:38 +00:00
|
|
|
isValid = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isValid {
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationIgnore, nil
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationAccept, nil
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) rejectInvalidSyncCommitteeSignature(m *ethpb.SyncCommitteeMessage) validationFn {
|
2021-09-26 01:06:48 +00:00
|
|
|
return func(ctx context.Context) (pubsub.ValidationResult, error) {
|
2021-09-02 02:26:38 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "sync.rejectInvalidSyncCommitteeSignature")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
// Ignore the message if it is not possible to retrieve the signing root.
|
|
|
|
// For internal errors, the correct behaviour is to ignore rather than reject outright,
|
|
|
|
// since the failure is locally derived.
|
2021-11-05 19:08:58 +00:00
|
|
|
d, err := s.cfg.chain.HeadSyncCommitteeDomain(ctx, m.Slot)
|
2021-09-02 02:26:38 +00:00
|
|
|
if err != nil {
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationIgnore, err
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
rawBytes := p2ptypes.SSZBytes(m.BlockRoot)
|
2021-09-27 16:19:20 +00:00
|
|
|
sigRoot, err := signing.ComputeSigningRoot(&rawBytes, d)
|
2021-09-02 02:26:38 +00:00
|
|
|
if err != nil {
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationIgnore, err
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reject for a validator index that is not found, as we should not remain peered with a node
|
|
|
|
// that is on such a different fork than our chain.
|
2021-11-05 19:08:58 +00:00
|
|
|
pubKey, err := s.cfg.chain.HeadValidatorIndexToPublicKey(ctx, m.ValidatorIndex)
|
2021-09-02 02:26:38 +00:00
|
|
|
if err != nil {
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationReject, err
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 05:49:50 +00:00
|
|
|
// Ignore a malformed public key from bytes according to the p2p specification.
|
|
|
|
pKey, err := bls.PublicKeyFromBytes(pubKey[:])
|
2021-09-02 02:26:38 +00:00
|
|
|
if err != nil {
|
2021-09-14 20:59:51 +00:00
|
|
|
tracing.AnnotateError(span, err)
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationIgnore, err
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 05:49:50 +00:00
|
|
|
// Batch verify message signature before unmarshalling
|
|
|
|
// the signature to a G2 point if batch verification is
|
|
|
|
// enabled.
|
2022-04-26 12:28:35 +00:00
|
|
|
set := &bls.SignatureBatch{
|
2022-12-20 10:41:47 +00:00
|
|
|
Messages: [][32]byte{sigRoot},
|
|
|
|
PublicKeys: []bls.PublicKey{pKey},
|
|
|
|
Signatures: [][]byte{m.Signature},
|
|
|
|
Descriptions: []string{signing.SyncCommitteeSignature},
|
2021-09-15 05:49:50 +00:00
|
|
|
}
|
2022-04-26 12:28:35 +00:00
|
|
|
return s.validateWithBatchVerifier(ctx, "sync committee message", set)
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
func ignoreEmptyCommittee(indices []primitives.CommitteeIndex) validationFn {
|
2021-09-26 01:06:48 +00:00
|
|
|
return func(ctx context.Context) (pubsub.ValidationResult, error) {
|
2021-09-02 02:26:38 +00:00
|
|
|
if len(indices) == 0 {
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationIgnore, nil
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationAccept, nil
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
func seenSyncCommitteeKey(slot primitives.Slot, valIndex primitives.ValidatorIndex, subCommitteeIndex uint64) string {
|
2021-09-02 02:26:38 +00:00
|
|
|
b := append(bytesutil.Bytes32(uint64(slot)), bytesutil.Bytes32(uint64(valIndex))...)
|
|
|
|
b = append(b, bytesutil.Bytes32(subCommitteeIndex)...)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2021-09-26 01:06:48 +00:00
|
|
|
func validationPipeline(ctx context.Context, fns ...validationFn) (pubsub.ValidationResult, error) {
|
2021-09-02 02:26:38 +00:00
|
|
|
for _, fn := range fns {
|
2021-09-26 01:06:48 +00:00
|
|
|
if result, err := fn(ctx); result != pubsub.ValidationAccept {
|
|
|
|
return result, err
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-26 01:06:48 +00:00
|
|
|
return pubsub.ValidationAccept, nil
|
2021-09-02 02:26:38 +00:00
|
|
|
}
|