Skip to content

Commit

Permalink
Revive json tracer unit tests (#2538)
Browse files Browse the repository at this point in the history
* Some `Aristo` clean-ups/updates

* Re-implemented core-db tracer functionality

* Rename nimbus tracer `no-tracer.nim` => `tracer.nim`

why:
  Restore original name for easy diff tracking with upcoming update

* Update nimbus tracer using new core-db tracer functionality

* Updating json tracer unit tests

* Enable json tracer unit tests
  • Loading branch information
mjfh authored Aug 1, 2024
1 parent e331c9e commit 01b5c08
Show file tree
Hide file tree
Showing 15 changed files with 1,442 additions and 221 deletions.
2 changes: 2 additions & 0 deletions nimbus/db/aristo/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
of proof nodes is rather small. Also, a right boundary leaf node is
typically cleared. This needs to be re-checked when writing the `proof`
function mentioned above.

* `aristo_nearby` also qualifies for a re-write, now
4 changes: 2 additions & 2 deletions nimbus/db/aristo/aristo_debug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ proc pp*(
): string =
sTab.ppXTab(db.orDefault)

proc pp*(root: VertexID, leg: Leg; db = AristoDbRef(nil)): string =
proc pp*(leg: Leg; root: VertexID; db = AristoDbRef(nil)): string =
let db = db.orDefault()
result = "(" & leg.wp.vid.ppVid & ","
block:
Expand All @@ -583,7 +583,7 @@ proc pp*(hike: Hike; db = AristoDbRef(nil); indent = 4): string =
else:
if hike.legs[0].wp.vid != hike.root:
result &= "(" & hike.root.ppVid & ")" & pfx
result &= hike.legs.mapIt(pp(hike.root, it, db)).join(pfx)
result &= hike.legs.mapIt(it.pp(hike.root, db)).join(pfx)
result &= pfx & "(" & hike.tail.ppPathPfx & ")"
result &= "]"

Expand Down
3 changes: 2 additions & 1 deletion nimbus/db/aristo/aristo_desc/desc_error.nim
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ type


# Part/proof node errors
PartArgNotInCore
PartArgNotGenericRoot
PartArgRootAlreadyUsed
PartArgRootAlreadyOnDatabase
PartChkChangedKeyNotInKeyTab
PartChkChangedVtxMissing
PartChkCoreKeyLookupFailed
Expand Down Expand Up @@ -188,7 +190,6 @@ type
PartRlpPayloadException
PartRootKeysDontMatch
PartRootVidsDontMatch
PartRootAlreadyOnDatabase
PartVtxSlotWasModified
PartVtxSlotWasNotModified

Expand Down
5 changes: 5 additions & 0 deletions nimbus/db/aristo/aristo_desc/desc_identifiers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ func to*(n: UInt256; T: type PathID): T =
## Representation of a scalar as `PathID` (preserving full information)
T(pfx: n, length: 64)

func to*(a: PathID; T: type UInt256): T =
if not a.pfx.isZero:
assert a.length < 64 # debugging only
result = a.pfx shr (4 * (64 - a.length))

# ------------------------------------------------------------------------------
# Public helpers: Miscellaneous mappings
# ------------------------------------------------------------------------------
Expand Down
26 changes: 16 additions & 10 deletions nimbus/db/aristo/aristo_nearby.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ proc zeroAdjust(
if 0 < hike.legs.len:
return ok(hike)

let root = db.getVtx (hike.root, hike.root)
if root.isValid:
let rootVtx = db.getVtx (hike.root, hike.root)
if rootVtx.isValid:
block fail:
var pfx: NibblesBuf
case root.vType:
case rootVtx.vType:
of Branch:
# Find first non-dangling link and assign it
let nibbleID = block:
Expand All @@ -166,18 +166,28 @@ proc zeroAdjust(
if hike.tail.len == 0:
break fail
hike.tail[0].int8
let n = root.branchBorderNibble nibbleID
let n = rootVtx.branchBorderNibble nibbleID
if n < 0:
# Before or after the database range
return err((hike.root,NearbyBeyondRange))
pfx = root.ePfx & NibblesBuf.nibble(n.byte)
pfx = rootVtx.ePfx & NibblesBuf.nibble(n.byte)

of Leaf:
pfx = root.lPfx
pfx = rootVtx.lPfx
if not hike.accept pfx:
# Before or after the database range
return err((hike.root,NearbyBeyondRange))

# Pathological case: matching `rootVtx` which is a leaf
if hike.legs.len == 0 and hike.tail.len == 0:
return ok(Hike(
root: hike.root,
legs: @[Leg(
nibble: -1,
wp: VidVtxPair(
vid: hike.root,
vtx: rootVtx))]))

var newHike = pfx.toHike(hike.root, db)
if 0 < newHike.legs.len:
return ok(newHike)
Expand Down Expand Up @@ -268,10 +278,6 @@ proc nearbyNext(
# Some easy cases
let hike = ? hike.zeroAdjust(db, doLeast=moveRight)

# if hike.legs[^1].wp.vtx.vType == Extension:
# let vid = hike.legs[^1].wp.vtx.eVid
# return hike.complete(vid, db, hikeLenMax, doLeast=moveRight)

var
uHike = hike
start = true
Expand Down
16 changes: 14 additions & 2 deletions nimbus/db/aristo/aristo_part.nim
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,36 @@ proc partPut*(
ok()


proc partGetSubTree*(ps: PartStateRef; rootHash: Hash256): VertexID =
## For the argument `roothash` retrieve the root vertex ID of a particular
## sub tree from the partial state descriptor argument `ps`. The function
## returns `VertexID(0)` if there is no match.
##
for vid in ps.core.keys:
if ps[vid].to(Hash256) == rootHash:
return vid


proc partReRoot*(
ps: PartStateRef;
frRoot: VertexID;
toRoot: VertexID;
): Result[void,AristoError] =
## Realign a generic root vertex (i.e `$2`..`$(LEAST_FREE_VID-1)`) for a
## `proof` state to a new root vertex.
if frRoot notin ps.core or frRoot == toRoot:
if frRoot == toRoot:
return ok() # nothing to do

if frRoot notin ps.core:
return err(PartArgNotInCore)
if frRoot < VertexID(2) or LEAST_FREE_VID <= frRoot.ord or
toRoot < VertexID(2) or LEAST_FREE_VID <= toRoot.ord:
return err(PartArgNotGenericRoot)
# Verify that the tree slot is free
if toRoot in ps.core:
return err(PartArgRootAlreadyUsed)
if ps.db.getVtx((toRoot,toRoot)).isValid:
return err(PartRootAlreadyOnDatabase)
return err(PartArgRootAlreadyOnDatabase)

# Migrate
for key in ps.byKey.keys:
Expand Down
7 changes: 4 additions & 3 deletions nimbus/db/aristo/aristo_part/part_debug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ proc pp*(
let
pfx0 = indent.toPfx()
pfx1 = indent.toPfx(1)

pfx2 = indent.toPfx(2)
var pfx = ""
if dbOk:
result &= pfx & "<db>" & pfx1 & ps.db.pp(
Expand All @@ -147,9 +147,10 @@ proc pp*(
if 0 < len:
var qfx = ""
result &= pfx1 & "{"
for (vid,vLst) in ps.core.pairs:
for vid in ps.core.keys.toSeq.sorted:
let vLst = ps.core.getOrDefault vid
result &= qfx & "(" & vid.pp & ":" & vLst.pp(ps) & ")"
qfx = pfx1
qfx = pfx2
result &= "}"
pfx = pfx0
if byKeyOk:
Expand Down
39 changes: 38 additions & 1 deletion nimbus/db/core_db/backend/aristo_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
{.push raises: [].}

import
eth/common,
../../aristo as use_ari,
../../aristo/aristo_desc/desc_identifiers,
../../aristo/[aristo_init/memory_only, aristo_walk],
../../kvt as use_kvt,
../../kvt/[kvt_init/memory_only, kvt_walk],
../base/[base_config, base_desc, base_helpers]

# ------------------------------------------------------------------------------
# Public constructor and helper
# Public constructors
# ------------------------------------------------------------------------------

proc create*(dbType: CoreDbType; kvt: KvtDbRef; mpt: AristoDbRef): CoreDbRef =
Expand Down Expand Up @@ -51,6 +53,41 @@ proc newAristoVoidCoreDbRef*(): CoreDbRef =
KvtDbRef.init(use_kvt.VoidBackendRef),
AristoDbRef.init(use_ari.VoidBackendRef))

proc newCtxByKey*(
ctx: CoreDbCtxRef;
key: Hash256;
info: static[string];
): CoreDbRc[CoreDbCtxRef] =
const
rvid: RootedVertexID = (VertexID(1),VertexID(1))
let
db = ctx.parent

# Find `(vid,key)` on transaction stack
inx = block:
let rc = db.ariApi.call(findTx, ctx.mpt, rvid, key.to(HashKey))
if rc.isErr:
return err(rc.error.toError info)
rc.value

# Fork MPT descriptor that provides `(vid,key)`
newMpt = block:
let rc = db.ariApi.call(forkTx, ctx.mpt, inx)
if rc.isErr:
return err(rc.error.toError info)
rc.value

# Fork KVT descriptor parallel to `newMpt`
newKvt = block:
let rc = db.kvtApi.call(forkTx, ctx.kvt, inx)
if rc.isErr:
discard db.ariApi.call(forget, newMpt)
return err(rc.error.toError info)
rc.value

# Create new context
ok(db.bless CoreDbCtxRef(kvt: newKvt, mpt: newMpt))

# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------
Loading

0 comments on commit 01b5c08

Please sign in to comment.