2020-04-21 10:20:39 +00:00
|
|
|
import
|
2020-04-22 04:21:39 +00:00
|
|
|
stew/[byteutils, endians2],
|
2020-04-28 02:30:10 +00:00
|
|
|
nimcrypto/[keccak, hash], eth/[common, rlp],
|
2020-04-21 10:20:39 +00:00
|
|
|
eth/trie/[trie_defs, nibbles, db],
|
2020-04-21 11:04:18 +00:00
|
|
|
faststreams/output_stream,
|
2020-04-29 05:12:24 +00:00
|
|
|
./witness_types, ../nimbus/constants,
|
2020-05-05 06:50:31 +00:00
|
|
|
../nimbus/db/storage_types, ./multi_keys
|
2020-04-21 10:20:39 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
DB = TrieDatabaseRef
|
|
|
|
|
|
|
|
WitnessBuilder* = object
|
|
|
|
db*: DB
|
|
|
|
root: KeccakHash
|
2020-04-22 04:21:39 +00:00
|
|
|
output: OutputStream
|
2020-04-29 05:46:50 +00:00
|
|
|
flags: WitnessFlags
|
2020-04-21 10:20:39 +00:00
|
|
|
|
2020-05-05 06:50:31 +00:00
|
|
|
StackElem = object
|
|
|
|
node: seq[byte]
|
|
|
|
parentGroup: Group
|
|
|
|
keys: MultikeysRef
|
|
|
|
depth: int
|
|
|
|
storageMode: bool
|
|
|
|
|
2020-04-29 05:46:50 +00:00
|
|
|
proc initWitnessBuilder*(db: DB, rootHash: KeccakHash, flags: WitnessFlags = {}): WitnessBuilder =
|
2020-04-21 10:20:39 +00:00
|
|
|
result.db = db
|
|
|
|
result.root = rootHash
|
2020-04-22 04:21:39 +00:00
|
|
|
result.output = memoryOutput().s
|
2020-04-29 05:46:50 +00:00
|
|
|
result.flags = flags
|
2020-04-21 10:20:39 +00:00
|
|
|
|
|
|
|
template extensionNodeKey(r: Rlp): auto =
|
|
|
|
hexPrefixDecode r.listElem(0).toBytes
|
|
|
|
|
|
|
|
proc expectHash(r: Rlp): seq[byte] =
|
|
|
|
result = r.toBytes
|
|
|
|
if result.len != 32:
|
|
|
|
raise newException(RlpTypeMismatch,
|
|
|
|
"RLP expected to be a Keccak hash value, but has an incorrect length")
|
|
|
|
|
|
|
|
template getNode(elem: untyped): untyped =
|
|
|
|
if elem.isList: @(elem.rawData)
|
2020-05-05 06:50:31 +00:00
|
|
|
else: @(get(wb.db, elem.expectHash))
|
2020-04-21 10:20:39 +00:00
|
|
|
|
2020-04-22 04:21:39 +00:00
|
|
|
proc rlpListToBitmask(r: var Rlp): uint =
|
|
|
|
var i = 0
|
|
|
|
for branch in r:
|
|
|
|
if not branch.isEmpty:
|
|
|
|
result.setBranchMaskBit(i)
|
|
|
|
inc i
|
|
|
|
r.position = 0
|
|
|
|
|
2020-05-05 13:27:12 +00:00
|
|
|
template write(wb: var WitnessBuilder, x: untyped) =
|
|
|
|
wb.output.append(x)
|
|
|
|
|
|
|
|
proc writeU32Impl(wb: var WitnessBuilder, x: uint32) =
|
|
|
|
wb.write(toBytesBE(x))
|
|
|
|
|
|
|
|
template writeU32(wb: var WitnessBuilder, x: untyped) =
|
|
|
|
wb.writeU32Impl(uint32(x))
|
|
|
|
|
|
|
|
template writeByte(wb: var WitnessBuilder, x: untyped) =
|
|
|
|
wb.write(byte(x))
|
2020-04-24 08:56:03 +00:00
|
|
|
|
2020-04-28 02:30:10 +00:00
|
|
|
proc writeNibbles(wb: var WitnessBuilder; n: NibblesSeq, withLen: bool = true) =
|
2020-04-28 14:06:37 +00:00
|
|
|
# convert the NibblesSeq into left aligned byte seq
|
|
|
|
# perhaps we can optimize it if the NibblesSeq already left aligned
|
2020-04-22 04:21:39 +00:00
|
|
|
let nibblesLen = n.len
|
|
|
|
let numBytes = nibblesLen div 2 + nibblesLen mod 2
|
|
|
|
var bytes: array[32, byte]
|
|
|
|
doAssert(nibblesLen >= 1)
|
|
|
|
doAssert(numBytes >= 0 and numBytes <= 64)
|
|
|
|
for pos in 0..<n.len:
|
|
|
|
if (pos and 1) != 0:
|
|
|
|
bytes[pos div 2] = bytes[pos div 2] or n[pos]
|
|
|
|
else:
|
|
|
|
bytes[pos div 2] = bytes[pos div 2] or (n[pos] shl 4)
|
|
|
|
|
2020-04-28 02:30:10 +00:00
|
|
|
if withLen:
|
|
|
|
# write nibblesLen
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(nibblesLen)
|
2020-04-22 04:21:39 +00:00
|
|
|
# write nibbles
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.write(bytes.toOpenArray(0, numBytes-1))
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
proc writeExtensionNode(wb: var WitnessBuilder, n: NibblesSeq, depth: int, node: openArray[byte]) =
|
2020-04-22 04:21:39 +00:00
|
|
|
# write type
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(ExtensionNodeType)
|
2020-04-22 04:21:39 +00:00
|
|
|
# write nibbles
|
|
|
|
wb.writeNibbles(n)
|
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
when defined(debugDepth):
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(depth)
|
2020-04-22 11:04:19 +00:00
|
|
|
|
|
|
|
when defined(debugHash):
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.write(keccak(node).data)
|
2020-04-22 11:04:19 +00:00
|
|
|
|
|
|
|
proc writeBranchNode(wb: var WitnessBuilder, mask: uint, depth: int, node: openArray[byte]) =
|
2020-04-22 04:21:39 +00:00
|
|
|
# write type
|
2020-04-24 09:04:43 +00:00
|
|
|
# branch node 17th elem should always empty
|
|
|
|
doAssert mask.branchMaskBitIsSet(16) == false
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(BranchNodeType)
|
2020-04-22 04:21:39 +00:00
|
|
|
# write branch mask
|
2020-04-28 14:06:37 +00:00
|
|
|
# countOnes(branch mask) >= 2 and <= 16
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte((mask shr 8) and 0xFF)
|
|
|
|
wb.writeByte(mask and 0xFF)
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
when defined(debugDepth):
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(depth)
|
2020-04-22 11:04:19 +00:00
|
|
|
|
|
|
|
when defined(debugHash):
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.write(keccak(node).data)
|
2020-04-22 11:04:19 +00:00
|
|
|
|
2020-04-28 07:27:55 +00:00
|
|
|
proc writeHashNode(wb: var WitnessBuilder, node: openArray[byte]) =
|
|
|
|
# write type
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(HashNodeType)
|
|
|
|
wb.write(node)
|
2020-04-28 07:27:55 +00:00
|
|
|
|
2020-05-05 13:07:38 +00:00
|
|
|
proc getBranchRecurse(wb: var WitnessBuilder, z: var StackElem) {.raises: [ContractCodeError, IOError, Defect, CatchableError, Exception].}
|
2020-05-05 06:50:31 +00:00
|
|
|
|
2020-05-05 08:21:39 +00:00
|
|
|
proc writeAccountNode(wb: var WitnessBuilder, kd: KeyData, acc: Account, nibbles: NibblesSeq,
|
|
|
|
node: openArray[byte], depth: int) {.raises: [ContractCodeError, IOError, Defect, CatchableError, Exception].} =
|
2020-04-28 14:06:37 +00:00
|
|
|
|
2020-04-22 04:21:39 +00:00
|
|
|
# write type
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(AccountNodeType)
|
2020-04-28 09:58:16 +00:00
|
|
|
|
|
|
|
when defined(debugHash):
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeU32(node.len)
|
|
|
|
wb.write(node)
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
when defined(debugDepth):
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(depth)
|
2020-04-22 11:04:19 +00:00
|
|
|
|
2020-04-28 02:30:10 +00:00
|
|
|
doAssert(nibbles.len == 64 - depth)
|
2020-05-05 08:21:39 +00:00
|
|
|
var accountType = if acc.codeHash == blankStringHash and acc.storageRoot == emptyRlpHash: SimpleAccountType
|
2020-04-28 02:30:10 +00:00
|
|
|
else: ExtendedAccountType
|
|
|
|
|
2020-05-05 08:21:39 +00:00
|
|
|
if not kd.codeTouched:
|
|
|
|
accountType = CodeUntouched
|
|
|
|
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(accountType)
|
2020-04-28 07:27:55 +00:00
|
|
|
wb.writeNibbles(nibbles, false)
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.write(kd.address)
|
|
|
|
wb.write(acc.balance.toBytesBE)
|
|
|
|
wb.write(acc.nonce.u256.toBytesBE)
|
2020-04-28 07:27:55 +00:00
|
|
|
|
2020-05-05 08:21:39 +00:00
|
|
|
if accountType != SimpleAccountType:
|
|
|
|
if not kd.codeTouched:
|
|
|
|
wb.writeHashNode(acc.codeHash.data)
|
2020-05-05 13:07:38 +00:00
|
|
|
let code = get(wb.db, contractHashKey(acc.codeHash).toOpenArray)
|
|
|
|
if wfEIP170 in wb.flags and code.len > EIP170_CODE_SIZE_LIMIT:
|
|
|
|
raise newException(ContractCodeError, "code len exceed EIP170 code size limit")
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeU32(code.len)
|
2020-05-05 13:07:38 +00:00
|
|
|
# no code here
|
2020-05-05 08:21:39 +00:00
|
|
|
elif acc.codeHash != blankStringHash:
|
2020-04-29 05:12:24 +00:00
|
|
|
let code = get(wb.db, contractHashKey(acc.codeHash).toOpenArray)
|
2020-04-29 05:46:50 +00:00
|
|
|
if wfEIP170 in wb.flags and code.len > EIP170_CODE_SIZE_LIMIT:
|
|
|
|
raise newException(ContractCodeError, "code len exceed EIP170 code size limit")
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeU32(code.len)
|
|
|
|
wb.write(code)
|
2020-04-28 07:27:55 +00:00
|
|
|
else:
|
|
|
|
wb.writeU32(0'u32)
|
2020-04-28 02:30:10 +00:00
|
|
|
|
2020-05-05 08:21:39 +00:00
|
|
|
if kd.storageKeys.isNil:
|
2020-05-05 06:50:31 +00:00
|
|
|
# we have storage but not touched by EVM
|
|
|
|
wb.writeHashNode(acc.storageRoot.data)
|
|
|
|
elif acc.storageRoot != emptyRlpHash:
|
|
|
|
var zz = StackElem(
|
|
|
|
node: wb.db.get(acc.storageRoot.data),
|
2020-05-05 08:21:39 +00:00
|
|
|
parentGroup: kd.storageKeys.initGroup(),
|
|
|
|
keys: kd.storageKeys,
|
2020-05-05 06:50:31 +00:00
|
|
|
depth: 0, # reset depth
|
|
|
|
storageMode: true # switch to storage mode
|
|
|
|
)
|
2020-05-05 13:07:38 +00:00
|
|
|
getBranchRecurse(wb, zz)
|
2020-04-28 07:27:55 +00:00
|
|
|
else:
|
|
|
|
wb.writeHashNode(emptyRlpHash.data)
|
2020-04-28 02:30:10 +00:00
|
|
|
|
|
|
|
#0x00 pathnibbles:<Nibbles(64-d)> address:<Address> balance:<Bytes32> nonce:<Bytes32>
|
|
|
|
#0x01 pathnibbles:<Nibbles(64-d)> address:<Address> balance:<Bytes32> nonce:<Bytes32> bytecode:<Bytecode> storage:<Tree_Node(0,1)>
|
2020-05-05 13:07:38 +00:00
|
|
|
#0x02 pathnibbles:<Nibbles(64-d)> address:<Address> balance:<Bytes32> nonce:<Bytes32> codehash:<Bytes32> codesize:<U32> storage:<Account_Storage_Tree_Node(0)>
|
2020-04-28 02:30:10 +00:00
|
|
|
|
2020-05-05 06:50:31 +00:00
|
|
|
proc writeAccountStorageLeafNode(wb: var WitnessBuilder, key: openArray[byte], val: UInt256, nibbles: NibblesSeq, node: openArray[byte], depth: int) =
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(StorageLeafNodeType)
|
2020-04-28 14:06:37 +00:00
|
|
|
|
2020-05-05 06:50:31 +00:00
|
|
|
when defined(debugHash):
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeU32(node.len)
|
|
|
|
wb.write(node)
|
2020-04-28 14:06:37 +00:00
|
|
|
|
2020-05-05 06:50:31 +00:00
|
|
|
when defined(debugDepth):
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(depth)
|
2020-04-28 14:06:37 +00:00
|
|
|
|
2020-05-05 06:50:31 +00:00
|
|
|
doAssert(nibbles.len == 64 - depth)
|
|
|
|
wb.writeNibbles(nibbles, false)
|
2020-04-22 11:04:19 +00:00
|
|
|
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.write(key)
|
|
|
|
wb.write(val.toBytesBE)
|
2020-04-22 11:04:19 +00:00
|
|
|
|
2020-05-05 06:50:31 +00:00
|
|
|
#<Storage_Leaf_Node(d<65)> := pathnibbles:<Nibbles(64-d))> key:<Bytes32> val:<Bytes32>
|
2020-04-22 11:04:19 +00:00
|
|
|
|
2020-05-05 13:07:38 +00:00
|
|
|
proc getBranchRecurse(wb: var WitnessBuilder, z: var StackElem) =
|
2020-05-05 06:50:31 +00:00
|
|
|
if z.node.len == 0: return
|
|
|
|
var nodeRlp = rlpFromBytes z.node
|
2020-04-21 10:20:39 +00:00
|
|
|
|
|
|
|
case nodeRlp.listLen
|
|
|
|
of 2:
|
|
|
|
let (isLeaf, k) = nodeRlp.extensionNodeKey
|
2020-05-05 06:50:31 +00:00
|
|
|
var match = false
|
2020-05-05 13:07:38 +00:00
|
|
|
# only zero or one group can match the path
|
|
|
|
# but if there is a match, it can be in any position
|
|
|
|
# 1st, 2nd, or max 3rd position
|
|
|
|
# recursion will go deeper depend on the common-prefix length nibbles
|
2020-05-05 06:50:31 +00:00
|
|
|
for mg in groups(z.keys, z.depth, k, z.parentGroup):
|
2020-05-05 13:07:38 +00:00
|
|
|
if not mg.match: continue
|
|
|
|
doAssert(match == false) # should be only one match
|
|
|
|
match = true
|
|
|
|
let value = nodeRlp.listElem(1)
|
|
|
|
if not isLeaf:
|
|
|
|
# ExtensionNodeType
|
|
|
|
writeExtensionNode(wb, k, z.depth, z.node)
|
|
|
|
var zz = StackElem(
|
|
|
|
node: value.getNode,
|
|
|
|
parentGroup: mg.group,
|
|
|
|
keys: z.keys,
|
|
|
|
depth: z.depth + k.len,
|
|
|
|
storageMode: z.storageMode
|
|
|
|
)
|
|
|
|
getBranchRecurse(wb, zz)
|
|
|
|
else:
|
|
|
|
# this should be only one match
|
|
|
|
# if there is more than one match
|
2020-05-06 03:36:00 +00:00
|
|
|
# it means we encounter an invalid address
|
2020-05-05 13:07:38 +00:00
|
|
|
for kd in keyDatas(z.keys, mg.group):
|
2020-05-06 03:36:00 +00:00
|
|
|
if not match(kd, k, z.depth): continue # skip the invalid address
|
2020-05-05 13:07:38 +00:00
|
|
|
kd.visited = true
|
|
|
|
if z.storageMode:
|
|
|
|
doAssert(kd.storageMode)
|
|
|
|
writeAccountStorageLeafNode(wb, kd.storageSlot, value.toBytes.decode(UInt256), k, z.node, z.depth)
|
|
|
|
else:
|
|
|
|
doAssert(not kd.storageMode)
|
|
|
|
writeAccountNode(wb, kd, value.toBytes.decode(Account), k, z.node, z.depth)
|
2020-05-05 06:50:31 +00:00
|
|
|
if not match:
|
|
|
|
writeHashNode(wb, keccak(z.node).data)
|
2020-04-21 10:20:39 +00:00
|
|
|
of 17:
|
2020-04-22 04:21:39 +00:00
|
|
|
let branchMask = rlpListToBitmask(nodeRlp)
|
2020-05-05 06:50:31 +00:00
|
|
|
writeBranchNode(wb, branchMask, z.depth, z.node)
|
|
|
|
let path = groups(z.keys, z.parentGroup, z.depth)
|
2020-04-22 11:04:19 +00:00
|
|
|
|
2020-05-05 13:07:38 +00:00
|
|
|
# if there is a match in any branch elem
|
|
|
|
# 1st to 16th, the recursion will go deeper
|
|
|
|
# by one nibble
|
2020-05-05 06:50:31 +00:00
|
|
|
let notLeaf = z.depth != 63 # path.len == 0
|
2020-04-22 04:28:16 +00:00
|
|
|
for i in 0..<16:
|
2020-05-05 13:07:38 +00:00
|
|
|
if not branchMask.branchMaskBitIsSet(i): continue
|
|
|
|
var branch = nodeRlp.listElem(i)
|
|
|
|
if notLeaf and branchMaskBitIsSet(path.mask, i):
|
|
|
|
var zz = StackElem(
|
|
|
|
node: branch.getNode,
|
|
|
|
parentGroup: path.groups[i],
|
|
|
|
keys: z.keys,
|
|
|
|
depth: z.depth + 1,
|
|
|
|
storageMode: z.storageMode
|
|
|
|
)
|
|
|
|
getBranchRecurse(wb, zz)
|
|
|
|
else:
|
|
|
|
if branch.isList:
|
|
|
|
doAssert(false, "Short node should not exist in block witness")
|
2020-04-22 04:28:16 +00:00
|
|
|
else:
|
2020-05-05 13:07:38 +00:00
|
|
|
# this is a potential branch for multiproof
|
|
|
|
writeHashNode(wb, branch.expectHash)
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-24 09:04:43 +00:00
|
|
|
# 17th elem should always empty
|
|
|
|
doAssert branchMask.branchMaskBitIsSet(16) == false
|
2020-04-21 10:20:39 +00:00
|
|
|
else:
|
|
|
|
raise newException(CorruptedTrieDatabase,
|
|
|
|
"HexaryTrie node with an unexpected number of children")
|
|
|
|
|
2020-05-05 13:07:38 +00:00
|
|
|
proc buildWitness*(wb: var WitnessBuilder, keys: MultikeysRef): seq[byte]
|
2020-05-05 06:50:31 +00:00
|
|
|
{.raises: [ContractCodeError, IOError, Defect, CatchableError, Exception].} =
|
2020-05-03 01:47:14 +00:00
|
|
|
|
2020-04-29 08:29:25 +00:00
|
|
|
# witness version
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(BlockWitnessVersion)
|
2020-04-29 08:29:25 +00:00
|
|
|
|
|
|
|
# one or more trees
|
|
|
|
|
|
|
|
# we only output one tree
|
2020-05-05 13:27:12 +00:00
|
|
|
wb.writeByte(MetadataNothing)
|
2020-05-05 06:50:31 +00:00
|
|
|
|
|
|
|
var z = StackElem(
|
|
|
|
node: @(wb.db.get(wb.root.data)),
|
|
|
|
parentGroup: keys.initGroup(),
|
|
|
|
keys: keys,
|
|
|
|
depth: 0,
|
|
|
|
storageMode: false
|
|
|
|
)
|
2020-05-05 13:07:38 +00:00
|
|
|
getBranchRecurse(wb, z)
|
2020-04-29 08:29:25 +00:00
|
|
|
|
|
|
|
# result
|
2020-04-24 11:55:58 +00:00
|
|
|
result = wb.output.getOutput(seq[byte])
|