nimbus-eth1/nimbus/sync/snap/worker/db/snapdb_pivot.nim
Jordan Hrycaj 221e6c9e2f
Unified database frontend integration (#1670)
* Nimbus folder environment update

details:
* Integrated `CoreDbRef` for the sources in the `nimbus` sub-folder.
* The `nimbus` program does not compile yet as it needs the updates
  in the parallel `stateless` sub-folder.

* Stateless environment update

details:
* Integrated `CoreDbRef` for the sources in the `stateless` sub-folder.
* The `nimbus` program compiles now.

* Premix environment update

details:
* Integrated `CoreDbRef` for the sources in the `premix` sub-folder.

* Fluffy environment update

details:
* Integrated `CoreDbRef` for the sources in the `fluffy` sub-folder.

* Tools environment update

details:
* Integrated `CoreDbRef` for the sources in the `tools` sub-folder.

* Nodocker environment update

details:
* Integrated `CoreDbRef` for the sources in the
  `hive_integration/nodocker` sub-folder.

* Tests environment update

details:
* Integrated `CoreDbRef` for the sources in the `tests` sub-folder.
* The unit tests compile and run cleanly now.

* Generalise `CoreDbRef` to any `select_backend` supported database

why:
  Generalisation was just missed due to overcoming some compiler oddity
  which was tied to rocksdb for testing.

* Suppress compiler warning for `newChainDB()`

why:
  Warning was added to this function which must be wrapped so that
  any `CatchableError` is re-raised as `Defect`.

* Split off persistent `CoreDbRef` constructor into separate file

why:
  This allows to compile a memory only database version without linking
  the backend library.

* Use memory `CoreDbRef` database by default

detail:
 Persistent DB constructor needs to import `db/core_db/persistent

why:
 Most tests use memory DB anyway. This avoids linking `-lrocksdb` or
 any other backend by default.

* fix `toLegacyBackend()` availability check

why:
  got garbled after memory/persistent split.

* Clarify raw access to MPT for snap sync handler

why:
  Logically, `kvt` is not the raw access for the hexary trie (although
  this holds for the legacy database)
2023-08-04 12:10:09 +01:00

81 lines
3.0 KiB
Nim

# nimbus-eth1
# Copyright (c) 2021 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed
# except according to those terms.
{.push raises: [].}
import
eth/[common, rlp],
stew/results,
../../range_desc,
"."/[hexary_error, snapdb_desc, snapdb_persistent]
type
SnapDbPivotRegistry* = object
predecessor*: NodeKey ## Predecessor key in chain, auto filled
header*: BlockHeader ## Pivot state, containg state root
nAccounts*: uint64 ## Imported # of accounts
nSlotLists*: uint64 ## Imported # of account storage tries
dangling*: seq[Blob] ## Dangling nodes in accounts trie
processed*: seq[
(NodeTag,NodeTag)] ## Processed acoount ranges
slotAccounts*: seq[NodeKey] ## List of accounts with missing storage slots
ctraAccounts*: seq[NodeKey] ## List of accounts with missing contracts
# ------------------------------------------------------------------------------
# Private helpers
# ------------------------------------------------------------------------------
template handleRlpException(info: static[string]; code: untyped) =
try:
code
except RlpError:
return err(RlpEncoding)
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
proc pivotSaveDB*(
pv: SnapDbRef; ## Base descriptor on `CoreDbRef`
data: SnapDbPivotRegistry; ## Registered data record
): Result[int,HexaryError] =
## Register pivot environment
handleRlpException("pivotSaveDB()"):
let rlpData = rlp.encode(data)
pv.kvDb.persistentStateRootPut(data.header.stateRoot.to(NodeKey), rlpData)
return ok(rlpData.len)
# notreached
proc pivotRecoverDB*(
pv: SnapDbRef; ## Base descriptor on `CoreDbRef`
stateRoot: NodeKey; ## Check for a particular state root
): Result[SnapDbPivotRegistry,HexaryError] =
## Restore pivot environment for a particular state root.
let rc = pv.kvDb.persistentStateRootGet(stateRoot)
if rc.isOk:
handleRlpException("rpivotRecoverDB()"):
var r = rlp.decode(rc.value.data, SnapDbPivotRegistry)
r.predecessor = rc.value.key
return ok(r)
err(StateRootNotFound)
proc pivotRecoverDB*(
pv: SnapDbRef; ## Base descriptor on `CoreDbRef`
): Result[SnapDbPivotRegistry,HexaryError] =
## Restore pivot environment that was saved latest.
let rc = pv.kvDb.persistentStateRootGet(NodeKey.default)
if rc.isOk:
return pv.pivotRecoverDB(rc.value.key)
err(StateRootNotFound)
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------