Skip to content

Commit

Permalink
Aristo db merkle hashify functionality added (status-im#1593)
Browse files Browse the repository at this point in the history
* Keep vertex ID generator state with each db-layer

why:
  The vertex ID generator state is part of the difference to the below
  layer

* Move otherwise unused source to test directory

* Add Merkle hash generator

also:
  * Verification facility for debugging
  * Empty Merkle key hashes encoded as `EMPTY_ROOT_HASH`
  • Loading branch information
mjfh authored May 30, 2023
1 parent cd78458 commit 2fc349f
Show file tree
Hide file tree
Showing 16 changed files with 1,163 additions and 421 deletions.
350 changes: 254 additions & 96 deletions nimbus/db/aristo/aristo_debug.nim

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions nimbus/db/aristo/aristo_desc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ import
std/[sets, tables],
eth/[common, trie/nibbles],
stew/results,
../../sync/snap/range_desc,
./aristo_error
"."/[aristo_constants, aristo_error]

import
../../sync/snap/range_desc
export
ByteArray32, NodeKey, NodeTag, digestTo, hash, to, `==`, `$`

type
VertexID* = distinct uint64
Expand Down Expand Up @@ -111,17 +115,18 @@ type
## Hexary trie plus helper structures
sTab*: Table[VertexID,VertexRef] ## Structural vertex table making up a trie
lTab*: Table[NodeTag,VertexID] ## Direct access, path to leaf node
sDel*: HashSet[VertexID] ## Deleted vertices
lRoot*: VertexID ## Root vertex for `lTab[]`
kMap*: Table[VertexID,NodeKey] ## Merkle hash key mapping
pAmk*: Table[NodeKey,VertexID] ## Reverse mapper for data import
pPrf*: HashSet[VertexID] ## Locked vertices (from proof vertices)
vGen*: seq[VertexID] ## Unique vertex ID generator

case cascaded*: bool ## Cascaded delta databases, tx layer
of true:
level*: int ## Positive number of stack layers
stack*: AristoDbRef ## Down the chain, not `nil`
base*: AristoDbRef ## Backend level descriptor
base*: AristoDbRef ## Backend level descriptor, maybe unneeded
else:
vidGen*: seq[VertexID] ## Unique vertex ID generator
backend*: AristoBackendRef ## backend database (maybe `nil`)

# Debugging data below, might go away in future
Expand Down Expand Up @@ -204,8 +209,11 @@ proc `==`*(a, b: NodeRef): bool =
# Public helpers, miscellaneous functions
# ------------------------------------------------------------------------------

proc isZero*[T: NodeKey|VertexID](a: T): bool =
a == typeof(a).default
proc isZero*(a: VertexID): bool =
a == VertexID(0)

proc isEmpty*(a: NodeKey): bool =
a == EMPTY_ROOT_KEY

proc isError*(a: NodeRef): bool =
a.error != AristoError(0)
Expand Down
28 changes: 27 additions & 1 deletion nimbus/db/aristo/aristo_error.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
type
AristoError* = enum
NothingSerious = 0
GenericError

# Rlp decoder, `fromRlpRecord()`
Rlp2Or17ListEntries
Expand Down Expand Up @@ -71,10 +72,35 @@ type
MergeBrLinkVtxPfxTooShort
MergeBranchGarbledNibble
MergeBranchGarbledTail
MergeBranchLinkLockedKey
MergeBranchLinkProofModeLock
MergeBranchProofModeLock
MergeBranchRootExpected
MergeLeafGarbledHike
MergeLeafPathCachedAlready
MergeNonBranchProofModeLock
MergeRootBranchLinkBusy

MergeNodeKeyZero
MergeNodeKeyEmpty
MergeNodeKeyCachedAlready

# Update `Merkle` hashes `hashify()`
HashifyCannotComplete
HashifyCannotHashRoot
HashifyExistingHashMismatch
HashifyLeafToRootAllFailed
HashifyRootHashMismatch

HashifyCheckRevCountMismatch
HashifyCheckRevHashMismatch
HashifyCheckRevHashMissing
HashifyCheckRevVtxDup
HashifyCheckRevVtxMissing
HashifyCheckVidVtxMismatch
HashifyCheckVtxCountMismatch
HashifyCheckVtxHashMismatch
HashifyCheckVtxHashMissing
HashifyCheckVtxIncomplete
HashifyCheckVtxLockWithoutKey

# End
4 changes: 2 additions & 2 deletions nimbus/db/aristo/aristo_get.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import
std/tables,
stew/results,
../../sync/snap/range_desc,
"."/[aristo_desc, aristo_error]

type
Expand All @@ -32,7 +31,8 @@ proc getVtxCascaded*(
db: AristoDbRef;
vid: VertexID;
): Result[VertexRef,AristoError] =
## Cascaded lookup for data record down the transaction cascade.
## Cascaded lookup for data record down the transaction cascade. This
## function will return a potential error code from the backend (if any).
db.sTab.withValue(vid, vtxPtr):
return ok vtxPtr[]

Expand Down
Loading

0 comments on commit 2fc349f

Please sign in to comment.