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

feat: make more block types generic #12812

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions bin/reth-bench/src/bench/new_payload_fcu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use clap::Parser;
use csv::Writer;
use reth_cli_runner::CliContext;
use reth_node_core::args::BenchmarkArgs;
use reth_primitives::Block;
use reth_primitives::{Block, BlockExt};
use reth_rpc_types_compat::engine::payload::block_to_payload;
use std::time::Instant;
use tracing::{debug, info};
Expand Down Expand Up @@ -75,11 +75,11 @@ impl Command {

while let Some((block, head, safe, finalized)) = receiver.recv().await {
// just put gas used here
let gas_used = block.header.gas_used;
let gas_used = block.gas_used;
let block_number = block.header.number;

let versioned_hashes: Vec<B256> =
block.blob_versioned_hashes().into_iter().copied().collect();
block.body.blob_versioned_hashes().into_iter().copied().collect();
let parent_beacon_block_root = block.parent_beacon_block_root;
let payload = block_to_payload(block);

Expand Down
6 changes: 3 additions & 3 deletions bin/reth-bench/src/bench/new_payload_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use clap::Parser;
use csv::Writer;
use reth_cli_runner::CliContext;
use reth_node_core::args::BenchmarkArgs;
use reth_primitives::Block;
use reth_primitives::{Block, BlockExt};
use reth_rpc_types_compat::engine::payload::block_to_payload;
use std::time::Instant;
use tracing::{debug, info};
Expand Down Expand Up @@ -60,10 +60,10 @@ impl Command {

while let Some(block) = receiver.recv().await {
// just put gas used here
let gas_used = block.header.gas_used;
let gas_used = block.gas_used;

let versioned_hashes: Vec<B256> =
block.blob_versioned_hashes().into_iter().copied().collect();
block.body.blob_versioned_hashes().into_iter().copied().collect();
let parent_beacon_block_root = block.parent_beacon_block_root;
let payload = block_to_payload(block);

Expand Down
8 changes: 4 additions & 4 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use reth_errors::RethResult;
use reth_evm::execute::{BlockExecutorProvider, Executor};
use reth_execution_types::ExecutionOutcome;
use reth_fs_util as fs;
use reth_node_api::{EngineApiMessageVersion, PayloadBuilderAttributes};
use reth_node_api::{BlockTy, EngineApiMessageVersion, PayloadBuilderAttributes};
use reth_node_ethereum::{EthEvmConfig, EthExecutorProvider};
use reth_primitives::{
BlobTransaction, PooledTransactionsElement, SealedBlock, SealedBlockWithSenders, SealedHeader,
Transaction, TransactionSigned,
BlobTransaction, BlockExt, PooledTransactionsElement, SealedBlock, SealedBlockWithSenders,
SealedHeader, Transaction, TransactionSigned,
};
use reth_provider::{
providers::{BlockchainProvider, ProviderNodeTypes},
Expand Down Expand Up @@ -259,7 +259,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {

let senders = block.senders().expect("sender recovery failed");
let block_with_senders =
SealedBlockWithSenders::new(block.clone(), senders).unwrap();
SealedBlockWithSenders::<BlockTy<N>>::new(block.clone(), senders).unwrap();

let db = StateProviderDatabase::new(blockchain_db.latest()?);
let executor =
Expand Down
1 change: 1 addition & 0 deletions bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use reth_execution_types::ExecutionOutcome;
use reth_network::{BlockDownloaderProvider, NetworkHandle};
use reth_network_api::NetworkInfo;
use reth_node_ethereum::EthExecutorProvider;
use reth_primitives::BlockExt;
use reth_provider::{
providers::ProviderNodeTypes, writer::UnifiedStorageWriter, AccountExtReader,
ChainSpecProvider, HashingWriter, HeaderProvider, LatestStateProviderRef, OriginalValuesKnown,
Expand Down
3 changes: 2 additions & 1 deletion bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use reth_evm::execute::{BatchExecutor, BlockExecutorProvider};
use reth_network::{BlockDownloaderProvider, NetworkHandle};
use reth_network_api::NetworkInfo;
use reth_network_p2p::full_block::FullBlockClient;
use reth_node_api::BlockTy;
use reth_node_ethereum::EthExecutorProvider;
use reth_provider::{
providers::ProviderNodeTypes, writer::UnifiedStorageWriter, BlockNumReader, BlockWriter,
Expand Down Expand Up @@ -144,7 +145,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
for block in blocks.into_iter().rev() {
let block_number = block.number;
let sealed_block = block
.try_seal_with_senders()
.try_seal_with_senders::<BlockTy<N>>()
.map_err(|block| eyre::eyre!("Error sealing block with senders: {block:?}"))?;
trace!(target: "reth::cli", block_number, "Executing block");

Expand Down
6 changes: 4 additions & 2 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,9 @@ mod tests {
body: Vec<TransactionSignedEcRecovered>,
num_of_signer_txs: u64|
-> SealedBlockWithSenders {
let transactions_root = calculate_transaction_root(&body);
let signed_body =
body.clone().into_iter().map(|tx| tx.into_signed()).collect::<Vec<_>>();
let transactions_root = calculate_transaction_root(&signed_body);
let receipts = body
.iter()
.enumerate()
Expand Down Expand Up @@ -1635,7 +1637,7 @@ mod tests {
SealedBlock {
header: SealedHeader::seal(header),
body: BlockBody {
transactions: body.clone().into_iter().map(|tx| tx.into_signed()).collect(),
transactions: signed_body,
ommers: Vec::new(),
withdrawals: Some(Withdrawals::default()),
},
Expand Down
30 changes: 23 additions & 7 deletions crates/blockchain-tree/src/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,40 @@ use reth_consensus::Consensus;
use reth_db::{static_file::BlockHashMask, tables};
use reth_db_api::{cursor::DbCursorRO, transaction::DbTx};
use reth_node_types::{FullNodePrimitives, NodeTypesWithDB};
use reth_primitives::{BlockBody, StaticFileSegment};
use reth_primitives::StaticFileSegment;
use reth_provider::{
providers::ProviderNodeTypes, ChainStateBlockReader, ChainStateBlockWriter, ProviderFactory,
StaticFileProviderFactory, StatsReader,
providers::{NodeTypesForProvider, ProviderNodeTypes},
ChainStateBlockReader, ChainStateBlockWriter, ProviderFactory, StaticFileProviderFactory,
StatsReader,
};
use reth_storage_errors::provider::ProviderResult;
use std::{collections::BTreeMap, sync::Arc};

/// A helper trait with requirements for [`ProviderNodeTypes`] to be used within [`TreeExternals`].
pub trait TreeNodeTypes:
ProviderNodeTypes<Primitives: FullNodePrimitives<BlockBody = BlockBody>>
pub trait NodeTypesForTree:
NodeTypesForProvider<
Primitives: FullNodePrimitives<
Block = reth_primitives::Block,
BlockBody = reth_primitives::BlockBody,
>,
>
{
}
impl<T> TreeNodeTypes for T where
T: ProviderNodeTypes<Primitives: FullNodePrimitives<BlockBody = BlockBody>>

impl<T> NodeTypesForTree for T where
T: NodeTypesForProvider<
Primitives: FullNodePrimitives<
Block = reth_primitives::Block,
BlockBody = reth_primitives::BlockBody,
>,
>
{
}

/// A helper trait with requirements for [`ProviderNodeTypes`] to be used within [`TreeExternals`].
pub trait TreeNodeTypes: ProviderNodeTypes + NodeTypesForTree {}
impl<T> TreeNodeTypes for T where T: ProviderNodeTypes + NodeTypesForTree {}

/// A container for external components.
///
/// This is a simple container for external components used throughout the blockchain tree
Expand Down
4 changes: 3 additions & 1 deletion crates/chain-state/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ impl TestBlockBuilder {
gas_limit: self.chain_spec.max_gas_limit,
mix_hash: B256::random(),
base_fee_per_gas: Some(INITIAL_BASE_FEE),
transactions_root: calculate_transaction_root(&transactions),
transactions_root: calculate_transaction_root(
&transactions.clone().into_iter().map(|tx| tx.into_signed()).collect::<Vec<_>>(),
),
receipts_root: calculate_receipt_root(&receipts),
beneficiary: Address::random(),
state_root: state_root_unhashed(HashMap::from([(
Expand Down
14 changes: 12 additions & 2 deletions crates/cli/commands/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,21 @@ impl AccessRights {
/// [`NodeTypes`](reth_node_builder::NodeTypes) in CLI.
pub trait CliNodeTypes:
NodeTypesWithEngine
+ NodeTypesForProvider<Primitives: FullNodePrimitives<BlockBody = reth_primitives::BlockBody>>
+ NodeTypesForProvider<
Primitives: FullNodePrimitives<
Block = reth_primitives::Block,
BlockBody = reth_primitives::BlockBody,
>,
>
{
}
impl<N> CliNodeTypes for N where
N: NodeTypesWithEngine
+ NodeTypesForProvider<Primitives: FullNodePrimitives<BlockBody = reth_primitives::BlockBody>>
+ NodeTypesForProvider<
Primitives: FullNodePrimitives<
Block = reth_primitives::Block,
BlockBody = reth_primitives::BlockBody,
>,
>
{
}
5 changes: 3 additions & 2 deletions crates/cli/commands/src/init_state/without_evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn setup_without_evm<Provider>(
where
Provider: StaticFileProviderFactory
+ StageCheckpointWriter
+ BlockWriter<Body: reth_node_api::BlockBody>,
+ BlockWriter<Block: reth_node_api::Block<Header = reth_primitives::Header>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
+ BlockWriter<Block: reth_node_api::Block<Header = reth_primitives::Header>>,
+ BlockWriter<BlockHeader = reth_primitives::Header>,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BlockWriter doesn't have this AT

{
info!(target: "reth::cli", "Setting up dummy EVM chain before importing state.");

Expand Down Expand Up @@ -64,7 +64,8 @@ fn append_first_block<Provider>(
total_difficulty: U256,
) -> Result<(), eyre::Error>
where
Provider: BlockWriter<Body: reth_node_api::BlockBody> + StaticFileProviderFactory,
Provider: BlockWriter<Block: reth_node_api::Block<Header = reth_primitives::Header>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Provider: BlockWriter<Block: reth_node_api::Block<Header = reth_primitives::Header>>
Provider: BlockWriter<BlockHeader = reth_primitives::Header>

+ StaticFileProviderFactory,
{
provider_rw.insert_block(
SealedBlockWithSenders::new(SealedBlock::new(header.clone(), Default::default()), vec![])
Expand Down
1 change: 1 addition & 0 deletions crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,7 @@ mod tests {
use assert_matches::assert_matches;
use reth_chainspec::{ChainSpecBuilder, MAINNET};
use reth_node_types::FullNodePrimitives;
use reth_primitives::BlockExt;
use reth_provider::{BlockWriter, ProviderFactory, StorageLocation};
use reth_rpc_types_compat::engine::payload::block_to_payload_v1;
use reth_stages::{ExecOutput, PipelineError, StageError};
Expand Down
4 changes: 3 additions & 1 deletion crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use alloy_consensus::{constants::MAXIMUM_EXTRA_DATA_SIZE, Header};
use alloy_eips::eip4844::{DATA_GAS_PER_BLOB, MAX_DATA_GAS_PER_BLOCK};
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_consensus::ConsensusError;
use reth_primitives::{BlockBody, EthereumHardfork, GotExpected, SealedBlock, SealedHeader};
use reth_primitives::{
BlockBody, BlockBodyTxExt, EthereumHardfork, GotExpected, SealedBlock, SealedHeader,
};
use revm_primitives::calc_excess_blob_gas;

/// Gas used needs to be less than gas limit. Gas used is going to be checked after execution.
Expand Down
7 changes: 4 additions & 3 deletions crates/e2e-test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::Arc;
use node::NodeTestContext;
use reth::{
args::{DiscoveryArgs, NetworkArgs, RpcServerArgs},
blockchain_tree::externals::NodeTypesForTree,
builder::{FullNodePrimitives, NodeBuilder, NodeConfig, NodeHandle},
network::PeersHandleProvider,
rpc::server_types::RpcModuleSelection,
Expand Down Expand Up @@ -53,13 +54,12 @@ pub async fn setup<N>(
attributes_generator: impl Fn(u64) -> <<N as NodeTypesWithEngine>::Engine as PayloadTypes>::PayloadBuilderAttributes + Copy + 'static,
) -> eyre::Result<(Vec<NodeHelperType<N, N::AddOns>>, TaskManager, Wallet)>
where
N: Default + Node<TmpNodeAdapter<N>> + NodeTypesForProvider + NodeTypesWithEngine,
N: Default + Node<TmpNodeAdapter<N>> + NodeTypesForTree + NodeTypesWithEngine,
N::ComponentsBuilder: NodeComponentsBuilder<
TmpNodeAdapter<N>,
Components: NodeComponents<TmpNodeAdapter<N>, Network: PeersHandleProvider>,
>,
N::AddOns: RethRpcAddOns<Adapter<N>>,
N::Primitives: FullNodePrimitives<BlockBody = reth_primitives::BlockBody>,
{
let tasks = TaskManager::current();
let exec = tasks.executor();
Expand Down Expand Up @@ -134,7 +134,8 @@ where
LocalPayloadAttributesBuilder<N::ChainSpec>: PayloadAttributesBuilder<
<<N as NodeTypesWithEngine>::Engine as PayloadTypes>::PayloadAttributes,
>,
N::Primitives: FullNodePrimitives<BlockBody = reth_primitives::BlockBody>,
N::Primitives:
FullNodePrimitives<Block = reth_primitives::Block, BlockBody = reth_primitives::BlockBody>,
{
let tasks = TaskManager::current();
let exec = tasks.executor();
Expand Down
2 changes: 1 addition & 1 deletion crates/e2e-test-utils/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ where
// get head block from notifications stream and verify the tx has been pushed to the
// pool is actually present in the canonical block
let head = self.engine_api.canonical_stream.next().await.unwrap();
let tx = head.tip().transactions().next();
let tx = head.tip().transactions().first();
assert_eq!(tx.unwrap().hash().as_slice(), tip_tx_hash.as_slice());

loop {
Expand Down
13 changes: 7 additions & 6 deletions crates/engine/local/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,13 @@ where

let block = payload.block();

let cancun_fields =
self.provider.chain_spec().is_cancun_active_at_timestamp(block.timestamp).then(|| {
CancunPayloadFields {
parent_beacon_block_root: block.parent_beacon_block_root.unwrap(),
versioned_hashes: block.blob_versioned_hashes().into_iter().copied().collect(),
}
let cancun_fields = self
.provider
.chain_spec()
.is_cancun_active_at_timestamp(block.timestamp)
.then(|| CancunPayloadFields {
parent_beacon_block_root: block.parent_beacon_block_root.unwrap(),
versioned_hashes: block.body.blob_versioned_hashes().into_iter().copied().collect(),
});

let (tx, rx) = oneshot::channel();
Expand Down
8 changes: 6 additions & 2 deletions crates/engine/tree/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ use tracing::{debug, error};
/// A helper trait with requirements for [`ProviderNodeTypes`] to be used within
/// [`PersistenceService`].
pub trait PersistenceNodeTypes:
ProviderNodeTypes<Primitives: FullNodePrimitives<BlockBody = BlockBody>>
ProviderNodeTypes<
Primitives: FullNodePrimitives<Block = reth_primitives::Block, BlockBody = BlockBody>,
>
{
}
impl<T> PersistenceNodeTypes for T where
T: ProviderNodeTypes<Primitives: FullNodePrimitives<BlockBody = BlockBody>>
T: ProviderNodeTypes<
Primitives: FullNodePrimitives<Block = reth_primitives::Block, BlockBody = BlockBody>,
>
{
}
/// Writes parts of reth's in memory tree state to the database and static files.
Expand Down
1 change: 1 addition & 0 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2609,6 +2609,7 @@ mod tests {
use reth_engine_primitives::ForkchoiceStatus;
use reth_ethereum_engine_primitives::EthEngineTypes;
use reth_evm::test_utils::MockExecutorProvider;
use reth_primitives::BlockExt;
use reth_provider::test_utils::MockEthProvider;
use reth_rpc_types_compat::engine::{block_to_payload_v1, payload::block_to_payload_v3};
use reth_trie::updates::TrieUpdates;
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/util/src/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use reth_evm::{
ConfigureEvm,
};
use reth_payload_validator::ExecutionPayloadValidator;
use reth_primitives::{proofs, Block, BlockBody, Receipt, Receipts};
use reth_primitives::{proofs, Block, BlockBody, BlockExt, Receipt, Receipts};
use reth_provider::{BlockReader, ExecutionOutcome, ProviderError, StateProviderFactory};
use reth_revm::{
database::StateProviderDatabase,
Expand Down
4 changes: 3 additions & 1 deletion crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ mod tests {
BasicBlockExecutorProvider, BatchExecutor, BlockExecutorProvider, Executor,
};
use reth_execution_types::BlockExecutionOutput;
use reth_primitives::{public_key_to_address, Account, Block, BlockBody, Transaction};
use reth_primitives::{
public_key_to_address, Account, Block, BlockBody, BlockExt, Transaction,
};
use reth_revm::{
database::StateProviderDatabase, test_utils::StateProviderTest, TransitionState,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/node/tests/e2e/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ where

let head = notifications.next().await.unwrap();

let tx = head.tip().transactions().next().unwrap();
let tx = &head.tip().transactions()[0];
assert_eq!(tx.hash(), hash);
println!("mined transaction: {hash}");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use reth_payload_builder_primitives::PayloadBuilderError;
use reth_payload_primitives::PayloadBuilderAttributes;
use reth_primitives::{
proofs::{self},
Block, BlockBody, EthereumHardforks, Receipt,
Block, BlockBody, BlockExt, EthereumHardforks, Receipt,
};
use reth_provider::{ChainSpecProvider, StateProviderFactory};
use reth_revm::database::StateProviderDatabase;
Expand Down
6 changes: 3 additions & 3 deletions crates/evm/execution-types/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl<N: NodePrimitives> Chain<N> {
self.blocks().iter().zip(self.execution_outcome.receipts().iter())
{
let mut tx_receipts = Vec::with_capacity(receipts.len());
for (tx, receipt) in block.body.transactions().zip(receipts.iter()) {
for (tx, receipt) in block.body.transactions.iter().zip(receipts.iter()) {
tx_receipts.push((
tx.hash(),
receipt.as_ref().expect("receipts have not been pruned").clone(),
Expand Down Expand Up @@ -417,7 +417,7 @@ impl ChainBlocks<'_> {
/// Returns an iterator over all transactions in the chain.
#[inline]
pub fn transactions(&self) -> impl Iterator<Item = &TransactionSigned> + '_ {
self.blocks.values().flat_map(|block| block.body.transactions())
self.blocks.values().flat_map(|block| block.body.transactions.iter())
}

/// Returns an iterator over all transactions and their senders.
Expand All @@ -441,7 +441,7 @@ impl ChainBlocks<'_> {
/// Returns an iterator over all transaction hashes in the block
#[inline]
pub fn transaction_hashes(&self) -> impl Iterator<Item = TxHash> + '_ {
self.blocks.values().flat_map(|block| block.transactions().map(|tx| tx.hash()))
self.blocks.values().flat_map(|block| block.transactions().iter().map(|tx| tx.hash()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/exex/exex/src/backfill/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use alloy_primitives::BlockNumber;
use reth_evm::execute::{
BatchExecutor, BlockExecutionError, BlockExecutionOutput, BlockExecutorProvider, Executor,
};
use reth_primitives::{Block, BlockWithSenders, Receipt};
use reth_primitives::{Block, BlockExt, BlockWithSenders, Receipt};
use reth_primitives_traits::format_gas_throughput;
use reth_provider::{
BlockReader, Chain, HeaderProvider, ProviderError, StateProviderFactory, TransactionVariant,
Expand Down
Loading
Loading