Skip to content

Commit

Permalink
feat: add Block AT to BlockReader (paradigmxyz#12837)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Nov 25, 2024
1 parent e2c42ae commit c44e11b
Show file tree
Hide file tree
Showing 69 changed files with 664 additions and 267 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions book/sources/exex/hello-world/src/bin/3.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use futures_util::TryStreamExt;
use reth::api::FullNodeComponents;
use reth::{api::FullNodeComponents, primitives::Block, providers::BlockReader};
use reth_exex::{ExExContext, ExExEvent, ExExNotification};
use reth_node_ethereum::EthereumNode;
use reth_tracing::tracing::info;

async fn my_exex<Node: FullNodeComponents>(mut ctx: ExExContext<Node>) -> eyre::Result<()> {
async fn my_exex<Node: FullNodeComponents<Provider: BlockReader<Block = Block>>>(
mut ctx: ExExContext<Node>,
) -> eyre::Result<()> {
while let Some(notification) = ctx.notifications.try_next().await? {
match &notification {
ExExNotification::ChainCommitted { new } => {
Expand Down
3 changes: 2 additions & 1 deletion book/sources/exex/remote/src/exex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use remote_exex::proto::{
self,
remote_ex_ex_server::{RemoteExEx, RemoteExExServer},
};
use reth::{primitives::Block, providers::BlockReader};
use reth_exex::{ExExContext, ExExEvent, ExExNotification};
use reth_node_api::FullNodeComponents;
use reth_node_ethereum::EthereumNode;
Expand Down Expand Up @@ -44,7 +45,7 @@ impl RemoteExEx for ExExService {
}
}

async fn remote_exex<Node: FullNodeComponents>(
async fn remote_exex<Node: FullNodeComponents<Provider: BlockReader<Block = Block>>>(
mut ctx: ExExContext<Node>,
notifications: Arc<broadcast::Sender<ExExNotification>>,
) -> eyre::Result<()> {
Expand Down
3 changes: 2 additions & 1 deletion book/sources/exex/remote/src/exex_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use remote_exex::proto::{
self,
remote_ex_ex_server::{RemoteExEx, RemoteExExServer},
};
use reth::{primitives::Block, providers::BlockReader};
use reth_exex::{ExExContext, ExExEvent, ExExNotification};
use reth_node_api::FullNodeComponents;
use reth_node_ethereum::EthereumNode;
Expand Down Expand Up @@ -46,7 +47,7 @@ impl RemoteExEx for ExExService {

// ANCHOR: snippet
#[allow(dead_code)]
async fn remote_exex<Node: FullNodeComponents>(
async fn remote_exex<Node: FullNodeComponents<Provider: BlockReader<Block = Block>>>(
mut ctx: ExExContext<Node>,
notifications: Arc<broadcast::Sender<ExExNotification>>,
) -> eyre::Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions book/sources/exex/tracking-state/src/bin/1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use futures_util::{FutureExt, TryStreamExt};
use reth::api::FullNodeComponents;
use reth::{api::FullNodeComponents, primitives::Block, providers::BlockReader};
use reth_exex::{ExExContext, ExExEvent, ExExNotification};
use reth_node_ethereum::EthereumNode;
use reth_tracing::tracing::info;
Expand All @@ -14,7 +14,7 @@ struct MyExEx<Node: FullNodeComponents> {
ctx: ExExContext<Node>,
}

impl<Node: FullNodeComponents> Future for MyExEx<Node> {
impl<Node: FullNodeComponents<Provider: BlockReader<Block = Block>>> Future for MyExEx<Node> {
type Output = eyre::Result<()>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Expand Down
4 changes: 2 additions & 2 deletions book/sources/exex/tracking-state/src/bin/2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use alloy_primitives::BlockNumber;
use futures_util::{FutureExt, TryStreamExt};
use reth::api::FullNodeComponents;
use reth::{api::FullNodeComponents, primitives::Block, providers::BlockReader};
use reth_exex::{ExExContext, ExExEvent};
use reth_node_ethereum::EthereumNode;
use reth_tracing::tracing::info;
Expand All @@ -25,7 +25,7 @@ impl<Node: FullNodeComponents> MyExEx<Node> {
}
}

impl<Node: FullNodeComponents> Future for MyExEx<Node> {
impl<Node: FullNodeComponents<Provider: BlockReader<Block = Block>>> Future for MyExEx<Node> {
type Output = eyre::Result<()>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Expand Down
14 changes: 7 additions & 7 deletions crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_consensus::Header;
use alloy_consensus::{BlockHeader, Header};
use alloy_eips::{merge::EPOCH_SLOTS, BlockNumHash};
use alloy_primitives::{BlockNumber, B256};
use alloy_rpc_types_engine::{
Expand All @@ -21,7 +21,7 @@ use reth_network_p2p::{
sync::{NetworkSyncUpdater, SyncState},
EthBlockClient,
};
use reth_node_types::NodeTypesWithEngine;
use reth_node_types::{Block, BlockTy, NodeTypesWithEngine};
use reth_payload_builder::PayloadBuilderHandle;
use reth_payload_builder_primitives::PayloadBuilder;
use reth_payload_primitives::{PayloadAttributes, PayloadBuilderAttributes};
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<N, BT, Client> BeaconConsensusEngine<N, BT, Client>
where
N: EngineNodeTypes,
BT: BlockchainTreeEngine
+ BlockReader
+ BlockReader<Block = BlockTy<N>>
+ BlockIdReader
+ CanonChainTracker
+ StageCheckpointReader
Expand Down Expand Up @@ -946,7 +946,7 @@ where
.blockchain
.find_block_by_hash(safe_block_hash, BlockSource::Any)?
.ok_or(ProviderError::UnknownBlockHash(safe_block_hash))?;
self.blockchain.set_safe(SealedHeader::new(safe.header, safe_block_hash));
self.blockchain.set_safe(SealedHeader::new(safe.split().0, safe_block_hash));
}
Ok(())
}
Expand All @@ -966,9 +966,9 @@ where
.blockchain
.find_block_by_hash(finalized_block_hash, BlockSource::Any)?
.ok_or(ProviderError::UnknownBlockHash(finalized_block_hash))?;
self.blockchain.finalize_block(finalized.number)?;
self.blockchain.finalize_block(finalized.header().number())?;
self.blockchain
.set_finalized(SealedHeader::new(finalized.header, finalized_block_hash));
.set_finalized(SealedHeader::new(finalized.split().0, finalized_block_hash));
}
Ok(())
}
Expand Down Expand Up @@ -1798,7 +1798,7 @@ where
N: EngineNodeTypes,
Client: EthBlockClient + 'static,
BT: BlockchainTreeEngine
+ BlockReader
+ BlockReader<Block = BlockTy<N>>
+ BlockIdReader
+ CanonChainTracker
+ StageCheckpointReader
Expand Down
1 change: 1 addition & 0 deletions crates/e2e-test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ url.workspace = true
# ethereum
alloy-primitives.workspace = true
alloy-eips.workspace = true
alloy-rlp.workspace = true
op-alloy-rpc-types-engine.workspace = true

futures-util.workspace = true
Expand Down
14 changes: 10 additions & 4 deletions crates/e2e-test-utils/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{marker::PhantomData, pin::Pin};

use alloy_consensus::{BlockHeader, Sealable};
use alloy_primitives::{BlockHash, BlockNumber, Bytes, B256};
use alloy_rpc_types_eth::BlockNumberOrTag;
use eyre::Ok;
Expand All @@ -15,6 +16,7 @@ use reth::{
},
};
use reth_chainspec::EthereumHardforks;
use reth_node_api::{Block, FullBlock, NodePrimitives};
use reth_node_builder::{rpc::RethRpcAddOns, NodeTypes, NodeTypesWithEngine};
use reth_stages_types::StageId;
use tokio_stream::StreamExt;
Expand Down Expand Up @@ -51,7 +53,11 @@ impl<Node, Engine, AddOns> NodeTestContext<Node, AddOns>
where
Engine: EngineTypes,
Node: FullNodeComponents,
Node::Types: NodeTypesWithEngine<ChainSpec: EthereumHardforks, Engine = Engine>,
Node::Types: NodeTypesWithEngine<
ChainSpec: EthereumHardforks,
Engine = Engine,
Primitives: NodePrimitives<Block: FullBlock>,
>,
Node::Network: PeersHandleProvider,
AddOns: RethRpcAddOns<Node>,
{
Expand Down Expand Up @@ -178,7 +184,7 @@ where

if check {
if let Some(latest_block) = self.inner.provider.block_by_number(number)? {
assert_eq!(latest_block.hash_slow(), expected_block_hash);
assert_eq!(latest_block.header().hash_slow(), expected_block_hash);
break
}
assert!(
Expand Down Expand Up @@ -225,10 +231,10 @@ where
if let Some(latest_block) =
self.inner.provider.block_by_number_or_tag(BlockNumberOrTag::Latest)?
{
if latest_block.number == block_number {
if latest_block.header().number() == block_number {
// make sure the block hash we submitted via FCU engine api is the new latest
// block using an RPC call
assert_eq!(latest_block.hash_slow(), block_hash);
assert_eq!(latest_block.header().hash_slow(), block_hash);
break
}
}
Expand Down
9 changes: 8 additions & 1 deletion crates/e2e-test-utils/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloy_consensus::TxEnvelope;
use alloy_network::eip2718::Decodable2718;
use alloy_primitives::{Bytes, B256};
use alloy_rlp::Encodable;
use reth::{
builder::{rpc::RpcRegistry, FullNodeComponents},
rpc::api::{
Expand All @@ -12,6 +13,7 @@ use reth::{
},
};
use reth_chainspec::EthereumHardforks;
use reth_node_api::NodePrimitives;
use reth_node_builder::NodeTypes;

#[allow(missing_debug_implementations)]
Expand All @@ -21,7 +23,12 @@ pub struct RpcTestContext<Node: FullNodeComponents, EthApi: EthApiTypes> {

impl<Node, EthApi> RpcTestContext<Node, EthApi>
where
Node: FullNodeComponents<Types: NodeTypes<ChainSpec: EthereumHardforks>>,
Node: FullNodeComponents<
Types: NodeTypes<
ChainSpec: EthereumHardforks,
Primitives: NodePrimitives<Block: Encodable>,
>,
>,
EthApi: EthApiSpec + EthTransactions + TraceExt,
{
/// Injects a raw transaction into the node tx pool via RPC server
Expand Down
13 changes: 9 additions & 4 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
engine::{DownloadRequest, EngineApiEvent, FromEngine},
persistence::PersistenceHandle,
};
use alloy_consensus::Header;
use alloy_consensus::{BlockHeader, Header};
use alloy_eips::BlockNumHash;
use alloy_primitives::{
map::{HashMap, HashSet},
Expand Down Expand Up @@ -536,7 +536,12 @@ impl<P: Debug, E: Debug, T: EngineTypes + Debug, Spec: Debug> std::fmt::Debug

impl<P, E, T, Spec> EngineApiTreeHandler<P, E, T, Spec>
where
P: DatabaseProviderFactory + BlockReader + StateProviderFactory + StateReader + Clone + 'static,
P: DatabaseProviderFactory
+ BlockReader<Block = reth_primitives::Block>
+ StateProviderFactory
+ StateReader
+ Clone
+ 'static,
<P as DatabaseProviderFactory>::Provider: BlockReader,
E: BlockExecutorProvider,
T: EngineTypes,
Expand Down Expand Up @@ -1539,8 +1544,8 @@ where
.ok_or_else(|| ProviderError::HeaderNotFound(hash.into()))?;
let execution_output = self
.provider
.get_state(block.number)?
.ok_or_else(|| ProviderError::StateForNumberNotFound(block.number))?;
.get_state(block.number())?
.ok_or_else(|| ProviderError::StateForNumberNotFound(block.number()))?;
let hashed_state = execution_output.hash_state_slow();

Ok(Some(ExecutedBlock {
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/util/src/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<S, Engine, Provider, Evm, Spec> Stream for EngineReorg<S, Engine, Provider,
where
S: Stream<Item = BeaconEngineMessage<Engine>>,
Engine: EngineTypes,
Provider: BlockReader + StateProviderFactory,
Provider: BlockReader<Block = reth_primitives::Block> + StateProviderFactory,
Evm: ConfigureEvm<Header = Header>,
Spec: EthereumHardforks,
{
Expand Down Expand Up @@ -256,7 +256,7 @@ fn create_reorg_head<Provider, Evm, Spec>(
next_sidecar: ExecutionPayloadSidecar,
) -> RethResult<(ExecutionPayload, ExecutionPayloadSidecar)>
where
Provider: BlockReader + StateProviderFactory,
Provider: BlockReader<Block = reth_primitives::Block> + StateProviderFactory,
Evm: ConfigureEvm<Header = Header>,
Spec: EthereumHardforks,
{
Expand Down
4 changes: 2 additions & 2 deletions crates/ethereum/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl EthereumNode {
EthereumConsensusBuilder,
>
where
Node: FullNodeTypes<Types: NodeTypes<ChainSpec = ChainSpec>>,
Node: FullNodeTypes<Types: NodeTypes<ChainSpec = ChainSpec, Primitives = EthPrimitives>>,
<Node::Types as NodeTypesWithEngine>::Engine: PayloadTypes<
BuiltPayload = EthBuiltPayload,
PayloadAttributes = EthPayloadAttributes,
Expand Down Expand Up @@ -304,7 +304,7 @@ pub struct EthereumNetworkBuilder {

impl<Node, Pool> NetworkBuilder<Node, Pool> for EthereumNetworkBuilder
where
Node: FullNodeTypes<Types: NodeTypes<ChainSpec = ChainSpec>>,
Node: FullNodeTypes<Types: NodeTypes<ChainSpec = ChainSpec, Primitives = EthPrimitives>>,
Pool: TransactionPool + Unpin + 'static,
{
async fn build_network(
Expand Down
1 change: 1 addition & 0 deletions crates/exex/exex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ reth-tasks.workspace = true
reth-tracing.workspace = true

# alloy
alloy-consensus.workspace = true
alloy-primitives.workspace = true
alloy-eips.workspace = true

Expand Down
Loading

0 comments on commit c44e11b

Please sign in to comment.