remove 17th elem code from branch node parser/encoder

This commit is contained in:
andri lim 2020-04-24 16:04:43 +07:00
parent 649597e714
commit 8db1109515
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 12 additions and 37 deletions

View File

@ -24,7 +24,7 @@ proc initTreeBuilder*(input: openArray[byte], db: DB): TreeBuilder =
result.input = memoryInput(input)
result.db = db
result.root = emptyRlpHash
func rootHash*(t: TreeBuilder): KeccakHash {.inline.} =
t.root
@ -72,7 +72,7 @@ proc toNodeKey(z: openArray[byte]): NodeKey =
result.data = keccak(z).data
result.usedBytes = 32
proc branchNode(t: var TreeBuilder, depth: int, has16Elem: bool = true): NodeKey
proc branchNode(t: var TreeBuilder, depth: int): NodeKey
proc extensionNode(t: var TreeBuilder, depth: int): NodeKey
proc accountNode(t: var TreeBuilder, depth: int): NodeKey
proc accountStorageLeafNode(t: var TreeBuilder, depth: int): NodeKey
@ -84,7 +84,6 @@ proc treeNode*(t: var TreeBuilder, depth: int = 0, accountMode = false): NodeKey
case nodeType
of BranchNodeType: result = t.branchNode(depth)
of Branch17NodeType: result = t.branchNode(depth, false)
of ExtensionNodeType: result = t.extensionNode(depth)
of AccountNodeType:
if accountMode:
@ -98,7 +97,7 @@ proc treeNode*(t: var TreeBuilder, depth: int = 0, accountMode = false): NodeKey
result.data = keccak(result.data.toOpenArray(0, result.usedBytes-1)).data
result.usedBytes = 32
proc branchNode(t: var TreeBuilder, depth: int, has16Elem: bool): NodeKey =
proc branchNode(t: var TreeBuilder, depth: int): NodeKey =
assert(depth < 64)
let mask = constructBranchMask(t.readByte, t.readByte)
@ -117,21 +116,8 @@ proc branchNode(t: var TreeBuilder, depth: int, has16Elem: bool): NodeKey =
else:
r.append ""
template safePeek(t: var TreeBuilder): int =
if t.len == 0 or has16Elem:
-1
else:
t.peek().int
# add the 17th elem
let nodeType = t.safePeek()
if nodeType == AccountNodeType.int:
r.append accountNode(t, depth+1)
elif nodeType == HashNodeType.int:
r.append hashNode(t)
else:
# anything else is empty
r.append ""
# 17th elem should always empty
r.append ""
result = toNodeKey(r.finish)
@ -175,7 +161,6 @@ proc extensionNode(t: var TreeBuilder, depth: int): NodeKey =
case nodeType
of BranchNodeType: r.append t.branchNode(depth + nibblesLen)
of Branch17NodeType: r.append t.branchNode(depth + nibblesLen, false)
of HashNodeType: r.append t.hashNode()
else: raise newException(ValueError, "wrong type during parsing child of extension node")

View File

@ -73,10 +73,9 @@ proc writeExtensionNode(wb: var WitnessBuilder, n: NibblesSeq, depth: int, node:
proc writeBranchNode(wb: var WitnessBuilder, mask: uint, depth: int, node: openArray[byte]) =
# write type
if mask.branchMaskBitIsSet(16):
wb.output.append(Branch17NodeType.byte)
else:
wb.output.append(BranchNodeType.byte)
# branch node 17th elem should always empty
doAssert mask.branchMaskBitIsSet(16) == false
wb.output.append(BranchNodeType.byte)
# write branch mask
wb.output.append(((mask shr 8) and 0xFF).byte)
wb.output.append((mask and 0xFF).byte)
@ -121,9 +120,8 @@ proc writeShortNode(wb: var WitnessBuilder, node: openArray[byte], depth: int) =
let nextLookup = branch.getNode
writeShortNode(wb, nextLookup, depth + 1)
var lastElem = nodeRlp.listElem(16)
if not lastElem.isEmpty:
writeAccountNode(wb, lastElem.toBytes, depth)
# 17th elem should always empty
doAssert branchMask.branchMaskBitIsSet(16) == false
else:
raise newException(CorruptedTrieDatabase, "Bad Short Node")
@ -165,15 +163,8 @@ proc getBranchRecurseAux(wb: var WitnessBuilder, node: openArray[byte], path: Ni
else:
writeHashNode(wb, branch.expectHash)
# put 17th elem
var lastElem = nodeRlp.listElem(16)
if not lastElem.isEmpty:
if path.len == 0:
doAssert(false, "ACC NODE A?")
writeAccountNode(wb, lastElem.toBytes, depth)
else:
doAssert(false, "HASH NODE B?")
writeHashNode(wb, lastElem.expectHash)
# 17th elem should always empty
doAssert branchMask.branchMaskBitIsSet(16) == false
else:
raise newException(CorruptedTrieDatabase,
"HexaryTrie node with an unexpected number of children")

View File

@ -3,7 +3,6 @@ import nimcrypto/[keccak, hash], stew/bitops2
type
TrieNodeType* = enum
BranchNodeType
Branch17NodeType
ExtensionNodeType
AccountNodeType
HashNodeType