From fe0fb8b983876dd8befd873e03947d783650bc07 Mon Sep 17 00:00:00 2001 From: Jordan Hrycaj Date: Fri, 16 Aug 2024 16:16:17 +0100 Subject: [PATCH 1/4] bump metrics --- vendor/nim-metrics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/nim-metrics b/vendor/nim-metrics index 4337ccd62c..29bb7ba63c 160000 --- a/vendor/nim-metrics +++ b/vendor/nim-metrics @@ -1 +1 @@ -Subproject commit 4337ccd62c0b7d57492402dd4cb838ddc0c78a84 +Subproject commit 29bb7ba63cd884770169891687595348a70cf166 From aba7a6d2c432e8f351e6c5d0bd2526055048248a Mon Sep 17 00:00:00 2001 From: Jordan Hrycaj Date: Fri, 23 Aug 2024 09:24:07 +0100 Subject: [PATCH 2/4] Remove cruft --- README.md | 6 ------ nimbus/config.nim | 13 ------------- nimbus/sync/beacon.nim | 2 +- nimbus/sync/sync_desc.nim | 1 - nimbus/sync/sync_sched.nim | 4 +--- 5 files changed, 2 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 5d0e9b2e8a..ad8fd290bb 100644 --- a/README.md +++ b/README.md @@ -219,12 +219,6 @@ available.) For these variables, using <variable>=0 is ignored and <variable>=2 has the same effect as <variable>=1 (ditto for other numbers.) -Other settings where the non-zero value matters: - - * ENABLE_ETH_VERSION=66
- Enable legacy protocol `eth66` (or another available protocol version.) - - ### Development tips Interesting Make variables and targets are documented in the [nimbus-build-system](https://github.com/status-im/nimbus-build-system) repo. diff --git a/nimbus/config.nim b/nimbus/config.nim index c1d7270c6d..66c91cb52d 100644 --- a/nimbus/config.nim +++ b/nimbus/config.nim @@ -183,14 +183,6 @@ type abbr: "y" name: "sync-mode" .}: SyncMode - syncCtrlFile* {. - desc: "Specify a file that is regularly checked for updates. If it " & - "exists it is checked for whether it contains extra information " & - "specific to the type of sync process. This option is primarily " & - "intended only for sync testing and debugging." - abbr: "z" - name: "sync-ctrl-file" }: Option[string] - importKey* {. desc: "Import unencrypted 32 bytes hex private key from a file" defaultValue: "" @@ -858,11 +850,6 @@ proc makeConfig*(cmdLine = commandLineParams()): NimbusConf result.dataDir.string != defaultDataDir(): result.keyStore = OutDir(result.dataDir.string / "keystore") - # For consistency - if result.syncCtrlFile.isSome and result.syncCtrlFile.unsafeGet == "": - error "Argument missing", option="sync-ctrl-file" - quit QuitFailure - when isMainModule: # for testing purpose discard makeConfig() diff --git a/nimbus/sync/beacon.nim b/nimbus/sync/beacon.nim index e4f0626078..61622aff83 100644 --- a/nimbus/sync/beacon.nim +++ b/nimbus/sync/beacon.nim @@ -132,7 +132,7 @@ proc init*( maxPeers: int; id: int = 0): T = new result - result.initSync(ethNode, chain, maxPeers, Opt.none(string)) + result.initSync(ethNode, chain, maxPeers) result.ctx.pool.rng = rng result.ctx.pool.id = id diff --git a/nimbus/sync/sync_desc.nim b/nimbus/sync/sync_desc.nim index 20f54cb152..a94b14c930 100644 --- a/nimbus/sync/sync_desc.nim +++ b/nimbus/sync/sync_desc.nim @@ -49,7 +49,6 @@ type chain*: ChainRef ## Block chain database (no need for `Peer`) poolMode*: bool ## Activate `runPool()` workers if set `true` daemon*: bool ## Enable global background job - exCtrlFile*: Opt[string] ## Extra instructions file (if any) pool*: S ## Shared context for all worker peers # ------------------------------------------------------------------------------ diff --git a/nimbus/sync/sync_sched.nim b/nimbus/sync/sync_sched.nim index 12ffe97877..ff07fdc5f1 100644 --- a/nimbus/sync/sync_sched.nim +++ b/nimbus/sync/sync_sched.nim @@ -383,9 +383,8 @@ proc onPeerDisconnected[S,W](dsc: RunnerSyncRef[S,W], peer: Peer) = proc initSync*[S,W]( dsc: RunnerSyncRef[S,W]; node: EthereumNode; - chain: ChainRef, + chain: ChainRef; slots: int; - exCtrlFile = Opt.none(string); ) = ## Constructor # Leave one extra slot so that it can holds a *zombie* even if all slots @@ -394,7 +393,6 @@ proc initSync*[S,W]( dsc.ctx = CtxRef[S]( ethWireCtx: cast[EthWireRef](node.protocolState protocol.eth), buddiesMax: max(1, slots + 1), - exCtrlFile: exCtrlFile, chain: chain) dsc.pool = node.peerPool dsc.buddies.init(dsc.ctx.buddiesMax) From bb6512d1a2a7670898e6501c679ed117b6f70960 Mon Sep 17 00:00:00 2001 From: Jordan Hrycaj Date: Fri, 16 Aug 2024 16:16:17 +0100 Subject: [PATCH 3/4] Cosmetics, update some logging, noise control --- nimbus/db/core_db/core_apps.nim | 125 +++++++++++++++----------------- nimbus/sync/handlers/eth.nim | 6 +- nimbus/sync/protocol/eth68.nim | 14 +++- nimbus/sync/protocol/snap1.nim | 3 + 4 files changed, 81 insertions(+), 67 deletions(-) diff --git a/nimbus/db/core_db/core_apps.nim b/nimbus/db/core_db/core_apps.nim index 845be531f5..59e0f924be 100644 --- a/nimbus/db/core_db/core_apps.nim +++ b/nimbus/db/core_db/core_apps.nim @@ -73,14 +73,11 @@ proc getCanonicalHeaderHash*(db: CoreDbRef): Opt[Hash256] {.gcsafe.} # Private helpers # ------------------------------------------------------------------------------ -template logTxt(info: static[string]): static[string] = - "Core app " & info - template discardRlpException(info: static[string]; code: untyped) = try: code except RlpError as e: - warn logTxt info, error=($e.name), msg=e.msg + warn info, error=($e.name), err=e.msg, errName=e.name # ------------------------------------------------------------------------------ # Private iterators @@ -104,7 +101,8 @@ iterator findNewAncestors( break else: if not db.getBlockHeader(h.parentHash, h): - warn logTxt "Could not find parent while iterating", hash = h.parentHash + warn "findNewAncestors(): Could not find parent while iterating", + hash = h.parentHash break # ------------------------------------------------------------------------------ @@ -123,8 +121,7 @@ iterator getBlockTransactionData*( for idx in 0'u16.. true/false proc getSavedStateBlockNumber*( db: CoreDbRef; @@ -323,7 +322,7 @@ proc getBlockHeader*( const info = "getBlockHeader()" let data = db.ctx.getKvt().get(genericHashKey(blockHash).toOpenArray).valueOr: if error.error != KvtNotFound: - warn logTxt info, blockHash, action="get()", error=($$error) + warn info, blockHash, error=($$error) return false discardRlpException info: @@ -345,15 +344,16 @@ proc getHash( db: CoreDbRef; key: DbKey; ): Opt[Hash256] = + const info = "getHash()" let data = db.ctx.getKvt().get(key.toOpenArray).valueOr: if error.error != KvtNotFound: - warn logTxt "getHash()", key, action="get()", error=($$error) + warn info, key, error=($$error) return Opt.none(Hash256) try: Opt.some(rlp.decode(data, Hash256)) except RlpError as exc: - warn logTxt "getHash()", key, action="rlp.decode()", error=exc.msg + warn info, key, error=exc.msg Opt.none(Hash256) proc getCanonicalHeaderHash*(db: CoreDbRef): Opt[Hash256] = @@ -441,22 +441,23 @@ proc getScore*( db: CoreDbRef; blockHash: Hash256; ): Opt[UInt256] = + const info = "getScore()" let data = db.ctx.getKvt() .get(blockHashToScoreKey(blockHash).toOpenArray).valueOr: if error.error != KvtNotFound: - warn logTxt "getScore()", blockHash, action="get()", error=($$error) + warn info, blockHash, error=($$error) return Opt.none(UInt256) try: Opt.some(rlp.decode(data, UInt256)) except RlpError as exc: - warn logTxt "getScore()", data = data.toHex(), error=exc.msg + warn info, data = data.toHex(), error=exc.msg Opt.none(UInt256) proc setScore*(db: CoreDbRef; blockHash: Hash256, score: UInt256) = ## for testing purpose let scoreKey = blockHashToScoreKey blockHash db.ctx.getKvt.put(scoreKey.toOpenArray, rlp.encode(score)).isOkOr: - warn logTxt "setScore()", scoreKey, action="put()", error=($$error) + warn "setScore()", scoreKey, error=($$error) return proc getTd*(db: CoreDbRef; blockHash: Hash256, td: var UInt256): bool = @@ -491,8 +492,7 @@ proc addBlockNumberToHashLookup*( db: CoreDbRef; blockNumber: BlockNumber, blockHash: Hash256) = let blockNumberKey = blockNumberToHashKey(blockNumber) db.ctx.getKvt.put(blockNumberKey.toOpenArray, rlp.encode(blockHash)).isOkOr: - warn logTxt "addBlockNumberToHashLookup()", - blockNumberKey, action="put()", error=($$error) + warn "addBlockNumberToHashLookup", blockNumberKey, error=($$error) proc persistTransactions*( db: CoreDbRef; @@ -515,10 +515,10 @@ proc persistTransactions*( txKey: TransactionKey = (blockNumber, idx.uint) key = hashIndexKey(txRoot, idx.uint16) kvt.put(key, encodedTx).isOkOr: - warn logTxt info, idx, action="put()", error=($$error) + warn info, idx, error=($$error) return kvt.put(blockKey.toOpenArray, rlp.encode(txKey)).isOkOr: - trace logTxt info, blockKey, action="put()", error=($$error) + trace info, blockKey, error=($$error) return proc forgetHistory*( @@ -551,17 +551,15 @@ proc getTransactionByIndex*( let kvt = db.ctx.getKvt() let key = hashIndexKey(txRoot, txIndex) let txData = kvt.getOrEmpty(key).valueOr: - warn logTxt "getTransaction()", - txRoot, key, action="getOrEmpty()", error=($$error) + warn info, txRoot, key, error=($$error) return false if txData.len == 0: return false try: res = rlp.decode(txData, Transaction) - except RlpError as exc: - warn logTxt info, - txRoot, action="rlp.decode()", error=exc.msg + except RlpError as e: + warn info, txRoot, err=e.msg, errName=e.name return false true @@ -577,8 +575,7 @@ proc getTransactionCount*( while true: let key = hashIndexKey(txRoot, txCount) let yes = kvt.hasKey(key).valueOr: - warn logTxt info, - txRoot, key, action="hasKey()", error=($$error) + warn info, txRoot, key, error=($$error) return 0 if yes: inc txCount @@ -598,7 +595,7 @@ proc getUnclesCount*( let key = genericHashKey(ommersHash) db.ctx.getKvt().get(key.toOpenArray).valueOr: if error.error == KvtNotFound: - warn logTxt info, ommersHash, action="get()", `error`=($$error) + warn info, ommersHash, error=($$error) return 0 return rlpFromBytes(encodedUncles).listLen @@ -613,7 +610,7 @@ proc getUncles*( let key = genericHashKey(ommersHash) db.ctx.getKvt().get(key.toOpenArray).valueOr: if error.error == KvtNotFound: - warn logTxt info, ommersHash, action="get()", `error`=($$error) + warn info, ommersHash, error=($$error) return @[] return rlp.decode(encodedUncles, seq[BlockHeader]) @@ -629,7 +626,7 @@ proc persistWithdrawals*( for idx, wd in withdrawals: let key = hashIndexKey(withdrawalsRoot, idx.uint16) kvt.put(key, rlp.encode(wd)).isOkOr: - warn logTxt info, idx, action="put()", error=($$error) + warn info, idx, error=($$error) return proc getWithdrawals*( @@ -726,8 +723,7 @@ proc getUncleHashes*( key = genericHashKey(header.ommersHash) encodedUncles = db.ctx.getKvt().get(key.toOpenArray).valueOr: if error.error == KvtNotFound: - warn logTxt "getUncleHashes()", - ommersHash=header.ommersHash, action="get()", `error`=($$error) + warn "getUncleHashes()", ommersHash=header.ommersHash, error=($$error) return @[] return rlp.decode(encodedUncles, seq[BlockHeader]).mapIt(it.rlpHash) @@ -740,8 +736,7 @@ proc getTransactionKey*( txKey = transactionHashToBlockKey(transactionHash) tx = db.ctx.getKvt().get(txKey.toOpenArray).valueOr: if error.error == KvtNotFound: - warn logTxt "getTransactionKey()", - transactionHash, action="get()", `error`=($$error) + warn "getTransactionKey()", transactionHash, error=($$error) return (0.BlockNumber, 0) let key = rlp.decode(tx, TransactionKey) (key.blockNumber, key.index.uint64) @@ -749,8 +744,9 @@ proc getTransactionKey*( proc headerExists*(db: CoreDbRef; blockHash: Hash256): bool = ## Returns True if the header with the given block hash is in our DB. db.ctx.getKvt().hasKey(genericHashKey(blockHash).toOpenArray).valueOr: - warn logTxt "headerExists()", blockHash, action="get()", `error`=($$error) + warn "headerExists()", blockHash, error=($$error) return false + # => true/false proc setHead*( db: CoreDbRef; @@ -765,7 +761,7 @@ proc setHead*( let canonicalHeadHash = canonicalHeadHashKey() db.ctx.getKvt.put(canonicalHeadHash.toOpenArray, rlp.encode(blockHash)).isOkOr: - warn logTxt "setHead()", canonicalHeadHash, action="put()", error=($$error) + warn "setHead()", canonicalHeadHash, error=($$error) return true proc setHead*( @@ -773,17 +769,18 @@ proc setHead*( header: BlockHeader; writeHeader = false; ): bool = + const info = "setHead()" var headerHash = rlpHash(header) let kvt = db.ctx.getKvt() if writeHeader: kvt.put(genericHashKey(headerHash).toOpenArray, rlp.encode(header)).isOkOr: - warn logTxt "setHead()", headerHash, action="put()", error=($$error) + warn info, headerHash, error=($$error) return false if not db.markCanonicalChain(header, headerHash): return false let canonicalHeadHash = canonicalHeadHashKey() kvt.put(canonicalHeadHash.toOpenArray, rlp.encode(headerHash)).isOkOr: - warn logTxt "setHead()", canonicalHeadHash, action="put()", error=($$error) + warn info, canonicalHeadHash, error=($$error) return false true @@ -800,7 +797,7 @@ proc persistReceipts*( for idx, rec in receipts: let key = hashIndexKey(receiptsRoot, idx.uint16) kvt.put(key, rlp.encode(rec)).isOkOr: - warn logTxt info, idx, action="merge()", error=($$error) + warn info, idx, error=($$error) proc getReceipts*( db: CoreDbRef; @@ -817,12 +814,13 @@ proc persistScore*( blockHash: Hash256; score: UInt256 ): bool = + const + info = "persistScore" let kvt = db.ctx.getKvt() scoreKey = blockHashToScoreKey(blockHash) kvt.put(scoreKey.toOpenArray, rlp.encode(score)).isOkOr: - warn logTxt "persistHeader()", - scoreKey, action="put()", `error`=($$error) + warn info, scoreKey, error=($$error) return true @@ -832,18 +830,18 @@ proc persistHeader*( header: BlockHeader; startOfHistory = GENESIS_PARENT_HASH; ): bool = + const + info = "persistHeader" let kvt = db.ctx.getKvt() isStartOfHistory = header.parentHash == startOfHistory if not isStartOfHistory and not db.headerExists(header.parentHash): - warn logTxt "persistHeaderWithoutSetHead()", - blockHash, action="headerExists(parent)" + warn info & ": parent header missing", blockNumber=header.number return false kvt.put(genericHashKey(blockHash).toOpenArray, rlp.encode(header)).isOkOr: - warn logTxt "persistHeaderWithoutSetHead()", - blockHash, action="put()", `error`=($$error) + warn info, blockHash, blockNumber=header.number, error=($$error) return false let @@ -907,8 +905,7 @@ proc persistUncles*(db: CoreDbRef, uncles: openArray[BlockHeader]): Hash256 = let enc = rlp.encode(uncles) result = keccakHash(enc) db.ctx.getKvt.put(genericHashKey(result).toOpenArray, enc).isOkOr: - warn logTxt "persistUncles()", - unclesHash=result, action="put()", `error`=($$error) + warn "persistUncles()", unclesHash=result, error=($$error) return EMPTY_ROOT_HASH @@ -918,8 +915,7 @@ proc safeHeaderHash*(db: CoreDbRef): Hash256 = proc safeHeaderHash*(db: CoreDbRef, headerHash: Hash256) = let safeHashKey = safeHashKey() db.ctx.getKvt.put(safeHashKey.toOpenArray, rlp.encode(headerHash)).isOkOr: - warn logTxt "safeHeaderHash()", - safeHashKey, action="put()", `error`=($$error) + warn "safeHeaderHash()", safeHashKey, error=($$error) return proc finalizedHeaderHash*( @@ -930,8 +926,7 @@ proc finalizedHeaderHash*( proc finalizedHeaderHash*(db: CoreDbRef, headerHash: Hash256) = let finalizedHashKey = finalizedHashKey() db.ctx.getKvt.put(finalizedHashKey.toOpenArray, rlp.encode(headerHash)).isOkOr: - warn logTxt "finalizedHeaderHash()", - finalizedHashKey, action="put()", `error`=($$error) + warn "finalizedHeaderHash()", finalizedHashKey, error=($$error) return proc safeHeader*( diff --git a/nimbus/sync/handlers/eth.nim b/nimbus/sync/handlers/eth.nim index 4d0d40d6b2..9fe4895f9c 100644 --- a/nimbus/sync/handlers/eth.nim +++ b/nimbus/sync/handlers/eth.nim @@ -36,6 +36,9 @@ type lastCleanup: Time const + extraTraceMessages = false + ## Enabled additional logging noise + txpool_enabled = defined(enable_txpool_in_synchronizer) when txpool_enabled: @@ -448,7 +451,8 @@ method handleAnnouncedTxsHashes*( txSizes: openArray[int]; txHashes: openArray[Hash256]; ): Result[void, string] = - trace "Wire handler ignoring txs hashes", nHashes=txHashes.len + when extraTraceMessages: + trace "Wire handler ignoring txs hashes", nHashes=txHashes.len ok() method handleNewBlock*(ctx: EthWireRef, diff --git a/nimbus/sync/protocol/eth68.nim b/nimbus/sync/protocol/eth68.nim index d348cadc9a..a2fec0f1ef 100644 --- a/nimbus/sync/protocol/eth68.nim +++ b/nimbus/sync/protocol/eth68.nim @@ -12,6 +12,9 @@ ## This module implements Ethereum Wire Protocol version 67, `eth/67`. ## Specification: ## `eth/68 `_ +## +## Use NIM command line optipn `-d:p2pProtocolDebug` for dumping the +## generated driver code (just to have it stored somewhere lest one forgets.) import stint, @@ -77,6 +80,15 @@ template handleHandlerError(x: untyped) = if x.isErr: raise newException(EthP2PError, x.error) +when trEthTraceGossipOk: + import std/[sequtils,strutils] + + func toStr(w: openArray[int]): string = + func toStr(n: int): string = + if n == 0: "0" + else: n.toHex.strip(trailing=false,chars={'0'}).toLowerAscii + w.mapIt(it.toStr).join(":") + p2pProtocol eth68(version = ethVersion, rlpxName = "eth", peerState = EthPeerState, @@ -244,7 +256,7 @@ p2pProtocol eth68(version = ethVersion, ) = when trEthTraceGossipOk: trace trEthRecvReceived & "NewPooledTransactionHashes (0x08)", peer, - txTypes=txTypes.toHex, txSizes, + txTypes=txTypes.toHex, txSizes=txSizes.toStr, hashes=txHashes.len let ctx = peer.networkState() diff --git a/nimbus/sync/protocol/snap1.nim b/nimbus/sync/protocol/snap1.nim index 2a33290357..e9f54a584b 100644 --- a/nimbus/sync/protocol/snap1.nim +++ b/nimbus/sync/protocol/snap1.nim @@ -12,6 +12,9 @@ ## This module implements Ethereum Snapshot Protocol version 1, `snap/1`. ## Specification: ## `snap/1 `_ +## +## Use NIM command line optipn `-d:p2pProtocolDebug` for dumping the +## generated driver code (just to have it stored somewhere lest one forgets.) import std/options, From 52a273d4e7eebacbecb982621b29df483f29cbc2 Mon Sep 17 00:00:00 2001 From: Jordan Hrycaj Date: Fri, 30 Aug 2024 09:41:53 +0100 Subject: [PATCH 4/4] Renamed `CoreDb` function `hasKey` => `hasKeyRc` and provided `hasKey` why: Currently, `hasKey` returns a `Result[]` rather than a `bool` which is what one would expect from a function prototype of this name. This was a bit of an annoyance and cost unnecessary attention. --- nimbus/common/common.nim | 2 +- nimbus/db/core_db/backend/aristo_trace.nim | 4 ++-- nimbus/db/core_db/base.nim | 21 +++++++++++++++++---- nimbus/db/core_db/base/api_tracking.nim | 1 + nimbus/db/core_db/core_apps.nim | 6 +++--- nimbus/db/kvt/kvt_api.nim | 18 +++++++++--------- nimbus/db/kvt/kvt_utils.nim | 15 ++++++++++++--- premix/persist.nim | 2 +- 8 files changed, 46 insertions(+), 23 deletions(-) diff --git a/nimbus/common/common.nim b/nimbus/common/common.nim index a0452cd780..dd119c0ca7 100644 --- a/nimbus/common/common.nim +++ b/nimbus/common/common.nim @@ -122,7 +122,7 @@ func daoCheck(conf: ChainConfig) = proc initializeDb(com: CommonRef) = let kvt = com.db.ctx.getKvt() proc contains(kvt: CoreDbKvtRef; key: openArray[byte]): bool = - kvt.hasKey(key).expect "valid bool" + kvt.hasKeyRc(key).expect "valid bool" if canonicalHeadHashKey().toOpenArray notin kvt: info "Writing genesis to DB" doAssert(com.genesisHeader.number == 0.BlockNumber, diff --git a/nimbus/db/core_db/backend/aristo_trace.nim b/nimbus/db/core_db/backend/aristo_trace.nim index 8a9e17f4e4..a9244ec47d 100644 --- a/nimbus/db/core_db/backend/aristo_trace.nim +++ b/nimbus/db/core_db/backend/aristo_trace.nim @@ -433,7 +433,7 @@ proc kvtTraceRecorder(tr: TraceRecorderRef) = # Find entry on DB let - hasKey = api.hasKey(kvt, key).valueOr: + hasKey = api.hasKeyRc(kvt, key).valueOr: when CoreDbNoisyCaptJournal: debug logTxt $info, level, key=($$key), error tr.jLogger(key, logRecord(info, TrqAdd, error)) @@ -455,7 +455,7 @@ proc kvtTraceRecorder(tr: TraceRecorderRef) = assert tr.kvtSave != tr.db.kvtApi assert tr.kvtSave.del != tr.db.kvtApi.del - assert tr.kvtSave.hasKey == tr.db.kvtApi.hasKey + assert tr.kvtSave.hasKeyRc == tr.db.kvtApi.hasKeyRc proc ariTraceRecorder(tr: TraceRecorderRef) = diff --git a/nimbus/db/core_db/base.nim b/nimbus/db/core_db/base.nim index 8074a4f72e..249278797b 100644 --- a/nimbus/db/core_db/base.nim +++ b/nimbus/db/core_db/base.nim @@ -391,18 +391,31 @@ proc put*( kvt.ifTrackNewApi: debug logTxt, api, elapsed, key=key.toStr, val=val.toLenStr, result -proc hasKey*(kvt: CoreDbKvtRef; key: openArray[byte]): CoreDbRc[bool] = - ## Would be named `contains` if it returned `bool` rather than `Result[]`. +proc hasKeyRc*(kvt: CoreDbKvtRef; key: openArray[byte]): 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. ## - kvt.setTrackNewApi KvtHasKeyFn + kvt.setTrackNewApi KvtHasKeyRcFn result = block: - let rc = kvt.call(hasKey, kvt.kvt, key) + let rc = kvt.call(hasKeyRc, kvt.kvt, key) if rc.isOk: ok(rc.value) else: err(rc.error.toError $api) kvt.ifTrackNewApi: debug logTxt, api, elapsed, key=key.toStr, result +proc hasKey*(kvt: CoreDbKvtRef; key: openArray[byte]): 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`. + ## + kvt.setTrackNewApi KvtHasKeyFn + result = kvt.call(hasKeyRc, kvt.kvt, key).valueOr: false + kvt.ifTrackNewApi: debug logTxt, api, elapsed, key=key.toStr, result + # ------------------------------------------------------------------------------ # Public functions for generic columns # ------------------------------------------------------------------------------ diff --git a/nimbus/db/core_db/base/api_tracking.nim b/nimbus/db/core_db/base/api_tracking.nim index 05c9e7f7a4..638b72c16a 100644 --- a/nimbus/db/core_db/base/api_tracking.nim +++ b/nimbus/db/core_db/base/api_tracking.nim @@ -73,6 +73,7 @@ type KvtDelFn = "del" KvtGetFn = "get" KvtGetOrEmptyFn = "getOrEmpty" + KvtHasKeyRcFn = "hasKeyRc" KvtHasKeyFn = "hasKey" KvtLenFn = "len" KvtPairsIt = "pairs" diff --git a/nimbus/db/core_db/core_apps.nim b/nimbus/db/core_db/core_apps.nim index 59e0f924be..8c4a6e712a 100644 --- a/nimbus/db/core_db/core_apps.nim +++ b/nimbus/db/core_db/core_apps.nim @@ -301,7 +301,7 @@ proc markCanonicalChain( # ------------------------------------------------------------------------------ proc exists*(db: CoreDbRef, hash: Hash256): bool = - db.ctx.getKvt().hasKey(hash.data).valueOr: + db.ctx.getKvt().hasKeyRc(hash.data).valueOr: warn "exisis", hash, error=($$error) return false # => true/false @@ -574,7 +574,7 @@ proc getTransactionCount*( var txCount = 0'u16 while true: let key = hashIndexKey(txRoot, txCount) - let yes = kvt.hasKey(key).valueOr: + let yes = kvt.hasKeyRc(key).valueOr: warn info, txRoot, key, error=($$error) return 0 if yes: @@ -743,7 +743,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.ctx.getKvt().hasKey(genericHashKey(blockHash).toOpenArray).valueOr: + db.ctx.getKvt().hasKeyRc(genericHashKey(blockHash).toOpenArray).valueOr: warn "headerExists()", blockHash, error=($$error) return false # => true/false diff --git a/nimbus/db/kvt/kvt_api.nim b/nimbus/db/kvt/kvt_api.nim index f1cad133e0..788bd0f1a1 100644 --- a/nimbus/db/kvt/kvt_api.nim +++ b/nimbus/db/kvt/kvt_api.nim @@ -52,7 +52,7 @@ type key: openArray[byte]): Result[Blob,KvtError] {.noRaise.} KvtApiLenFn* = proc(db: KvtDbRef, key: openArray[byte]): Result[int,KvtError] {.noRaise.} - KvtApiHasKeyFn* = proc(db: KvtDbRef, + KvtApiHasKeyRcFn* = proc(db: KvtDbRef, key: openArray[byte]): Result[bool,KvtError] {.noRaise.} KvtApiIsCentreFn* = proc(db: KvtDbRef): bool {.noRaise.} KvtApiIsTopFn* = proc(tx: KvtTxRef): bool {.noRaise.} @@ -79,7 +79,7 @@ type forkTx*: KvtApiForkTxFn get*: KvtApiGetFn len*: KvtApiLenFn - hasKey*: KvtApiHasKeyFn + hasKeyRc*: KvtApiHasKeyRcFn isCentre*: KvtApiIsCentreFn isTop*: KvtApiIsTopFn level*: KvtApiLevelFn @@ -104,7 +104,7 @@ type KvtApiProfForkTxFn = "forkTx" KvtApiProfGetFn = "get" KvtApiProfLenFn = "len" - KvtApiProfHasKeyFn = "hasKey" + KvtApiProfHasKeyRcFn = "hasKeyRc" KvtApiProfIsCentreFn = "isCentre" KvtApiProfIsTopFn = "isTop" KvtApiProfLevelFn = "level" @@ -139,7 +139,7 @@ when AutoValidateApiHooks: doAssert not api.forget.isNil doAssert not api.forkTx.isNil doAssert not api.get.isNil - doAssert not api.hasKey.isNil + doAssert not api.hasKeyRc.isNil doAssert not api.isCentre.isNil doAssert not api.isTop.isNil doAssert not api.level.isNil @@ -182,7 +182,7 @@ func init*(api: var KvtApiObj) = api.forkTx = forkTx api.get = get api.len = len - api.hasKey = hasKey + api.hasKeyRc = hasKeyRc api.isCentre = isCentre api.isTop = isTop api.level = level @@ -210,7 +210,7 @@ func dup*(api: KvtApiRef): KvtApiRef = forkTx: api.forkTx, get: api.get, len: api.len, - hasKey: api.hasKey, + hasKeyRc: api.hasKeyRc, isCentre: api.isCentre, isTop: api.isTop, level: api.level, @@ -287,10 +287,10 @@ func init*( KvtApiProfLenFn.profileRunner: result = api.len(a, b) - profApi.hasKey = + profApi.hasKeyRc = proc(a: KvtDbRef, b: openArray[byte]): auto = - KvtApiProfHasKeyFn.profileRunner: - result = api.hasKey(a, b) + KvtApiProfHasKeyRcFn.profileRunner: + result = api.hasKeyRc(a, b) profApi.isCentre = proc(a: KvtDbRef): auto = diff --git a/nimbus/db/kvt/kvt_utils.nim b/nimbus/db/kvt/kvt_utils.nim index 0c053406c0..6e7c2cb131 100644 --- a/nimbus/db/kvt/kvt_utils.nim +++ b/nimbus/db/kvt/kvt_utils.nim @@ -133,12 +133,13 @@ proc len*( return db.getBeLen key ok(len) -proc hasKey*( +proc hasKeyRc*( db: KvtDbRef; # Database key: openArray[byte]; # Key of database record ): Result[bool,KvtError] = - ## For the argument `key` return the associated value preferably from the - ## top layer, or the database otherwise. + ## For the argument `key` return `true` if `get()` returned a value on + ## that argument, `false` if it returned `GetNotFound`, and an error + ## otherwise. ## if key.len == 0: return err(KeyInvalid) @@ -153,6 +154,14 @@ proc hasKey*( return ok(false) err(rc.error) +proc hasKey*( + db: KvtDbRef; # Database + key: openArray[byte]; # Key of database record + ): bool = + ## Simplified version of `hasKeyRc` where `false` is returned instead of + ## an error. + db.hasKeyRc(key).valueOr: false + # ------------------------------------------------------------------------------ # End # ------------------------------------------------------------------------------ diff --git a/premix/persist.nim b/premix/persist.nim index 7033882c1d..7e2ce1f652 100644 --- a/premix/persist.nim +++ b/premix/persist.nim @@ -41,7 +41,7 @@ template persistToDb(db: CoreDbRef, body: untyped) = block: body proc contains(kvt: CoreDbKvtRef; key: openArray[byte]): bool = - kvt.hasKey(key).expect "valid bool" + kvt.hasKeyRc(key).expect "valid bool" proc main() {.used.} = # 97 block with uncles