Fix blob syncing for Electra (#6438)

* Fix blob syncing for Electra

`BlobSidecar` requests on libp2p have a context prefix based on:

> The `<context-bytes>` field is calculated as context =
>     `compute_fork_digest(fork_version, genesis_validators_root)`

We currently only process blobs if that indicates Deneb, meaning that
on Electra we incorrectly report `InvalidContextBytes` and refuse to
process the blob response data.

Fix this, and also ensure that the code no longer needs maintenance
with every fork unrelated to blobs.

* fix
This commit is contained in:
Etan Kissling 2024-07-23 05:10:41 +02:00 committed by GitHub
parent 5762ebc249
commit ae0a1488b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 46 deletions

View File

@ -44,50 +44,19 @@ proc readChunkPayload*(
var contextBytes: ForkDigest var contextBytes: ForkDigest
try: try:
await conn.readExactly(addr contextBytes, sizeof contextBytes) await conn.readExactly(addr contextBytes, sizeof contextBytes)
except CancelledError as exc:
raise exc
except CatchableError: except CatchableError:
return neterr UnexpectedEOF return neterr UnexpectedEOF
let contextFork =
peer.network.forkDigests[].consensusForkForDigest(contextBytes).valueOr:
return neterr InvalidContextBytes
static: doAssert ConsensusFork.high == ConsensusFork.Electra withConsensusFork(contextFork):
if contextBytes == peer.network.forkDigests.phase0: let res = await readChunkPayload(
let res = await readChunkPayload(conn, peer, phase0.SignedBeaconBlock) conn, peer, consensusFork.SignedBeaconBlock)
if res.isOk: if res.isOk:
return ok newClone(ForkedSignedBeaconBlock.init(res.get)) return ok newClone(ForkedSignedBeaconBlock.init(res.get))
else: else:
return err(res.error) return err(res.error)
elif contextBytes == peer.network.forkDigests.altair:
let res = await readChunkPayload(conn, peer, altair.SignedBeaconBlock)
if res.isOk:
return ok newClone(ForkedSignedBeaconBlock.init(res.get))
else:
return err(res.error)
elif contextBytes == peer.network.forkDigests.bellatrix:
let res = await readChunkPayload(conn, peer, bellatrix.SignedBeaconBlock)
if res.isOk:
return ok newClone(ForkedSignedBeaconBlock.init(res.get))
else:
return err(res.error)
elif contextBytes == peer.network.forkDigests.capella:
let res = await readChunkPayload(conn, peer, capella.SignedBeaconBlock)
if res.isOk:
return ok newClone(ForkedSignedBeaconBlock.init(res.get))
else:
return err(res.error)
elif contextBytes == peer.network.forkDigests.deneb:
let res = await readChunkPayload(conn, peer, deneb.SignedBeaconBlock)
if res.isOk:
return ok newClone(ForkedSignedBeaconBlock.init(res.get))
else:
return err(res.error)
elif contextBytes == peer.network.forkDigests.electra:
let res = await readChunkPayload(conn, peer, electra.SignedBeaconBlock)
if res.isOk:
return ok newClone(ForkedSignedBeaconBlock.init(res.get))
else:
return err(res.error)
else:
return neterr InvalidContextBytes
proc readChunkPayload*( proc readChunkPayload*(
conn: Connection, peer: Peer, MsgType: type (ref BlobSidecar)): conn: Connection, peer: Peer, MsgType: type (ref BlobSidecar)):
@ -95,19 +64,21 @@ proc readChunkPayload*(
var contextBytes: ForkDigest var contextBytes: ForkDigest
try: try:
await conn.readExactly(addr contextBytes, sizeof contextBytes) await conn.readExactly(addr contextBytes, sizeof contextBytes)
except CancelledError as exc:
raise exc
except CatchableError: except CatchableError:
return neterr UnexpectedEOF return neterr UnexpectedEOF
let contextFork =
peer.network.forkDigests[].consensusForkForDigest(contextBytes).valueOr:
return neterr InvalidContextBytes
if contextBytes == peer.network.forkDigests.deneb: withConsensusFork(contextFork):
let res = await readChunkPayload(conn, peer, BlobSidecar) when consensusFork >= ConsensusFork.Deneb:
if res.isOk: let res = await readChunkPayload(conn, peer, BlobSidecar)
return ok newClone(res.get) if res.isOk:
return ok newClone(res.get)
else:
return err(res.error)
else: else:
return err(res.error) return neterr InvalidContextBytes
else:
return neterr InvalidContextBytes
{.pop.} # TODO fix p2p macro for raises {.pop.} # TODO fix p2p macro for raises