Skip to content

feat: create header proof using BeaconBlockElectra #1800

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

Merged
merged 2 commits into from
May 3, 2025
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
16 changes: 8 additions & 8 deletions bin/e2hs-writer/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use e2store::{
era1::{BlockTuple, Era1},
utils::{get_era1_files, get_era_files},
};
use ethportal_api::{consensus::beacon_state::HistoricalBatch, types::network_spec::network_spec};
use ethportal_api::{
consensus::{beacon_block::SignedBeaconBlock, beacon_state::HistoricalBatch},
types::network_spec::network_spec,
};
use reqwest::{
header::{HeaderMap, HeaderValue, CONTENT_TYPE},
Client,
Expand Down Expand Up @@ -46,10 +49,7 @@ pub struct MinimalEra {
}

impl MinimalEra {
pub fn get_block(
&self,
block_number: u64,
) -> Option<(CompressedSignedBeaconBlock, &MinimalEra)> {
pub fn get_block(&self, block_number: u64) -> Option<&SignedBeaconBlock> {
let first_block_number = self.blocks[0].block.execution_block_number();
let last_block_number = self.blocks[self.blocks.len() - 1]
.block
Expand All @@ -60,7 +60,7 @@ impl MinimalEra {
.iter()
.find(|block| block.block.execution_block_number() == block_number)
{
return Some((block.clone(), self));
return Some(&block.block);
}
}
None
Expand Down Expand Up @@ -179,15 +179,15 @@ impl EraProvider {
pub fn get_post_merge(
&self,
block_number: u64,
) -> anyhow::Result<(CompressedSignedBeaconBlock, &MinimalEra)> {
) -> anyhow::Result<(&SignedBeaconBlock, &HistoricalBatch)> {
ensure!(
network_spec().is_paris_active_at_block(block_number),
"Invalid logic, tried to lookup era file for pre-merge block"
);
for sources in self.sources.iter() {
if let EraSource::PostMerge(era) = sources {
if let Some(block) = era.get_block(block_number) {
return Ok(block);
return Ok((block, &era.historical_batch));
}
}
}
Expand Down
199 changes: 119 additions & 80 deletions bin/e2hs-writer/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ use alloy::{
eips::eip4895::{Withdrawal, Withdrawals},
primitives::B256,
};
use alloy_hardforks::{EthereumHardfork, EthereumHardforks};
use alloy_hardforks::EthereumHardforks;
use anyhow::{anyhow, bail, ensure};
use ethportal_api::{
consensus::{
beacon_block::{
BeaconBlockBellatrix, BeaconBlockCapella, BeaconBlockDeneb, BeaconBlockElectra,
SignedBeaconBlock,
},
beacon_state::HistoricalBatch,
},
types::{
execution::{
accumulator::EpochAccumulator,
block_body::BlockBody,
header_with_proof::{
build_capella_historical_summaries_proof, build_deneb_historical_summaries_proof,
build_historical_roots_proof, BlockHeaderProof,
BlockProofHistoricalHashesAccumulator, HeaderWithProof,
build_electra_historical_summaries_proof, build_historical_roots_proof,
BlockHeaderProof, BlockProofHistoricalHashesAccumulator, HeaderWithProof,
},
},
network_spec::network_spec,
Expand All @@ -32,7 +39,7 @@ use crate::{
provider::EraProvider,
utils::{
bellatrix_execution_payload_to_header, capella_execution_payload_to_header,
lookup_epoch_acc, post_deneb_execution_payload_to_header,
deneb_execution_payload_to_header, electra_execution_payload_to_header, lookup_epoch_acc,
},
};

Expand Down Expand Up @@ -107,6 +114,16 @@ impl EpochReader {
})
}

pub fn iter_blocks(mut self) -> impl Iterator<Item = anyhow::Result<AllBlockData>> {
(self.starting_block..self.ending_block).map(move |current_block| {
if network_spec().is_paris_active_at_block(current_block) {
self.get_post_merge_block_data(current_block)
} else {
self.get_pre_merge_block_data(current_block)
}
})
}

fn get_pre_merge_block_data(&self, block_number: u64) -> anyhow::Result<AllBlockData> {
let tuple = self.era_provider.get_pre_merge(block_number)?;
let header = tuple.header.header;
Expand All @@ -129,23 +146,53 @@ impl EpochReader {
})
}

fn get_merge_to_capella_block_data(
&mut self,
block_number: u64,
) -> anyhow::Result<AllBlockData> {
let (block, era) = self.era_provider.get_post_merge(block_number)?;
let block = block
.block
.message_merge()
.map_err(|e| anyhow!("Unable to decode merge block: {e:?}"))?;
let execution_payload = &block.body.execution_payload;
let transactions = decode_transactions(&execution_payload.transactions)?;
fn get_post_merge_block_data(&mut self, block_number: u64) -> anyhow::Result<AllBlockData> {
let (block, historical_batch) = self.era_provider.get_post_merge(block_number)?;
ensure!(
block.execution_block_number() == block_number,
"Post-merge block data is for wrong block! Expected: {block_number}, actual: {}",
block.execution_block_number()
);

let (header_with_proof, body) = match &block {
SignedBeaconBlock::Bellatrix(beacon_block) => {
self.get_merge_to_capella_header_and_body(&beacon_block.message, historical_batch)?
}
SignedBeaconBlock::Capella(beacon_block) => {
self.get_capella_to_deneb_header_and_body(&beacon_block.message, historical_batch)?
}
SignedBeaconBlock::Deneb(beacon_block) => {
self.get_deneb_to_electra_header_and_body(&beacon_block.message, historical_batch)?
}
SignedBeaconBlock::Electra(beacon_block) => {
self.get_post_electra_header_and_body(&beacon_block.message, historical_batch)?
}
};

let receipts = self.get_receipts(block_number, header_with_proof.header.receipts_root)?;

Ok(AllBlockData {
block_number,
header_with_proof,
body,
receipts,
})
}

fn get_merge_to_capella_header_and_body(
&self,
block: &BeaconBlockBellatrix,
historical_batch: &HistoricalBatch,
) -> anyhow::Result<(HeaderWithProof, BlockBody)> {
let payload = &block.body.execution_payload;

let transactions = decode_transactions(&payload.transactions)?;

let header_with_proof = HeaderWithProof {
header: bellatrix_execution_payload_to_header(execution_payload, &transactions)?,
header: bellatrix_execution_payload_to_header(payload, &transactions)?,
proof: BlockHeaderProof::HistoricalRoots(build_historical_roots_proof(
block.slot,
&era.historical_batch,
historical_batch,
block,
)),
};
Expand All @@ -154,25 +201,17 @@ impl EpochReader {
ommers: vec![],
withdrawals: None,
});
let receipts = self.get_receipts(block_number, header_with_proof.header.receipts_root)?;
Ok(AllBlockData {
block_number,
header_with_proof,
body,
receipts,
})

Ok((header_with_proof, body))
}

fn get_capella_to_deneb_block_data(
&mut self,
block_number: u64,
) -> anyhow::Result<AllBlockData> {
let (block, era) = self.era_provider.get_post_merge(block_number)?;
let block = block
.block
.message_capella()
.map_err(|e| anyhow!("Unable to decode capella block: {e:?}"))?;
fn get_capella_to_deneb_header_and_body(
&self,
block: &BeaconBlockCapella,
historical_batch: &HistoricalBatch,
) -> anyhow::Result<(HeaderWithProof, BlockBody)> {
let payload = &block.body.execution_payload;

let transactions = decode_transactions(&payload.transactions)?;
let withdrawals: Vec<Withdrawal> =
payload.withdrawals.iter().map(Withdrawal::from).collect();
Expand All @@ -182,7 +221,7 @@ impl EpochReader {
proof: BlockHeaderProof::HistoricalSummariesCapella(
build_capella_historical_summaries_proof(
block.slot,
&era.historical_batch.block_roots,
&historical_batch.block_roots,
block,
),
),
Expand All @@ -192,28 +231,23 @@ impl EpochReader {
ommers: vec![],
withdrawals: Some(Withdrawals::new(withdrawals)),
});
let receipts = self.get_receipts(block_number, header_with_proof.header.receipts_root)?;
Ok(AllBlockData {
block_number,
header_with_proof,
body,
receipts,
})

Ok((header_with_proof, body))
}

fn get_deneb_block_data(&mut self, block_number: u64) -> anyhow::Result<AllBlockData> {
let (block, era) = self.era_provider.get_post_merge(block_number)?;
let block = block
.block
.message_deneb()
.map_err(|e| anyhow!("Unable to decode deneb block: {e:?}"))?;
fn get_deneb_to_electra_header_and_body(
&self,
block: &BeaconBlockDeneb,
historical_batch: &HistoricalBatch,
) -> anyhow::Result<(HeaderWithProof, BlockBody)> {
let payload = &block.body.execution_payload;

let transactions = decode_transactions(&payload.transactions)?;
let withdrawals: Vec<Withdrawal> =
payload.withdrawals.iter().map(Withdrawal::from).collect();

let header_with_proof = HeaderWithProof {
header: post_deneb_execution_payload_to_header(
header: deneb_execution_payload_to_header(
payload,
block.parent_root,
&transactions,
Expand All @@ -222,7 +256,7 @@ impl EpochReader {
proof: BlockHeaderProof::HistoricalSummariesDeneb(
build_deneb_historical_summaries_proof(
block.slot,
&era.historical_batch.block_roots,
&historical_batch.block_roots,
block,
),
),
Expand All @@ -232,39 +266,44 @@ impl EpochReader {
ommers: vec![],
withdrawals: Some(Withdrawals::new(withdrawals)),
});
let receipts = self.get_receipts(block_number, header_with_proof.header.receipts_root)?;
Ok(AllBlockData {
block_number,
header_with_proof,
body,
receipts,
})

Ok((header_with_proof, body))
}

pub fn iter_blocks(mut self) -> impl Iterator<Item = anyhow::Result<AllBlockData>> {
(self.starting_block..self.ending_block).map(move |current_block| {
if current_block
< EthereumHardfork::Paris
.activation_block(network_spec().network().into())
.expect("Paris should be available")
{
self.get_pre_merge_block_data(current_block)
} else if current_block
< EthereumHardfork::Shanghai
.activation_block(network_spec().network().into())
.expect("Shanghai should be available")
{
self.get_merge_to_capella_block_data(current_block)
} else if current_block
< EthereumHardfork::Cancun
.activation_block(network_spec().network().into())
.expect("Cancun should be available")
{
self.get_capella_to_deneb_block_data(current_block)
} else {
self.get_deneb_block_data(current_block)
}
})
fn get_post_electra_header_and_body(
&self,
block: &BeaconBlockElectra,
historical_batch: &HistoricalBatch,
) -> anyhow::Result<(HeaderWithProof, BlockBody)> {
let payload = &block.body.execution_payload;

let transactions = decode_transactions(&payload.transactions)?;
let withdrawals: Vec<Withdrawal> =
payload.withdrawals.iter().map(Withdrawal::from).collect();

let header_with_proof = HeaderWithProof {
header: electra_execution_payload_to_header(
payload,
block.parent_root,
&transactions,
&withdrawals,
&block.body.execution_requests,
)?,
proof: BlockHeaderProof::HistoricalSummariesDeneb(
build_electra_historical_summaries_proof(
block.slot,
&historical_batch.block_roots,
block,
),
),
};
let body = BlockBody(AlloyBlockBody {
transactions,
ommers: vec![],
withdrawals: Some(Withdrawals::new(withdrawals)),
});

Ok((header_with_proof, body))
}

/// Returns the receipts for a given block number and receipts root.
Expand Down
Loading