diff --git a/beacon-chain/p2p/gossip_topic_mappings.go b/beacon-chain/p2p/gossip_topic_mappings.go index 31733df0aa83..d88a4499ce2b 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,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 { @@ -81,7 +89,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..1ec9d079448a 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,7 +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) + m, ok := base.(ssz.Unmarshaler) if !ok { return nil, errors.Errorf("message of %T does not support marshaller interface", base) }