feat(breaking): Use HlHeader for HlPrimitives

This commit is contained in:
sprites0
2025-10-06 06:20:47 +00:00
parent 2390ed864a
commit bcdf4d933d
7 changed files with 62 additions and 64 deletions

View File

@ -1,21 +1,21 @@
//! Copy of reth codebase.
use crate::HlBlock;
use alloy_consensus::{BlockHeader, TxReceipt, proofs::calculate_receipt_root};
use alloy_eips::eip7685::Requests;
use alloy_primitives::{B256, Bloom};
use reth::consensus::ConsensusError;
use reth_chainspec::EthereumHardforks;
use reth_primitives::{GotExpected, RecoveredBlock, gas_spent_by_transactions};
use reth_primitives_traits::{Block, Receipt as ReceiptTrait};
use reth_primitives_traits::Receipt as ReceiptTrait;
pub fn validate_block_post_execution<B, R, ChainSpec>(
block: &RecoveredBlock<B>,
pub fn validate_block_post_execution<R, ChainSpec>(
block: &RecoveredBlock<HlBlock>,
chain_spec: &ChainSpec,
receipts: &[R],
requests: &Requests,
) -> Result<(), ConsensusError>
where
B: Block,
R: ReceiptTrait,
ChainSpec: EthereumHardforks,
{
@ -42,7 +42,7 @@ where
receipts.iter().filter(|&r| r.cumulative_gas_used() != 0).cloned().collect::<Vec<_>>();
if let Err(error) = verify_receipts(
block.header().receipts_root(),
block.header().logs_bloom(),
block.header().inner.logs_bloom(),
&receipts_for_root,
) {
tracing::debug!(%error, ?receipts, "receipts verification failed");

View File

@ -1,11 +1,15 @@
use super::{executor::HlBlockExecutor, factory::HlEvmFactory};
use crate::{
chainspec::HlChainSpec, evm::{spec::HlSpecId, transaction::HlTxEnv}, hardforks::HlHardforks, node::{
HlBlock, HlBlockBody, HlHeader, HlPrimitives,
chainspec::HlChainSpec,
evm::{spec::HlSpecId, transaction::HlTxEnv},
hardforks::HlHardforks,
node::{
evm::{executor::is_system_transaction, receipt_builder::RethReceiptBuilder},
primitives::{BlockBody, TransactionSigned},
rpc::engine_api::validator::HlExecutionData,
types::HlExtras,
}, HlBlock, HlBlockBody, HlHeader, HlPrimitives
},
};
use alloy_consensus::{BlockHeader, EMPTY_OMMER_ROOT_HASH, Header, Transaction as _, TxReceipt};
use alloy_eips::{Encodable2718, merge::BEACON_NONCE};
@ -132,7 +136,9 @@ where
excess_blob_gas,
requests_hash,
};
let header = HlHeader::from_ethereum_header(header, receipts);
let system_tx_count =
transactions.iter().filter(|t| is_system_transaction(t)).count() as u64;
let header = HlHeader::from_ethereum_header(header, receipts, system_tx_count);
Ok(Self::Block {
header,

View File

@ -9,8 +9,6 @@ use reth_primitives_traits::{BlockHeader, InMemorySize, serde_bincode_compat::Rl
use reth_rpc_convert::transaction::FromConsensusHeader;
use serde::{Deserialize, Serialize};
use crate::node::types::HlExtras;
/// The header type of this node
///
/// This type extends the regular ethereum header with an extension.
@ -33,22 +31,25 @@ pub struct HlHeader {
/// The regular eth header
#[as_ref]
#[deref]
#[serde(flatten)]
pub inner: Header,
/// The extended header fields that is not part of the block hash
pub extras: HlHeaderExtras,
}
#[derive(
Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, RlpEncodable, RlpDecodable, Hash,
)]
pub struct HlHeaderExtras {
pub logs_bloom_with_system_txs: Bloom,
pub system_tx_count: u64,
pub read_precompile_calls: HlExtras,
}
impl HlHeader {
pub(crate) fn from_ethereum_header(header: Header, receipts: &[EthereumReceipt]) -> HlHeader {
pub(crate) fn from_ethereum_header(header: Header, receipts: &[EthereumReceipt], system_tx_count: u64) -> HlHeader {
let logs_bloom = logs_bloom(receipts.iter().flat_map(|r| &r.logs));
let system_tx_count = receipts.iter().filter(|r| r.cumulative_gas_used == 0).count() as u64;
HlHeader {
inner: header,
logs_bloom_with_system_txs: logs_bloom,
system_tx_count,
read_precompile_calls: Default::default(),
extras: HlHeaderExtras { logs_bloom_with_system_txs: logs_bloom, system_tx_count },
}
}
}
@ -101,7 +102,7 @@ impl alloy_consensus::BlockHeader for HlHeader {
}
fn logs_bloom(&self) -> Bloom {
self.inner.logs_bloom()
self.extras.logs_bloom_with_system_txs
}
fn difficulty(&self) -> U256 {
@ -155,14 +156,21 @@ impl alloy_consensus::BlockHeader for HlHeader {
fn extra_data(&self) -> &Bytes {
self.inner.extra_data()
}
fn is_empty(&self) -> bool {
self.extras.system_tx_count == 0 && self.inner.is_empty()
}
}
impl InMemorySize for HlHeader {
fn size(&self) -> usize {
self.inner.size()
+ self.logs_bloom_with_system_txs.data().len()
+ self.system_tx_count.size()
+ self.read_precompile_calls.size()
self.inner.size() + self.extras.size()
}
}
impl InMemorySize for HlHeaderExtras {
fn size(&self) -> usize {
self.logs_bloom_with_system_txs.data().len() + self.system_tx_count.size()
}
}
@ -171,15 +179,20 @@ impl reth_codecs::Compact for HlHeader {
where
B: alloy_rlp::bytes::BufMut + AsMut<[u8]>,
{
// It's too tedious to implement to_compact for every field, so use rmp_serde to serialize
// This also helps the struct to be upgradable in future thanks to serde's flexibility.
// Because Header ends with extra_data which is `Bytes`, we can't use to_compact for extras,
// because Compact trait requires the Bytes field to be placed at the end of the struct.
// Bytes::from_compact just reads all trailing data as the Bytes field.
//
// Hence we need to use other form of serialization, since extra headers are not Compact-compatible.
// We just treat all header fields as rmp-serialized one `Bytes` field.
let result: Bytes = rmp_serde::to_vec(&self).unwrap().into();
result.to_compact(buf)
}
fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8]) {
let header: HlHeader = rmp_serde::from_slice(&buf[identifier..]).unwrap();
(header, &buf[identifier + buf.len()..])
fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
let (bytes, remaining) = Bytes::from_compact(buf, len);
let header: HlHeader = rmp_serde::from_slice(&bytes).unwrap();
(header, remaining)
}
}

View File

@ -120,6 +120,11 @@ impl SealedBlock {
let mut merged_txs = vec![];
merged_txs.extend(system_txs.iter().map(|tx| system_tx_to_reth_transaction(tx, chain_id)));
merged_txs.extend(self.body.transactions.iter().map(|tx| tx.to_reth_transaction()));
let mut merged_receipts = vec![];
merged_receipts.extend(system_txs.iter().map(|tx| tx.receipt.clone().unwrap().into()));
merged_receipts.extend(receipts.into_iter().map(From::from));
let block_body = HlBlockBody {
inner: reth_primitives::BlockBody {
transactions: merged_txs,
@ -131,10 +136,12 @@ impl SealedBlock {
highest_precompile_address,
};
let system_tx_count = system_txs.len() as u64;
HlBlock {
header: HlHeader::from_ethereum_header(
self.header.header.clone(),
&receipts.into_iter().map(From::from).collect::<Vec<_>>(),
&merged_receipts,
system_tx_count,
),
body: block_body,
}