nimbus-eth1/tests/test_sync_snap/test_storage.nim
Jordan Hrycaj 6ca6bcd96f
Snap sync fix trie interpolation fringe condition (#1457)
* Cosmetics

details:
+ Update doc generator
+ Fix key type representation in `hexary_desc` for debugging
+ Redefine `isImportOk()` as template for better `check()` line reporting

* Fix fringe condition when interpolating Merkle-Patricia tries

details:
  Small change with profound effect fixing some pathological condition
  that haunted the unit test set on large data sers. There is still one
  condition left which might well be due to an incomplete data set.

* Unit test proof nodes for node range extractor

* Unit tests to run on full extraction set

why:
  Left over from troubleshooting, range length was only 5
2023-02-01 18:56:06 +00:00

109 lines
3.5 KiB
Nim

# 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.
## Snap sync components tester and TDD environment
import
std/[sequtils, tables],
eth/[common, p2p],
unittest2,
../../nimbus/db/select_backend,
../../nimbus/sync/snap/range_desc,
../../nimbus/sync/snap/worker/db/[
hexary_desc, hexary_error, hexary_inspect,
snapdb_accounts, snapdb_desc, snapdb_storage_slots],
../replay/[pp, undump_accounts, undump_storages],
./test_helpers
let
# Forces `check()` to print the error (as opposed when using `isOk()`)
OkStoDb = Result[void,seq[(int,HexaryError)]].ok()
# ------------------------------------------------------------------------------
# Private helpers
# ------------------------------------------------------------------------------
proc toStoDbRc(r: seq[HexaryNodeReport]): Result[void,seq[(int,HexaryError)]]=
## Kludge: map error report to (older version) return code
if r.len != 0:
return err(r.mapIt((it.slot.get(otherwise = -1),it.error)))
ok()
# ------------------------------------------------------------------------------
# Public test function
# ------------------------------------------------------------------------------
proc test_storageAccountsImport*(
inList: seq[UndumpAccounts];
dbBase: SnapDbRef;
persistent: bool;
) =
## Import and merge accounts lists
let
root = inList[0].root
for w in inList:
let desc = SnapDbAccountsRef.init(dbBase, root, Peer())
check desc.importAccounts(w.base, w.data, persistent).isImportOk
proc test_storageSlotsImport*(
inList: seq[UndumpStorages];
dbBase: SnapDbRef;
persistent: bool;
ignore: KnownStorageFailure;
idPfx: string;
) =
## Import and merge storages lists
let
skipEntry = ignore.toTable
dbDesc = SnapDbStorageSlotsRef.init(
dbBase, NodeKey.default, Hash256(), Peer())
for n,w in inList:
let
testId = idPfx & "#" & $n
expRc = if skipEntry.hasKey(testId):
Result[void,seq[(int,HexaryError)]].err(skipEntry[testId])
else:
OkStoDb
check dbDesc.importStorageSlots(w.data, persistent).toStoDbRc == expRc
proc test_storageSlotsTries*(
inList: seq[UndumpStorages];
dbBase: SnapDbRef;
persistent: bool;
ignore: KnownStorageFailure;
idPfx: string;
) =
## Inspecting imported storages lists sub-tries
let
skipEntry = ignore.toTable
for n,w in inList:
let
testId = idPfx & "#" & $n
errInx = if skipEntry.hasKey(testId): skipEntry[testId][0][0]
else: high(int)
for m in 0 ..< w.data.storages.len:
let
accKey = w.data.storages[m].account.accKey
root = w.data.storages[m].account.storageRoot
dbDesc = SnapDbStorageSlotsRef.init(dbBase, accKey, root, Peer())
rc = dbDesc.inspectStorageSlotsTrie(persistent=persistent)
if m == errInx:
check rc == Result[TrieNodeStat,HexaryError].err(TrieIsEmpty)
else:
check rc.isOk # ok => level > 0 and not stopped
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------