diff --git a/hive_integration/nodocker/consensus/consensus_sim.nim b/hive_integration/nodocker/consensus/consensus_sim.nim index 83e0c72df4..979581dba9 100644 --- a/hive_integration/nodocker/consensus/consensus_sim.nim +++ b/hive_integration/nodocker/consensus/consensus_sim.nim @@ -26,8 +26,6 @@ proc processChainData(cd: ChainData): TestStatus = cd.params ) - com.initializeEmptyDb() - for bytes in cd.blocksRlp: # ignore return value here # because good blocks maybe interleaved with diff --git a/hive_integration/nodocker/engine/engine_env.nim b/hive_integration/nodocker/engine/engine_env.nim index b8eed02769..fb8b58bef8 100644 --- a/hive_integration/nodocker/engine/engine_env.nim +++ b/hive_integration/nodocker/engine/engine_env.nim @@ -90,7 +90,6 @@ proc newEngineEnv*(conf: var NimbusConf, chainFile: string, enableAuth: bool): E com = makeCom(conf) chain = newChain(com) - com.initializeEmptyDb() let txPool = TxPoolRef.new(com) node.addEthHandlerCapability( diff --git a/hive_integration/nodocker/graphql/graphql_sim.nim b/hive_integration/nodocker/graphql/graphql_sim.nim index ba2c919d24..969221c2d9 100644 --- a/hive_integration/nodocker/graphql/graphql_sim.nim +++ b/hive_integration/nodocker/graphql/graphql_sim.nim @@ -83,7 +83,6 @@ proc main() = conf.networkParams ) - com.initializeEmptyDb() let txPool = TxPoolRef.new(com) discard importRlpBlock(blocksFile, com) let ctx = setupGraphqlContext(com, ethNode, txPool) diff --git a/hive_integration/nodocker/rpc/test_env.nim b/hive_integration/nodocker/rpc/test_env.nim index 631fcd876c..6e8ab3a933 100644 --- a/hive_integration/nodocker/rpc/test_env.nim +++ b/hive_integration/nodocker/rpc/test_env.nim @@ -81,7 +81,6 @@ proc setupEnv*(): TestEnv = ) manageAccounts(ethCtx, conf) - com.initializeEmptyDb() let chainRef = newChain(com) let txPool = TxPoolRef.new(com) diff --git a/nimbus/common/common.nim b/nimbus/common/common.nim index ba7edf4428..a0452cd780 100644 --- a/nimbus/common/common.nim +++ b/nimbus/common/common.nim @@ -119,6 +119,50 @@ func daoCheck(conf: ChainConfig) = if conf.daoForkSupport and conf.daoForkBlock.isNone: conf.daoForkBlock = conf.homesteadBlock +proc initializeDb(com: CommonRef) = + let kvt = com.db.ctx.getKvt() + proc contains(kvt: CoreDbKvtRef; key: openArray[byte]): bool = + kvt.hasKey(key).expect "valid bool" + if canonicalHeadHashKey().toOpenArray notin kvt: + info "Writing genesis to DB" + doAssert(com.genesisHeader.number == 0.BlockNumber, + "can't commit genesis block with number > 0") + doAssert(com.db.persistHeader(com.genesisHeader, + com.consensusType == ConsensusType.POS, + startOfHistory=com.genesisHeader.parentHash), + "can persist genesis header") + doAssert(canonicalHeadHashKey().toOpenArray in kvt) + + # The database must at least contain the base and head pointers - the base + # is implicitly considered finalized + let + baseNum = com.db.getSavedStateBlockNumber() + base = + try: + com.db.getBlockHeader(baseNum) + except BlockNotFound as exc: + fatal "Cannot load base block header", + baseNum, err = exc.msg + quit 1 + finalized = + try: + com.db.finalizedHeader() + except BlockNotFound as exc: + debug "No finalized block stored in database, reverting to base" + base + head = + try: + com.db.getCanonicalHead() + except EVMError as exc: + fatal "Cannot load canonical block header", + err = exc.msg + quit 1 + + info "Database initialized", + base = (base.blockHash, base.number), + finalized = (finalized.blockHash, finalized.number), + head = (head.blockHash, head.number) + proc init(com : CommonRef, db : CoreDbRef, networkId : NetworkId, @@ -174,6 +218,8 @@ proc init(com : CommonRef, # By default, history begins at genesis. com.startOfHistory = GENESIS_PARENT_HASH + com.initializeDb() + proc getTd(com: CommonRef, blockHash: Hash256): Opt[DifficultyInt] = var td: DifficultyInt if not com.db.getTd(blockHash, td): @@ -345,20 +391,6 @@ proc consensus*(com: CommonRef, header: BlockHeader): ConsensusType = return com.config.consensusType -proc initializeEmptyDb*(com: CommonRef) = - let kvt = com.db.ctx.getKvt() - proc contains(kvt: CoreDbKvtRef; key: openArray[byte]): bool = - kvt.hasKey(key).expect "valid bool" - if canonicalHeadHashKey().toOpenArray notin kvt: - info "Writing genesis to DB" - doAssert(com.genesisHeader.number == 0.BlockNumber, - "can't commit genesis block with number > 0") - doAssert(com.db.persistHeader(com.genesisHeader, - com.consensusType == ConsensusType.POS, - startOfHistory=com.genesisHeader.parentHash), - "can persist genesis header") - doAssert(canonicalHeadHashKey().toOpenArray in kvt) - proc syncReqNewHead*(com: CommonRef; header: BlockHeader) {.gcsafe, raises: [].} = ## Used by RPC to update the beacon head for snap sync diff --git a/nimbus/nimbus.nim b/nimbus/nimbus.nim index 62f205dad2..ac562ae7bf 100644 --- a/nimbus/nimbus.nim +++ b/nimbus/nimbus.nim @@ -243,8 +243,6 @@ proc run(nimbus: NimbusNode, conf: NimbusConf) = defer: com.db.finish() - com.initializeEmptyDb() - case conf.cmd of NimbusCmd.`import`: importBlocks(conf, com) diff --git a/premix/persist.nim b/premix/persist.nim index 9fae27d742..7033882c1d 100644 --- a/premix/persist.nim +++ b/premix/persist.nim @@ -67,11 +67,6 @@ proc main() {.used.} = discard com.db.setHead(parentBlock.header) let kvt = com.db.ctx.getKvt() - if canonicalHeadHashKey().toOpenArray notin kvt: - persistToDb(com.db): - com.initializeEmptyDb() - doAssert(canonicalHeadHashKey().toOpenArray in kvt) - var head = com.db.getCanonicalHead() var blockNumber = head.number + 1 var chain = newChain(com) diff --git a/tests/macro_assembler.nim b/tests/macro_assembler.nim index f370bb9e56..b936aac7d8 100644 --- a/tests/macro_assembler.nim +++ b/tests/macro_assembler.nim @@ -280,7 +280,6 @@ proc initVMEnv*(network: string): BaseVMState = gasLimit: 100_000 ) - com.initializeEmptyDb() BaseVMState.new(parent, header, com) proc verifyAsmResult(vmState: BaseVMState, boa: Assembler, asmResult: CallResult): bool = diff --git a/tests/test_beacon/setup_env.nim b/tests/test_beacon/setup_env.nim index 09982f356f..d8003803b1 100644 --- a/tests/test_beacon/setup_env.nim +++ b/tests/test_beacon/setup_env.nim @@ -69,7 +69,6 @@ proc setupEnv*(extraValidation: bool = false, ccm: CCModify = nil): TestEnv = ) chain = newChain(com, extraValidation) - com.initializeEmptyDb() TestEnv( conf : conf, chain: chain, diff --git a/tests/test_coredb.nim b/tests/test_coredb.nim index acd5f10852..613f2d091d 100644 --- a/tests/test_coredb.nim +++ b/tests/test_coredb.nim @@ -185,8 +185,6 @@ proc initRunnerDB( params = params, pruneHistory = pruneHistory) - result.initializeEmptyDb - setErrorLevel() when CoreDbEnableApiTracking: coreDB.trackCoreDbApi = false diff --git a/tests/test_forked_chain.nim b/tests/test_forked_chain.nim index d0e48961bc..a1a25d395e 100644 --- a/tests/test_forked_chain.nim +++ b/tests/test_forked_chain.nim @@ -34,16 +34,12 @@ proc setupEnv(): TestEnv = TestEnv(conf: conf) proc newCom(env: TestEnv): CommonRef = - let - com = CommonRef.new( + CommonRef.new( newCoreDbRef DefaultDbMemory, env.conf.networkId, env.conf.networkParams ) - com.initializeEmptyDb() - com - proc makeBlk(com: CommonRef, number: BlockNumber, parentBlk: EthBlock): EthBlock = template parent(): BlockHeader = parentBlk.header diff --git a/tests/test_graphql.nim b/tests/test_graphql.nim index f1a847ce57..ef1a026ad1 100644 --- a/tests/test_graphql.nim +++ b/tests/test_graphql.nim @@ -73,7 +73,6 @@ proc setupChain(): CommonRef = CustomNet, customNetwork ) - com.initializeEmptyDb() let blocks = jn["blocks"] var headers: seq[BlockHeader] diff --git a/tests/test_ledger.nim b/tests/test_ledger.nim index 8a5f61c333..5308f88049 100644 --- a/tests/test_ledger.nim +++ b/tests/test_ledger.nim @@ -102,7 +102,6 @@ proc initEnv(): TestEnv = conf.networkId, conf.networkParams ) - com.initializeEmptyDb() TestEnv( com : com, diff --git a/tests/test_merge.nim b/tests/test_merge.nim index 28df17f863..a2a425d3f7 100644 --- a/tests/test_merge.nim +++ b/tests/test_merge.nim @@ -71,8 +71,6 @@ proc runTest(steps: Steps) = ) chainRef = newChain(com) - com.initializeEmptyDb() - var rpcServer = newRpcSocketServer(["127.0.0.1:0"]) client = newRpcSocketClient() diff --git a/tests/test_rpc.nim b/tests/test_rpc.nim index 0c4f0b31de..b721fced03 100644 --- a/tests/test_rpc.nim +++ b/tests/test_rpc.nim @@ -238,7 +238,6 @@ proc rpcMain*() = debugEcho unlock.error doAssert(unlock.isOk) - com.initializeEmptyDb() let env = setupEnv(com, signer, ks2, ctx) # Create Ethereum RPCs diff --git a/tests/test_rpc_getproofs_track_state_changes.nim b/tests/test_rpc_getproofs_track_state_changes.nim index ee32952a23..e6dda6730b 100644 --- a/tests/test_rpc_getproofs_track_state_changes.nim +++ b/tests/test_rpc_getproofs_track_state_changes.nim @@ -121,7 +121,6 @@ proc rpcGetProofsTrackStateChangesMain*() = let com = CommonRef.new(newCoreDbRef( DefaultDbPersistent, DATABASE_PATH, DbOptions.init())) - com.initializeEmptyDb() let blockHeader = waitFor client.eth_getBlockByNumber(blockId(START_BLOCK), false) diff --git a/tests/test_txpool/setup.nim b/tests/test_txpool/setup.nim index 8068a9ad1f..f3ea808202 100644 --- a/tests/test_txpool/setup.nim +++ b/tests/test_txpool/setup.nim @@ -96,7 +96,6 @@ proc setupTxPool*(getStatus: proc(): TxItemStatus): (CommonRef, TxPoolRef, int) conf.networkParams ) - com.initializeEmptyDb() let txPool = TxPoolRef.new(com) for n, tx in txEnv.txs: diff --git a/tests/test_txpool2.nim b/tests/test_txpool2.nim index e001620c7e..6682d1debb 100644 --- a/tests/test_txpool2.nim +++ b/tests/test_txpool2.nim @@ -110,8 +110,6 @@ proc initEnv(envFork: HardFork): TestEnv = ) chain = newChain(com) - com.initializeEmptyDb() - result = TestEnv( conf: conf, com: com,