From d1b95cf01236a9f6d0970b1ac53dcd0b43376136 Mon Sep 17 00:00:00 2001 From: pedro bufulin Date: Wed, 27 Nov 2024 19:32:10 -0300 Subject: [PATCH] refactor: rename file also add explanatory comment --- .../examples/post_merge_pre_capella_proof.rs | 48 --------------- ...ngle_block_post_merge_pre_capella_proof.rs | 58 +++++++++++++++++++ 2 files changed, 58 insertions(+), 48 deletions(-) delete mode 100644 crates/forrestrie-examples/examples/post_merge_pre_capella_proof.rs create mode 100644 crates/forrestrie-examples/examples/single_block_post_merge_pre_capella_proof.rs diff --git a/crates/forrestrie-examples/examples/post_merge_pre_capella_proof.rs b/crates/forrestrie-examples/examples/post_merge_pre_capella_proof.rs deleted file mode 100644 index a9d7dd13..00000000 --- a/crates/forrestrie-examples/examples/post_merge_pre_capella_proof.rs +++ /dev/null @@ -1,48 +0,0 @@ -//! Proof for an era of beacon blocks using the [`HistoricalBatch`]. -//! -use std::{env, fs, str::FromStr}; - -use ethportal_api::{ - consensus::beacon_state::HistoricalBatch, - types::execution::header_with_proof::HistoricalRootsBlockProof, -}; - -use reth_primitives::revm_primitives::{alloy_primitives::BlockHash, B256}; -use ssz::Decode; -use ssz_types::FixedVector; -use trin_validation::{ - historical_roots_acc::HistoricalRootsAccumulator, merkle::proof::verify_merkle_proof, -}; -use types::{light_client_update::CURRENT_SYNC_COMMITTEE_PROOF_LEN, MainnetEthSpec}; - -#[tokio::main] -async fn main() { - // Load a historical batch. - // A historical batch has to be generated from beacon blocks or retrieved - // from some source that already calculated these - let bytes = - fs::read("./crates/forrestrie-examples/assets/historical_batch-573-c847a969.ssz").unwrap(); - let hist_batch = HistoricalBatch::from_ssz_bytes(&bytes).unwrap(); - // construct proof from historical batch - let historical_roots_proof = hist_batch.build_block_root_proof(0); - - // // verify the proof - let epoch_size = 8192; - let slot = 4_698_112; - let block_root_index = slot % epoch_size; - let historical_root_index: i32 = slot / epoch_size; - let hist_acc = HistoricalRootsAccumulator::default(); - let historical_root = hist_acc.historical_roots[historical_root_index as usize]; - - let gen_index = 2 * epoch_size + block_root_index; - - let result = verify_merkle_proof( - B256::from_str("0x5273538177993fb75d8d27a00f32cd6cf583755062e97a45eb362cac356e3088") - .unwrap(), - &historical_roots_proof, - 14, - gen_index as usize, - historical_root, - ); - println!("result of verifying proof: {:?}", result); -} diff --git a/crates/forrestrie-examples/examples/single_block_post_merge_pre_capella_proof.rs b/crates/forrestrie-examples/examples/single_block_post_merge_pre_capella_proof.rs new file mode 100644 index 00000000..fd1900c3 --- /dev/null +++ b/crates/forrestrie-examples/examples/single_block_post_merge_pre_capella_proof.rs @@ -0,0 +1,58 @@ +//! Proof for single block to be part of an era of beacon blocks using the [`HistoricalBatch`]. +//! +//! Notice that A [`HistoricalBatch`]` isn't an accumulator, it is a list of block_roots and state_roots +//! So each root in the [`HistoricalRoots`] corresponds to hash_tree_root(historical_batch). +//! The batch is used to verify era against the accumulator. A block can be verified against an +//! [`HistoricalBatch`], hence chaining the proofs +use std::fs; + +use ethportal_api::consensus::beacon_state::HistoricalBatch; + +use ssz::Decode; +use trin_validation::{ + historical_roots_acc::HistoricalRootsAccumulator, merkle::proof::verify_merkle_proof, +}; + +#[tokio::main] +async fn main() { + // Load a historical batch. + // A historical batch has to be generated from beacon blocks or retrieved + // from some source that already calculated these + let bytes = + fs::read("./crates/forrestrie-examples/assets/historical_batch-573-c847a969.ssz").unwrap(); + let hist_batch = HistoricalBatch::from_ssz_bytes(&bytes).unwrap(); + + // check if my block_root is inside the HistoricalBatch + + // construct proof from historical batch + // In this example a slot that is inside the `HistoricalBatch` + // was picked: https://beaconcha.in/slot/4685828 + const EPOCH_SIZE: i32 = 8192; + let slot = 4685828; + let historical_root_index: i32 = slot % EPOCH_SIZE; + let historical_roots_proof = + hist_batch.build_block_root_proof((historical_root_index as u32).into()); + + // just checking if the rot macthes + let block_root = hist_batch.block_roots[historical_root_index as usize]; + + // The historical root we are getting: + println!("root: {:?}, index, {:?}", block_root, historical_root_index); + + // // verify the proof + let hist_acc = HistoricalRootsAccumulator::default(); + let block_root_index = slot % EPOCH_SIZE; + let gen_index = 2 * EPOCH_SIZE + block_root_index; + let historical_root_index = slot / EPOCH_SIZE; + let historical_root = hist_acc.historical_roots[historical_root_index as usize]; + + let result = verify_merkle_proof( + block_root, + &historical_roots_proof, + 14, + gen_index as usize, + historical_root, + ); + + println!("result of verifying proof: {:?}", result); +}