From 692bed176ea205224346fc73b0f4d0a702aba5e9 Mon Sep 17 00:00:00 2001 From: andri lim Date: Tue, 28 Apr 2020 09:30:10 +0700 Subject: [PATCH] allow test_block_witness to run single test --- stateless/test_block_witness.nim | 46 ++++++++++++++++++++++++++------ stateless/test_witness_keys.nim | 19 ++++++++++++- stateless/witness_from_tree.nim | 37 +++++++++++++++++++------ 3 files changed, 85 insertions(+), 17 deletions(-) diff --git a/stateless/test_block_witness.nim b/stateless/test_block_witness.nim index 2c0047150..9e8431a4b 100644 --- a/stateless/test_block_witness.nim +++ b/stateless/test_block_witness.nim @@ -2,7 +2,7 @@ import unittest2, os, json, strutils, eth/[common, rlp], eth/trie/[hexary, db, trie_defs], stew/byteutils, faststreams/input_stream, - ../tests/test_helpers, + ../tests/[test_helpers, test_config], ../nimbus/db/accounts_cache, ../stateless/[witness_from_tree, tree_from_witness] @@ -31,16 +31,16 @@ proc testGetBranch(tester: Tester, rootHash: KeccakHash, testStatusIMPL: var Tes for address in tester.address: var wb = initWitnessBuilder(tester.memdb, rootHash) var witness = wb.getBranchRecurse(address) - + var db = newMemoryDB() var input = memoryInput(witness) var tb = initTreeBuilder(input, db) var root = tb.treeNode() check root.data == rootHash.data - + #echo "ROOT: ", root.data.toHex #echo "rootHash: ", rootHash.data.toHex - + #var stackBranch = wb.getBranchStack(address) #check recurseBranch == stackBranch # @@ -97,8 +97,38 @@ proc testFixtureGST(node: JsonNode, testStatusIMPL: var TestStatus) = fixture["pre"].testBlockWitness(emptyRlpHash, testStatusIMPL) -#suite "Block Witness": - #jsonTest("newBlockChainTests", "witnessBuilderBC", testFixtureBC) +proc blockWitnessMain*(debugMode = false) = + if paramCount() == 0 or not debugMode: + # run all test fixtures + suite "Block Witness": + jsonTest("newBlockChainTests", "witnessBuilderBC", testFixtureBC) + suite "Block Witness": + jsonTest("GeneralStateTests", "witnessBuilderGST", testFixtureGST) + else: + # execute single test in debug mode + let config = getConfiguration() + if config.testSubject.len == 0: + echo "missing test subject" + quit(QuitFailure) -suite "Block Witness": - jsonTest("GeneralStateTests", "witnessBuilderGST", testFixtureGST) + let folder = if config.legacy: "GeneralStateTests" else: "newGeneralStateTests" + 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) + +when isMainModule: + var message: string + + ## Processing command line arguments + if processArguments(message) != Success: + echo message + quit(QuitFailure) + else: + if len(message) > 0: + echo message + quit(QuitSuccess) + blockWitnessMain(true) diff --git a/stateless/test_witness_keys.nim b/stateless/test_witness_keys.nim index b83e14266..e3c201c3f 100644 --- a/stateless/test_witness_keys.nim +++ b/stateless/test_witness_keys.nim @@ -1,9 +1,26 @@ import randutils, stew/byteutils, random, eth/[common, rlp], eth/trie/[hexary, db], - faststreams/input_stream, + faststreams/input_stream, nimcrypto/[utils, sysrand], ../stateless/[witness_from_tree, tree_from_witness] +proc randU256(): UInt256 = + var bytes: array[32, byte] + discard randomBytes(bytes[0].addr, sizeof(result)) + result = UInt256.fromBytesBE(bytes) + +proc randNonce(): AccountNonce = + discard randomBytes(result.addr, sizeof(result)) + +proc randHash(): Hash256 = + discard randomBytes(result.data[0].addr, sizeof(result)) + +proc randAccount(): Account = + result.nonce = randNonce() + result.balance = randU256() + result.codeHash = randHash() + result.storageRoot = randHash() + proc runTest(keyBytes: int, valBytes: int, numPairs: int) = var memDB = newMemoryDB() var trie = initHexaryTrie(memDB) diff --git a/stateless/witness_from_tree.nim b/stateless/witness_from_tree.nim index d9bf1d9c1..3b93abf30 100644 --- a/stateless/witness_from_tree.nim +++ b/stateless/witness_from_tree.nim @@ -1,9 +1,9 @@ import stew/[byteutils, endians2], - nimcrypto/[keccak, hash], eth/rlp, + nimcrypto/[keccak, hash], eth/[common, rlp], eth/trie/[trie_defs, nibbles, db], faststreams/output_stream, - ./witness_types + ./witness_types, ../nimbus/constants type DB = TrieDatabaseRef @@ -42,7 +42,7 @@ proc rlpListToBitmask(r: var Rlp): uint = proc writeU32(wb: var WitnessBuilder, x: uint32) = wb.output.append(toBytesBE(x)) -proc writeNibbles(wb: var WitnessBuilder; n: NibblesSeq) = +proc writeNibbles(wb: var WitnessBuilder; n: NibblesSeq, withLen: bool = true) = let nibblesLen = n.len let numBytes = nibblesLen div 2 + nibblesLen mod 2 var bytes: array[32, byte] @@ -54,8 +54,9 @@ proc writeNibbles(wb: var WitnessBuilder; n: NibblesSeq) = else: bytes[pos div 2] = bytes[pos div 2] or (n[pos] shl 4) - # write nibblesLen - wb.output.append(nibblesLen.byte) + if withLen: + # write nibblesLen + wb.output.append(nibblesLen.byte) # write nibbles wb.output.append(bytes.toOpenArray(0, numBytes-1)) @@ -86,7 +87,7 @@ proc writeBranchNode(wb: var WitnessBuilder, mask: uint, depth: int, node: openA when defined(debugHash): wb.output.append(keccak(node).data) -proc writeAccountNode(wb: var WitnessBuilder, node: openArray[byte], depth: int) = +proc writeAccountNode(wb: var WitnessBuilder, acc: Account, nibbles: NibblesSeq, node: openArray[byte], depth: int) = # write type wb.output.append(AccountNodeType.byte) wb.writeU32(node.len.uint32) @@ -95,6 +96,25 @@ proc writeAccountNode(wb: var WitnessBuilder, node: openArray[byte], depth: int) when defined(debugDepth): wb.output.append(depth.byte) + #EIP170_CODE_SIZE_LIMIT + + doAssert(nibbles.len == 64 - depth) + let accountType = if acc.codeHash == blankStringHash or acc.storageRoot == emptyRlpHash: SimpleAccountType + else: ExtendedAccountType + + #wb.output.append(accountType.byte) + #wb.writeNibbles(nibbles, false) + #wb.output.append(acc.address) + #wb.output.append(acc.balance.toBytesBE) + #wb.output.append(acc.nonce.u256.toBytesBE) + + #if accountType == ExtendedAccountType: + + #0x00 pathnibbles: address:
balance: nonce: + #0x01 pathnibbles: address:
balance: nonce: bytecode: storage: + + # := len: b:^len + proc writeHashNode(wb: var WitnessBuilder, node: openArray[byte]) = # write type wb.output.append(HashNodeType.byte) @@ -107,7 +127,8 @@ proc writeShortNode(wb: var WitnessBuilder, node: openArray[byte], depth: int) = of 2: let (isLeaf, k) = nodeRlp.extensionNodeKey if isLeaf: - writeAccountNode(wb, node, depth) + let acc = nodeRlp.listElem(1).toBytes.decode(Account) + writeAccountNode(wb, acc, k, node, depth) else: # why this short extension node have no # child and still valid when we reconstruct @@ -151,7 +172,7 @@ proc getBranchRecurseAux(wb: var WitnessBuilder, node: openArray[byte], path: Ni getBranchRecurseAux(wb, nextLookup, path.slice(sharedNibbles), depth + sharedNibbles) else: # AccountNodeType - writeAccountNode(wb, node, depth) + writeAccountNode(wb, value.toBytes.decode(Account), k, node, depth) else: # this is a potential branch for multiproof writeHashNode(wb, keccak(node).data)