Skip to content

Commit

Permalink
getMetaData_v3 (#6739)
Browse files Browse the repository at this point in the history
* add getMetaDataV3

* simpler fix, to review

* drop returns
  • Loading branch information
agnxsh authored Nov 30, 2024
1 parent 18f492d commit c13b0c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
31 changes: 26 additions & 5 deletions beacon_chain/networking/eth2_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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*: fulu.MetaData
connectTimeout*: chronos.Duration
seenThreshold*: chronos.Duration
connQueue: AsyncQueue[PeerAddr]
Expand Down Expand Up @@ -108,7 +108,7 @@ type
lastReqTime*: Moment
connections*: int
enr*: Opt[enr.Record]
metadata*: Opt[altair.MetaData]
metadata*: Opt[fulu.MetaData]
failedMetadataRequests: int
lastMetadataTime*: Moment
direction*: PeerType
Expand Down Expand Up @@ -1803,7 +1803,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 = fulu.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
Expand Down Expand Up @@ -2082,12 +2082,33 @@ proc p2pProtocolBackendImpl*(p: P2PProtocol): Backend =
import ./peer_protocol
export peer_protocol

proc updateMetadataV2ToV3(metadataRes: NetRes[altair.MetaData]):
NetRes[fulu.MetaData] =
if metadataRes.isOk:
let metadata = metadataRes.get
ok(fulu.MetaData(seq_number: metadata.seq_number,
attnets: metadata.attnets,
syncnets: metadata.syncnets))
else:
err(metadataRes.error)

proc getMetadata_vx(node: Eth2Node, peer: Peer):
Future[NetRes[fulu.MetaData]]
{.async: (raises: [CancelledError]).} =
let
res =
if node.cfg.FULU_FORK_EPOCH != FAR_FUTURE_EPOCH:
# Directly fetch fulu metadata if available
await getMetadata_v3(peer)
else:
updateMetadataV2ToV3(await getMetadata_v2(peer))
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_v2()
newMetadataRes = await node.getMetadata_vx(peer)
newMetadata = newMetadataRes.valueOr:
debug "Failed to retrieve metadata from peer!", peerId, error = newMetadataRes.error
peer.failedMetadataRequests.inc()
Expand Down
8 changes: 8 additions & 0 deletions beacon_chain/networking/peer_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ p2pProtocol PeerSync(version = 1,

proc getMetadata_v2(peer: Peer): altair.MetaData
{.libp2pProtocol("metadata", 2).} =
let altair_metadata = altair.MetaData(
seq_number: peer.network.metadata.seq_number,
attnets: peer.network.metadata.attnets,
syncnets: peer.network.metadata.syncnets)
altair_metadata

proc getMetadata_v3(peer: Peer): fulu.MetaData
{. libp2pProtocol("metadata", 3).} =
peer.network.metadata

proc goodbye(peer: Peer, reason: uint64) {.
Expand Down

0 comments on commit c13b0c9

Please sign in to comment.