From 90c2ede43b5c7cf782ac29accaa14e4a6f2544b1 Mon Sep 17 00:00:00 2001 From: andri lim Date: Wed, 29 Apr 2020 12:46:50 +0700 Subject: [PATCH] deals with EIP170 error --- stateless/test_block_witness.nim | 47 ++++++++++++-------------------- stateless/test_witness_keys.nim | 8 +++--- stateless/tree_from_witness.nim | 32 +++++++++++++++++----- stateless/witness_from_tree.nim | 8 ++++-- stateless/witness_types.nim | 8 ++++++ 5 files changed, 59 insertions(+), 44 deletions(-) diff --git a/stateless/test_block_witness.nim b/stateless/test_block_witness.nim index 9830120d3..4ef23a431 100644 --- a/stateless/test_block_witness.nim +++ b/stateless/test_block_witness.nim @@ -3,7 +3,7 @@ import eth/[common, rlp], eth/trie/[hexary, db, trie_defs], stew/byteutils, faststreams/input_stream, ../tests/[test_helpers, test_config], - ../nimbus/db/accounts_cache, + ../nimbus/db/accounts_cache, ./witness_types, ../stateless/[witness_from_tree, tree_from_witness] type @@ -26,34 +26,24 @@ proc isValidBranch(branch: openArray[seq[byte]], rootHash: KeccakHash, key, valu proc testGetBranch(tester: Tester, rootHash: KeccakHash, testStatusIMPL: var TestStatus) = var trie = initHexaryTrie(tester.memdb, rootHash) + let flags = {wfEIP170} - #try: - for address in tester.address: - var wb = initWitnessBuilder(tester.memdb, rootHash) - var witness = wb.buildWitness(address) + try: + for address in tester.address: + var wb = initWitnessBuilder(tester.memdb, rootHash, flags) + var witness = wb.buildWitness(address) - var db = newMemoryDB() - when defined(useInputStream): - var input = memoryInput(witness) - var tb = initTreeBuilder(input, db) - else: - var tb = initTreeBuilder(witness, db) - - var root = tb.treeNode() - check root.data == rootHash.data + var db = newMemoryDB() + when defined(useInputStream): + var input = memoryInput(witness) + var tb = initTreeBuilder(input, db, flags) + else: + var tb = initTreeBuilder(witness, db, flags) - #echo "ROOT: ", root.data.toHex - #echo "rootHash: ", rootHash.data.toHex - - #var stackBranch = wb.getBranchStack(address) - #check recurseBranch == stackBranch - # - #var branch = wb.getBranch(address) - #let account = trie.get(address) - #check isValidBranch(branch, trie.rootHash, address, account) - #except: - #debugEcho "MSG: ", getCurrentExceptionMsg() - #quit(1) + var root = tb.treeNode() + check root.data == rootHash.data + except ContractCodeError as e: + debugEcho "CONTRACT CODE ERROR: ", e.msg func parseHash256(n: JsonNode, name: string): Hash256 = hexToByteArray(n[name].getStr(), result.data) @@ -119,10 +109,7 @@ proc blockWitnessMain*(debugMode = false) = let path = "tests" / "fixtures" / folder let n = json.parseFile(path / config.testSubject) var testStatusIMPL: TestStatus - if config.legacy: - testFixtureGST(n, testStatusIMPL) - else: - testFixtureBC(n, testStatusIMPL) + testFixtureGST(n, testStatusIMPL) when isMainModule: var message: string diff --git a/stateless/test_witness_keys.nim b/stateless/test_witness_keys.nim index abdc78222..16aec21d4 100644 --- a/stateless/test_witness_keys.nim +++ b/stateless/test_witness_keys.nim @@ -3,7 +3,7 @@ import eth/[common, rlp], eth/trie/[hexary, db, trie_defs], faststreams/input_stream, nimcrypto/[utils, sysrand], ../stateless/[witness_from_tree, tree_from_witness], - ../nimbus/db/storage_types + ../nimbus/db/storage_types, ./witness_types type DB = TrieDatabaseRef @@ -59,14 +59,14 @@ proc runTest(numPairs: int) = let rootHash = trie.rootHash - var wb = initWitnessBuilder(memDB, rootHash) + var wb = initWitnessBuilder(memDB, rootHash, {wfEIP170}) var witness = wb.buildWitness(addrs[0]) var db = newMemoryDB() when defined(useInputStream): var input = memoryInput(witness) - var tb = initTreeBuilder(input, db) + var tb = initTreeBuilder(input, db, {wfEIP170}) else: - var tb = initTreeBuilder(witness, db) + var tb = initTreeBuilder(witness, db, {wfEIP170}) var root = tb.treeNode() debugEcho "root: ", root.data.toHex debugEcho "rootHash: ", rootHash.data.toHex diff --git a/stateless/tree_from_witness.nim b/stateless/tree_from_witness.nim index e054cd7c8..bbc27eb37 100644 --- a/stateless/tree_from_witness.nim +++ b/stateless/tree_from_witness.nim @@ -18,26 +18,30 @@ type pos: int db: DB root: KeccakHash + flags: WitnessFlags # the InputStream still unstable # when using large dataset for testing # or run longer when defined(useInputStream): - proc initTreeBuilder*(input: InputStream, db: DB): TreeBuilder = + proc initTreeBuilder*(input: InputStream, db: DB, flags: WitnessFlags): TreeBuilder = result.input = input result.db = db result.root = emptyRlpHash + result.flags = flags - proc initTreeBuilder*(input: openArray[byte], db: DB): TreeBuilder = + proc initTreeBuilder*(input: openArray[byte], db: DB, flags: WitnessFlags): TreeBuilder = result.input = memoryInput(input) result.db = db result.root = emptyRlpHash + result.flags = flags else: - proc initTreeBuilder*(input: openArray[byte], db: DB): TreeBuilder = + proc initTreeBuilder*(input: openArray[byte], db: DB, flags: WitnessFlags): TreeBuilder = result.input = @input result.db = db result.root = emptyRlpHash + result.flags = flags func rootHash*(t: TreeBuilder): KeccakHash {.inline.} = t.root @@ -216,7 +220,8 @@ proc accountNode(t: var TreeBuilder, depth: int): NodeKey = when defined(debugHash): let len = t.readU32().int - let nodeKey = toNodeKey(t.read(len)) + let node = @(t.read(len)) + let nodeKey = toNodeKey(node) when defined(debugDepth): let readDepth = t.readByte.int @@ -241,8 +246,8 @@ proc accountNode(t: var TreeBuilder, depth: int): NodeKey = acc.storageRoot = emptyRlpHash else: let codeLen = t.readU32() - if codeLen > EIP170_CODE_SIZE_LIMIT: - raise newException(ValueError, "code len exceed EIP170 code size limit") + if wfEIP170 in t.flags and codeLen > EIP170_CODE_SIZE_LIMIT: + raise newException(ContractCodeError, "code len exceed EIP170 code size limit") acc.codeHash = t.writeCode(t.read(codeLen.int)) # switch to account storage parsing mode @@ -252,9 +257,22 @@ proc accountNode(t: var TreeBuilder, depth: int): NodeKey = acc.storageRoot.data = storageRoot.data r.append rlp.encode(acc) - result = toNodeKey(r.finish) + let noderes = r.finish + result = toNodeKey(noderes) when defined(debugHash): + if result != nodeKey: + debugEcho "result.usedBytes: ", result.usedBytes + debugEcho "nodeKey.usedBytes: ", nodeKey.usedBytes + var rlpa = rlpFromBytes(node) + var rlpb = rlpFromBytes(noderes) + debugEcho "Expected: ", inspect(rlpa) + debugEcho "Actual: ", inspect(rlpb) + var a = rlpa.listElem(1).toBytes.decode(Account) + var b = rlpb.listElem(1).toBytes.decode(Account) + debugEcho "Expected: ", a + debugEcho "Actual: ", b + doAssert(result == nodeKey, "account node parsing error") proc accountStorageLeafNode(t: var TreeBuilder, depth: int): NodeKey = diff --git a/stateless/witness_from_tree.nim b/stateless/witness_from_tree.nim index 5202372a3..637e927c0 100644 --- a/stateless/witness_from_tree.nim +++ b/stateless/witness_from_tree.nim @@ -13,11 +13,13 @@ type db*: DB root: KeccakHash output: OutputStream + flags: WitnessFlags -proc initWitnessBuilder*(db: DB, rootHash: KeccakHash = emptyRlpHash): WitnessBuilder = +proc initWitnessBuilder*(db: DB, rootHash: KeccakHash, flags: WitnessFlags = {}): WitnessBuilder = result.db = db result.root = rootHash result.output = memoryOutput().s + result.flags = flags template extensionNodeKey(r: Rlp): auto = hexPrefixDecode r.listElem(0).toBytes @@ -125,8 +127,8 @@ proc writeAccountNode(wb: var WitnessBuilder, acc: Account, nibbles: NibblesSeq, if accountType == ExtendedAccountType: if acc.codeHash != blankStringHash: let code = get(wb.db, contractHashKey(acc.codeHash).toOpenArray) - if code.len > EIP170_CODE_SIZE_LIMIT: - raise newException(ValueError, "code len exceed EIP170 code size limit") + if wfEIP170 in wb.flags and code.len > EIP170_CODE_SIZE_LIMIT: + raise newException(ContractCodeError, "code len exceed EIP170 code size limit") wb.writeU32(code.len.uint32) wb.output.append(code) else: diff --git a/stateless/witness_types.nim b/stateless/witness_types.nim index 4f8c824bb..5dbd6ee41 100644 --- a/stateless/witness_types.nim +++ b/stateless/witness_types.nim @@ -11,6 +11,14 @@ type SimpleAccountType ExtendedAccountType + WitnessFlag* = enum + wfNoFlag + wfEIP170 # fork >= Spurious Dragon + + WitnessFlags* = set[WitnessFlag] + + ContractCodeError* = object of ValueError + const StorageLeafNodeType* = AccountNodeType BlockWitnessVersion* = 0x01