Implement push_leaf for MerkleTree (#563)

* Prototype for far_right push

* Add push method and tests

* Modify beacon_chain_builder for interop to use push instead of create

* Add Push method to MerkleTree

* Cargo fmt

* Remove redundant tests

* Fix typo

* Rename push to push_leaf

* Fix clippy warnings

* Add DepthTooSmall enum variant

* Avoid cloning in MerkleTree::push_leaf

* Add quickcheck test for push_leaf

* Cargo fmt updated

* Return err instead of using unwrap()

* Use enumerate instead of hard indexing

* Use if let and return string on error

* Fix typo in deposit_leave
This commit is contained in:
pscott
2019-10-25 07:07:03 +02:00
committed by Paul Hauner
parent 6c2871637d
commit 13faa47da1
2 changed files with 138 additions and 13 deletions

View File

@@ -210,22 +210,19 @@ fn interop_genesis_state<T: EthSpec>(
.collect::<Vec<_>>();
let mut proofs = vec![];
for i in 1..=deposit_root_leaves.len() {
// Note: this implementation is not so efficient.
//
// If `MerkleTree` had a push method, we could just build one tree and sample it instead of
// rebuilding the tree for each deposit.
let tree = MerkleTree::create(
&deposit_root_leaves[0..i],
spec.deposit_contract_tree_depth as usize,
);
let depth = spec.deposit_contract_tree_depth as usize;
let mut tree = MerkleTree::create(&[], depth);
for (i, deposit_leaf) in deposit_root_leaves.iter().enumerate() {
if let Err(_) = tree.push_leaf(*deposit_leaf, depth) {
return Err(String::from("Failed to push leaf"))
}
let (_, mut proof) = tree.generate_proof(i - 1, spec.deposit_contract_tree_depth as usize);
proof.push(Hash256::from_slice(&int_to_bytes32(i)));
let (_, mut proof) = tree.generate_proof(i, depth);
proof.push(Hash256::from_slice(&int_to_bytes32(i + 1)));
assert_eq!(
proof.len(),
spec.deposit_contract_tree_depth as usize + 1,
depth + 1,
"Deposit proof should be correct len"
);