2023-06-02 10:04:29 +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.
|
|
|
|
|
|
|
|
## Aristo (aka Patricia) DB records merge test
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[algorithm, sequtils, sets],
|
|
|
|
eth/common,
|
|
|
|
stew/results,
|
|
|
|
unittest2,
|
|
|
|
../../nimbus/db/aristo/[
|
2023-06-12 13:48:47 +00:00
|
|
|
aristo_desc, aristo_debug, aristo_merge, aristo_nearby],
|
2023-06-02 10:04:29 +00:00
|
|
|
./test_helpers
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc fwdWalkLeafsCompleteDB(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef;
|
2023-06-09 11:17:37 +00:00
|
|
|
root: VertexID;
|
2023-06-12 18:16:03 +00:00
|
|
|
tags: openArray[HashID];
|
2023-06-02 10:04:29 +00:00
|
|
|
noisy: bool;
|
|
|
|
): tuple[visited: int, error: AristoError] =
|
|
|
|
let
|
|
|
|
tLen = tags.len
|
|
|
|
var
|
2023-06-12 18:16:03 +00:00
|
|
|
lty = LeafTie(root: root, path: HashID(tags[0].u256 div 2))
|
2023-06-02 10:04:29 +00:00
|
|
|
n = 0
|
|
|
|
while true:
|
2023-06-30 22:22:33 +00:00
|
|
|
let rc = lty.right(db)
|
2023-06-02 10:04:29 +00:00
|
|
|
#noisy.say "=================== ", n
|
|
|
|
if rc.isErr:
|
2023-06-30 22:22:33 +00:00
|
|
|
if rc.error[1] != NearbyBeyondRange:
|
2023-06-02 10:04:29 +00:00
|
|
|
noisy.say "***", "[", n, "/", tLen-1, "] fwd-walk error=", rc.error
|
2023-06-30 22:22:33 +00:00
|
|
|
check rc.error == (0,0)
|
|
|
|
return (n,rc.error[1])
|
|
|
|
if n != tLen:
|
2023-06-02 10:04:29 +00:00
|
|
|
check n == tLen
|
2023-06-30 22:22:33 +00:00
|
|
|
return (n,AristoError(1))
|
2023-06-02 10:04:29 +00:00
|
|
|
break
|
|
|
|
if tLen <= n:
|
|
|
|
noisy.say "***", "[", n, "/", tLen-1, "] fwd-walk -- ",
|
|
|
|
" oops, too many leafs (index overflow)"
|
|
|
|
check n < tlen
|
2023-06-30 22:22:33 +00:00
|
|
|
return (n,AristoError(1))
|
2023-06-09 11:17:37 +00:00
|
|
|
if rc.value.path != tags[n]:
|
2023-06-02 10:04:29 +00:00
|
|
|
noisy.say "***", "[", n, "/", tLen-1, "] fwd-walk -- leafs differ,",
|
|
|
|
" got=", rc.value.pp(db),
|
2023-06-12 13:48:47 +00:00
|
|
|
" wanted=", LeafTie(root: root, path: tags[n]).pp(db) #,
|
|
|
|
# " db-dump\n ", db.pp
|
2023-06-09 11:17:37 +00:00
|
|
|
check rc.value.path == tags[n]
|
2023-06-30 22:22:33 +00:00
|
|
|
return (n,AristoError(1))
|
2023-06-12 18:16:03 +00:00
|
|
|
if rc.value.path < high(HashID):
|
|
|
|
lty.path = HashID(rc.value.path.u256 + 1)
|
2023-06-02 10:04:29 +00:00
|
|
|
n.inc
|
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
(n,AristoError(0))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc revWalkLeafsCompleteDB(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef;
|
2023-06-09 11:17:37 +00:00
|
|
|
root: VertexID;
|
2023-06-12 18:16:03 +00:00
|
|
|
tags: openArray[HashID];
|
2023-06-02 10:04:29 +00:00
|
|
|
noisy: bool;
|
2023-06-09 11:17:37 +00:00
|
|
|
): tuple[visited: int, error: AristoError] =
|
2023-06-02 10:04:29 +00:00
|
|
|
let
|
|
|
|
tLen = tags.len
|
|
|
|
var
|
|
|
|
delta = ((high(UInt256) - tags[^1].u256) div 2)
|
2023-06-12 18:16:03 +00:00
|
|
|
lty = LeafTie(root: root, path: HashID(tags[^1].u256 + delta))
|
2023-06-02 10:04:29 +00:00
|
|
|
n = tLen-1
|
|
|
|
while true: # and false:
|
2023-06-30 22:22:33 +00:00
|
|
|
let rc = lty.left(db)
|
2023-06-02 10:04:29 +00:00
|
|
|
if rc.isErr:
|
2023-06-30 22:22:33 +00:00
|
|
|
if rc.error[1] != NearbyBeyondRange:
|
2023-06-02 10:04:29 +00:00
|
|
|
noisy.say "***", "[", n, "/", tLen-1, "] rev-walk error=", rc.error
|
2023-06-30 22:22:33 +00:00
|
|
|
check rc.error == (0,0)
|
|
|
|
return (n,rc.error[1])
|
|
|
|
if n != -1:
|
2023-06-02 10:04:29 +00:00
|
|
|
check n == -1
|
2023-06-30 22:22:33 +00:00
|
|
|
return (n,AristoError(1))
|
2023-06-02 10:04:29 +00:00
|
|
|
break
|
|
|
|
if n < 0:
|
|
|
|
noisy.say "***", "[", n, "/", tLen-1, "] rev-walk -- ",
|
|
|
|
" oops, too many leafs (index underflow)"
|
|
|
|
check 0 <= n
|
2023-06-30 22:22:33 +00:00
|
|
|
return (n,AristoError(1))
|
2023-06-09 11:17:37 +00:00
|
|
|
if rc.value.path != tags[n]:
|
2023-06-02 10:04:29 +00:00
|
|
|
noisy.say "***", "[", n, "/", tLen-1, "] rev-walk -- leafs differ,",
|
|
|
|
" got=", rc.value.pp(db),
|
|
|
|
" wanted=", tags[n]..pp(db) #, " db-dump\n ", db.pp
|
2023-06-09 11:17:37 +00:00
|
|
|
check rc.value.path == tags[n]
|
2023-06-30 22:22:33 +00:00
|
|
|
return (n,AristoError(1))
|
2023-06-12 18:16:03 +00:00
|
|
|
if low(HashID) < rc.value.path:
|
|
|
|
lty.path = HashID(rc.value.path.u256 - 1)
|
2023-06-02 10:04:29 +00:00
|
|
|
n.dec
|
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
(tLen-1 - n, AristoError(0))
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public test function
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc test_nearbyKvpList*(
|
|
|
|
noisy: bool;
|
|
|
|
list: openArray[ProofTrieData];
|
|
|
|
resetDb = false;
|
2023-06-09 11:17:37 +00:00
|
|
|
): bool =
|
2023-07-04 18:24:03 +00:00
|
|
|
let
|
|
|
|
db = AristoDbRef()
|
2023-06-02 10:04:29 +00:00
|
|
|
var
|
2023-06-12 18:16:03 +00:00
|
|
|
rootKey = HashKey.default
|
|
|
|
tagSet: HashSet[HashID]
|
2023-06-09 11:17:37 +00:00
|
|
|
count = 0
|
2023-06-02 10:04:29 +00:00
|
|
|
for n,w in list:
|
2023-06-09 11:17:37 +00:00
|
|
|
if resetDb or w.root != rootKey:
|
|
|
|
db.top = AristoLayerRef()
|
|
|
|
rootKey = w.root
|
2023-06-02 10:04:29 +00:00
|
|
|
tagSet.reset
|
2023-06-09 11:17:37 +00:00
|
|
|
count = 0
|
|
|
|
count.inc
|
|
|
|
|
2023-06-02 10:04:29 +00:00
|
|
|
let
|
|
|
|
lstLen = list.len
|
2023-06-09 11:17:37 +00:00
|
|
|
lTabLen = db.top.lTab.len
|
|
|
|
leafs = w.kvpLst.mapRootVid VertexID(1) # merge into main trie
|
2023-06-02 10:04:29 +00:00
|
|
|
added = db.merge leafs
|
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
if added.error != AristoError(0):
|
|
|
|
check added.error == AristoError(0)
|
|
|
|
return
|
|
|
|
|
|
|
|
check db.top.lTab.len == lTabLen + added.merged
|
2023-06-02 10:04:29 +00:00
|
|
|
check added.merged + added.dups == leafs.len
|
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
for kvp in leafs:
|
2023-06-12 13:48:47 +00:00
|
|
|
tagSet.incl kvp.leafTie.path
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
tags = tagSet.toSeq.sorted
|
2023-06-12 13:48:47 +00:00
|
|
|
rootVid = leafs[0].leafTie.root
|
2023-06-09 11:17:37 +00:00
|
|
|
fwdWalk = db.fwdWalkLeafsCompleteDB(rootVid, tags, noisy=true)
|
|
|
|
revWalk = db.revWalkLeafsCompleteDB(rootVid, tags, noisy=true)
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
check fwdWalk.error == AristoError(0)
|
|
|
|
check revWalk.error == AristoError(0)
|
|
|
|
check fwdWalk == revWalk
|
|
|
|
|
|
|
|
if {fwdWalk.error, revWalk.error} != {AristoError(0)}:
|
2023-06-09 11:17:37 +00:00
|
|
|
noisy.say "***", "<", n, "/", lstLen-1, ">",
|
|
|
|
" groups=", count, " db dump",
|
2023-06-02 10:04:29 +00:00
|
|
|
"\n post-state ", db.pp,
|
|
|
|
"\n"
|
2023-06-09 11:17:37 +00:00
|
|
|
return
|
2023-06-02 10:04:29 +00:00
|
|
|
|
|
|
|
#noisy.say "***", "sample ",n,"/",lstLen-1, " visited=", fwdWalk.visited
|
|
|
|
|
2023-06-09 11:17:37 +00:00
|
|
|
true
|
|
|
|
|
2023-06-02 10:04:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|