mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 13:28:01 -05:00
Avoid Cloning When Creating a New Gossip Message (#14201)
* Add Current Changes * add back check * Avoid a Panic
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user