mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-23 18:49:57 +00:00
separate underdeveloped stack based witness builder source code from recursive code
This commit is contained in:
parent
d95ded217b
commit
4e12ba825c
92
stateless/stack_based_witness_builder.nim
Normal file
92
stateless/stack_based_witness_builder.nim
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
proc getBranchStack*(wb: WitnessBuilder; key: openArray[byte]): string =
|
||||||
|
var
|
||||||
|
node = wb.db.get(wb.root.data)
|
||||||
|
stack = @[(node, initNibbleRange(key))]
|
||||||
|
|
||||||
|
result.add node.toHex
|
||||||
|
while stack.len > 0:
|
||||||
|
let (node, path) = stack.pop()
|
||||||
|
if node.len == 0: continue
|
||||||
|
var nodeRlp = rlpFromBytes node
|
||||||
|
|
||||||
|
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
|
||||||
|
stack.add((nextLookup, path.slice(sharedNibbles)))
|
||||||
|
result.add nextLookup.toHex
|
||||||
|
of 17:
|
||||||
|
if path.len != 0:
|
||||||
|
var branch = nodeRlp.listElem(path[0].int)
|
||||||
|
if not branch.isEmpty:
|
||||||
|
let nextLookup = branch.getNode
|
||||||
|
stack.add((nextLookup, path.slice(1)))
|
||||||
|
result.add nextLookup.toHex
|
||||||
|
else:
|
||||||
|
raise newException(CorruptedTrieDatabase,
|
||||||
|
"HexaryTrie node with an unexpected number of children")
|
||||||
|
|
||||||
|
proc getBranch*(wb: WitnessBuilder; key: openArray[byte]): seq[seq[byte]] =
|
||||||
|
var
|
||||||
|
node = wb.db.get(wb.root.data)
|
||||||
|
stack = @[(node, initNibbleRange(key))]
|
||||||
|
|
||||||
|
result.add node
|
||||||
|
while stack.len > 0:
|
||||||
|
let (node, path) = stack.pop()
|
||||||
|
if node.len == 0: continue
|
||||||
|
var nodeRlp = rlpFromBytes node
|
||||||
|
|
||||||
|
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
|
||||||
|
stack.add((nextLookup, path.slice(sharedNibbles)))
|
||||||
|
result.add nextLookup
|
||||||
|
of 17:
|
||||||
|
if path.len != 0:
|
||||||
|
var branch = nodeRlp.listElem(path[0].int)
|
||||||
|
if not branch.isEmpty:
|
||||||
|
let nextLookup = branch.getNode
|
||||||
|
stack.add((nextLookup, path.slice(1)))
|
||||||
|
result.add nextLookup
|
||||||
|
else:
|
||||||
|
raise newException(CorruptedTrieDatabase,
|
||||||
|
"HexaryTrie node with an unexpected number of children")
|
||||||
|
|
||||||
|
proc buildWitness*(wb: var WitnessBuilder; key: openArray[byte]) =
|
||||||
|
var
|
||||||
|
node = wb.db.get(wb.root.data)
|
||||||
|
stack = @[(node, initNibbleRange(key))]
|
||||||
|
|
||||||
|
while stack.len > 0:
|
||||||
|
let (node, path) = stack.pop()
|
||||||
|
if node.len == 0: continue
|
||||||
|
var nodeRlp = rlpFromBytes node
|
||||||
|
|
||||||
|
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
|
||||||
|
stack.add((nextLookup, path.slice(sharedNibbles)))
|
||||||
|
of 17:
|
||||||
|
if path.len != 0:
|
||||||
|
var branch = nodeRlp.listElem(path[0].int)
|
||||||
|
if not branch.isEmpty:
|
||||||
|
let nextLookup = branch.getNode
|
||||||
|
stack.add((nextLookup, path.slice(1)))
|
||||||
|
else:
|
||||||
|
raise newException(CorruptedTrieDatabase,
|
||||||
|
"HexaryTrie node with an unexpected number of children")
|
@ -245,96 +245,3 @@ proc getBranchRecurse*(wb: var WitnessBuilder; key: openArray[byte]): seq[byte]
|
|||||||
var node = wb.db.get(wb.root.data)
|
var node = wb.db.get(wb.root.data)
|
||||||
getBranchRecurseAux(wb, node, initNibbleRange(key), 0, false)
|
getBranchRecurseAux(wb, node, initNibbleRange(key), 0, false)
|
||||||
result = wb.output.getOutput(seq[byte])
|
result = wb.output.getOutput(seq[byte])
|
||||||
|
|
||||||
proc getBranchStack*(wb: WitnessBuilder; key: openArray[byte]): string =
|
|
||||||
var
|
|
||||||
node = wb.db.get(wb.root.data)
|
|
||||||
stack = @[(node, initNibbleRange(key))]
|
|
||||||
|
|
||||||
result.add node.toHex
|
|
||||||
while stack.len > 0:
|
|
||||||
let (node, path) = stack.pop()
|
|
||||||
if node.len == 0: continue
|
|
||||||
var nodeRlp = rlpFromBytes node
|
|
||||||
|
|
||||||
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
|
|
||||||
stack.add((nextLookup, path.slice(sharedNibbles)))
|
|
||||||
result.add nextLookup.toHex
|
|
||||||
of 17:
|
|
||||||
if path.len != 0:
|
|
||||||
var branch = nodeRlp.listElem(path[0].int)
|
|
||||||
if not branch.isEmpty:
|
|
||||||
let nextLookup = branch.getNode
|
|
||||||
stack.add((nextLookup, path.slice(1)))
|
|
||||||
result.add nextLookup.toHex
|
|
||||||
else:
|
|
||||||
raise newException(CorruptedTrieDatabase,
|
|
||||||
"HexaryTrie node with an unexpected number of children")
|
|
||||||
|
|
||||||
proc getBranch*(wb: WitnessBuilder; key: openArray[byte]): seq[seq[byte]] =
|
|
||||||
var
|
|
||||||
node = wb.db.get(wb.root.data)
|
|
||||||
stack = @[(node, initNibbleRange(key))]
|
|
||||||
|
|
||||||
result.add node
|
|
||||||
while stack.len > 0:
|
|
||||||
let (node, path) = stack.pop()
|
|
||||||
if node.len == 0: continue
|
|
||||||
var nodeRlp = rlpFromBytes node
|
|
||||||
|
|
||||||
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
|
|
||||||
stack.add((nextLookup, path.slice(sharedNibbles)))
|
|
||||||
result.add nextLookup
|
|
||||||
of 17:
|
|
||||||
if path.len != 0:
|
|
||||||
var branch = nodeRlp.listElem(path[0].int)
|
|
||||||
if not branch.isEmpty:
|
|
||||||
let nextLookup = branch.getNode
|
|
||||||
stack.add((nextLookup, path.slice(1)))
|
|
||||||
result.add nextLookup
|
|
||||||
else:
|
|
||||||
raise newException(CorruptedTrieDatabase,
|
|
||||||
"HexaryTrie node with an unexpected number of children")
|
|
||||||
|
|
||||||
proc buildWitness*(wb: var WitnessBuilder; key: openArray[byte]) =
|
|
||||||
var
|
|
||||||
node = wb.db.get(wb.root.data)
|
|
||||||
stack = @[(node, initNibbleRange(key))]
|
|
||||||
|
|
||||||
while stack.len > 0:
|
|
||||||
let (node, path) = stack.pop()
|
|
||||||
if node.len == 0: continue
|
|
||||||
var nodeRlp = rlpFromBytes node
|
|
||||||
|
|
||||||
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
|
|
||||||
stack.add((nextLookup, path.slice(sharedNibbles)))
|
|
||||||
of 17:
|
|
||||||
if path.len != 0:
|
|
||||||
var branch = nodeRlp.listElem(path[0].int)
|
|
||||||
if not branch.isEmpty:
|
|
||||||
let nextLookup = branch.getNode
|
|
||||||
stack.add((nextLookup, path.slice(1)))
|
|
||||||
else:
|
|
||||||
raise newException(CorruptedTrieDatabase,
|
|
||||||
"HexaryTrie node with an unexpected number of children")
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user