2022-07-01 11:42:17 +00:00
|
|
|
# Nimbus - Types, data structures and shared utilities used in network sync
|
|
|
|
#
|
|
|
|
# Copyright (c) 2018-2021 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)
|
|
|
|
# * 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.
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
## Snap sync components tester and TDD environment
|
2022-07-01 11:42:17 +00:00
|
|
|
|
|
|
|
import
|
2023-01-23 16:09:12 +00:00
|
|
|
std/[distros, os, sets, sequtils, strformat, strutils, tables],
|
2022-07-01 11:42:17 +00:00
|
|
|
chronicles,
|
2023-01-23 16:09:12 +00:00
|
|
|
eth/[common, p2p],
|
2022-08-12 15:42:07 +00:00
|
|
|
rocksdb,
|
2022-07-01 11:42:17 +00:00
|
|
|
unittest2,
|
2023-01-23 16:09:12 +00:00
|
|
|
../nimbus/db/select_backend,
|
2022-12-02 04:39:12 +00:00
|
|
|
../nimbus/core/chain,
|
2022-08-24 13:44:18 +00:00
|
|
|
../nimbus/sync/types,
|
2022-08-04 08:04:30 +00:00
|
|
|
../nimbus/sync/snap/range_desc,
|
2022-10-14 16:40:32 +00:00
|
|
|
../nimbus/sync/snap/worker/db/[
|
2022-12-06 17:35:56 +00:00
|
|
|
hexary_desc, hexary_envelope, hexary_error, hexary_inspect, hexary_nearby,
|
2023-01-23 16:09:12 +00:00
|
|
|
hexary_paths, rocky_bulk_load, snapdb_accounts, snapdb_desc],
|
|
|
|
./replay/[pp, undump_accounts, undump_storages],
|
|
|
|
./test_sync_snap/[
|
|
|
|
bulk_test_xx, snap_test_xx,
|
|
|
|
test_accounts, test_node_range, test_inspect, test_pivot, test_storage,
|
|
|
|
test_db_timing, test_types]
|
2022-07-01 11:42:17 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
baseDir = [".", "..", ".."/"..", $DirSep]
|
2022-08-12 15:42:07 +00:00
|
|
|
repoDir = [".", "tests"/"replay", "tests"/"test_sync_snap",
|
|
|
|
"nimbus-eth1-blobs"/"replay"]
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
# Reference file for finding the database directory
|
|
|
|
sampleDirRefFile = "sample0.txt.gz"
|
|
|
|
|
|
|
|
# Standard test samples
|
|
|
|
bChainCapture = bulkTest0
|
|
|
|
accSample = snapTest0
|
|
|
|
storSample = snapTest4
|
|
|
|
|
|
|
|
# Number of database slots (needed for timing tests)
|
2022-08-12 15:42:07 +00:00
|
|
|
nTestDbInstances = 9
|
2022-07-01 11:42:17 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
TestDbs = object
|
2022-08-12 15:42:07 +00:00
|
|
|
## Provide enough spare empty databases
|
2022-07-01 11:42:17 +00:00
|
|
|
persistent: bool
|
|
|
|
dbDir: string
|
2022-08-12 15:42:07 +00:00
|
|
|
cdb: array[nTestDbInstances,ChainDb]
|
2022-07-01 11:42:17 +00:00
|
|
|
|
|
|
|
when defined(linux):
|
|
|
|
# The `detectOs(Ubuntu)` directive is not Windows compatible, causes an
|
|
|
|
# error when running the system command `lsb_release -d` in the background.
|
|
|
|
let isUbuntu32bit = detectOs(Ubuntu) and int.sizeof == 4
|
|
|
|
else:
|
|
|
|
const isUbuntu32bit = false
|
|
|
|
|
|
|
|
let
|
|
|
|
# Forces `check()` to print the error (as opposed when using `isOk()`)
|
2022-11-28 09:03:23 +00:00
|
|
|
OkHexDb = Result[void,HexaryError].ok()
|
2022-07-01 11:42:17 +00:00
|
|
|
|
|
|
|
# There was a problem with the Github/CI which results in spurious crashes
|
2022-12-02 04:39:12 +00:00
|
|
|
# when leaving the `runner()` if the persistent ChainDBRef initialisation
|
2022-07-01 11:42:17 +00:00
|
|
|
# was present, see `test_custom_network` for more details.
|
|
|
|
disablePersistentDB = isUbuntu32bit
|
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
var
|
|
|
|
xTmpDir: string
|
|
|
|
xDbs: TestDbs # for repeated storage/overwrite tests
|
|
|
|
xTab32: Table[ByteArray32,Blob] # extracted data
|
|
|
|
xTab33: Table[ByteArray33,Blob]
|
|
|
|
|
2022-07-01 11:42:17 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
proc findFilePath(file: string;
|
|
|
|
baseDir, repoDir: openArray[string]): Result[string,void] =
|
2022-07-01 11:42:17 +00:00
|
|
|
for dir in baseDir:
|
|
|
|
for repo in repoDir:
|
|
|
|
let path = dir / repo / file
|
|
|
|
if path.fileExists:
|
|
|
|
return ok(path)
|
2022-09-02 18:16:09 +00:00
|
|
|
echo "*** File not found \"", file, "\"."
|
2022-07-01 11:42:17 +00:00
|
|
|
err()
|
|
|
|
|
2022-08-24 13:44:18 +00:00
|
|
|
proc getTmpDir(sampleDir = sampleDirRefFile): string =
|
2022-08-12 15:42:07 +00:00
|
|
|
sampleDir.findFilePath(baseDir,repoDir).value.splitFile.dir
|
|
|
|
|
|
|
|
proc say*(noisy = false; pfx = "***"; args: varargs[string, `$`]) =
|
|
|
|
if noisy:
|
|
|
|
if args.len == 0:
|
|
|
|
echo "*** ", pfx
|
|
|
|
elif 0 < pfx.len and pfx[^1] != ' ':
|
|
|
|
echo pfx, " ", args.toSeq.join
|
|
|
|
else:
|
|
|
|
echo pfx, args.toSeq.join
|
2022-07-01 11:42:17 +00:00
|
|
|
|
|
|
|
proc setTraceLevel =
|
|
|
|
discard
|
|
|
|
when defined(chronicles_runtime_filtering) and loggingEnabled:
|
|
|
|
setLogLevel(LogLevel.TRACE)
|
|
|
|
|
|
|
|
proc setErrorLevel =
|
|
|
|
discard
|
|
|
|
when defined(chronicles_runtime_filtering) and loggingEnabled:
|
|
|
|
setLogLevel(LogLevel.ERROR)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
proc to(sample: AccountsSample; T: type seq[UndumpAccounts]): T =
|
|
|
|
## Convert test data into usable in-memory format
|
2022-08-24 13:44:18 +00:00
|
|
|
let file = sample.file.findFilePath(baseDir,repoDir).value
|
2022-09-02 18:16:09 +00:00
|
|
|
var root: Hash256
|
|
|
|
for w in file.undumpNextAccount:
|
|
|
|
let n = w.seenAccounts - 1
|
|
|
|
if n < sample.firstItem:
|
|
|
|
continue
|
|
|
|
if sample.lastItem < n:
|
|
|
|
break
|
|
|
|
if sample.firstItem == n:
|
|
|
|
root = w.root
|
|
|
|
elif w.root != root:
|
|
|
|
break
|
|
|
|
result.add w
|
|
|
|
|
|
|
|
proc to(sample: AccountsSample; T: type seq[UndumpStorages]): T =
|
|
|
|
## Convert test data into usable in-memory format
|
|
|
|
let file = sample.file.findFilePath(baseDir,repoDir).value
|
|
|
|
var root: Hash256
|
|
|
|
for w in file.undumpNextStorages:
|
|
|
|
let n = w.seenAccounts - 1 # storages selector based on accounts
|
2022-08-24 13:44:18 +00:00
|
|
|
if n < sample.firstItem:
|
|
|
|
continue
|
|
|
|
if sample.lastItem < n:
|
|
|
|
break
|
|
|
|
if sample.firstItem == n:
|
|
|
|
root = w.root
|
|
|
|
elif w.root != root:
|
|
|
|
break
|
|
|
|
result.add w
|
2022-07-01 11:42:17 +00:00
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
proc flushDbDir(s: string; subDir = "") =
|
2022-07-01 11:42:17 +00:00
|
|
|
if s != "":
|
|
|
|
let baseDir = s / "tmp"
|
2022-08-12 15:42:07 +00:00
|
|
|
for n in 0 ..< nTestDbInstances:
|
|
|
|
let instDir = if subDir == "": baseDir / $n else: baseDir / subDir / $n
|
2022-07-01 11:42:17 +00:00
|
|
|
if (instDir / "nimbus" / "data").dirExists:
|
|
|
|
# Typically under Windows: there might be stale file locks.
|
|
|
|
try: instDir.removeDir except: discard
|
2022-08-12 15:42:07 +00:00
|
|
|
try: (baseDir / subDir).removeDir except: discard
|
2022-07-01 11:42:17 +00:00
|
|
|
block dontClearUnlessEmpty:
|
|
|
|
for w in baseDir.walkDir:
|
|
|
|
break dontClearUnlessEmpty
|
|
|
|
try: baseDir.removeDir except: discard
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
proc testDbs(workDir = ""; subDir = ""; instances = nTestDbInstances): TestDbs =
|
2022-07-01 11:42:17 +00:00
|
|
|
if disablePersistentDB or workDir == "":
|
|
|
|
result.persistent = false
|
|
|
|
result.dbDir = "*notused*"
|
|
|
|
else:
|
|
|
|
result.persistent = true
|
2022-08-12 15:42:07 +00:00
|
|
|
if subDir != "":
|
|
|
|
result.dbDir = workDir / "tmp" / subDir
|
|
|
|
else:
|
|
|
|
result.dbDir = workDir / "tmp"
|
2022-07-01 11:42:17 +00:00
|
|
|
if result.persistent:
|
|
|
|
result.dbDir.flushDbDir
|
2022-09-02 18:16:09 +00:00
|
|
|
for n in 0 ..< min(result.cdb.len, instances):
|
2022-08-12 15:42:07 +00:00
|
|
|
result.cdb[n] = (result.dbDir / $n).newChainDB
|
2022-07-01 11:42:17 +00:00
|
|
|
|
|
|
|
proc lastTwo(a: openArray[string]): seq[string] =
|
|
|
|
if 1 < a.len: @[a[^2],a[^1]] else: a.toSeq
|
|
|
|
|
2023-01-23 16:09:12 +00:00
|
|
|
proc snapDbRef(cdb: ChainDb; pers: bool): SnapDbRef =
|
|
|
|
if pers: SnapDbRef.init(cdb) else: SnapDbRef.init(newMemoryDB())
|
2022-08-12 15:42:07 +00:00
|
|
|
|
2023-01-23 16:09:12 +00:00
|
|
|
proc snapDbAccountsRef(cdb:ChainDb; root:Hash256; pers:bool):SnapDbAccountsRef =
|
|
|
|
SnapDbAccountsRef.init(cdb.snapDbRef(pers), root, Peer())
|
2022-08-12 15:42:07 +00:00
|
|
|
|
2022-07-01 11:42:17 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2022-09-02 18:16:09 +00:00
|
|
|
# Test Runners: accounts and accounts storages
|
2022-07-01 11:42:17 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
proc accountsRunner(noisy = true; persistent = true; sample = accSample) =
|
2022-07-01 11:42:17 +00:00
|
|
|
let
|
2022-08-04 08:04:30 +00:00
|
|
|
peer = Peer.new
|
2023-01-23 16:09:12 +00:00
|
|
|
accLst = sample.to(seq[UndumpAccounts])
|
|
|
|
root = accLst[0].root
|
2022-08-12 15:42:07 +00:00
|
|
|
tmpDir = getTmpDir()
|
2022-09-02 18:16:09 +00:00
|
|
|
db = if persistent: tmpDir.testDbs(sample.name, instances=2) else: testDbs()
|
2022-07-01 11:42:17 +00:00
|
|
|
dbDir = db.dbDir.split($DirSep).lastTwo.join($DirSep)
|
2022-08-15 15:51:50 +00:00
|
|
|
info = if db.persistent: &"persistent db on \"{dbDir}\""
|
|
|
|
else: "in-memory db"
|
2022-09-02 18:16:09 +00:00
|
|
|
fileInfo = sample.file.splitPath.tail.replace(".txt.gz","")
|
2022-07-01 11:42:17 +00:00
|
|
|
|
|
|
|
defer:
|
|
|
|
if db.persistent:
|
2022-09-02 18:16:09 +00:00
|
|
|
if not db.cdb[0].rocksStoreRef.isNil:
|
|
|
|
db.cdb[0].rocksStoreRef.store.db.rocksdb_close
|
|
|
|
db.cdb[1].rocksStoreRef.store.db.rocksdb_close
|
2022-08-12 15:42:07 +00:00
|
|
|
tmpDir.flushDbDir(sample.name)
|
2022-07-01 11:42:17 +00:00
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
suite &"SyncSnap: {fileInfo} accounts and proofs for {info}":
|
2023-01-23 16:09:12 +00:00
|
|
|
test &"Proofing {accLst.len} items for state root ..{root.pp}":
|
|
|
|
let desc = db.cdb[0].snapDbAccountsRef(root, db.persistent)
|
|
|
|
accLst.test_accountsImport(desc, db.persistent)
|
|
|
|
|
|
|
|
var accKeys: seq[NodeKey]
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2023-01-23 16:09:12 +00:00
|
|
|
block:
|
2022-09-16 07:24:12 +00:00
|
|
|
let
|
2023-01-23 16:09:12 +00:00
|
|
|
# Common descriptor for this group of tests
|
|
|
|
desc = db.cdb[1].snapDbAccountsRef(root, db.persistent)
|
|
|
|
|
|
|
|
# Database abstractions
|
|
|
|
getFn = desc.getAccountFn # pestistent case
|
|
|
|
hexaDB = desc.hexaDB # in-memory, and debugging setup
|
|
|
|
|
|
|
|
test &"Merging {accLst.len} proofs for state root ..{root.pp}":
|
|
|
|
accLst.test_accountsMergeProofs(desc, accKeys) # set up `accKeys`
|
|
|
|
|
|
|
|
test &"Revisiting {accKeys.len} stored items on ChainDBRef":
|
|
|
|
accKeys.test_accountsRevisitStoredItems(desc, noisy)
|
|
|
|
# Beware: dumping a large database is not recommended
|
|
|
|
# true.say "***", "database dump\n ", desc.dumpHexaDB()
|
|
|
|
|
|
|
|
test &"Decompose path prefix envelopes on {info}":
|
|
|
|
if db.persistent:
|
|
|
|
accKeys.test_NodeRangeDecompose(root, getFn, hexaDB)
|
|
|
|
else:
|
|
|
|
accKeys.test_NodeRangeDecompose(root, hexaDB, hexaDB)
|
2022-08-12 15:42:07 +00:00
|
|
|
|
2022-11-16 23:51:06 +00:00
|
|
|
test &"Storing/retrieving {accKeys.len} items " &
|
2022-12-06 17:35:56 +00:00
|
|
|
"on persistent pivot/checkpoint registry":
|
2023-01-23 16:09:12 +00:00
|
|
|
if db.persistent:
|
|
|
|
accKeys.test_pivotStoreRead(db.cdb[0])
|
2022-11-16 23:51:06 +00:00
|
|
|
else:
|
2023-01-23 16:09:12 +00:00
|
|
|
skip()
|
2022-12-06 17:35:56 +00:00
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
proc storagesRunner(
|
|
|
|
noisy = true;
|
|
|
|
persistent = true;
|
|
|
|
sample = storSample;
|
2022-11-28 09:03:23 +00:00
|
|
|
knownFailures: seq[(string,seq[(int,HexaryError)])] = @[]) =
|
2022-09-02 18:16:09 +00:00
|
|
|
let
|
2023-01-23 16:09:12 +00:00
|
|
|
accLst = sample.to(seq[UndumpAccounts])
|
|
|
|
stoLst = sample.to(seq[UndumpStorages])
|
2022-09-02 18:16:09 +00:00
|
|
|
tmpDir = getTmpDir()
|
|
|
|
db = if persistent: tmpDir.testDbs(sample.name, instances=1) else: testDbs()
|
|
|
|
dbDir = db.dbDir.split($DirSep).lastTwo.join($DirSep)
|
|
|
|
info = if db.persistent: &"persistent db on \"{dbDir}\""
|
|
|
|
else: "in-memory db"
|
2023-01-23 16:09:12 +00:00
|
|
|
idPfx = sample.file.splitPath.tail.replace(".txt.gz","")
|
2022-09-02 18:16:09 +00:00
|
|
|
|
|
|
|
defer:
|
|
|
|
if db.persistent:
|
|
|
|
if not db.cdb[0].rocksStoreRef.isNil:
|
|
|
|
db.cdb[0].rocksStoreRef.store.db.rocksdb_close
|
|
|
|
tmpDir.flushDbDir(sample.name)
|
|
|
|
|
2023-01-23 16:09:12 +00:00
|
|
|
suite &"SyncSnap: {idPfx} accounts storage for {info}":
|
|
|
|
let xdb = db.cdb[0].snapDbRef(db.persistent)
|
2022-09-02 18:16:09 +00:00
|
|
|
|
2023-01-23 16:09:12 +00:00
|
|
|
test &"Merging {accLst.len} accounts for state root ..{accLst[0].root.pp}":
|
|
|
|
accLst.test_storageAccountsImport(xdb, db.persistent)
|
2022-09-02 18:16:09 +00:00
|
|
|
|
2023-01-23 16:09:12 +00:00
|
|
|
test &"Merging {stoLst.len} storages lists":
|
|
|
|
stoLst.test_storageSlotsImport(xdb, db.persistent, knownFailures,idPfx)
|
2022-10-20 16:59:54 +00:00
|
|
|
|
2023-01-23 16:09:12 +00:00
|
|
|
test &"Inspecting {stoLst.len} imported storages lists sub-tries":
|
|
|
|
stoLst.test_storageSlotsTries(xdb, db.persistent, knownFailures,idPfx)
|
2022-10-20 16:59:54 +00:00
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc inspectionRunner(
|
|
|
|
noisy = true;
|
|
|
|
persistent = true;
|
|
|
|
cascaded = true;
|
|
|
|
sample: openArray[AccountsSample] = snapTestList) =
|
|
|
|
let
|
|
|
|
inspectList = sample.mapIt(it.to(seq[UndumpAccounts]))
|
|
|
|
tmpDir = getTmpDir()
|
|
|
|
db = if persistent: tmpDir.testDbs(sample[0].name) else: testDbs()
|
|
|
|
dbDir = db.dbDir.split($DirSep).lastTwo.join($DirSep)
|
|
|
|
info = if db.persistent: &"persistent db on \"{dbDir}\""
|
|
|
|
else: "in-memory db"
|
|
|
|
fileInfo = "[" & sample[0].file.splitPath.tail.replace(".txt.gz","") & "..]"
|
|
|
|
|
|
|
|
defer:
|
|
|
|
if db.persistent:
|
|
|
|
for n in 0 ..< nTestDbInstances:
|
|
|
|
if db.cdb[n].rocksStoreRef.isNil:
|
|
|
|
break
|
|
|
|
db.cdb[n].rocksStoreRef.store.db.rocksdb_close
|
|
|
|
tmpDir.flushDbDir(sample[0].name)
|
|
|
|
|
|
|
|
suite &"SyncSnap: inspect {fileInfo} lists for {info} for healing":
|
|
|
|
var
|
|
|
|
singleStats: seq[(int,TrieNodeStat)]
|
|
|
|
accuStats: seq[(int,TrieNodeStat)]
|
2023-01-23 16:09:12 +00:00
|
|
|
let
|
|
|
|
ingerprinting = &"ingerprinting {inspectList.len}"
|
|
|
|
singleAcc = &"F{ingerprinting} single accounts lists"
|
|
|
|
accumAcc = &"F{ingerprinting} accumulated accounts"
|
|
|
|
cascAcc = &"Cascaded f{ingerprinting} accumulated accounts lists"
|
|
|
|
|
|
|
|
memBase = SnapDbRef.init(newMemoryDB())
|
|
|
|
dbSlot = proc(n: int): SnapDbRef =
|
|
|
|
if 2+n < nTestDbInstances and not db.cdb[2+n].rocksStoreRef.isNil:
|
|
|
|
return SnapDbRef.init(db.cdb[2+n])
|
|
|
|
|
|
|
|
test &"{singleAcc} for in-memory-db":
|
|
|
|
inspectList.test_inspectSingleAccountsMemDb(memBase, singleStats)
|
|
|
|
|
|
|
|
test &"{singleAcc} for persistent db":
|
|
|
|
if persistent:
|
|
|
|
inspectList.test_inspectSingleAccountsPersistent(dbSlot, singleStats)
|
2022-09-16 07:24:12 +00:00
|
|
|
else:
|
|
|
|
skip()
|
2023-01-23 16:09:12 +00:00
|
|
|
|
|
|
|
test &"{accumAcc} for in-memory-db":
|
|
|
|
inspectList.test_inspectAccountsInMemDb(memBase, accuStats)
|
|
|
|
|
|
|
|
test &"{accumAcc} for persistent db":
|
|
|
|
if persistent:
|
|
|
|
inspectList.test_inspectAccountsPersistent(db.cdb[0], accuStats)
|
2022-09-16 07:24:12 +00:00
|
|
|
else:
|
|
|
|
skip()
|
2023-01-23 16:09:12 +00:00
|
|
|
|
|
|
|
test &"{cascAcc} for in-memory-db":
|
|
|
|
if cascaded:
|
|
|
|
inspectList.test_inspectCascadedMemDb()
|
2022-09-16 07:24:12 +00:00
|
|
|
else:
|
|
|
|
skip()
|
2023-01-23 16:09:12 +00:00
|
|
|
|
|
|
|
test &"{cascAcc} for persistent db":
|
|
|
|
if cascaded and persistent:
|
|
|
|
inspectList.test_inspectCascadedPersistent(db.cdb[1])
|
2022-09-16 07:24:12 +00:00
|
|
|
else:
|
2023-01-23 16:09:12 +00:00
|
|
|
skip()
|
2022-09-02 18:16:09 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Test Runners: database timing tests
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc importRunner(noisy = true; persistent = true; capture = bChainCapture) =
|
2022-08-12 15:42:07 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
fileInfo = capture.file.splitFile.name.split(".")[0]
|
|
|
|
filePath = capture.file.findFilePath(baseDir,repoDir).value
|
|
|
|
tmpDir = getTmpDir()
|
|
|
|
db = if persistent: tmpDir.testDbs(capture.name) else: testDbs()
|
2023-01-23 16:09:12 +00:00
|
|
|
numBlocks = capture.numBlocks
|
|
|
|
numBlocksInfo = if numBlocks == high(int): "" else: $numBlocks & " "
|
2022-08-12 15:42:07 +00:00
|
|
|
loadNoise = noisy
|
|
|
|
|
|
|
|
defer:
|
|
|
|
if db.persistent:
|
|
|
|
tmpDir.flushDbDir(capture.name)
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
suite &"SyncSnap: using {fileInfo} capture for testing db timings":
|
2023-01-23 16:09:12 +00:00
|
|
|
var ddb: CommonRef # perstent DB on disk
|
2022-08-12 15:42:07 +00:00
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
test &"Create persistent ChainDBRef on {tmpDir}":
|
|
|
|
ddb = CommonRef.new(
|
2023-01-23 16:09:12 +00:00
|
|
|
db = if db.persistent: db.cdb[0].trieDB else: newMemoryDB(),
|
2022-12-02 04:39:12 +00:00
|
|
|
networkId = capture.network,
|
2022-08-12 15:42:07 +00:00
|
|
|
pruneTrie = true,
|
|
|
|
params = capture.network.networkParams)
|
|
|
|
ddb.initializeEmptyDb
|
|
|
|
|
|
|
|
test &"Storing {numBlocksInfo}persistent blocks from dump":
|
2023-01-23 16:09:12 +00:00
|
|
|
noisy.test_dbTimingUndumpBlocks(filePath, ddb, numBlocks, loadNoise)
|
2022-08-12 15:42:07 +00:00
|
|
|
|
|
|
|
test "Extract key-value records into memory tables via rocksdb iterator":
|
|
|
|
if db.cdb[0].rocksStoreRef.isNil:
|
2023-01-23 16:09:12 +00:00
|
|
|
skip() # not persistent => db.cdb[0] is nil
|
2022-08-12 15:42:07 +00:00
|
|
|
else:
|
2023-01-23 16:09:12 +00:00
|
|
|
noisy.test_dbTimingRockySetup(xTab32, xTab33, db.cdb[0])
|
|
|
|
|
|
|
|
|
|
|
|
proc dbTimingRunner(noisy = true; persistent = true; cleanUp = true) =
|
2022-08-12 15:42:07 +00:00
|
|
|
let
|
|
|
|
fullNoise = false
|
|
|
|
var
|
|
|
|
emptyDb = "empty"
|
|
|
|
|
|
|
|
# Allows to repeat storing on existing data
|
|
|
|
if not xDbs.cdb[0].isNil:
|
|
|
|
emptyDb = "pre-loaded"
|
|
|
|
elif persistent:
|
|
|
|
xTmpDir = getTmpDir()
|
|
|
|
xDbs = xTmpDir.testDbs("store-runner")
|
|
|
|
else:
|
|
|
|
xDbs = testDbs()
|
|
|
|
|
|
|
|
defer:
|
|
|
|
if xDbs.persistent and cleanUp:
|
2022-09-16 07:24:12 +00:00
|
|
|
for n in 0 ..< nTestDbInstances:
|
|
|
|
if xDbs.cdb[n].rocksStoreRef.isNil:
|
|
|
|
break
|
|
|
|
xDbs.cdb[n].rocksStoreRef.store.db.rocksdb_close
|
2022-08-12 15:42:07 +00:00
|
|
|
xTmpDir.flushDbDir("store-runner")
|
|
|
|
xDbs.reset
|
|
|
|
|
|
|
|
suite &"SyncSnap: storage tests on {emptyDb} databases":
|
|
|
|
#
|
|
|
|
# `xDbs` instance slots layout:
|
|
|
|
#
|
|
|
|
# * cdb[0] -- direct db, key length 32, no transaction
|
|
|
|
# * cdb[1] -- direct db, key length 32 as 33, no transaction
|
|
|
|
#
|
|
|
|
# * cdb[2] -- direct db, key length 32, transaction based
|
|
|
|
# * cdb[3] -- direct db, key length 32 as 33, transaction based
|
|
|
|
#
|
|
|
|
# * cdb[4] -- direct db, key length 33, no transaction
|
|
|
|
# * cdb[5] -- direct db, key length 33, transaction based
|
|
|
|
#
|
|
|
|
# * cdb[6] -- rocksdb, key length 32
|
|
|
|
# * cdb[7] -- rocksdb, key length 32 as 33
|
|
|
|
# * cdb[8] -- rocksdb, key length 33
|
|
|
|
#
|
|
|
|
doAssert 9 <= nTestDbInstances
|
2022-09-02 18:16:09 +00:00
|
|
|
doAssert not xDbs.cdb[8].isNil
|
2022-08-12 15:42:07 +00:00
|
|
|
|
2023-01-23 16:09:12 +00:00
|
|
|
let
|
|
|
|
storeDir32 = &"Directly store {xTab32.len} records"
|
|
|
|
storeDir33 = &"Directly store {xTab33.len} records"
|
|
|
|
storeTx32 = &"Transactionally store directly {xTab32.len} records"
|
|
|
|
storeTx33 = &"Transactionally store directly {xTab33.len} records"
|
|
|
|
intoTrieDb = &"into {emptyDb} trie db"
|
|
|
|
|
|
|
|
storeRks32 = &"Store {xTab32.len} records"
|
|
|
|
storeRks33 = &"Store {xTab33.len} records"
|
|
|
|
intoRksDb = &"into {emptyDb} rocksdb table"
|
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
if xTab32.len == 0 or xTab33.len == 0:
|
|
|
|
test &"Both tables with 32 byte keys(size={xTab32.len}), " &
|
|
|
|
&"33 byte keys(size={xTab32.len}) must be non-empty":
|
|
|
|
skip()
|
|
|
|
else:
|
2023-01-23 16:09:12 +00:00
|
|
|
test &"{storeDir32} (key length 32) {intoTrieDb}":
|
|
|
|
noisy.test_dbTimingStoreDirect32(xTab32, xDbs.cdb[0])
|
|
|
|
|
|
|
|
test &"{storeDir32} (key length 33) {intoTrieDb}":
|
|
|
|
noisy.test_dbTimingStoreDirectly32as33(xTab32, xDbs.cdb[1])
|
|
|
|
|
|
|
|
test &"{storeTx32} (key length 32) {intoTrieDb}":
|
|
|
|
noisy.test_dbTimingStoreTx32(xTab32, xDbs.cdb[2])
|
|
|
|
|
|
|
|
test &"{storeTx32} (key length 33) {intoTrieDb}":
|
|
|
|
noisy.test_dbTimingStoreTx32as33(xTab32, xDbs.cdb[3])
|
|
|
|
|
|
|
|
test &"{storeDir33} (key length 33) {intoTrieDb}":
|
|
|
|
noisy.test_dbTimingDirect33(xTab33, xDbs.cdb[4])
|
|
|
|
|
|
|
|
test &"{storeTx33} (key length 33) {intoTrieDb}":
|
|
|
|
noisy.test_dbTimingTx33(xTab33, xDbs.cdb[5])
|
2022-08-12 15:42:07 +00:00
|
|
|
|
|
|
|
if xDbs.cdb[0].rocksStoreRef.isNil:
|
2022-09-02 18:16:09 +00:00
|
|
|
test "The rocksdb interface must be available": skip()
|
2022-08-12 15:42:07 +00:00
|
|
|
else:
|
2023-01-23 16:09:12 +00:00
|
|
|
test &"{storeRks32} (key length 32) {intoRksDb}":
|
|
|
|
noisy.test_dbTimingRocky32(xTab32, xDbs.cdb[6], fullNoise)
|
|
|
|
|
|
|
|
test &"{storeRks32} (key length 33) {intoRksDb}":
|
|
|
|
noisy.test_dbTimingRocky32as33(xTab32, xDbs.cdb[7], fullNoise)
|
|
|
|
|
|
|
|
test &"{storeRks33} (key length 33) {intoRksDb}":
|
|
|
|
noisy.test_dbTimingRocky33(xTab33, xDbs.cdb[8], fullNoise)
|
2022-07-01 11:42:17 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Main function(s)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc syncSnapMain*(noisy = defined(debug)) =
|
2022-08-15 15:51:50 +00:00
|
|
|
noisy.accountsRunner(persistent=true)
|
2022-12-06 17:35:56 +00:00
|
|
|
noisy.accountsRunner(persistent=false)
|
2022-08-12 15:42:07 +00:00
|
|
|
noisy.importRunner() # small sample, just verify functionality
|
2022-09-16 07:24:12 +00:00
|
|
|
noisy.inspectionRunner()
|
2023-01-23 16:09:12 +00:00
|
|
|
noisy.dbTimingRunner()
|
2022-07-01 11:42:17 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
2022-08-04 08:04:30 +00:00
|
|
|
const
|
|
|
|
noisy = defined(debug) or true
|
2022-08-17 07:30:11 +00:00
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
#setTraceLevel()
|
2022-08-04 08:04:30 +00:00
|
|
|
setErrorLevel()
|
2022-07-01 11:42:17 +00:00
|
|
|
|
2022-08-17 07:30:11 +00:00
|
|
|
# The `accountsRunner()` tests a snap sync functionality for storing chain
|
|
|
|
# chain data directly rather than derive them by executing the EVM. Here,
|
|
|
|
# only accounts are considered.
|
|
|
|
#
|
|
|
|
# The `snap/1` protocol allows to fetch data for a certain account range. The
|
|
|
|
# following boundary conditions apply to the received data:
|
|
|
|
#
|
|
|
|
# * `State root`: All data are relaive to the same state root.
|
|
|
|
#
|
|
|
|
# * `Accounts`: There is an accounts interval sorted in strictly increasing
|
|
|
|
# order. The accounts are required consecutive, i.e. without holes in
|
|
|
|
# between although this cannot be verified immediately.
|
|
|
|
#
|
|
|
|
# * `Lower bound`: There is a start value which might be lower than the first
|
|
|
|
# account hash. There must be no other account between this start value and
|
|
|
|
# the first account (not verifyable yet.) For all practicat purposes, this
|
|
|
|
# value is mostly ignored but carried through.
|
|
|
|
#
|
|
|
|
# * `Proof`: There is a list of hexary nodes which allow to build a partial
|
2022-10-20 16:59:54 +00:00
|
|
|
# Patricia-Merkle trie starting at the state root with all the account
|
2022-08-17 07:30:11 +00:00
|
|
|
# leaves. There are enough nodes that show that there is no account before
|
|
|
|
# the least account (which is currently ignored.)
|
2022-09-02 18:16:09 +00:00
|
|
|
#
|
2022-08-17 07:30:11 +00:00
|
|
|
# There are test data samples on the sub-directory `test_sync_snap`. These
|
|
|
|
# are complete replies for some (admittedly smapp) test requests from a `kiln`
|
|
|
|
# session.
|
|
|
|
#
|
|
|
|
# The `accountsRunner()` does three tests:
|
|
|
|
#
|
|
|
|
# 1. Run the `importAccounts()` function which is the all-in-one production
|
|
|
|
# function processoing the data described above. The test applies it
|
|
|
|
# sequentially to about 20 data sets.
|
|
|
|
#
|
|
|
|
# 2. Test individual functional items which are hidden in test 1. while
|
2022-09-02 18:16:09 +00:00
|
|
|
# merging the sample data.
|
2022-08-17 07:30:11 +00:00
|
|
|
# * Load/accumulate `proofs` data from several samples
|
|
|
|
# * Load/accumulate accounts (needs some unique sorting)
|
|
|
|
# * Build/complete hexary trie for accounts
|
|
|
|
# * Save/bulk-store hexary trie on disk. If rocksdb is available, data
|
2022-10-20 16:59:54 +00:00
|
|
|
# are bulk stored via sst.
|
2022-08-17 07:30:11 +00:00
|
|
|
#
|
|
|
|
# 3. Traverse trie nodes stored earlier. The accounts from test 2 are
|
|
|
|
# re-visted using the account hash as access path.
|
|
|
|
#
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
# This one uses dumps from the external `nimbus-eth1-blob` repo
|
Prep for full sync after snap make 4 (#1282)
* Re-arrange fetching storage slots in batch module
why;
Previously, fetching partial slot ranges first has a chance of
terminating the worker peer 9due to network error) while there were
many inheritable storage slots on the queue.
Now, inheritance is checked first, then full slot ranges and finally
partial ranges.
* Update logging
* Bundled node information for healing into single object `NodeSpecs`
why:
Previously, partial paths and node keys were kept in separate variables.
This approach was error prone due to copying/reassembling function
argument objects.
As all partial paths, keys, and node data types are more or less handled
as `Blob`s over the network (using Eth/6x, or Snap/1) it makes sense to
hold these `Blob`s as named field in a single object (even if not all
fields are active for the current purpose.)
* For good housekeeping, using `NodeKey` type only for account keys
why:
previously, a mixture of `NodeKey` and `Hash256` was used. Now, only
state or storage root keys use the `Hash256` type.
* Always accept latest pivot (and not a slightly older one)
why;
For testing it was tried to use a slightly older pivot state root than
available. Some anecdotal tests seemed to suggest an advantage so that
more peers are willing to serve on that older pivot. But this could not
be confirmed in subsequent tests (still anecdotal, though.)
As a side note, the distance of the latest pivot to its predecessor is
at least 128 (or whatever the constant `minPivotBlockDistance` is
assigned to.)
* Reshuffle name components for some file and function names
why:
Clarifies purpose:
"storages" becomes: "storage slots"
"store" becomes: "range fetch"
* Stash away currently unused modules in sub-folder named "notused"
2022-10-27 13:49:28 +00:00
|
|
|
when true and false:
|
2022-09-02 18:16:09 +00:00
|
|
|
import ./test_sync_snap/snap_other_xx
|
|
|
|
noisy.showElapsed("accountsRunner()"):
|
|
|
|
for n,sam in snapOtherList:
|
2022-12-06 20:13:31 +00:00
|
|
|
false.accountsRunner(persistent=true, sam)
|
2022-09-16 07:24:12 +00:00
|
|
|
noisy.showElapsed("inspectRunner()"):
|
|
|
|
for n,sam in snapOtherHealingList:
|
|
|
|
false.inspectionRunner(persistent=true, cascaded=false, sam)
|
2022-09-02 18:16:09 +00:00
|
|
|
|
|
|
|
# This one usues dumps from the external `nimbus-eth1-blob` repo
|
Prep for full sync after snap make 4 (#1282)
* Re-arrange fetching storage slots in batch module
why;
Previously, fetching partial slot ranges first has a chance of
terminating the worker peer 9due to network error) while there were
many inheritable storage slots on the queue.
Now, inheritance is checked first, then full slot ranges and finally
partial ranges.
* Update logging
* Bundled node information for healing into single object `NodeSpecs`
why:
Previously, partial paths and node keys were kept in separate variables.
This approach was error prone due to copying/reassembling function
argument objects.
As all partial paths, keys, and node data types are more or less handled
as `Blob`s over the network (using Eth/6x, or Snap/1) it makes sense to
hold these `Blob`s as named field in a single object (even if not all
fields are active for the current purpose.)
* For good housekeeping, using `NodeKey` type only for account keys
why:
previously, a mixture of `NodeKey` and `Hash256` was used. Now, only
state or storage root keys use the `Hash256` type.
* Always accept latest pivot (and not a slightly older one)
why;
For testing it was tried to use a slightly older pivot state root than
available. Some anecdotal tests seemed to suggest an advantage so that
more peers are willing to serve on that older pivot. But this could not
be confirmed in subsequent tests (still anecdotal, though.)
As a side note, the distance of the latest pivot to its predecessor is
at least 128 (or whatever the constant `minPivotBlockDistance` is
assigned to.)
* Reshuffle name components for some file and function names
why:
Clarifies purpose:
"storages" becomes: "storage slots"
"store" becomes: "range fetch"
* Stash away currently unused modules in sub-folder named "notused"
2022-10-27 13:49:28 +00:00
|
|
|
when true and false:
|
2022-09-02 18:16:09 +00:00
|
|
|
import ./test_sync_snap/snap_storage_xx
|
|
|
|
let knownFailures = @[
|
|
|
|
("storages3__18__25_dump#11", @[( 233, RightBoundaryProofFailed)]),
|
|
|
|
("storages4__26__33_dump#11", @[(1193, RightBoundaryProofFailed)]),
|
|
|
|
("storages5__34__41_dump#10", @[( 508, RootNodeMismatch)]),
|
|
|
|
("storagesB__84__92_dump#6", @[( 325, RightBoundaryProofFailed)]),
|
|
|
|
("storagesD_102_109_dump#17", @[(1102, RightBoundaryProofFailed)]),
|
|
|
|
]
|
|
|
|
noisy.showElapsed("storageRunner()"):
|
|
|
|
for n,sam in snapStorageList:
|
2022-09-16 07:24:12 +00:00
|
|
|
false.storagesRunner(persistent=true, sam, knownFailures)
|
2022-09-02 18:16:09 +00:00
|
|
|
|
|
|
|
# This one uses readily available dumps
|
2022-08-24 13:44:18 +00:00
|
|
|
when true: # and false:
|
2022-09-16 07:24:12 +00:00
|
|
|
false.inspectionRunner()
|
2022-12-06 17:35:56 +00:00
|
|
|
for n,sam in snapTestList:
|
|
|
|
false.accountsRunner(persistent=false, sam)
|
2022-09-02 18:16:09 +00:00
|
|
|
false.accountsRunner(persistent=true, sam)
|
2022-12-06 17:35:56 +00:00
|
|
|
for n,sam in snapTestStorageList:
|
|
|
|
false.accountsRunner(persistent=false, sam)
|
2022-09-02 18:16:09 +00:00
|
|
|
false.accountsRunner(persistent=true, sam)
|
|
|
|
false.storagesRunner(persistent=true, sam)
|
|
|
|
|
|
|
|
# This one uses readily available dumps
|
|
|
|
when true and false:
|
2022-08-15 15:51:50 +00:00
|
|
|
# ---- database storage timings -------
|
2022-08-12 15:42:07 +00:00
|
|
|
|
2022-08-15 15:51:50 +00:00
|
|
|
noisy.showElapsed("importRunner()"):
|
|
|
|
noisy.importRunner(capture = bulkTest0)
|
2022-08-12 15:42:07 +00:00
|
|
|
|
2023-01-23 16:09:12 +00:00
|
|
|
noisy.showElapsed("dbTimingRunner()"):
|
|
|
|
true.dbTimingRunner(cleanUp = false)
|
|
|
|
true.dbTimingRunner()
|
2022-07-01 11:42:17 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|