Skip to content

Commit

Permalink
Merge pull request #7 from semiotic-ai/suchapalaver/test-inclusion-pr…
Browse files Browse the repository at this point in the history
…oof-for-block-body-given_eth1-data

Test inclusion proof for block body given eth1 data
  • Loading branch information
suchapalaver authored Aug 6, 2024
2 parents 5127488 + e0195b0 commit 3fa4890
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Blackboxing validating post-merge Ethereum data in practice to figure out the ne
- Notion doc on
[Post-merge Header Record Data Structure](https://www.notion.so/semiotic/Post-merge-header_record-data-structure-7290d03d356946188bdb9ac29366f510?pvs=4).
- [Beacon Chain `BeaconState` spec](https://github.com/ethereum/consensus-specs/blob/dev/specs/capella/beacon-chain.md#beaconstate)
- [Beacon Chain `BeaconBlockBody` spec](https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/beacon-chain.md#beaconblockbody)
- The [fork of `sigp/lighthouse`](https://github.com/semiotic-ai/lighthouse) we've been spiking.
- [Google Drive shared resources](https://drive.google.com/drive/u/1/folders/15diM-Gu4WFg9FrMWti3_B8xP0J0szUhW),
including `head-state.json` used in `head_state.rs` tests.
Expand All @@ -17,4 +18,5 @@ including `head-state.json` used in `head_state.rs` tests.

> [!NOTE]
> You need to add the `head-state.json` file from our shared Google Drive to
> the root of this repo to run tests!
> the root of this repo to run tests, as well as the
> [`bb-8786333.json`](https://drive.google.com/file/d/1-9SgmdxrOU5t1XlBc0hsRcEM-xZVN91N/view?usp=drive_link)!
72 changes: 72 additions & 0 deletions src/beacon_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use serde::{Deserialize, Serialize};
use types::{BeaconBlock, MainnetEthSpec};

#[derive(Debug, Deserialize, Serialize)]
pub struct BlockWrapper {
pub version: String,
pub execution_optimistic: bool,
pub finalized: bool,
pub data: Data,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Data {
pub message: BeaconBlock<MainnetEthSpec>,
}

/// Merkle proof depth for a `BeaconBlockBody` struct with 12 fields.
///
/// The proof depth is determined by finding the smallest power of 2 that is
/// greater than or equal to the number of fields. In this case, the number of
/// fields is 12, which is between 8 (2^3) and 16 (2^4).
pub const BEACON_BLOCK_BODY_PROOF_DEPTH: usize = 4;

/// The field corresponds to the index of the `eth1_data` field in the [`BeaconBlockBody`] struct:
/// <https://github.com/ethereum/annotated-spec/blob/master/deneb/beacon-chain.md#beaconblockbody>.
pub const ETH1_DATA_FIELD_INDEX: usize = 1;

#[cfg(test)]
mod tests {
use std::cell::LazyCell;

use super::*;

use merkle_proof::verify_merkle_proof;
use tree_hash::TreeHash;
use types::{light_client_update::ETH1_DATA_INDEX, BeaconBlockBody};

const DENEB_BLOCK_JSON: &str = include_str!("../bb-8786333.json");

const BLOCK_WRAPPER: LazyCell<BlockWrapper> = LazyCell::new(|| {
serde_json::from_str(DENEB_BLOCK_JSON).expect(
"For this spike we are using a Deneb block JSON file that has been shared among contributors",
)
});

#[test]
fn test_inclusion_proof_for_block_body_given_eth1_data() {
let block_wrapper = &BLOCK_WRAPPER;
let block = &block_wrapper.data.message;

let eth1_data = block.body().eth1_data();
let eth1_data_root = eth1_data.tree_hash_root();

let block_body = block.body_deneb().unwrap();
let block_body_hash = block_body.tree_hash_root();

let body = BeaconBlockBody::from(block_body.clone());
let proof = body.block_body_merkle_proof(ETH1_DATA_INDEX).unwrap();

let depth = BEACON_BLOCK_BODY_PROOF_DEPTH;

assert_eq!(proof.len(), depth, "proof length should equal depth");

assert!(verify_merkle_proof(
eth1_data_root,
&proof,
depth,
ETH1_DATA_FIELD_INDEX,
block_body_hash
));
}
}
6 changes: 3 additions & 3 deletions src/head_state.rs → src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use serde::{Deserialize, Serialize};
use tree_hash::TreeHash;
use types::{BeaconState, Error, EthSpec, MainnetEthSpec};

pub const HISTORICAL_ROOTS_FIELD_INDEX: usize = 7;
pub const HISTORICAL_SUMMARIES_FIELD_INDEX: usize = 27;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct HeadState<E: EthSpec> {
version: String,
Expand Down Expand Up @@ -53,9 +56,6 @@ mod tests {

const HEAD_STATE_JSON: &str = include_str!("../head-state.json");

const HISTORICAL_ROOTS_FIELD_INDEX: usize = 7;
const HISTORICAL_SUMMARIES_FIELD_INDEX: usize = 27;

const STATE: LazyCell<HeadState<MainnetEthSpec>> = LazyCell::new(|| {
serde_json::from_str(HEAD_STATE_JSON).expect(
"For this spike we are using a 'head-state.json' file that has been shared among contributors",
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod head_state;
pub mod beacon_block;
pub mod beacon_state;

0 comments on commit 3fa4890

Please sign in to comment.