Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move db init to init #2552

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions hive_integration/nodocker/consensus/consensus_sim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion hive_integration/nodocker/engine/engine_env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion hive_integration/nodocker/graphql/graphql_sim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion hive_integration/nodocker/rpc/test_env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ proc setupEnv*(): TestEnv =
)

manageAccounts(ethCtx, conf)
com.initializeEmptyDb()

let chainRef = newChain(com)
let txPool = TxPoolRef.new(com)
Expand Down
60 changes: 46 additions & 14 deletions nimbus/common/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions nimbus/nimbus.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions premix/persist.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion tests/macro_assembler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
1 change: 0 additions & 1 deletion tests/test_beacon/setup_env.nim
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ proc setupEnv*(extraValidation: bool = false, ccm: CCModify = nil): TestEnv =
)
chain = newChain(com, extraValidation)

com.initializeEmptyDb()
TestEnv(
conf : conf,
chain: chain,
Expand Down
2 changes: 0 additions & 2 deletions tests/test_coredb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ proc initRunnerDB(
params = params,
pruneHistory = pruneHistory)

result.initializeEmptyDb

setErrorLevel()
when CoreDbEnableApiTracking:
coreDB.trackCoreDbApi = false
Expand Down
6 changes: 1 addition & 5 deletions tests/test_forked_chain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/test_graphql.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ proc setupChain(): CommonRef =
CustomNet,
customNetwork
)
com.initializeEmptyDb()

let blocks = jn["blocks"]
var headers: seq[BlockHeader]
Expand Down
1 change: 0 additions & 1 deletion tests/test_ledger.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ proc initEnv(): TestEnv =
conf.networkId,
conf.networkParams
)
com.initializeEmptyDb()

TestEnv(
com : com,
Expand Down
2 changes: 0 additions & 2 deletions tests/test_merge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ proc runTest(steps: Steps) =
)
chainRef = newChain(com)

com.initializeEmptyDb()

var
rpcServer = newRpcSocketServer(["127.0.0.1:0"])
client = newRpcSocketClient()
Expand Down
1 change: 0 additions & 1 deletion tests/test_rpc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ proc rpcMain*() =
debugEcho unlock.error
doAssert(unlock.isOk)

com.initializeEmptyDb()
let env = setupEnv(com, signer, ks2, ctx)

# Create Ethereum RPCs
Expand Down
1 change: 0 additions & 1 deletion tests/test_rpc_getproofs_track_state_changes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion tests/test_txpool/setup.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions tests/test_txpool2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ proc initEnv(envFork: HardFork): TestEnv =
)
chain = newChain(com)

com.initializeEmptyDb()

result = TestEnv(
conf: conf,
com: com,
Expand Down
Loading