mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 18:32:42 +00:00
Added Merkle Proof Generation for Beacon State (#3674)
## Issue Addressed This PR addresses partially #3651 ## Proposed Changes This PR adds the following methods: * a new method to trait `TreeHash`, `hash_tree_leaves` which returns all the Merkle leaves of the ssz object. * a new method to `BeaconState`: `compute_merkle_proof` which generates a specific merkle proof for given depth and index by using the `hash_tree_leaves` as leaves function. ## Additional Info Now here is some rationale on why I decided to go down this route: adding a new function to commonly used trait is a pain but was necessary to make sure we have all merkle leaves for every object, that is why I just added `hash_tree_leaves` in the trait and not `compute_merkle_proof` as well. although it would make sense it gives us code duplication/harder review time and we just need it from one specific object in one specific usecase so not worth the effort YET. In my humble opinion. Co-authored-by: Michael Sproul <micsproul@gmail.com>
This commit is contained in:
@@ -18,6 +18,7 @@ mod fork;
|
||||
mod fork_choice;
|
||||
mod genesis_initialization;
|
||||
mod genesis_validity;
|
||||
mod merkle_proof_validity;
|
||||
mod operations;
|
||||
mod rewards;
|
||||
mod sanity_blocks;
|
||||
@@ -41,6 +42,7 @@ pub use epoch_processing::*;
|
||||
pub use fork::ForkTest;
|
||||
pub use genesis_initialization::*;
|
||||
pub use genesis_validity::*;
|
||||
pub use merkle_proof_validity::*;
|
||||
pub use operations::*;
|
||||
pub use rewards::RewardsTest;
|
||||
pub use sanity_blocks::*;
|
||||
|
||||
83
testing/ef_tests/src/cases/merkle_proof_validity.rs
Normal file
83
testing/ef_tests/src/cases/merkle_proof_validity.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use super::*;
|
||||
use crate::decode::{ssz_decode_state, yaml_decode_file};
|
||||
use serde_derive::Deserialize;
|
||||
use std::path::Path;
|
||||
use tree_hash::Hash256;
|
||||
use types::{BeaconState, EthSpec, ForkName};
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct Metadata {
|
||||
#[serde(rename(deserialize = "description"))]
|
||||
_description: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct MerkleProof {
|
||||
pub leaf: Hash256,
|
||||
pub leaf_index: usize,
|
||||
pub branch: Vec<Hash256>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(bound = "E: EthSpec")]
|
||||
pub struct MerkleProofValidity<E: EthSpec> {
|
||||
pub metadata: Option<Metadata>,
|
||||
pub state: BeaconState<E>,
|
||||
pub merkle_proof: MerkleProof,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> LoadCase for MerkleProofValidity<E> {
|
||||
fn load_from_dir(path: &Path, fork_name: ForkName) -> Result<Self, Error> {
|
||||
let spec = &testing_spec::<E>(fork_name);
|
||||
let state = ssz_decode_state(&path.join("state.ssz_snappy"), spec)?;
|
||||
let merkle_proof = yaml_decode_file(&path.join("proof.yaml"))?;
|
||||
// Metadata does not exist in these tests but it is left like this just in case.
|
||||
let meta_path = path.join("meta.yaml");
|
||||
let metadata = if meta_path.exists() {
|
||||
Some(yaml_decode_file(&meta_path)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
metadata,
|
||||
state,
|
||||
merkle_proof,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> Case for MerkleProofValidity<E> {
|
||||
fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
|
||||
let mut state = self.state.clone();
|
||||
state.initialize_tree_hash_cache();
|
||||
let proof = match state.compute_merkle_proof(self.merkle_proof.leaf_index) {
|
||||
Ok(proof) => proof,
|
||||
Err(_) => {
|
||||
return Err(Error::FailedToParseTest(
|
||||
"Could not retrieve merkle proof".to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
let proof_len = proof.len();
|
||||
let branch_len = self.merkle_proof.branch.len();
|
||||
if proof_len != branch_len {
|
||||
return Err(Error::NotEqual(format!(
|
||||
"Branches not equal in length computed: {}, expected {}",
|
||||
proof_len, branch_len
|
||||
)));
|
||||
}
|
||||
|
||||
for (i, proof_leaf) in proof.iter().enumerate().take(proof_len) {
|
||||
let expected_leaf = self.merkle_proof.branch[i];
|
||||
if *proof_leaf != expected_leaf {
|
||||
return Err(Error::NotEqual(format!(
|
||||
"Leaves not equal in merke proof computed: {}, expected: {}",
|
||||
hex::encode(proof_leaf),
|
||||
hex::encode(expected_leaf)
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -617,6 +617,30 @@ impl<E: EthSpec + TypeName> Handler for GenesisInitializationHandler<E> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Derivative)]
|
||||
#[derivative(Default(bound = ""))]
|
||||
pub struct MerkleProofValidityHandler<E>(PhantomData<E>);
|
||||
|
||||
impl<E: EthSpec + TypeName> Handler for MerkleProofValidityHandler<E> {
|
||||
type Case = cases::MerkleProofValidity<E>;
|
||||
|
||||
fn config_name() -> &'static str {
|
||||
E::name()
|
||||
}
|
||||
|
||||
fn runner_name() -> &'static str {
|
||||
"light_client"
|
||||
}
|
||||
|
||||
fn handler_name(&self) -> String {
|
||||
"single_merkle_proof".into()
|
||||
}
|
||||
|
||||
fn is_enabled_for_fork(&self, fork_name: ForkName) -> bool {
|
||||
fork_name != ForkName::Base
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Derivative)]
|
||||
#[derivative(Default(bound = ""))]
|
||||
pub struct OperationsHandler<E, O>(PhantomData<(E, O)>);
|
||||
|
||||
Reference in New Issue
Block a user