mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-10 20:36:31 +00:00
2c35390bdf
* Aristo: Update error return code why: Failing of `Aristo` function `delete()` might fail because there is no such data item on the db. This must return a single error code as is done with `fetch()`. * Ledger: Better error handling why: The `expect()` clauses have been replaced by raising asserts indicating the error from the database backend. Also, `delete()` failures are legitimate if the item to delete does not exist. * Aristo: Delete function must always leave a label on DB for `hashify()` why: The `hashify()` uses the labels left bu `merge()` and `delete()` to compile (and optimise) a scheduler for subsequent hashing. Originally, the labels were not used for deleted entries and `delete()` still had some edge case where the deletion label was not properly handled. * Aristo: Update `hashify()` scheduler, remove buggy optimisation why: Was left over from version without virtual state roots which did not know about account payload leaf vertices referring to storage roots. * Aristo: Label storage trie account in `delete()` similar to `merge()` details; The `delete()` function applied to a non-static state root (assumed to be a storage root) will check the payload of an accounts leaf and mark its Merkle keys to be re-checked when runninh `hashify()` * Aristo: Clean up and re-org recycled vertex IDs in `hashify()` why: Re-organising the recycled vertex IDs list intends to reduce the size of the list. This list is organised as a LIFO (or stack.) By reorganising it in a way so that the least vertex ID numbers are on top, the list will be kept smaller as observed on some examples (less than 30%.) * CoreDb: Accept storage trie deletion requests in non-initialised state why: Due to lazy initialisation, the root vertex ID might not yet exist. So the `Aristo` database handlers would reject this call with an error and this condition needs to be handled by the API (which realises the lazy feature.) * Cosmetics & code massage, prettify logging * fix missing import
160 lines
4.9 KiB
Nim
160 lines
4.9 KiB
Nim
# nimbus-eth1
|
|
# Copyright (c) 2023-2024 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.
|
|
|
|
{.push raises: [].}
|
|
|
|
import
|
|
eth/[common, trie/nibbles],
|
|
results,
|
|
"."/[aristo_desc, aristo_get]
|
|
|
|
type
|
|
Leg* = object
|
|
## For constructing a `VertexPath`
|
|
wp*: VidVtxPair ## Vertex ID and data ref
|
|
nibble*: int8 ## Next vertex selector for `Branch` (if any)
|
|
|
|
Hike* = object
|
|
## Trie traversal path
|
|
root*: VertexID ## Handy for some fringe cases
|
|
legs*: seq[Leg] ## Chain of vertices and IDs
|
|
tail*: NibblesSeq ## Portion of non completed path
|
|
|
|
const
|
|
HikeAcceptableStopsNotFound* = {
|
|
HikeBranchTailEmpty,
|
|
HikeBranchMissingEdge,
|
|
HikeExtTailEmpty,
|
|
HikeExtTailMismatch,
|
|
HikeLeafUnexpected,
|
|
HikeNoLegs}
|
|
## When trying to find a leaf vertex the Patricia tree, there are several
|
|
## conditions where the search stops which do not constitute a problem
|
|
## with the trie (aka sysetm error.)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Private functions
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func getNibblesImpl(hike: Hike; start = 0; maxLen = high(int)): NibblesSeq =
|
|
## May be needed for partial rebuild, as well
|
|
for n in start ..< min(hike.legs.len, maxLen):
|
|
let leg = hike.legs[n]
|
|
case leg.wp.vtx.vType:
|
|
of Branch:
|
|
result = result & @[leg.nibble.byte].initNibbleRange.slice(1)
|
|
of Extension:
|
|
result = result & leg.wp.vtx.ePfx
|
|
of Leaf:
|
|
result = result & leg.wp.vtx.lPfx
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public functions
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func to*(rc: Result[Hike,(VertexID,AristoError,Hike)]; T: type Hike): T =
|
|
## Extract `Hike` from either ok ot error part of argument `rc`.
|
|
if rc.isOk: rc.value else: rc.error[2]
|
|
|
|
func to*(hike: Hike; T: type NibblesSeq): T =
|
|
## Convert back
|
|
hike.getNibblesImpl() & hike.tail
|
|
|
|
func legsTo*(hike: Hike; T: type NibblesSeq): T =
|
|
## Convert back
|
|
hike.getNibblesImpl()
|
|
|
|
func legsTo*(hike: Hike; numLegs: int; T: type NibblesSeq): T =
|
|
## variant of `legsTo()`
|
|
hike.getNibblesImpl(0, numLegs)
|
|
|
|
# --------
|
|
|
|
proc hikeUp*(
|
|
path: NibblesSeq; # Partial path
|
|
root: VertexID; # Start vertex
|
|
db: AristoDbRef; # Database
|
|
): Result[Hike,(VertexID,AristoError,Hike)] =
|
|
## For the argument `path`, find and return the logest possible path in the
|
|
## argument database `db`.
|
|
var hike = Hike(
|
|
root: root,
|
|
tail: path)
|
|
|
|
if not root.isValid:
|
|
return err((VertexID(0),HikeRootMissing,hike))
|
|
if path.len == 0:
|
|
return err((VertexID(0),HikeEmptyPath,hike))
|
|
|
|
var vid = root
|
|
while vid.isValid:
|
|
var leg = Leg(wp: VidVtxPair(vid: vid), nibble: -1)
|
|
|
|
# Fetch next vertex
|
|
leg.wp.vtx = db.getVtxRc(vid).valueOr:
|
|
if error != GetVtxNotFound:
|
|
return err((vid,error,hike))
|
|
if hike.legs.len == 0:
|
|
return err((vid,HikeNoLegs,hike))
|
|
break
|
|
|
|
case leg.wp.vtx.vType:
|
|
of Leaf:
|
|
if hike.tail.len == hike.tail.sharedPrefixLen(leg.wp.vtx.lPfx):
|
|
# Bingo, got full path
|
|
hike.legs.add leg
|
|
hike.tail = EmptyNibbleSeq
|
|
break
|
|
|
|
return err((vid,HikeLeafUnexpected,hike))
|
|
|
|
of Branch:
|
|
if hike.tail.len == 0:
|
|
hike.legs.add leg
|
|
return err((vid,HikeBranchTailEmpty,hike))
|
|
|
|
let
|
|
nibble = hike.tail[0].int8
|
|
nextVid = leg.wp.vtx.bVid[nibble]
|
|
|
|
if not nextVid.isValid:
|
|
return err((vid,HikeBranchMissingEdge,hike))
|
|
|
|
leg.nibble = nibble
|
|
hike.legs.add leg
|
|
hike.tail = hike.tail.slice(1)
|
|
vid = nextVid
|
|
|
|
of Extension:
|
|
if hike.tail.len == 0:
|
|
hike.legs.add leg
|
|
hike.tail = EmptyNibbleSeq
|
|
return err((vid,HikeExtTailEmpty,hike)) # Well, somehow odd
|
|
|
|
if leg.wp.vtx.ePfx.len != hike.tail.sharedPrefixLen(leg.wp.vtx.ePfx):
|
|
return err((vid,HikeExtTailMismatch,hike)) # Need to branch from here
|
|
|
|
hike.legs.add leg
|
|
hike.tail = hike.tail.slice(leg.wp.vtx.ePfx.len)
|
|
vid = leg.wp.vtx.eVid
|
|
|
|
ok hike
|
|
|
|
proc hikeUp*(
|
|
lty: LeafTie;
|
|
db: AristoDbRef;
|
|
): Result[Hike,(VertexID,AristoError,Hike)] =
|
|
## Variant of `hike()`
|
|
lty.path.to(NibblesSeq).hikeUp(lty.root, db)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# End
|
|
# ------------------------------------------------------------------------------
|