2021-03-15 14:11:51 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
2020-04-23 06:27:35 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2018-11-23 23:58:49 +00:00
|
|
|
import
|
2021-05-17 16:37:26 +00:00
|
|
|
std/[typetraits, tables],
|
|
|
|
stew/[arrayops, assign2, byteutils, endians2, io2, objects, results],
|
2020-06-13 18:57:07 +00:00
|
|
|
serialization, chronicles, snappy,
|
2020-10-12 01:07:20 +00:00
|
|
|
eth/db/[kvstore, kvstore_sqlite3],
|
2021-03-15 14:11:51 +00:00
|
|
|
./networking/network_metadata, ./beacon_chain_db_immutable,
|
2021-02-15 16:40:00 +00:00
|
|
|
./spec/[crypto, datatypes, digest, state_transition],
|
2020-10-27 11:04:17 +00:00
|
|
|
./ssz/[ssz_serialization, merkleization],
|
2021-03-05 13:12:00 +00:00
|
|
|
./filepath
|
2018-11-23 23:58:49 +00:00
|
|
|
|
|
|
|
type
|
2020-10-12 01:07:20 +00:00
|
|
|
DbSeq*[T] = object
|
2020-10-28 18:35:31 +00:00
|
|
|
insertStmt: SqliteStmt[openArray[byte], void]
|
2020-11-21 17:53:40 +00:00
|
|
|
selectStmt: SqliteStmt[int64, openArray[byte]]
|
2020-10-13 19:16:54 +00:00
|
|
|
recordCount: int64
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2020-10-15 11:49:02 +00:00
|
|
|
DepositsSeq = DbSeq[DepositData]
|
2021-03-15 14:11:51 +00:00
|
|
|
ImmutableValidatorsSeq = DbSeq[ImmutableValidatorData]
|
2020-10-15 11:49:02 +00:00
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
DepositContractSnapshot* = object
|
|
|
|
eth1Block*: Eth2Digest
|
|
|
|
depositContractState*: DepositContractState
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
BeaconChainDBV0* = ref object
|
|
|
|
## BeaconChainDBV0 based on old kvstore table that sets the WITHOUT ROWID
|
|
|
|
## option which becomes unbearably slow with large blobs. It is used as a
|
|
|
|
## read-only store to support old versions - by freezing it at its current
|
|
|
|
## data set, downgrading remains possible since it's no longer touched -
|
|
|
|
## anyone downgrading will have to sync up whatever they missed.
|
|
|
|
##
|
|
|
|
## Newer versions read from the new tables first - if the data is not found,
|
|
|
|
## they turn to the old tables for reading. Writing is done only to the new
|
|
|
|
## tables.
|
|
|
|
##
|
|
|
|
## V0 stored most data in a single table, prefixing each key with a tag
|
|
|
|
## identifying the type of data.
|
|
|
|
##
|
|
|
|
## 1.1 introduced BeaconStateNoImmutableValidators storage where immutable
|
|
|
|
## validator data is stored in a separate table and only a partial
|
|
|
|
## BeaconState is written to kvstore
|
|
|
|
##
|
|
|
|
## 1.2 moved BeaconStateNoImmutableValidators to a separate table to
|
|
|
|
## alleviate some of the btree balancing issues - this doubled the speed but
|
|
|
|
## was still slow
|
|
|
|
backend: KvStoreRef # kvstore
|
|
|
|
stateStore: KvStoreRef # state_no_validators
|
|
|
|
|
2018-11-23 23:58:49 +00:00
|
|
|
BeaconChainDB* = ref object
|
2019-03-13 22:59:20 +00:00
|
|
|
## Database storing resolved blocks and states - resolved blocks are such
|
|
|
|
## blocks that form a chain back to the tail block.
|
2020-04-23 06:27:35 +00:00
|
|
|
##
|
|
|
|
## We assume that the database backend is working / not corrupt - as such,
|
|
|
|
## we will raise a Defect any time there is an issue. This should be
|
|
|
|
## revisited in the future, when/if the calling code safely can handle
|
|
|
|
## corruption of this kind.
|
|
|
|
##
|
|
|
|
## We do however make an effort not to crash on invalid data inside the
|
|
|
|
## database - this may have a number of "natural" causes such as switching
|
|
|
|
## between different versions of the client and accidentally using an old
|
|
|
|
## database.
|
2021-05-17 16:37:26 +00:00
|
|
|
db: SqStoreRef
|
|
|
|
|
|
|
|
v0: BeaconChainDBV0
|
2021-01-29 21:21:44 +00:00
|
|
|
preset*: RuntimePreset
|
2020-11-24 21:21:47 +00:00
|
|
|
genesisDeposits*: DepositsSeq
|
2021-03-15 14:11:51 +00:00
|
|
|
|
|
|
|
# ImmutableValidatorsSeq only stores the total count; it's a proxy for SQL
|
|
|
|
# queries.
|
|
|
|
immutableValidators*: ImmutableValidatorsSeq
|
|
|
|
immutableValidatorsMem*: seq[ImmutableValidatorData]
|
|
|
|
|
2021-03-24 16:20:55 +00:00
|
|
|
checkpoint*: proc() {.gcsafe, raises: [Defect].}
|
2020-10-26 08:55:10 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
keyValues: KvStoreRef # Random stuff using DbKeyKind - suitable for small values mainly!
|
|
|
|
blocks: KvStoreRef # BlockRoot -> TrustedBeaconBlock
|
|
|
|
stateRoots: KvStoreRef # (Slot, BlockRoot) -> StateRoot
|
|
|
|
statesNoVal: KvStoreRef # StateRoot -> BeaconStateNoImmutableValidators
|
|
|
|
stateDiffs: KvStoreRef ##\
|
|
|
|
## StateRoot -> BeaconStateDiff
|
|
|
|
## Instead of storing full BeaconStates, one can store only the diff from
|
|
|
|
## a different state. As 75% of a typical BeaconState's serialized form's
|
|
|
|
## the validators, which are mostly immutable and append-only, just using
|
|
|
|
## a simple append-diff representation helps significantly. Various roots
|
|
|
|
## are stored in a mod-increment pattern across fixed-sized arrays, which
|
|
|
|
## addresses most of the rest of the BeaconState sizes.
|
2021-04-06 18:56:45 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
summaries: KvStoreRef # BlockRoot -> BeaconBlockSummary
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2019-01-14 12:19:44 +00:00
|
|
|
DbKeyKind = enum
|
2019-02-21 04:42:17 +00:00
|
|
|
kHashToState
|
2019-01-14 12:19:44 +00:00
|
|
|
kHashToBlock
|
2020-09-22 20:42:42 +00:00
|
|
|
kHeadBlock
|
|
|
|
## Pointer to the most recent block selected by the fork choice
|
|
|
|
kTailBlock
|
|
|
|
## Pointer to the earliest finalized block - this is the genesis block when
|
|
|
|
## the chain starts, but might advance as the database gets pruned
|
|
|
|
## TODO: determine how aggressively the database should be pruned. For a
|
|
|
|
## healthy network sync, we probably need to store blocks at least
|
|
|
|
## past the weak subjectivity period.
|
|
|
|
kBlockSlotStateRoot
|
|
|
|
## BlockSlot -> state_root mapping
|
|
|
|
kGenesisBlockRoot
|
|
|
|
## Immutable reference to the network genesis state
|
|
|
|
## (needed for satisfying requests to the beacon node API).
|
2021-05-17 16:37:26 +00:00
|
|
|
kEth1PersistedTo # Obsolete
|
|
|
|
kDepositsFinalizedByEth1 # Obsolete
|
2020-11-24 21:21:47 +00:00
|
|
|
kDepositsFinalizedByEth2
|
|
|
|
## A merkleizer checkpoint used for computing merkle proofs of
|
|
|
|
## deposits added to Eth2 blocks (it may lag behind the finalized
|
|
|
|
## eth1 deposits checkpoint).
|
2021-05-17 16:37:26 +00:00
|
|
|
kHashToBlockSummary # Block summaries for fast startup
|
2020-11-24 21:21:47 +00:00
|
|
|
kSpeculativeDeposits
|
|
|
|
## A merkelizer checkpoint created on the basis of deposit events
|
|
|
|
## that we were not able to verify against a `deposit_root` served
|
|
|
|
## by the web3 provider. This may happen on Geth nodes that serve
|
|
|
|
## only recent contract state data (i.e. only recent `deposit_roots`).
|
2021-05-17 16:37:26 +00:00
|
|
|
kHashToStateDiff # Obsolete
|
2021-03-15 14:11:51 +00:00
|
|
|
kHashToStateOnlyMutableValidators
|
2020-11-03 22:30:43 +00:00
|
|
|
|
|
|
|
BeaconBlockSummary* = object
|
2021-05-17 16:37:26 +00:00
|
|
|
## Cache of beacon block summaries - during startup when we construct the
|
|
|
|
## chain dag, loading full blocks takes a lot of time - the block
|
|
|
|
## summary contains a minimal snapshot of what's needed to instanciate
|
|
|
|
## the BlockRef tree.
|
2020-11-03 22:30:43 +00:00
|
|
|
slot*: Slot
|
|
|
|
parent_root*: Eth2Digest
|
2019-01-14 12:19:44 +00:00
|
|
|
|
2020-08-18 20:31:55 +00:00
|
|
|
const
|
2020-11-12 07:42:45 +00:00
|
|
|
# The largest object we're saving is the BeaconState, and by far, the largest
|
|
|
|
# part of it is the validator - each validator takes up at least 129 bytes
|
|
|
|
# in phase0, which means 100k validators is >12mb - in addition to this,
|
|
|
|
# there are several MB of hashes.
|
|
|
|
maxDecompressedDbRecordSize = 64*1024*1024
|
2020-08-18 20:31:55 +00:00
|
|
|
|
2020-04-23 06:27:35 +00:00
|
|
|
# Subkeys essentially create "tables" within the key-value store by prefixing
|
|
|
|
# each entry with a table id
|
|
|
|
|
2019-02-21 04:42:17 +00:00
|
|
|
func subkey(kind: DbKeyKind): array[1, byte] =
|
|
|
|
result[0] = byte ord(kind)
|
2019-01-14 12:19:44 +00:00
|
|
|
|
2019-03-04 11:50:26 +00:00
|
|
|
func subkey[N: static int](kind: DbKeyKind, key: array[N, byte]):
|
|
|
|
array[N + 1, byte] =
|
|
|
|
result[0] = byte ord(kind)
|
|
|
|
result[1 .. ^1] = key
|
|
|
|
|
2020-04-28 08:08:32 +00:00
|
|
|
func subkey(kind: type BeaconState, key: Eth2Digest): auto =
|
2019-02-21 04:42:17 +00:00
|
|
|
subkey(kHashToState, key.data)
|
2019-01-25 14:17:35 +00:00
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
func subkey(
|
|
|
|
kind: type BeaconStateNoImmutableValidators, key: Eth2Digest): auto =
|
|
|
|
subkey(kHashToStateOnlyMutableValidators, key.data)
|
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
func subkey(kind: type SignedBeaconBlock, key: Eth2Digest): auto =
|
2019-02-21 04:42:17 +00:00
|
|
|
subkey(kHashToBlock, key.data)
|
2019-01-25 14:17:35 +00:00
|
|
|
|
2020-11-03 22:30:43 +00:00
|
|
|
func subkey(kind: type BeaconBlockSummary, key: Eth2Digest): auto =
|
|
|
|
subkey(kHashToBlockSummary, key.data)
|
|
|
|
|
2020-04-23 06:27:35 +00:00
|
|
|
func subkey(root: Eth2Digest, slot: Slot): array[40, byte] =
|
|
|
|
var ret: array[40, byte]
|
|
|
|
# big endian to get a naturally ascending order on slots in sorted indices
|
|
|
|
ret[0..<8] = toBytesBE(slot.uint64)
|
|
|
|
# .. but 7 bytes should be enough for slots - in return, we get a nicely
|
|
|
|
# rounded key length
|
2019-03-28 06:10:48 +00:00
|
|
|
ret[0] = byte ord(kBlockSlotStateRoot)
|
2020-04-23 06:27:35 +00:00
|
|
|
ret[8..<40] = root.data
|
2019-03-28 06:10:48 +00:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2020-10-12 01:07:20 +00:00
|
|
|
template panic =
|
2020-11-10 18:41:04 +00:00
|
|
|
# TODO(zah): Could we recover from a corrupted database?
|
|
|
|
# Review all usages.
|
2020-10-12 01:07:20 +00:00
|
|
|
raiseAssert "The database should not be corrupted"
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
template expectDb(x: auto): untyped =
|
|
|
|
# There's no meaningful error handling implemented for a corrupt database or
|
|
|
|
# full disk - this requires manual intervention, so we'll panic for now
|
|
|
|
x.expect("working database (disk broken/full?)")
|
|
|
|
|
2020-10-15 11:49:02 +00:00
|
|
|
proc init*[T](Seq: type DbSeq[T], db: SqStoreRef, name: string): Seq =
|
2020-10-13 19:16:54 +00:00
|
|
|
db.exec("""
|
|
|
|
CREATE TABLE IF NOT EXISTS """ & name & """(
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
value BLOB
|
|
|
|
);
|
2021-05-17 16:37:26 +00:00
|
|
|
""").expectDb()
|
2020-10-13 19:16:54 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
insertStmt = db.prepareStmt(
|
|
|
|
"INSERT INTO " & name & "(value) VALUES (?);",
|
2021-05-17 16:37:26 +00:00
|
|
|
openArray[byte], void, managed = false).expect("this is a valid statement")
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2020-10-13 19:16:54 +00:00
|
|
|
selectStmt = db.prepareStmt(
|
|
|
|
"SELECT value FROM " & name & " WHERE id = ?;",
|
2021-05-17 16:37:26 +00:00
|
|
|
int64, openArray[byte], managed = false).expect("this is a valid statement")
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2020-10-13 19:16:54 +00:00
|
|
|
countStmt = db.prepareStmt(
|
2020-11-24 21:21:47 +00:00
|
|
|
"SELECT COUNT(1) FROM " & name & ";",
|
2021-05-17 16:37:26 +00:00
|
|
|
NoParams, int64, managed = false).expect("this is a valid statement")
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2020-10-13 19:16:54 +00:00
|
|
|
var recordCount = int64 0
|
|
|
|
let countQueryRes = countStmt.exec do (res: int64):
|
|
|
|
recordCount = res
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
let found = countQueryRes.expectDb()
|
2020-10-13 19:16:54 +00:00
|
|
|
if not found: panic()
|
2021-05-17 16:37:26 +00:00
|
|
|
countStmt.dispose()
|
2020-10-13 19:16:54 +00:00
|
|
|
|
2020-10-15 11:49:02 +00:00
|
|
|
Seq(insertStmt: insertStmt,
|
|
|
|
selectStmt: selectStmt,
|
|
|
|
recordCount: recordCount)
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc close*(s: DbSeq) =
|
|
|
|
s.insertStmt.dispose()
|
|
|
|
s.selectStmt.dispose()
|
|
|
|
|
2020-10-12 01:07:20 +00:00
|
|
|
proc add*[T](s: var DbSeq[T], val: T) =
|
|
|
|
var bytes = SSZ.encode(val)
|
2021-05-17 16:37:26 +00:00
|
|
|
s.insertStmt.exec(bytes).expectDb()
|
2020-10-14 14:04:08 +00:00
|
|
|
inc s.recordCount
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2021-03-10 18:01:43 +00:00
|
|
|
template len*[T](s: DbSeq[T]): int64 =
|
|
|
|
s.recordCount
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2021-03-10 18:01:43 +00:00
|
|
|
proc get*[T](s: DbSeq[T], idx: int64): T =
|
2020-10-13 19:16:54 +00:00
|
|
|
# This is used only locally
|
|
|
|
let resultAddr = addr result
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2021-03-10 18:01:43 +00:00
|
|
|
let queryRes = s.selectStmt.exec(idx + 1) do (recordBytes: openArray[byte]):
|
2020-10-13 19:16:54 +00:00
|
|
|
try:
|
|
|
|
resultAddr[] = decode(SSZ, recordBytes, T)
|
|
|
|
except SerializationError:
|
|
|
|
panic()
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
let found = queryRes.expectDb()
|
2020-10-13 19:16:54 +00:00
|
|
|
if not found: panic()
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
proc loadImmutableValidators(db: BeaconChainDB): seq[ImmutableValidatorData] =
|
|
|
|
# TODO not called, but build fails otherwise
|
|
|
|
for i in 0 ..< db.immutableValidators.len:
|
|
|
|
result.add db.immutableValidators.get(i)
|
|
|
|
|
2021-04-06 18:56:45 +00:00
|
|
|
proc new*(T: type BeaconChainDB,
|
|
|
|
preset: RuntimePreset,
|
|
|
|
dir: string,
|
|
|
|
inMemory = false,
|
|
|
|
): BeaconChainDB =
|
2021-05-17 16:37:26 +00:00
|
|
|
var db = if inMemory:
|
|
|
|
SqStoreRef.init("", "test", inMemory = true).expect(
|
2021-04-06 18:56:45 +00:00
|
|
|
"working database (out of memory?)")
|
2021-03-15 14:11:51 +00:00
|
|
|
else:
|
|
|
|
let s = secureCreatePath(dir)
|
|
|
|
doAssert s.isOk # TODO(zah) Handle this in a better way
|
2020-10-15 11:49:02 +00:00
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
SqStoreRef.init(
|
2021-05-17 16:37:26 +00:00
|
|
|
dir, "nbc", manualCheckpoint = true).expectDb()
|
2020-10-15 11:49:02 +00:00
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
# Remove the deposits table we used before we switched
|
|
|
|
# to storing only deposit contract checkpoints
|
2021-05-17 16:37:26 +00:00
|
|
|
if db.exec("DROP TABLE IF EXISTS deposits;").isErr:
|
2021-03-15 14:11:51 +00:00
|
|
|
debug "Failed to drop the deposits table"
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2021-05-18 10:22:18 +00:00
|
|
|
# An old pubkey->index mapping that hasn't been used on any mainnet release
|
|
|
|
if db.exec("DROP TABLE IF EXISTS validatorIndexFromPubKey;").isErr:
|
|
|
|
debug "Failed to drop the validatorIndexFromPubKey table"
|
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
var
|
2021-05-17 16:37:26 +00:00
|
|
|
# V0 compatibility tables
|
|
|
|
backend = kvStore db.openKvStore().expectDb()
|
2021-05-18 10:22:18 +00:00
|
|
|
# state_no_validators is similar to state_no_validators2 but uses a
|
|
|
|
# different key encoding and was created WITHOUT ROWID
|
2021-05-17 16:37:26 +00:00
|
|
|
stateStore = kvStore db.openKvStore("state_no_validators").expectDb()
|
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
genesisDepositsSeq =
|
2021-05-17 16:37:26 +00:00
|
|
|
DbSeq[DepositData].init(db, "genesis_deposits")
|
2021-03-15 14:11:51 +00:00
|
|
|
immutableValidatorsSeq =
|
2021-05-17 16:37:26 +00:00
|
|
|
DbSeq[ImmutableValidatorData].init(db, "immutable_validators")
|
2021-05-13 16:35:47 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
# V1 - expected-to-be small rows get without rowid optimizations
|
|
|
|
keyValues = kvStore db.openKvStore("key_values", true).expectDb()
|
|
|
|
blocks = kvStore db.openKvStore("blocks").expectDb()
|
|
|
|
stateRoots = kvStore db.openKvStore("state_roots", true).expectDb()
|
|
|
|
statesNoVal = kvStore db.openKvStore("state_no_validators2").expectDb()
|
|
|
|
stateDiffs = kvStore db.openKvStore("state_diffs").expectDb()
|
|
|
|
summaries = kvStore db.openKvStore("beacon_block_summaries", true).expectDb()
|
|
|
|
|
|
|
|
T(
|
|
|
|
db: db,
|
|
|
|
v0: BeaconChainDBV0(
|
|
|
|
backend: backend,
|
|
|
|
stateStore: stateStore,
|
|
|
|
),
|
2021-03-15 14:11:51 +00:00
|
|
|
preset: preset,
|
|
|
|
genesisDeposits: genesisDepositsSeq,
|
|
|
|
immutableValidators: immutableValidatorsSeq,
|
|
|
|
immutableValidatorsMem: loadImmutableValidators(immutableValidatorsSeq),
|
2021-05-17 16:37:26 +00:00
|
|
|
checkpoint: proc() = db.checkpoint(),
|
|
|
|
keyValues: keyValues,
|
|
|
|
blocks: blocks,
|
|
|
|
stateRoots: stateRoots,
|
|
|
|
statesNoVal: statesNoVal,
|
|
|
|
stateDiffs: stateDiffs,
|
|
|
|
summaries: summaries,
|
|
|
|
)
|
2021-05-13 16:35:47 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc decodeSSZ[T](data: openArray[byte], output: var T): bool =
|
2021-05-13 16:35:47 +00:00
|
|
|
try:
|
2021-05-17 16:37:26 +00:00
|
|
|
readSszBytes(data, output, updateRoot = false)
|
|
|
|
true
|
|
|
|
except SerializationError as e:
|
|
|
|
# If the data can't be deserialized, it could be because it's from a
|
|
|
|
# version of the software that uses a different SSZ encoding
|
|
|
|
warn "Unable to deserialize data, old database?",
|
|
|
|
err = e.msg, typ = name(T), dataLen = data.len
|
|
|
|
false
|
|
|
|
|
|
|
|
proc decodeSnappySSZ[T](data: openArray[byte], output: var T): bool =
|
|
|
|
try:
|
|
|
|
let decompressed = snappy.decode(data, maxDecompressedDbRecordSize)
|
|
|
|
readSszBytes(decompressed, output, updateRoot = false)
|
|
|
|
true
|
|
|
|
except SerializationError as e:
|
|
|
|
# If the data can't be deserialized, it could be because it's from a
|
|
|
|
# version of the software that uses a different SSZ encoding
|
|
|
|
warn "Unable to deserialize data, old database?",
|
|
|
|
err = e.msg, typ = name(T), dataLen = data.len
|
|
|
|
false
|
2020-06-13 18:57:07 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc encodeSSZ(v: auto): seq[byte] =
|
2021-03-26 06:52:01 +00:00
|
|
|
try:
|
|
|
|
SSZ.encode(v)
|
|
|
|
except IOError as err:
|
2021-05-13 16:35:47 +00:00
|
|
|
raiseAssert err.msg
|
2020-06-13 18:57:07 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc encodeSnappySSZ(v: auto): seq[byte] =
|
|
|
|
try:
|
|
|
|
snappy.encode(SSZ.encode(v))
|
|
|
|
except CatchableError as err:
|
|
|
|
# In-memory encode shouldn't fail!
|
|
|
|
raiseAssert err.msg
|
2021-05-17 06:34:44 +00:00
|
|
|
|
2021-04-06 18:56:45 +00:00
|
|
|
proc getRaw(db: KvStoreRef, key: openArray[byte], T: type Eth2Digest): Opt[T] =
|
2020-06-13 18:57:07 +00:00
|
|
|
var res: Opt[T]
|
|
|
|
proc decode(data: openArray[byte]) =
|
2021-05-17 16:37:26 +00:00
|
|
|
if data.len == sizeof(Eth2Digest):
|
|
|
|
res.ok Eth2Digest(data: toArray(sizeof(Eth2Digest), data))
|
2020-06-13 18:57:07 +00:00
|
|
|
else:
|
|
|
|
# If the data can't be deserialized, it could be because it's from a
|
|
|
|
# version of the software that uses a different SSZ encoding
|
|
|
|
warn "Unable to deserialize data, old database?",
|
|
|
|
typ = name(T), dataLen = data.len
|
|
|
|
discard
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
discard db.get(key, decode).expectDb()
|
2020-06-13 18:57:07 +00:00
|
|
|
|
|
|
|
res
|
2020-04-23 06:27:35 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc putRaw(db: KvStoreRef, key: openArray[byte], v: Eth2Digest) =
|
|
|
|
db.put(key, v.data).expectDb()
|
|
|
|
|
2020-08-13 09:50:05 +00:00
|
|
|
type GetResult = enum
|
2020-11-24 21:21:47 +00:00
|
|
|
found = "Found"
|
|
|
|
notFound = "Not found"
|
|
|
|
corrupted = "Corrupted"
|
2020-08-13 09:50:05 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc getSSZ[T](db: KvStoreRef, key: openArray[byte], output: var T): GetResult =
|
2020-08-13 09:50:05 +00:00
|
|
|
var status = GetResult.notFound
|
2020-06-25 10:23:10 +00:00
|
|
|
|
|
|
|
# TODO address is needed because there's no way to express lifetimes in nim
|
|
|
|
# we'll use unsafeAddr to find the code later
|
2020-08-13 09:50:05 +00:00
|
|
|
var outputPtr = unsafeAddr output # callback is local, ptr wont escape
|
2020-04-23 06:27:35 +00:00
|
|
|
proc decode(data: openArray[byte]) =
|
2021-05-17 16:37:26 +00:00
|
|
|
status =
|
|
|
|
if decodeSSZ(data, outputPtr[]): GetResult.found
|
|
|
|
else: GetResult.corrupted
|
2021-05-13 16:35:47 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
discard db.get(key, decode).expectDb()
|
2021-05-13 16:35:47 +00:00
|
|
|
|
|
|
|
status
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc putSSZ(db: KvStoreRef, key: openArray[byte], v: auto) =
|
|
|
|
db.put(key, encodeSSZ(v)).expectDb()
|
|
|
|
|
|
|
|
proc getSnappySSZ[T](db: KvStoreRef, key: openArray[byte], output: var T): GetResult =
|
|
|
|
var status = GetResult.notFound
|
|
|
|
|
|
|
|
# TODO address is needed because there's no way to express lifetimes in nim
|
|
|
|
# we'll use unsafeAddr to find the code later
|
|
|
|
var outputPtr = unsafeAddr output # callback is local, ptr wont escape
|
|
|
|
proc decode(data: openArray[byte]) =
|
|
|
|
status =
|
|
|
|
if decodeSnappySSZ(data, outputPtr[]): GetResult.found
|
|
|
|
else: GetResult.corrupted
|
|
|
|
|
|
|
|
discard db.get(key, decode).expectDb()
|
|
|
|
|
|
|
|
status
|
|
|
|
|
|
|
|
proc putSnappySSZ(db: KvStoreRef, key: openArray[byte], v: auto) =
|
|
|
|
db.put(key, encodeSnappySSZ(v)).expectDb()
|
|
|
|
|
|
|
|
proc close*(db: BeaconChainDBV0) =
|
|
|
|
discard db.stateStore.close()
|
2020-09-12 05:35:58 +00:00
|
|
|
discard db.backend.close()
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc close*(db: BeaconchainDB) =
|
|
|
|
if db.db == nil: return
|
|
|
|
|
|
|
|
# Close things in reverse order
|
|
|
|
discard db.summaries.close()
|
|
|
|
discard db.stateDiffs.close()
|
|
|
|
discard db.statesNoVal.close()
|
|
|
|
discard db.stateRoots.close()
|
|
|
|
discard db.blocks.close()
|
|
|
|
discard db.keyValues.close()
|
|
|
|
db.immutableValidators.close()
|
|
|
|
db.genesisDeposits.close()
|
|
|
|
db.v0.close()
|
|
|
|
db.db.close()
|
|
|
|
|
|
|
|
db.db = nil
|
|
|
|
|
2020-11-03 22:30:43 +00:00
|
|
|
func toBeaconBlockSummary(v: SomeBeaconBlock): BeaconBlockSummary =
|
|
|
|
BeaconBlockSummary(
|
|
|
|
slot: v.slot,
|
|
|
|
parent_root: v.parent_root,
|
|
|
|
)
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc putBeaconBlockSummary(
|
|
|
|
db: BeaconChainDB, root: Eth2Digest, value: BeaconBlockSummary) =
|
|
|
|
# Summaries are too simple / small to compress, store them as plain SSZ
|
|
|
|
db.summaries.putSSZ(root.data, value)
|
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
proc putBlock*(db: BeaconChainDB, value: TrustedSignedBeaconBlock) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.blocks.putSnappySSZ(value.root.data, value)
|
|
|
|
db.putBeaconBlockSummary(value.root, value.message.toBeaconBlockSummary())
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
proc updateImmutableValidators(
|
|
|
|
db: BeaconChainDB, immutableValidators: var seq[ImmutableValidatorData],
|
|
|
|
validators: auto) =
|
|
|
|
let
|
|
|
|
numValidators = validators.lenu64
|
|
|
|
origNumImmutableValidators = immutableValidators.lenu64
|
|
|
|
|
|
|
|
doAssert immutableValidators.len == db.immutableValidators.len
|
|
|
|
|
|
|
|
if numValidators <= origNumImmutableValidators:
|
|
|
|
return
|
|
|
|
|
|
|
|
for validatorIndex in origNumImmutableValidators ..< numValidators:
|
|
|
|
# This precedes state storage
|
|
|
|
let immutableValidator =
|
|
|
|
getImmutableValidatorData(validators[validatorIndex])
|
|
|
|
db.immutableValidators.add immutableValidator
|
|
|
|
immutableValidators.add immutableValidator
|
|
|
|
|
|
|
|
proc putState*(db: BeaconChainDB, key: Eth2Digest, value: var BeaconState) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.updateImmutableValidators(db.immutableValidatorsMem, value.validators)
|
|
|
|
db.statesNoVal.putSnappySSZ(
|
|
|
|
key.data,
|
2021-03-19 12:30:20 +00:00
|
|
|
isomorphicCast[BeaconStateNoImmutableValidators](value))
|
2019-03-13 22:59:20 +00:00
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
proc putState*(db: BeaconChainDB, value: var BeaconState) =
|
2019-03-25 16:46:31 +00:00
|
|
|
db.putState(hash_tree_root(value), value)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
func stateRootKey(root: Eth2Digest, slot: Slot): array[40, byte] =
|
|
|
|
var ret: array[40, byte]
|
|
|
|
# big endian to get a naturally ascending order on slots in sorted indices
|
|
|
|
ret[0..<8] = toBytesBE(slot.uint64)
|
|
|
|
ret[8..<40] = root.data
|
2021-03-15 14:11:51 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
ret
|
2021-03-15 14:11:51 +00:00
|
|
|
|
2019-03-28 06:10:48 +00:00
|
|
|
proc putStateRoot*(db: BeaconChainDB, root: Eth2Digest, slot: Slot,
|
|
|
|
value: Eth2Digest) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.stateRoots.putRaw(stateRootKey(root, slot), value)
|
2019-03-28 06:10:48 +00:00
|
|
|
|
2021-01-18 20:34:41 +00:00
|
|
|
proc putStateDiff*(db: BeaconChainDB, root: Eth2Digest, value: BeaconStateDiff) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.stateDiffs.putSnappySSZ(root.data, value)
|
2021-01-18 20:34:41 +00:00
|
|
|
|
2019-11-22 14:14:13 +00:00
|
|
|
proc delBlock*(db: BeaconChainDB, key: Eth2Digest) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.blocks.del(key.data).expectDb()
|
|
|
|
db.summaries.del(key.data).expectDb()
|
2019-11-22 14:14:13 +00:00
|
|
|
|
|
|
|
proc delState*(db: BeaconChainDB, key: Eth2Digest) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.statesNoVal.del(key.data).expectDb()
|
2019-11-22 14:14:13 +00:00
|
|
|
|
2020-01-22 12:59:54 +00:00
|
|
|
proc delStateRoot*(db: BeaconChainDB, root: Eth2Digest, slot: Slot) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.stateRoots.del(stateRootKey(root, slot)).expectDb()
|
2020-01-22 12:59:54 +00:00
|
|
|
|
2021-01-18 20:34:41 +00:00
|
|
|
proc delStateDiff*(db: BeaconChainDB, root: Eth2Digest) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.stateDiffs.del(root.data).expectDb()
|
2021-01-18 20:34:41 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
proc putHeadBlock*(db: BeaconChainDB, key: Eth2Digest) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.keyValues.putRaw(subkey(kHeadBlock), key)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
proc putTailBlock*(db: BeaconChainDB, key: Eth2Digest) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.keyValues.putRaw(subkey(kTailBlock), key)
|
2019-01-14 12:19:44 +00:00
|
|
|
|
2020-09-22 20:42:42 +00:00
|
|
|
proc putGenesisBlockRoot*(db: BeaconChainDB, key: Eth2Digest) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.keyValues.putRaw(subkey(kGenesisBlockRoot), key)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
proc putEth2FinalizedTo*(db: BeaconChainDB,
|
|
|
|
eth1Checkpoint: DepositContractSnapshot) =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.keyValues.putSnappySSZ(subkey(kDepositsFinalizedByEth2), eth1Checkpoint)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc getBlock(db: BeaconChainDBV0, key: Eth2Digest): Opt[TrustedSignedBeaconBlock] =
|
2020-06-25 10:23:10 +00:00
|
|
|
# We only store blocks that we trust in the database
|
2020-09-04 06:35:10 +00:00
|
|
|
result.ok(TrustedSignedBeaconBlock())
|
2021-05-17 16:37:26 +00:00
|
|
|
if db.backend.getSnappySSZ(subkey(SignedBeaconBlock, key), result.get) != GetResult.found:
|
2020-06-25 10:23:10 +00:00
|
|
|
result.err()
|
2020-09-04 06:35:10 +00:00
|
|
|
else:
|
|
|
|
# set root after deserializing (so it doesn't get zeroed)
|
|
|
|
result.get().root = key
|
2019-02-21 04:42:17 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc getBlock*(db: BeaconChainDB, key: Eth2Digest): Opt[TrustedSignedBeaconBlock] =
|
2020-11-03 22:30:43 +00:00
|
|
|
# We only store blocks that we trust in the database
|
2021-05-17 16:37:26 +00:00
|
|
|
result.ok(TrustedSignedBeaconBlock())
|
|
|
|
if db.blocks.getSnappySSZ(key.data, result.get) != GetResult.found:
|
|
|
|
result = db.v0.getBlock(key)
|
|
|
|
else:
|
|
|
|
# set root after deserializing (so it doesn't get zeroed)
|
|
|
|
result.get().root = key
|
2020-11-03 22:30:43 +00:00
|
|
|
|
2021-03-15 14:11:51 +00:00
|
|
|
proc getStateOnlyMutableValidators(
|
2021-05-17 16:37:26 +00:00
|
|
|
immutableValidatorsMem: openArray[ImmutableValidatorData],
|
|
|
|
store: KvStoreRef, key: openArray[byte], output: var BeaconState,
|
2021-03-15 14:11:51 +00:00
|
|
|
rollback: RollbackProc): bool =
|
|
|
|
## Load state into `output` - BeaconState is large so we want to avoid
|
|
|
|
## re-allocating it if possible
|
|
|
|
## Return `true` iff the entry was found in the database and `output` was
|
|
|
|
## overwritten.
|
|
|
|
## Rollback will be called only if output was partially written - if it was
|
|
|
|
## not found at all, rollback will not be called
|
|
|
|
# TODO rollback is needed to deal with bug - use `noRollback` to ignore:
|
|
|
|
# https://github.com/nim-lang/Nim/issues/14126
|
|
|
|
# TODO RVO is inefficient for large objects:
|
|
|
|
# https://github.com/nim-lang/Nim/issues/13879
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
case store.getSnappySSZ(
|
|
|
|
key, isomorphicCast[BeaconStateNoImmutableValidators](output))
|
2021-03-15 14:11:51 +00:00
|
|
|
of GetResult.found:
|
|
|
|
let numValidators = output.validators.len
|
2021-05-17 16:37:26 +00:00
|
|
|
doAssert immutableValidatorsMem.len >= numValidators
|
2021-03-15 14:11:51 +00:00
|
|
|
|
|
|
|
for i in 0 ..< numValidators:
|
|
|
|
let
|
|
|
|
# Bypass hash cache invalidation
|
|
|
|
dstValidator = addr output.validators.data[i]
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
assign(dstValidator.pubkey, immutableValidatorsMem[i].pubkey)
|
2021-03-15 14:11:51 +00:00
|
|
|
assign(dstValidator.withdrawal_credentials,
|
2021-05-17 16:37:26 +00:00
|
|
|
immutableValidatorsMem[i].withdrawal_credentials)
|
2021-03-15 14:11:51 +00:00
|
|
|
|
2021-04-08 10:11:04 +00:00
|
|
|
output.validators.resetCache()
|
2021-03-15 14:11:51 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
of GetResult.notFound:
|
|
|
|
false
|
|
|
|
of GetResult.corrupted:
|
|
|
|
rollback(output)
|
|
|
|
false
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc getState(
|
|
|
|
db: BeaconChainDBV0,
|
|
|
|
immutableValidatorsMem: openArray[ImmutableValidatorData],
|
|
|
|
key: Eth2Digest, output: var BeaconState,
|
|
|
|
rollback: RollbackProc): bool =
|
|
|
|
# Nimbus 1.0 reads and writes writes genesis BeaconState to `backend`
|
|
|
|
# Nimbus 1.1 writes a genesis BeaconStateNoImmutableValidators to `backend` and
|
|
|
|
# reads both BeaconState and BeaconStateNoImmutableValidators from `backend`
|
|
|
|
# Nimbus 1.2 writes a genesis BeaconStateNoImmutableValidators to `stateStore`
|
|
|
|
# and reads BeaconState from `backend` and BeaconStateNoImmutableValidators
|
|
|
|
# from `stateStore`. We will try to read the state from all these locations.
|
|
|
|
if getStateOnlyMutableValidators(
|
|
|
|
immutableValidatorsMem, db.stateStore,
|
|
|
|
subkey(BeaconStateNoImmutableValidators, key), output, rollback):
|
|
|
|
return true
|
|
|
|
if getStateOnlyMutableValidators(
|
|
|
|
immutableValidatorsMem, db.backend,
|
|
|
|
subkey(BeaconStateNoImmutableValidators, key), output, rollback):
|
|
|
|
return true
|
|
|
|
|
|
|
|
case db.backend.getSnappySSZ(subkey(BeaconState, key), output)
|
|
|
|
of GetResult.found:
|
|
|
|
true
|
|
|
|
of GetResult.notFound:
|
|
|
|
false
|
|
|
|
of GetResult.corrupted:
|
|
|
|
rollback(output)
|
|
|
|
false
|
|
|
|
|
2020-04-28 08:08:32 +00:00
|
|
|
proc getState*(
|
|
|
|
db: BeaconChainDB, key: Eth2Digest, output: var BeaconState,
|
|
|
|
rollback: RollbackProc): bool =
|
|
|
|
## Load state into `output` - BeaconState is large so we want to avoid
|
|
|
|
## re-allocating it if possible
|
|
|
|
## Return `true` iff the entry was found in the database and `output` was
|
|
|
|
## overwritten.
|
2020-08-13 09:50:05 +00:00
|
|
|
## Rollback will be called only if output was partially written - if it was
|
|
|
|
## not found at all, rollback will not be called
|
2020-04-28 08:08:32 +00:00
|
|
|
# TODO rollback is needed to deal with bug - use `noRollback` to ignore:
|
|
|
|
# https://github.com/nim-lang/Nim/issues/14126
|
|
|
|
# TODO RVO is inefficient for large objects:
|
|
|
|
# https://github.com/nim-lang/Nim/issues/13879
|
2021-05-17 16:37:26 +00:00
|
|
|
if not getStateOnlyMutableValidators(
|
|
|
|
db.immutableValidatorsMem, db.statesNoVal, key.data, output, rollback):
|
|
|
|
db.v0.getState(db.immutableValidatorsMem, key, output, rollback)
|
|
|
|
else:
|
2021-05-17 06:34:44 +00:00
|
|
|
true
|
2019-03-28 06:10:48 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc getStateRoot(db: BeaconChainDBV0,
|
2021-05-13 16:35:47 +00:00
|
|
|
root: Eth2Digest,
|
|
|
|
slot: Slot): Opt[Eth2Digest] =
|
2021-05-17 06:34:44 +00:00
|
|
|
db.backend.getRaw(subkey(root, slot), Eth2Digest)
|
2021-05-13 16:35:47 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc getStateRoot*(db: BeaconChainDB,
|
|
|
|
root: Eth2Digest,
|
|
|
|
slot: Slot): Opt[Eth2Digest] =
|
|
|
|
db.stateRoots.getRaw(stateRootKey(root, slot), Eth2Digest) or
|
|
|
|
db.v0.getStateRoot(root, slot)
|
|
|
|
|
2021-01-18 20:34:41 +00:00
|
|
|
proc getStateDiff*(db: BeaconChainDB,
|
|
|
|
root: Eth2Digest): Opt[BeaconStateDiff] =
|
|
|
|
result.ok(BeaconStateDiff())
|
2021-05-17 16:37:26 +00:00
|
|
|
if db.stateDiffs.getSnappySSZ(root.data, result.get) != GetResult.found:
|
2021-01-18 20:34:41 +00:00
|
|
|
result.err
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc getHeadBlock(db: BeaconChainDBV0): Opt[Eth2Digest] =
|
2021-05-17 06:34:44 +00:00
|
|
|
db.backend.getRaw(subkey(kHeadBlock), Eth2Digest)
|
2019-02-21 21:38:26 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc getHeadBlock*(db: BeaconChainDB): Opt[Eth2Digest] =
|
|
|
|
db.keyValues.getRaw(subkey(kHeadBlock), Eth2Digest) or
|
|
|
|
db.v0.getHeadBlock()
|
|
|
|
|
|
|
|
proc getTailBlock(db: BeaconChainDBV0): Opt[Eth2Digest] =
|
2021-05-17 06:34:44 +00:00
|
|
|
db.backend.getRaw(subkey(kTailBlock), Eth2Digest)
|
2021-05-13 16:35:47 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc getTailBlock*(db: BeaconChainDB): Opt[Eth2Digest] =
|
|
|
|
db.keyValues.getRaw(subkey(kTailBlock), Eth2Digest) or
|
|
|
|
db.v0.getTailBlock()
|
|
|
|
|
|
|
|
proc getGenesisBlockRoot(db: BeaconChainDBV0): Eth2Digest =
|
|
|
|
db.backend.getRaw(subkey(kGenesisBlockRoot), Eth2Digest).expectDb()
|
|
|
|
|
2020-09-22 20:42:42 +00:00
|
|
|
proc getGenesisBlockRoot*(db: BeaconChainDB): Eth2Digest =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.keyValues.getRaw(subkey(kGenesisBlockRoot), Eth2Digest).expect(
|
2020-10-12 01:07:20 +00:00
|
|
|
"The database must be seeded with the genesis state")
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc getEth2FinalizedTo(db: BeaconChainDBV0): Opt[DepositContractSnapshot] =
|
2020-11-24 21:21:47 +00:00
|
|
|
result.ok(DepositContractSnapshot())
|
2021-05-17 16:37:26 +00:00
|
|
|
let r = db.backend.getSnappySSZ(subkey(kDepositsFinalizedByEth2), result.get)
|
2020-11-24 21:21:47 +00:00
|
|
|
if r != found: result.err()
|
|
|
|
|
|
|
|
proc getEth2FinalizedTo*(db: BeaconChainDB): Opt[DepositContractSnapshot] =
|
|
|
|
result.ok(DepositContractSnapshot())
|
2021-05-17 16:37:26 +00:00
|
|
|
let r = db.keyValues.getSnappySSZ(subkey(kDepositsFinalizedByEth2), result.get)
|
|
|
|
if r != found: return db.v0.getEth2FinalizedTo()
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc containsBlock*(db: BeaconChainDBV0, key: Eth2Digest): bool =
|
|
|
|
db.backend.contains(subkey(SignedBeaconBlock, key)).expectDb()
|
2020-09-22 20:42:42 +00:00
|
|
|
|
2020-04-23 18:58:54 +00:00
|
|
|
proc containsBlock*(db: BeaconChainDB, key: Eth2Digest): bool =
|
2021-05-17 16:37:26 +00:00
|
|
|
db.blocks.contains(key.data).expectDb() or db.v0.containsBlock(key)
|
2021-05-17 06:34:44 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc containsState*(db: BeaconChainDBV0, key: Eth2Digest): bool =
|
|
|
|
let sk = subkey(BeaconStateNoImmutableValidators, key)
|
|
|
|
db.stateStore.contains(sk).expectDb() or
|
|
|
|
db.backend.contains(sk).expectDb() or
|
|
|
|
db.backend.contains(subkey(BeaconState, key)).expectDb
|
2021-05-17 06:34:44 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc containsState*(db: BeaconChainDB, key: Eth2Digest): bool =
|
|
|
|
db.statesNoVal.contains(key.data).expectDb or db.v0.containsState(key)
|
2021-04-20 12:17:11 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
iterator getAncestors*(db: BeaconChainDB, root: Eth2Digest):
|
2020-07-16 13:16:51 +00:00
|
|
|
TrustedSignedBeaconBlock =
|
2019-02-21 17:20:50 +00:00
|
|
|
## Load a chain of ancestors for blck - returns a list of blocks with the
|
|
|
|
## oldest block last (blck will be at result[0]).
|
|
|
|
##
|
2019-02-28 21:21:29 +00:00
|
|
|
## The search will go on until the ancestor cannot be found.
|
2019-02-21 17:20:50 +00:00
|
|
|
|
2020-09-04 06:35:10 +00:00
|
|
|
var
|
|
|
|
res: TrustedSignedBeaconBlock
|
|
|
|
root = root
|
2021-05-17 16:37:26 +00:00
|
|
|
while db.blocks.getSnappySSZ(root.data, res) == GetResult.found or
|
|
|
|
db.v0.backend.getSnappySSZ(
|
|
|
|
subkey(SignedBeaconBlock, root), res) == GetResult.found:
|
2020-09-04 06:35:10 +00:00
|
|
|
res.root = root
|
2020-06-25 10:23:10 +00:00
|
|
|
yield res
|
2020-09-04 06:35:10 +00:00
|
|
|
root = res.message.parent_root
|
2020-11-03 22:30:43 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
proc loadSummaries(db: BeaconChainDB): Table[Eth2Digest, BeaconBlockSummary] =
|
|
|
|
# Load summaries into table - there's no telling what order they're in so we
|
|
|
|
# load them all - bugs in nim prevent this code from living in the iterator.
|
|
|
|
var summaries = initTable[Eth2Digest, BeaconBlockSummary](1024*1024)
|
|
|
|
|
|
|
|
discard db.summaries.find([], proc(k, v: openArray[byte]) =
|
|
|
|
var output: BeaconBlockSummary
|
|
|
|
|
|
|
|
if k.len() == sizeof(Eth2Digest) and decodeSSz(v, output):
|
|
|
|
summaries[Eth2Digest(data: toArray(sizeof(Eth2Digest), k))] = output
|
|
|
|
else:
|
|
|
|
warn "Invalid summary in database", klen = k.len(), vlen = v.len()
|
|
|
|
)
|
|
|
|
|
|
|
|
summaries
|
|
|
|
|
2020-11-03 22:30:43 +00:00
|
|
|
iterator getAncestorSummaries*(db: BeaconChainDB, root: Eth2Digest):
|
|
|
|
tuple[root: Eth2Digest, summary: BeaconBlockSummary] =
|
|
|
|
## Load a chain of ancestors for blck - returns a list of blocks with the
|
|
|
|
## oldest block last (blck will be at result[0]).
|
|
|
|
##
|
|
|
|
## The search will go on until the ancestor cannot be found.
|
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
# Summaries are loaded from the dedicated summaries table. For backwards
|
|
|
|
# compatibility, we also load from `kvstore` and finally, if no summaries
|
|
|
|
# can be found, by loading the blocks instead.
|
|
|
|
|
|
|
|
# First, load the full summary table into memory in one query - this makes
|
|
|
|
# initial startup very fast.
|
2020-11-03 22:30:43 +00:00
|
|
|
var
|
2021-05-17 16:37:26 +00:00
|
|
|
summaries = db.loadSummaries()
|
2020-11-03 22:30:43 +00:00
|
|
|
res: tuple[root: Eth2Digest, summary: BeaconBlockSummary]
|
2021-05-17 16:37:26 +00:00
|
|
|
blck: TrustedSignedBeaconBlock
|
|
|
|
foundOldSummary = false
|
2021-05-13 16:35:47 +00:00
|
|
|
|
2021-05-17 16:37:26 +00:00
|
|
|
res.root = root
|
|
|
|
|
|
|
|
# Yield summaries in reverse chain order by walking the parent references.
|
|
|
|
# If a summary is missing, try loading it from the older version or create one
|
|
|
|
# from block data.
|
2020-11-03 22:30:43 +00:00
|
|
|
while true:
|
2021-05-17 16:37:26 +00:00
|
|
|
summaries.withValue(res.root, summary) do:
|
|
|
|
res.summary = summary[]
|
2021-05-17 06:34:44 +00:00
|
|
|
yield res
|
2021-05-17 16:37:26 +00:00
|
|
|
do: # Summary was not found in summary table, look elsewhere
|
|
|
|
if db.v0.backend.getSnappySSZ(subkey(BeaconBlockSummary, res.root), res.summary) == GetResult.found:
|
|
|
|
yield res
|
|
|
|
elif db.v0.backend.getSnappySSZ(subkey(SignedBeaconBlock, res.root), blck) == GetResult.found:
|
|
|
|
res.summary = blck.message.toBeaconBlockSummary()
|
|
|
|
yield res
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
# Next time, load them from the right place
|
|
|
|
db.putBeaconBlockSummary(res.root, res.summary)
|
|
|
|
|
|
|
|
res.root = res.summary.parent_root
|
|
|
|
|
|
|
|
if false:
|
|
|
|
# When the current version has been online for a bit, we can safely remove
|
|
|
|
# summaries from kvstore by enabling this little snippet - if users were
|
|
|
|
# to downgrade after the summaries have been purged, the old versions that
|
|
|
|
# use summaries can also recreate them on the fly from blocks.
|
|
|
|
db.db.exec(
|
|
|
|
"DELETE FROM kvstore WHERE key >= ? and key < ?",
|
|
|
|
([byte ord(kHashToBlockSummary)], [byte ord(kHashToBlockSummary) + 1])).expectDb()
|