From 1f858262da54c52cd5edb59b2d41d1e16976786a Mon Sep 17 00:00:00 2001 From: Agnish Ghosh Date: Sat, 10 Aug 2024 00:21:39 +0530 Subject: [PATCH 1/5] init: add metadatav3, save progress --- beacon_chain/networking/eth2_network.nim | 8 ++++---- beacon_chain/networking/peer_protocol.nim | 4 ++++ beacon_chain/spec/datatypes/eip7594.nim | 9 ++++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/beacon_chain/networking/eth2_network.nim b/beacon_chain/networking/eth2_network.nim index 4e3d6e5933..e14df26631 100644 --- a/beacon_chain/networking/eth2_network.nim +++ b/beacon_chain/networking/eth2_network.nim @@ -69,7 +69,7 @@ type protocols: seq[ProtocolInfo] ## Protocols managed by the DSL and mounted on the switch protocolStates*: seq[RootRef] - metadata*: altair.MetaData + metadata*: eip7594.MetaData connectTimeout*: chronos.Duration seenThreshold*: chronos.Duration connQueue: AsyncQueue[PeerAddr] @@ -106,7 +106,7 @@ type lastReqTime*: Moment connections*: int enr*: Opt[enr.Record] - metadata*: Opt[altair.MetaData] + metadata*: Opt[eip7594.MetaData] failedMetadataRequests: int lastMetadataTime*: Moment direction*: PeerType @@ -1795,7 +1795,7 @@ proc new(T: type Eth2Node, let connectTimeout = chronos.seconds(10) seenThreshold = chronos.seconds(10) - type MetaData = altair.MetaData # Weird bug without this.. + type MetaData = eip7594.MetaData # Weird bug without this.. # Versions up to v22.3.0 would write an empty `MetaData` to #`data-dir/node-metadata.json` which would then be reloaded on startup - don't @@ -2082,7 +2082,7 @@ proc updatePeerMetadata(node: Eth2Node, peerId: PeerId) {.async: (raises: [Cance let peer = node.getPeer(peerId) - newMetadataRes = await peer.getMetadata_v2() + newMetadataRes = await peer.getMetadata_v3() newMetadata = newMetadataRes.valueOr: debug "Failed to retrieve metadata from peer!", peerId, error = newMetadataRes.error peer.failedMetadataRequests.inc() diff --git a/beacon_chain/networking/peer_protocol.nim b/beacon_chain/networking/peer_protocol.nim index ff47b17dd3..bdda7fcc98 100644 --- a/beacon_chain/networking/peer_protocol.nim +++ b/beacon_chain/networking/peer_protocol.nim @@ -179,6 +179,10 @@ p2pProtocol PeerSync(version = 1, {.libp2pProtocol("metadata", 2).} = peer.network.metadata + proc getMetadata_v3(peer: ): eip7594.MetaData + {.libp2pProtocol("metadata", 3).} = + peer.network.metadata + proc goodbye(peer: Peer, reason: uint64) {.async, libp2pProtocol("goodbye", 1).} = debug "Received Goodbye message", reason = disconnectReasonName(reason), peer diff --git a/beacon_chain/spec/datatypes/eip7594.nim b/beacon_chain/spec/datatypes/eip7594.nim index 8ba81174b6..5758d7af84 100644 --- a/beacon_chain/spec/datatypes/eip7594.nim +++ b/beacon_chain/spec/datatypes/eip7594.nim @@ -9,7 +9,7 @@ import std/[sequtils], - "."/[base, deneb], + "."/[base, deneb, altair], kzg4844, stew/[byteutils] @@ -79,6 +79,13 @@ type CscBits* = BitArray[DATA_COLUMN_SIDECAR_SUBNET_COUNT] + # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/altair/p2p-interface.md#metadata + MetaData* = object + seq_number*: uint64 + attnets*: AttnetBits + syncnets*: SyncnetBits + custody_subnet_count*: uint64 + # func serializeDataColumn(data_column: DataColumn): auto = # var counter = 0 # var serd : array[MAX_BLOB_COMMITMENTS_PER_BLOCK * BYTES_PER_CELL, byte] From 792651f8374dea16efa3ef1d1093b9fb18407e0f Mon Sep 17 00:00:00 2001 From: Agnish Ghosh Date: Sun, 11 Aug 2024 20:28:01 +0530 Subject: [PATCH 2/5] fix import issues --- beacon_chain/networking/peer_protocol.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/beacon_chain/networking/peer_protocol.nim b/beacon_chain/networking/peer_protocol.nim index bdda7fcc98..3c371e15c5 100644 --- a/beacon_chain/networking/peer_protocol.nim +++ b/beacon_chain/networking/peer_protocol.nim @@ -175,11 +175,11 @@ p2pProtocol PeerSync(version = 1, {.libp2pProtocol("metadata", 1).} = raise newException(InvalidInputsError, "GetMetaData v1 unsupported") - proc getMetadata_v2(peer: Peer): altair.MetaData + proc getMetadata_v2(peer: Peer): eip7594.MetaData {.libp2pProtocol("metadata", 2).} = peer.network.metadata - proc getMetadata_v3(peer: ): eip7594.MetaData + proc getMetadata_v3(peer: Peer): eip7594.MetaData {.libp2pProtocol("metadata", 3).} = peer.network.metadata From d43bd41e37d22d784655e06f69e08fd0050396ce Mon Sep 17 00:00:00 2001 From: Agnish Ghosh Date: Sun, 11 Aug 2024 20:32:09 +0530 Subject: [PATCH 3/5] fix spec version --- beacon_chain/spec/datatypes/eip7594.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon_chain/spec/datatypes/eip7594.nim b/beacon_chain/spec/datatypes/eip7594.nim index 5758d7af84..b73d9cf79b 100644 --- a/beacon_chain/spec/datatypes/eip7594.nim +++ b/beacon_chain/spec/datatypes/eip7594.nim @@ -79,7 +79,7 @@ type CscBits* = BitArray[DATA_COLUMN_SIDECAR_SUBNET_COUNT] - # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/altair/p2p-interface.md#metadata + # https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.4/specs/_features/eip7594/p2p-interface.md#metadata MetaData* = object seq_number*: uint64 attnets*: AttnetBits From b2822d69ca287d7a18ac6b8e4ff614fc6cae8ecc Mon Sep 17 00:00:00 2001 From: Agnish Ghosh Date: Mon, 12 Aug 2024 13:39:01 +0530 Subject: [PATCH 4/5] fix: metadata_v2 to return altair.MetaData --- beacon_chain/networking/peer_protocol.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/beacon_chain/networking/peer_protocol.nim b/beacon_chain/networking/peer_protocol.nim index 3c371e15c5..22d5c77f30 100644 --- a/beacon_chain/networking/peer_protocol.nim +++ b/beacon_chain/networking/peer_protocol.nim @@ -175,9 +175,13 @@ p2pProtocol PeerSync(version = 1, {.libp2pProtocol("metadata", 1).} = raise newException(InvalidInputsError, "GetMetaData v1 unsupported") - proc getMetadata_v2(peer: Peer): eip7594.MetaData + proc getMetadata_v2(peer: Peer): altair.MetaData {.libp2pProtocol("metadata", 2).} = - peer.network.metadata + let altair_metadata = altair.MetaData( + seq_number: peer.network.metadata.seq_number, + attnets: peer.network.metadata.attnets, + syncnets: peer.network.metadata.syncnets) + return altair_metadata proc getMetadata_v3(peer: Peer): eip7594.MetaData {.libp2pProtocol("metadata", 3).} = From bfa7d5d5eb86adf73d148adc53360f98948b3170 Mon Sep 17 00:00:00 2001 From: Agnish Ghosh Date: Mon, 12 Aug 2024 18:56:13 +0530 Subject: [PATCH 5/5] update metata function backward compatible now --- beacon_chain/networking/eth2_network.nim | 24 +++++++++++++++++++++-- beacon_chain/networking/peer_protocol.nim | 2 +- beacon_chain/spec/datatypes/eip7594.nim | 2 +- beacon_chain/spec/presets.nim | 2 ++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/beacon_chain/networking/eth2_network.nim b/beacon_chain/networking/eth2_network.nim index e14df26631..f017bf52b2 100644 --- a/beacon_chain/networking/eth2_network.nim +++ b/beacon_chain/networking/eth2_network.nim @@ -2077,12 +2077,32 @@ proc p2pProtocolBackendImpl*(p: P2PProtocol): Backend = import ./peer_protocol export peer_protocol +proc metadataV2ToV3(metadata: altair.MetaData): eip7594.MetaData = + eip7594.MetaData( + seq_number: metadata.seq_number, + attnets: metadata.attnets, + syncnets: metadata.syncnets) + +proc getMetadata_vx(node: Eth2Node, peer: Peer): + Future[NetRes[eip7594.MetaData]] + {.async: (raises: [CancelledError]).} = + let + res = + if node.cfg.EIP7594_FORK_EPOCH != FAR_FUTURE_EPOCH: + # Directly fetch eip7594 metadata if available + await getMetadata_v3(peer) + else: + let metadata_v2_result = await getMetadata_v2(peer) + metadata_v2_result.map(proc (altairData: altair.MetaData): eip7594.MetaData {.closure.} = + metadataV2ToV3(altairData) + ) + return res + proc updatePeerMetadata(node: Eth2Node, peerId: PeerId) {.async: (raises: [CancelledError]).} = trace "updating peer metadata", peerId - let peer = node.getPeer(peerId) - newMetadataRes = await peer.getMetadata_v3() + newMetadataRes = await node.getMetadata_vx(peer) newMetadata = newMetadataRes.valueOr: debug "Failed to retrieve metadata from peer!", peerId, error = newMetadataRes.error peer.failedMetadataRequests.inc() diff --git a/beacon_chain/networking/peer_protocol.nim b/beacon_chain/networking/peer_protocol.nim index 22d5c77f30..59efc87094 100644 --- a/beacon_chain/networking/peer_protocol.nim +++ b/beacon_chain/networking/peer_protocol.nim @@ -181,7 +181,7 @@ p2pProtocol PeerSync(version = 1, seq_number: peer.network.metadata.seq_number, attnets: peer.network.metadata.attnets, syncnets: peer.network.metadata.syncnets) - return altair_metadata + altair_metadata proc getMetadata_v3(peer: Peer): eip7594.MetaData {.libp2pProtocol("metadata", 3).} = diff --git a/beacon_chain/spec/datatypes/eip7594.nim b/beacon_chain/spec/datatypes/eip7594.nim index b73d9cf79b..317bb2c180 100644 --- a/beacon_chain/spec/datatypes/eip7594.nim +++ b/beacon_chain/spec/datatypes/eip7594.nim @@ -9,7 +9,7 @@ import std/[sequtils], - "."/[base, deneb, altair], + "."/[altair, base, deneb], kzg4844, stew/[byteutils] diff --git a/beacon_chain/spec/presets.nim b/beacon_chain/spec/presets.nim index 482f9501db..d8785505e0 100644 --- a/beacon_chain/spec/presets.nim +++ b/beacon_chain/spec/presets.nim @@ -60,6 +60,8 @@ type CAPELLA_FORK_EPOCH*: Epoch DENEB_FORK_VERSION*: Version DENEB_FORK_EPOCH*: Epoch + EIP7594_FORK_VERSION*: Version + EIP7594_FORK_EPOCH*: Epoch ELECTRA_FORK_VERSION*: Version ELECTRA_FORK_EPOCH*: Epoch