-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
also add explanatory comment
- Loading branch information
Showing
2 changed files
with
58 additions
and
48 deletions.
There are no files selected for viewing
48 changes: 0 additions & 48 deletions
48
crates/forrestrie-examples/examples/post_merge_pre_capella_proof.rs
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
crates/forrestrie-examples/examples/single_block_post_merge_pre_capella_proof.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |