From 55642b2cf74bda1445c15183dbd50d8bf2b27dd4 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 10 Jul 2024 13:02:17 +0800 Subject: [PATCH 1/3] Add Current Changes --- beacon-chain/p2p/gossip_topic_mappings.go | 34 +++++++++---------- .../p2p/gossip_topic_mappings_test.go | 2 +- beacon-chain/sync/decode_pubsub.go | 6 +--- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/beacon-chain/p2p/gossip_topic_mappings.go b/beacon-chain/p2p/gossip_topic_mappings.go index 31733df0aa83..fb10d70650eb 100644 --- a/beacon-chain/p2p/gossip_topic_mappings.go +++ b/beacon-chain/p2p/gossip_topic_mappings.go @@ -11,17 +11,17 @@ import ( // gossipTopicMappings represent the protocol ID to protobuf message type map for easy // lookup. -var gossipTopicMappings = map[string]proto.Message{ - BlockSubnetTopicFormat: ðpb.SignedBeaconBlock{}, - AttestationSubnetTopicFormat: ðpb.Attestation{}, - ExitSubnetTopicFormat: ðpb.SignedVoluntaryExit{}, - ProposerSlashingSubnetTopicFormat: ðpb.ProposerSlashing{}, - AttesterSlashingSubnetTopicFormat: ðpb.AttesterSlashing{}, - AggregateAndProofSubnetTopicFormat: ðpb.SignedAggregateAttestationAndProof{}, - SyncContributionAndProofSubnetTopicFormat: ðpb.SignedContributionAndProof{}, - SyncCommitteeSubnetTopicFormat: ðpb.SyncCommitteeMessage{}, - BlsToExecutionChangeSubnetTopicFormat: ðpb.SignedBLSToExecutionChange{}, - BlobSubnetTopicFormat: ðpb.BlobSidecar{}, +var gossipTopicMappings = map[string]func() proto.Message{ + BlockSubnetTopicFormat: func() proto.Message { return ðpb.SignedBeaconBlock{} }, + AttestationSubnetTopicFormat: func() proto.Message { return ðpb.Attestation{} }, + ExitSubnetTopicFormat: func() proto.Message { return ðpb.SignedVoluntaryExit{} }, + ProposerSlashingSubnetTopicFormat: func() proto.Message { return ðpb.ProposerSlashing{} }, + AttesterSlashingSubnetTopicFormat: func() proto.Message { return ðpb.AttesterSlashing{} }, + AggregateAndProofSubnetTopicFormat: func() proto.Message { return ðpb.SignedAggregateAttestationAndProof{} }, + SyncContributionAndProofSubnetTopicFormat: func() proto.Message { return ðpb.SignedContributionAndProof{} }, + SyncCommitteeSubnetTopicFormat: func() proto.Message { return ðpb.SyncCommitteeMessage{} }, + BlsToExecutionChangeSubnetTopicFormat: func() proto.Message { return ðpb.SignedBLSToExecutionChange{} }, + BlobSubnetTopicFormat: func() proto.Message { return ðpb.BlobSidecar{} }, } // GossipTopicMappings is a function to return the assigned data type @@ -44,24 +44,24 @@ func GossipTopicMappings(topic string, epoch primitives.Epoch) proto.Message { if epoch >= params.BeaconConfig().AltairForkEpoch { return ðpb.SignedBeaconBlockAltair{} } - return gossipTopicMappings[topic] + return gossipTopicMappings[topic]() case AttestationSubnetTopicFormat: if epoch >= params.BeaconConfig().ElectraForkEpoch { return ðpb.AttestationElectra{} } - return gossipTopicMappings[topic] + return gossipTopicMappings[topic]() case AttesterSlashingSubnetTopicFormat: if epoch >= params.BeaconConfig().ElectraForkEpoch { return ðpb.AttesterSlashingElectra{} } - return gossipTopicMappings[topic] + return gossipTopicMappings[topic]() case AggregateAndProofSubnetTopicFormat: if epoch >= params.BeaconConfig().ElectraForkEpoch { return ðpb.SignedAggregateAttestationAndProofElectra{} } - return gossipTopicMappings[topic] + return gossipTopicMappings[topic]() default: - return gossipTopicMappings[topic] + return gossipTopicMappings[topic]() } } @@ -81,7 +81,7 @@ var GossipTypeMapping = make(map[reflect.Type]string, len(gossipTopicMappings)) func init() { for k, v := range gossipTopicMappings { - GossipTypeMapping[reflect.TypeOf(v)] = k + GossipTypeMapping[reflect.TypeOf(v())] = k } // Specially handle Altair objects. GossipTypeMapping[reflect.TypeOf(ðpb.SignedBeaconBlockAltair{})] = BlockSubnetTopicFormat diff --git a/beacon-chain/p2p/gossip_topic_mappings_test.go b/beacon-chain/p2p/gossip_topic_mappings_test.go index efe7f00e6e4f..2c134f425fa6 100644 --- a/beacon-chain/p2p/gossip_topic_mappings_test.go +++ b/beacon-chain/p2p/gossip_topic_mappings_test.go @@ -15,7 +15,7 @@ func TestMappingHasNoDuplicates(t *testing.T) { params.SetupTestConfigCleanup(t) m := make(map[reflect.Type]bool) for _, v := range gossipTopicMappings { - if _, ok := m[reflect.TypeOf(v)]; ok { + if _, ok := m[reflect.TypeOf(v())]; ok { t.Errorf("%T is duplicated in the topic mapping", v) } m[reflect.TypeOf(v)] = true diff --git a/beacon-chain/sync/decode_pubsub.go b/beacon-chain/sync/decode_pubsub.go index a0e6070149c2..fd035430c5e7 100644 --- a/beacon-chain/sync/decode_pubsub.go +++ b/beacon-chain/sync/decode_pubsub.go @@ -16,7 +16,6 @@ import ( "github.com/prysmaticlabs/prysm/v5/config/params" "github.com/prysmaticlabs/prysm/v5/encoding/bytesutil" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" - "google.golang.org/protobuf/proto" ) var errNilPubsubMessage = errors.New("nil pubsub message") @@ -52,10 +51,7 @@ func (s *Service) decodePubsubMessage(msg *pubsub.Message) (ssz.Unmarshaler, err if base == nil { return nil, p2p.ErrMessageNotMapped } - m, ok := proto.Clone(base).(ssz.Unmarshaler) - if !ok { - return nil, errors.Errorf("message of %T does not support marshaller interface", base) - } + m := base.(ssz.Unmarshaler) // Handle different message types across forks. dt, err := extractValidDataTypeFromTopic(topic, fDigest[:], s.cfg.clock) if err != nil { From ba3484b4c419060f6fae86e460d2961da2ffe70b Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 10 Jul 2024 13:06:40 +0800 Subject: [PATCH 2/3] add back check --- beacon-chain/sync/decode_pubsub.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/beacon-chain/sync/decode_pubsub.go b/beacon-chain/sync/decode_pubsub.go index fd035430c5e7..1ec9d079448a 100644 --- a/beacon-chain/sync/decode_pubsub.go +++ b/beacon-chain/sync/decode_pubsub.go @@ -51,7 +51,10 @@ func (s *Service) decodePubsubMessage(msg *pubsub.Message) (ssz.Unmarshaler, err if base == nil { return nil, p2p.ErrMessageNotMapped } - m := base.(ssz.Unmarshaler) + m, ok := base.(ssz.Unmarshaler) + if !ok { + return nil, errors.Errorf("message of %T does not support marshaller interface", base) + } // Handle different message types across forks. dt, err := extractValidDataTypeFromTopic(topic, fDigest[:], s.cfg.clock) if err != nil { From f86bab993cd4a332c3939ffafe7e106daee4c1d6 Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 10 Jul 2024 13:30:24 +0800 Subject: [PATCH 3/3] Avoid a Panic --- beacon-chain/p2p/gossip_topic_mappings.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/beacon-chain/p2p/gossip_topic_mappings.go b/beacon-chain/p2p/gossip_topic_mappings.go index fb10d70650eb..d88a4499ce2b 100644 --- a/beacon-chain/p2p/gossip_topic_mappings.go +++ b/beacon-chain/p2p/gossip_topic_mappings.go @@ -44,27 +44,35 @@ func GossipTopicMappings(topic string, epoch primitives.Epoch) proto.Message { if epoch >= params.BeaconConfig().AltairForkEpoch { return ðpb.SignedBeaconBlockAltair{} } - return gossipTopicMappings[topic]() + return gossipMessage(topic) case AttestationSubnetTopicFormat: if epoch >= params.BeaconConfig().ElectraForkEpoch { return ðpb.AttestationElectra{} } - return gossipTopicMappings[topic]() + return gossipMessage(topic) case AttesterSlashingSubnetTopicFormat: if epoch >= params.BeaconConfig().ElectraForkEpoch { return ðpb.AttesterSlashingElectra{} } - return gossipTopicMappings[topic]() + return gossipMessage(topic) case AggregateAndProofSubnetTopicFormat: if epoch >= params.BeaconConfig().ElectraForkEpoch { return ðpb.SignedAggregateAttestationAndProofElectra{} } - return gossipTopicMappings[topic]() + return gossipMessage(topic) default: - return gossipTopicMappings[topic]() + return gossipMessage(topic) } } +func gossipMessage(topic string) proto.Message { + msgGen, ok := gossipTopicMappings[topic] + if !ok { + return nil + } + return msgGen() +} + // AllTopics returns all topics stored in our // gossip mapping. func AllTopics() []string {