2020-04-21 11:04:18 +00:00
|
|
|
import
|
2020-04-22 04:21:39 +00:00
|
|
|
faststreams/input_stream, eth/[common, rlp], stint, stew/endians2,
|
|
|
|
eth/trie/[db, trie_defs], nimcrypto/[keccak, hash],
|
2020-04-28 07:27:55 +00:00
|
|
|
./witness_types, stew/byteutils, ../nimbus/constants
|
2020-04-20 11:23:22 +00:00
|
|
|
|
|
|
|
type
|
2020-04-22 04:21:39 +00:00
|
|
|
DB = TrieDatabaseRef
|
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
NodeKey = object
|
|
|
|
usedBytes: int
|
|
|
|
data*: array[32, byte]
|
|
|
|
|
2020-04-20 11:23:22 +00:00
|
|
|
TreeBuilder = object
|
2020-04-28 04:59:58 +00:00
|
|
|
when defined(useInputStream):
|
|
|
|
input: InputStream
|
|
|
|
else:
|
|
|
|
input: seq[byte]
|
|
|
|
pos: int
|
2020-04-22 04:21:39 +00:00
|
|
|
db: DB
|
|
|
|
root: KeccakHash
|
|
|
|
|
2020-04-28 04:59:58 +00:00
|
|
|
# the InputStream still unstable
|
|
|
|
# when using large dataset for testing
|
|
|
|
# or run longer
|
|
|
|
|
|
|
|
when defined(useInputStream):
|
|
|
|
proc initTreeBuilder*(input: InputStream, db: DB): TreeBuilder =
|
|
|
|
result.input = input
|
|
|
|
result.db = db
|
|
|
|
result.root = emptyRlpHash
|
|
|
|
|
|
|
|
proc initTreeBuilder*(input: openArray[byte], db: DB): TreeBuilder =
|
|
|
|
result.input = memoryInput(input)
|
|
|
|
result.db = db
|
|
|
|
result.root = emptyRlpHash
|
|
|
|
else:
|
|
|
|
proc initTreeBuilder*(input: openArray[byte], db: DB): TreeBuilder =
|
|
|
|
result.input = @input
|
|
|
|
result.db = db
|
|
|
|
result.root = emptyRlpHash
|
2020-04-24 09:04:43 +00:00
|
|
|
|
2020-04-22 04:21:39 +00:00
|
|
|
func rootHash*(t: TreeBuilder): KeccakHash {.inline.} =
|
|
|
|
t.root
|
|
|
|
|
|
|
|
proc writeNode(t: var TreeBuilder, n: openArray[byte]): KeccakHash =
|
|
|
|
result = keccak(n)
|
|
|
|
t.db.put(result.data, n)
|
|
|
|
|
2020-04-28 04:59:58 +00:00
|
|
|
when defined(useInputStream):
|
|
|
|
template readByte(t: var TreeBuilder): byte =
|
|
|
|
t.input.read
|
|
|
|
|
|
|
|
template len(t: TreeBuilder): int =
|
|
|
|
t.input.len
|
|
|
|
|
|
|
|
template read(t: var TreeBuilder, len: int): auto =
|
|
|
|
t.input.read(len)
|
|
|
|
|
|
|
|
else:
|
|
|
|
template readByte(t: var TreeBuilder): byte =
|
|
|
|
let pos = t.pos
|
|
|
|
inc t.pos
|
|
|
|
t.input[pos]
|
2020-04-23 02:56:35 +00:00
|
|
|
|
2020-04-28 04:59:58 +00:00
|
|
|
template len(t: TreeBuilder): int =
|
|
|
|
t.input.len
|
2020-04-23 02:56:35 +00:00
|
|
|
|
2020-04-28 04:59:58 +00:00
|
|
|
template peek(t: TreeBuilder): byte =
|
|
|
|
t.input.peek
|
|
|
|
t.input[t.pos]
|
2020-04-23 02:56:35 +00:00
|
|
|
|
2020-04-28 04:59:58 +00:00
|
|
|
template read(t: var TreeBuilder, len: int): auto =
|
|
|
|
let pos = t.pos
|
|
|
|
inc(t.pos, len)
|
|
|
|
toOpenArray(t.input, pos, pos+len-1)
|
2020-04-23 02:56:35 +00:00
|
|
|
|
2020-04-24 08:56:03 +00:00
|
|
|
proc readU32(t: var TreeBuilder): uint32 =
|
|
|
|
result = fromBytesBE(uint32, t.read(4))
|
2020-04-20 11:23:22 +00:00
|
|
|
|
2020-04-22 04:21:39 +00:00
|
|
|
proc toAddress(r: var EthAddress, x: openArray[byte]) {.inline.} =
|
|
|
|
r[0..19] = x[0..19]
|
2020-04-20 11:23:22 +00:00
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
proc toKeccak(r: var NodeKey, x: openArray[byte]) {.inline.} =
|
2020-04-22 04:21:39 +00:00
|
|
|
r.data[0..31] = x[0..31]
|
2020-04-22 11:04:19 +00:00
|
|
|
r.usedBytes = 32
|
2020-04-20 11:23:22 +00:00
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
proc toKeccak(x: openArray[byte]): NodeKey {.inline.} =
|
2020-04-22 04:21:39 +00:00
|
|
|
result.data[0..31] = x[0..31]
|
2020-04-22 11:04:19 +00:00
|
|
|
result.usedBytes = 32
|
|
|
|
|
|
|
|
proc append(r: var RlpWriter, n: NodeKey) =
|
|
|
|
if n.usedBytes < 32:
|
|
|
|
r.append rlpFromBytes(n.data.toOpenArray(0, n.usedBytes-1))
|
|
|
|
else:
|
|
|
|
r.append n.data.toOpenArray(0, n.usedBytes-1)
|
|
|
|
|
|
|
|
proc toNodeKey(z: openArray[byte]): NodeKey =
|
|
|
|
if z.len < 32:
|
|
|
|
result.usedBytes = z.len
|
|
|
|
result.data[0..z.len-1] = z[0..z.len-1]
|
|
|
|
else:
|
|
|
|
result.data = keccak(z).data
|
|
|
|
result.usedBytes = 32
|
2020-04-20 11:23:22 +00:00
|
|
|
|
2020-04-28 07:27:55 +00:00
|
|
|
proc writeCode(t: var TreeBuilder, code: openArray[byte]): Hash256 =
|
|
|
|
result = keccak(code)
|
|
|
|
put(t.db, result.data, code)
|
|
|
|
|
2020-04-28 14:06:37 +00:00
|
|
|
proc branchNode(t: var TreeBuilder, depth: int, storageMode: bool): NodeKey
|
|
|
|
proc extensionNode(t: var TreeBuilder, depth: int, storageMode: bool): NodeKey
|
2020-04-22 11:04:19 +00:00
|
|
|
proc accountNode(t: var TreeBuilder, depth: int): NodeKey
|
|
|
|
proc accountStorageLeafNode(t: var TreeBuilder, depth: int): NodeKey
|
|
|
|
proc hashNode(t: var TreeBuilder): NodeKey
|
2020-04-20 11:23:22 +00:00
|
|
|
|
2020-04-28 14:06:37 +00:00
|
|
|
proc treeNode*(t: var TreeBuilder, depth: int = 0, storageMode = false): NodeKey =
|
2020-04-20 11:23:22 +00:00
|
|
|
assert(depth < 64)
|
|
|
|
let nodeType = TrieNodeType(t.readByte)
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-20 11:23:22 +00:00
|
|
|
case nodeType
|
2020-04-28 14:06:37 +00:00
|
|
|
of BranchNodeType: result = t.branchNode(depth, storageMode)
|
|
|
|
of ExtensionNodeType: result = t.extensionNode(depth, storageMode)
|
2020-04-20 11:23:22 +00:00
|
|
|
of AccountNodeType:
|
2020-04-28 14:06:37 +00:00
|
|
|
if storageMode:
|
2020-04-20 11:23:22 +00:00
|
|
|
# parse account storage leaf node
|
2020-04-22 04:21:39 +00:00
|
|
|
result = t.accountStorageLeafNode(depth)
|
2020-04-20 11:23:22 +00:00
|
|
|
else:
|
2020-04-22 04:21:39 +00:00
|
|
|
result = t.accountNode(depth)
|
|
|
|
of HashNodeType: result = t.hashNode()
|
2020-04-20 11:23:22 +00:00
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
if depth == 0 and result.usedBytes < 32:
|
|
|
|
result.data = keccak(result.data.toOpenArray(0, result.usedBytes-1)).data
|
|
|
|
result.usedBytes = 32
|
|
|
|
|
2020-04-28 14:06:37 +00:00
|
|
|
proc branchNode(t: var TreeBuilder, depth: int, storageMode: bool): NodeKey =
|
2020-04-20 11:23:22 +00:00
|
|
|
assert(depth < 64)
|
|
|
|
let mask = constructBranchMask(t.readByte, t.readByte)
|
2020-04-22 11:04:19 +00:00
|
|
|
|
|
|
|
when defined(debugDepth):
|
|
|
|
let readDepth = t.readByte.int
|
|
|
|
doAssert(readDepth == depth, "branchNode " & $readDepth & " vs. " & $depth)
|
|
|
|
|
|
|
|
when defined(debugHash):
|
|
|
|
let hash = toKeccak(t.read(32))
|
|
|
|
|
2020-04-22 04:21:39 +00:00
|
|
|
var r = initRlpList(17)
|
|
|
|
|
2020-04-20 11:23:22 +00:00
|
|
|
for i in 0 ..< 16:
|
|
|
|
if mask.branchMaskBitIsSet(i):
|
2020-04-28 14:06:37 +00:00
|
|
|
r.append t.treeNode(depth+1, storageMode)
|
2020-04-22 04:21:39 +00:00
|
|
|
else:
|
|
|
|
r.append ""
|
2020-04-20 11:23:22 +00:00
|
|
|
|
2020-04-24 09:04:43 +00:00
|
|
|
# 17th elem should always empty
|
|
|
|
r.append ""
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
result = toNodeKey(r.finish)
|
|
|
|
|
|
|
|
when defined(debugHash):
|
|
|
|
if result != hash:
|
|
|
|
debugEcho "DEPTH: ", depth
|
|
|
|
debugEcho "result: ", result.data.toHex, " vs. ", hash.data.toHex
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-28 07:27:55 +00:00
|
|
|
func hexPrefix(r: var RlpWriter, x: openArray[byte], nibblesLen: int, isLeaf: static[bool] = false) =
|
2020-04-22 04:43:37 +00:00
|
|
|
var bytes: array[33, byte]
|
2020-04-28 07:27:55 +00:00
|
|
|
if (nibblesLen mod 2) == 0: # even
|
|
|
|
when isLeaf:
|
|
|
|
bytes[0] = 0b0010_0000.byte
|
|
|
|
else:
|
|
|
|
bytes[0] = 0.byte
|
2020-04-22 04:43:37 +00:00
|
|
|
var i = 1
|
2020-04-22 04:21:39 +00:00
|
|
|
for y in x:
|
2020-04-22 04:43:37 +00:00
|
|
|
bytes[i] = y
|
|
|
|
inc i
|
2020-04-28 07:27:55 +00:00
|
|
|
else: # odd
|
|
|
|
when isLeaf:
|
|
|
|
bytes[0] = 0b0011_0000.byte or (x[0] shr 4)
|
|
|
|
else:
|
|
|
|
bytes[0] = 0b0001_0000.byte or (x[0] shr 4)
|
2020-04-22 04:21:39 +00:00
|
|
|
var last = nibblesLen div 2
|
|
|
|
for i in 1..last:
|
2020-04-22 04:43:37 +00:00
|
|
|
bytes[i] = (x[i-1] shl 4) or (x[i] shr 4)
|
2020-04-22 11:04:19 +00:00
|
|
|
|
2020-04-22 04:43:37 +00:00
|
|
|
r.append toOpenArray(bytes, 0, nibblesLen div 2)
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-28 14:06:37 +00:00
|
|
|
proc extensionNode(t: var TreeBuilder, depth: int, storageMode: bool): NodeKey =
|
2020-04-20 11:23:22 +00:00
|
|
|
assert(depth < 63)
|
|
|
|
let nibblesLen = int(t.readByte)
|
|
|
|
assert(nibblesLen < 65)
|
2020-04-22 04:21:39 +00:00
|
|
|
var r = initRlpList(2)
|
2020-04-22 04:43:37 +00:00
|
|
|
r.hexPrefix(t.read(nibblesLen div 2 + nibblesLen mod 2), nibblesLen)
|
2020-04-20 11:23:22 +00:00
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
when defined(debugDepth):
|
|
|
|
let readDepth = t.readByte.int
|
|
|
|
doAssert(readDepth == depth, "extensionNode " & $readDepth & " vs. " & $depth)
|
|
|
|
|
|
|
|
when defined(debugHash):
|
|
|
|
let hash = toKeccak(t.read(32))
|
|
|
|
|
2020-04-20 11:23:22 +00:00
|
|
|
assert(depth + nibblesLen < 65)
|
|
|
|
let nodeType = TrieNodeType(t.readByte)
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-20 11:23:22 +00:00
|
|
|
case nodeType
|
2020-04-28 14:06:37 +00:00
|
|
|
of BranchNodeType: r.append t.branchNode(depth + nibblesLen, storageMode)
|
2020-04-22 04:21:39 +00:00
|
|
|
of HashNodeType: r.append t.hashNode()
|
2020-04-20 11:23:22 +00:00
|
|
|
else: raise newException(ValueError, "wrong type during parsing child of extension node")
|
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
result = toNodeKey(r.finish)
|
|
|
|
|
|
|
|
when defined(debugHash):
|
|
|
|
if result != hash:
|
|
|
|
debugEcho "DEPTH: ", depth
|
|
|
|
doAssert(result == hash, "EXT HASH DIFF " & result.data.toHex & " vs. " & hash.data.toHex)
|
2020-04-20 11:23:22 +00:00
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
proc accountNode(t: var TreeBuilder, depth: int): NodeKey =
|
2020-04-20 11:23:22 +00:00
|
|
|
assert(depth < 65)
|
2020-04-28 09:58:16 +00:00
|
|
|
|
|
|
|
when defined(debugHash):
|
|
|
|
let len = t.readU32().int
|
|
|
|
let nodeKey = toNodeKey(t.read(len))
|
2020-04-22 11:04:19 +00:00
|
|
|
|
|
|
|
when defined(debugDepth):
|
|
|
|
let readDepth = t.readByte.int
|
|
|
|
doAssert(readDepth == depth, "accountNode " & $readDepth & " vs. " & $depth)
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-28 07:27:55 +00:00
|
|
|
let accountType = AccountType(t.readByte)
|
2020-04-20 11:23:22 +00:00
|
|
|
let nibblesLen = 64 - depth
|
2020-04-28 07:27:55 +00:00
|
|
|
var r = initRlpList(2)
|
|
|
|
r.hexPrefix(t.read(nibblesLen div 2 + nibblesLen mod 2), nibblesLen, true)
|
|
|
|
|
2020-04-28 14:06:37 +00:00
|
|
|
# TODO: parse address
|
|
|
|
# let address = toAddress(t.read(20))
|
|
|
|
|
2020-04-28 07:27:55 +00:00
|
|
|
var acc = Account(
|
|
|
|
balance: UInt256.fromBytesBE(t.read(32), false),
|
|
|
|
# TODO: why nonce must be 32 bytes, isn't 64 bit uint enough?
|
|
|
|
nonce: UInt256.fromBytesBE(t.read(32), false).truncate(AccountNonce)
|
|
|
|
)
|
|
|
|
|
|
|
|
if accountType == SimpleAccountType:
|
|
|
|
acc.codeHash = blankStringHash
|
|
|
|
acc.storageRoot = emptyRlpHash
|
|
|
|
else:
|
2020-04-20 11:23:22 +00:00
|
|
|
let codeLen = t.readU32()
|
2020-04-28 07:27:55 +00:00
|
|
|
if codeLen > EIP170_CODE_SIZE_LIMIT:
|
|
|
|
raise newException(ValueError, "code len exceed EIP170 code size limit")
|
|
|
|
acc.codeHash = t.writeCode(t.read(codeLen.int))
|
2020-04-28 14:06:37 +00:00
|
|
|
|
2020-04-20 11:23:22 +00:00
|
|
|
# switch to account storage parsing mode
|
|
|
|
# and reset the depth
|
2020-04-28 14:06:37 +00:00
|
|
|
let storageRoot = t.treeNode(0, storageMode = true)
|
2020-04-28 07:27:55 +00:00
|
|
|
doAssert(storageRoot.usedBytes == 32)
|
|
|
|
acc.storageRoot.data = storageRoot.data
|
2020-04-28 14:06:37 +00:00
|
|
|
|
2020-04-28 09:58:16 +00:00
|
|
|
r.append rlp.encode(acc)
|
2020-04-28 14:06:37 +00:00
|
|
|
result = toNodeKey(r.finish)
|
|
|
|
|
2020-04-28 09:58:16 +00:00
|
|
|
when defined(debugHash):
|
|
|
|
doAssert(result == nodeKey, "account node parsing error")
|
2020-04-20 11:23:22 +00:00
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
proc accountStorageLeafNode(t: var TreeBuilder, depth: int): NodeKey =
|
2020-04-20 11:23:22 +00:00
|
|
|
assert(depth < 65)
|
|
|
|
let nibblesLen = 64 - depth
|
2020-04-28 14:06:37 +00:00
|
|
|
var r = initRlpList(2)
|
|
|
|
r.hexPrefix(t.read(nibblesLen div 2 + nibblesLen mod 2), nibblesLen, true)
|
|
|
|
# TODO: parse key
|
|
|
|
# let key = @(t.read(32))
|
|
|
|
# UInt256 -> BytesBE -> keccak
|
|
|
|
let val = UInt256.fromBytesBE(t.read(32))
|
|
|
|
r.append rlp.encode(val)
|
|
|
|
result = toNodeKey(r.finish)
|
2020-04-21 09:13:43 +00:00
|
|
|
|
2020-04-22 11:04:19 +00:00
|
|
|
proc hashNode(t: var TreeBuilder): NodeKey =
|
2020-04-22 04:21:39 +00:00
|
|
|
result.toKeccak(t.read(32))
|