From 366da6df36ff3efa950ae35a403262c9d5aa2861 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Wed, 8 Apr 2020 16:06:30 +0200 Subject: [PATCH] fix and refactor merkle_minimal sanity checks to run correctly and under CI --- tests/all_tests.nim | 1 + tests/mocking/merkle_minimal.nim | 110 ++++++++++++++++--------------- tests/test_mocking.nim | 16 +++++ 3 files changed, 73 insertions(+), 54 deletions(-) create mode 100644 tests/test_mocking.nim diff --git a/tests/all_tests.nim b/tests/all_tests.nim index acdb1714b..396b02f77 100644 --- a/tests/all_tests.nim +++ b/tests/all_tests.nim @@ -19,6 +19,7 @@ import # Unit test ./test_discovery_helpers, ./test_helpers, ./test_kvstore, + ./test_mocking, ./test_kvstore_sqlite3, ./test_ssz, ./test_state_transition, diff --git a/tests/mocking/merkle_minimal.nim b/tests/mocking/merkle_minimal.nim index 26afea317..5f60564a8 100644 --- a/tests/mocking/merkle_minimal.nim +++ b/tests/mocking/merkle_minimal.nim @@ -1,16 +1,19 @@ # beacon_chain -# Copyright (c) 2018-2019 Status Research & Development GmbH +# Copyright (c) 2018-2020 Status Research & Development GmbH # Licensed and distributed under either of # * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). # at your option. This file may not be copied, modified, or distributed except according to those terms. +# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/tests/core/pyspec/eth2spec/utils/merkle_minimal.py + # Merkle tree helpers # --------------------------------------------------------------- import + strutils, macros, bitops, # Specs - ../../beacon_chain/spec/[datatypes, digest], + ../../beacon_chain/spec/[beaconstate, datatypes, digest], ../../beacon_chain/ssz func round_step_down*(x: Natural, step: static Natural): int {.inline.} = @@ -82,9 +85,7 @@ proc getMerkleProof*[Depth: static int]( else: result[depth] = ZeroHashes[depth] -when isMainModule: # Checks - import strutils, macros, bitops - +proc testMerkleMinimal*(): bool = proc toDigest[N: static int](x: array[N, byte]): Eth2Digest = result.data[0 .. N-1] = x @@ -122,63 +123,64 @@ when isMainModule: # Checks # Running tests with hash_tree_root([a, b, c]) # works for depth 2 (3 or 4 leaves) - when false: - macro roundTrips(): untyped = - result = newStmtList() + macro roundTrips(): untyped = + result = newStmtList() - # Unsure why sszList ident is undeclared in "quote do" - let list = bindSym"sszList" + # Unsure why sszList ident is undeclared in "quote do" + let list = bindSym"sszList" - # compile-time unrolled test - for nleaves in [3, 4, 5, 7, 8, 1 shl 10, 1 shl 32]: - let depth = fastLog2(nleaves-1) + 1 + # compile-time unrolled test + for nleaves in [3, 4, 5, 7, 8, 1 shl 10, 1 shl 32]: + let depth = fastLog2(nleaves-1) + 1 - result.add quote do: - block: - let tree = merkleTreeFromLeaves([a, b, c], Depth = `depth`) - echo "Tree: ", tree + result.add quote do: + block: + let tree = merkleTreeFromLeaves([a, b, c], Depth = `depth`) + #echo "Tree: ", tree - let leaves = `list`(@[a, b, c], int64(`nleaves`)) - let root = hash_tree_root(leaves) - echo "Root: ", root + doAssert tree.nnznodes[`depth`].len == 1 + let root = tree.nnznodes[`depth`][0] + #echo "Root: ", root - block: # proof for a - let index = 0 - let proof = getMerkleProof(tree, index) - echo "Proof: ", proof + block: # proof for a + let index = 0 + let proof = getMerkleProof(tree, index) + #echo "Proof: ", proof - doAssert is_valid_merkle_branch( - a, get_merkle_proof(tree, index = index), - depth = `depth`, - index = index.uint64, - root = root - ), "Failed (depth: " & $`depth` & - ", nleaves: " & $`nleaves` & ')' + doAssert is_valid_merkle_branch( + a, get_merkle_proof(tree, index = index), + depth = `depth`, + index = index.uint64, + root = root + ), "Failed (depth: " & $`depth` & + ", nleaves: " & $`nleaves` & ')' - block: # proof for b - let index = 1 - let proof = getMerkleProof(tree, index) - # echo "Proof: ", proof + block: # proof for b + let index = 1 + let proof = getMerkleProof(tree, index) - doAssert is_valid_merkle_branch( - b, get_merkle_proof(tree, index = index), - depth = `depth`, - index = index.uint64, - root = root - ), "Failed (depth: " & $`depth` & - ", nleaves: " & $`nleaves` & ')' + doAssert is_valid_merkle_branch( + b, get_merkle_proof(tree, index = index), + depth = `depth`, + index = index.uint64, + root = root + ), "Failed (depth: " & $`depth` & + ", nleaves: " & $`nleaves` & ')' - block: # proof for c - let index = 2 - let proof = getMerkleProof(tree, index) - # echo "Proof: ", proof + block: # proof for c + let index = 2 + let proof = getMerkleProof(tree, index) - doAssert is_valid_merkle_branch( - c, get_merkle_proof(tree, index = index), - depth = `depth`, - index = index.uint64, - root = root - ), "Failed (depth: " & $`depth` & - ", nleaves: " & $`nleaves` & ')' + doAssert is_valid_merkle_branch( + c, get_merkle_proof(tree, index = index), + depth = `depth`, + index = index.uint64, + root = root + ), "Failed (depth: " & $`depth` & + ", nleaves: " & $`nleaves` & ')' - roundTrips() + roundTrips() + true + +when isMainModule: + discard testMerkleMinimal() diff --git a/tests/test_mocking.nim b/tests/test_mocking.nim new file mode 100644 index 000000000..2c74e2d12 --- /dev/null +++ b/tests/test_mocking.nim @@ -0,0 +1,16 @@ +# beacon_chain +# Copyright (c) 2020 Status Research & Development GmbH +# Licensed and distributed under either of +# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). +# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +{.used.} + +import + unittest, ./testutil, ./mocking/merkle_minimal + +suiteReport "Mocking utilities": + timedTest "merkle_minimal": + check: + testMerkleMinimal()