2021-04-28 16:41:02 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-04-28 16:41:02 +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-29 13:24:08 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
import
|
|
|
|
chronicles,
|
2023-10-24 01:58:52 +00:00
|
|
|
../beacon_chain/beacon_chain_db,
|
2021-04-28 16:41:02 +00:00
|
|
|
../beacon_chain/consensus_object_pools/blockchain_dag,
|
2023-10-24 01:58:52 +00:00
|
|
|
../beacon_chain/spec/[forks, state_transition],
|
2021-04-28 16:41:02 +00:00
|
|
|
eth/db/[kvstore, kvstore_sqlite3],
|
|
|
|
./testblockutil
|
|
|
|
|
2023-10-24 01:58:52 +00:00
|
|
|
from ../beacon_chain/spec/beaconstate import
|
|
|
|
initialize_hashed_beacon_state_from_eth1
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
export beacon_chain_db, testblockutil, kvstore, kvstore_sqlite3
|
|
|
|
|
2023-03-11 20:11:33 +00:00
|
|
|
proc makeTestDB*(
|
2023-05-15 15:41:30 +00:00
|
|
|
validators: Natural,
|
|
|
|
eth1Data = Opt.none(Eth1Data),
|
2023-10-24 01:58:52 +00:00
|
|
|
flags: UpdateFlags = {},
|
2023-05-15 15:41:30 +00:00
|
|
|
cfg = defaultRuntimeConfig): BeaconChainDB =
|
2023-10-26 03:40:04 +00:00
|
|
|
# Blob support requires DENEB_FORK_EPOCH != FAR_FUTURE_EPOCH
|
|
|
|
var cfg = cfg
|
2024-05-17 12:37:41 +00:00
|
|
|
if cfg.CAPELLA_FORK_EPOCH == FAR_FUTURE_EPOCH:
|
|
|
|
cfg.CAPELLA_FORK_EPOCH = 90000.Epoch
|
|
|
|
if cfg.DENEB_FORK_EPOCH == FAR_FUTURE_EPOCH:
|
|
|
|
cfg.DENEB_FORK_EPOCH = 100000.Epoch
|
2024-07-26 02:50:57 +00:00
|
|
|
if cfg.ELECTRA_FORK_EPOCH == FAR_FUTURE_EPOCH:
|
|
|
|
cfg.ELECTRA_FORK_EPOCH = 110000.Epoch
|
2023-10-26 03:40:04 +00:00
|
|
|
|
2023-07-14 07:35:58 +00:00
|
|
|
var genState = (ref ForkedHashedBeaconState)(
|
|
|
|
kind: ConsensusFork.Phase0,
|
|
|
|
phase0Data: initialize_hashed_beacon_state_from_eth1(
|
|
|
|
cfg,
|
|
|
|
ZERO_HASH,
|
|
|
|
0,
|
|
|
|
makeInitialDeposits(validators.uint64, flags),
|
|
|
|
flags))
|
2023-05-15 15:41:30 +00:00
|
|
|
|
|
|
|
# Override Eth1Data on request, skipping the lengthy Eth1 voting process
|
|
|
|
if eth1Data.isOk:
|
|
|
|
withState(genState[]):
|
|
|
|
forkyState.data.eth1_data = eth1Data.get
|
|
|
|
forkyState.root = hash_tree_root(forkyState.data)
|
State-only checkpoint state startup (#4251)
Currently, we require genesis and a checkpoint block and state to start
from an arbitrary slot - this PR relaxes this requirement so that we can
start with a state alone.
The current trusted-node-sync algorithm works by first downloading
blocks until we find an epoch aligned non-empty slot, then downloads the
state via slot.
However, current
[proposals](https://github.com/ethereum/beacon-APIs/pull/226) for
checkpointing prefer finalized state as
the main reference - this allows more simple access control and caching
on the server side - in particular, this should help checkpoint-syncing
from sources that have a fast `finalized` state download (like infura
and teku) but are slow when accessing state via slot.
Earlier versions of Nimbus will not be able to read databases created
without a checkpoint block and genesis. In most cases, backfilling makes
the database compatible except where genesis is also missing (custom
networks).
* backfill checkpoint block from libp2p instead of checkpoint source,
when doing trusted node sync
* allow starting the client without genesis / checkpoint block
* perform epoch start slot lookahead when loading tail state, so as to
deal with the case where the epoch start slot does not have a block
* replace `--blockId` with `--state-id` in TNS command line
* when replaying, also look at the parent of the last-known-block (even
if we don't have the parent block data, we can still replay from a
"parent" state) - in particular, this clears the way for implementing
state pruning
* deprecate `--finalized-checkpoint-block` option (no longer needed)
2022-11-02 10:02:38 +00:00
|
|
|
|
2023-07-14 07:35:58 +00:00
|
|
|
# Upgrade genesis state to later fork, if required by fork schedule
|
2024-04-28 14:13:17 +00:00
|
|
|
var cache: StateCache
|
|
|
|
cfg.maybeUpgradeState(genState[], cache)
|
2023-07-14 07:35:58 +00:00
|
|
|
withState(genState[]):
|
2024-04-05 19:30:06 +00:00
|
|
|
when consensusFork > ConsensusFork.Phase0:
|
2023-07-14 07:35:58 +00:00
|
|
|
forkyState.data.fork.previous_version =
|
|
|
|
forkyState.data.fork.current_version
|
|
|
|
forkyState.data.latest_block_header.body_root =
|
2023-10-05 12:01:40 +00:00
|
|
|
hash_tree_root(default(BeaconBlockBody(consensusFork)))
|
2023-07-14 07:35:58 +00:00
|
|
|
forkyState.root = hash_tree_root(forkyState.data)
|
|
|
|
|
2023-03-11 20:11:33 +00:00
|
|
|
result = BeaconChainDB.new("", cfg = cfg, inMemory = true)
|
State-only checkpoint state startup (#4251)
Currently, we require genesis and a checkpoint block and state to start
from an arbitrary slot - this PR relaxes this requirement so that we can
start with a state alone.
The current trusted-node-sync algorithm works by first downloading
blocks until we find an epoch aligned non-empty slot, then downloads the
state via slot.
However, current
[proposals](https://github.com/ethereum/beacon-APIs/pull/226) for
checkpointing prefer finalized state as
the main reference - this allows more simple access control and caching
on the server side - in particular, this should help checkpoint-syncing
from sources that have a fast `finalized` state download (like infura
and teku) but are slow when accessing state via slot.
Earlier versions of Nimbus will not be able to read databases created
without a checkpoint block and genesis. In most cases, backfilling makes
the database compatible except where genesis is also missing (custom
networks).
* backfill checkpoint block from libp2p instead of checkpoint source,
when doing trusted node sync
* allow starting the client without genesis / checkpoint block
* perform epoch start slot lookahead when loading tail state, so as to
deal with the case where the epoch start slot does not have a block
* replace `--blockId` with `--state-id` in TNS command line
* when replaying, also look at the parent of the last-known-block (even
if we don't have the parent block data, we can still replay from a
"parent" state) - in particular, this clears the way for implementing
state pruning
* deprecate `--finalized-checkpoint-block` option (no longer needed)
2022-11-02 10:02:38 +00:00
|
|
|
ChainDAGRef.preInit(result, genState[])
|
2022-10-03 13:10:08 +00:00
|
|
|
|
|
|
|
proc getEarliestInvalidBlockRoot*(
|
|
|
|
dag: ChainDAGRef, initialSearchRoot: Eth2Digest,
|
|
|
|
latestValidHash: Eth2Digest, defaultEarliestInvalidBlockRoot: Eth2Digest):
|
|
|
|
Eth2Digest =
|
|
|
|
# Earliest within a chain/fork in question, per LVH definition. Intended to
|
|
|
|
# be called with `initialRoot` as the parent of the block regarding which a
|
|
|
|
# newPayload or forkchoiceUpdated execution_status has been received as the
|
|
|
|
# tests effectively require being able to access this before the BlockRef's
|
|
|
|
# made. Therefore, to accommodate the EF consensus spec sync tests, and the
|
|
|
|
# possibilities that the LVH might be an immediate parent or a more distant
|
|
|
|
# ancestor special-case handling of an earliest invalid root as potentially
|
|
|
|
# not being from this function's search, but being provided as a default by
|
|
|
|
# the caller with access to the block.
|
|
|
|
var curBlck = dag.getBlockRef(initialSearchRoot).valueOr:
|
|
|
|
# Being asked to traverse a chain which the DAG doesn't know about -- but
|
|
|
|
# that'd imply the block's otherwise invalid for CL as well as EL.
|
|
|
|
return static(default(Eth2Digest))
|
|
|
|
|
|
|
|
# Only allow this special case outside loop; it's when the LVH is the direct
|
|
|
|
# parent of the reported invalid block
|
2023-04-11 23:31:47 +00:00
|
|
|
if curBlck.executionBlockHash.isSome and
|
|
|
|
curBlck.executionBlockHash.get == latestValidHash:
|
2022-10-03 13:10:08 +00:00
|
|
|
return defaultEarliestInvalidBlockRoot
|
|
|
|
|
|
|
|
while true:
|
|
|
|
# This was supposed to have been either caught by the pre-loop check or the
|
|
|
|
# parent check.
|
2023-04-11 23:31:47 +00:00
|
|
|
if curBlck.executionBlockHash.isSome and
|
|
|
|
curBlck.executionBlockHash.get == latestValidHash:
|
2022-10-03 13:10:08 +00:00
|
|
|
doAssert false, "getEarliestInvalidBlockRoot: unexpected LVH in loop body"
|
|
|
|
|
|
|
|
if (curBlck.parent.isNil) or
|
2023-04-11 23:31:47 +00:00
|
|
|
curBlck.parent.executionBlockHash.get(latestValidHash) ==
|
2022-10-03 13:10:08 +00:00
|
|
|
latestValidHash:
|
|
|
|
break
|
|
|
|
curBlck = curBlck.parent
|
|
|
|
|
2024-07-26 02:50:57 +00:00
|
|
|
curBlck.root
|