diff --git a/hive_integration/nodocker/engine/node.nim b/hive_integration/nodocker/engine/node.nim index 3a94b5480..02ccaed31 100644 --- a/hive_integration/nodocker/engine/node.nim +++ b/hive_integration/nodocker/engine/node.nim @@ -42,7 +42,7 @@ proc processBlock( ## implementations (but can be savely removed, as well.) ## variant of `processBlock()` where the `header` argument is explicitely set. template header: BlockHeader = blk.header - var dbTx = vmState.com.db.newTransaction() + var dbTx = vmState.com.db.ctx.newTransaction() defer: dbTx.dispose() let com = vmState.com @@ -92,7 +92,7 @@ proc getVmState(c: ChainRef, header: BlockHeader): # intended to accepts invalid block proc setBlock*(c: ChainRef; blk: EthBlock): Result[void, string] = template header: BlockHeader = blk.header - let dbTx = c.db.newTransaction() + let dbTx = c.db.ctx.newTransaction() defer: dbTx.dispose() c.com.hardForkTransition(header) diff --git a/nimbus/beacon/merge_tracker.nim b/nimbus/beacon/merge_tracker.nim index 4389b356f..dd18e4ba5 100644 --- a/nimbus/beacon/merge_tracker.nim +++ b/nimbus/beacon/merge_tracker.nim @@ -38,11 +38,11 @@ type # ------------------------------------------------------------------------------ proc writeStatus(db: CoreDbRef, status: TransitionStatus) = - db.newKvt.put(transitionStatusKey().toOpenArray(), rlp.encode(status)).isOkOr: + db.ctx.getKvt.put(transitionStatusKey().toOpenArray(), rlp.encode(status)).isOkOr: raiseAssert "writeStatus(): put() failed " & $$error proc readStatus(db: CoreDbRef): TransitionStatus = - var bytes = db.newKvt.get(transitionStatusKey().toOpenArray()).valueOr: + var bytes = db.ctx.getKvt.get(transitionStatusKey().toOpenArray()).valueOr: EmptyBlob if bytes.len > 0: try: diff --git a/nimbus/common/common.nim b/nimbus/common/common.nim index eeb900a8b..8e5594870 100644 --- a/nimbus/common/common.nim +++ b/nimbus/common/common.nim @@ -355,7 +355,7 @@ proc consensus*(com: CommonRef, header: BlockHeader): ConsensusType = return com.config.consensusType proc initializeEmptyDb*(com: CommonRef) = - let kvt = com.db.newKvt() + 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: diff --git a/nimbus/core/chain/forked_chain.nim b/nimbus/core/chain/forked_chain.nim index 5ab74b4e6..5b922847f 100644 --- a/nimbus/core/chain/forked_chain.nim +++ b/nimbus/core/chain/forked_chain.nim @@ -145,7 +145,7 @@ proc validateBlock(c: ForkedChainRef, parent: BlockHeader, blk: EthBlock, updateCursor: bool = true): Result[void, string] = - let dbTx = c.db.newTransaction() + let dbTx = c.db.ctx.newTransaction() defer: dbTx.dispose() @@ -172,7 +172,7 @@ proc replaySegment(c: ForkedChainRef, target: Hash256) = prevHash = chain[^1].header.parentHash c.stagingTx.rollback() - c.stagingTx = c.db.newTransaction() + c.stagingTx = c.db.ctx.newTransaction() c.cursorHeader = c.baseHeader for i in countdown(chain.high, chain.low): c.validateBlock(c.cursorHeader, chain[i], @@ -397,7 +397,7 @@ proc importBlock*(c: ForkedChainRef, blk: EthBlock): Result[void, string] = # Try to import block to canonical or side chain. # return error if the block is invalid if c.stagingTx.isNil: - c.stagingTx = c.db.newTransaction() + c.stagingTx = c.db.ctx.newTransaction() template header(): BlockHeader = blk.header @@ -407,7 +407,7 @@ proc importBlock*(c: ForkedChainRef, blk: EthBlock): Result[void, string] = if header.parentHash == c.baseHash: c.stagingTx.rollback() - c.stagingTx = c.db.newTransaction() + c.stagingTx = c.db.ctx.newTransaction() return c.validateBlock(c.baseHeader, blk) if header.parentHash notin c.blocks: @@ -442,7 +442,7 @@ proc forkChoice*(c: ForkedChainRef, if c.cursorHash != head.cursorHash: if not c.stagingTx.isNil: c.stagingTx.rollback() - c.stagingTx = c.db.newTransaction() + c.stagingTx = c.db.ctx.newTransaction() c.replaySegment(headHash) c.trimCanonicalChain(head, headHash) @@ -452,7 +452,7 @@ proc forkChoice*(c: ForkedChainRef, if c.stagingTx.isNil: # setHead below don't go straight to db - c.stagingTx = c.db.newTransaction() + c.stagingTx = c.db.ctx.newTransaction() c.setHead(headHash, head.header.number) return ok() @@ -489,7 +489,7 @@ proc forkChoice*(c: ForkedChainRef, # Write segment from base+1 to newBase into database c.stagingTx.rollback() - c.stagingTx = c.db.newTransaction() + c.stagingTx = c.db.ctx.newTransaction() if newBase.header.number > c.baseHeader.number: c.replaySegment(newBase.hash) @@ -504,7 +504,7 @@ proc forkChoice*(c: ForkedChainRef, if c.stagingTx.isNil: # replaySegment or setHead below don't # go straight to db - c.stagingTx = c.db.newTransaction() + c.stagingTx = c.db.ctx.newTransaction() # Move chain state forward to current head if newBase.header.number < head.header.number: diff --git a/nimbus/core/chain/persist_blocks.nim b/nimbus/core/chain/persist_blocks.nim index 12233b6d3..e6cceba0b 100644 --- a/nimbus/core/chain/persist_blocks.nim +++ b/nimbus/core/chain/persist_blocks.nim @@ -78,7 +78,7 @@ proc purgeOlderBlocksFromHistory(db: CoreDbRef, bn: BlockNumber) = proc persistBlocksImpl( c: ChainRef, blocks: openArray[EthBlock], flags: PersistBlockFlags = {} ): Result[PersistStats, string] = - let dbTx = c.db.newTransaction() + let dbTx = c.db.ctx.newTransaction() defer: dbTx.dispose() diff --git a/nimbus/core/tx_pool/tx_tasks/tx_packer.nim b/nimbus/core/tx_pool/tx_tasks/tx_packer.nim index a9c0ea2c0..f3188488e 100644 --- a/nimbus/core/tx_pool/tx_tasks/tx_packer.nim +++ b/nimbus/core/tx_pool/tx_tasks/tx_packer.nim @@ -165,7 +165,7 @@ proc vmExecInit(xp: TxPoolRef): Result[TxPackerStateRef, string] let packer = TxPackerStateRef( # return value xp: xp, - tr: AristoDbMemory.newCoreDbRef().ctx.getColumn(CtGeneric, clearData=true), + tr: AristoDbMemory.newCoreDbRef().ctx.getGeneric(clearData=true), balance: xp.chain.vmState.readOnlyStateDB.getBalance(xp.chain.feeRecipient), numBlobPerBlock: 0, ) @@ -274,7 +274,7 @@ proc packerVmExec*(xp: TxPoolRef): Result[void, string] {.gcsafe,raises: [Catcha ## Rebuild `packed` bucket by selection items from the `staged` bucket ## after executing them in the VM. let db = xp.chain.com.db - let dbTx = db.newTransaction + let dbTx = db.ctx.newTransaction() defer: dbTx.dispose() var pst = xp.vmExecInit.valueOr: diff --git a/nimbus/db/aristo/TODO.md b/nimbus/db/aristo/TODO.md index 4d1d960ab..713b45f60 100644 --- a/nimbus/db/aristo/TODO.md +++ b/nimbus/db/aristo/TODO.md @@ -6,13 +6,6 @@ remember the follow up vertices which can travel through the tx-layers to be picked up by the backend store. -* Consider changing fetch/merge/delete prototypes for account and storage. At - the moment they all use `openArray[]` for strictly 32 byte arrays (which is - only implicitely enforced at run time -- i.e. it would fail otherwise.) - * Mental note: For *proof-mode* with pre-allocated locked vertices and Merkle - keys, verification of a patyion tree must be done by computing sub-tree keys + keys, verification of a partial tree must be done by computing sub-tree keys at the relative roots and comparing them with the pre-allocated Merkle keys. - -* Remove legacy state format import from `deblobifyTo()` after a while (last - updated 28/06/24). diff --git a/nimbus/db/core_db/backend/aristo_replicate.nim b/nimbus/db/core_db/backend/aristo_replicate.nim index a8335b3c7..4d754121e 100644 --- a/nimbus/db/core_db/backend/aristo_replicate.nim +++ b/nimbus/db/core_db/backend/aristo_replicate.nim @@ -41,9 +41,6 @@ template call(kvt: CoreDbKvtRef; fn: untyped; args: varArgs[untyped]): untyped = template mpt(dsc: CoreDbAccRef | CoreDbMptRef): AristoDbRef = dsc.distinctBase.mpt -template rootID(mpt: CoreDbMptRef): VertexID = - VertexID(CtGeneric) - template call(api: AristoApiRef; fn: untyped; args: varArgs[untyped]): untyped = when CoreDbEnableApiJumpTable: api.fn(args) @@ -67,10 +64,11 @@ iterator aristoReplicate[T]( ## let p = mpt.call(forkTx, mpt.mpt, 0).valueOrApiError "aristoReplicate()" defer: discard mpt.call(forget, p) - for (rvid,key,vtx,node) in T.replicate(p): + for (rVid,key,vtx,node) in T.replicate(p): if key.len == 32: yield (@(key.data), node.encode) - elif rvid.vid == mpt.rootID: + elif rVid.vid == CoreDbVidGeneric: + # FIXME: Would an assert rather be appropriate here? yield (@(key.to(Hash256).data), node.encode) # End diff --git a/nimbus/db/core_db/backend/aristo_rocksdb.nim b/nimbus/db/core_db/backend/aristo_rocksdb.nim index ace85fcca..9a681cc16 100644 --- a/nimbus/db/core_db/backend/aristo_rocksdb.nim +++ b/nimbus/db/core_db/backend/aristo_rocksdb.nim @@ -24,8 +24,6 @@ import ./aristo_db, ../../opts -include ./aristo_replicate - const # Expectation messages aristoFail = "Aristo/RocksDB init() failed" diff --git a/nimbus/db/core_db/base.nim b/nimbus/db/core_db/base.nim index 8ee4cbd6b..c4c0c6192 100644 --- a/nimbus/db/core_db/base.nim +++ b/nimbus/db/core_db/base.nim @@ -44,7 +44,6 @@ export CoreDbApiError, #CoreDbCaptFlags, #CoreDbCaptRef, - CoreDbColType, CoreDbCtxRef, CoreDbErrorCode, CoreDbErrorRef, @@ -170,9 +169,6 @@ template mpt(tx: CoreDbTxRef): AristoDbRef = template ctx(acc: CoreDbAccRef): CoreDbCtxRef = acc.distinctBase -template rootID(mpt: CoreDbMptRef): VertexID = - VertexID(CtGeneric) - # --------------- template call(api: AristoApiRef; fn: untyped; args: varArgs[untyped]): untyped = @@ -344,8 +340,8 @@ proc stateBlockNumber*(db: CoreDbRef): BlockNumber = # ------------------------------------------------------------------------------ proc getKvt*(ctx: CoreDbCtxRef): CoreDbKvtRef = - ## This function subscribes to the common base object shared with other - ## KVT descriptors. Any changes are immediately visible to subscribers. + ## This function retrieves the common base object shared with other KVT + ## descriptors. Any changes are immediately visible to subscribers. ## On destruction (when the constructed object gets out of scope), changes ## are not saved to the backend database but are still cached and available. ## @@ -444,7 +440,7 @@ proc getGeneric*( ctx.setTrackNewApi CtxGetGenericFn result = CoreDbMptRef(ctx) if clearData: - result.call(deleteGenericTree, ctx.mpt, result.rootID).isOkOr: + result.call(deleteGenericTree, ctx.mpt, CoreDbVidGeneric).isOkOr: raiseAssert $api & ": " & $error ctx.ifTrackNewApi: debug newApiTxt, api, clearData, elapsed @@ -456,7 +452,7 @@ proc fetch*(mpt: CoreDbMptRef; key: openArray[byte]): CoreDbRc[Blob] = ## mpt.setTrackNewApi MptFetchFn result = block: - let rc = mpt.call(fetchGenericData, mpt.mpt, mpt.rootID, key) + let rc = mpt.call(fetchGenericData, mpt.mpt, CoreDbVidGeneric, key) if rc.isOk: ok(rc.value) elif rc.error == FetchPathNotFound: @@ -471,7 +467,7 @@ proc fetchOrEmpty*(mpt: CoreDbMptRef; key: openArray[byte]): CoreDbRc[Blob] = ## mpt.setTrackNewApi MptFetchOrEmptyFn result = block: - let rc = mpt.call(fetchGenericData, mpt.mpt, mpt.rootID, key) + let rc = mpt.call(fetchGenericData, mpt.mpt, CoreDbVidGeneric, key) if rc.isOk: ok(rc.value) elif rc.error == FetchPathNotFound: @@ -483,7 +479,7 @@ proc fetchOrEmpty*(mpt: CoreDbMptRef; key: openArray[byte]): CoreDbRc[Blob] = proc delete*(mpt: CoreDbMptRef; key: openArray[byte]): CoreDbRc[void] = mpt.setTrackNewApi MptDeleteFn result = block: - let rc = mpt.call(deleteGenericData, mpt.mpt, mpt.rootID, key) + let rc = mpt.call(deleteGenericData, mpt.mpt,CoreDbVidGeneric, key) if rc.isOk: ok() elif rc.error == DelPathNotFound: @@ -499,7 +495,7 @@ proc merge*( ): CoreDbRc[void] = mpt.setTrackNewApi MptMergeFn result = block: - let rc = mpt.call(mergeGenericData, mpt.mpt, mpt.rootID, key, val) + let rc = mpt.call(mergeGenericData, mpt.mpt,CoreDbVidGeneric, key, val) if rc.isOk: ok() else: @@ -513,7 +509,7 @@ proc hasPath*(mpt: CoreDbMptRef; key: openArray[byte]): CoreDbRc[bool] = ## mpt.setTrackNewApi MptHasPathFn result = block: - let rc = mpt.call(hasPathGeneric, mpt.mpt, mpt.rootID, key) + let rc = mpt.call(hasPathGeneric, mpt.mpt, CoreDbVidGeneric, key) if rc.isOk: ok(rc.value) else: @@ -529,7 +525,7 @@ proc state*(mpt: CoreDbMptRef; updateOk = false): CoreDbRc[Hash256] = ## mpt.setTrackNewApi MptStateFn result = block: - let rc = mpt.call(fetchGenericState, mpt.mpt, mpt.rootID, updateOk) + let rc = mpt.call(fetchGenericState, mpt.mpt, CoreDbVidGeneric, updateOk) if rc.isOk: ok(rc.value) else: @@ -874,28 +870,6 @@ proc dispose*(tx: CoreDbTxRef) = raiseAssert $api & ": " & $error tx.ifTrackNewApi: debug newApiTxt, api, elapsed, prvLevel -# ------------------------------------------------------------------------------ -# Legacy and convenience methods -# ------------------------------------------------------------------------------ - -proc newKvt*(db: CoreDbRef): CoreDbKvtRef = - ## Variant of `getKvt()` retrieving the KVT from the default context - db.ctx.getKvt - -proc newTransaction*(db: CoreDbRef): CoreDbTxRef = - ## Variant of `newTransaction()` starting the transaction on the default - ## context - db.ctx.newTransaction - -proc getColumn*( - ctx: CoreDbCtxRef; - colType: CoreDbColType; - clearData = false; - ): CoreDbMptRef = - ## Variant of `getGenteric()` forcing `colType` to be `CtGeneric` - doAssert colType == CtGeneric - ctx.getGeneric clearData - # ------------------------------------------------------------------------------ # Public tracer methods # ------------------------------------------------------------------------------ diff --git a/nimbus/db/core_db/base/base_desc.nim b/nimbus/db/core_db/base/base_desc.nim index 21a05be86..6c6d3d23a 100644 --- a/nimbus/db/core_db/base/base_desc.nim +++ b/nimbus/db/core_db/base/base_desc.nim @@ -29,6 +29,10 @@ type const CoreDbPersistentTypes* = {AristoDbRocks} + ## List of persistent DB types (currently only a single one) + + CoreDbVidGeneric* = VertexID(2) + ## Generic `MPT` root vertex ID for calculating Merkle hashes type CoreDbProfListRef* = AristoDbProfListRef @@ -59,9 +63,6 @@ type StoNotFound TxPending - CoreDbColType* = enum # Keep that legacy type for a while .. - CtGeneric = 2 # Actually only this constant is needed - CoreDbCaptFlags* {.pure.} = enum PersistPut PersistDel diff --git a/nimbus/db/core_db/base_iterators.nim b/nimbus/db/core_db/base_iterators.nim index 19f6aadbc..710643661 100644 --- a/nimbus/db/core_db/base_iterators.nim +++ b/nimbus/db/core_db/base_iterators.nim @@ -69,7 +69,7 @@ iterator pairs*(mpt: CoreDbMptRef): (Blob, Blob) = mpt.setTrackNewApi MptPairsIt case mpt.dbType: of AristoDbMemory, AristoDbRocks, AristoDbVoid: - for (path,data) in mpt.mpt.rightPairsGeneric mpt.rootID: + for (path,data) in mpt.mpt.rightPairsGeneric CoreDbVidGeneric: yield (mpt.call(pathAsBlob, path), data) of Ooops: raiseAssert: "Unsupported database type: " & $mpt.dbType diff --git a/nimbus/db/core_db/core_apps.nim b/nimbus/db/core_db/core_apps.nim index c9451edcb..8d48741a4 100644 --- a/nimbus/db/core_db/core_apps.nim +++ b/nimbus/db/core_db/core_apps.nim @@ -119,7 +119,7 @@ iterator getBlockTransactionData*( if txRoot == EMPTY_ROOT_HASH: break body - let kvt = db.newKvt() + let kvt = db.ctx.getKvt() for idx in 0'u16..blockHash discard kvt.del(blockNumberToHashKey(blockNum).toOpenArray) result = true @@ -542,7 +542,7 @@ proc getTransactionByIndex*( const info = "getTransaction()" - let kvt = db.newKvt() + let kvt = db.ctx.getKvt() let key = hashIndexKey(txRoot, txIndex) let txData = kvt.getOrEmpty(key).valueOr: warn logTxt "getTransaction()", @@ -566,7 +566,7 @@ proc getTransactionCount*( const info = "getTransactionCount()" - let kvt = db.newKvt() + let kvt = db.ctx.getKvt() var txCount = 0'u16 while true: let key = hashIndexKey(txRoot, txCount) @@ -590,7 +590,7 @@ proc getUnclesCount*( if ommersHash != EMPTY_UNCLE_HASH: let encodedUncles = block: let key = genericHashKey(ommersHash) - db.newKvt().get(key.toOpenArray).valueOr: + db.ctx.getKvt().get(key.toOpenArray).valueOr: if error.error == KvtNotFound: warn logTxt info, ommersHash, action="get()", `error`=($$error) return 0 @@ -605,7 +605,7 @@ proc getUncles*( if ommersHash != EMPTY_UNCLE_HASH: let encodedUncles = block: let key = genericHashKey(ommersHash) - db.newKvt().get(key.toOpenArray).valueOr: + db.ctx.getKvt().get(key.toOpenArray).valueOr: if error.error == KvtNotFound: warn logTxt info, ommersHash, action="get()", `error`=($$error) return @[] @@ -619,7 +619,7 @@ proc persistWithdrawals*( const info = "persistWithdrawals()" if withdrawals.len == 0: return - let kvt = db.newKvt() + let kvt = db.ctx.getKvt() for idx, wd in withdrawals: let key = hashIndexKey(withdrawalsRoot, idx.uint16) kvt.put(key, rlp.encode(wd)).isOkOr: @@ -718,7 +718,7 @@ proc getUncleHashes*( if header.ommersHash != EMPTY_UNCLE_HASH: let key = genericHashKey(header.ommersHash) - encodedUncles = db.newKvt().get(key.toOpenArray).valueOr: + encodedUncles = db.ctx.getKvt().get(key.toOpenArray).valueOr: if error.error == KvtNotFound: warn logTxt "getUncleHashes()", ommersHash=header.ommersHash, action="get()", `error`=($$error) @@ -732,7 +732,7 @@ proc getTransactionKey*( {.gcsafe, raises: [RlpError].} = let txKey = transactionHashToBlockKey(transactionHash) - tx = db.newKvt().get(txKey.toOpenArray).valueOr: + tx = db.ctx.getKvt().get(txKey.toOpenArray).valueOr: if error.error == KvtNotFound: warn logTxt "getTransactionKey()", transactionHash, action="get()", `error`=($$error) @@ -742,7 +742,7 @@ proc getTransactionKey*( proc headerExists*(db: CoreDbRef; blockHash: Hash256): bool = ## Returns True if the header with the given block hash is in our DB. - db.newKvt().hasKey(genericHashKey(blockHash).toOpenArray).valueOr: + db.ctx.getKvt().hasKey(genericHashKey(blockHash).toOpenArray).valueOr: warn logTxt "headerExists()", blockHash, action="get()", `error`=($$error) return false @@ -759,7 +759,7 @@ proc setHead*( return false let canonicalHeadHash = canonicalHeadHashKey() - db.newKvt.put(canonicalHeadHash.toOpenArray, rlp.encode(blockHash)).isOkOr: + db.ctx.getKvt.put(canonicalHeadHash.toOpenArray, rlp.encode(blockHash)).isOkOr: warn logTxt "setHead()", canonicalHeadHash, action="put()", error=($$error) return true @@ -770,7 +770,7 @@ proc setHead*( ): bool {.gcsafe, raises: [RlpError].} = var headerHash = rlpHash(header) - let kvt = db.newKvt() + let kvt = db.ctx.getKvt() if writeHeader: kvt.put(genericHashKey(headerHash).toOpenArray, rlp.encode(header)).isOkOr: warn logTxt "setHead()", headerHash, action="put()", error=($$error) @@ -792,7 +792,7 @@ proc persistReceipts*( if receipts.len == 0: return - let kvt = db.newKvt() + let kvt = db.ctx.getKvt() for idx, rec in receipts: let key = hashIndexKey(receiptsRoot, idx.uint16) kvt.put(key, rlp.encode(rec)).isOkOr: @@ -814,7 +814,7 @@ proc persistScore*( score: UInt256 ): bool = let - kvt = db.newKvt() + kvt = db.ctx.getKvt() scoreKey = blockHashToScoreKey(blockHash) kvt.put(scoreKey.toOpenArray, rlp.encode(score)).isOkOr: warn logTxt "persistHeader()", @@ -829,7 +829,7 @@ proc persistHeader*( startOfHistory = GENESIS_PARENT_HASH; ): bool = let - kvt = db.newKvt() + kvt = db.ctx.getKvt() isStartOfHistory = header.parentHash == startOfHistory if not isStartOfHistory and not db.headerExists(header.parentHash): @@ -902,7 +902,7 @@ proc persistUncles*(db: CoreDbRef, uncles: openArray[BlockHeader]): Hash256 = ## Returns the uncles hash. let enc = rlp.encode(uncles) result = keccakHash(enc) - db.newKvt.put(genericHashKey(result).toOpenArray, enc).isOkOr: + db.ctx.getKvt.put(genericHashKey(result).toOpenArray, enc).isOkOr: warn logTxt "persistUncles()", unclesHash=result, action="put()", `error`=($$error) return EMPTY_ROOT_HASH @@ -913,7 +913,7 @@ proc safeHeaderHash*(db: CoreDbRef): Hash256 = proc safeHeaderHash*(db: CoreDbRef, headerHash: Hash256) = let safeHashKey = safeHashKey() - db.newKvt.put(safeHashKey.toOpenArray, rlp.encode(headerHash)).isOkOr: + db.ctx.getKvt.put(safeHashKey.toOpenArray, rlp.encode(headerHash)).isOkOr: warn logTxt "safeHeaderHash()", safeHashKey, action="put()", `error`=($$error) return @@ -925,7 +925,7 @@ proc finalizedHeaderHash*( proc finalizedHeaderHash*(db: CoreDbRef, headerHash: Hash256) = let finalizedHashKey = finalizedHashKey() - db.newKvt.put(finalizedHashKey.toOpenArray, rlp.encode(headerHash)).isOkOr: + db.ctx.getKvt.put(finalizedHashKey.toOpenArray, rlp.encode(headerHash)).isOkOr: warn logTxt "finalizedHeaderHash()", finalizedHashKey, action="put()", `error`=($$error) return diff --git a/nimbus/db/ledger/accounts_ledger.nim b/nimbus/db/ledger/accounts_ledger.nim index 3327d583e..b40ceae87 100644 --- a/nimbus/db/ledger/accounts_ledger.nim +++ b/nimbus/db/ledger/accounts_ledger.nim @@ -154,7 +154,7 @@ proc init*(x: typedesc[AccountsLedgerRef], db: CoreDbRef, root: KeccakHash): AccountsLedgerRef = new result result.ledger = db.ctx.getAccounts() - result.kvt = db.newKvt() # save manually in `persist()` + result.kvt = db.ctx.getKvt() result.witnessCache = Table[EthAddress, WitnessData]() discard result.beginSavepoint diff --git a/nimbus/no-tracer.nim b/nimbus/no-tracer.nim index aff63138c..7280bac40 100644 --- a/nimbus/no-tracer.nim +++ b/nimbus/no-tracer.nim @@ -94,7 +94,7 @@ proc captureAccount(n: JsonNode, db: LedgerRef, address: EthAddress, name: strin proc dumpMemoryDB*(node: JsonNode, db: CoreDbRef) = var n = newJObject() - for k, v in db.newKvt(): + for k, v in db.ctx.getKvt(): n[k.toHex(false)] = %v node["state"] = n diff --git a/nimbus/rpc/experimental.nim b/nimbus/rpc/experimental.nim index 19e8b2bf6..280c33db8 100644 --- a/nimbus/rpc/experimental.nim +++ b/nimbus/rpc/experimental.nim @@ -49,7 +49,7 @@ proc getMultiKeys*( vmState.collectWitnessData = true # Enable saving witness data vmState.com.hardForkTransition(blockHeader) - let dbTx = vmState.com.db.newTransaction() + let dbTx = vmState.com.db.ctx.newTransaction() defer: dbTx.dispose() # Execute the block of transactions and collect the keys of the touched account state diff --git a/nimbus/sync/beacon/skeleton_db.nim b/nimbus/sync/beacon/skeleton_db.nim index 0e90d6728..9b9cd3483 100644 --- a/nimbus/sync/beacon/skeleton_db.nim +++ b/nimbus/sync/beacon/skeleton_db.nim @@ -30,15 +30,15 @@ logScope: # ------------------------------------------------------------------------------ template get(sk: SkeletonRef, key: untyped): untyped = - sk.db.newKvt().get(key.toOpenArray).valueOr: EmptyBlob + sk.db.ctx.getKvt().get(key.toOpenArray).valueOr: EmptyBlob template put(sk: SkeletonRef, key, val: untyped): untyped = - let rc = sk.db.newKvt().put(key.toOpenArray, val) + let rc = sk.db.ctx.getKvt().put(key.toOpenArray, val) if rc.isErr: raiseAssert "put() failed: " & $$rc.error template del(sk: SkeletonRef, key: untyped): untyped = - discard sk.db.newKvt().del(key.toOpenArray) + discard sk.db.ctx.getKvt().del(key.toOpenArray) proc append(w: var RlpWriter, s: Segment) = w.startList(3) diff --git a/nimbus/transaction/call_evm.nim b/nimbus/transaction/call_evm.nim index e37883029..74c368118 100644 --- a/nimbus/transaction/call_evm.nim +++ b/nimbus/transaction/call_evm.nim @@ -35,7 +35,7 @@ proc rpcCallEvm*(args: TransactionArgs, let vmState = ? BaseVMState.new(topHeader, com) let params = ? toCallParams(vmState, args, globalGasCap, header.baseFeePerGas) - var dbTx = com.db.newTransaction() + var dbTx = com.db.ctx.newTransaction() defer: dbTx.dispose() # always dispose state changes ok(runComputation(params)) @@ -47,7 +47,7 @@ proc rpcCallEvm*(args: TransactionArgs, const globalGasCap = 0 # TODO: globalGasCap should configurable by user let params = ? toCallParams(vmState, args, globalGasCap, header.baseFeePerGas) - var dbTx = com.db.newTransaction() + var dbTx = com.db.ctx.newTransaction() defer: dbTx.dispose() # always dispose state changes ok(runComputation(params)) @@ -72,7 +72,7 @@ proc rpcEstimateGas*(args: TransactionArgs, hi : GasInt = GasInt args.gas.get(0.Quantity) cap: GasInt - var dbTx = com.db.newTransaction() + var dbTx = com.db.ctx.newTransaction() defer: dbTx.dispose() # always dispose state changes # Determine the highest gas limit can be used during the estimation. diff --git a/nimbus_verified_proxy/rpc/rpc_utils.nim b/nimbus_verified_proxy/rpc/rpc_utils.nim index 132e91c5f..3704dcde6 100644 --- a/nimbus_verified_proxy/rpc/rpc_utils.nim +++ b/nimbus_verified_proxy/rpc/rpc_utils.nim @@ -86,7 +86,7 @@ proc calculateTransactionData( ## - root of transactions trie ## - list of transactions hashes ## - total size of transactions in block - var tr = newCoreDbRef(DefaultDbMemory).ctx.getColumn(CtGeneric) + var tr = newCoreDbRef(DefaultDbMemory).ctx.getGeneric() var txHashes: seq[TxOrHash] var txSize: uint64 for i, t in items: diff --git a/premix/debug.nim b/premix/debug.nim index 3aed14b41..d931e0bf9 100644 --- a/premix/debug.nim +++ b/premix/debug.nim @@ -21,7 +21,7 @@ import proc prepareBlockEnv(node: JsonNode, memoryDB: CoreDbRef) = let state = node["state"] - let kvt = memoryDB.newKvt() + let kvt = memoryDB.ctx.getKvt() for k, v in state: let key = hexToSeqByte(k) let value = hexToSeqByte(v.getStr()) @@ -34,7 +34,7 @@ proc executeBlock(blockEnv: JsonNode, memoryDB: CoreDbRef, blockNumber: BlockNum com = CommonRef.new(memoryDB) parent = com.db.getBlockHeader(parentNumber) blk = com.db.getEthBlock(blockNumber) - let transaction = memoryDB.newTransaction() + let transaction = memoryDB.ctx.newTransaction() defer: transaction.dispose() let diff --git a/premix/dumper.nim b/premix/dumper.nim index 8c21bf7f6..2cdfb7b4a 100644 --- a/premix/dumper.nim +++ b/premix/dumper.nim @@ -28,7 +28,7 @@ proc dumpDebug(com: CommonRef, blockNumber: BlockNumber) = capture = com.db.newCapture.value captureCom = com.clone(capture.recorder) - let transaction = capture.recorder.newTransaction() + let transaction = capture.recorder.ctx.newTransaction() defer: transaction.dispose() diff --git a/premix/persist.nim b/premix/persist.nim index 022f6bd40..9fae27d74 100644 --- a/premix/persist.nim +++ b/premix/persist.nim @@ -66,7 +66,7 @@ proc main() {.used.} = var parentBlock = requestBlock(conf.head, { DownloadAndValidate }) discard com.db.setHead(parentBlock.header) - let kvt = com.db.newKvt() + let kvt = com.db.ctx.getKvt() if canonicalHeadHashKey().toOpenArray notin kvt: persistToDb(com.db): com.initializeEmptyDb() diff --git a/premix/prestate.nim b/premix/prestate.nim index fede7a7cd..21cfd6117 100644 --- a/premix/prestate.nim +++ b/premix/prestate.nim @@ -21,7 +21,7 @@ proc generatePrestate*(nimbus, geth: JsonNode, blockNumber: BlockNumber, parent: state = nimbus["state"] headerHash = rlpHash(header) chainDB = newCoreDbRef(DefaultDbMemory) - kvt = chainDB.newKvt() + kvt = chainDB.ctx.getKvt() discard chainDB.setHead(parent, true) chainDB.persistTransactions(blockNumber, header.txRoot, blk.transactions) diff --git a/premix/regress.nim b/premix/regress.nim index b36908a60..e0b6e1ddc 100644 --- a/premix/regress.nim +++ b/premix/regress.nim @@ -29,7 +29,7 @@ proc validateBlock(com: CommonRef, blockNumber: BlockNumber): BlockNumber = for i in 0 ..< numBlocks: blocks[i] = com.db.getEthBlock(blockNumber + i.BlockNumber) - let transaction = com.db.newTransaction() + let transaction = com.db.ctx.newTransaction() defer: transaction.dispose() for i in 0 ..< numBlocks: diff --git a/tests/test_forked_chain.nim b/tests/test_forked_chain.nim index 65bad5d77..d0e48961b 100644 --- a/tests/test_forked_chain.nim +++ b/tests/test_forked_chain.nim @@ -117,7 +117,7 @@ proc forkedChainMain*() = blk2 = cc.makeBlk(2, blk1) blk3 = cc.makeBlk(3, blk2) - dbTx = cc.db.newTransaction() + dbTx = cc.db.ctx.newTransaction() blk4 = cc.makeBlk(4, blk3) blk5 = cc.makeBlk(5, blk4) blk6 = cc.makeBlk(6, blk5) diff --git a/tests/test_ledger.nim b/tests/test_ledger.nim index a54247519..6aae35dd4 100644 --- a/tests/test_ledger.nim +++ b/tests/test_ledger.nim @@ -213,7 +213,7 @@ proc runTrial3Survive(env: TestEnv, ledger: LedgerRef; inx: int; noisy = false) let eAddr = env.txs[inx].getRecipient block: - let dbTx = env.xdb.newTransaction() + let dbTx = env.xdb.ctx.newTransaction() block: let accTx = ledger.beginSavepoint @@ -229,7 +229,7 @@ proc runTrial3Survive(env: TestEnv, ledger: LedgerRef; inx: int; noisy = false) dbTx.rollback() block: - let dbTx = env.xdb.newTransaction() + let dbTx = env.xdb.ctx.newTransaction() block: let accTx = ledger.beginSavepoint @@ -248,7 +248,7 @@ proc runTrial4(env: TestEnv, ledger: LedgerRef; inx: int; rollback: bool) = let eAddr = env.txs[inx].getRecipient block: - let dbTx = env.xdb.newTransaction() + let dbTx = env.xdb.ctx.newTransaction() block: let accTx = ledger.beginSavepoint @@ -278,7 +278,7 @@ proc runTrial4(env: TestEnv, ledger: LedgerRef; inx: int; rollback: bool) = dbTx.commit() block: - let dbTx = env.xdb.newTransaction() + let dbTx = env.xdb.ctx.newTransaction() block: let accTx = ledger.beginSavepoint @@ -355,7 +355,7 @@ proc runLedgerTransactionTests(noisy = true) = test &"Run {env.txi.len} two-step trials with rollback": let head = env.xdb.getCanonicalHead() for n in env.txi: - let dbTx = env.xdb.newTransaction() + let dbTx = env.xdb.ctx.newTransaction() defer: dbTx.dispose() let ledger = env.com.getLedger(head) env.runTrial2ok(ledger, n) @@ -363,7 +363,7 @@ proc runLedgerTransactionTests(noisy = true) = test &"Run {env.txi.len} three-step trials with rollback": let head = env.xdb.getCanonicalHead() for n in env.txi: - let dbTx = env.xdb.newTransaction() + let dbTx = env.xdb.ctx.newTransaction() defer: dbTx.dispose() let ledger = env.com.getLedger(head) env.runTrial3(ledger, n, rollback = true) @@ -372,7 +372,7 @@ proc runLedgerTransactionTests(noisy = true) = " throwing Exceptions": let head = env.xdb.getCanonicalHead() for n in env.txi: - let dbTx = env.xdb.newTransaction() + let dbTx = env.xdb.ctx.newTransaction() defer: dbTx.dispose() let ledger = env.com.getLedger(head) env.runTrial3Survive(ledger, n, noisy) @@ -380,7 +380,7 @@ proc runLedgerTransactionTests(noisy = true) = test &"Run {env.txi.len} tree-step trials without rollback": let head = env.xdb.getCanonicalHead() for n in env.txi: - let dbTx = env.xdb.newTransaction() + let dbTx = env.xdb.ctx.newTransaction() defer: dbTx.dispose() let ledger = env.com.getLedger(head) env.runTrial3(ledger, n, rollback = false) @@ -388,7 +388,7 @@ proc runLedgerTransactionTests(noisy = true) = test &"Run {env.txi.len} four-step trials with rollback and db frames": let head = env.xdb.getCanonicalHead() for n in env.txi: - let dbTx = env.xdb.newTransaction() + let dbTx = env.xdb.ctx.newTransaction() defer: dbTx.dispose() let ledger = env.com.getLedger(head) env.runTrial4(ledger, n, rollback = true) @@ -524,7 +524,7 @@ proc runLedgerBasicOperationsTests() = check ac.getCode(addr2) == code let key = contractHashKey(keccakHash(code)) - val = memDB.newKvt().get(key.toOpenArray).valueOr: EmptyBlob + val = memDB.ctx.getKvt().get(key.toOpenArray).valueOr: EmptyBlob check val == code test "accessList operations": diff --git a/tests/test_rpc.nim b/tests/test_rpc.nim index e3e09a416..0c4f0b31d 100644 --- a/tests/test_rpc.nim +++ b/tests/test_rpc.nim @@ -85,7 +85,7 @@ proc verifySlotProof(trustedStorageRoot: Web3Hash, slot: StorageProof): MptProof proc persistFixtureBlock(chainDB: CoreDbRef) = let header = getBlockHeader4514995() # Manually inserting header to avoid any parent checks - discard chainDB.newKvt.put(genericHashKey(header.blockHash).toOpenArray, rlp.encode(header)) + discard chainDB.ctx.getKvt.put(genericHashKey(header.blockHash).toOpenArray, rlp.encode(header)) chainDB.addBlockNumberToHashLookup(header) chainDB.persistTransactions(header.number, header.txRoot, getBlockBody4514995().transactions) chainDB.persistReceipts(header.receiptsRoot, getReceipts4514995()) @@ -167,6 +167,10 @@ proc setupEnv(com: CommonRef, signer, ks2: EthAddress, ctx: EthContext): TestEnv com.db.persistReceipts(vmState.receipts) let + # TODO: `getColumn(CtReceipts)` does not exists anymore. There s only the + # generic `MPT` left that can be retrieved with `getGeneric()`, + # optionally with argument `clearData=true` + # receiptRoot = com.db.ctx.getColumn(CtReceipts).state(updateOk=true).valueOr(EMPTY_ROOT_HASH) date = dateTime(2017, mMar, 30) timeStamp = date.toTime.toUnix.EthTime diff --git a/tests/test_tracer_json.nim b/tests/test_tracer_json.nim index 29a51ad62..afe95cb1d 100644 --- a/tests/test_tracer_json.nim +++ b/tests/test_tracer_json.nim @@ -34,8 +34,8 @@ proc preLoadAristoDb(cdb: CoreDbRef; jKvp: JsonNode; num: BlockNumber) = txRoot: Hash256 # header with block number `num` rcptRoot: Hash256 # ditto let - adb = cdb.ctx.getColumn(CtGeneric).backend.toAristo - kdb = cdb.newKvt.backend.toAristo + adb = cdb.mpt + kdb = cdb.kvt # Fill KVT and collect `proof` data for (k,v) in jKvp.pairs: @@ -60,6 +60,10 @@ proc preLoadAristoDb(cdb: CoreDbRef; jKvp: JsonNode; num: BlockNumber) = discard check kdb.put(key, val).isOk + # TODO: `getColumn(CtXyy)` does not exists anymore. There is only the generic + # `MPT` left that can be retrieved with `getGeneric()`, optionally with + # argument `clearData=true` + # Install sub-trie roots onto production db if txRoot.isValid: doAssert adb.mergeProof(txRoot, VertexID(CtTxs)).isOk