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 cd656d19eb..df5e953c4c 100644 --- a/execution_chain/db/core_db/backend/aristo_db.nim +++ b/execution_chain/db/core_db/backend/aristo_db.nim @@ -23,14 +23,17 @@ export base_desc # Public constructors # ------------------------------------------------------------------------------ -proc create*(dbType: CoreDbType; kvt: KvtDbRef; mpt: AristoDbRef): CoreDbRef = +proc create*(dbType: CoreDbType; mpt: AristoDbRef; kvts: array[KvtType, 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[KvtType, KvtDbRef] + kvts[KvtType.Generic] = KvtDbRef.init() + kvts[KvtType.ContractCode] = KvtDbRef.init() + kvts[KvtType.Witness] = 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..9f882a005a 100644 --- a/execution_chain/db/core_db/backend/aristo_rocksdb.nim +++ b/execution_chain/db/core_db/backend/aristo_rocksdb.nim @@ -183,19 +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)] & 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 - ) - + "Open database from " & basePath) adb = AristoDbRef.init(opts, baseDb).valueOr: raiseAssert "Could not initialize aristo: " & $error - kdb = KvtDbRef.init(baseDb) + + 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 @@ -203,7 +204,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..10415b1a8c 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, + KvtType 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[KvtType, 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[KvtType.Generic].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[KvtType, 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], kvtType = KvtType.Generic): 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[kvtType].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], 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.kTx.get(key) + let rc = kvt.kTxs[kvtType].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], kvtType = KvtType.Generic): CoreDbRc[int] = ## This function returns the size of the value associated with `key`. - let rc = kvt.kTx.len(key) + let rc = kvt.kTxs[kvtType].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], kvtType = KvtType.Generic): CoreDbRc[void] = + kvt.kTxs[kvtType].del(key).isOkOr: return err(error.toError("")) ok() @@ -185,30 +208,31 @@ proc put*( kvt: CoreDbTxRef; key: openArray[byte]; val: openArray[byte]; + kvtType = KvtType.Generic; ): CoreDbRc[void] = - kvt.kTx.put(key, val).isOkOr: + kvt.kTxs[kvtType].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], 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.kTx.hasKeyRc(key).valueOr: + let rc = kvt.kTxs[kvtType].hasKeyRc(key).valueOr: return err(error.toError("")) ok(rc) -proc hasKey*(kvt: CoreDbTxRef; key: openArray[byte]): 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.kTx.hasKeyRc(key).valueOr: false + result = kvt.kTxs[kvtType].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[KvtType, 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[KvtType, 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..7a10db093c 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[KvtType, 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[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 299e0dbfc1..9104724e2e 100644 --- a/execution_chain/db/core_db/core_apps.nim +++ b/execution_chain/db/core_db/core_apps.nim @@ -631,24 +631,24 @@ 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) proc getCodeByHash*(db: CoreDbTxRef, codeHash: Hash32): Result[seq[byte], string] = - let code = db.get(contractHashKey(codeHash).toOpenArray).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(contractHashKey(codeHash).toOpenArray, code).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 3940e0272b..e83a037b1a 100644 --- a/execution_chain/db/kvt.nim +++ b/execution_chain/db/kvt.nim @@ -31,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..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) @@ -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..af71751058 100644 --- a/execution_chain/db/kvt/kvt_desc.nim +++ b/execution_chain/db/kvt/kvt_desc.nim @@ -79,11 +79,17 @@ 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 + Witness = "KvtWitness" ## Witness kvt + # ------------------------------------------------------------------------------ # Public helpers # ------------------------------------------------------------------------------ diff --git a/execution_chain/db/kvt/kvt_init/persistent.nim b/execution_chain/db/kvt/kvt_init/persistent.nim index 2c9c41253d..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] = KvtGeneric): 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 d1ee920668..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,22 +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* = enum - ## Column family symbols/handles and names used on the database - KvtGeneric = "KvtGen" ## Generic column family - KvtSynchro = "KvtSync" - - 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 213537969b..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 @@ -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) @@ -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[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 7494eb9e92..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, KvtSynchro) + 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 3058b21f5e..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( - contractHashKey(acc.statement.codeHash).toOpenArray, acc.code.bytes()) + 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(contractHashKey(acc.statement.codeHash).toOpenArray) + 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(contractHashKey(acc.statement.codeHash).toOpenArray) + 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/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)) diff --git a/tests/test_ledger.nim b/tests/test_ledger.nim index a37cff3d00..8a706dc878 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, KvtType.ContractCode).valueOr: EmptyBlob check val == code test "accessList operations":