allow test_block_witness to run single test

This commit is contained in:
andri lim 2020-04-28 09:30:10 +07:00
parent a1a6b7b9c4
commit 692bed176e
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 85 additions and 17 deletions

View File

@ -2,7 +2,7 @@ import
unittest2, os, json, strutils, unittest2, os, json, strutils,
eth/[common, rlp], eth/trie/[hexary, db, trie_defs], eth/[common, rlp], eth/trie/[hexary, db, trie_defs],
stew/byteutils, faststreams/input_stream, stew/byteutils, faststreams/input_stream,
../tests/test_helpers, ../tests/[test_helpers, test_config],
../nimbus/db/accounts_cache, ../nimbus/db/accounts_cache,
../stateless/[witness_from_tree, tree_from_witness] ../stateless/[witness_from_tree, tree_from_witness]
@ -97,8 +97,38 @@ proc testFixtureGST(node: JsonNode, testStatusIMPL: var TestStatus) =
fixture["pre"].testBlockWitness(emptyRlpHash, testStatusIMPL) fixture["pre"].testBlockWitness(emptyRlpHash, testStatusIMPL)
#suite "Block Witness": proc blockWitnessMain*(debugMode = false) =
#jsonTest("newBlockChainTests", "witnessBuilderBC", testFixtureBC) 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": let folder = if config.legacy: "GeneralStateTests" else: "newGeneralStateTests"
jsonTest("GeneralStateTests", "witnessBuilderGST", testFixtureGST) 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)

View File

@ -1,9 +1,26 @@
import import
randutils, stew/byteutils, random, randutils, stew/byteutils, random,
eth/[common, rlp], eth/trie/[hexary, db], eth/[common, rlp], eth/trie/[hexary, db],
faststreams/input_stream, faststreams/input_stream, nimcrypto/[utils, sysrand],
../stateless/[witness_from_tree, tree_from_witness] ../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) = proc runTest(keyBytes: int, valBytes: int, numPairs: int) =
var memDB = newMemoryDB() var memDB = newMemoryDB()
var trie = initHexaryTrie(memDB) var trie = initHexaryTrie(memDB)

View File

@ -1,9 +1,9 @@
import import
stew/[byteutils, endians2], stew/[byteutils, endians2],
nimcrypto/[keccak, hash], eth/rlp, nimcrypto/[keccak, hash], eth/[common, rlp],
eth/trie/[trie_defs, nibbles, db], eth/trie/[trie_defs, nibbles, db],
faststreams/output_stream, faststreams/output_stream,
./witness_types ./witness_types, ../nimbus/constants
type type
DB = TrieDatabaseRef DB = TrieDatabaseRef
@ -42,7 +42,7 @@ proc rlpListToBitmask(r: var Rlp): uint =
proc writeU32(wb: var WitnessBuilder, x: uint32) = proc writeU32(wb: var WitnessBuilder, x: uint32) =
wb.output.append(toBytesBE(x)) 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 nibblesLen = n.len
let numBytes = nibblesLen div 2 + nibblesLen mod 2 let numBytes = nibblesLen div 2 + nibblesLen mod 2
var bytes: array[32, byte] var bytes: array[32, byte]
@ -54,8 +54,9 @@ proc writeNibbles(wb: var WitnessBuilder; n: NibblesSeq) =
else: else:
bytes[pos div 2] = bytes[pos div 2] or (n[pos] shl 4) bytes[pos div 2] = bytes[pos div 2] or (n[pos] shl 4)
# write nibblesLen if withLen:
wb.output.append(nibblesLen.byte) # write nibblesLen
wb.output.append(nibblesLen.byte)
# write nibbles # write nibbles
wb.output.append(bytes.toOpenArray(0, numBytes-1)) 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): when defined(debugHash):
wb.output.append(keccak(node).data) 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 # write type
wb.output.append(AccountNodeType.byte) wb.output.append(AccountNodeType.byte)
wb.writeU32(node.len.uint32) wb.writeU32(node.len.uint32)
@ -95,6 +96,25 @@ proc writeAccountNode(wb: var WitnessBuilder, node: openArray[byte], depth: int)
when defined(debugDepth): when defined(debugDepth):
wb.output.append(depth.byte) 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:<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)>
#<Bytecode> := len:<U32> b:<Byte>^len
proc writeHashNode(wb: var WitnessBuilder, node: openArray[byte]) = proc writeHashNode(wb: var WitnessBuilder, node: openArray[byte]) =
# write type # write type
wb.output.append(HashNodeType.byte) wb.output.append(HashNodeType.byte)
@ -107,7 +127,8 @@ proc writeShortNode(wb: var WitnessBuilder, node: openArray[byte], depth: int) =
of 2: of 2:
let (isLeaf, k) = nodeRlp.extensionNodeKey let (isLeaf, k) = nodeRlp.extensionNodeKey
if isLeaf: if isLeaf:
writeAccountNode(wb, node, depth) let acc = nodeRlp.listElem(1).toBytes.decode(Account)
writeAccountNode(wb, acc, k, node, depth)
else: else:
# why this short extension node have no # why this short extension node have no
# child and still valid when we reconstruct # 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) getBranchRecurseAux(wb, nextLookup, path.slice(sharedNibbles), depth + sharedNibbles)
else: else:
# AccountNodeType # AccountNodeType
writeAccountNode(wb, node, depth) writeAccountNode(wb, value.toBytes.decode(Account), k, node, depth)
else: else:
# this is a potential branch for multiproof # this is a potential branch for multiproof
writeHashNode(wb, keccak(node).data) writeHashNode(wb, keccak(node).data)