2024-02-02 20:23:04 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-10-03 11:56:13 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or
|
|
|
|
# distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
2023-12-12 19:12:56 +00:00
|
|
|
std/[strformat, times],
|
2023-10-18 19:27:22 +00:00
|
|
|
chronicles,
|
2023-10-03 11:56:13 +00:00
|
|
|
eth/common,
|
|
|
|
results,
|
|
|
|
unittest2,
|
2023-12-12 17:47:41 +00:00
|
|
|
../../nimbus/core/chain,
|
|
|
|
../../nimbus/db/ledger,
|
2023-12-12 19:12:56 +00:00
|
|
|
../replay/[pp, undump_blocks, xcheck],
|
2023-10-03 11:56:13 +00:00
|
|
|
./test_helpers
|
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
type StopMoaningAboutLedger {.used.} = LedgerType
|
|
|
|
|
|
|
|
when CoreDbEnableApiProfiling or LedgerEnableApiProfiling:
|
2023-12-12 19:12:56 +00:00
|
|
|
import std/[algorithm, sequtils, strutils]
|
|
|
|
|
|
|
|
const
|
|
|
|
EnableExtraLoggingControl = true
|
|
|
|
var
|
|
|
|
logStartTime {.used.} = Time()
|
|
|
|
logSavedEnv {.used.}: (bool,bool,bool,bool)
|
2023-12-12 17:47:41 +00:00
|
|
|
|
2023-10-03 11:56:13 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-10-18 19:27:22 +00:00
|
|
|
proc setTraceLevel {.used.} =
|
|
|
|
when defined(chronicles_runtime_filtering) and loggingEnabled:
|
|
|
|
setLogLevel(LogLevel.TRACE)
|
|
|
|
|
2023-12-12 19:12:56 +00:00
|
|
|
proc setDebugLevel {.used.} =
|
|
|
|
when defined(chronicles_runtime_filtering) and loggingEnabled:
|
|
|
|
setLogLevel(LogLevel.DEBUG)
|
|
|
|
|
2023-10-18 19:27:22 +00:00
|
|
|
proc setErrorLevel {.used.} =
|
|
|
|
when defined(chronicles_runtime_filtering) and loggingEnabled:
|
|
|
|
setLogLevel(LogLevel.ERROR)
|
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
# --------------
|
|
|
|
|
2024-02-02 20:23:04 +00:00
|
|
|
template initLogging(noisy: bool, com: CommonRef) =
|
2023-12-12 19:12:56 +00:00
|
|
|
when EnableExtraLoggingControl:
|
2024-02-02 20:23:04 +00:00
|
|
|
if noisy:
|
|
|
|
setDebugLevel()
|
|
|
|
debug "start undumping into persistent blocks"
|
2023-12-12 19:12:56 +00:00
|
|
|
logStartTime = Time()
|
|
|
|
logSavedEnv = (com.db.trackLegaApi, com.db.trackNewApi,
|
|
|
|
com.db.trackLedgerApi, com.db.localDbOnly)
|
|
|
|
setErrorLevel()
|
|
|
|
com.db.trackLegaApi = true
|
|
|
|
com.db.trackNewApi = true
|
|
|
|
com.db.trackLedgerApi = true
|
|
|
|
com.db.localDbOnly = true
|
|
|
|
|
|
|
|
proc finishLogging(com: CommonRef) =
|
|
|
|
when EnableExtraLoggingControl:
|
|
|
|
setErrorLevel()
|
|
|
|
(com.db.trackLegaApi, com.db.trackNewApi,
|
|
|
|
com.db.trackLedgerApi, com.db.localDbOnly) = logSavedEnv
|
|
|
|
|
|
|
|
|
2024-02-02 20:23:04 +00:00
|
|
|
template startLogging(noisy: bool; num: BlockNumber) =
|
|
|
|
when EnableExtraLoggingControl:
|
|
|
|
if noisy and logStartTime == Time():
|
|
|
|
logStartTime = getTime()
|
|
|
|
setDebugLevel()
|
|
|
|
debug "start logging ...", blockNumber=num
|
|
|
|
|
|
|
|
template startLogging(noisy: bool) =
|
2023-12-12 19:12:56 +00:00
|
|
|
when EnableExtraLoggingControl:
|
|
|
|
if noisy and logStartTime == Time():
|
|
|
|
logStartTime = getTime()
|
|
|
|
setDebugLevel()
|
|
|
|
debug "start logging ..."
|
|
|
|
|
2024-02-02 20:23:04 +00:00
|
|
|
template stopLogging(noisy: bool) =
|
2023-12-12 19:12:56 +00:00
|
|
|
when EnableExtraLoggingControl:
|
|
|
|
if logStartTime != Time():
|
|
|
|
debug "stop logging", elapsed=(getTime() - logStartTime).pp
|
|
|
|
logStartTime = Time()
|
|
|
|
setErrorLevel()
|
|
|
|
|
|
|
|
template stopLoggingAfter(noisy: bool; code: untyped) =
|
|
|
|
## Turn logging off after executing `block`
|
|
|
|
block:
|
|
|
|
defer: noisy.stopLogging()
|
|
|
|
code
|
|
|
|
|
|
|
|
# --------------
|
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
proc coreDbProfResults(info: string; indent = 4): string =
|
|
|
|
when CoreDbEnableApiProfiling:
|
|
|
|
let
|
|
|
|
pfx = indent.toPfx
|
|
|
|
pfx2 = pfx & " "
|
|
|
|
result = "CoreDb profiling results" & info & ":"
|
|
|
|
result &= "\n" & pfx & "by accumulated duration per procedure"
|
|
|
|
for (ela,w) in coreDbProfTab.byElapsed:
|
|
|
|
result &= pfx2 & ela.pp & ": " &
|
|
|
|
w.mapIt($it & coreDbProfTab.stats(it).pp(true)).sorted.join(", ")
|
|
|
|
result &= "\n" & pfx & "by number of visits"
|
|
|
|
for (count,w) in coreDbProfTab.byVisits:
|
|
|
|
result &= pfx2 & $count & ": " &
|
|
|
|
w.mapIt($it & coreDbProfTab.stats(it).pp).sorted.join(", ")
|
|
|
|
|
|
|
|
proc ledgerProfResults(info: string; indent = 4): string =
|
|
|
|
when LedgerEnableApiProfiling:
|
|
|
|
let
|
|
|
|
pfx = indent.toPfx
|
|
|
|
pfx2 = pfx & " "
|
|
|
|
result = "Ledger profiling results" & info & ":"
|
|
|
|
result &= "\n" & pfx & "by accumulated duration per procedure"
|
|
|
|
for (ela,w) in ledgerProfTab.byElapsed:
|
|
|
|
result &= pfx2 & ela.pp & ": " &
|
|
|
|
w.mapIt($it & ledgerProfTab.stats(it).pp(true)).sorted.join(", ")
|
|
|
|
result &= "\n" & pfx & "by number of visits"
|
|
|
|
for (count,w) in ledgerProfTab.byVisits:
|
|
|
|
result &= pfx2 & $count & ": " &
|
|
|
|
w.mapIt($it & ledgerProfTab.stats(it).pp).sorted.join(", ")
|
|
|
|
|
2023-10-03 11:56:13 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public test function
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
proc test_chainSyncProfilingPrint*(
|
|
|
|
noisy = false;
|
|
|
|
nBlocks: int;
|
|
|
|
) =
|
|
|
|
if noisy:
|
|
|
|
let info =
|
|
|
|
if 0 < nBlocks and nBlocks < high(int): " (" & $nBlocks & " blocks)"
|
|
|
|
else: ""
|
|
|
|
block:
|
|
|
|
let s = info.coreDbProfResults()
|
|
|
|
if 0 < s.len: true.say "***", s, "\n"
|
|
|
|
block:
|
|
|
|
let s = info.ledgerProfResults()
|
|
|
|
if 0 < s.len: true.say "***", s, "\n"
|
|
|
|
|
|
|
|
|
2023-10-25 14:03:09 +00:00
|
|
|
proc test_chainSync*(
|
2023-10-03 11:56:13 +00:00
|
|
|
noisy: bool;
|
2024-02-09 13:30:07 +00:00
|
|
|
filePaths: seq[string];
|
2023-10-03 11:56:13 +00:00
|
|
|
com: CommonRef;
|
2023-10-18 19:27:22 +00:00
|
|
|
numBlocks = high(int);
|
2023-12-12 19:12:56 +00:00
|
|
|
enaLogging = true;
|
2023-10-18 19:27:22 +00:00
|
|
|
lastOneExtra = true
|
2023-10-03 11:56:13 +00:00
|
|
|
): bool =
|
|
|
|
## Store persistent blocks from dump into chain DB
|
|
|
|
let
|
2023-12-12 19:12:56 +00:00
|
|
|
sayBlocks = 900
|
2023-10-03 11:56:13 +00:00
|
|
|
chain = com.newChain
|
2023-12-12 19:12:56 +00:00
|
|
|
lastBlock = max(1, numBlocks).toBlockNumber
|
|
|
|
|
2024-02-02 20:23:04 +00:00
|
|
|
noisy.initLogging com
|
2023-12-12 19:12:56 +00:00
|
|
|
defer: com.finishLogging()
|
2023-10-03 11:56:13 +00:00
|
|
|
|
2024-02-09 13:30:07 +00:00
|
|
|
for w in filePaths.undumpBlocks:
|
2023-10-03 11:56:13 +00:00
|
|
|
let (fromBlock, toBlock) = (w[0][0].blockNumber, w[0][^1].blockNumber)
|
|
|
|
if fromBlock == 0.u256:
|
|
|
|
xCheck w[0][0] == com.db.getBlockHeader(0.u256)
|
|
|
|
continue
|
|
|
|
|
2024-02-02 20:23:04 +00:00
|
|
|
# Process groups of blocks ...
|
2023-10-18 19:27:22 +00:00
|
|
|
if toBlock < lastBlock:
|
|
|
|
# Message if `[fromBlock,toBlock]` contains a multiple of `sayBlocks`
|
2023-12-12 19:12:56 +00:00
|
|
|
if fromBlock + (toBlock mod sayBlocks.u256) <= toBlock:
|
2023-10-18 19:27:22 +00:00
|
|
|
noisy.say "***", &"processing ...[#{fromBlock},#{toBlock}]..."
|
2023-12-12 19:12:56 +00:00
|
|
|
if enaLogging:
|
2024-02-02 20:23:04 +00:00
|
|
|
noisy.startLogging(w[0][0].blockNumber)
|
2023-12-12 19:12:56 +00:00
|
|
|
noisy.stopLoggingAfter():
|
|
|
|
let runPersistBlocksRc = chain.persistBlocks(w[0], w[1])
|
|
|
|
xCheck runPersistBlocksRc == ValidationResult.OK:
|
|
|
|
if noisy:
|
|
|
|
# Re-run with logging enabled
|
|
|
|
setTraceLevel()
|
2024-02-02 20:23:04 +00:00
|
|
|
com.db.trackLegaApi = false
|
|
|
|
com.db.trackNewApi = false
|
|
|
|
com.db.trackLedgerApi = false
|
2023-12-12 19:12:56 +00:00
|
|
|
discard chain.persistBlocks(w[0], w[1])
|
2023-10-18 19:27:22 +00:00
|
|
|
continue
|
|
|
|
|
2024-02-02 20:23:04 +00:00
|
|
|
# Last group or single block
|
|
|
|
#
|
2023-10-18 19:27:22 +00:00
|
|
|
# Make sure that the `lastBlock` is the first item of the argument batch.
|
|
|
|
# So It might be necessary to Split off all blocks smaller than `lastBlock`
|
|
|
|
# and execute them first. Then the next batch starts with the `lastBlock`.
|
|
|
|
let
|
|
|
|
pivot = (lastBlock - fromBlock).truncate(uint)
|
|
|
|
headers9 = w[0][pivot .. ^1]
|
|
|
|
bodies9 = w[1][pivot .. ^1]
|
|
|
|
doAssert lastBlock == headers9[0].blockNumber
|
|
|
|
|
|
|
|
# Process leading betch before `lastBlock` (if any)
|
|
|
|
var dotsOrSpace = "..."
|
|
|
|
if fromBlock < lastBlock:
|
|
|
|
let
|
|
|
|
headers1 = w[0][0 ..< pivot]
|
|
|
|
bodies1 = w[1][0 ..< pivot]
|
|
|
|
noisy.say "***", &"processing {dotsOrSpace}[#{fromBlock},#{lastBlock-1}]"
|
|
|
|
let runPersistBlocks1Rc = chain.persistBlocks(headers1, bodies1)
|
|
|
|
xCheck runPersistBlocks1Rc == ValidationResult.OK
|
|
|
|
dotsOrSpace = " "
|
|
|
|
|
2024-02-02 20:23:04 +00:00
|
|
|
noisy.startLogging(headers9[0].blockNumber)
|
2023-10-18 19:27:22 +00:00
|
|
|
if lastOneExtra:
|
|
|
|
let
|
|
|
|
headers0 = headers9[0..0]
|
|
|
|
bodies0 = bodies9[0..0]
|
|
|
|
noisy.say "***", &"processing {dotsOrSpace}[#{lastBlock},#{lastBlock}]"
|
2023-12-12 19:12:56 +00:00
|
|
|
noisy.stopLoggingAfter():
|
|
|
|
let runPersistBlocks0Rc = chain.persistBlocks(headers0, bodies0)
|
|
|
|
xCheck runPersistBlocks0Rc == ValidationResult.OK
|
2023-10-18 19:27:22 +00:00
|
|
|
else:
|
|
|
|
noisy.say "***", &"processing {dotsOrSpace}[#{lastBlock},#{toBlock}]"
|
2023-12-12 19:12:56 +00:00
|
|
|
noisy.stopLoggingAfter():
|
|
|
|
let runPersistBlocks9Rc = chain.persistBlocks(headers9, bodies9)
|
|
|
|
xCheck runPersistBlocks9Rc == ValidationResult.OK
|
2023-10-18 19:27:22 +00:00
|
|
|
break
|
2023-10-03 11:56:13 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|