Skip to content

Commit

Permalink
initial VC Electra publishBlock support (#6189)
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec committed Apr 9, 2024
1 parent 749a456 commit 8a30cff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
20 changes: 6 additions & 14 deletions beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ type
capella.SignedBeaconBlock |
phase0.SignedBeaconBlock |
DenebSignedBlockContents |
ElectraSignedBlockContents |
ForkedMaybeBlindedBeaconBlock

EncodeArrays* =
Expand Down Expand Up @@ -403,8 +404,8 @@ type

RestBlockTypes* = phase0.BeaconBlock | altair.BeaconBlock |
bellatrix.BeaconBlock | capella.BeaconBlock |
deneb.BlockContents | capella_mev.BlindedBeaconBlock |
deneb_mev.BlindedBeaconBlock
deneb.BlockContents | deneb_mev.BlindedBeaconBlock |
electra.BlockContents | electra_mev.BlindedBeaconBlock

func readStrictHexChar(c: char, radix: static[uint8]): Result[int8, cstring] =
## Converts an hex char to an int
Expand Down Expand Up @@ -3970,14 +3971,8 @@ proc decodeBytes*[T: DecodeConsensysTypes](
forked = ForkedBlindedBeaconBlock(
kind: ConsensusFork.Deneb, denebData: blck)
ok(ProduceBlindedBlockResponse(forked))
of ConsensusFork.Capella:
let
blck = ? readSszResBytes(capella_mev.BlindedBeaconBlock, value)
forked = ForkedBlindedBeaconBlock(
kind: ConsensusFork.Capella, capellaData: blck)
ok(ProduceBlindedBlockResponse(forked))
of ConsensusFork.Bellatrix, ConsensusFork.Altair, ConsensusFork.Phase0:
err("Unable to decode blinded block for Bellatrix, Altair, and Phase0 forks")
of ConsensusFork.Phase0 .. ConsensusFork.Capella:
err("Unable to decode blinded block for pre-Deneb forks")
else:
err("Unsupported Content-Type")

Expand Down Expand Up @@ -4036,10 +4031,7 @@ proc decodeBytes*[T: ProduceBlockResponseV3](
except ValueError:
return err("Incorrect `Eth-Consensus-Block-Value` header value")
withConsensusFork(fork):
when consensusFork >= ConsensusFork.Electra:
debugRaiseAssert "eth2 rest serialization"
return err("electra missing")
elif consensusFork >= ConsensusFork.Deneb:
when consensusFork >= ConsensusFork.Deneb:
if blinded:
let contents =
? readSszResBytes(consensusFork.BlindedBlockContents, value)
Expand Down
5 changes: 5 additions & 0 deletions beacon_chain/spec/eth2_apis/rest_beacon_calls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ proc publishBlock*(body: DenebSignedBlockContents): RestPlainResponse {.
meth: MethodPost.}
## https://ethereum.github.io/beacon-APIs/#/Beacon/publishBlock

proc publishBlock*(body: ElectraSignedBlockContents): RestPlainResponse {.
rest, endpoint: "/eth/v1/beacon/blocks",
meth: MethodPost.}
## https://ethereum.github.io/beacon-APIs/#/Beacon/publishBlock

proc publishSszBlock*(
client: RestClientRef,
blck: ForkySignedBeaconBlock
Expand Down
26 changes: 25 additions & 1 deletion beacon_chain/spec/forks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,9 @@ template BlockContents*(

template BlindedBlockContents*(
kind: static ConsensusFork): auto =
when kind == ConsensusFork.Deneb:
when kind == ConsensusFork.Electra:
typedesc[electra_mev.BlindedBeaconBlock]
elif kind == ConsensusFork.Deneb:
typedesc[deneb_mev.BlindedBeaconBlock]
else:
{.error: "BlindedBlockContents does not support " & $kind.}
Expand Down Expand Up @@ -1496,3 +1498,25 @@ template init*(T: type ForkedMaybeBlindedBeaconBlock,
blindedData: blck),
consensusValue: cvalue,
executionValue: evalue)

template init*(T: type ForkedMaybeBlindedBeaconBlock,
blck: electra.BlockContents,
evalue: Opt[UInt256], cvalue: Opt[UInt256]): T =
ForkedMaybeBlindedBeaconBlock(
kind: ConsensusFork.Electra,
electraData: electra_mev.MaybeBlindedBeaconBlock(
isBlinded: false,
data: blck),
consensusValue: cvalue,
executionValue: evalue)

template init*(T: type ForkedMaybeBlindedBeaconBlock,
blck: electra_mev.BlindedBeaconBlock,
evalue: Opt[UInt256], cvalue: Opt[UInt256]): T =
ForkedMaybeBlindedBeaconBlock(
kind: ConsensusFork.Electra,
electraData: electra_mev.MaybeBlindedBeaconBlock(
isBlinded: true,
blindedData: blck),
consensusValue: cvalue,
executionValue: evalue)
3 changes: 1 addition & 2 deletions beacon_chain/validator_client/api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2260,8 +2260,7 @@ proc publishBlock*(
of ConsensusFork.Deneb:
publishBlock(it, data.denebData)
of ConsensusFork.Electra:
debugRaiseAssert "electra missing"
publishBlock(it, data.denebData)
publishBlock(it, data.electraData)
do:
if apiResponse.isErr():
handleCommunicationError()
Expand Down

0 comments on commit 8a30cff

Please sign in to comment.