move db init to init (#2552)

When using the common interface, the database always (potentially) needs
init - take the opportunity to log some basic database info on startup.
This commit is contained in:
Jacek Sieka 2024-08-08 07:45:30 +02:00 committed by GitHub
parent 93a160b569
commit 3cefd7ed38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 47 additions and 44 deletions

View File

@ -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

View File

@ -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(

View File

@ -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)

View File

@ -81,7 +81,6 @@ proc setupEnv*(): TestEnv =
)
manageAccounts(ethCtx, conf)
com.initializeEmptyDb()
let chainRef = newChain(com)
let txPool = TxPoolRef.new(com)

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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 =

View File

@ -69,7 +69,6 @@ proc setupEnv*(extraValidation: bool = false, ccm: CCModify = nil): TestEnv =
)
chain = newChain(com, extraValidation)
com.initializeEmptyDb()
TestEnv(
conf : conf,
chain: chain,

View File

@ -185,8 +185,6 @@ proc initRunnerDB(
params = params,
pruneHistory = pruneHistory)
result.initializeEmptyDb
setErrorLevel()
when CoreDbEnableApiTracking:
coreDB.trackCoreDbApi = false

View File

@ -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

View File

@ -73,7 +73,6 @@ proc setupChain(): CommonRef =
CustomNet,
customNetwork
)
com.initializeEmptyDb()
let blocks = jn["blocks"]
var headers: seq[BlockHeader]

View File

@ -102,7 +102,6 @@ proc initEnv(): TestEnv =
conf.networkId,
conf.networkParams
)
com.initializeEmptyDb()
TestEnv(
com : com,

View File

@ -71,8 +71,6 @@ proc runTest(steps: Steps) =
)
chainRef = newChain(com)
com.initializeEmptyDb()
var
rpcServer = newRpcSocketServer(["127.0.0.1:0"])
client = newRpcSocketClient()

View File

@ -238,7 +238,6 @@ proc rpcMain*() =
debugEcho unlock.error
doAssert(unlock.isOk)
com.initializeEmptyDb()
let env = setupEnv(com, signer, ks2, ctx)
# Create Ethereum RPCs

View File

@ -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)

View File

@ -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:

View File

@ -110,8 +110,6 @@ proc initEnv(envFork: HardFork): TestEnv =
)
chain = newChain(com)
com.initializeEmptyDb()
result = TestEnv(
conf: conf,
com: com,