Skip to content

Commit

Permalink
implement Electra beacon API getBlindedBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec committed Apr 8, 2024
1 parent 361be08 commit 9655caa
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 24 deletions.
6 changes: 1 addition & 5 deletions beacon_chain/rpc/rest_beacon_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1003,11 +1003,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
RestApiResponse.jsonError(Http500, InvalidAcceptError)

withBlck(bdata.asSigned()):
when consensusFork == ConsensusFork.Electra:
debugRaiseAssert "electra, beacon API missing"
let x = 5
RestApiResponse.jsonError(Http500, InvalidAcceptError)
elif consensusFork <= ConsensusFork.Altair:
when consensusFork <= ConsensusFork.Altair:
respondSszOrJson(forkyBlck, consensusFork)
else:
respondSszOrJson(toSignedBlindedBeaconBlock(forkyBlck), consensusFork)
Expand Down
3 changes: 3 additions & 0 deletions beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ RestJson.useDefaultSerializationFor(
electra.ExecutionPayload,
electra.ExecutionPayloadHeader,
electra.SignedBeaconBlock,
electra_mev.BlindedBeaconBlock,
electra_mev.BlindedBeaconBlockBody,
electra_mev.ExecutionPayloadAndBlobsBundle,
electra_mev.SignedBlindedBeaconBlock,
phase0.BeaconBlock,
phase0.BeaconBlockBody,
phase0.BeaconState,
Expand Down
16 changes: 13 additions & 3 deletions beacon_chain/spec/eth2_apis/rest_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,20 @@ func init*(t: typedesc[RestPublishedSignedBlockContents],
)

func init*(t: typedesc[RestPublishedSignedBlockContents],
contents: electra.BeaconBlock, root: Eth2Digest,
contents: electra.BlockContents, root: Eth2Digest,
signature: ValidatorSig): RestPublishedSignedBlockContents =
debugRaiseAssert "init*(t: typedesc[RestPublishedSignedBlockContents],"
default(RestPublishedSignedBlockContents)
RestPublishedSignedBlockContents(
kind: ConsensusFork.Electra,
electraData: ElectraSignedBlockContents(
signed_block: electra.SignedBeaconBlock(
message: contents.`block`,
root: root,
signature: signature
),
kzg_proofs: contents.kzg_proofs,
blobs: contents.blobs
)
)

func init*(t: typedesc[StateIdent], v: StateIdentType): StateIdent =
StateIdent(kind: StateQueryKind.Named, value: v)
Expand Down
45 changes: 30 additions & 15 deletions beacon_chain/spec/forks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ type
of ConsensusFork.Deneb:
denebData*: deneb_mev.MaybeBlindedBeaconBlock
of ConsensusFork.Electra:
electraData*: electra.BeaconBlock
electraData*: electra_mev.MaybeBlindedBeaconBlock
consensusValue*: Opt[UInt256]
executionValue*: Opt[UInt256]

Expand All @@ -188,7 +188,7 @@ type
of ConsensusFork.Bellatrix: bellatrixData*: bellatrix_mev.BlindedBeaconBlock
of ConsensusFork.Capella: capellaData*: capella_mev.BlindedBeaconBlock
of ConsensusFork.Deneb: denebData*: deneb_mev.BlindedBeaconBlock
of ConsensusFork.Electra: electraData*: electra.BeaconBlock
of ConsensusFork.Electra: electraData*: electra_mev.BlindedBeaconBlock

ForkySignedBeaconBlock* =
phase0.SignedBeaconBlock |
Expand All @@ -212,7 +212,8 @@ type
altair.SignedBeaconBlock |
bellatrix_mev.SignedBlindedBeaconBlock |
capella_mev.SignedBlindedBeaconBlock |
deneb_mev.SignedBlindedBeaconBlock
deneb_mev.SignedBlindedBeaconBlock |
electra_mev.SignedBlindedBeaconBlock

ForkedSignedBlindedBeaconBlock* = object
case kind*: ConsensusFork
Expand All @@ -221,7 +222,7 @@ type
of ConsensusFork.Bellatrix: bellatrixData*: bellatrix_mev.SignedBlindedBeaconBlock
of ConsensusFork.Capella: capellaData*: capella_mev.SignedBlindedBeaconBlock
of ConsensusFork.Deneb: denebData*: deneb_mev.SignedBlindedBeaconBlock
of ConsensusFork.Electra: electraData*: electra.SignedBeaconBlock
of ConsensusFork.Electra: electraData*: electra_mev.SignedBlindedBeaconBlock

ForkySigVerifiedSignedBeaconBlock* =
phase0.SigVerifiedSignedBeaconBlock |
Expand Down Expand Up @@ -496,7 +497,9 @@ template BlindedBeaconBlock*(kind: static ConsensusFork): auto =
static: raiseAssert "Unreachable"

template MaybeBlindedBeaconBlock*(kind: static ConsensusFork): auto =
when kind == ConsensusFork.Deneb:
when kind == ConsensusFork.Electra:
typedesc[electra_mev.MaybeBlindedBeaconBlock]
elif kind == ConsensusFork.Deneb:
typedesc[deneb_mev.MaybeBlindedBeaconBlock]
elif kind == ConsensusFork.Capella or kind == ConsensusFork.Bellatrix:
static: raiseAssert "Unsupported"
Expand Down Expand Up @@ -716,9 +719,8 @@ func init*(T: type ForkedSignedBlindedBeaconBlock,
signature: signature))
of ConsensusFork.Electra:
T(kind: ConsensusFork.Electra,
electraData: electra.SignedBeaconBlock(message: forked.electraData,
root: blockRoot,
signature: signature))
electraData: electra_mev.SignedBlindedBeaconBlock(message: forked.electraData,
signature: signature))

template init*(T: type ForkedSignedBlindedBeaconBlock,
blck: capella_mev.BlindedBeaconBlock, blockRoot: Eth2Digest,
Expand All @@ -734,6 +736,13 @@ template init*(T: type ForkedSignedBlindedBeaconBlock,
denebData: deneb_mev.SignedBlindedBeaconBlock(
message: blck, signature: signature))

template init*(T: type ForkedSignedBlindedBeaconBlock,
blck: electra_mev.BlindedBeaconBlock, blockRoot: Eth2Digest,
signature: ValidatorSig): T =
T(kind: ConsensusFork.Electra,
electraData: electra_mev.SignedBlindedBeaconBlock(
message: blck, signature: signature))

template init*(T: type ForkedMsgTrustedSignedBeaconBlock, blck: phase0.MsgTrustedSignedBeaconBlock): T =
T(kind: ConsensusFork.Phase0, phase0Data: blck)
template init*(T: type ForkedMsgTrustedSignedBeaconBlock, blck: altair.MsgTrustedSignedBeaconBlock): T =
Expand Down Expand Up @@ -1104,11 +1113,17 @@ template withForkyMaybeBlindedBlck*(
body: untyped): untyped =
case b.kind
of ConsensusFork.Electra:
const
consensusFork {.inject, used.} = ConsensusFork.Electra
isBlinded {.inject, used.} = false
template forkyMaybeBlindedBlck: untyped {.inject, used.} = b.electraData
body
const consensusFork {.inject, used.} = ConsensusFork.Electra
template d: untyped = b.electraData
case d.isBlinded:
of true:
const isBlinded {.inject, used.} = true
template forkyMaybeBlindedBlck: untyped {.inject, used.} = d.blindedData
body
of false:
const isBlinded {.inject, used.} = false
template forkyMaybeBlindedBlck: untyped {.inject, used.} = d.data
body
of ConsensusFork.Deneb:
const consensusFork {.inject, used.} = ConsensusFork.Deneb
template d: untyped = b.denebData
Expand Down Expand Up @@ -1185,8 +1200,8 @@ template withStateAndBlck*(
body

func toBeaconBlockHeader*(
blck: SomeForkyBeaconBlock | deneb_mev.BlindedBeaconBlock):
BeaconBlockHeader =
blck: SomeForkyBeaconBlock | deneb_mev.BlindedBeaconBlock |
electra_mev.BlindedBeaconBlock): BeaconBlockHeader =
## Reduce a given `BeaconBlock` to its `BeaconBlockHeader`.
BeaconBlockHeader(
slot: blck.slot,
Expand Down
3 changes: 2 additions & 1 deletion beacon_chain/validators/validator_pool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ proc getBlockSignature*(v: AttachedValidator, fork: Fork,
capella.BeaconBlockBody |
deneb.BeaconBlockBody |
deneb_mev.BlindedBeaconBlockBody |
electra.BeaconBlockBody
electra.BeaconBlockBody |
electra_mev.BlindedBeaconBlockBody

template blockPropertiesProofs(blockBody: SomeBlockBody,
forkIndexField: untyped): seq[Web3SignerMerkleProof] =
Expand Down

0 comments on commit 9655caa

Please sign in to comment.