2
0
mirror of https://github.com/status-im/nimbus-eth2.git synced 2025-02-25 20:55:24 +00:00

convert mostly-uncompiled verify_merkle_branch(...) calls to newer is_valid_merkle_branch(...) name; re-enable several lines of deposit testing code and outline roadmap for completion

This commit is contained in:
Dustin Brody 2019-11-18 17:52:02 +01:00
parent d8590bbffc
commit 2749cb14a6
3 changed files with 23 additions and 22 deletions

@ -12,7 +12,7 @@ import
./crypto, ./datatypes, ./digest, ./helpers, ./validator
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.1/specs/core/0_beacon-chain.md#is_valid_merkle_branch
func is_valid_merkle_branch(leaf: Eth2Digest, branch: openarray[Eth2Digest], depth: uint64, index: uint64, root: Eth2Digest): bool =
func is_valid_merkle_branch*(leaf: Eth2Digest, branch: openarray[Eth2Digest], depth: uint64, index: uint64, root: Eth2Digest): bool =
## Check if ``leaf`` at ``index`` verifies against the Merkle ``root`` and
## ``branch``.
var

@ -44,8 +44,7 @@ proc merkleTreeFromLeaves*(
values: openarray[Eth2Digest],
Depth: static[int] = DEPOSIT_CONTRACT_TREE_DEPTH
): SparseMerkleTree[Depth] =
## Depth should be the same as
## verify_merkle_branch / is_valid_merkle_branch
## Depth should be the same as is_valid_merkle_branch
result.nnznodes[0] = @values
@ -150,7 +149,7 @@ when isMainModule: # Checks
let proof = getMerkleProof(tree, index)
echo "Proof: ", proof
doAssert verify_merkle_branch(
doAssert is_valid_merkle_branch(
a, get_merkle_proof(tree, index = index),
depth = `depth`,
index = index.uint64,
@ -163,7 +162,7 @@ when isMainModule: # Checks
let proof = getMerkleProof(tree, index)
# echo "Proof: ", proof
doAssert verify_merkle_branch(
doAssert is_valid_merkle_branch(
b, get_merkle_proof(tree, index = index),
depth = `depth`,
index = index.uint64,
@ -176,7 +175,7 @@ when isMainModule: # Checks
let proof = getMerkleProof(tree, index)
# echo "Proof: ", proof
doAssert verify_merkle_branch(
doAssert is_valid_merkle_branch(
c, get_merkle_proof(tree, index = index),
depth = `depth`,
index = index.uint64,

@ -14,7 +14,7 @@ import
# 0.19.6 shims
stew/objects, # import default
# Specs
../../beacon_chain/spec/[datatypes, crypto, helpers, digest],
../../beacon_chain/spec/[datatypes, crypto, helpers, digest, beaconstate],
# Internals
../../beacon_chain/[ssz, extras],
# Mocking procs
@ -136,23 +136,24 @@ template mockGenesisDepositsImpl(
depositsDataHash.add hash_tree_root(result[valIdx].data)
# 2nd & 3rd loops - build hashes and proofs
when false:
let root = hash_tree_root(depositsData)
let tree = merkleTreeFromLeaves(depositsDataHash)
let root = hash_tree_root(depositsData)
let tree = merkleTreeFromLeaves(depositsDataHash)
# 4th loop - append proof
for valIdx in 0 ..< validatorCount.int:
when false: # TODO
result[valIdx].proof[0..31] = tree.getMerkleProof(valIdx)
result[valIdx].proof[32] = int_to_bytes32(index + 1)
doAssert:
verify_merkle_branch(
depositsDataHash[valIdx],
result[valIdx].proof,
DEPOSIT_CONTRACT_TREE_DEPTH,
valIdx,
root
)
# TODO ensure genesis & deposit process tests suffice to catch whether
# changes here break things; ensure that this matches the merkle proof
# sequence is_valid_merkle_branch(...) now looks for
result[valIdx].proof[0..31] = tree.getMerkleProof(valIdx)
result[valIdx].proof[32] =
Eth2Digest(data: int_to_bytes32((valIdx + 1).uint64))
doAssert is_valid_merkle_branch(
depositsDataHash[valIdx],
result[valIdx].proof,
DEPOSIT_CONTRACT_TREE_DEPTH,
valIdx.uint64,
root
)
proc mockGenesisBalancedDeposits*(
validatorCount: uint64,
@ -199,6 +200,7 @@ proc mockUpdateStateForNewDeposit*(
flags: UpdateFlags
): Deposit =
# TODO withdrawal credentials
mockDepositData(
@ -214,7 +216,7 @@ proc mockUpdateStateForNewDeposit*(
let tree = merkleTreeFromLeaves([hash_tree_root(result.data)])
result[valIdx].proof[0..31] = tree.getMerkleProof(0)
result[valIdx].proof[32] = int_to_bytes32(0 + 1)
# doAssert: verify_merkle_branch(...)
# doAssert is_valid_merkle_branch(...)
# TODO: this logic from the eth2.0-specs test suite seems strange
# but confirmed by running it