nimbus-eth1/nimbus/db/aristo/aristo_part/part_chain_rlp.nim

119 lines
3.5 KiB
Nim
Raw Normal View History

# nimbus-eth1
# Copyright (c) 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,
results,
".."/[aristo_desc, aristo_get, aristo_utils, aristo_serialise]
const
ChainRlpNodesNoEntry* = {
PartChnLeafPathMismatch, PartChnExtPfxMismatch, PartChnBranchVoidEdge}
## Partial path errors that can be used to proof that a path does
## not exists.
TrackRlpNodesNoEntry* = {PartTrkLinkExpected, PartTrkLeafPfxMismatch}
## This is the opposite of `ChainRlpNodesNoEntry` when verifying that a
## node does not exist.
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
proc chainRlpNodes*(
db: AristoDbRef;
rvid: RootedVertexID;
path: NibblesBuf,
chain: var seq[seq[byte]];
): Result[void,AristoError] =
## Inspired by the `getBranchAux()` function from `hexary.nim`
let
(vtx,_) = ? db.getVtxRc rvid
node = vtx.toNode(rvid.root, db).valueOr:
return err(PartChnNodeConvError)
# Save rpl encoded node(s)
chain &= node.to(seq[seq[byte]])
# Follow up child node
case vtx.vType:
of Leaf:
if path != vtx.pfx:
err(PartChnLeafPathMismatch)
else:
ok()
of Branch:
let nChewOff = sharedPrefixLen(vtx.pfx, path)
if nChewOff != vtx.pfx.len:
err(PartChnExtPfxMismatch)
elif path.len == nChewOff:
err(PartChnBranchPathExhausted)
else:
let
nibble = path[nChewOff]
rest = path.slice(nChewOff+1)
Pre-allocate vids for branches (#2882) Each branch node may have up to 16 sub-items - currently, these are given VertexID based when they are first needed leading to a mostly-random order of vertexid for each subitem. Here, we pre-allocate all 16 vertex ids such that when a branch subitem is filled, it already has a vertexid waiting for it. This brings several important benefits: * subitems are sorted and "close" in their id sequencing - this means that when rocksdb stores them, they are likely to end up in the same data block thus improving read efficiency * because the ids are consequtive, we can store just the starting id and a bitmap representing which subitems are in use - this reduces disk space usage for branches allowing more of them fit into a single disk read, further improving disk read and caching performance - disk usage at block 18M is down from 84 to 78gb! * the in-memory footprint of VertexRef reduced allowing more instances to fit into caches and less memory to be used overall. Because of the increased locality of reference, it turns out that we no longer need to iterate over the entire database to efficiently generate the hash key database because the normal computation is now faster - this significantly benefits "live" chain processing as well where each dirtied key must be accompanied by a read of all branch subitems next to it - most of the performance benefit in this branch comes from this locality-of-reference improvement. On a sample resync, there's already ~20% improvement with later blocks seeing increasing benefit (because the trie is deeper in later blocks leading to more benefit from branch read perf improvements) ``` blocks: 18729664, baseline: 190h43m49s, contender: 153h59m0s Time (total): -36h44m48s, -19.27% ``` Note: clients need to be resynced as the PR changes the on-disk format R.I.P. little bloom filter - your life in the repo was short but valuable
2024-12-04 10:42:04 +00:00
if not vtx.bVid(nibble).isValid:
return err(PartChnBranchVoidEdge)
# Recursion!
Pre-allocate vids for branches (#2882) Each branch node may have up to 16 sub-items - currently, these are given VertexID based when they are first needed leading to a mostly-random order of vertexid for each subitem. Here, we pre-allocate all 16 vertex ids such that when a branch subitem is filled, it already has a vertexid waiting for it. This brings several important benefits: * subitems are sorted and "close" in their id sequencing - this means that when rocksdb stores them, they are likely to end up in the same data block thus improving read efficiency * because the ids are consequtive, we can store just the starting id and a bitmap representing which subitems are in use - this reduces disk space usage for branches allowing more of them fit into a single disk read, further improving disk read and caching performance - disk usage at block 18M is down from 84 to 78gb! * the in-memory footprint of VertexRef reduced allowing more instances to fit into caches and less memory to be used overall. Because of the increased locality of reference, it turns out that we no longer need to iterate over the entire database to efficiently generate the hash key database because the normal computation is now faster - this significantly benefits "live" chain processing as well where each dirtied key must be accompanied by a read of all branch subitems next to it - most of the performance benefit in this branch comes from this locality-of-reference improvement. On a sample resync, there's already ~20% improvement with later blocks seeing increasing benefit (because the trie is deeper in later blocks leading to more benefit from branch read perf improvements) ``` blocks: 18729664, baseline: 190h43m49s, contender: 153h59m0s Time (total): -36h44m48s, -19.27% ``` Note: clients need to be resynced as the PR changes the on-disk format R.I.P. little bloom filter - your life in the repo was short but valuable
2024-12-04 10:42:04 +00:00
db.chainRlpNodes((rvid.root,vtx.bVid(nibble)), rest, chain)
proc trackRlpNodes*(
chain: openArray[seq[byte]];
topKey: HashKey;
path: NibblesBuf;
start = false;
): Result[seq[byte],AristoError]
{.gcsafe, raises: [RlpError]} =
## Verify rlp-encoded node chain created by `chainRlpNodes()`.
if path.len == 0:
return err(PartTrkEmptyPath)
# Verify key against rlp-node
let digest = chain[0].digestTo(HashKey)
if start:
if topKey.to(Hash32) != digest.to(Hash32):
return err(PartTrkFollowUpKeyMismatch)
else:
if topKey != digest:
return err(PartTrkFollowUpKeyMismatch)
var
node = rlpFromBytes chain[0]
nChewOff = 0
link: seq[byte]
# Decode rlp-node and prepare for recursion
case node.listLen
of 2:
let (isLeaf, segm) = NibblesBuf.fromHexPrefix node.listElem(0).toBytes
nChewOff = sharedPrefixLen(path, segm)
link = node.listElem(1).toBytes # link or payload
if isLeaf:
if nChewOff == path.len:
return ok(link)
return err(PartTrkLeafPfxMismatch)
of 17:
nChewOff = 1
link = node.listElem(path[0].int).toBytes
else:
return err(PartTrkGarbledNode)
let nextKey = HashKey.fromBytes(link).valueOr:
return err(PartTrkLinkExpected)
chain.toOpenArray(1,chain.len-1).trackRlpNodes(nextKey, path.slice nChewOff)
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------