From d0d7021c1dbac3c3d57e2ca5ff7c5cf4aa151f97 Mon Sep 17 00:00:00 2001 From: terencechain Date: Wed, 9 Nov 2022 15:11:46 -0800 Subject: [PATCH] Add Capella p2p changes (#11644) --- beacon-chain/p2p/fork_watcher.go | 6 ++++-- beacon-chain/p2p/gossip_topic_mappings.go | 6 ++++++ beacon-chain/p2p/gossip_topic_mappings_test.go | 7 +++++++ beacon-chain/p2p/message_id.go | 15 ++++++++------- beacon-chain/p2p/pubsub_filter.go | 6 ++++++ beacon-chain/p2p/types/object_mapping.go | 8 ++++++++ 6 files changed, 39 insertions(+), 9 deletions(-) diff --git a/beacon-chain/p2p/fork_watcher.go b/beacon-chain/p2p/fork_watcher.go index cbd3c20adb72..bf1167188a39 100644 --- a/beacon-chain/p2p/fork_watcher.go +++ b/beacon-chain/p2p/fork_watcher.go @@ -16,7 +16,8 @@ func (s *Service) forkWatcher() { case currSlot := <-slotTicker.C(): currEpoch := slots.ToEpoch(currSlot) if currEpoch == params.BeaconConfig().AltairForkEpoch || - currEpoch == params.BeaconConfig().BellatrixForkEpoch { + currEpoch == params.BeaconConfig().BellatrixForkEpoch || + currEpoch == params.BeaconConfig().CapellaForkEpoch { // 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 @@ -27,7 +28,8 @@ func (s *Service) forkWatcher() { } // from Bellatrix Epoch, the MaxGossipSize and the MaxChunkSize is changed to 10Mb. - if currEpoch == params.BeaconConfig().BellatrixForkEpoch { + if currEpoch == params.BeaconConfig().BellatrixForkEpoch || + currEpoch == params.BeaconConfig().CapellaForkEpoch { encoder.SetMaxGossipSizeForBellatrix() encoder.SetMaxChunkSizeForBellatrix() } diff --git a/beacon-chain/p2p/gossip_topic_mappings.go b/beacon-chain/p2p/gossip_topic_mappings.go index 48d2eaae1acc..2f674c22a15e 100644 --- a/beacon-chain/p2p/gossip_topic_mappings.go +++ b/beacon-chain/p2p/gossip_topic_mappings.go @@ -26,6 +26,9 @@ var gossipTopicMappings = map[string]proto.Message{ // versioned by epoch. func GossipTopicMappings(topic string, epoch types.Epoch) proto.Message { if topic == BlockSubnetTopicFormat { + if epoch >= params.BeaconConfig().CapellaForkEpoch { + return ðpb.SignedBeaconBlockCapella{} + } if epoch >= params.BeaconConfig().BellatrixForkEpoch { return ðpb.SignedBeaconBlockBellatrix{} } @@ -59,4 +62,7 @@ func init() { // Specially handle Bellatrix objects. GossipTypeMapping[reflect.TypeOf(ðpb.SignedBeaconBlockBellatrix{})] = BlockSubnetTopicFormat GossipTypeMapping[reflect.TypeOf(ðpb.SignedBlindedBeaconBlockBellatrix{})] = BlockSubnetTopicFormat + // Specially handle Capella objects + GossipTypeMapping[reflect.TypeOf(ðpb.SignedBeaconBlockCapella{})] = BlockSubnetTopicFormat + GossipTypeMapping[reflect.TypeOf(ðpb.SignedBlindedBeaconBlockCapella{})] = BlockSubnetTopicFormat } diff --git a/beacon-chain/p2p/gossip_topic_mappings_test.go b/beacon-chain/p2p/gossip_topic_mappings_test.go index e1671aab6ddb..9b8dd271c479 100644 --- a/beacon-chain/p2p/gossip_topic_mappings_test.go +++ b/beacon-chain/p2p/gossip_topic_mappings_test.go @@ -27,11 +27,14 @@ func TestGossipTopicMappings_CorrectBlockType(t *testing.T) { bCfg := params.BeaconConfig().Copy() altairForkEpoch := eth2types.Epoch(100) BellatrixForkEpoch := eth2types.Epoch(200) + CapellaForkEpoch := eth2types.Epoch(300) bCfg.AltairForkEpoch = altairForkEpoch bCfg.BellatrixForkEpoch = BellatrixForkEpoch + bCfg.CapellaForkEpoch = CapellaForkEpoch bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.AltairForkVersion)] = eth2types.Epoch(100) bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.BellatrixForkVersion)] = eth2types.Epoch(200) + bCfg.ForkVersionSchedule[bytesutil.ToBytes4(bCfg.CapellaForkVersion)] = eth2types.Epoch(300) params.OverrideBeaconConfig(bCfg) // Phase 0 @@ -49,4 +52,8 @@ func TestGossipTopicMappings_CorrectBlockType(t *testing.T) { _, ok = pMessage.(*ethpb.SignedBeaconBlockBellatrix) assert.Equal(t, true, ok) + // Capella Fork + pMessage = GossipTopicMappings(BlockSubnetTopicFormat, CapellaForkEpoch) + _, ok = pMessage.(*ethpb.SignedBeaconBlockCapella) + assert.Equal(t, true, ok) } diff --git a/beacon-chain/p2p/message_id.go b/beacon-chain/p2p/message_id.go index 95e7b3061a66..0a92f00f5626 100644 --- a/beacon-chain/p2p/message_id.go +++ b/beacon-chain/p2p/message_id.go @@ -14,14 +14,15 @@ import ( // MsgID is a content addressable ID function. // // Ethereum Beacon Chain spec defines the message ID as: -// The `message-id` of a gossipsub message MUST be the following 20 byte value computed from the message data: -// If `message.data` has a valid snappy decompression, set `message-id` to the first 20 bytes of the `SHA256` hash of -// the concatenation of `MESSAGE_DOMAIN_VALID_SNAPPY` with the snappy decompressed message data, -// i.e. `SHA256(MESSAGE_DOMAIN_VALID_SNAPPY + snappy_decompress(message.data))[:20]`. // -// Otherwise, set `message-id` to the first 20 bytes of the `SHA256` hash of -// the concatenation of `MESSAGE_DOMAIN_INVALID_SNAPPY` with the raw message data, -// i.e. `SHA256(MESSAGE_DOMAIN_INVALID_SNAPPY + message.data)[:20]`. +// The `message-id` of a gossipsub message MUST be the following 20 byte value computed from the message data: +// If `message.data` has a valid snappy decompression, set `message-id` to the first 20 bytes of the `SHA256` hash of +// the concatenation of `MESSAGE_DOMAIN_VALID_SNAPPY` with the snappy decompressed message data, +// i.e. `SHA256(MESSAGE_DOMAIN_VALID_SNAPPY + snappy_decompress(message.data))[:20]`. +// +// Otherwise, set `message-id` to the first 20 bytes of the `SHA256` hash of +// the concatenation of `MESSAGE_DOMAIN_INVALID_SNAPPY` with the raw message data, +// i.e. `SHA256(MESSAGE_DOMAIN_INVALID_SNAPPY + message.data)[:20]`. func MsgID(genesisValidatorsRoot []byte, pmsg *pubsubpb.Message) string { if pmsg == nil || pmsg.Data == nil || pmsg.Topic == nil { // Impossible condition that should diff --git a/beacon-chain/p2p/pubsub_filter.go b/beacon-chain/p2p/pubsub_filter.go index ddd2891432eb..e819e27c6da9 100644 --- a/beacon-chain/p2p/pubsub_filter.go +++ b/beacon-chain/p2p/pubsub_filter.go @@ -52,11 +52,17 @@ func (s *Service) CanSubscribe(topic string) bool { log.WithError(err).Error("Could not determine Bellatrix fork digest") return false } + capellaForkDigest, err := forks.ForkDigestFromEpoch(params.BeaconConfig().CapellaForkEpoch, s.genesisValidatorsRoot) + if err != nil { + log.WithError(err).Error("Could not determine Capella 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): default: return false } diff --git a/beacon-chain/p2p/types/object_mapping.go b/beacon-chain/p2p/types/object_mapping.go index 97a07ca5f41e..2cc7aa79e32e 100644 --- a/beacon-chain/p2p/types/object_mapping.go +++ b/beacon-chain/p2p/types/object_mapping.go @@ -48,6 +48,11 @@ func InitializeDataMaps() { ðpb.SignedBeaconBlockBellatrix{Block: ðpb.BeaconBlockBellatrix{Body: ðpb.BeaconBlockBodyBellatrix{}}}, ) }, + bytesutil.ToBytes4(params.BeaconConfig().CapellaForkVersion): func() (interfaces.SignedBeaconBlock, error) { + return blocks.NewSignedBeaconBlock( + ðpb.SignedBeaconBlockCapella{Block: ðpb.BeaconBlockCapella{Body: ðpb.BeaconBlockBodyCapella{}}}, + ) + }, } // Reset our metadata map. @@ -61,5 +66,8 @@ func InitializeDataMaps() { bytesutil.ToBytes4(params.BeaconConfig().BellatrixForkVersion): func() metadata.Metadata { return wrapper.WrappedMetadataV1(ðpb.MetaDataV1{}) }, + bytesutil.ToBytes4(params.BeaconConfig().CapellaForkVersion): func() metadata.Metadata { + return wrapper.WrappedMetadataV1(ðpb.MetaDataV1{}) + }, } }