Skip to content

Commit

Permalink
remove block fork guessing/inference from REST JSON decoding (#6552)
Browse files Browse the repository at this point in the history
* remove block fork guessing/inference from REST JSON decoding

* use template to avoid repetitive per-fork code

* consolidate RestPublishedSignedBeaconBlock and RestPublishedSignedBlockContents parsing fork handling
  • Loading branch information
tersec authored Sep 25, 2024
1 parent 31b5c3e commit f2d6166
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 625 deletions.
5 changes: 2 additions & 3 deletions AllTests-mainnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -821,10 +821,9 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
+ DenebSignedBlockContents decoding OK
+ KzgCommitment OK
+ KzgProof OK
+ RestPublishedSignedBlockContents decoding OK
+ Validator pubkey hack OK
```
OK: 6/6 Fail: 0/6 Skip: 0/6
OK: 5/5 Fail: 0/5 Skip: 0/5
## Remove keystore testing suite
```diff
+ Many remotes OK
Expand Down Expand Up @@ -1126,4 +1125,4 @@ OK: 2/2 Fail: 0/2 Skip: 0/2
OK: 9/9 Fail: 0/9 Skip: 0/9

---TOTAL---
OK: 763/768 Fail: 0/768 Skip: 5/768
OK: 762/767 Fail: 0/767 Skip: 5/767
33 changes: 28 additions & 5 deletions beacon_chain/rpc/rest_beacon_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -901,10 +901,23 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
let
body = contentBody.get()
version = request.headers.getString("eth-consensus-version")
restBlock = decodeBody(
RestPublishedSignedBlockContents, body, version).valueOr:
return RestApiResponse.jsonError(error)
currentEpochFork =
node.dag.cfg.consensusForkAtEpoch(node.currentSlot().epoch())
rawVersion = request.headers.getString("eth-consensus-version")

# The V1 endpoint doesn't require the version to be specified but the
# only fork which works is the current gossip fork. Either it can use
# and broadcast a block in that fork or that broadcast will not prove
# useful anyway, so allow it to fail at the decoding stage.
version =
if rawVersion == "":
currentEpochFork.toString
else:
rawVersion

let restBlock = decodeBody(
RestPublishedSignedBlockContents, body, version).valueOr:
return RestApiResponse.jsonError(error)

withForkyBlck(restBlock):
if restBlock.kind != node.dag.cfg.consensusForkAtEpoch(
Expand Down Expand Up @@ -1047,9 +1060,19 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
let
currentEpochFork =
node.dag.cfg.consensusForkAtEpoch(node.currentSlot().epoch())
version = request.headers.getString("eth-consensus-version")
rawVersion = request.headers.getString("eth-consensus-version")
body = contentBody.get()

# The V1 endpoint doesn't require the version to be specified but the
# only fork which works is the current gossip fork. Either it can use
# and broadcast a block in that fork or that broadcast will not prove
# useful anyway, so allow it to fail at the decoding stage.
version =
if rawVersion == "":
currentEpochFork.toString
else:
rawVersion

if (body.contentType == OctetStreamMediaType) and
(currentEpochFork.toString != version):
return RestApiResponse.jsonError(Http400, BlockIncorrectFork)
Expand Down
Loading

0 comments on commit f2d6166

Please sign in to comment.