2019-02-05 12:01:10 +00:00
|
|
|
import
|
2021-04-06 11:33:24 +00:00
|
|
|
std/tables,
|
|
|
|
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
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc keccak*(r: openArray[byte]): KeccakHash =
|
|
|
|
keccak256.digest r
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc dbPut(db: DB, data: openArray[byte]): TrieNodeKey {.gcsafe.}
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +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
|
|
|
|
|
|
|
|
proc initHexaryTrie*(db: DB, rootHash: KeccakHash, isPruning = true): HexaryTrie =
|
|
|
|
result.db = db
|
|
|
|
result.root = rootHash
|
|
|
|
result.isPruning = isPruning
|
|
|
|
|
|
|
|
template initSecureHexaryTrie*(db: DB, rootHash: KeccakHash, isPruning = true): SecureHexaryTrie =
|
|
|
|
SecureHexaryTrie initHexaryTrie(db, rootHash, isPruning)
|
|
|
|
|
|
|
|
proc initHexaryTrie*(db: DB, isPruning = true): HexaryTrie =
|
|
|
|
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
|
|
|
|
|
|
|
|
template initSecureHexaryTrie*(db: DB, isPruning = true): SecureHexaryTrie =
|
|
|
|
SecureHexaryTrie initHexaryTrie(db, isPruning)
|
|
|
|
|
|
|
|
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
|
2020-04-20 18:14:39 +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
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc getAux(db: DB, nodeRlp: Rlp, path: NibblesSeq): seq[byte] {.gcsafe.}
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc getAuxByHash(db: DB, node: TrieNodeKey, path: NibblesSeq): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
var nodeRlp = rlpFromBytes keyToLocalBytes(db, node)
|
|
|
|
return getAux(db, nodeRlp, path)
|
|
|
|
|
|
|
|
template getLookup(elem: untyped): untyped =
|
|
|
|
if elem.isList: elem
|
2020-04-20 18:14:39 +00:00
|
|
|
else: rlpFromBytes(get(db, elem.expectHash))
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc getAux(db: DB, nodeRlp: Rlp, path: NibblesSeq): seq[byte] =
|
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:
|
|
|
|
let nextLookup = value.getLookup
|
|
|
|
return getAux(db, nextLookup, path.slice(sharedNibbles))
|
|
|
|
|
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:
|
|
|
|
let nextLookup = branch.getLookup
|
|
|
|
return getAux(db, nextLookup, path.slice(1))
|
|
|
|
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] =
|
2019-02-05 12:01:10 +00:00
|
|
|
return getAuxByHash(self.db, self.root, initNibbleRange(key))
|
|
|
|
|
2020-04-20 18:14:39 +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)
|
|
|
|
nextLookup = value.getLookup
|
|
|
|
stack.add((nextLookup, key))
|
|
|
|
of 17:
|
|
|
|
for i in 0 ..< 16:
|
|
|
|
var branch = nodeRlp.listElem(i)
|
|
|
|
if not branch.isEmpty:
|
|
|
|
let nextLookup = branch.getLookup
|
|
|
|
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:
|
|
|
|
yield getKeysAux(self.db, stack)
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc getValuesAux(db: DB, stack: var seq[Rlp]): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
while stack.len > 0:
|
|
|
|
let nodeRlp = stack.pop()
|
|
|
|
if not nodeRlp.hasData or nodeRlp.isEmpty:
|
|
|
|
continue
|
|
|
|
|
|
|
|
case nodeRlp.listLen
|
|
|
|
of 2:
|
|
|
|
let
|
|
|
|
(isLeaf, _) = nodeRlp.extensionNodeKey
|
|
|
|
value = nodeRlp.listElem(1)
|
|
|
|
|
|
|
|
if isLeaf:
|
|
|
|
return value.toBytes
|
|
|
|
else:
|
|
|
|
let nextLookup = value.getLookup
|
|
|
|
stack.add(nextLookup)
|
|
|
|
of 17:
|
|
|
|
for i in 0 ..< 16:
|
|
|
|
var branch = nodeRlp.listElem(i)
|
|
|
|
if not branch.isEmpty:
|
|
|
|
let nextLookup = branch.getLookup
|
|
|
|
stack.add(nextLookup)
|
|
|
|
|
|
|
|
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)
|
|
|
|
stack = @[nodeRlp]
|
|
|
|
while stack.len > 0:
|
|
|
|
yield getValuesAux(self.db, stack)
|
|
|
|
|
2020-04-20 18:14:39 +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:
|
|
|
|
let nextLookup = value.getLookup
|
|
|
|
stack.add((nextLookup, key))
|
|
|
|
of 17:
|
|
|
|
for i in 0 ..< 16:
|
|
|
|
var branch = nodeRlp.listElem(i)
|
|
|
|
if not branch.isEmpty:
|
|
|
|
let nextLookup = branch.getLookup
|
|
|
|
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
|
|
|
|
let res = getPairsAux(self.db, stack)
|
|
|
|
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
|
|
|
|
|
|
|
|
template getNode(elem: untyped): untyped =
|
2020-04-20 18:14:39 +00:00
|
|
|
if elem.isList: @(elem.rawData)
|
|
|
|
else: get(db, elem.expectHash)
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2020-04-20 18:14:39 +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:
|
|
|
|
let nextLookup = value.getNode
|
|
|
|
output.add nextLookup
|
|
|
|
getBranchAux(db, nextLookup, path.slice(sharedNibbles), output)
|
|
|
|
of 17:
|
|
|
|
if path.len != 0:
|
|
|
|
var branch = nodeRlp.listElem(path[0].int)
|
|
|
|
if not branch.isEmpty:
|
|
|
|
let nextLookup = branch.getNode
|
|
|
|
output.add nextLookup
|
|
|
|
getBranchAux(db, nextLookup, path.slice(1), output)
|
|
|
|
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
|
|
|
|
getBranchAux(self.db, node, initNibbleRange(key), result)
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc dbDel(t: var HexaryTrie, data: openArray[byte]) =
|
2019-02-05 12:01:10 +00:00
|
|
|
if data.len >= 32: t.prune(data.keccak.data)
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc dbPut(db: DB, data: openArray[byte]): TrieNodeKey =
|
2019-02-05 12:01:10 +00:00
|
|
|
result.hash = data.keccak
|
|
|
|
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()
|
|
|
|
|
|
|
|
proc isTwoItemNode(self: HexaryTrie; r: Rlp): bool =
|
|
|
|
if r.isBlob:
|
|
|
|
let resolved = self.db.get(r)
|
|
|
|
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
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc deleteAt(self: var HexaryTrie; origRlp: Rlp, key: NibblesSeq): seq[byte] {.gcsafe.}
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
proc deleteAux(self: var HexaryTrie; rlpWriter: var RlpWriter;
|
2020-04-20 18:14:39 +00:00
|
|
|
origRlp: Rlp; path: NibblesSeq): bool =
|
2019-02-05 12:01:10 +00:00
|
|
|
if origRlp.isEmpty:
|
|
|
|
return false
|
|
|
|
|
|
|
|
var toDelete = if origRlp.isList: origRlp
|
|
|
|
else: rlpFromBytes self.db.get(origRlp)
|
|
|
|
|
|
|
|
let b = self.deleteAt(toDelete, path)
|
|
|
|
|
|
|
|
if b.len == 0:
|
|
|
|
return false
|
|
|
|
|
|
|
|
rlpWriter.appendAndSave(b, self.db)
|
|
|
|
return true
|
|
|
|
|
2020-04-20 18:14:39 +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
|
2020-04-20 18:14:39 +00:00
|
|
|
var resolvedData = self.db.get(nodeKey)
|
|
|
|
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()
|
|
|
|
|
|
|
|
if self.isTwoItemNode(soleChild):
|
2020-04-20 18:14:39 +00:00
|
|
|
result = self.graft(rlpFromBytes(result))
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
proc deleteAt(self: var HexaryTrie;
|
2020-04-20 18:14:39 +00:00
|
|
|
origRlp: Rlp, key: NibblesSeq): seq[byte] =
|
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
|
|
|
|
if k == key and isLeaf:
|
|
|
|
self.dbDel origBytes
|
2020-04-20 18:14:39 +00:00
|
|
|
return emptyRlp
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
if key.startsWith(k):
|
|
|
|
var
|
|
|
|
rlpWriter = initRlpList(2)
|
|
|
|
path = origRlp.listElem(0)
|
|
|
|
value = origRlp.listElem(1)
|
|
|
|
rlpWriter.append(path)
|
|
|
|
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)
|
|
|
|
if self.isTwoItemNode(rlp.listElem(1)):
|
2020-04-20 18:14:39 +00:00
|
|
|
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:
|
|
|
|
if key.len == 0 and origRlp.listElem(16).isEmpty:
|
|
|
|
self.dbDel origBytes
|
|
|
|
var foundChildPos: byte
|
|
|
|
let singleChild = origRlp.findSingleChild(foundChildPos)
|
|
|
|
if singleChild.hasData and foundChildPos != 16:
|
2020-04-20 18:14:39 +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)
|
|
|
|
let keyHead = int(key[0])
|
|
|
|
var i = 0
|
|
|
|
var origCopy = origRlp
|
|
|
|
for elem in items(origCopy):
|
|
|
|
if i == keyHead:
|
|
|
|
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:
|
2020-04-20 18:14:39 +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
|
|
|
|
|
|
|
|
var newRootBytes = self.deleteAt(rootRlp, initNibbleRange(key))
|
|
|
|
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,
|
2020-04-20 18:14:39 +00:00
|
|
|
key: NibblesSeq, value: openArray[byte],
|
|
|
|
isInline = false): seq[byte] {.gcsafe.}
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
proc mergeAt(self: var HexaryTrie, rlp: Rlp,
|
2020-04-20 18:14:39 +00:00
|
|
|
key: NibblesSeq, value: openArray[byte],
|
|
|
|
isInline = false): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
self.mergeAt(rlp, rlp.rawData.keccak, key, value, isInline)
|
|
|
|
|
|
|
|
proc mergeAtAux(self: var HexaryTrie, output: var RlpWriter, orig: Rlp,
|
2020-04-20 18:14:39 +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):
|
|
|
|
resolved = rlpFromBytes self.db.get(orig)
|
|
|
|
isRemovable = true
|
|
|
|
|
|
|
|
let b = self.mergeAt(resolved, key, value, not isRemovable)
|
|
|
|
output.appendAndSave(b, self.db)
|
|
|
|
|
|
|
|
proc mergeAt(self: var HexaryTrie, orig: Rlp, origHash: KeccakHash,
|
2020-04-20 18:14:39 +00:00
|
|
|
key: NibblesSeq, value: openArray[byte],
|
|
|
|
isInline = false): seq[byte] =
|
2019-02-05 12:01:10 +00:00
|
|
|
template origWithNewValue: auto =
|
|
|
|
self.prune(origHash.data)
|
2020-04-20 18:14:39 +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)
|
|
|
|
|
|
|
|
if k == key and isLeaf:
|
|
|
|
return origWithNewValue()
|
|
|
|
|
|
|
|
let sharedNibbles = sharedPrefixLen(key, k)
|
|
|
|
|
|
|
|
if sharedNibbles == k.len and not isLeaf:
|
|
|
|
var r = initRlpList(2)
|
|
|
|
r.append orig.listElem(0)
|
|
|
|
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
|
|
|
|
2020-04-20 18:14:39 +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 ""
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
return self.mergeAt(rlpFromBytes(branches.finish), key, value, true)
|
2019-02-05 12:01:10 +00:00
|
|
|
else:
|
|
|
|
if key.len == 0:
|
|
|
|
return origWithNewValue()
|
|
|
|
|
|
|
|
if isInline:
|
|
|
|
self.prune(origHash.data)
|
|
|
|
|
|
|
|
let n = key[0]
|
|
|
|
var i = 0
|
|
|
|
var r = initRlpList(17)
|
|
|
|
|
|
|
|
var origCopy = orig
|
|
|
|
for elem in items(origCopy):
|
|
|
|
if i == int(n):
|
|
|
|
self.mergeAtAux(r, elem, key.slice(1), value)
|
|
|
|
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
|
|
|
|
|
2020-04-20 18:14:39 +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,
|
|
|
|
initNibbleRange(key), value)
|
|
|
|
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]) =
|
|
|
|
put(HexaryTrie(self), key.keccak.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] =
|
|
|
|
return get(HexaryTrie(self), key.keccak.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]) =
|
|
|
|
del(HexaryTrie(self), key.keccak.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
|