nimbus-eth1/premix/persist.nim

108 lines
2.8 KiB
Nim
Raw Normal View History

2019-01-06 15:21:34 +00:00
# use this module to quickly populate db with data from geth/parity
import
eth/[common, rlp], stint,
2019-02-05 19:15:50 +00:00
chronicles, downloader, configuration,
2020-07-21 06:15:06 +00:00
../nimbus/errors
2019-01-06 15:21:34 +00:00
import
eth/trie/[hexary, db],
2019-01-06 15:21:34 +00:00
../nimbus/db/[storage_types, db_chain, select_backend],
../nimbus/[genesis],
2019-01-06 15:21:34 +00:00
../nimbus/p2p/chain
const
manualCommit = nimbus_db_backend == "lmdb"
template persistToDb(db: ChainDB, body: untyped) =
when manualCommit:
2019-03-13 21:36:54 +00:00
if not db.txBegin(): doAssert(false)
2019-01-06 15:21:34 +00:00
body
when manualCommit:
2019-03-13 21:36:54 +00:00
if not db.txCommit(): doAssert(false)
2019-01-06 15:21:34 +00:00
2020-07-21 06:15:06 +00:00
proc main() {.used.} =
2019-01-06 15:21:34 +00:00
# 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
2020-06-19 10:52:19 +00:00
let conf = configuration.getConfiguration()
2019-01-06 15:21:34 +00:00
let db = newChainDb(conf.dataDir)
let trieDB = trieDB db
2020-06-19 10:52:19 +00:00
let chainDB = newBaseChainDB(trieDB, false, conf.netId)
2019-01-06 15:21:34 +00:00
# move head to block number ...
2019-01-08 11:29:56 +00:00
if conf.head != 0.u256:
var parentBlock = requestBlock(conf.head)
chainDB.setHead(parentBlock.header)
2019-01-06 15:21:34 +00:00
if canonicalHeadHashKey().toOpenArray notin trieDB:
persistToDb(db):
initializeEmptyDb(chainDB)
2019-03-13 21:36:54 +00:00
doAssert(canonicalHeadHashKey().toOpenArray in trieDB)
2019-01-06 15:21:34 +00:00
var head = chainDB.getCanonicalHead()
var blockNumber = head.blockNumber + 1
var chain = newChain(chainDB)
2019-01-08 11:29:56 +00:00
let numBlocksToCommit = conf.numCommits
2019-01-06 15:21:34 +00:00
var headers = newSeqOfCap[BlockHeader](numBlocksToCommit)
var bodies = newSeqOfCap[BlockBody](numBlocksToCommit)
var one = 1.u256
var numBlocks = 0
2019-01-08 11:29:56 +00:00
var counter = 0
2019-07-07 10:12:01 +00:00
while true:
2019-01-06 15:21:34 +00:00
var thisBlock = requestBlock(blockNumber)
headers.add thisBlock.header
bodies.add thisBlock.body
2019-03-11 13:21:09 +00:00
info "REQUEST HEADER", blockNumber=blockNumber, txs=thisBlock.body.transactions.len
2019-07-07 10:12:01 +00:00
2019-01-06 15:21:34 +00:00
inc numBlocks
blockNumber += one
if numBlocks == numBlocksToCommit:
persistToDb(db):
2019-01-08 11:29:56 +00:00
if chain.persistBlocks(headers, bodies) != ValidationResult.OK:
2019-01-09 12:10:58 +00:00
raise newException(ValidationError, "Error when validating blocks")
2019-01-06 15:21:34 +00:00
numBlocks = 0
headers.setLen(0)
bodies.setLen(0)
2019-01-08 11:29:56 +00:00
inc counter
if conf.maxBlocks != 0 and counter >= conf.maxBlocks:
break
2019-01-06 15:21:34 +00:00
if numBlocks > 0:
persistToDb(db):
2019-01-09 12:10:58 +00:00
if chain.persistBlocks(headers, bodies) != ValidationResult.OK:
raise newException(ValidationError, "Error when validating blocks")
2019-01-06 15:21:34 +00:00
2019-01-08 11:29:56 +00:00
when isMainModule:
var message: string
## Processing command line arguments
2020-06-19 10:52:19 +00:00
if configuration.processArguments(message) != Success:
2019-01-08 11:29:56 +00:00
echo message
quit(QuitFailure)
else:
if len(message) > 0:
echo message
quit(QuitSuccess)
2019-01-09 12:10:58 +00:00
try:
main()
except:
echo getCurrentExceptionMsg()