fix and refactor merkle_minimal sanity checks to run correctly and under CI

This commit is contained in:
Dustin Brody 2020-04-08 16:06:30 +02:00 committed by tersec
parent 64e6713fde
commit 366da6df36
3 changed files with 73 additions and 54 deletions

View File

@ -19,6 +19,7 @@ import # Unit test
./test_discovery_helpers,
./test_helpers,
./test_kvstore,
./test_mocking,
./test_kvstore_sqlite3,
./test_ssz,
./test_state_transition,

View File

@ -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,7 +123,6 @@ 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()
@ -136,16 +136,16 @@ when isMainModule: # Checks
result.add quote do:
block:
let tree = merkleTreeFromLeaves([a, b, c], Depth = `depth`)
echo "Tree: ", tree
#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
#echo "Proof: ", proof
doAssert is_valid_merkle_branch(
a, get_merkle_proof(tree, index = index),
@ -158,7 +158,6 @@ when isMainModule: # Checks
block: # proof for b
let index = 1
let proof = getMerkleProof(tree, index)
# echo "Proof: ", proof
doAssert is_valid_merkle_branch(
b, get_merkle_proof(tree, index = index),
@ -171,7 +170,6 @@ when isMainModule: # Checks
block: # proof for c
let index = 2
let proof = getMerkleProof(tree, index)
# echo "Proof: ", proof
doAssert is_valid_merkle_branch(
c, get_merkle_proof(tree, index = index),
@ -182,3 +180,7 @@ when isMainModule: # Checks
", nleaves: " & $`nleaves` & ')'
roundTrips()
true
when isMainModule:
discard testMerkleMinimal()

16
tests/test_mocking.nim Normal file
View File

@ -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()