Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forrestrie): fix historical summaries index in PoC example #6

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/forrestrie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ types.workspace = true
[dev-dependencies]
firehose-client = { path = "../firehose-client" }
insta.workspace = true
reqwest.workspace = true
reqwest = { workspace = true, features = ["json"] }
serde_json.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
30 changes: 17 additions & 13 deletions crates/forrestrie/examples/single_execution_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ use forrestrie::{
HistoricalDataProofs, BEACON_BLOCK_BODY_PROOF_DEPTH, EXECUTION_PAYLOAD_FIELD_INDEX,
},
beacon_state::{
compute_block_roots_proof_only, HeadState, HISTORY_TREE_DEPTH, SLOTS_PER_HISTORICAL_ROOT,
compute_block_roots_proof_only, HeadState, CAPELLA_START_ERA, HISTORY_TREE_DEPTH,
SLOTS_PER_HISTORICAL_ROOT,
},
BlockRoot,
};
Expand All @@ -46,22 +47,20 @@ use types::{
/// The execution block is in the execution payload of the Beacon block in slot [`BEACON_SLOT_NUMBER`].
const EXECUTION_BLOCK_NUMBER: u64 = 20759937;
/// This slot is the slot of the Beacon block that contains the execution block with [`EXECUTION_BLOCK_NUMBER`].
const BEACON_SLOT_NUMBER: u64 = 9968872; // <- this is the one that pairs with 20759937
const BEACON_SLOT_NUMBER: u64 = 9968872; // <- 9968872 pairs with 20759937
/// The URL to fetch the head state of the Beacon chain.
const LIGHT_CLIENT_DATA_URL: &str =
"https://www.lightclientdata.org/eth/v2/debug/beacon/states/head";

#[tokio::main]
async fn main() {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Get the head state of the Beacon chain from a Beacon API provider.
let state_handle = tokio::spawn(async move {
let url = "https://www.lightclientdata.org/eth/v2/debug/beacon/states/head".to_string();
let url = LIGHT_CLIENT_DATA_URL.to_string();
println!("Requesting head state ... (this can take a while!)");
let response = reqwest::get(url).await.unwrap();
let head_state: HeadState<MainnetEthSpec> = if response.status().is_success() {
let json_response: serde_json::Value = response.json().await.unwrap();
serde_json::from_value(json_response).unwrap()
} else {
panic!("Request failed with status: {}", response.status());
};
let head_state: HeadState<MainnetEthSpec> = response.json().await.unwrap();
head_state
});

Expand Down Expand Up @@ -148,15 +147,15 @@ async fn main() {

// The era of the block's slot.
// This is also the index of the historical summary containing the block roots for this era.
let era = lighthouse_beacon_block.slot().as_u64() / 8192;
let era = lighthouse_beacon_block.slot().as_usize() / SLOTS_PER_HISTORICAL_ROOT;

println!("Requesting 8192 blocks for the era... (this takes a while)");
let num_blocks = SLOTS_PER_HISTORICAL_ROOT as u64;
let mut stream = beacon_client
.stream_beacon_with_retry(era * SLOTS_PER_HISTORICAL_ROOT as u64, num_blocks)
.stream_beacon_with_retry((era * SLOTS_PER_HISTORICAL_ROOT) as u64, num_blocks)
.await
.unwrap();
let mut block_roots: Vec<Hash256> = Vec::with_capacity(8192);
let mut block_roots: Vec<Hash256> = Vec::with_capacity(SLOTS_PER_HISTORICAL_ROOT);
while let Some(block) = stream.next().await {
let root = BlockRoot::try_from(block).unwrap();
block_roots.push(root.0);
Expand All @@ -171,13 +170,18 @@ async fn main() {
let index = lighthouse_beacon_block.slot().as_usize() % SLOTS_PER_HISTORICAL_ROOT;
// Compute the proof of the block's inclusion in the block roots.
let proof = compute_block_roots_proof_only::<MainnetEthSpec>(&block_roots, index).unwrap();
// To get the correct index, we need to subtract the Capella start era.
// `HistoricalSummary` was introduced in Capella and the block we're proving inclusion for is in
// the post-Capella era.
// For pre-Capella states, we would use the same method, only using the historical_roots field.
let proof_era = era - CAPELLA_START_ERA;

let head_state = state_handle.await.unwrap();
let historical_summary: &HistoricalSummary = head_state
.data()
.historical_summaries()
.unwrap()
.get(era as usize)
.get(proof_era)
.unwrap();
let block_roots_tree_hash_root = historical_summary.block_summary_root();
assert_eq!(proof.len(), HISTORY_TREE_DEPTH);
Expand Down
Loading