Coredb and sync maintenance update (#2583)
* bump metrics * Remove cruft * Cosmetics, update some logging, noise control * 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.
This commit is contained in:
parent
ee6a7e8259
commit
42a08cfba9
|
@ -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<br>
|
||||
Enable legacy protocol `eth66` (or another available protocol version.)
|
||||
|
||||
|
||||
### <a name="devel-tips"></a>Development tips
|
||||
|
||||
Interesting Make variables and targets are documented in the [nimbus-build-system](https://github.com/status-im/nimbus-build-system) repo.
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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) =
|
||||
|
|
|
@ -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
|
||||
# ------------------------------------------------------------------------------
|
||||
|
|
|
@ -73,6 +73,7 @@ type
|
|||
KvtDelFn = "del"
|
||||
KvtGetFn = "get"
|
||||
KvtGetOrEmptyFn = "getOrEmpty"
|
||||
KvtHasKeyRcFn = "hasKeyRc"
|
||||
KvtHasKeyFn = "hasKey"
|
||||
KvtLenFn = "len"
|
||||
KvtPairsIt = "pairs"
|
||||
|
|
|
@ -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..<uint16.high:
|
||||
let key = hashIndexKey(txRoot, idx)
|
||||
let txData = kvt.getOrEmpty(key).valueOr:
|
||||
warn logTxt "getBlockTransactionData()",
|
||||
txRoot, key, action="getOrEmpty()", error=($$error)
|
||||
warn "getBlockTransactionData", txRoot, key, error=($$error)
|
||||
break body
|
||||
if txData.len == 0:
|
||||
break body
|
||||
|
@ -137,8 +134,9 @@ iterator getBlockTransactions*(
|
|||
for encodedTx in db.getBlockTransactionData(header.txRoot):
|
||||
try:
|
||||
yield rlp.decode(encodedTx, Transaction)
|
||||
except RlpError as exc:
|
||||
warn logTxt "Cannot decode database transaction", data = toHex(encodedTx), error = exc.msg
|
||||
except RlpError as e:
|
||||
warn "getBlockTransactions(): Cannot decode tx",
|
||||
data = toHex(encodedTx), err=e.msg, errName=e.name
|
||||
|
||||
iterator getBlockTransactionHashes*(
|
||||
db: CoreDbRef;
|
||||
|
@ -161,8 +159,7 @@ iterator getWithdrawals*(
|
|||
for idx in 0'u16..<uint16.high:
|
||||
let key = hashIndexKey(withdrawalsRoot, idx)
|
||||
let data = kvt.getOrEmpty(key).valueOr:
|
||||
warn logTxt "getWithdrawals()",
|
||||
withdrawalsRoot, key, action="getOrEmpty()", error=($$error)
|
||||
warn "getWithdrawals", withdrawalsRoot, key, error=($$error)
|
||||
break body
|
||||
if data.len == 0:
|
||||
break body
|
||||
|
@ -181,8 +178,7 @@ iterator getReceipts*(
|
|||
for idx in 0'u16..<uint16.high:
|
||||
let key = hashIndexKey(receiptsRoot, idx)
|
||||
let data = kvt.getOrEmpty(key).valueOr:
|
||||
warn logTxt "getReceipts()",
|
||||
receiptsRoot, key, action="getOrEmpty()", error=($$error)
|
||||
warn "getReceipts", receiptsRoot, key, error=($$error)
|
||||
break body
|
||||
if data.len == 0:
|
||||
break body
|
||||
|
@ -199,8 +195,8 @@ proc removeTransactionFromCanonicalChain(
|
|||
## Removes the transaction specified by the given hash from the canonical
|
||||
## chain.
|
||||
db.ctx.getKvt.del(transactionHashToBlockKey(transactionHash).toOpenArray).isOkOr:
|
||||
warn logTxt "removeTransactionFromCanonicalChain()",
|
||||
transactionHash, action="del()", error=($$error)
|
||||
warn "removeTransactionFromCanonicalChain",
|
||||
transactionHash, error=($$error)
|
||||
|
||||
proc setAsCanonicalChainHead(
|
||||
db: CoreDbRef;
|
||||
|
@ -208,6 +204,7 @@ proc setAsCanonicalChainHead(
|
|||
header: BlockHeader;
|
||||
) =
|
||||
## Sets the header as the canonical chain HEAD.
|
||||
const info = "setAsCanonicalChainHead()"
|
||||
|
||||
# TODO This code handles reorgs - this should be moved elsewhere because we'll
|
||||
# be handling reorgs mainly in-memory
|
||||
|
@ -226,7 +223,7 @@ proc setAsCanonicalChainHead(
|
|||
db.removeTransactionFromCanonicalChain(txHash)
|
||||
# TODO re-add txn to internal pending pool (only if local sender)
|
||||
except BlockNotFound:
|
||||
warn logTxt "Could not load old header", oldHash
|
||||
warn info & ": Could not load old header", oldHash
|
||||
|
||||
for h in newCanonicalHeaders:
|
||||
# TODO don't recompute block hash
|
||||
|
@ -234,8 +231,7 @@ proc setAsCanonicalChainHead(
|
|||
|
||||
let canonicalHeadHash = canonicalHeadHashKey()
|
||||
db.ctx.getKvt.put(canonicalHeadHash.toOpenArray, rlp.encode(headerHash)).isOkOr:
|
||||
warn logTxt "setAsCanonicalChainHead()",
|
||||
canonicalHeadHash, action="put()", error=($$error)
|
||||
warn info, canonicalHeadHash, error=($$error)
|
||||
|
||||
proc markCanonicalChain(
|
||||
db: CoreDbRef;
|
||||
|
@ -244,6 +240,8 @@ proc markCanonicalChain(
|
|||
): bool =
|
||||
## mark this chain as canonical by adding block number to hash lookup
|
||||
## down to forking point
|
||||
const
|
||||
info = "markCanonicalChain()"
|
||||
var
|
||||
currHash = headerHash
|
||||
currHeader = header
|
||||
|
@ -253,7 +251,7 @@ proc markCanonicalChain(
|
|||
kvt = db.ctx.getKvt()
|
||||
key = blockNumberToHashKey(currHeader.number)
|
||||
kvt.put(key.toOpenArray, rlp.encode(currHash)).isOkOr:
|
||||
warn logTxt "markCanonicalChain()", key, action="put()", error=($$error)
|
||||
warn info, key, error=($$error)
|
||||
return false
|
||||
|
||||
# it is a genesis block, done
|
||||
|
@ -269,22 +267,22 @@ proc markCanonicalChain(
|
|||
try:
|
||||
rlp.decode(data, Hash256)
|
||||
except RlpError as exc:
|
||||
warn logTxt "markCanonicalChain()", key, action="put()", error=exc.msg
|
||||
warn info, key, error=exc.msg
|
||||
Hash256()
|
||||
|
||||
while currHash != Hash256():
|
||||
let key = blockNumberToHashKey(currHeader.number)
|
||||
let data = kvt.getOrEmpty(key.toOpenArray).valueOr:
|
||||
warn logTxt "markCanonicalChain()", key, action="get()", error=($$error)
|
||||
warn info, key, error=($$error)
|
||||
return false
|
||||
if data.len == 0:
|
||||
# not marked, mark it
|
||||
kvt.put(key.toOpenArray, rlp.encode(currHash)).isOkOr:
|
||||
warn logTxt "markCanonicalChain()", key, action="put()", error=($$error)
|
||||
warn info, key, error=($$error)
|
||||
elif rlpDecodeOrZero(data) != currHash:
|
||||
# replace prev chain
|
||||
kvt.put(key.toOpenArray, rlp.encode(currHash)).isOkOr:
|
||||
warn logTxt "markCanonicalChain()", key, action="put()", error=($$error)
|
||||
warn info, key, error=($$error)
|
||||
else:
|
||||
# forking point, done
|
||||
break
|
||||
|
@ -303,9 +301,10 @@ proc markCanonicalChain(
|
|||
# ------------------------------------------------------------------------------
|
||||
|
||||
proc exists*(db: CoreDbRef, hash: Hash256): bool =
|
||||
db.ctx.getKvt().hasKey(hash.data).valueOr:
|
||||
warn logTxt "exisis()", hash, action="hasKey()", error=($$error)
|
||||
db.ctx.getKvt().hasKeyRc(hash.data).valueOr:
|
||||
warn "exisis", hash, error=($$error)
|
||||
return false
|
||||
# => 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
|
||||
|
||||
|
@ -576,9 +574,8 @@ proc getTransactionCount*(
|
|||
var txCount = 0'u16
|
||||
while true:
|
||||
let key = hashIndexKey(txRoot, txCount)
|
||||
let yes = kvt.hasKey(key).valueOr:
|
||||
warn logTxt info,
|
||||
txRoot, key, action="hasKey()", error=($$error)
|
||||
let yes = kvt.hasKeyRc(key).valueOr:
|
||||
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,17 +736,17 @@ 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)
|
||||
|
||||
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)
|
||||
db.ctx.getKvt().hasKeyRc(genericHashKey(blockHash).toOpenArray).valueOr:
|
||||
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*(
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -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
|
||||
# ------------------------------------------------------------------------------
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -12,6 +12,9 @@
|
|||
## This module implements Ethereum Wire Protocol version 67, `eth/67`.
|
||||
## Specification:
|
||||
## `eth/68 <https://github.com/ethereum/devp2p/blob/master/caps/eth.md>`_
|
||||
##
|
||||
## 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()
|
||||
|
|
|
@ -12,6 +12,9 @@
|
|||
## This module implements Ethereum Snapshot Protocol version 1, `snap/1`.
|
||||
## Specification:
|
||||
## `snap/1 <https://github.com/ethereum/devp2p/blob/master/caps/snap.md>`_
|
||||
##
|
||||
## 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,
|
||||
|
|
|
@ -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
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 4337ccd62c0b7d57492402dd4cb838ddc0c78a84
|
||||
Subproject commit 29bb7ba63cd884770169891687595348a70cf166
|
Loading…
Reference in New Issue