2023-05-10 13:50:04 +00:00
|
|
|
# nim-eth
|
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2019-02-05 12:01:10 +00:00
|
|
|
import
|
2024-05-28 08:05:57 +00:00
|
|
|
std/tables,
|
2021-04-06 11:33:24 +00:00
|
|
|
nimcrypto/[keccak, hash],
|
|
|
|
../rlp,
|
|
|
|
"."/[trie_defs, nibbles, db]
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
TrieNodeKey = object
|
|
|
|
hash: KeccakHash
|
|
|
|
usedBytes: uint8
|
|
|
|
|
|
|
|
DB = TrieDatabaseRef
|
|
|
|
|
|
|
|
HexaryTrie* = object
|
|
|
|
db*: DB
|
|
|
|
root: TrieNodeKey
|
|
|
|
isPruning: bool
|
|
|
|
|
|
|
|
SecureHexaryTrie* = distinct HexaryTrie
|
|
|
|
|
|
|
|
template len(key: TrieNodeKey): int =
|
|
|
|
key.usedBytes.int
|
|
|
|
|
|
|
|
template asDbKey(k: TrieNodeKey): untyped =
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert k.usedBytes == 32
|
2019-02-05 12:01:10 +00:00
|
|
|
k.hash.data
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc expectHash(r: Rlp): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
result = r.toBytes
|
|
|
|
if result.len != 32:
|
|
|
|
raise newException(RlpTypeMismatch,
|
|
|
|
"RLP expected to be a Keccak hash value, but has an incorrect length")
|
|
|
|
|
2021-05-11 15:32:47 +00:00
|
|
|
proc dbPut(db: DB, data: openArray[byte]): TrieNodeKey
|
2023-05-10 13:50:04 +00:00
|
|
|
{.gcsafe, raises: [].}
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
template get(db: DB, key: Rlp): seq[byte] =
|
|
|
|
db.get(key.expectHash)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
converter toTrieNodeKey(hash: KeccakHash): TrieNodeKey =
|
|
|
|
result.hash = hash
|
|
|
|
result.usedBytes = 32
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc initHexaryTrie*(db: DB, rootHash: KeccakHash, isPruning = true): HexaryTrie =
|
2019-02-05 12:01:10 +00:00
|
|
|
result.db = db
|
|
|
|
result.root = rootHash
|
|
|
|
result.isPruning = isPruning
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
template initSecureHexaryTrie*(db: DB, rootHash: KeccakHash, isPruning = true): SecureHexaryTrie =
|
|
|
|
SecureHexaryTrie initHexaryTrie(db, rootHash, isPruning)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc initHexaryTrie*(db: DB, isPruning = true): HexaryTrie
|
2023-05-10 13:50:04 +00:00
|
|
|
{.raises: [].} =
|
2019-02-05 12:01:10 +00:00
|
|
|
result.db = db
|
2020-04-20 18:14:39 +00:00
|
|
|
result.root = result.db.dbPut(emptyRlp)
|
2019-02-05 12:01:10 +00:00
|
|
|
result.isPruning = isPruning
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
template initSecureHexaryTrie*(db: DB, isPruning = true): SecureHexaryTrie =
|
|
|
|
SecureHexaryTrie initHexaryTrie(db, isPruning)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
proc rootHash*(t: HexaryTrie): KeccakHash =
|
|
|
|
t.root.hash
|
|
|
|
|
|
|
|
proc rootHashHex*(t: HexaryTrie): string =
|
|
|
|
$t.root.hash
|
|
|
|
|
|
|
|
template prune(t: HexaryTrie, x: openArray[byte]) =
|
|
|
|
if t.isPruning: t.db.del(x)
|
|
|
|
|
|
|
|
proc isPruning*(t: HexaryTrie): bool =
|
|
|
|
t.isPruning
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc getLocalBytes(x: TrieNodeKey): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
## This proc should be used on nodes using the optimization
|
|
|
|
## of short values within the key.
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert x.usedBytes < 32
|
2020-04-20 18:14:39 +00:00
|
|
|
x.hash.data[0..<x.usedBytes]
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
template keyToLocalBytes(db: DB, k: TrieNodeKey): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
if k.len < 32: k.getLocalBytes
|
2024-05-28 08:05:57 +00:00
|
|
|
else: db.get(k.asDbKey)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
template extensionNodeKey(r: Rlp): auto =
|
|
|
|
hexPrefixDecode r.listElem(0).toBytes
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc getAux(db: DB, nodeRlp: Rlp, path: NibblesSeq): seq[byte]
|
|
|
|
{.gcsafe, raises: [RlpError].}
|
|
|
|
|
|
|
|
proc getAuxByHash(db: DB, node: TrieNodeKey, path: NibblesSeq): seq[byte] =
|
|
|
|
var nodeRlp = rlpFromBytes keyToLocalBytes(db, node)
|
|
|
|
return getAux(db, nodeRlp, path)
|
|
|
|
|
|
|
|
template getLookup(elem: untyped): untyped =
|
2019-02-05 12:01:10 +00:00
|
|
|
if elem.isList: elem
|
2024-05-28 08:05:57 +00:00
|
|
|
else: rlpFromBytes(get(db, elem.expectHash))
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc getAux(db: DB, nodeRlp: Rlp, path: NibblesSeq): seq[byte]
|
2023-05-10 13:50:04 +00:00
|
|
|
{.gcsafe, raises: [RlpError].} =
|
2019-02-05 12:01:10 +00:00
|
|
|
if not nodeRlp.hasData or nodeRlp.isEmpty:
|
2020-04-20 18:14:39 +00:00
|
|
|
return
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
case nodeRlp.listLen
|
|
|
|
of 2:
|
|
|
|
let (isLeaf, k) = nodeRlp.extensionNodeKey
|
|
|
|
let sharedNibbles = sharedPrefixLen(path, k)
|
|
|
|
|
|
|
|
if sharedNibbles == k.len:
|
|
|
|
let value = nodeRlp.listElem(1)
|
|
|
|
if sharedNibbles == path.len and isLeaf:
|
|
|
|
return value.toBytes
|
|
|
|
elif not isLeaf:
|
2024-05-28 08:05:57 +00:00
|
|
|
let nextLookup = value.getLookup
|
|
|
|
return getAux(db, nextLookup, path.slice(sharedNibbles))
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
return
|
2019-02-05 12:01:10 +00:00
|
|
|
of 17:
|
|
|
|
if path.len == 0:
|
|
|
|
return nodeRlp.listElem(16).toBytes
|
|
|
|
var branch = nodeRlp.listElem(path[0].int)
|
|
|
|
if branch.isEmpty:
|
2020-04-20 18:14:39 +00:00
|
|
|
return
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
2024-05-28 08:05:57 +00:00
|
|
|
let nextLookup = branch.getLookup
|
|
|
|
return getAux(db, nextLookup, path.slice(1))
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
2020-02-05 12:29:54 +00:00
|
|
|
raise newException(CorruptedTrieDatabase,
|
2019-02-05 12:01:10 +00:00
|
|
|
"HexaryTrie node with an unexpected number of children")
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc get*(self: HexaryTrie; key: openArray[byte]): seq[byte] =
|
2024-05-28 08:05:57 +00:00
|
|
|
return getAuxByHash(self.db, self.root, initNibbleRange(key))
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc getKeysAux(db: DB, stack: var seq[tuple[nodeRlp: Rlp, path: NibblesSeq]]): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
while stack.len > 0:
|
|
|
|
let (nodeRlp, path) = stack.pop()
|
|
|
|
if not nodeRlp.hasData or nodeRlp.isEmpty:
|
|
|
|
continue
|
|
|
|
|
|
|
|
case nodeRlp.listLen
|
|
|
|
of 2:
|
|
|
|
let
|
|
|
|
(isLeaf, k) = nodeRlp.extensionNodeKey
|
|
|
|
key = path & k
|
|
|
|
|
|
|
|
if isLeaf:
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(key.len mod 2 == 0)
|
2019-02-05 12:01:10 +00:00
|
|
|
return key.getBytes
|
|
|
|
else:
|
|
|
|
let
|
|
|
|
value = nodeRlp.listElem(1)
|
2024-05-28 08:05:57 +00:00
|
|
|
nextLookup = value.getLookup
|
2019-02-05 12:01:10 +00:00
|
|
|
stack.add((nextLookup, key))
|
|
|
|
of 17:
|
|
|
|
for i in 0 ..< 16:
|
|
|
|
var branch = nodeRlp.listElem(i)
|
|
|
|
if not branch.isEmpty:
|
2024-05-28 08:05:57 +00:00
|
|
|
let nextLookup = branch.getLookup
|
2019-02-05 12:01:10 +00:00
|
|
|
var key = path.cloneAndReserveNibble()
|
|
|
|
key.replaceLastNibble(i.byte)
|
|
|
|
stack.add((nextLookup, key))
|
|
|
|
|
|
|
|
var lastElem = nodeRlp.listElem(16)
|
|
|
|
if not lastElem.isEmpty:
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(path.len mod 2 == 0)
|
2019-02-05 12:01:10 +00:00
|
|
|
return path.getBytes
|
|
|
|
else:
|
2020-02-05 12:29:54 +00:00
|
|
|
raise newException(CorruptedTrieDatabase,
|
2019-02-05 12:01:10 +00:00
|
|
|
"HexaryTrie node with an unexpected number of children")
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
iterator keys*(self: HexaryTrie): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
var
|
|
|
|
nodeRlp = rlpFromBytes keyToLocalBytes(self.db, self.root)
|
2020-04-20 18:14:39 +00:00
|
|
|
stack = @[(nodeRlp, initNibbleRange([]))]
|
2019-02-05 12:01:10 +00:00
|
|
|
while stack.len > 0:
|
2024-05-28 08:05:57 +00:00
|
|
|
yield getKeysAux(self.db, stack)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc getValuesAux(db: DB, stack: var seq[Rlp]): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
while stack.len > 0:
|
2024-05-28 08:05:57 +00:00
|
|
|
let nodeRlp = stack.pop()
|
2019-02-05 12:01:10 +00:00
|
|
|
if not nodeRlp.hasData or nodeRlp.isEmpty:
|
|
|
|
continue
|
|
|
|
|
|
|
|
case nodeRlp.listLen
|
|
|
|
of 2:
|
|
|
|
let
|
2024-05-28 08:05:57 +00:00
|
|
|
(isLeaf, _) = nodeRlp.extensionNodeKey
|
2019-02-05 12:01:10 +00:00
|
|
|
value = nodeRlp.listElem(1)
|
|
|
|
|
|
|
|
if isLeaf:
|
|
|
|
return value.toBytes
|
|
|
|
else:
|
2024-05-28 08:05:57 +00:00
|
|
|
let nextLookup = value.getLookup
|
|
|
|
stack.add(nextLookup)
|
2019-02-05 12:01:10 +00:00
|
|
|
of 17:
|
|
|
|
for i in 0 ..< 16:
|
|
|
|
var branch = nodeRlp.listElem(i)
|
|
|
|
if not branch.isEmpty:
|
2024-05-28 08:05:57 +00:00
|
|
|
let nextLookup = branch.getLookup
|
|
|
|
stack.add(nextLookup)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
var lastElem = nodeRlp.listElem(16)
|
|
|
|
if not lastElem.isEmpty:
|
|
|
|
return lastElem.toBytes
|
|
|
|
else:
|
2020-02-05 12:29:54 +00:00
|
|
|
raise newException(CorruptedTrieDatabase,
|
2019-02-05 12:01:10 +00:00
|
|
|
"HexaryTrie node with an unexpected number of children")
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
iterator values*(self: HexaryTrie): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
var
|
|
|
|
nodeRlp = rlpFromBytes keyToLocalBytes(self.db, self.root)
|
2024-05-28 08:05:57 +00:00
|
|
|
stack = @[nodeRlp]
|
2019-02-05 12:01:10 +00:00
|
|
|
while stack.len > 0:
|
2024-05-28 08:05:57 +00:00
|
|
|
yield getValuesAux(self.db, stack)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc getPairsAux(db: DB, stack: var seq[tuple[nodeRlp: Rlp, path: NibblesSeq]]): (seq[byte], seq[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
while stack.len > 0:
|
|
|
|
let (nodeRlp, path) = stack.pop()
|
|
|
|
if not nodeRlp.hasData or nodeRlp.isEmpty:
|
|
|
|
continue
|
|
|
|
|
|
|
|
case nodeRlp.listLen
|
|
|
|
of 2:
|
|
|
|
let
|
|
|
|
(isLeaf, k) = nodeRlp.extensionNodeKey
|
|
|
|
key = path & k
|
|
|
|
value = nodeRlp.listElem(1)
|
|
|
|
|
|
|
|
if isLeaf:
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(key.len mod 2 == 0)
|
2019-02-05 12:01:10 +00:00
|
|
|
return (key.getBytes, value.toBytes)
|
|
|
|
else:
|
2024-05-28 08:05:57 +00:00
|
|
|
let nextLookup = value.getLookup
|
2019-02-05 12:01:10 +00:00
|
|
|
stack.add((nextLookup, key))
|
|
|
|
of 17:
|
|
|
|
for i in 0 ..< 16:
|
|
|
|
var branch = nodeRlp.listElem(i)
|
|
|
|
if not branch.isEmpty:
|
2024-05-28 08:05:57 +00:00
|
|
|
let nextLookup = branch.getLookup
|
2019-02-05 12:01:10 +00:00
|
|
|
var key = path.cloneAndReserveNibble()
|
|
|
|
key.replaceLastNibble(i.byte)
|
|
|
|
stack.add((nextLookup, key))
|
|
|
|
|
|
|
|
var lastElem = nodeRlp.listElem(16)
|
|
|
|
if not lastElem.isEmpty:
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(path.len mod 2 == 0)
|
2019-02-05 12:01:10 +00:00
|
|
|
return (path.getBytes, lastElem.toBytes)
|
|
|
|
else:
|
2020-02-05 12:29:54 +00:00
|
|
|
raise newException(CorruptedTrieDatabase,
|
2019-02-05 12:01:10 +00:00
|
|
|
"HexaryTrie node with an unexpected number of children")
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
iterator pairs*(self: HexaryTrie): (seq[byte], seq[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
var
|
|
|
|
nodeRlp = rlpFromBytes keyToLocalBytes(self.db, self.root)
|
2020-04-20 18:14:39 +00:00
|
|
|
stack = @[(nodeRlp, initNibbleRange([]))]
|
2019-02-05 12:01:10 +00:00
|
|
|
while stack.len > 0:
|
|
|
|
# perhaps a Nim bug #9778
|
|
|
|
# cannot yield the helper proc directly
|
|
|
|
# it will cut the yield in half
|
2024-05-28 08:05:57 +00:00
|
|
|
let res = getPairsAux(self.db, stack)
|
2019-02-05 12:01:10 +00:00
|
|
|
yield res
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
iterator replicate*(self: HexaryTrie): (seq[byte], seq[byte]) =
|
2019-12-11 07:50:10 +00:00
|
|
|
# this iterator helps 'rebuild' the entire trie without
|
|
|
|
# going through a trie algorithm, but it will pull the entire
|
|
|
|
# low level KV pairs. Thus the target db will only use put operations
|
|
|
|
# without del or contains, can speed up huge trie replication.
|
|
|
|
var
|
|
|
|
localBytes = keyToLocalBytes(self.db, self.root)
|
|
|
|
nodeRlp = rlpFromBytes localBytes
|
2020-04-20 18:14:39 +00:00
|
|
|
stack = @[(nodeRlp, initNibbleRange([]))]
|
2019-12-11 07:50:10 +00:00
|
|
|
|
|
|
|
template pushOrYield(elem: untyped) =
|
|
|
|
if elem.isList:
|
|
|
|
stack.add((elem, key))
|
|
|
|
else:
|
2020-04-20 18:14:39 +00:00
|
|
|
let rlpBytes = get(self.db, elem.expectHash)
|
2019-12-11 07:50:10 +00:00
|
|
|
let nextLookup = rlpFromBytes(rlpBytes)
|
|
|
|
stack.add((nextLookup, key))
|
|
|
|
yield (elem.toBytes, rlpBytes)
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
yield (@(self.rootHash.data), localBytes)
|
2019-12-11 07:50:10 +00:00
|
|
|
while stack.len > 0:
|
|
|
|
let (nodeRlp, path) = stack.pop()
|
|
|
|
if not nodeRlp.hasData or nodeRlp.isEmpty:
|
|
|
|
continue
|
|
|
|
|
|
|
|
case nodeRlp.listLen
|
|
|
|
of 2:
|
|
|
|
let
|
|
|
|
(isLeaf, k) = nodeRlp.extensionNodeKey
|
|
|
|
key = path & k
|
|
|
|
value = nodeRlp.listElem(1)
|
|
|
|
if not isLeaf: pushOrYield(value)
|
|
|
|
of 17:
|
|
|
|
for i in 0 ..< 16:
|
|
|
|
var branch = nodeRlp.listElem(i)
|
|
|
|
if not branch.isEmpty:
|
|
|
|
var key = path.cloneAndReserveNibble()
|
|
|
|
key.replaceLastNibble(i.byte)
|
|
|
|
pushOrYield(branch)
|
|
|
|
else:
|
2020-02-05 12:29:54 +00:00
|
|
|
raise newException(CorruptedTrieDatabase,
|
2019-12-11 07:50:10 +00:00
|
|
|
"HexaryTrie node with an unexpected number of children")
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc getValues*(self: HexaryTrie): seq[seq[byte]] =
|
2019-02-05 12:01:10 +00:00
|
|
|
result = @[]
|
|
|
|
for v in self.values:
|
|
|
|
result.add v
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc getKeys*(self: HexaryTrie): seq[seq[byte]] =
|
2019-02-05 12:01:10 +00:00
|
|
|
result = @[]
|
|
|
|
for k in self.keys:
|
|
|
|
result.add k
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
template getNode(elem: untyped): untyped =
|
2020-04-20 18:14:39 +00:00
|
|
|
if elem.isList: @(elem.rawData)
|
2024-05-28 08:05:57 +00:00
|
|
|
else: get(db, elem.expectHash)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc getBranchAux(db: DB, node: openArray[byte], path: NibblesSeq, output: var seq[seq[byte]]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
var nodeRlp = rlpFromBytes node
|
|
|
|
if not nodeRlp.hasData or nodeRlp.isEmpty: return
|
|
|
|
|
|
|
|
case nodeRlp.listLen
|
|
|
|
of 2:
|
|
|
|
let (isLeaf, k) = nodeRlp.extensionNodeKey
|
|
|
|
let sharedNibbles = sharedPrefixLen(path, k)
|
|
|
|
if sharedNibbles == k.len:
|
|
|
|
let value = nodeRlp.listElem(1)
|
|
|
|
if not isLeaf:
|
2024-05-28 08:05:57 +00:00
|
|
|
let nextLookup = value.getNode
|
2019-02-05 12:01:10 +00:00
|
|
|
output.add nextLookup
|
2024-05-28 08:05:57 +00:00
|
|
|
getBranchAux(db, nextLookup, path.slice(sharedNibbles), output)
|
2019-02-05 12:01:10 +00:00
|
|
|
of 17:
|
|
|
|
if path.len != 0:
|
|
|
|
var branch = nodeRlp.listElem(path[0].int)
|
|
|
|
if not branch.isEmpty:
|
2024-05-28 08:05:57 +00:00
|
|
|
let nextLookup = branch.getNode
|
2019-02-05 12:01:10 +00:00
|
|
|
output.add nextLookup
|
2024-05-28 08:05:57 +00:00
|
|
|
getBranchAux(db, nextLookup, path.slice(1), output)
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
2020-02-05 12:29:54 +00:00
|
|
|
raise newException(CorruptedTrieDatabase,
|
2019-02-05 12:01:10 +00:00
|
|
|
"HexaryTrie node with an unexpected number of children")
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc getBranch*(self: HexaryTrie; key: openArray[byte]): seq[seq[byte]] =
|
2019-02-05 12:01:10 +00:00
|
|
|
result = @[]
|
|
|
|
var node = keyToLocalBytes(self.db, self.root)
|
|
|
|
result.add node
|
2024-05-28 08:05:57 +00:00
|
|
|
getBranchAux(self.db, node, initNibbleRange(key), result)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc dbDel(t: var HexaryTrie, data: openArray[byte]) =
|
2022-09-02 14:57:52 +00:00
|
|
|
if data.len >= 32: t.prune(data.keccakHash.data)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2021-05-11 15:32:47 +00:00
|
|
|
proc dbPut(db: DB, data: openArray[byte]): TrieNodeKey
|
2023-05-10 13:50:04 +00:00
|
|
|
{.raises: [].} =
|
2022-09-02 14:57:52 +00:00
|
|
|
result.hash = data.keccakHash
|
2019-02-05 12:01:10 +00:00
|
|
|
result.usedBytes = 32
|
2020-04-20 18:14:39 +00:00
|
|
|
put(db, result.asDbKey, data)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc appendAndSave(rlpWriter: var RlpWriter, data: openArray[byte], db: DB) =
|
2019-02-05 12:01:10 +00:00
|
|
|
if data.len >= 32:
|
|
|
|
var nodeKey = dbPut(db, data)
|
|
|
|
rlpWriter.append(nodeKey.hash)
|
|
|
|
else:
|
|
|
|
rlpWriter.appendRawBytes(data)
|
|
|
|
|
|
|
|
proc isTrieBranch(rlp: Rlp): bool =
|
|
|
|
rlp.isList and (var len = rlp.listLen; len == 2 or len == 17)
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc replaceValue(data: Rlp, key: NibblesSeq, value: openArray[byte]): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
if data.isEmpty:
|
|
|
|
let prefix = hexPrefixEncode(key, true)
|
|
|
|
return encodeList(prefix, value)
|
|
|
|
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert data.isTrieBranch
|
2019-02-05 12:01:10 +00:00
|
|
|
if data.listLen == 2:
|
|
|
|
return encodeList(data.listElem(0), value)
|
|
|
|
|
|
|
|
var r = initRlpList(17)
|
|
|
|
|
2020-02-24 22:51:39 +00:00
|
|
|
# XXX: This can be optimized to a direct bitwise copy of the source RLP
|
2019-02-05 12:01:10 +00:00
|
|
|
var iter = data
|
2020-02-27 18:09:05 +00:00
|
|
|
# We already know that we are working with a list
|
2020-02-29 16:35:08 +00:00
|
|
|
doAssert iter.enterList()
|
2019-02-05 12:01:10 +00:00
|
|
|
for i in 0 ..< 16:
|
|
|
|
r.append iter
|
|
|
|
iter.skipElem
|
|
|
|
|
|
|
|
r.append value
|
|
|
|
return r.finish()
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc isTwoItemNode(self: HexaryTrie; r: Rlp): bool =
|
2019-02-05 12:01:10 +00:00
|
|
|
if r.isBlob:
|
2024-05-28 08:05:57 +00:00
|
|
|
let resolved = self.db.get(r)
|
2019-02-05 12:01:10 +00:00
|
|
|
let rlp = rlpFromBytes(resolved)
|
|
|
|
return rlp.isList and rlp.listLen == 2
|
|
|
|
else:
|
|
|
|
return r.isList and r.listLen == 2
|
|
|
|
|
|
|
|
proc findSingleChild(r: Rlp; childPos: var byte): Rlp =
|
|
|
|
result = zeroBytesRlp
|
|
|
|
var i: byte = 0
|
|
|
|
var rlp = r
|
|
|
|
for elem in rlp:
|
|
|
|
if not elem.isEmpty:
|
|
|
|
if not result.hasData:
|
|
|
|
result = elem
|
|
|
|
childPos = i
|
|
|
|
else:
|
|
|
|
return zeroBytesRlp
|
|
|
|
inc i
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc deleteAt(self: var HexaryTrie; origRlp: Rlp, key: NibblesSeq): seq[byte]
|
2023-05-10 13:50:04 +00:00
|
|
|
{.gcsafe, raises: [RlpError].}
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc deleteAux(self: var HexaryTrie; rlpWriter: var RlpWriter;
|
|
|
|
origRlp: Rlp; path: NibblesSeq): bool =
|
2019-02-05 12:01:10 +00:00
|
|
|
if origRlp.isEmpty:
|
|
|
|
return false
|
|
|
|
|
|
|
|
var toDelete = if origRlp.isList: origRlp
|
2024-05-28 08:05:57 +00:00
|
|
|
else: rlpFromBytes self.db.get(origRlp)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
let b = self.deleteAt(toDelete, path)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
if b.len == 0:
|
|
|
|
return false
|
|
|
|
|
|
|
|
rlpWriter.appendAndSave(b, self.db)
|
|
|
|
return true
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc graft(self: var HexaryTrie; r: Rlp): seq[byte] =
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert r.isList and r.listLen == 2
|
2020-04-20 18:14:39 +00:00
|
|
|
var (_, origPath) = r.extensionNodeKey
|
2019-02-05 12:01:10 +00:00
|
|
|
var value = r.listElem(1)
|
|
|
|
|
|
|
|
if not value.isList:
|
|
|
|
let nodeKey = value.expectHash
|
2024-05-28 08:05:57 +00:00
|
|
|
var resolvedData = self.db.get(nodeKey)
|
2020-04-20 18:14:39 +00:00
|
|
|
self.prune(nodeKey)
|
2019-02-05 12:01:10 +00:00
|
|
|
value = rlpFromBytes resolvedData
|
|
|
|
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert value.listLen == 2
|
2019-02-05 12:01:10 +00:00
|
|
|
let (valueIsLeaf, valueKey) = value.extensionNodeKey
|
|
|
|
|
|
|
|
var rlpWriter = initRlpList(2)
|
|
|
|
rlpWriter.append hexPrefixEncode(origPath, valueKey, valueIsLeaf)
|
|
|
|
rlpWriter.append value.listElem(1)
|
|
|
|
return rlpWriter.finish
|
|
|
|
|
|
|
|
proc mergeAndGraft(self: var HexaryTrie;
|
2020-04-20 18:14:39 +00:00
|
|
|
soleChild: Rlp, childPos: byte): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
var output = initRlpList(2)
|
|
|
|
if childPos == 16:
|
2020-04-20 18:14:39 +00:00
|
|
|
output.append hexPrefixEncode(NibblesSeq(), true)
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(not soleChild.isEmpty)
|
2019-02-05 12:01:10 +00:00
|
|
|
output.append int(hexPrefixEncodeByte(childPos))
|
|
|
|
output.append(soleChild)
|
|
|
|
result = output.finish()
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
if self.isTwoItemNode(soleChild):
|
|
|
|
result = self.graft(rlpFromBytes(result))
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
proc deleteAt(self: var HexaryTrie; origRlp: Rlp, key: NibblesSeq): seq[byte]
|
2023-05-10 13:50:04 +00:00
|
|
|
{.gcsafe, raises: [RlpError].} =
|
2019-02-05 12:01:10 +00:00
|
|
|
if origRlp.isEmpty:
|
2020-04-20 18:14:39 +00:00
|
|
|
return
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert origRlp.isTrieBranch
|
2020-04-20 18:14:39 +00:00
|
|
|
let origBytes = @(origRlp.rawData)
|
2019-02-05 12:01:10 +00:00
|
|
|
if origRlp.listLen == 2:
|
|
|
|
let (isLeaf, k) = origRlp.extensionNodeKey
|
2024-05-28 08:05:57 +00:00
|
|
|
if k == key and isLeaf:
|
2019-02-05 12:01:10 +00:00
|
|
|
self.dbDel origBytes
|
2020-04-20 18:14:39 +00:00
|
|
|
return emptyRlp
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
if key.startsWith(k):
|
|
|
|
var
|
|
|
|
rlpWriter = initRlpList(2)
|
|
|
|
path = origRlp.listElem(0)
|
|
|
|
value = origRlp.listElem(1)
|
2019-02-05 12:01:10 +00:00
|
|
|
rlpWriter.append(path)
|
2024-05-28 08:05:57 +00:00
|
|
|
if not self.deleteAux(rlpWriter, value, key.slice(k.len)):
|
2020-04-20 18:14:39 +00:00
|
|
|
return
|
2019-02-05 12:01:10 +00:00
|
|
|
self.dbDel origBytes
|
2020-04-20 18:14:39 +00:00
|
|
|
var finalBytes = rlpWriter.finish
|
2019-02-05 12:01:10 +00:00
|
|
|
var rlp = rlpFromBytes(finalBytes)
|
2024-05-28 08:05:57 +00:00
|
|
|
if self.isTwoItemNode(rlp.listElem(1)):
|
|
|
|
return self.graft(rlp)
|
2019-02-05 12:01:10 +00:00
|
|
|
return finalBytes
|
|
|
|
else:
|
2020-04-20 18:14:39 +00:00
|
|
|
return
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
2024-05-28 08:05:57 +00:00
|
|
|
if key.len == 0 and origRlp.listElem(16).isEmpty:
|
2019-02-05 12:01:10 +00:00
|
|
|
self.dbDel origBytes
|
|
|
|
var foundChildPos: byte
|
|
|
|
let singleChild = origRlp.findSingleChild(foundChildPos)
|
|
|
|
if singleChild.hasData and foundChildPos != 16:
|
2024-05-28 08:05:57 +00:00
|
|
|
result = self.mergeAndGraft(singleChild, foundChildPos)
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
|
|
|
var rlpRes = initRlpList(17)
|
|
|
|
var iter = origRlp
|
2020-02-27 18:09:05 +00:00
|
|
|
# We already know that we are working with a list
|
2020-02-29 16:35:08 +00:00
|
|
|
doAssert iter.enterList
|
2019-02-05 12:01:10 +00:00
|
|
|
for i in 0 ..< 16:
|
|
|
|
rlpRes.append iter
|
|
|
|
iter.skipElem
|
|
|
|
rlpRes.append ""
|
2020-04-20 18:14:39 +00:00
|
|
|
return rlpRes.finish
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
|
|
|
var rlpWriter = initRlpList(17)
|
2024-05-28 08:05:57 +00:00
|
|
|
let keyHead = int(key[0])
|
2019-02-05 12:01:10 +00:00
|
|
|
var i = 0
|
|
|
|
var origCopy = origRlp
|
|
|
|
for elem in items(origCopy):
|
|
|
|
if i == keyHead:
|
2024-05-28 08:05:57 +00:00
|
|
|
if not self.deleteAux(rlpWriter, elem, key.slice(1)):
|
2020-04-20 18:14:39 +00:00
|
|
|
return
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
|
|
|
rlpWriter.append(elem)
|
|
|
|
inc i
|
|
|
|
|
|
|
|
self.dbDel origBytes
|
2020-04-20 18:14:39 +00:00
|
|
|
result = rlpWriter.finish
|
2019-02-05 12:01:10 +00:00
|
|
|
var resultRlp = rlpFromBytes(result)
|
|
|
|
var foundChildPos: byte
|
|
|
|
let singleChild = resultRlp.findSingleChild(foundChildPos)
|
|
|
|
if singleChild.hasData:
|
2024-05-28 08:05:57 +00:00
|
|
|
result = self.mergeAndGraft(singleChild, foundChildPos)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc del*(self: var HexaryTrie; key: openArray[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
var
|
|
|
|
rootBytes = keyToLocalBytes(self.db, self.root)
|
|
|
|
rootRlp = rlpFromBytes rootBytes
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
var newRootBytes = self.deleteAt(rootRlp, initNibbleRange(key))
|
2019-02-05 12:01:10 +00:00
|
|
|
if newRootBytes.len > 0:
|
|
|
|
if rootBytes.len < 32:
|
|
|
|
self.prune(self.root.asDbKey)
|
|
|
|
self.root = self.db.dbPut(newRootBytes)
|
|
|
|
|
|
|
|
proc mergeAt(self: var HexaryTrie, orig: Rlp, origHash: KeccakHash,
|
2024-05-28 08:05:57 +00:00
|
|
|
key: NibblesSeq, value: openArray[byte],
|
2021-05-11 15:32:47 +00:00
|
|
|
isInline = false): seq[byte]
|
2023-05-10 13:50:04 +00:00
|
|
|
{.gcsafe, raises: [RlpError].}
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
proc mergeAt(self: var HexaryTrie, rlp: Rlp,
|
2024-05-28 08:05:57 +00:00
|
|
|
key: NibblesSeq, value: openArray[byte],
|
2020-04-20 18:14:39 +00:00
|
|
|
isInline = false): seq[byte] =
|
2024-05-28 08:05:57 +00:00
|
|
|
self.mergeAt(rlp, rlp.rawData.keccakHash, key, value, isInline)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
proc mergeAtAux(self: var HexaryTrie, output: var RlpWriter, orig: Rlp,
|
2024-05-28 08:05:57 +00:00
|
|
|
key: NibblesSeq, value: openArray[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
var resolved = orig
|
|
|
|
var isRemovable = false
|
|
|
|
if not (orig.isList or orig.isEmpty):
|
2024-05-28 08:05:57 +00:00
|
|
|
resolved = rlpFromBytes self.db.get(orig)
|
2019-02-05 12:01:10 +00:00
|
|
|
isRemovable = true
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
let b = self.mergeAt(resolved, key, value, not isRemovable)
|
2019-02-05 12:01:10 +00:00
|
|
|
output.appendAndSave(b, self.db)
|
|
|
|
|
|
|
|
proc mergeAt(self: var HexaryTrie, orig: Rlp, origHash: KeccakHash,
|
2024-05-28 08:05:57 +00:00
|
|
|
key: NibblesSeq, value: openArray[byte],
|
2021-05-11 15:32:47 +00:00
|
|
|
isInline = false): seq[byte]
|
2023-05-10 13:50:04 +00:00
|
|
|
{.gcsafe, raises: [RlpError].} =
|
2019-02-05 12:01:10 +00:00
|
|
|
template origWithNewValue: auto =
|
|
|
|
self.prune(origHash.data)
|
2024-05-28 08:05:57 +00:00
|
|
|
replaceValue(orig, key, value)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
if orig.isEmpty:
|
|
|
|
return origWithNewValue()
|
|
|
|
|
2020-04-27 13:16:11 +00:00
|
|
|
doAssert orig.isTrieBranch, $orig
|
2019-02-05 12:01:10 +00:00
|
|
|
if orig.listLen == 2:
|
|
|
|
let (isLeaf, k) = orig.extensionNodeKey
|
|
|
|
var origValue = orig.listElem(1)
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
if k == key and isLeaf:
|
2019-02-05 12:01:10 +00:00
|
|
|
return origWithNewValue()
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
let sharedNibbles = sharedPrefixLen(key, k)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
if sharedNibbles == k.len and not isLeaf:
|
|
|
|
var r = initRlpList(2)
|
|
|
|
r.append orig.listElem(0)
|
2024-05-28 08:05:57 +00:00
|
|
|
self.mergeAtAux(r, origValue, key.slice(k.len), value)
|
2020-04-20 18:14:39 +00:00
|
|
|
return r.finish
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
if orig.rawData.len >= 32:
|
|
|
|
self.prune(origHash.data)
|
|
|
|
|
|
|
|
if sharedNibbles > 0:
|
|
|
|
# Split the extension node
|
|
|
|
var bottom = initRlpList(2)
|
|
|
|
bottom.append hexPrefixEncode(k.slice(sharedNibbles), isLeaf)
|
|
|
|
bottom.append origValue
|
|
|
|
|
|
|
|
var top = initRlpList(2)
|
|
|
|
top.append hexPrefixEncode(k.slice(0, sharedNibbles), false)
|
2020-04-20 18:14:39 +00:00
|
|
|
top.appendAndSave(bottom.finish, self.db)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
return self.mergeAt(rlpFromBytes(top.finish), key, value, true)
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
|
|
|
# Create a branch node
|
|
|
|
var branches = initRlpList(17)
|
|
|
|
if k.len == 0:
|
|
|
|
# The key is now exhausted. This must be a leaf node
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert isLeaf
|
2019-02-05 12:01:10 +00:00
|
|
|
for i in 0 ..< 16:
|
|
|
|
branches.append ""
|
|
|
|
branches.append origValue
|
|
|
|
else:
|
|
|
|
let n = k[0]
|
|
|
|
for i in 0 ..< 16:
|
|
|
|
if byte(i) == n:
|
|
|
|
if isLeaf or k.len > 1:
|
|
|
|
let childNode = encodeList(hexPrefixEncode(k.slice(1), isLeaf),
|
2020-04-20 18:14:39 +00:00
|
|
|
origValue)
|
2019-02-05 12:01:10 +00:00
|
|
|
branches.appendAndSave(childNode, self.db)
|
|
|
|
else:
|
|
|
|
branches.append origValue
|
|
|
|
else:
|
|
|
|
branches.append ""
|
|
|
|
branches.append ""
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
return self.mergeAt(rlpFromBytes(branches.finish), key, value, true)
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
2024-05-28 08:05:57 +00:00
|
|
|
if key.len == 0:
|
2019-02-05 12:01:10 +00:00
|
|
|
return origWithNewValue()
|
|
|
|
|
|
|
|
if isInline:
|
|
|
|
self.prune(origHash.data)
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
let n = key[0]
|
2019-02-05 12:01:10 +00:00
|
|
|
var i = 0
|
|
|
|
var r = initRlpList(17)
|
|
|
|
|
|
|
|
var origCopy = orig
|
|
|
|
for elem in items(origCopy):
|
|
|
|
if i == int(n):
|
2024-05-28 08:05:57 +00:00
|
|
|
self.mergeAtAux(r, elem, key.slice(1), value)
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
|
|
|
r.append(elem)
|
|
|
|
inc i
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
return r.finish
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc put*(self: var HexaryTrie; key, value: openArray[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
let root = self.root.hash
|
|
|
|
|
2024-05-28 08:05:57 +00:00
|
|
|
var rootBytes = self.db.get(root.data)
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert rootBytes.len > 0
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
let newRootBytes = self.mergeAt(rlpFromBytes(rootBytes), root,
|
2024-05-28 08:05:57 +00:00
|
|
|
initNibbleRange(key), value)
|
2019-02-05 12:01:10 +00:00
|
|
|
if rootBytes.len < 32:
|
|
|
|
self.prune(root.data)
|
|
|
|
|
|
|
|
self.root = self.db.dbPut(newRootBytes)
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc put*(self: var SecureHexaryTrie; key, value: openArray[byte]) =
|
2022-09-02 14:57:52 +00:00
|
|
|
put(HexaryTrie(self), key.keccakHash.data, value)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc get*(self: SecureHexaryTrie; key: openArray[byte]): seq[byte] =
|
2022-09-02 14:57:52 +00:00
|
|
|
return get(HexaryTrie(self), key.keccakHash.data)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc del*(self: var SecureHexaryTrie; key: openArray[byte]) =
|
2022-09-02 14:57:52 +00:00
|
|
|
del(HexaryTrie(self), key.keccakHash.data)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
proc rootHash*(self: SecureHexaryTrie): KeccakHash {.borrow.}
|
|
|
|
proc rootHashHex*(self: SecureHexaryTrie): string {.borrow.}
|
|
|
|
proc isPruning*(self: SecureHexaryTrie): bool {.borrow.}
|
|
|
|
|
|
|
|
template contains*(self: HexaryTrie | SecureHexaryTrie;
|
2020-04-20 18:14:39 +00:00
|
|
|
key: openArray[byte]): bool =
|
2019-02-05 12:01:10 +00:00
|
|
|
self.get(key).len > 0
|
2021-08-12 14:15:02 +00:00
|
|
|
|
|
|
|
# Validates merkle proof against provided root hash
|
|
|
|
proc isValidBranch*(branch: seq[seq[byte]], rootHash: KeccakHash, key, value: seq[byte]): bool =
|
|
|
|
# branch must not be empty
|
|
|
|
doAssert(branch.len != 0)
|
|
|
|
|
|
|
|
var db = newMemoryDB()
|
|
|
|
for node in branch:
|
|
|
|
doAssert(node.len != 0)
|
2022-09-02 14:57:52 +00:00
|
|
|
let nodeHash = keccakHash(node)
|
2021-08-12 14:15:02 +00:00
|
|
|
db.put(nodeHash.data, node)
|
|
|
|
|
|
|
|
var trie = initHexaryTrie(db, rootHash)
|
|
|
|
result = trie.get(key) == value
|