From d3d6d1c27efaba8e29a6101dd309bec9d1b2c918 Mon Sep 17 00:00:00 2001 From: bhartnett Date: Tue, 14 Oct 2025 19:48:03 +0800 Subject: [PATCH 01/10] Add kvts for column families to txFrame and use KvtContractCode column family for reading and storing code. --- .../db/core_db/backend/aristo_db.nim | 12 ++- .../db/core_db/backend/aristo_rocksdb.nim | 7 +- execution_chain/db/core_db/base.nim | 102 ++++++++++++------ execution_chain/db/core_db/base/base_desc.nim | 10 +- execution_chain/db/core_db/core_apps.nim | 4 +- execution_chain/db/kvt.nim | 6 +- .../db/kvt/kvt_init/rocks_db/rdb_desc.nim | 1 + execution_chain/db/ledger.nim | 7 +- 8 files changed, 94 insertions(+), 55 deletions(-) diff --git a/execution_chain/db/core_db/backend/aristo_db.nim b/execution_chain/db/core_db/backend/aristo_db.nim index cd656d19eb..95ed70f500 100644 --- a/execution_chain/db/core_db/backend/aristo_db.nim +++ b/execution_chain/db/core_db/backend/aristo_db.nim @@ -23,14 +23,16 @@ export base_desc # Public constructors # ------------------------------------------------------------------------------ -proc create*(dbType: CoreDbType; kvt: KvtDbRef; mpt: AristoDbRef): CoreDbRef = +proc create*(dbType: CoreDbType; mpt: AristoDbRef; kvts: array[KvtCFs, KvtDbRef]): CoreDbRef = ## Constructor helper - CoreDbRef(dbType: dbType, mpt: mpt, kvt: kvt) + CoreDbRef(dbType: dbType, mpt: mpt, kvts: kvts) proc newMemoryCoreDbRef*(): CoreDbRef = - AristoDbMemory.create( - KvtDbRef.init(), - AristoDbRef.init()) + var kvts: array[KvtCFs, KvtDbRef] + kvts[KvtGeneric] = KvtDbRef.init() + kvts[KvtContractCode] = KvtDbRef.init() + + AristoDbMemory.create(AristoDbRef.init(), kvts) # ------------------------------------------------------------------------------ # End diff --git a/execution_chain/db/core_db/backend/aristo_rocksdb.nim b/execution_chain/db/core_db/backend/aristo_rocksdb.nim index 6ef56c95d9..c1c0b9ce78 100644 --- a/execution_chain/db/core_db/backend/aristo_rocksdb.nim +++ b/execution_chain/db/core_db/backend/aristo_rocksdb.nim @@ -195,7 +195,10 @@ proc newRocksDbCoreDbRef*(basePath: string, opts: DbOptions): CoreDbRef = adb = AristoDbRef.init(opts, baseDb).valueOr: raiseAssert "Could not initialize aristo: " & $error - kdb = KvtDbRef.init(baseDb) + + var kvts: array[KvtCFs, KvtDbRef] + kvts[KvtGeneric] = KvtDbRef.init(baseDb, KvtGeneric) + kvts[KvtContractCode] = KvtDbRef.init(baseDb, KvtContractCode) if opts.rdbKeyCacheSize > 0: # Make sure key cache isn't empty @@ -203,7 +206,7 @@ proc newRocksDbCoreDbRef*(basePath: string, opts: DbOptions): CoreDbRef = fatal "Cannot compute root keys", msg = error quit(QuitFailure) - AristoDbRocks.create(kdb, adb) + AristoDbRocks.create(adb, kvts) # ------------------------------------------------------------------------------ # End diff --git a/execution_chain/db/core_db/base.nim b/execution_chain/db/core_db/base.nim index c0366fc8c1..ac500cf3f5 100644 --- a/execution_chain/db/core_db/base.nim +++ b/execution_chain/db/core_db/base.nim @@ -27,7 +27,8 @@ export CoreDbPersistentTypes, CoreDbRef, CoreDbTxRef, - CoreDbType + CoreDbType, + KvtCFs import ../aristo/[ @@ -46,13 +47,18 @@ proc baseTxFrame*(db: CoreDbRef): CoreDbTxRef = ## up in the base txframe before being persisted to the database with a ## persist call. + var kTxs: array[KvtCFs, KvtTxRef] + for i, kvt in db.kvts: + if not kvt.isNil(): + kTxs[i] = kvt.baseTxFrame() + CoreDbTxRef( aTx: db.mpt.baseTxFrame(), - kTx: db.kvt.baseTxFrame()) + kTxs: kTxs) proc kvtBackend*(db: CoreDbRef): TypedBackendRef = ## Get KVT backend - db.kvt.getBackendFn() + db.kvts[KvtGeneric].getBackendFn() # ------------------------------------------------------------------------------ # Public base descriptor methods @@ -66,8 +72,11 @@ proc finish*(db: CoreDbRef; eradicate = false) = ## depends on the backend database. Currently, only the `AristoDbRocks` type ## backend removes the database on `true`. - db.kvt.finish(eradicate) db.mpt.finish(eradicate) + for kvt in db.kvts: + if not kvt.isNil(): + kvt.finish(eradicate) + proc `$$`*(e: CoreDbError): string = ## Pretty print error symbol @@ -82,11 +91,19 @@ proc persist*(db: CoreDbRef, txFrame: CoreDbTxRef) = ## This function persists changes up to and including the given frame to the ## database. - let - kvtBatch = db.kvt.putBegFn() - mptBatch = db.mpt.putBegFn() + let mptBatch = db.mpt.putBegFn() + + var + kvtBatches: array[KvtCFs, Result[init_common.PutHdlRef, KvtError]] + kvtBatchesOk = true - if kvtBatch.isOk() and mptBatch.isOk(): + for i, kvt in db.kvts: + if not kvt.isNil(): + kvtBatches[i] = kvt.putBegFn() + if kvtBatches[i].isErr(): + kvtBatchesOk = false + + if mptBatch.isOk() and kvtBatchesOk: # TODO the `persist` api stages changes but does not actually persist - a # separate "actually-write" api is needed so the changes from both # kvt and ari can be staged and then written together - for this to @@ -97,14 +114,18 @@ proc persist*(db: CoreDbRef, txFrame: CoreDbTxRef) = # error), we have to panic instead. let kvtTick = Moment.now() - db.kvt.persist(kvtBatch[], txFrame.kTx) + for i, kvt in db.kvts: + if not kvt.isNil(): + kvt.persist(kvtBatches[i][], txFrame.kTxs[i]) + let mptTick = Moment.now() db.mpt.persist(mptBatch[], txFrame.aTx) let endTick = Moment.now() - db.kvt.putEndFn(kvtBatch[]).isOkOr: - raiseAssert $error - + for i, kvt in db.kvts: + if not kvt.isNil(): + kvt.putEndFn(kvtBatches[i][]).isOkOr: + raiseAssert $error db.mpt.putEndFn(mptBatch[]).isOkOr: raiseAssert $error @@ -113,8 +134,10 @@ proc persist*(db: CoreDbRef, txFrame: CoreDbTxRef) = mptDur = endTick - mptTick, endDur = Moment.now() - endTick else: - discard kvtBatch.expect("should always be able to create batch") discard mptBatch.expect("should always be able to create batch") + for i, kvt in db.kvts: + if not kvt.isNil(): + discard kvtBatches[i].expect("should always be able to create batch") proc stateBlockNumber*(db: CoreDbTxRef): BlockNumber = ## This function returns the block number stored with the latest `persist()` @@ -143,9 +166,9 @@ proc verifyProof*( # ----------- KVT --------------- -proc get*(kvt: CoreDbTxRef; key: openArray[byte]): CoreDbRc[seq[byte]] = +proc get*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): CoreDbRc[seq[byte]] = ## This function always returns a non-empty `seq[byte]` or an error code. - let rc = kvt.kTx.get(key) + let rc = kvt.kTxs[cf].get(key) if rc.isOk: ok(rc.value) elif rc.error == GetNotFound: @@ -153,11 +176,11 @@ proc get*(kvt: CoreDbTxRef; key: openArray[byte]): CoreDbRc[seq[byte]] = else: err(rc.error.toError("")) -proc getOrEmpty*(kvt: CoreDbTxRef; key: openArray[byte]): CoreDbRc[seq[byte]] = +proc getOrEmpty*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): CoreDbRc[seq[byte]] = ## Variant of `get()` returning an empty `seq[byte]` if the key is not found ## on the database. ## - let rc = kvt.kTx.get(key) + let rc = kvt.kTxs[cf].get(key) if rc.isOk: ok(rc.value) elif rc.error == GetNotFound: @@ -165,9 +188,9 @@ proc getOrEmpty*(kvt: CoreDbTxRef; key: openArray[byte]): CoreDbRc[seq[byte]] = else: err(rc.error.toError("")) -proc len*(kvt: CoreDbTxRef; key: openArray[byte]): CoreDbRc[int] = +proc len*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): CoreDbRc[int] = ## This function returns the size of the value associated with `key`. - let rc = kvt.kTx.len(key) + let rc = kvt.kTxs[cf].len(key) if rc.isOk: ok(rc.value) elif rc.error == GetNotFound: @@ -175,8 +198,8 @@ proc len*(kvt: CoreDbTxRef; key: openArray[byte]): CoreDbRc[int] = else: err(rc.error.toError("")) -proc del*(kvt: CoreDbTxRef; key: openArray[byte]): CoreDbRc[void] = - kvt.kTx.del(key).isOkOr: +proc del*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): CoreDbRc[void] = + kvt.kTxs[cf].del(key).isOkOr: return err(error.toError("")) ok() @@ -185,30 +208,31 @@ proc put*( kvt: CoreDbTxRef; key: openArray[byte]; val: openArray[byte]; + cf: KvtCFs = KvtGeneric; ): CoreDbRc[void] = - kvt.kTx.put(key, val).isOkOr: + kvt.kTxs[cf].put(key, val).isOkOr: return err(error.toError("")) ok() -proc hasKeyRc*(kvt: CoreDbTxRef; key: openArray[byte]): CoreDbRc[bool] = +proc hasKeyRc*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): CoreDbRc[bool] = ## For the argument `key` return `true` if `get()` returned a value on ## that argument, `false` if it returned `GetNotFound`, and an error ## otherwise. ## - let rc = kvt.kTx.hasKeyRc(key).valueOr: + let rc = kvt.kTxs[cf].hasKeyRc(key).valueOr: return err(error.toError("")) ok(rc) -proc hasKey*(kvt: CoreDbTxRef; key: openArray[byte]): bool = +proc hasKey*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): bool = ## Simplified version of `hasKeyRc` where `false` is returned instead of ## an error. ## ## This function prototype is in line with the `hasKey` function for ## `Tables`. ## - result = kvt.kTx.hasKeyRc(key).valueOr: false + result = kvt.kTxs[cf].hasKeyRc(key).valueOr: false # ------------------------------------------------------------------------------ # Public methods for accounts @@ -463,11 +487,14 @@ proc putSubtrie*( proc txFrameBegin*(db: CoreDbRef): CoreDbTxRef = ## Constructor ## - let - kTx = db.kvt.txFrameBegin(nil) - aTx = db.mpt.txFrameBegin(nil, false) + let aTx = db.mpt.txFrameBegin(nil, false) - CoreDbTxRef(kTx: kTx, aTx: aTx) + var kTxs: array[KvtCFs, KvtTxRef] + for i, kvt in db.kvts: + if not kvt.isNil(): + kTxs[i] = kvt.txFrameBegin(nil) + + CoreDbTxRef(aTx: aTx, kTxs: kTxs) proc txFrameBegin*( parent: CoreDbTxRef, @@ -475,11 +502,14 @@ proc txFrameBegin*( ): CoreDbTxRef = ## Constructor ## - let - kTx = parent.kTx.db.txFrameBegin(parent.kTx) - aTx = parent.aTx.db.txFrameBegin(parent.aTx, moveParentHashKeys) + let aTx = parent.aTx.db.txFrameBegin(parent.aTx, moveParentHashKeys) + + var kTxs: array[KvtCFs, KvtTxRef] + for i, kTx in parent.kTxs: + if not kTx.isNil(): + kTxs[i] = kTx.db.txFrameBegin(kTx) - CoreDbTxRef(kTx: kTx, aTx: aTx) + CoreDbTxRef(aTx: aTx, kTxs: kTxs) proc checkpoint*(tx: CoreDbTxRef, blockNumber: BlockNumber, skipSnapshot = false) = tx.aTx.checkpoint(blockNumber, skipSnapshot) @@ -489,7 +519,9 @@ proc clearSnapshot*(tx: CoreDbTxRef) = proc dispose*(tx: CoreDbTxRef) = tx.aTx.dispose() - tx.kTx.dispose() + for kTx in tx.kTxs: + if not kTx.isNil(): + kTx.dispose() tx[].reset() # ------------------------------------------------------------------------------ diff --git a/execution_chain/db/core_db/base/base_desc.nim b/execution_chain/db/core_db/base/base_desc.nim index 89d193c5ad..5570e3c0e9 100644 --- a/execution_chain/db/core_db/base/base_desc.nim +++ b/execution_chain/db/core_db/base/base_desc.nim @@ -54,14 +54,14 @@ type # -------------------------------------------------- CoreDbRef* = ref object ## Database descriptor - dbType*: CoreDbType ## Type of database backend - mpt*: AristoDbRef ## `Aristo` database - kvt*: KvtDbRef ## `KVT` key-value table + dbType*: CoreDbType ## Type of database backend + mpt*: AristoDbRef ## `Aristo` database + kvts*: array[KvtCFs, KvtDbRef] ## `KVT` key-value tables CoreDbTxRef* = ref object ## Transaction descriptor - aTx*: AristoTxRef ## `Aristo` transaction (if any) - kTx*: KvtTxRef ## `KVT` transaction (if any) + aTx*: AristoTxRef ## `Aristo` transaction (if any) + kTxs*: array[KvtCFs, KvtTxRef] ## `KVT` transactions (if any) CoreDbError* = object ## Generic error object diff --git a/execution_chain/db/core_db/core_apps.nim b/execution_chain/db/core_db/core_apps.nim index 299e0dbfc1..6e3fcc8752 100644 --- a/execution_chain/db/core_db/core_apps.nim +++ b/execution_chain/db/core_db/core_apps.nim @@ -642,13 +642,13 @@ proc getWitness*(db: CoreDbTxRef, blockHash: Hash32): Result[Witness, string] = Witness.decode(witnessBytes) proc getCodeByHash*(db: CoreDbTxRef, codeHash: Hash32): Result[seq[byte], string] = - let code = db.get(contractHashKey(codeHash).toOpenArray).valueOr: + let code = db.get(codeHash.data, KvtCFs.KvtContractCode).valueOr: return err("getCodeByHash: " & $$error) ok(code) proc setCodeByHash*(db: CoreDbTxRef, codeHash: Hash32, code: openArray[byte]): Result[void, string] = - db.put(contractHashKey(codeHash).toOpenArray, code).isOkOr: + db.put(codeHash.data, code, KvtCFs.KvtContractCode).isOkOr: return err("setCodeByHash: " & $$error) ok() diff --git a/execution_chain/db/kvt.nim b/execution_chain/db/kvt.nim index 3940e0272b..93fc1ba36e 100644 --- a/execution_chain/db/kvt.nim +++ b/execution_chain/db/kvt.nim @@ -19,11 +19,13 @@ export kvt_constants import - kvt/kvt_init/memory_only + kvt/kvt_init/memory_only, + kvt/kvt_init/rocks_db/rdb_desc export MemBackendRef, finish, - init + init, + KvtCFs import kvt/kvt_desc diff --git a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim index d1ee920668..1d9ea8cd08 100644 --- a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim +++ b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim @@ -27,6 +27,7 @@ type ## Column family symbols/handles and names used on the database KvtGeneric = "KvtGen" ## Generic column family KvtSynchro = "KvtSync" + KvtContractCode = "KvtCode" KvtCfStore* = array[KvtCFs, ColFamilyReadWrite] ## List of column family handlers diff --git a/execution_chain/db/ledger.nim b/execution_chain/db/ledger.nim index 3058b21f5e..b1ac3a9f53 100644 --- a/execution_chain/db/ledger.nim +++ b/execution_chain/db/ledger.nim @@ -291,8 +291,7 @@ proc persistMode(acc: AccountRef): PersistMode = proc persistCode(acc: AccountRef, ac: LedgerRef) = if acc.code.len != 0 and not acc.code.persisted: - let rc = ac.txFrame.put( - contractHashKey(acc.statement.codeHash).toOpenArray, acc.code.bytes()) + let rc = ac.txFrame.put(acc.statement.codeHash.data, acc.code.bytes(), KvtCFs.KvtContractCode) if rc.isErr: warn logTxt "persistCode()", codeHash=acc.statement.codeHash, error=($$rc.error) @@ -482,7 +481,7 @@ proc getCode*(ac: LedgerRef, acc.code = if acc.statement.codeHash != EMPTY_CODE_HASH: ac.code.get(acc.statement.codeHash).valueOr: - var rc = ac.txFrame.get(contractHashKey(acc.statement.codeHash).toOpenArray) + var rc = ac.txFrame.get(acc.statement.codeHash.data, KvtCFs.KvtContractCode) if rc.isErr: warn logTxt "getCode()", codeHash=acc.statement.codeHash, error=($$rc.error) CodeBytesRef() @@ -512,7 +511,7 @@ proc getCodeSize*(ac: LedgerRef, address: Address): int = # cached and easily accessible in the database layer - this is to prevent # EXTCODESIZE calls from messing up the code cache and thus causing # recomputation of the jump destination table - var rc = ac.txFrame.len(contractHashKey(acc.statement.codeHash).toOpenArray) + var rc = ac.txFrame.len(acc.statement.codeHash.data, KvtCFs.KvtContractCode) return rc.valueOr: warn logTxt "getCodeSize()", codeHash=acc.statement.codeHash, error=($$rc.error) From d54d9e83332342d0363785b17e9dd59b57b4c6cd Mon Sep 17 00:00:00 2001 From: bhartnett Date: Tue, 14 Oct 2025 20:11:13 +0800 Subject: [PATCH 02/10] Update test. --- tests/test_ledger.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_ledger.nim b/tests/test_ledger.nim index a37cff3d00..faf858b173 100644 --- a/tests/test_ledger.nim +++ b/tests/test_ledger.nim @@ -491,8 +491,8 @@ proc runLedgerBasicOperationsTests() = ac.persist() check ac.getCode(addr2) == code let - key = contractHashKey(keccak256(code)) - val = memDB.baseTxFrame().get(key.toOpenArray).valueOr: EmptyBlob + key = keccak256(code).data + val = memDB.baseTxFrame().get(key, KvtCFs.KvtContractCode).valueOr: EmptyBlob check val == code test "accessList operations": From 5bd3af17b5e5bf2be740a592bc403a1df68e933f Mon Sep 17 00:00:00 2001 From: bhartnett Date: Wed, 15 Oct 2025 13:05:57 +0800 Subject: [PATCH 03/10] Use pairsNotNil iterator. --- execution_chain/db/core_db/base.nim | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/execution_chain/db/core_db/base.nim b/execution_chain/db/core_db/base.nim index ac500cf3f5..6a229fcbaa 100644 --- a/execution_chain/db/core_db/base.nim +++ b/execution_chain/db/core_db/base.nim @@ -36,6 +36,11 @@ import aristo_tx_frame], ../kvt/[kvt_desc, kvt_utils, kvt_tx_frame] +iterator pairsNotNil[T](elements: array[KvtCFs, T]): (KvtCFs, T) = + for i, v in elements: + if not v.isNil(): + yield (i, v) + # ------------------------------------------------------------------------------ # Public context constructors and administration # ------------------------------------------------------------------------------ @@ -48,9 +53,8 @@ proc baseTxFrame*(db: CoreDbRef): CoreDbTxRef = ## persist call. var kTxs: array[KvtCFs, KvtTxRef] - for i, kvt in db.kvts: - if not kvt.isNil(): - kTxs[i] = kvt.baseTxFrame() + for i, kvt in db.kvts.pairsNotNil(): + kTxs[i] = kvt.baseTxFrame() CoreDbTxRef( aTx: db.mpt.baseTxFrame(), From 03d5cdad1fd8246e80d3988b38f6d5b42a4345af Mon Sep 17 00:00:00 2001 From: bhartnett Date: Wed, 15 Oct 2025 14:38:36 +0800 Subject: [PATCH 04/10] Make KvtCFs enum pure and map to CoreDbKvtType enum type when used in coredb layer. --- .../db/core_db/backend/aristo_db.nim | 4 +-- .../db/core_db/backend/aristo_rocksdb.nim | 4 +-- execution_chain/db/core_db/base.nim | 36 +++++++++---------- execution_chain/db/core_db/base/base_desc.nim | 6 ++-- execution_chain/db/core_db/core_apps.nim | 4 +-- .../db/kvt/kvt_init/persistent.nim | 2 +- .../db/kvt/kvt_init/rocks_db/rdb_desc.nim | 8 ++--- .../db/kvt/kvt_init/rocks_db/rdb_walk.nim | 2 +- execution_chain/db/kvt_cf.nim | 2 +- execution_chain/db/ledger.nim | 7 ++-- tests/test_ledger.nim | 2 +- 11 files changed, 38 insertions(+), 39 deletions(-) diff --git a/execution_chain/db/core_db/backend/aristo_db.nim b/execution_chain/db/core_db/backend/aristo_db.nim index 95ed70f500..5764501884 100644 --- a/execution_chain/db/core_db/backend/aristo_db.nim +++ b/execution_chain/db/core_db/backend/aristo_db.nim @@ -29,8 +29,8 @@ proc create*(dbType: CoreDbType; mpt: AristoDbRef; kvts: array[KvtCFs, KvtDbRef] proc newMemoryCoreDbRef*(): CoreDbRef = var kvts: array[KvtCFs, KvtDbRef] - kvts[KvtGeneric] = KvtDbRef.init() - kvts[KvtContractCode] = KvtDbRef.init() + kvts[KvtCFs.Generic] = KvtDbRef.init() + kvts[KvtCFs.ContractCode] = KvtDbRef.init() AristoDbMemory.create(AristoDbRef.init(), kvts) diff --git a/execution_chain/db/core_db/backend/aristo_rocksdb.nim b/execution_chain/db/core_db/backend/aristo_rocksdb.nim index c1c0b9ce78..bc24140134 100644 --- a/execution_chain/db/core_db/backend/aristo_rocksdb.nim +++ b/execution_chain/db/core_db/backend/aristo_rocksdb.nim @@ -197,8 +197,8 @@ proc newRocksDbCoreDbRef*(basePath: string, opts: DbOptions): CoreDbRef = raiseAssert "Could not initialize aristo: " & $error var kvts: array[KvtCFs, KvtDbRef] - kvts[KvtGeneric] = KvtDbRef.init(baseDb, KvtGeneric) - kvts[KvtContractCode] = KvtDbRef.init(baseDb, KvtContractCode) + kvts[KvtCFs.Generic] = KvtDbRef.init(baseDb, KvtCFs.Generic) + kvts[KvtCFs.ContractCode] = KvtDbRef.init(baseDb, KvtCFs.ContractCode) if opts.rdbKeyCacheSize > 0: # Make sure key cache isn't empty diff --git a/execution_chain/db/core_db/base.nim b/execution_chain/db/core_db/base.nim index 6a229fcbaa..413a9cfcd2 100644 --- a/execution_chain/db/core_db/base.nim +++ b/execution_chain/db/core_db/base.nim @@ -28,7 +28,7 @@ export CoreDbRef, CoreDbTxRef, CoreDbType, - KvtCFs + CoreDbKvtType import ../aristo/[ @@ -36,11 +36,6 @@ import aristo_tx_frame], ../kvt/[kvt_desc, kvt_utils, kvt_tx_frame] -iterator pairsNotNil[T](elements: array[KvtCFs, T]): (KvtCFs, T) = - for i, v in elements: - if not v.isNil(): - yield (i, v) - # ------------------------------------------------------------------------------ # Public context constructors and administration # ------------------------------------------------------------------------------ @@ -52,9 +47,10 @@ proc baseTxFrame*(db: CoreDbRef): CoreDbTxRef = ## up in the base txframe before being persisted to the database with a ## persist call. - var kTxs: array[KvtCFs, KvtTxRef] - for i, kvt in db.kvts.pairsNotNil(): - kTxs[i] = kvt.baseTxFrame() + var kTxs: array[CoreDbKvtType, KvtTxRef] + for i, kvt in db.kvts: + if not kvt.isNil(): + kTxs[i] = kvt.baseTxFrame() CoreDbTxRef( aTx: db.mpt.baseTxFrame(), @@ -62,7 +58,7 @@ proc baseTxFrame*(db: CoreDbRef): CoreDbTxRef = proc kvtBackend*(db: CoreDbRef): TypedBackendRef = ## Get KVT backend - db.kvts[KvtGeneric].getBackendFn() + db.kvts[CoreDbKvtType.Generic].getBackendFn() # ------------------------------------------------------------------------------ # Public base descriptor methods @@ -98,7 +94,7 @@ proc persist*(db: CoreDbRef, txFrame: CoreDbTxRef) = let mptBatch = db.mpt.putBegFn() var - kvtBatches: array[KvtCFs, Result[init_common.PutHdlRef, KvtError]] + kvtBatches: array[CoreDbKvtType, Result[init_common.PutHdlRef, KvtError]] kvtBatchesOk = true for i, kvt in db.kvts: @@ -170,7 +166,7 @@ proc verifyProof*( # ----------- KVT --------------- -proc get*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): CoreDbRc[seq[byte]] = +proc get*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): CoreDbRc[seq[byte]] = ## This function always returns a non-empty `seq[byte]` or an error code. let rc = kvt.kTxs[cf].get(key) if rc.isOk: @@ -180,7 +176,7 @@ proc get*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): Core else: err(rc.error.toError("")) -proc getOrEmpty*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): CoreDbRc[seq[byte]] = +proc getOrEmpty*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): CoreDbRc[seq[byte]] = ## Variant of `get()` returning an empty `seq[byte]` if the key is not found ## on the database. ## @@ -192,7 +188,7 @@ proc getOrEmpty*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric else: err(rc.error.toError("")) -proc len*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): CoreDbRc[int] = +proc len*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): CoreDbRc[int] = ## This function returns the size of the value associated with `key`. let rc = kvt.kTxs[cf].len(key) if rc.isOk: @@ -202,7 +198,7 @@ proc len*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): Core else: err(rc.error.toError("")) -proc del*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): CoreDbRc[void] = +proc del*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): CoreDbRc[void] = kvt.kTxs[cf].del(key).isOkOr: return err(error.toError("")) @@ -212,14 +208,14 @@ proc put*( kvt: CoreDbTxRef; key: openArray[byte]; val: openArray[byte]; - cf: KvtCFs = KvtGeneric; + cf = CoreDbKvtType.Generic; ): CoreDbRc[void] = kvt.kTxs[cf].put(key, val).isOkOr: return err(error.toError("")) ok() -proc hasKeyRc*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): CoreDbRc[bool] = +proc hasKeyRc*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): CoreDbRc[bool] = ## For the argument `key` return `true` if `get()` returned a value on ## that argument, `false` if it returned `GetNotFound`, and an error ## otherwise. @@ -229,7 +225,7 @@ proc hasKeyRc*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): ok(rc) -proc hasKey*(kvt: CoreDbTxRef; key: openArray[byte], cf: KvtCFs = KvtGeneric): bool = +proc hasKey*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): bool = ## Simplified version of `hasKeyRc` where `false` is returned instead of ## an error. ## @@ -493,7 +489,7 @@ proc txFrameBegin*(db: CoreDbRef): CoreDbTxRef = ## let aTx = db.mpt.txFrameBegin(nil, false) - var kTxs: array[KvtCFs, KvtTxRef] + var kTxs: array[CoreDbKvtType, KvtTxRef] for i, kvt in db.kvts: if not kvt.isNil(): kTxs[i] = kvt.txFrameBegin(nil) @@ -508,7 +504,7 @@ proc txFrameBegin*( ## let aTx = parent.aTx.db.txFrameBegin(parent.aTx, moveParentHashKeys) - var kTxs: array[KvtCFs, KvtTxRef] + var kTxs: array[CoreDbKvtType, KvtTxRef] for i, kTx in parent.kTxs: if not kTx.isNil(): kTxs[i] = kTx.db.txFrameBegin(kTx) diff --git a/execution_chain/db/core_db/base/base_desc.nim b/execution_chain/db/core_db/base/base_desc.nim index 5570e3c0e9..07a69e1006 100644 --- a/execution_chain/db/core_db/base/base_desc.nim +++ b/execution_chain/db/core_db/base/base_desc.nim @@ -49,6 +49,8 @@ type StoNotFound TxPending + CoreDbKvtType* = KvtCFs + # -------------------------------------------------- # Production descriptors # -------------------------------------------------- @@ -56,12 +58,12 @@ type ## Database descriptor dbType*: CoreDbType ## Type of database backend mpt*: AristoDbRef ## `Aristo` database - kvts*: array[KvtCFs, KvtDbRef] ## `KVT` key-value tables + kvts*: array[CoreDbKvtType, KvtDbRef] ## `KVT` key-value tables CoreDbTxRef* = ref object ## Transaction descriptor aTx*: AristoTxRef ## `Aristo` transaction (if any) - kTxs*: array[KvtCFs, KvtTxRef] ## `KVT` transactions (if any) + kTxs*: array[CoreDbKvtType, KvtTxRef] ## `KVT` transactions (if any) CoreDbError* = object ## Generic error object diff --git a/execution_chain/db/core_db/core_apps.nim b/execution_chain/db/core_db/core_apps.nim index 6e3fcc8752..a28c0d49e0 100644 --- a/execution_chain/db/core_db/core_apps.nim +++ b/execution_chain/db/core_db/core_apps.nim @@ -642,13 +642,13 @@ proc getWitness*(db: CoreDbTxRef, blockHash: Hash32): Result[Witness, string] = Witness.decode(witnessBytes) proc getCodeByHash*(db: CoreDbTxRef, codeHash: Hash32): Result[seq[byte], string] = - let code = db.get(codeHash.data, KvtCFs.KvtContractCode).valueOr: + let code = db.get(codeHash.data, CoreDbKvtType.ContractCode).valueOr: return err("getCodeByHash: " & $$error) ok(code) proc setCodeByHash*(db: CoreDbTxRef, codeHash: Hash32, code: openArray[byte]): Result[void, string] = - db.put(codeHash.data, code, KvtCFs.KvtContractCode).isOkOr: + db.put(codeHash.data, code, CoreDbKvtType.ContractCode).isOkOr: return err("setCodeByHash: " & $$error) ok() diff --git a/execution_chain/db/kvt/kvt_init/persistent.nim b/execution_chain/db/kvt/kvt_init/persistent.nim index 2c9c41253d..eac6670844 100644 --- a/execution_chain/db/kvt/kvt_init/persistent.nim +++ b/execution_chain/db/kvt/kvt_init/persistent.nim @@ -33,7 +33,7 @@ export proc init*( T: type KvtDbRef; baseDb: RocksDbInstanceRef; - cf: static[KvtCFs] = KvtGeneric): T = + cf: static[KvtCFs] = KvtCFs.Generic): T = ## Generic constructor for `RocksDb` backend ## let db = rocksDbKvtBackend(baseDb, cf) diff --git a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim index 1d9ea8cd08..59a4a9221a 100644 --- a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim +++ b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim @@ -23,11 +23,11 @@ type baseDb*: RocksDbInstanceRef store*: KvtCfStore ## Rocks DB database handler - KvtCFs* = enum + KvtCFs* {.pure.} = enum ## Column family symbols/handles and names used on the database - KvtGeneric = "KvtGen" ## Generic column family - KvtSynchro = "KvtSync" - KvtContractCode = "KvtCode" + Generic = "KvtGen" ## Generic column family + Synchro = "KvtSync" ## Syncer block headers column family + ContractCode = "KvtCode" ## Contract code column family KvtCfStore* = array[KvtCFs, ColFamilyReadWrite] ## List of column family handlers diff --git a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim index 213537969b..f931264a3b 100644 --- a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim +++ b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim @@ -36,7 +36,7 @@ iterator walk*(rdb: RdbInst): tuple[key: seq[byte], data: seq[byte]] = ## ## Non-decodable entries are stepped over and ignored. block walkBody: - let rit = rdb.store[KvtGeneric].openIterator().valueOr: + let rit = rdb.store[KvtCFs.Generic].openIterator().valueOr: when extraTraceMessages: trace logTxt "walk", pfx="all", error break walkBody diff --git a/execution_chain/db/kvt_cf.nim b/execution_chain/db/kvt_cf.nim index 7494eb9e92..a06e87c40f 100644 --- a/execution_chain/db/kvt_cf.nim +++ b/execution_chain/db/kvt_cf.nim @@ -21,6 +21,6 @@ proc synchronizerKvt*(be: TypedBackendRef): KvtTxRef = doAssert be.beKind == BackendRocksDB let baseDb = RdbBackendRef(be).getBaseDb() - rdb = rocksDbKvtBackend(baseDb, KvtSynchro) + rdb = rocksDbKvtBackend(baseDb, KvtCFs.Synchro) rdb.txRef = KvtTxRef(db: rdb) rdb.txRef diff --git a/execution_chain/db/ledger.nim b/execution_chain/db/ledger.nim index b1ac3a9f53..cc9f6ebaf7 100644 --- a/execution_chain/db/ledger.nim +++ b/execution_chain/db/ledger.nim @@ -291,7 +291,8 @@ proc persistMode(acc: AccountRef): PersistMode = proc persistCode(acc: AccountRef, ac: LedgerRef) = if acc.code.len != 0 and not acc.code.persisted: - let rc = ac.txFrame.put(acc.statement.codeHash.data, acc.code.bytes(), KvtCFs.KvtContractCode) + let rc = ac.txFrame.put( + acc.statement.codeHash.data, acc.code.bytes(), CoreDbKvtType.ContractCode) if rc.isErr: warn logTxt "persistCode()", codeHash=acc.statement.codeHash, error=($$rc.error) @@ -481,7 +482,7 @@ proc getCode*(ac: LedgerRef, acc.code = if acc.statement.codeHash != EMPTY_CODE_HASH: ac.code.get(acc.statement.codeHash).valueOr: - var rc = ac.txFrame.get(acc.statement.codeHash.data, KvtCFs.KvtContractCode) + var rc = ac.txFrame.get(acc.statement.codeHash.data, CoreDbKvtType.ContractCode) if rc.isErr: warn logTxt "getCode()", codeHash=acc.statement.codeHash, error=($$rc.error) CodeBytesRef() @@ -511,7 +512,7 @@ proc getCodeSize*(ac: LedgerRef, address: Address): int = # cached and easily accessible in the database layer - this is to prevent # EXTCODESIZE calls from messing up the code cache and thus causing # recomputation of the jump destination table - var rc = ac.txFrame.len(acc.statement.codeHash.data, KvtCFs.KvtContractCode) + var rc = ac.txFrame.len(acc.statement.codeHash.data, CoreDbKvtType.ContractCode) return rc.valueOr: warn logTxt "getCodeSize()", codeHash=acc.statement.codeHash, error=($$rc.error) diff --git a/tests/test_ledger.nim b/tests/test_ledger.nim index faf858b173..6c87dadbe1 100644 --- a/tests/test_ledger.nim +++ b/tests/test_ledger.nim @@ -492,7 +492,7 @@ proc runLedgerBasicOperationsTests() = check ac.getCode(addr2) == code let key = keccak256(code).data - val = memDB.baseTxFrame().get(key, KvtCFs.KvtContractCode).valueOr: EmptyBlob + val = memDB.baseTxFrame().get(key, KvtCFs.ContractCode).valueOr: EmptyBlob check val == code test "accessList operations": From f53e6b0adc18dba60fe6676607de1f6f592f73ab Mon Sep 17 00:00:00 2001 From: bhartnett Date: Wed, 15 Oct 2025 14:52:04 +0800 Subject: [PATCH 05/10] Update test. --- tests/test_ledger.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_ledger.nim b/tests/test_ledger.nim index 6c87dadbe1..303d607f39 100644 --- a/tests/test_ledger.nim +++ b/tests/test_ledger.nim @@ -492,7 +492,7 @@ proc runLedgerBasicOperationsTests() = check ac.getCode(addr2) == code let key = keccak256(code).data - val = memDB.baseTxFrame().get(key, KvtCFs.ContractCode).valueOr: EmptyBlob + val = memDB.baseTxFrame().get(key, CoreDbKvtType.ContractCode).valueOr: EmptyBlob check val == code test "accessList operations": From a222d26a9e473138922569d907c36faaa6f29aab Mon Sep 17 00:00:00 2001 From: bhartnett Date: Wed, 15 Oct 2025 19:33:18 +0800 Subject: [PATCH 06/10] Try fixing verified proxy CI build. --- .github/workflows/nimbus_verified_proxy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nimbus_verified_proxy.yml b/.github/workflows/nimbus_verified_proxy.yml index e7b9790ebb..4ea0ee64c2 100644 --- a/.github/workflows/nimbus_verified_proxy.yml +++ b/.github/workflows/nimbus_verified_proxy.yml @@ -72,7 +72,7 @@ jobs: - name: Run verified proxy tests run: | gcc --version - DEFAULT_MAKE_FLAGS="-j${ncpu}" + DEFAULT_MAKE_FLAGS="-j${ncpu} ROCKSDB_CI_CACHE=RocksBinCache" make ${DEFAULT_MAKE_FLAGS} nimbus_verified_proxy libverifproxy build/nimbus_verified_proxy --help # "-static" option will not work for osx unless static system libraries are provided From cb8ca0fc82664362654dd20a10a1e516851f3efa Mon Sep 17 00:00:00 2001 From: bhartnett Date: Wed, 15 Oct 2025 20:11:35 +0800 Subject: [PATCH 07/10] Fix copyright and revert CI change. --- .github/workflows/nimbus_verified_proxy.yml | 2 +- execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/nimbus_verified_proxy.yml b/.github/workflows/nimbus_verified_proxy.yml index 4ea0ee64c2..e7b9790ebb 100644 --- a/.github/workflows/nimbus_verified_proxy.yml +++ b/.github/workflows/nimbus_verified_proxy.yml @@ -72,7 +72,7 @@ jobs: - name: Run verified proxy tests run: | gcc --version - DEFAULT_MAKE_FLAGS="-j${ncpu} ROCKSDB_CI_CACHE=RocksBinCache" + DEFAULT_MAKE_FLAGS="-j${ncpu}" make ${DEFAULT_MAKE_FLAGS} nimbus_verified_proxy libverifproxy build/nimbus_verified_proxy --help # "-static" option will not work for osx unless static system libraries are provided diff --git a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim index f931264a3b..867e7f432e 100644 --- a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim +++ b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim @@ -1,5 +1,5 @@ # nimbus-eth1 -# Copyright (c) 2023-2024 Status Research & Development GmbH +# Copyright (c) 2023-2025 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or # http://www.apache.org/licenses/LICENSE-2.0) From 052123d7f77fbf81168285d0a82b102d57f34397 Mon Sep 17 00:00:00 2001 From: bhartnett Date: Wed, 15 Oct 2025 20:46:00 +0800 Subject: [PATCH 08/10] Rename KvtCfs to KvtType, move and fix module imports. --- .../db/core_db/backend/aristo_db.nim | 8 ++-- .../db/core_db/backend/aristo_rocksdb.nim | 8 ++-- execution_chain/db/core_db/base.nim | 40 +++++++++---------- execution_chain/db/core_db/base/base_desc.nim | 6 +-- execution_chain/db/core_db/core_apps.nim | 4 +- execution_chain/db/kvt.nim | 7 ++-- execution_chain/db/kvt/kvt_constants.nim | 5 +-- execution_chain/db/kvt/kvt_desc.nim | 7 +++- .../db/kvt/kvt_init/persistent.nim | 2 +- execution_chain/db/kvt/kvt_init/rocks_db.nim | 10 ++--- .../db/kvt/kvt_init/rocks_db/rdb_desc.nim | 15 +++---- .../db/kvt/kvt_init/rocks_db/rdb_get.nim | 4 +- .../db/kvt/kvt_init/rocks_db/rdb_init.nim | 2 +- .../db/kvt/kvt_init/rocks_db/rdb_put.nim | 4 +- .../db/kvt/kvt_init/rocks_db/rdb_walk.nim | 2 +- execution_chain/db/kvt_cf.nim | 2 +- execution_chain/db/ledger.nim | 6 +-- tests/test_ledger.nim | 2 +- 18 files changed, 64 insertions(+), 70 deletions(-) diff --git a/execution_chain/db/core_db/backend/aristo_db.nim b/execution_chain/db/core_db/backend/aristo_db.nim index 5764501884..bc0ae2f111 100644 --- a/execution_chain/db/core_db/backend/aristo_db.nim +++ b/execution_chain/db/core_db/backend/aristo_db.nim @@ -23,14 +23,14 @@ export base_desc # Public constructors # ------------------------------------------------------------------------------ -proc create*(dbType: CoreDbType; mpt: AristoDbRef; kvts: array[KvtCFs, KvtDbRef]): CoreDbRef = +proc create*(dbType: CoreDbType; mpt: AristoDbRef; kvts: array[KvtType, KvtDbRef]): CoreDbRef = ## Constructor helper CoreDbRef(dbType: dbType, mpt: mpt, kvts: kvts) proc newMemoryCoreDbRef*(): CoreDbRef = - var kvts: array[KvtCFs, KvtDbRef] - kvts[KvtCFs.Generic] = KvtDbRef.init() - kvts[KvtCFs.ContractCode] = KvtDbRef.init() + var kvts: array[KvtType, KvtDbRef] + kvts[KvtType.Generic] = KvtDbRef.init() + kvts[KvtType.ContractCode] = KvtDbRef.init() AristoDbMemory.create(AristoDbRef.init(), kvts) diff --git a/execution_chain/db/core_db/backend/aristo_rocksdb.nim b/execution_chain/db/core_db/backend/aristo_rocksdb.nim index bc24140134..20d7bde1e5 100644 --- a/execution_chain/db/core_db/backend/aristo_rocksdb.nim +++ b/execution_chain/db/core_db/backend/aristo_rocksdb.nim @@ -188,7 +188,7 @@ proc newRocksDbCoreDbRef*(basePath: string, opts: DbOptions): CoreDbRef = kcfOpts = opts.toCfOpts(cache, false) cfDescs = - @[($AristoCFs.VtxCF, acfOpts)] & KvtCFs.items().toSeq().mapIt(($it, kcfOpts)) + @[($AristoCFs.VtxCF, acfOpts)] & KvtType.items().toSeq().mapIt(($it, kcfOpts)) baseDb = RocksDbInstanceRef.open(basePath, dbOpts, cfDescs).expect( "Open database from " & basePath ) @@ -196,9 +196,9 @@ proc newRocksDbCoreDbRef*(basePath: string, opts: DbOptions): CoreDbRef = adb = AristoDbRef.init(opts, baseDb).valueOr: raiseAssert "Could not initialize aristo: " & $error - var kvts: array[KvtCFs, KvtDbRef] - kvts[KvtCFs.Generic] = KvtDbRef.init(baseDb, KvtCFs.Generic) - kvts[KvtCFs.ContractCode] = KvtDbRef.init(baseDb, KvtCFs.ContractCode) + var kvts: array[KvtType, KvtDbRef] + kvts[KvtType.Generic] = KvtDbRef.init(baseDb, KvtType.Generic) + kvts[KvtType.ContractCode] = KvtDbRef.init(baseDb, KvtType.ContractCode) if opts.rdbKeyCacheSize > 0: # Make sure key cache isn't empty diff --git a/execution_chain/db/core_db/base.nim b/execution_chain/db/core_db/base.nim index 413a9cfcd2..10415b1a8c 100644 --- a/execution_chain/db/core_db/base.nim +++ b/execution_chain/db/core_db/base.nim @@ -28,7 +28,7 @@ export CoreDbRef, CoreDbTxRef, CoreDbType, - CoreDbKvtType + KvtType import ../aristo/[ @@ -47,7 +47,7 @@ proc baseTxFrame*(db: CoreDbRef): CoreDbTxRef = ## up in the base txframe before being persisted to the database with a ## persist call. - var kTxs: array[CoreDbKvtType, KvtTxRef] + var kTxs: array[KvtType, KvtTxRef] for i, kvt in db.kvts: if not kvt.isNil(): kTxs[i] = kvt.baseTxFrame() @@ -58,7 +58,7 @@ proc baseTxFrame*(db: CoreDbRef): CoreDbTxRef = proc kvtBackend*(db: CoreDbRef): TypedBackendRef = ## Get KVT backend - db.kvts[CoreDbKvtType.Generic].getBackendFn() + db.kvts[KvtType.Generic].getBackendFn() # ------------------------------------------------------------------------------ # Public base descriptor methods @@ -94,7 +94,7 @@ proc persist*(db: CoreDbRef, txFrame: CoreDbTxRef) = let mptBatch = db.mpt.putBegFn() var - kvtBatches: array[CoreDbKvtType, Result[init_common.PutHdlRef, KvtError]] + kvtBatches: array[KvtType, Result[init_common.PutHdlRef, KvtError]] kvtBatchesOk = true for i, kvt in db.kvts: @@ -166,9 +166,9 @@ proc verifyProof*( # ----------- KVT --------------- -proc get*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): CoreDbRc[seq[byte]] = +proc get*(kvt: CoreDbTxRef; key: openArray[byte], kvtType = KvtType.Generic): CoreDbRc[seq[byte]] = ## This function always returns a non-empty `seq[byte]` or an error code. - let rc = kvt.kTxs[cf].get(key) + let rc = kvt.kTxs[kvtType].get(key) if rc.isOk: ok(rc.value) elif rc.error == GetNotFound: @@ -176,11 +176,11 @@ proc get*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): C else: err(rc.error.toError("")) -proc getOrEmpty*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): CoreDbRc[seq[byte]] = +proc getOrEmpty*(kvt: CoreDbTxRef; key: openArray[byte], kvtType = KvtType.Generic): CoreDbRc[seq[byte]] = ## Variant of `get()` returning an empty `seq[byte]` if the key is not found ## on the database. ## - let rc = kvt.kTxs[cf].get(key) + let rc = kvt.kTxs[kvtType].get(key) if rc.isOk: ok(rc.value) elif rc.error == GetNotFound: @@ -188,9 +188,9 @@ proc getOrEmpty*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Gene else: err(rc.error.toError("")) -proc len*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): CoreDbRc[int] = +proc len*(kvt: CoreDbTxRef; key: openArray[byte], kvtType = KvtType.Generic): CoreDbRc[int] = ## This function returns the size of the value associated with `key`. - let rc = kvt.kTxs[cf].len(key) + let rc = kvt.kTxs[kvtType].len(key) if rc.isOk: ok(rc.value) elif rc.error == GetNotFound: @@ -198,8 +198,8 @@ proc len*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): C else: err(rc.error.toError("")) -proc del*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): CoreDbRc[void] = - kvt.kTxs[cf].del(key).isOkOr: +proc del*(kvt: CoreDbTxRef; key: openArray[byte], kvtType = KvtType.Generic): CoreDbRc[void] = + kvt.kTxs[kvtType].del(key).isOkOr: return err(error.toError("")) ok() @@ -208,31 +208,31 @@ proc put*( kvt: CoreDbTxRef; key: openArray[byte]; val: openArray[byte]; - cf = CoreDbKvtType.Generic; + kvtType = KvtType.Generic; ): CoreDbRc[void] = - kvt.kTxs[cf].put(key, val).isOkOr: + kvt.kTxs[kvtType].put(key, val).isOkOr: return err(error.toError("")) ok() -proc hasKeyRc*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): CoreDbRc[bool] = +proc hasKeyRc*(kvt: CoreDbTxRef; key: openArray[byte], kvtType = KvtType.Generic): CoreDbRc[bool] = ## For the argument `key` return `true` if `get()` returned a value on ## that argument, `false` if it returned `GetNotFound`, and an error ## otherwise. ## - let rc = kvt.kTxs[cf].hasKeyRc(key).valueOr: + let rc = kvt.kTxs[kvtType].hasKeyRc(key).valueOr: return err(error.toError("")) ok(rc) -proc hasKey*(kvt: CoreDbTxRef; key: openArray[byte], cf = CoreDbKvtType.Generic): bool = +proc hasKey*(kvt: CoreDbTxRef; key: openArray[byte], kvtType = KvtType.Generic): bool = ## Simplified version of `hasKeyRc` where `false` is returned instead of ## an error. ## ## This function prototype is in line with the `hasKey` function for ## `Tables`. ## - result = kvt.kTxs[cf].hasKeyRc(key).valueOr: false + result = kvt.kTxs[kvtType].hasKeyRc(key).valueOr: false # ------------------------------------------------------------------------------ # Public methods for accounts @@ -489,7 +489,7 @@ proc txFrameBegin*(db: CoreDbRef): CoreDbTxRef = ## let aTx = db.mpt.txFrameBegin(nil, false) - var kTxs: array[CoreDbKvtType, KvtTxRef] + var kTxs: array[KvtType, KvtTxRef] for i, kvt in db.kvts: if not kvt.isNil(): kTxs[i] = kvt.txFrameBegin(nil) @@ -504,7 +504,7 @@ proc txFrameBegin*( ## let aTx = parent.aTx.db.txFrameBegin(parent.aTx, moveParentHashKeys) - var kTxs: array[CoreDbKvtType, KvtTxRef] + var kTxs: array[KvtType, KvtTxRef] for i, kTx in parent.kTxs: if not kTx.isNil(): kTxs[i] = kTx.db.txFrameBegin(kTx) diff --git a/execution_chain/db/core_db/base/base_desc.nim b/execution_chain/db/core_db/base/base_desc.nim index 07a69e1006..7a10db093c 100644 --- a/execution_chain/db/core_db/base/base_desc.nim +++ b/execution_chain/db/core_db/base/base_desc.nim @@ -49,8 +49,6 @@ type StoNotFound TxPending - CoreDbKvtType* = KvtCFs - # -------------------------------------------------- # Production descriptors # -------------------------------------------------- @@ -58,12 +56,12 @@ type ## Database descriptor dbType*: CoreDbType ## Type of database backend mpt*: AristoDbRef ## `Aristo` database - kvts*: array[CoreDbKvtType, KvtDbRef] ## `KVT` key-value tables + kvts*: array[KvtType, KvtDbRef] ## `KVT` key-value tables CoreDbTxRef* = ref object ## Transaction descriptor aTx*: AristoTxRef ## `Aristo` transaction (if any) - kTxs*: array[CoreDbKvtType, KvtTxRef] ## `KVT` transactions (if any) + kTxs*: array[KvtType, KvtTxRef] ## `KVT` transactions (if any) CoreDbError* = object ## Generic error object diff --git a/execution_chain/db/core_db/core_apps.nim b/execution_chain/db/core_db/core_apps.nim index a28c0d49e0..7d543931bd 100644 --- a/execution_chain/db/core_db/core_apps.nim +++ b/execution_chain/db/core_db/core_apps.nim @@ -642,13 +642,13 @@ proc getWitness*(db: CoreDbTxRef, blockHash: Hash32): Result[Witness, string] = Witness.decode(witnessBytes) proc getCodeByHash*(db: CoreDbTxRef, codeHash: Hash32): Result[seq[byte], string] = - let code = db.get(codeHash.data, CoreDbKvtType.ContractCode).valueOr: + let code = db.get(codeHash.data, KvtType.ContractCode).valueOr: return err("getCodeByHash: " & $$error) ok(code) proc setCodeByHash*(db: CoreDbTxRef, codeHash: Hash32, code: openArray[byte]): Result[void, string] = - db.put(codeHash.data, code, CoreDbKvtType.ContractCode).isOkOr: + db.put(codeHash.data, code, KvtType.ContractCode).isOkOr: return err("setCodeByHash: " & $$error) ok() diff --git a/execution_chain/db/kvt.nim b/execution_chain/db/kvt.nim index 93fc1ba36e..e83a037b1a 100644 --- a/execution_chain/db/kvt.nim +++ b/execution_chain/db/kvt.nim @@ -19,13 +19,11 @@ export kvt_constants import - kvt/kvt_init/memory_only, - kvt/kvt_init/rocks_db/rdb_desc + kvt/kvt_init/memory_only export MemBackendRef, finish, - init, - KvtCFs + init import kvt/kvt_desc @@ -33,6 +31,7 @@ export KvtDbRef, KvtError, KvtTxRef, + KvtType, isValid # End diff --git a/execution_chain/db/kvt/kvt_constants.nim b/execution_chain/db/kvt/kvt_constants.nim index 1d82c4901c..9568d4a395 100644 --- a/execution_chain/db/kvt/kvt_constants.nim +++ b/execution_chain/db/kvt/kvt_constants.nim @@ -8,10 +8,7 @@ # at your option. This file may not be copied, modified, or distributed # except according to those terms. -import - ../aristo/aristo_constants +import ../aristo/aristo_constants export EmptyBlob - -# End diff --git a/execution_chain/db/kvt/kvt_desc.nim b/execution_chain/db/kvt/kvt_desc.nim index 57570b4570..37e070fd93 100644 --- a/execution_chain/db/kvt/kvt_desc.nim +++ b/execution_chain/db/kvt/kvt_desc.nim @@ -79,11 +79,16 @@ type closeFn*: CloseFn ## Generic destructor getBackendFn*: GetBackendFn - + txRef*: KvtTxRef ## Tx holding data scheduled to be written to disk during the next ## `persist` call + KvtType* {.pure.} = enum + Generic = "KvtGen" ## Generic kvt + Synchro = "KvtSync" ## Syncer block headers kvt + ContractCode = "KvtCode" ## Contract code kvt + # ------------------------------------------------------------------------------ # Public helpers # ------------------------------------------------------------------------------ diff --git a/execution_chain/db/kvt/kvt_init/persistent.nim b/execution_chain/db/kvt/kvt_init/persistent.nim index eac6670844..93beaf523d 100644 --- a/execution_chain/db/kvt/kvt_init/persistent.nim +++ b/execution_chain/db/kvt/kvt_init/persistent.nim @@ -33,7 +33,7 @@ export proc init*( T: type KvtDbRef; baseDb: RocksDbInstanceRef; - cf: static[KvtCFs] = KvtCFs.Generic): T = + cf: static[KvtType] = KvtType.Generic): T = ## Generic constructor for `RocksDb` backend ## let db = rocksDbKvtBackend(baseDb, cf) diff --git a/execution_chain/db/kvt/kvt_init/rocks_db.nim b/execution_chain/db/kvt/kvt_init/rocks_db.nim index 4039d471e0..9c008c268c 100644 --- a/execution_chain/db/kvt/kvt_init/rocks_db.nim +++ b/execution_chain/db/kvt/kvt_init/rocks_db.nim @@ -69,7 +69,7 @@ proc endSession(hdl: PutHdlRef; db: RdbBackendRef): RdbPutHdlRef = # Private functions: standard interface # ------------------------------------------------------------------------------ -proc getKvpFn(db: RdbBackendRef, cf: static[KvtCFs]): GetKvpFn = +proc getKvpFn(db: RdbBackendRef, cf: static[KvtType]): GetKvpFn = result = proc(key: openArray[byte]): Result[seq[byte],KvtError] = @@ -85,7 +85,7 @@ proc getKvpFn(db: RdbBackendRef, cf: static[KvtCFs]): GetKvpFn = err(GetNotFound) -proc lenKvpFn(db: RdbBackendRef, cf: static[KvtCFs]): LenKvpFn = +proc lenKvpFn(db: RdbBackendRef, cf: static[KvtType]): LenKvpFn = result = proc(key: openArray[byte]): Result[int,KvtError] = @@ -109,7 +109,7 @@ proc putBegFn(db: RdbBackendRef): PutBegFn = ok db.newSession(db.rdb.begin()) -proc putKvpFn(db: RdbBackendRef, cf: static[KvtCFs]): PutKvpFn = +proc putKvpFn(db: RdbBackendRef, cf: static[KvtType]): PutKvpFn = result = proc(hdl: PutHdlRef; k, v: openArray[byte]) = let hdl = hdl.getSession db @@ -122,7 +122,7 @@ proc putKvpFn(db: RdbBackendRef, cf: static[KvtCFs]): PutKvpFn = return -proc putEndFn(db: RdbBackendRef, cf: static[KvtCFs]): PutEndFn = +proc putEndFn(db: RdbBackendRef, cf: static[KvtType]): PutEndFn = result = proc(hdl: PutHdlRef): Result[void,KvtError] = let hdl = hdl.endSession db @@ -157,7 +157,7 @@ proc getBackendFn(db: RdbBackendRef): GetBackendFn = # Public functions # ------------------------------------------------------------------------------ -proc rocksDbKvtBackend*(baseDb: RocksDbInstanceRef, cf: static[KvtCFs]): KvtDbRef = +proc rocksDbKvtBackend*(baseDb: RocksDbInstanceRef, cf: static[KvtType]): KvtDbRef = let be = RdbBackendRef(beKind: BackendRocksDB) db = KvtDbRef() diff --git a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim index 59a4a9221a..af7c65ce2a 100644 --- a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim +++ b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_desc.nim @@ -13,23 +13,18 @@ {.push raises: [].} -import - ../../../core_db/backend/rocksdb_desc +import ../../../core_db/backend/rocksdb_desc -export rocksdb_desc +from ../../kvt_desc import KvtType + +export rocksdb_desc, KvtType type RdbInst* = object baseDb*: RocksDbInstanceRef store*: KvtCfStore ## Rocks DB database handler - KvtCFs* {.pure.} = enum - ## Column family symbols/handles and names used on the database - Generic = "KvtGen" ## Generic column family - Synchro = "KvtSync" ## Syncer block headers column family - ContractCode = "KvtCode" ## Contract code column family - - KvtCfStore* = array[KvtCFs, ColFamilyReadWrite] + KvtCfStore* = array[KvtType, ColFamilyReadWrite] ## List of column family handlers # ------------------------------------------------------------------------------ diff --git a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_get.nim b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_get.nim index 56d9f6d458..960bc9bda2 100644 --- a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_get.nim +++ b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_get.nim @@ -36,7 +36,7 @@ when extraTraceMessages: proc get*( rdb: RdbInst; key: openArray[byte], - cf: static[KvtCFs]): Result[seq[byte],(KvtError,string)] = + cf: static[KvtType]): Result[seq[byte],(KvtError,string)] = var res: seq[byte] let onData: DataProc = proc(data: openArray[byte]) = res = @data @@ -54,7 +54,7 @@ proc get*( proc len*( rdb: RdbInst; key: openArray[byte], - cf: static[KvtCFs]): Result[int,(KvtError,string)] = + cf: static[KvtType]): Result[int,(KvtError,string)] = var res: int let onData: DataProc = proc(data: openArray[byte]) = res = data.len diff --git a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_init.nim b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_init.nim index 89a83cfb02..5b8bf93682 100644 --- a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_init.nim +++ b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_init.nim @@ -30,7 +30,7 @@ proc init*( ## rdb.baseDb = baseDb - for col in KvtCFs: + for col in KvtType: # Failure here would indicate that the database was incorrectly opened which # shouldn't happen rdb.store[col] = baseDb.db.getColFamily($col).valueOr: diff --git a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_put.nim b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_put.nim index 0a8238e159..14b82da969 100644 --- a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_put.nim +++ b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_put.nim @@ -50,7 +50,7 @@ proc rollback*(rdb: var RdbInst, session: SharedWriteBatchRef) = session.close() proc commit*( - rdb: var RdbInst, session: SharedWriteBatchRef, cf: static[KvtCFs] + rdb: var RdbInst, session: SharedWriteBatchRef, cf: static[KvtType] ): Result[void, (KvtError, string)] = if not session.isClosed(): defer: session.close() @@ -65,7 +65,7 @@ proc put*( rdb: RdbInst; session: SharedWriteBatchRef, key, val: openArray[byte]; - cf: static[KvtCFs]): Result[void,(KvtError,string)] = + cf: static[KvtType]): Result[void,(KvtError,string)] = if val.len == 0: session.batch.delete(key, rdb.store[cf].handle()).isOkOr: const errSym = RdbBeDriverDelError diff --git a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim index 867e7f432e..2001abb2a0 100644 --- a/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim +++ b/execution_chain/db/kvt/kvt_init/rocks_db/rdb_walk.nim @@ -36,7 +36,7 @@ iterator walk*(rdb: RdbInst): tuple[key: seq[byte], data: seq[byte]] = ## ## Non-decodable entries are stepped over and ignored. block walkBody: - let rit = rdb.store[KvtCFs.Generic].openIterator().valueOr: + let rit = rdb.store[KvtType.Generic].openIterator().valueOr: when extraTraceMessages: trace logTxt "walk", pfx="all", error break walkBody diff --git a/execution_chain/db/kvt_cf.nim b/execution_chain/db/kvt_cf.nim index a06e87c40f..5990763e28 100644 --- a/execution_chain/db/kvt_cf.nim +++ b/execution_chain/db/kvt_cf.nim @@ -21,6 +21,6 @@ proc synchronizerKvt*(be: TypedBackendRef): KvtTxRef = doAssert be.beKind == BackendRocksDB let baseDb = RdbBackendRef(be).getBaseDb() - rdb = rocksDbKvtBackend(baseDb, KvtCFs.Synchro) + rdb = rocksDbKvtBackend(baseDb, KvtType.Synchro) rdb.txRef = KvtTxRef(db: rdb) rdb.txRef diff --git a/execution_chain/db/ledger.nim b/execution_chain/db/ledger.nim index cc9f6ebaf7..a086929e90 100644 --- a/execution_chain/db/ledger.nim +++ b/execution_chain/db/ledger.nim @@ -292,7 +292,7 @@ proc persistMode(acc: AccountRef): PersistMode = proc persistCode(acc: AccountRef, ac: LedgerRef) = if acc.code.len != 0 and not acc.code.persisted: let rc = ac.txFrame.put( - acc.statement.codeHash.data, acc.code.bytes(), CoreDbKvtType.ContractCode) + acc.statement.codeHash.data, acc.code.bytes(), KvtType.ContractCode) if rc.isErr: warn logTxt "persistCode()", codeHash=acc.statement.codeHash, error=($$rc.error) @@ -482,7 +482,7 @@ proc getCode*(ac: LedgerRef, acc.code = if acc.statement.codeHash != EMPTY_CODE_HASH: ac.code.get(acc.statement.codeHash).valueOr: - var rc = ac.txFrame.get(acc.statement.codeHash.data, CoreDbKvtType.ContractCode) + var rc = ac.txFrame.get(acc.statement.codeHash.data, KvtType.ContractCode) if rc.isErr: warn logTxt "getCode()", codeHash=acc.statement.codeHash, error=($$rc.error) CodeBytesRef() @@ -512,7 +512,7 @@ proc getCodeSize*(ac: LedgerRef, address: Address): int = # cached and easily accessible in the database layer - this is to prevent # EXTCODESIZE calls from messing up the code cache and thus causing # recomputation of the jump destination table - var rc = ac.txFrame.len(acc.statement.codeHash.data, CoreDbKvtType.ContractCode) + var rc = ac.txFrame.len(acc.statement.codeHash.data, KvtType.ContractCode) return rc.valueOr: warn logTxt "getCodeSize()", codeHash=acc.statement.codeHash, error=($$rc.error) diff --git a/tests/test_ledger.nim b/tests/test_ledger.nim index 303d607f39..8a706dc878 100644 --- a/tests/test_ledger.nim +++ b/tests/test_ledger.nim @@ -492,7 +492,7 @@ proc runLedgerBasicOperationsTests() = check ac.getCode(addr2) == code let key = keccak256(code).data - val = memDB.baseTxFrame().get(key, CoreDbKvtType.ContractCode).valueOr: EmptyBlob + val = memDB.baseTxFrame().get(key, KvtType.ContractCode).valueOr: EmptyBlob check val == code test "accessList operations": From da93e38e05235b92cc12a9c315fef83df983da7e Mon Sep 17 00:00:00 2001 From: bhartnett Date: Wed, 15 Oct 2025 21:26:01 +0800 Subject: [PATCH 09/10] Fix copyright. --- execution_chain/db/kvt/kvt_constants.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/execution_chain/db/kvt/kvt_constants.nim b/execution_chain/db/kvt/kvt_constants.nim index 9568d4a395..6aebdd24d4 100644 --- a/execution_chain/db/kvt/kvt_constants.nim +++ b/execution_chain/db/kvt/kvt_constants.nim @@ -1,5 +1,5 @@ # nimbus-eth1 -# Copyright (c) 2023 Status Research & Development GmbH +# Copyright (c) 2023-2025 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or # http://www.apache.org/licenses/LICENSE-2.0) From 5a7eb0afef6c18120eeb21d357cdbe51598abee4 Mon Sep 17 00:00:00 2001 From: bhartnett Date: Thu, 16 Oct 2025 10:41:20 +0800 Subject: [PATCH 10/10] Use separate column family for witness storage. --- execution_chain/common/common.nim | 4 +++- execution_chain/db/core_db/backend/aristo_db.nim | 1 + .../db/core_db/backend/aristo_rocksdb.nim | 8 +++----- execution_chain/db/core_db/core_apps.nim | 4 ++-- execution_chain/db/kvt/kvt_desc.nim | 1 + execution_chain/db/storage_types.nim | 14 ++------------ 6 files changed, 12 insertions(+), 20 deletions(-) diff --git a/execution_chain/common/common.nim b/execution_chain/common/common.nim index c24b4d5e3e..b9d5e910db 100644 --- a/execution_chain/common/common.nim +++ b/execution_chain/common/common.nim @@ -172,7 +172,9 @@ proc init(com : CommonRef, initializeDb: bool, statelessProviderEnabled: bool, statelessWitnessValidation: bool) = - + # When stateless is not enabled, disable the witness kvt which is created by default. + if not statelessProviderEnabled: + db.kvts[KvtType.Witness] = nil config.daoCheck() diff --git a/execution_chain/db/core_db/backend/aristo_db.nim b/execution_chain/db/core_db/backend/aristo_db.nim index bc0ae2f111..df5e953c4c 100644 --- a/execution_chain/db/core_db/backend/aristo_db.nim +++ b/execution_chain/db/core_db/backend/aristo_db.nim @@ -31,6 +31,7 @@ proc newMemoryCoreDbRef*(): CoreDbRef = var kvts: array[KvtType, KvtDbRef] kvts[KvtType.Generic] = KvtDbRef.init() kvts[KvtType.ContractCode] = KvtDbRef.init() + kvts[KvtType.Witness] = KvtDbRef.init() AristoDbMemory.create(AristoDbRef.init(), kvts) diff --git a/execution_chain/db/core_db/backend/aristo_rocksdb.nim b/execution_chain/db/core_db/backend/aristo_rocksdb.nim index 20d7bde1e5..9f882a005a 100644 --- a/execution_chain/db/core_db/backend/aristo_rocksdb.nim +++ b/execution_chain/db/core_db/backend/aristo_rocksdb.nim @@ -183,22 +183,20 @@ proc newRocksDbCoreDbRef*(basePath: string, opts: DbOptions): CoreDbRef = nil dbOpts = opts.toDbOpts() acfOpts = opts.toCfOpts(cache, true) - # The KVT is is not bulk-flushed so we have to use a skiplist memtable for - # it + # The KVT is is not bulk-flushed so we have to use a skiplist memtable for it kcfOpts = opts.toCfOpts(cache, false) cfDescs = @[($AristoCFs.VtxCF, acfOpts)] & KvtType.items().toSeq().mapIt(($it, kcfOpts)) baseDb = RocksDbInstanceRef.open(basePath, dbOpts, cfDescs).expect( - "Open database from " & basePath - ) - + "Open database from " & basePath) adb = AristoDbRef.init(opts, baseDb).valueOr: raiseAssert "Could not initialize aristo: " & $error var kvts: array[KvtType, KvtDbRef] kvts[KvtType.Generic] = KvtDbRef.init(baseDb, KvtType.Generic) kvts[KvtType.ContractCode] = KvtDbRef.init(baseDb, KvtType.ContractCode) + kvts[KvtType.Witness] = KvtDbRef.init(baseDb, KvtType.Witness) if opts.rdbKeyCacheSize > 0: # Make sure key cache isn't empty diff --git a/execution_chain/db/core_db/core_apps.nim b/execution_chain/db/core_db/core_apps.nim index 7d543931bd..9104724e2e 100644 --- a/execution_chain/db/core_db/core_apps.nim +++ b/execution_chain/db/core_db/core_apps.nim @@ -631,12 +631,12 @@ proc persistUncles*(db: CoreDbTxRef, uncles: openArray[Header]): Hash32 = return EMPTY_ROOT_HASH proc persistWitness*(db: CoreDbTxRef, blockHash: Hash32, witness: Witness): Result[void, string] = - db.put(blockHashToWitnessKey(blockHash).toOpenArray, witness.encode()).isOkOr: + db.put(blockHash.data, witness.encode(), KvtType.Witness).isOkOr: return err("persistWitness: " & $$error) ok() proc getWitness*(db: CoreDbTxRef, blockHash: Hash32): Result[Witness, string] = - let witnessBytes = db.get(blockHashToWitnessKey(blockHash).toOpenArray).valueOr: + let witnessBytes = db.get(blockHash.data, KvtType.Witness).valueOr: return err("getWitness: " & $$error) Witness.decode(witnessBytes) diff --git a/execution_chain/db/kvt/kvt_desc.nim b/execution_chain/db/kvt/kvt_desc.nim index 37e070fd93..af71751058 100644 --- a/execution_chain/db/kvt/kvt_desc.nim +++ b/execution_chain/db/kvt/kvt_desc.nim @@ -88,6 +88,7 @@ type Generic = "KvtGen" ## Generic kvt Synchro = "KvtSync" ## Syncer block headers kvt ContractCode = "KvtCode" ## Contract code kvt + Witness = "KvtWitness" ## Witness kvt # ------------------------------------------------------------------------------ # Public helpers diff --git a/execution_chain/db/storage_types.nim b/execution_chain/db/storage_types.nim index bffeb8341e..c00dbde1c5 100644 --- a/execution_chain/db/storage_types.nim +++ b/execution_chain/db/storage_types.nim @@ -22,13 +22,13 @@ type transactionHashToBlock = 3 canonicalHeadHash = 4 slotHashToSlot = 5 - contractHash = 6 + contractHash = 6 # deprecated - separate KvtType used instead dataDirId = 7 fcuNumAndHash = 8 fcState = 9 beaconHeader = 10 wdKey = 11 - witness = 12 + witness = 12 # deprecated - separate KvtType used instead DbKey* = object # The first byte stores the key type. The rest are key-specific values @@ -71,11 +71,6 @@ func slotHashToSlotKey*(h: Hash32): DbKey {.inline.} = result.data[1 .. 32] = h.data() result.dataEndPos = uint8 32 -func contractHashKey*(h: Hash32): DbKey {.inline.} = - result.data[0] = byte ord(contractHash) - result.data[1 .. 32] = h.data - result.dataEndPos = uint8 32 - template uint64KeyImpl(keyEnum) = result.data[0] = byte ord(keyEnum) doAssert sizeof(u) <= 32 @@ -105,11 +100,6 @@ func withdrawalsKey*(h: Hash32): DbKey {.inline.} = result.data[1 .. 32] = h.data result.dataEndPos = uint8 32 -func blockHashToWitnessKey*(h: Hash32): DbKey {.inline.} = - result.data[0] = byte ord(witness) - result.data[1 .. 32] = h.data - result.dataEndPos = uint8 32 - template toOpenArray*(k: DbKey): openArray[byte] = k.data.toOpenArray(0, int(k.dataEndPos))