nimbus-eth1/premix/persist.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

115 lines
3.0 KiB
Nim

# use this module to quickly populate db with data from geth/parity
import
chronicles,
../nimbus/errors,
../nimbus/core/chain,
../nimbus/common,
../nimbus/db/[core_db/persistent, storage_types],
configuration # must be late (compilation annoyance)
when defined(graphql):
import graphql_downloader
else:
import downloader
# `lmdb` is not used, anymore
#
# const
# manualCommit = nimbus_db_backend == "lmdb"
#
# template persistToDb(db: ChainDB, body: untyped) =
# when manualCommit:
# if not db.txBegin(): doAssert(false)
# body
# when manualCommit:
# if not db.txCommit(): doAssert(false)
template persistToDb(db: CoreDbRef, body: untyped) =
block: body
proc main() {.used.} =
# 97 block with uncles
# 46147 block with first transaction
# 46400 block with transaction
# 46402 block with first contract: failed
# 47205 block with first success contract
# 48712 block with 5 transactions
# 48915 block with contract
# 49018 first problematic block
# 49439 first block with contract call
# 52029 first block with receipts logs
# 66407 failed transaction
let conf = configuration.getConfiguration()
let com = CommonRef.new(
newCoreDbRef(LegacyDbPersistent, conf.dataDir),
false, conf.netId, networkParams(conf.netId))
# move head to block number ...
if conf.head != 0.u256:
var parentBlock = requestBlock(conf.head)
discard com.db.setHead(parentBlock.header)
if canonicalHeadHashKey().toOpenArray notin com.db.kvt:
persistToDb(com.db):
com.initializeEmptyDb()
doAssert(canonicalHeadHashKey().toOpenArray in com.db.kvt)
var head = com.db.getCanonicalHead()
var blockNumber = head.blockNumber + 1
var chain = newChain(com)
let numBlocksToCommit = conf.numCommits
var headers = newSeqOfCap[BlockHeader](numBlocksToCommit)
var bodies = newSeqOfCap[BlockBody](numBlocksToCommit)
var one = 1.u256
var numBlocks = 0
var counter = 0
while true:
var thisBlock = requestBlock(blockNumber)
headers.add thisBlock.header
bodies.add thisBlock.body
info "REQUEST HEADER", blockNumber=blockNumber, txs=thisBlock.body.transactions.len
inc numBlocks
blockNumber += one
if numBlocks == numBlocksToCommit:
persistToDb(com.db):
if chain.persistBlocks(headers, bodies) != ValidationResult.OK:
raise newException(ValidationError, "Error when validating blocks")
numBlocks = 0
headers.setLen(0)
bodies.setLen(0)
inc counter
if conf.maxBlocks != 0 and counter >= conf.maxBlocks:
break
if numBlocks > 0:
persistToDb(com.db):
if chain.persistBlocks(headers, bodies) != ValidationResult.OK:
raise newException(ValidationError, "Error when validating blocks")
when isMainModule:
var message: string
## Processing command line arguments
if configuration.processArguments(message) != Success:
echo message
quit(QuitFailure)
else:
if len(message) > 0:
echo message
quit(QuitSuccess)
try:
main()
except CatchableError:
echo getCurrentExceptionMsg()