From 354c563bf9066c9b9d9dbd2e235eddf1e252f4b2 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 11 May 2024 06:36:20 -0700 Subject: [PATCH 1/2] Add p2p support for Electra --- beacon-chain/p2p/fork_watcher.go | 3 ++- beacon-chain/p2p/gossip_topic_mappings.go | 5 +++++ beacon-chain/p2p/gossip_topic_mappings_test.go | 14 ++++++++++++++ beacon-chain/p2p/pubsub_filter.go | 6 ++++++ beacon-chain/p2p/types/object_mapping.go | 8 ++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/beacon-chain/p2p/fork_watcher.go b/beacon-chain/p2p/fork_watcher.go index a8de512d3320..3d02d57bb6f1 100644 --- a/beacon-chain/p2p/fork_watcher.go +++ b/beacon-chain/p2p/fork_watcher.go @@ -17,7 +17,8 @@ func (s *Service) forkWatcher() { if currEpoch == params.BeaconConfig().AltairForkEpoch || currEpoch == params.BeaconConfig().BellatrixForkEpoch || currEpoch == params.BeaconConfig().CapellaForkEpoch || - currEpoch == params.BeaconConfig().DenebForkEpoch { + currEpoch == params.BeaconConfig().DenebForkEpoch || + currEpoch == params.BeaconConfig().ElectraForkEpoch { // If we are in the fork epoch, we update our enr with // the updated fork digest. These repeatedly does // this over the epoch, which might be slightly wasteful diff --git a/beacon-chain/p2p/gossip_topic_mappings.go b/beacon-chain/p2p/gossip_topic_mappings.go index c81b6d11c5c7..76dfe722ed65 100644 --- a/beacon-chain/p2p/gossip_topic_mappings.go +++ b/beacon-chain/p2p/gossip_topic_mappings.go @@ -28,6 +28,9 @@ var gossipTopicMappings = map[string]proto.Message{ // versioned by epoch. func GossipTopicMappings(topic string, epoch primitives.Epoch) proto.Message { if topic == BlockSubnetTopicFormat { + if epoch >= params.BeaconConfig().ElectraForkEpoch { + return ðpb.SignedBeaconBlockElectra{} + } if epoch >= params.BeaconConfig().DenebForkEpoch { return ðpb.SignedBeaconBlockDeneb{} } @@ -70,4 +73,6 @@ func init() { GossipTypeMapping[reflect.TypeOf(ðpb.SignedBeaconBlockCapella{})] = BlockSubnetTopicFormat // Specially handle Deneb objects. GossipTypeMapping[reflect.TypeOf(ðpb.SignedBeaconBlockDeneb{})] = BlockSubnetTopicFormat + // Specially handle Electra objects. + GossipTypeMapping[reflect.TypeOf(ðpb.SignedBeaconBlockElectra{})] = BlockSubnetTopicFormat } diff --git a/beacon-chain/p2p/gossip_topic_mappings_test.go b/beacon-chain/p2p/gossip_topic_mappings_test.go index 0a0f8cadba79..7b32c658d9da 100644 --- a/beacon-chain/p2p/gossip_topic_mappings_test.go +++ b/beacon-chain/p2p/gossip_topic_mappings_test.go @@ -28,6 +28,8 @@ func TestGossipTopicMappings_CorrectBlockType(t *testing.T) { altairForkEpoch := primitives.Epoch(100) BellatrixForkEpoch := primitives.Epoch(200) CapellaForkEpoch := primitives.Epoch(300) + DenebForkEpoch := primitives.Epoch(400) + ElectraForkEpoch := primitives.Epoch(500) bCfg.AltairForkEpoch = altairForkEpoch bCfg.BellatrixForkEpoch = BellatrixForkEpoch @@ -35,6 +37,8 @@ func TestGossipTopicMappings_CorrectBlockType(t *testing.T) { bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.AltairForkVersion)] = primitives.Epoch(100) bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.BellatrixForkVersion)] = primitives.Epoch(200) bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.CapellaForkVersion)] = primitives.Epoch(300) + bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.DenebForkVersion)] = primitives.Epoch(400) + bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.ElectraForkVersion)] = primitives.Epoch(500) params.OverrideBeaconConfig(bCfg) // Phase 0 @@ -56,4 +60,14 @@ func TestGossipTopicMappings_CorrectBlockType(t *testing.T) { pMessage = GossipTopicMappings(BlockSubnetTopicFormat, CapellaForkEpoch) _, ok = pMessage.(*ethpb.SignedBeaconBlockCapella) assert.Equal(t, true, ok) + + // Deneb Fork + pMessage = GossipTopicMappings(BlockSubnetTopicFormat, DenebForkEpoch) + _, ok = pMessage.(*ethpb.SignedBeaconBlockDeneb) + assert.Equal(t, true, ok) + + // Electra Fork + pMessage = GossipTopicMappings(BlockSubnetTopicFormat, ElectraForkEpoch) + _, ok = pMessage.(*ethpb.SignedBeaconBlockElectra) + assert.Equal(t, true, ok) } diff --git a/beacon-chain/p2p/pubsub_filter.go b/beacon-chain/p2p/pubsub_filter.go index 3c2c8e99b0de..e02371c587f9 100644 --- a/beacon-chain/p2p/pubsub_filter.go +++ b/beacon-chain/p2p/pubsub_filter.go @@ -62,12 +62,18 @@ func (s *Service) CanSubscribe(topic string) bool { log.WithError(err).Error("Could not determine Deneb fork digest") return false } + electraForkDigest, err := forks.ForkDigestFromEpoch(params.BeaconConfig().ElectraForkEpoch, s.genesisValidatorsRoot) + if err != nil { + log.WithError(err).Error("Could not determine Electra fork digest") + return false + } switch parts[2] { case fmt.Sprintf("%x", phase0ForkDigest): case fmt.Sprintf("%x", altairForkDigest): case fmt.Sprintf("%x", bellatrixForkDigest): case fmt.Sprintf("%x", capellaForkDigest): case fmt.Sprintf("%x", denebForkDigest): + case fmt.Sprintf("%x", electraForkDigest): default: return false } diff --git a/beacon-chain/p2p/types/object_mapping.go b/beacon-chain/p2p/types/object_mapping.go index 34e0dd1f4e5f..2698bf93f54a 100644 --- a/beacon-chain/p2p/types/object_mapping.go +++ b/beacon-chain/p2p/types/object_mapping.go @@ -59,6 +59,11 @@ func InitializeDataMaps() { ðpb.SignedBeaconBlockDeneb{Block: ðpb.BeaconBlockDeneb{Body: ðpb.BeaconBlockBodyDeneb{ExecutionPayload: &enginev1.ExecutionPayloadDeneb{}}}}, ) }, + bytesutil.ToBytes4(params.BeaconConfig().ElectraForkVersion): func() (interfaces.ReadOnlySignedBeaconBlock, error) { + return blocks.NewSignedBeaconBlock( + ðpb.SignedBeaconBlockElectra{Block: ðpb.BeaconBlockElectra{Body: ðpb.BeaconBlockBodyElectra{ExecutionPayload: &enginev1.ExecutionPayloadElectra{}}}}, + ) + }, } // Reset our metadata map. @@ -78,5 +83,8 @@ func InitializeDataMaps() { bytesutil.ToBytes4(params.BeaconConfig().DenebForkVersion): func() metadata.Metadata { return wrapper.WrappedMetadataV1(ðpb.MetaDataV1{}) }, + bytesutil.ToBytes4(params.BeaconConfig().ElectraForkVersion): func() metadata.Metadata { + return wrapper.WrappedMetadataV1(ðpb.MetaDataV1{}) + }, } } From 568a2ffa41b28bf92c68d4cefd2f8b8e3ae84ae6 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 11 May 2024 07:51:21 -0700 Subject: [PATCH 2/2] Fix TestGossipTopicMappings_CorrectBlockType --- beacon-chain/p2p/gossip_topic_mappings_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/beacon-chain/p2p/gossip_topic_mappings_test.go b/beacon-chain/p2p/gossip_topic_mappings_test.go index 7b32c658d9da..88b03ba642fa 100644 --- a/beacon-chain/p2p/gossip_topic_mappings_test.go +++ b/beacon-chain/p2p/gossip_topic_mappings_test.go @@ -34,6 +34,8 @@ func TestGossipTopicMappings_CorrectBlockType(t *testing.T) { bCfg.AltairForkEpoch = altairForkEpoch bCfg.BellatrixForkEpoch = BellatrixForkEpoch bCfg.CapellaForkEpoch = CapellaForkEpoch + bCfg.DenebForkEpoch = DenebForkEpoch + bCfg.ElectraForkEpoch = ElectraForkEpoch bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.AltairForkVersion)] = primitives.Epoch(100) bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.BellatrixForkVersion)] = primitives.Epoch(200) bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.CapellaForkVersion)] = primitives.Epoch(300)