2022-06-19 05:57:52 +00:00
|
|
|
# beacon_chain
|
2023-01-20 14:14:37 +00:00
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-06-19 05:57:52 +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.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2022-06-19 05:57:52 +00:00
|
|
|
|
2022-01-05 14:49:10 +00:00
|
|
|
import std/[options, macros],
|
2022-01-05 15:42:29 +00:00
|
|
|
stew/byteutils, presto,
|
2021-08-12 13:08:20 +00:00
|
|
|
../spec/[forks],
|
2023-06-28 13:33:07 +00:00
|
|
|
../spec/eth2_apis/[rest_types, eth2_rest_serialization, rest_common],
|
2023-08-23 16:39:57 +00:00
|
|
|
../validators/beacon_validators,
|
2021-11-30 01:20:21 +00:00
|
|
|
../consensus_object_pools/blockchain_dag,
|
2022-08-19 10:30:07 +00:00
|
|
|
../beacon_node,
|
2022-01-05 14:49:10 +00:00
|
|
|
"."/[rest_constants, state_ttl_cache]
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
export
|
2021-11-30 01:20:21 +00:00
|
|
|
options, eth2_rest_serialization, blockchain_dag, presto, rest_types,
|
2023-06-28 13:33:07 +00:00
|
|
|
rest_constants, rest_common
|
2021-04-08 10:49:28 +00:00
|
|
|
|
2021-03-17 18:46:45 +00:00
|
|
|
type
|
2021-04-03 00:21:44 +00:00
|
|
|
ValidatorIndexError* {.pure.} = enum
|
|
|
|
UnsupportedValue, TooHighValue
|
|
|
|
|
2022-04-08 16:22:49 +00:00
|
|
|
func match(data: openArray[char], charset: set[char]): int =
|
2021-03-17 18:46:45 +00:00
|
|
|
for ch in data:
|
|
|
|
if ch notin charset:
|
|
|
|
return 1
|
|
|
|
0
|
|
|
|
|
2023-02-23 23:13:17 +00:00
|
|
|
proc getSyncedHead*(
|
|
|
|
node: BeaconNode,
|
|
|
|
slot: Slot
|
2023-05-25 13:57:24 +00:00
|
|
|
): Result[BlockRef, cstring] =
|
|
|
|
let head = node.dag.head
|
|
|
|
|
|
|
|
if not node.isSynced(head):
|
|
|
|
return err("Beacon node not fully and non-optimistically synced")
|
2022-11-25 22:49:31 +00:00
|
|
|
|
|
|
|
# Enough ahead not to know the shuffling
|
|
|
|
if slot > head.slot + SLOTS_PER_EPOCH * 2:
|
|
|
|
return err("Requesting far ahead of the current head")
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2023-05-25 13:57:24 +00:00
|
|
|
ok(head)
|
2022-03-23 11:42:16 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
func getCurrentSlot*(node: BeaconNode, slot: Slot):
|
|
|
|
Result[Slot, cstring] =
|
|
|
|
if slot <= (node.dag.head.slot + (SLOTS_PER_EPOCH * 2)):
|
|
|
|
ok(slot)
|
|
|
|
else:
|
|
|
|
err("Requesting slot too far ahead of the current head")
|
|
|
|
|
2023-02-23 23:13:17 +00:00
|
|
|
proc getSyncedHead*(
|
|
|
|
node: BeaconNode,
|
|
|
|
epoch: Epoch,
|
2023-05-25 13:57:24 +00:00
|
|
|
): Result[BlockRef, cstring] =
|
2021-04-27 20:46:24 +00:00
|
|
|
if epoch > MaxEpoch:
|
2021-03-17 18:46:45 +00:00
|
|
|
return err("Requesting epoch for which slot would overflow")
|
2022-03-23 11:42:16 +00:00
|
|
|
node.getSyncedHead(epoch.start_slot())
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2022-11-25 22:49:31 +00:00
|
|
|
func getBlockSlotId*(node: BeaconNode,
|
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
|
|
|
stateIdent: StateIdent): Result[BlockSlotId, cstring] =
|
2021-03-17 18:46:45 +00:00
|
|
|
case stateIdent.kind
|
|
|
|
of StateQueryKind.Slot:
|
2022-03-23 11:42:16 +00:00
|
|
|
# Limit requests by state id to the next epoch with respect to the current
|
|
|
|
# head to avoid long empty slot replays (in particular a second epoch
|
|
|
|
# transition)
|
|
|
|
if stateIdent.slot.epoch > (node.dag.head.slot.epoch + 1):
|
|
|
|
return err("Requesting state too far ahead of current head")
|
|
|
|
|
|
|
|
let bsi = node.dag.getBlockIdAtSlot(stateIdent.slot).valueOr:
|
2023-07-06 10:53:35 +00:00
|
|
|
return err("History for given slot not available")
|
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
|
|
|
|
|
|
|
ok(bsi)
|
|
|
|
|
2021-03-17 18:46:45 +00:00
|
|
|
of StateQueryKind.Root:
|
2022-03-16 07:20:40 +00:00
|
|
|
if stateIdent.root == getStateRoot(node.dag.headState):
|
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
|
|
|
ok(node.dag.head.bid.atSlot())
|
2021-12-06 18:52:35 +00:00
|
|
|
else:
|
2023-07-06 10:53:35 +00:00
|
|
|
# The `state_roots` field holds 8k historical state roots but not the
|
|
|
|
# one of the current state - this trick allows us to lookup states without
|
|
|
|
# keeping an on-disk index.
|
|
|
|
let headSlot = getStateField(node.dag.headState, slot)
|
|
|
|
for i in 0'u64..<SLOTS_PER_HISTORICAL_ROOT:
|
|
|
|
if i >= headSlot:
|
|
|
|
break
|
|
|
|
if getStateField(node.dag.headState, state_roots).item(
|
|
|
|
(headSlot - i - 1) mod SLOTS_PER_HISTORICAL_ROOT) ==
|
|
|
|
stateIdent.root:
|
|
|
|
return node.dag.getBlockIdAtSlot(headSlot - i - 1).orErr(
|
|
|
|
cstring("History for for given root not available"))
|
|
|
|
|
2021-12-06 18:52:35 +00:00
|
|
|
# We don't have a state root -> BlockSlot mapping
|
2023-07-06 10:53:35 +00:00
|
|
|
err("State root not found - use by-slot lookup to query deep state history")
|
2021-03-17 18:46:45 +00:00
|
|
|
of StateQueryKind.Named:
|
|
|
|
case stateIdent.value
|
|
|
|
of StateIdentType.Head:
|
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
|
|
|
ok(node.dag.head.bid.atSlot())
|
2021-03-17 18:46:45 +00:00
|
|
|
of StateIdentType.Genesis:
|
2022-10-14 19:40:10 +00:00
|
|
|
let bid = node.dag.getBlockIdAtSlot(GENESIS_SLOT).valueOr:
|
|
|
|
return err("Genesis state not available / pruned")
|
|
|
|
ok bid
|
2021-03-17 18:46:45 +00:00
|
|
|
of StateIdentType.Finalized:
|
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
|
|
|
ok(node.dag.finalizedHead.toBlockSlotId().expect("not nil"))
|
2021-03-17 18:46:45 +00:00
|
|
|
of StateIdentType.Justified:
|
2022-03-23 11:42:16 +00:00
|
|
|
# Take checkpoint-synced nodes into account
|
|
|
|
let justifiedEpoch =
|
|
|
|
max(
|
|
|
|
getStateField(node.dag.headState, current_justified_checkpoint).epoch,
|
|
|
|
node.dag.finalizedHead.slot.epoch)
|
|
|
|
ok(node.dag.head.atEpochStart(justifiedEpoch).toBlockSlotId().expect("not nil"))
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2022-03-11 12:08:17 +00:00
|
|
|
proc getBlockId*(node: BeaconNode, id: BlockIdent): Opt[BlockId] =
|
2021-03-17 18:46:45 +00:00
|
|
|
case id.kind
|
|
|
|
of BlockQueryKind.Named:
|
|
|
|
case id.value
|
|
|
|
of BlockIdentType.Head:
|
limit by-root requests to non-finalized blocks (#3293)
* limit by-root requests to non-finalized blocks
Presently, we keep a mapping from block root to `BlockRef` in memory -
this has simplified reasoning about the dag, but is not sustainable with
the chain growing.
We can distinguish between two cases where by-root access is useful:
* unfinalized blocks - this is where the beacon chain is operating
generally, by validating incoming data as interesting for future fork
choice decisions - bounded by the length of the unfinalized period
* finalized blocks - historical access in the REST API etc - no bounds,
really
In this PR, we limit the by-root block index to the first use case:
finalized chain data can more efficiently be addressed by slot number.
Future work includes:
* limiting the `BlockRef` horizon in general - each instance is 40
bytes+overhead which adds up - this needs further refactoring to deal
with the tail vs state problem
* persisting the finalized slot-to-hash index - this one also keeps
growing unbounded (albeit slowly)
Anyway, this PR easily shaves ~128mb of memory usage at the time of
writing.
* No longer honor `BeaconBlocksByRoot` requests outside of the
non-finalized period - previously, Nimbus would generously return any
block through this libp2p request - per the spec, finalized blocks
should be fetched via `BeaconBlocksByRange` instead.
* return `Opt[BlockRef]` instead of `nil` when blocks can't be found -
this becomes a lot more common now and thus deserves more attention
* `dag.blocks` -> `dag.forkBlocks` - this index only carries unfinalized
blocks from now - `finalizedBlocks` covers the other `BlockRef`
instances
* in backfill, verify that the last backfilled block leads back to
genesis, or panic
* add backfill timings to log
* fix missing check that `BlockRef` block can be fetched with
`getForkedBlock` reliably
* shortcut doppelganger check when feature is not enabled
* in REST/JSON-RPC, fetch blocks without involving `BlockRef`
* fix dag.blocks ref
2022-01-21 11:33:16 +00:00
|
|
|
ok(node.dag.head.bid)
|
2021-03-17 18:46:45 +00:00
|
|
|
of BlockIdentType.Genesis:
|
2022-10-14 19:40:10 +00:00
|
|
|
node.dag.getBlockIdAtSlot(GENESIS_SLOT).map(proc(x: auto): auto = x.bid)
|
2021-03-17 18:46:45 +00:00
|
|
|
of BlockIdentType.Finalized:
|
limit by-root requests to non-finalized blocks (#3293)
* limit by-root requests to non-finalized blocks
Presently, we keep a mapping from block root to `BlockRef` in memory -
this has simplified reasoning about the dag, but is not sustainable with
the chain growing.
We can distinguish between two cases where by-root access is useful:
* unfinalized blocks - this is where the beacon chain is operating
generally, by validating incoming data as interesting for future fork
choice decisions - bounded by the length of the unfinalized period
* finalized blocks - historical access in the REST API etc - no bounds,
really
In this PR, we limit the by-root block index to the first use case:
finalized chain data can more efficiently be addressed by slot number.
Future work includes:
* limiting the `BlockRef` horizon in general - each instance is 40
bytes+overhead which adds up - this needs further refactoring to deal
with the tail vs state problem
* persisting the finalized slot-to-hash index - this one also keeps
growing unbounded (albeit slowly)
Anyway, this PR easily shaves ~128mb of memory usage at the time of
writing.
* No longer honor `BeaconBlocksByRoot` requests outside of the
non-finalized period - previously, Nimbus would generously return any
block through this libp2p request - per the spec, finalized blocks
should be fetched via `BeaconBlocksByRange` instead.
* return `Opt[BlockRef]` instead of `nil` when blocks can't be found -
this becomes a lot more common now and thus deserves more attention
* `dag.blocks` -> `dag.forkBlocks` - this index only carries unfinalized
blocks from now - `finalizedBlocks` covers the other `BlockRef`
instances
* in backfill, verify that the last backfilled block leads back to
genesis, or panic
* add backfill timings to log
* fix missing check that `BlockRef` block can be fetched with
`getForkedBlock` reliably
* shortcut doppelganger check when feature is not enabled
* in REST/JSON-RPC, fetch blocks without involving `BlockRef`
* fix dag.blocks ref
2022-01-21 11:33:16 +00:00
|
|
|
ok(node.dag.finalizedHead.blck.bid)
|
2021-03-17 18:46:45 +00:00
|
|
|
of BlockQueryKind.Root:
|
2022-03-11 12:08:17 +00:00
|
|
|
node.dag.getBlockId(id.root)
|
2021-03-17 18:46:45 +00:00
|
|
|
of BlockQueryKind.Slot:
|
limit by-root requests to non-finalized blocks (#3293)
* limit by-root requests to non-finalized blocks
Presently, we keep a mapping from block root to `BlockRef` in memory -
this has simplified reasoning about the dag, but is not sustainable with
the chain growing.
We can distinguish between two cases where by-root access is useful:
* unfinalized blocks - this is where the beacon chain is operating
generally, by validating incoming data as interesting for future fork
choice decisions - bounded by the length of the unfinalized period
* finalized blocks - historical access in the REST API etc - no bounds,
really
In this PR, we limit the by-root block index to the first use case:
finalized chain data can more efficiently be addressed by slot number.
Future work includes:
* limiting the `BlockRef` horizon in general - each instance is 40
bytes+overhead which adds up - this needs further refactoring to deal
with the tail vs state problem
* persisting the finalized slot-to-hash index - this one also keeps
growing unbounded (albeit slowly)
Anyway, this PR easily shaves ~128mb of memory usage at the time of
writing.
* No longer honor `BeaconBlocksByRoot` requests outside of the
non-finalized period - previously, Nimbus would generously return any
block through this libp2p request - per the spec, finalized blocks
should be fetched via `BeaconBlocksByRange` instead.
* return `Opt[BlockRef]` instead of `nil` when blocks can't be found -
this becomes a lot more common now and thus deserves more attention
* `dag.blocks` -> `dag.forkBlocks` - this index only carries unfinalized
blocks from now - `finalizedBlocks` covers the other `BlockRef`
instances
* in backfill, verify that the last backfilled block leads back to
genesis, or panic
* add backfill timings to log
* fix missing check that `BlockRef` block can be fetched with
`getForkedBlock` reliably
* shortcut doppelganger check when feature is not enabled
* in REST/JSON-RPC, fetch blocks without involving `BlockRef`
* fix dag.blocks ref
2022-01-21 11:33:16 +00:00
|
|
|
let bsid = node.dag.getBlockIdAtSlot(id.slot)
|
2022-03-15 08:24:55 +00:00
|
|
|
if bsid.isSome and bsid.get().isProposed():
|
|
|
|
ok bsid.get().bid
|
limit by-root requests to non-finalized blocks (#3293)
* limit by-root requests to non-finalized blocks
Presently, we keep a mapping from block root to `BlockRef` in memory -
this has simplified reasoning about the dag, but is not sustainable with
the chain growing.
We can distinguish between two cases where by-root access is useful:
* unfinalized blocks - this is where the beacon chain is operating
generally, by validating incoming data as interesting for future fork
choice decisions - bounded by the length of the unfinalized period
* finalized blocks - historical access in the REST API etc - no bounds,
really
In this PR, we limit the by-root block index to the first use case:
finalized chain data can more efficiently be addressed by slot number.
Future work includes:
* limiting the `BlockRef` horizon in general - each instance is 40
bytes+overhead which adds up - this needs further refactoring to deal
with the tail vs state problem
* persisting the finalized slot-to-hash index - this one also keeps
growing unbounded (albeit slowly)
Anyway, this PR easily shaves ~128mb of memory usage at the time of
writing.
* No longer honor `BeaconBlocksByRoot` requests outside of the
non-finalized period - previously, Nimbus would generously return any
block through this libp2p request - per the spec, finalized blocks
should be fetched via `BeaconBlocksByRange` instead.
* return `Opt[BlockRef]` instead of `nil` when blocks can't be found -
this becomes a lot more common now and thus deserves more attention
* `dag.blocks` -> `dag.forkBlocks` - this index only carries unfinalized
blocks from now - `finalizedBlocks` covers the other `BlockRef`
instances
* in backfill, verify that the last backfilled block leads back to
genesis, or panic
* add backfill timings to log
* fix missing check that `BlockRef` block can be fetched with
`getForkedBlock` reliably
* shortcut doppelganger check when feature is not enabled
* in REST/JSON-RPC, fetch blocks without involving `BlockRef`
* fix dag.blocks ref
2022-01-21 11:33:16 +00:00
|
|
|
else:
|
2022-03-11 12:08:17 +00:00
|
|
|
err()
|
2021-12-06 18:52:35 +00:00
|
|
|
|
limit by-root requests to non-finalized blocks (#3293)
* limit by-root requests to non-finalized blocks
Presently, we keep a mapping from block root to `BlockRef` in memory -
this has simplified reasoning about the dag, but is not sustainable with
the chain growing.
We can distinguish between two cases where by-root access is useful:
* unfinalized blocks - this is where the beacon chain is operating
generally, by validating incoming data as interesting for future fork
choice decisions - bounded by the length of the unfinalized period
* finalized blocks - historical access in the REST API etc - no bounds,
really
In this PR, we limit the by-root block index to the first use case:
finalized chain data can more efficiently be addressed by slot number.
Future work includes:
* limiting the `BlockRef` horizon in general - each instance is 40
bytes+overhead which adds up - this needs further refactoring to deal
with the tail vs state problem
* persisting the finalized slot-to-hash index - this one also keeps
growing unbounded (albeit slowly)
Anyway, this PR easily shaves ~128mb of memory usage at the time of
writing.
* No longer honor `BeaconBlocksByRoot` requests outside of the
non-finalized period - previously, Nimbus would generously return any
block through this libp2p request - per the spec, finalized blocks
should be fetched via `BeaconBlocksByRange` instead.
* return `Opt[BlockRef]` instead of `nil` when blocks can't be found -
this becomes a lot more common now and thus deserves more attention
* `dag.blocks` -> `dag.forkBlocks` - this index only carries unfinalized
blocks from now - `finalizedBlocks` covers the other `BlockRef`
instances
* in backfill, verify that the last backfilled block leads back to
genesis, or panic
* add backfill timings to log
* fix missing check that `BlockRef` block can be fetched with
`getForkedBlock` reliably
* shortcut doppelganger check when feature is not enabled
* in REST/JSON-RPC, fetch blocks without involving `BlockRef`
* fix dag.blocks ref
2022-01-21 11:33:16 +00:00
|
|
|
proc getForkedBlock*(node: BeaconNode, id: BlockIdent):
|
2022-03-11 12:08:17 +00:00
|
|
|
Opt[ForkedTrustedSignedBeaconBlock] =
|
|
|
|
let bid = ? node.getBlockId(id)
|
|
|
|
|
|
|
|
node.dag.getForkedBlock(bid)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2022-11-25 22:49:31 +00:00
|
|
|
func disallowInterruptionsAux(body: NimNode) =
|
2022-01-05 14:49:10 +00:00
|
|
|
for n in body:
|
|
|
|
const because =
|
|
|
|
"because the `state` variable may be mutated (and thus invalidated) " &
|
|
|
|
"before the function resumes execution."
|
2021-03-17 20:42:55 +00:00
|
|
|
|
2022-01-05 14:49:10 +00:00
|
|
|
if n.kind == nnkYieldStmt:
|
|
|
|
macros.error "You cannot use yield in this block " & because, n
|
|
|
|
|
|
|
|
if (n.kind in {nnkCall, nnkCommand} and
|
|
|
|
n[0].kind in {nnkIdent, nnkSym} and
|
|
|
|
$n[0] == "await"):
|
|
|
|
macros.error "You cannot use await in this block " & because, n
|
|
|
|
|
|
|
|
disallowInterruptionsAux(n)
|
|
|
|
|
|
|
|
macro disallowInterruptions(body: untyped) =
|
|
|
|
disallowInterruptionsAux(body)
|
|
|
|
|
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
|
|
|
template withStateForBlockSlotId*(nodeParam: BeaconNode,
|
|
|
|
blockSlotIdParam: BlockSlotId,
|
|
|
|
body: untyped): untyped =
|
2022-01-05 14:49:10 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
let
|
|
|
|
node = nodeParam
|
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
|
|
|
blockSlotId = blockSlotIdParam
|
2022-01-05 14:49:10 +00:00
|
|
|
|
2022-03-16 07:20:40 +00:00
|
|
|
template isState(state: ForkedHashedBeaconState): bool =
|
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
|
|
|
state.matches_block_slot(blockSlotId.bid.root, blockSlotId.slot)
|
2022-01-05 14:49:10 +00:00
|
|
|
|
|
|
|
var cache {.inject, used.}: StateCache
|
|
|
|
|
|
|
|
# If we have a cache hit, there is a concern that the REST request
|
|
|
|
# handler may continue executing asynchronously while we hit the same
|
|
|
|
# advanced state is another request. We don't want the two requests
|
|
|
|
# to work over the same state object because mutations to it will be
|
|
|
|
# visible in both, so we must outlaw yielding within the `body` block.
|
|
|
|
# Please note that the problem is not limited to the situations where
|
|
|
|
# we have a cache hit. Working with the `headState` will result in the
|
|
|
|
# same problem as it may change while the request is executing.
|
|
|
|
#
|
|
|
|
# TODO
|
|
|
|
# The solution below is only partion, because it theory yields or awaits
|
|
|
|
# can still be hidden in the body through the use of helper templates
|
|
|
|
disallowInterruptions(body)
|
|
|
|
|
|
|
|
# TODO view-types
|
|
|
|
# Avoid the code bloat produced by the double `body` reference through a lent var
|
|
|
|
if isState(node.dag.headState):
|
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
|
|
|
template state: untyped {.inject, used.} = node.dag.headState
|
|
|
|
template stateRoot: untyped {.inject, used.} =
|
|
|
|
getStateRoot(node.dag.headState)
|
|
|
|
body
|
2022-01-05 14:49:10 +00:00
|
|
|
else:
|
|
|
|
let cachedState = if node.stateTtlCache != nil:
|
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.stateTtlCache.getClosestState(node.dag, blockSlotId)
|
2022-01-05 14:49:10 +00:00
|
|
|
else:
|
|
|
|
nil
|
|
|
|
|
|
|
|
let stateToAdvance = if cachedState != nil:
|
|
|
|
cachedState
|
|
|
|
else:
|
|
|
|
assignClone(node.dag.headState)
|
|
|
|
|
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
|
|
|
if node.dag.updateState(stateToAdvance[], blockSlotId, false, cache):
|
2022-01-05 18:38:04 +00:00
|
|
|
if cachedState == nil and node.stateTtlCache != nil:
|
|
|
|
# This was not a cached state, we can cache it now
|
|
|
|
node.stateTtlCache.add(stateToAdvance)
|
2022-01-05 14:49:10 +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
|
|
|
template state: untyped {.inject, used.} = stateToAdvance[]
|
|
|
|
template stateRoot: untyped {.inject, used.} = getStateRoot(stateToAdvance[])
|
|
|
|
|
|
|
|
body
|
2021-04-03 00:21:44 +00:00
|
|
|
|
2022-01-05 15:42:29 +00:00
|
|
|
template strData*(body: ContentBody): string =
|
|
|
|
bind fromBytes
|
|
|
|
string.fromBytes(body.data)
|
|
|
|
|
2022-11-25 22:49:31 +00:00
|
|
|
func toValidatorIndex*(value: RestValidatorIndex): Result[ValidatorIndex,
|
2021-04-03 00:21:44 +00:00
|
|
|
ValidatorIndexError] =
|
|
|
|
when sizeof(ValidatorIndex) == 4:
|
|
|
|
if uint64(value) < VALIDATOR_REGISTRY_LIMIT:
|
2021-04-09 16:12:59 +00:00
|
|
|
# On x86 platform Nim allows only `int32` indexes, so all the indexes in
|
|
|
|
# range `2^31 <= x < 2^32` are not supported.
|
|
|
|
if uint64(value) <= uint64(high(int32)):
|
2021-04-03 00:21:44 +00:00
|
|
|
ok(ValidatorIndex(value))
|
|
|
|
else:
|
|
|
|
err(ValidatorIndexError.UnsupportedValue)
|
|
|
|
else:
|
|
|
|
err(ValidatorIndexError.TooHighValue)
|
|
|
|
elif sizeof(ValidatorIndex) == 8:
|
|
|
|
if uint64(value) < VALIDATOR_REGISTRY_LIMIT:
|
|
|
|
ok(ValidatorIndex(value))
|
|
|
|
else:
|
|
|
|
err(ValidatorIndexError.TooHighValue)
|
|
|
|
else:
|
|
|
|
doAssert(false, "ValidatorIndex type size is incorrect")
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2021-10-14 10:38:38 +00:00
|
|
|
func syncCommitteeParticipants*(forkedState: ForkedHashedBeaconState,
|
2021-11-30 01:14:31 +00:00
|
|
|
epoch: Epoch
|
|
|
|
): Result[seq[ValidatorPubKey], cstring] =
|
2021-10-14 10:38:38 +00:00
|
|
|
withState(forkedState):
|
2023-03-11 00:35:52 +00:00
|
|
|
when consensusFork >= ConsensusFork.Altair:
|
2021-10-14 10:38:38 +00:00
|
|
|
let
|
2021-10-20 16:32:46 +00:00
|
|
|
epochPeriod = sync_committee_period(epoch)
|
2022-09-13 11:53:12 +00:00
|
|
|
curPeriod = sync_committee_period(forkyState.data.slot)
|
2021-10-20 16:32:46 +00:00
|
|
|
if epochPeriod == curPeriod:
|
2022-09-13 11:53:12 +00:00
|
|
|
ok(@(forkyState.data.current_sync_committee.pubkeys.data))
|
2021-10-20 16:32:46 +00:00
|
|
|
elif epochPeriod == curPeriod + 1:
|
2022-09-13 11:53:12 +00:00
|
|
|
ok(@(forkyState.data.next_sync_committee.pubkeys.data))
|
2021-10-14 10:38:38 +00:00
|
|
|
else:
|
|
|
|
err("Epoch is outside the sync committee period of the state")
|
|
|
|
else:
|
|
|
|
err("State's fork do not support sync committees")
|
|
|
|
|
|
|
|
func keysToIndices*(cacheTable: var Table[ValidatorPubKey, ValidatorIndex],
|
|
|
|
forkedState: ForkedHashedBeaconState,
|
|
|
|
keys: openArray[ValidatorPubKey]
|
|
|
|
): seq[Option[ValidatorIndex]] =
|
|
|
|
var indices = newSeq[Option[ValidatorIndex]](len(keys))
|
2022-03-07 15:02:15 +00:00
|
|
|
let totalValidatorsInState = getStateField(forkedState, validators).lenu64
|
2021-10-14 10:38:38 +00:00
|
|
|
var keyset =
|
|
|
|
block:
|
|
|
|
var res: Table[ValidatorPubKey, int]
|
2022-05-10 10:03:40 +00:00
|
|
|
for inputIndex, pubkey in keys:
|
2021-10-14 10:38:38 +00:00
|
|
|
# Try to search in cache first.
|
|
|
|
cacheTable.withValue(pubkey, vindex):
|
2022-03-07 15:02:15 +00:00
|
|
|
if uint64(vindex[]) < totalValidatorsInState:
|
|
|
|
indices[inputIndex] = some(vindex[])
|
2021-10-14 10:38:38 +00:00
|
|
|
do:
|
|
|
|
res[pubkey] = inputIndex
|
|
|
|
res
|
|
|
|
if len(keyset) > 0:
|
2022-05-10 10:03:40 +00:00
|
|
|
for validatorIndex, validator in getStateField(forkedState, validators):
|
2021-10-14 10:38:38 +00:00
|
|
|
keyset.withValue(validator.pubkey, listIndex):
|
|
|
|
# Store pair (pubkey, index) into cache table.
|
|
|
|
cacheTable[validator.pubkey] = ValidatorIndex(validatorIndex)
|
|
|
|
# Fill result sequence.
|
|
|
|
indices[listIndex[]] = some(ValidatorIndex(validatorIndex))
|
|
|
|
indices
|
|
|
|
|
2023-07-31 18:12:15 +00:00
|
|
|
proc getBidOptimistic*(node: BeaconNode, bid: BlockId): Option[bool] =
|
|
|
|
if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH:
|
|
|
|
some[bool](node.dag.is_optimistic(bid))
|
|
|
|
else:
|
|
|
|
none[bool]()
|
|
|
|
|
2023-05-15 20:42:42 +00:00
|
|
|
proc getShufflingOptimistic*(node: BeaconNode,
|
|
|
|
dependentSlot: Slot,
|
|
|
|
dependentRoot: Eth2Digest): Option[bool] =
|
|
|
|
if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH:
|
2023-05-25 13:57:24 +00:00
|
|
|
# `slot` in this `BlockId` may be higher than block's actual slot,
|
|
|
|
# this is alright for the purpose of calling `is_optimistic`.
|
|
|
|
let bid = BlockId(slot: dependentSlot, root: dependentRoot)
|
|
|
|
some[bool](node.dag.is_optimistic(bid))
|
2023-05-15 20:42:42 +00:00
|
|
|
else:
|
|
|
|
none[bool]()
|
|
|
|
|
2022-06-20 05:53:39 +00:00
|
|
|
proc getStateOptimistic*(node: BeaconNode,
|
|
|
|
state: ForkedHashedBeaconState): Option[bool] =
|
2022-06-28 10:21:16 +00:00
|
|
|
if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH:
|
2023-05-25 13:57:24 +00:00
|
|
|
if state.kind >= ConsensusFork.Bellatrix:
|
2022-07-04 20:35:33 +00:00
|
|
|
# A state is optimistic iff the block which created it is
|
2023-05-25 13:57:24 +00:00
|
|
|
let stateBid = withState(state): forkyState.latest_block_id
|
|
|
|
some[bool](node.dag.is_optimistic(stateBid))
|
|
|
|
else:
|
|
|
|
some[bool](false)
|
2022-06-20 05:53:39 +00:00
|
|
|
else:
|
|
|
|
none[bool]()
|
|
|
|
|
|
|
|
proc getBlockOptimistic*(node: BeaconNode,
|
|
|
|
blck: ForkedTrustedSignedBeaconBlock |
|
|
|
|
ForkedSignedBeaconBlock): Option[bool] =
|
2022-06-28 10:21:16 +00:00
|
|
|
if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH:
|
2023-05-25 13:57:24 +00:00
|
|
|
if blck.kind >= ConsensusFork.Bellatrix:
|
|
|
|
some[bool](node.dag.is_optimistic(blck.toBlockId()))
|
|
|
|
else:
|
2022-06-20 05:53:39 +00:00
|
|
|
some[bool](false)
|
|
|
|
else:
|
|
|
|
none[bool]()
|
|
|
|
|
2022-01-21 16:52:34 +00:00
|
|
|
const
|
|
|
|
jsonMediaType* = MediaType.init("application/json")
|
|
|
|
sszMediaType* = MediaType.init("application/octet-stream")
|
|
|
|
textEventStreamMediaType* = MediaType.init("text/event-stream")
|
2022-12-19 13:11:12 +00:00
|
|
|
|
|
|
|
proc verifyRandao*(
|
|
|
|
node: BeaconNode, slot: Slot, proposer: ValidatorIndex,
|
|
|
|
randao: ValidatorSig, skip_randao_verification: bool): bool =
|
|
|
|
let
|
2023-01-11 12:29:21 +00:00
|
|
|
proposer_pubkey = node.dag.validatorKey(proposer).valueOr:
|
|
|
|
return false
|
2022-12-19 13:11:12 +00:00
|
|
|
|
|
|
|
if skip_randao_verification:
|
|
|
|
randao == ValidatorSig.infinity()
|
|
|
|
else:
|
|
|
|
let
|
|
|
|
fork = node.dag.forkAtEpoch(slot.epoch)
|
|
|
|
genesis_validators_root = node.dag.genesis_validators_root
|
|
|
|
|
|
|
|
verify_epoch_signature(
|
2023-01-11 12:29:21 +00:00
|
|
|
fork, genesis_validators_root, slot.epoch, proposer_pubkey, randao)
|