update /eth/v1/debug/fork_choice format to latest (#4802)

Syncs the `/eth/v1/debug/fork_choice` REST endpoint with latest specs.

- Validity is now reported as tri-state `enum` instead of two `bool`s
- Response includes store's justified and finalized checkpoints
- Additional `ExtraData` field on outer layer (empty for now)

https://github.com/ethereum/beacon-APIs/pull/232
This commit is contained in:
Etan Kissling 2023-04-10 17:35:14 +02:00 committed by GitHub
parent 450f06566b
commit df7ecd4fe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 17 deletions

View File

@ -518,7 +518,7 @@ type
debugForkChoice* {. debugForkChoice* {.
hidden hidden
desc: "Enable debug API for fork choice (https://github.com/ethereum/beacon-APIs/pull/232)" desc: "Enable debug API for fork choice (https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Debug/getDebugForkChoice)"
defaultValue: false defaultValue: false
name: "debug-fork-choice" .}: bool name: "debug-fork-choice" .}: bool

View File

@ -81,33 +81,51 @@ proc installDebugApiHandlers*(router: var RestRouter, node: BeaconNode) =
) )
) )
# https://github.com/ethereum/beacon-APIs/pull/232 # https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Debug/getDebugForkChoice
if node.config.debugForkChoice or experimental in node.dag.updateFlags: if node.config.debugForkChoice or experimental in node.dag.updateFlags:
router.api(MethodGet, router.api(MethodGet,
"/eth/v1/debug/fork_choice") do () -> RestApiResponse: "/eth/v1/debug/fork_choice") do () -> RestApiResponse:
type type
ForkChoiceResponseExtraData = object NodeValidity {.pure.} = enum
valid = "VALID",
invalid = "INVALID",
optimistic = "OPTIMISTIC"
NodeExtraData = object
justified_root: Eth2Digest justified_root: Eth2Digest
finalized_root: Eth2Digest finalized_root: Eth2Digest
u_justified_checkpoint: Option[Checkpoint] u_justified_checkpoint: Option[Checkpoint]
u_finalized_checkpoint: Option[Checkpoint] u_finalized_checkpoint: Option[Checkpoint]
best_child: Eth2Digest best_child: Eth2Digest
best_descendant: Eth2Digest best_descendant: Eth2Digest
invalid: bool
ForkChoiceResponse = object Node = object
slot: Slot slot: Slot
block_root: Eth2Digest block_root: Eth2Digest
parent_root: Eth2Digest parent_root: Eth2Digest
justified_epoch: Epoch justified_epoch: Epoch
finalized_epoch: Epoch finalized_epoch: Epoch
weight: uint64 weight: uint64
execution_optimistic: bool validity: NodeValidity
execution_payload_root: Eth2Digest execution_block_hash: Eth2Digest
extra_data: Option[ForkChoiceResponseExtraData] extra_data: Option[NodeExtraData]
var responses: seq[ForkChoiceResponse] ExtraData = object
for item in node.attestationPool[].forkChoice.backend.proto_array: discard
GetForkChoiceResponse = object
justified_checkpoint: Checkpoint
finalized_checkpoint: Checkpoint
fork_choice_nodes: seq[Node]
extra_data: ExtraData
template forkChoice: auto = node.attestationPool[].forkChoice
var response = GetForkChoiceResponse(
justified_checkpoint: forkChoice.checkpoints.justified.checkpoint,
finalized_checkpoint: forkChoice.checkpoints.finalized)
for item in forkChoice.backend.proto_array:
let let
unrealized = item.unrealized.get(item.checkpoints) unrealized = item.unrealized.get(item.checkpoints)
u_justified_checkpoint = u_justified_checkpoint =
@ -121,21 +139,27 @@ proc installDebugApiHandlers*(router: var RestRouter, node: BeaconNode) =
else: else:
none(Checkpoint) none(Checkpoint)
responses.add ForkChoiceResponse( response.fork_choice_nodes.add Node(
slot: item.bid.slot, slot: item.bid.slot,
block_root: item.bid.root, block_root: item.bid.root,
parent_root: item.parent, parent_root: item.parent,
justified_epoch: item.checkpoints.justified.epoch, justified_epoch: item.checkpoints.justified.epoch,
finalized_epoch: item.checkpoints.finalized.epoch, finalized_epoch: item.checkpoints.finalized.epoch,
weight: cast[uint64](item.weight), weight: cast[uint64](item.weight),
execution_optimistic: node.dag.is_optimistic(item.bid.root), validity:
execution_payload_root: node.dag.loadExecutionBlockRoot(item.bid), if item.invalid:
extra_data: some ForkChoiceResponseExtraData( NodeValidity.invalid
elif node.dag.is_optimistic(item.bid.root):
NodeValidity.optimistic
else:
NodeValidity.valid,
execution_block_hash: node.dag.loadExecutionBlockRoot(item.bid),
extra_data: some NodeExtraData(
justified_root: item.checkpoints.justified.root, justified_root: item.checkpoints.justified.root,
finalized_root: item.checkpoints.finalized.root, finalized_root: item.checkpoints.finalized.root,
u_justified_checkpoint: u_justified_checkpoint, u_justified_checkpoint: u_justified_checkpoint,
u_finalized_checkpoint: u_finalized_checkpoint, u_finalized_checkpoint: u_finalized_checkpoint,
best_child: item.bestChild, best_child: item.bestChild,
bestDescendant: item.bestDescendant, bestDescendant: item.bestDescendant))
invalid: item.invalid))
return RestApiResponse.jsonResponse(responses) return RestApiResponse.jsonResponse(response)