2021-10-06 17:05:06 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-10-06 17:05:06 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2024-02-18 01:16:49 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
import std/sequtils
|
2024-05-15 06:52:57 +00:00
|
|
|
import chronicles, metrics
|
2022-06-21 19:01:45 +00:00
|
|
|
import ".."/beacon_node,
|
2021-08-23 10:41:48 +00:00
|
|
|
".."/spec/forks,
|
2022-01-05 14:49:10 +00:00
|
|
|
"."/[rest_utils, state_ttl_cache]
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2022-08-29 22:02:29 +00:00
|
|
|
from ../fork_choice/proto_array import ProtoArrayItem, items
|
|
|
|
|
2021-10-27 12:01:11 +00:00
|
|
|
export rest_utils
|
|
|
|
|
2021-03-17 18:46:45 +00:00
|
|
|
logScope: topics = "rest_debug"
|
|
|
|
|
|
|
|
proc installDebugApiHandlers*(router: var RestRouter, node: BeaconNode) =
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Debug/getState
|
2024-05-15 06:52:57 +00:00
|
|
|
router.api2(MethodGet, "/eth/v1/debug/beacon/states/{state_id}") do (
|
2021-03-17 18:46:45 +00:00
|
|
|
state_id: StateIdent) -> RestApiResponse:
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonError(
|
2022-11-02 10:56:55 +00:00
|
|
|
Http410, DeprecatedRemovalBeaconBlocksDebugStateV1)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Debug/getStateV2
|
2024-05-15 06:52:57 +00:00
|
|
|
router.metricsApi2(
|
|
|
|
MethodGet, "/eth/v2/debug/beacon/states/{state_id}",
|
|
|
|
{RestServerMetricsType.Status, Response}) do (
|
2021-08-23 10:41:48 +00:00
|
|
|
state_id: StateIdent) -> RestApiResponse:
|
|
|
|
let bslot =
|
|
|
|
block:
|
|
|
|
if state_id.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidStateIdValueError,
|
|
|
|
$state_id.error())
|
Prune `BlockRef` on finalization (#3513)
Up til now, the block dag has been using `BlockRef`, a structure adapted
for a full DAG, to represent all of chain history. This is a correct and
simple design, but does not exploit the linearity of the chain once
parts of it finalize.
By pruning the in-memory `BlockRef` structure at finalization, we save,
at the time of writing, a cool ~250mb (or 25%:ish) chunk of memory
landing us at a steady state of ~750mb normal memory usage for a
validating node.
Above all though, we prevent memory usage from growing proportionally
with the length of the chain, something that would not be sustainable
over time - instead, the steady state memory usage is roughly
determined by the validator set size which grows much more slowly. With
these changes, the core should remain sustainable memory-wise post-merge
all the way to withdrawals (when the validator set is expected to grow).
In-memory indices are still used for the "hot" unfinalized portion of
the chain - this ensure that consensus performance remains unchanged.
What changes is that for historical access, we use a db-based linear
slot index which is cache-and-disk-friendly, keeping the cost for
accessing historical data at a similar level as before, achieving the
savings at no percievable cost to functionality or performance.
A nice collateral benefit is the almost-instant startup since we no
longer load any large indicies at dag init.
The cost of this functionality instead can be found in the complexity of
having to deal with two ways of traversing the chain - by `BlockRef` and
by slot.
* use `BlockId` instead of `BlockRef` where finalized / historical data
may be required
* simplify clearance pre-advancement
* remove dag.finalizedBlocks (~50:ish mb)
* remove `getBlockAtSlot` - use `getBlockIdAtSlot` instead
* `parent` and `atSlot` for `BlockId` now require a `ChainDAGRef`
instance, unlike `BlockRef` traversal
* prune `BlockRef` parents on finality (~200:ish mb)
* speed up ChainDAG init by not loading finalized history index
* mess up light client server error handling - this need revisiting :)
2022-03-17 17:42:56 +00:00
|
|
|
let bres = node.getBlockSlotId(state_id.get())
|
2021-08-23 10:41:48 +00:00
|
|
|
if bres.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http404, StateNotFoundError,
|
|
|
|
$bres.error())
|
|
|
|
bres.get()
|
2021-09-16 13:32:32 +00:00
|
|
|
let contentType =
|
|
|
|
block:
|
2022-01-21 16:52:34 +00:00
|
|
|
let res = preferredContentType(jsonMediaType,
|
|
|
|
sszMediaType)
|
2021-09-16 13:32:32 +00:00
|
|
|
if res.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http406, ContentNotAcceptableError)
|
|
|
|
res.get()
|
2024-01-20 16:06:28 +00:00
|
|
|
|
Prune `BlockRef` on finalization (#3513)
Up til now, the block dag has been using `BlockRef`, a structure adapted
for a full DAG, to represent all of chain history. This is a correct and
simple design, but does not exploit the linearity of the chain once
parts of it finalize.
By pruning the in-memory `BlockRef` structure at finalization, we save,
at the time of writing, a cool ~250mb (or 25%:ish) chunk of memory
landing us at a steady state of ~750mb normal memory usage for a
validating node.
Above all though, we prevent memory usage from growing proportionally
with the length of the chain, something that would not be sustainable
over time - instead, the steady state memory usage is roughly
determined by the validator set size which grows much more slowly. With
these changes, the core should remain sustainable memory-wise post-merge
all the way to withdrawals (when the validator set is expected to grow).
In-memory indices are still used for the "hot" unfinalized portion of
the chain - this ensure that consensus performance remains unchanged.
What changes is that for historical access, we use a db-based linear
slot index which is cache-and-disk-friendly, keeping the cost for
accessing historical data at a similar level as before, achieving the
savings at no percievable cost to functionality or performance.
A nice collateral benefit is the almost-instant startup since we no
longer load any large indicies at dag init.
The cost of this functionality instead can be found in the complexity of
having to deal with two ways of traversing the chain - by `BlockRef` and
by slot.
* use `BlockId` instead of `BlockRef` where finalized / historical data
may be required
* simplify clearance pre-advancement
* remove dag.finalizedBlocks (~50:ish mb)
* remove `getBlockAtSlot` - use `getBlockIdAtSlot` instead
* `parent` and `atSlot` for `BlockId` now require a `ChainDAGRef`
instance, unlike `BlockRef` traversal
* prune `BlockRef` parents on finality (~200:ish mb)
* speed up ChainDAG init by not loading finalized history index
* mess up light client server error handling - this need revisiting :)
2022-03-17 17:42:56 +00:00
|
|
|
node.withStateForBlockSlotId(bslot):
|
2021-09-16 13:32:32 +00:00
|
|
|
return
|
2022-01-21 16:52:34 +00:00
|
|
|
if contentType == jsonMediaType:
|
2022-06-20 05:53:39 +00:00
|
|
|
RestApiResponse.jsonResponseState(
|
2024-01-20 16:06:28 +00:00
|
|
|
state, node.getStateOptimistic(state))
|
2022-01-21 16:52:34 +00:00
|
|
|
elif contentType == sszMediaType:
|
2022-06-20 05:53:39 +00:00
|
|
|
let headers = [("eth-consensus-version", state.kind.toString())]
|
2022-03-16 07:20:40 +00:00
|
|
|
withState(state):
|
2024-04-04 03:17:31 +00:00
|
|
|
RestApiResponse.sszResponse(forkyState.data, headers)
|
2021-09-16 13:32:32 +00:00
|
|
|
else:
|
|
|
|
RestApiResponse.jsonError(Http500, InvalidAcceptError)
|
2024-01-20 16:06:28 +00:00
|
|
|
|
|
|
|
RestApiResponse.jsonError(Http404, StateNotFoundError)
|
2021-08-23 10:41:48 +00:00
|
|
|
|
2023-06-10 08:08:01 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Debug/getDebugChainHeads
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodGet,
|
|
|
|
"/eth/v1/debug/beacon/heads") do () -> RestApiResponse:
|
|
|
|
RestApiResponse.jsonError(
|
2023-06-15 09:58:38 +00:00
|
|
|
Http410, DeprecatedRemovalGetDebugChainHeadsV1)
|
2023-06-10 08:08:01 +00:00
|
|
|
|
2022-06-20 05:53:39 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Debug/getDebugChainHeadsV2
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodGet,
|
|
|
|
"/eth/v2/debug/beacon/heads") do () -> RestApiResponse:
|
|
|
|
RestApiResponse.jsonResponse(
|
2022-06-20 05:53:39 +00:00
|
|
|
node.dag.heads.mapIt(
|
2024-02-07 16:51:12 +00:00
|
|
|
RestChainHeadV2(
|
|
|
|
root: it.root,
|
|
|
|
slot: it.slot,
|
|
|
|
execution_optimistic: not it.executionValid
|
|
|
|
)
|
2022-06-20 05:53:39 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-04-10 15:35:14 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Debug/getDebugForkChoice
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodGet,
|
|
|
|
"/eth/v1/debug/fork_choice") do () -> RestApiResponse:
|
2023-04-18 19:26:36 +00:00
|
|
|
template forkChoice: auto = node.attestationPool[].forkChoice
|
2022-08-29 22:02:29 +00:00
|
|
|
|
2023-04-18 19:26:36 +00:00
|
|
|
var response = GetForkChoiceResponse(
|
|
|
|
justified_checkpoint: forkChoice.checkpoints.justified.checkpoint,
|
2023-09-12 07:52:51 +00:00
|
|
|
finalized_checkpoint: forkChoice.checkpoints.finalized,
|
2024-08-07 14:48:05 +00:00
|
|
|
extra_data: RestExtraData())
|
2023-04-10 15:35:14 +00:00
|
|
|
|
2023-04-18 19:26:36 +00:00
|
|
|
for item in forkChoice.backend.proto_array:
|
|
|
|
let
|
|
|
|
unrealized = item.unrealized.get(item.checkpoints)
|
|
|
|
u_justified_checkpoint =
|
|
|
|
if unrealized.justified != item.checkpoints.justified:
|
|
|
|
some unrealized.justified
|
|
|
|
else:
|
|
|
|
none(Checkpoint)
|
|
|
|
u_finalized_checkpoint =
|
|
|
|
if unrealized.finalized != item.checkpoints.finalized:
|
|
|
|
some unrealized.finalized
|
|
|
|
else:
|
|
|
|
none(Checkpoint)
|
2022-08-29 22:02:29 +00:00
|
|
|
|
2023-04-18 19:26:36 +00:00
|
|
|
response.fork_choice_nodes.add RestNode(
|
|
|
|
slot: item.bid.slot,
|
|
|
|
block_root: item.bid.root,
|
|
|
|
parent_root: item.parent,
|
|
|
|
justified_epoch: item.checkpoints.justified.epoch,
|
|
|
|
finalized_epoch: item.checkpoints.finalized.epoch,
|
|
|
|
weight: cast[uint64](item.weight),
|
|
|
|
validity:
|
|
|
|
if item.invalid:
|
|
|
|
RestNodeValidity.invalid
|
|
|
|
else:
|
2023-08-20 07:19:07 +00:00
|
|
|
# Fork choice doesn't necessarily prune finalized blocks in a
|
|
|
|
# synchronized way to the chaindag, so can't assume that when
|
|
|
|
# item.bid.slot is before finalizedHead, it has shared status
|
|
|
|
# regarding optimistic/validity as finalizedHead, so fallback
|
|
|
|
# to optimistic is more appropriate for this REST endpoint.
|
|
|
|
let blck = node.dag.getBlockRef(item.bid.root)
|
|
|
|
if blck.isNone() or not blck.get.executionValid:
|
|
|
|
RestNodeValidity.optimistic
|
|
|
|
else:
|
|
|
|
RestNodeValidity.valid,
|
2024-02-09 22:10:38 +00:00
|
|
|
execution_block_hash:
|
|
|
|
node.dag.loadExecutionBlockHash(item.bid).get(ZERO_HASH),
|
2023-04-18 19:26:36 +00:00
|
|
|
extra_data: some RestNodeExtraData(
|
|
|
|
justified_root: item.checkpoints.justified.root,
|
|
|
|
finalized_root: item.checkpoints.finalized.root,
|
|
|
|
u_justified_checkpoint: u_justified_checkpoint,
|
|
|
|
u_finalized_checkpoint: u_finalized_checkpoint,
|
|
|
|
best_child: item.bestChild,
|
|
|
|
bestDescendant: item.bestDescendant))
|
2023-04-10 15:35:14 +00:00
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonResponsePlain(response)
|