Skip to content

Commit

Permalink
feat: integrate type specific engine validator in tree service (#12952)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 28, 2024
1 parent 55b758a commit cca6372
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 63 deletions.
4 changes: 1 addition & 3 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion crates/e2e-test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use reth_chainspec::EthChainSpec;
use reth_db::{test_utils::TempDatabase, DatabaseEnv};
use reth_engine_local::LocalPayloadAttributesBuilder;
use reth_network_api::test_utils::PeersHandleProvider;
use reth_node_api::EngineValidator;
use reth_node_builder::{
components::NodeComponentsBuilder,
rpc::{EngineValidatorAddOn, RethRpcAddOns},
Expand Down Expand Up @@ -131,7 +132,10 @@ where
>,
>,
N::AddOns: RethRpcAddOns<Adapter<N, BlockchainProvider2<NodeTypesWithDBAdapter<N, TmpDB>>>>
+ EngineValidatorAddOn<Adapter<N, BlockchainProvider2<NodeTypesWithDBAdapter<N, TmpDB>>>>,
+ EngineValidatorAddOn<
Adapter<N, BlockchainProvider2<NodeTypesWithDBAdapter<N, TmpDB>>>,
Validator: EngineValidator<N::Engine, Block = reth_primitives::Block>,
>,
LocalPayloadAttributesBuilder<N::ChainSpec>: PayloadAttributesBuilder<
<<N as NodeTypesWithEngine>::Engine as PayloadTypes>::PayloadAttributes,
>,
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/local/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ reth-consensus.workspace = true
reth-engine-primitives.workspace = true
reth-engine-service.workspace = true
reth-engine-tree.workspace = true
reth-node-types.workspace = true
reth-evm.workspace = true
reth-ethereum-engine-primitives.workspace = true
reth-payload-builder.workspace = true
reth-payload-builder-primitives.workspace = true
reth-payload-primitives.workspace = true
reth-payload-validator.workspace = true
reth-provider.workspace = true
reth-prune.workspace = true
reth-rpc-types-compat.workspace = true
Expand Down
10 changes: 5 additions & 5 deletions crates/engine/local/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use futures_util::{Stream, StreamExt};
use reth_beacon_consensus::{BeaconConsensusEngineEvent, EngineNodeTypes};
use reth_chainspec::EthChainSpec;
use reth_consensus::Consensus;
use reth_engine_primitives::BeaconEngineMessage;
use reth_engine_primitives::{BeaconEngineMessage, EngineValidator};
use reth_engine_service::service::EngineMessageStream;
use reth_engine_tree::{
chain::{ChainEvent, HandlerEvent},
Expand All @@ -31,9 +31,9 @@ use reth_engine_tree::{
tree::{EngineApiTreeHandler, InvalidBlockHook, TreeConfig},
};
use reth_evm::execute::BlockExecutorProvider;
use reth_node_types::BlockTy;
use reth_payload_builder::PayloadBuilderHandle;
use reth_payload_primitives::{PayloadAttributesBuilder, PayloadTypes};
use reth_payload_validator::ExecutionPayloadValidator;
use reth_provider::{providers::BlockchainProvider2, ChainSpecProvider, ProviderFactory};
use reth_prune::PrunerWithFactory;
use reth_stages_api::MetricEventsSender;
Expand Down Expand Up @@ -63,13 +63,14 @@ where
{
/// Constructor for [`LocalEngineService`].
#[allow(clippy::too_many_arguments)]
pub fn new<B>(
pub fn new<B, V>(
consensus: Arc<dyn Consensus>,
executor_factory: impl BlockExecutorProvider,
provider: ProviderFactory<N>,
blockchain_db: BlockchainProvider2<N>,
pruner: PrunerWithFactory<ProviderFactory<N>>,
payload_builder: PayloadBuilderHandle<N::Engine>,
payload_validator: V,
tree_config: TreeConfig,
invalid_block_hook: Box<dyn InvalidBlockHook>,
sync_metrics_tx: MetricEventsSender,
Expand All @@ -80,15 +81,14 @@ where
) -> Self
where
B: PayloadAttributesBuilder<<N::Engine as PayloadTypes>::PayloadAttributes>,
V: EngineValidator<N::Engine, Block = BlockTy<N>>,
{
let chain_spec = provider.chain_spec();
let engine_kind =
if chain_spec.is_optimism() { EngineApiKind::OpStack } else { EngineApiKind::Ethereum };

let persistence_handle =
PersistenceHandle::spawn_service(provider, pruner, sync_metrics_tx);
let payload_validator = ExecutionPayloadValidator::new(chain_spec);

let canonical_in_memory_state = blockchain_db.canonical_in_memory_state();

let (to_tree_tx, from_tree) = EngineApiTreeHandler::<N::Primitives, _, _, _, _>::spawn_new(
Expand Down
1 change: 0 additions & 1 deletion crates/engine/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ reth-engine-tree.workspace = true
reth-evm.workspace = true
reth-network-p2p.workspace = true
reth-payload-builder.workspace = true
reth-payload-validator.workspace = true
reth-provider.workspace = true
reth-prune.workspace = true
reth-stages-api.workspace = true
Expand Down
19 changes: 11 additions & 8 deletions crates/engine/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pin_project::pin_project;
use reth_beacon_consensus::{BeaconConsensusEngineEvent, EngineNodeTypes};
use reth_chainspec::EthChainSpec;
use reth_consensus::Consensus;
use reth_engine_primitives::BeaconEngineMessage;
use reth_engine_primitives::{BeaconEngineMessage, EngineValidator};
use reth_engine_tree::{
backfill::PipelineSync,
download::BasicBlockDownloader,
Expand All @@ -17,9 +17,8 @@ pub use reth_engine_tree::{
};
use reth_evm::execute::BlockExecutorProvider;
use reth_network_p2p::EthBlockClient;
use reth_node_types::NodeTypesWithEngine;
use reth_node_types::{BlockTy, NodeTypesWithEngine};
use reth_payload_builder::PayloadBuilderHandle;
use reth_payload_validator::ExecutionPayloadValidator;
use reth_provider::{providers::BlockchainProvider2, ProviderFactory};
use reth_prune::PrunerWithFactory;
use reth_stages_api::{MetricEventsSender, Pipeline};
Expand Down Expand Up @@ -65,7 +64,7 @@ where
{
/// Constructor for `EngineService`.
#[allow(clippy::too_many_arguments)]
pub fn new(
pub fn new<V>(
consensus: Arc<dyn Consensus>,
executor_factory: E,
chain_spec: Arc<N::ChainSpec>,
Expand All @@ -77,18 +76,21 @@ where
blockchain_db: BlockchainProvider2<N>,
pruner: PrunerWithFactory<ProviderFactory<N>>,
payload_builder: PayloadBuilderHandle<N::Engine>,
payload_validator: V,
tree_config: TreeConfig,
invalid_block_hook: Box<dyn InvalidBlockHook>,
sync_metrics_tx: MetricEventsSender,
) -> Self {
) -> Self
where
V: EngineValidator<N::Engine, Block = BlockTy<N>>,
{
let engine_kind =
if chain_spec.is_optimism() { EngineApiKind::OpStack } else { EngineApiKind::Ethereum };

let downloader = BasicBlockDownloader::new(client, consensus.clone());

let persistence_handle =
PersistenceHandle::spawn_service(provider, pruner, sync_metrics_tx);
let payload_validator = ExecutionPayloadValidator::new(chain_spec);

let canonical_in_memory_state = blockchain_db.canonical_in_memory_state();

Expand Down Expand Up @@ -148,7 +150,7 @@ mod tests {
use reth_chainspec::{ChainSpecBuilder, MAINNET};
use reth_engine_primitives::BeaconEngineMessage;
use reth_engine_tree::{test_utils::TestPipelineBuilder, tree::NoopInvalidBlockHook};
use reth_ethereum_engine_primitives::EthEngineTypes;
use reth_ethereum_engine_primitives::{EthEngineTypes, EthereumEngineValidator};
use reth_evm_ethereum::execute::EthExecutorProvider;
use reth_exex_types::FinishedExExHeight;
use reth_network_p2p::test_utils::TestFullBlockClient;
Expand Down Expand Up @@ -186,7 +188,7 @@ mod tests {
let blockchain_db =
BlockchainProvider2::with_latest(provider_factory.clone(), SealedHeader::default())
.unwrap();

let engine_payload_validator = EthereumEngineValidator::new(chain_spec.clone());
let (_tx, rx) = watch::channel(FinishedExExHeight::NoExExs);
let pruner = Pruner::new_with_factory(provider_factory.clone(), vec![], 0, 0, None, rx);

Expand All @@ -204,6 +206,7 @@ mod tests {
blockchain_db,
pruner,
PayloadBuilderHandle::new(tx),
engine_payload_validator,
TreeConfig::default(),
Box::new(NoopInvalidBlockHook::default()),
sync_metrics_tx,
Expand Down
3 changes: 1 addition & 2 deletions crates/engine/tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ reth-beacon-consensus.workspace = true
reth-blockchain-tree-api.workspace = true
reth-blockchain-tree.workspace = true
reth-chain-state.workspace = true
reth-chainspec.workspace = true
reth-chainspec = { workspace = true, optional = true }
reth-consensus.workspace = true
reth-engine-primitives.workspace = true
reth-errors.workspace = true
Expand All @@ -25,7 +25,6 @@ reth-network-p2p.workspace = true
reth-payload-builder-primitives.workspace = true
reth-payload-builder.workspace = true
reth-payload-primitives.workspace = true
reth-payload-validator.workspace = true
reth-primitives.workspace = true
reth-provider.workspace = true
reth-prune.workspace = true
Expand Down
36 changes: 17 additions & 19 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{
backfill::{BackfillAction, BackfillSyncState},
chain::FromOrchestrator,
engine::{DownloadRequest, EngineApiEvent, FromEngine},
engine::{DownloadRequest, EngineApiEvent, EngineApiKind, EngineApiRequest, FromEngine},
persistence::PersistenceHandle,
tree::metrics::EngineApiMetrics,
};
use alloy_consensus::{BlockHeader, Header};
use alloy_eips::BlockNumHash;
Expand All @@ -24,18 +25,16 @@ use reth_blockchain_tree::{
use reth_chain_state::{
CanonicalInMemoryState, ExecutedBlock, MemoryOverlayStateProvider, NewCanonicalChain,
};
use reth_chainspec::EthereumHardforks;
use reth_consensus::{Consensus, PostExecutionInput};
use reth_engine_primitives::{
BeaconEngineMessage, BeaconOnNewPayloadError, EngineApiMessageVersion, EngineTypes,
ForkchoiceStateTracker, OnForkChoiceUpdated,
EngineValidator, ForkchoiceStateTracker, OnForkChoiceUpdated,
};
use reth_errors::{ConsensusError, ProviderResult};
use reth_evm::execute::BlockExecutorProvider;
use reth_payload_builder::PayloadBuilderHandle;
use reth_payload_builder_primitives::PayloadBuilder;
use reth_payload_primitives::{PayloadAttributes, PayloadBuilderAttributes};
use reth_payload_validator::ExecutionPayloadValidator;
use reth_primitives::{
Block, EthPrimitives, GotExpected, NodePrimitives, SealedBlock, SealedBlockWithSenders,
SealedHeader,
Expand Down Expand Up @@ -72,10 +71,6 @@ pub mod config;
mod invalid_block_hook;
mod metrics;
mod persistence_state;
use crate::{
engine::{EngineApiKind, EngineApiRequest},
tree::metrics::EngineApiMetrics,
};
pub use config::TreeConfig;
pub use invalid_block_hook::{InvalidBlockHooks, NoopInvalidBlockHook};
pub use persistence_state::PersistenceState;
Expand Down Expand Up @@ -472,11 +467,14 @@ pub enum TreeAction {
///
/// This type is responsible for processing engine API requests, maintaining the canonical state and
/// emitting events.
pub struct EngineApiTreeHandler<N, P, E, T: EngineTypes, Spec> {
pub struct EngineApiTreeHandler<N, P, E, T, V>
where
T: EngineTypes,
{
provider: P,
executor_provider: E,
consensus: Arc<dyn Consensus>,
payload_validator: ExecutionPayloadValidator<Spec>,
payload_validator: V,
/// Keeps track of internals such as executed and buffered blocks.
state: EngineApiTreeState,
/// The half for sending messages to the engine.
Expand Down Expand Up @@ -516,8 +514,8 @@ pub struct EngineApiTreeHandler<N, P, E, T: EngineTypes, Spec> {
_primtives: PhantomData<N>,
}

impl<N, P: Debug, E: Debug, T: EngineTypes + Debug, Spec: Debug> std::fmt::Debug
for EngineApiTreeHandler<N, P, E, T, Spec>
impl<N, P: Debug, E: Debug, T: EngineTypes + Debug, V: Debug> std::fmt::Debug
for EngineApiTreeHandler<N, P, E, T, V>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EngineApiTreeHandler")
Expand All @@ -540,7 +538,7 @@ impl<N, P: Debug, E: Debug, T: EngineTypes + Debug, Spec: Debug> std::fmt::Debug
}
}

impl<N, P, E, T, Spec> EngineApiTreeHandler<N, P, E, T, Spec>
impl<N, P, E, T, V> EngineApiTreeHandler<N, P, E, T, V>
where
N: NodePrimitives,
P: DatabaseProviderFactory
Expand All @@ -552,15 +550,15 @@ where
<P as DatabaseProviderFactory>::Provider: BlockReader,
E: BlockExecutorProvider,
T: EngineTypes,
Spec: Send + Sync + EthereumHardforks + 'static,
V: EngineValidator<T, Block = reth_primitives::Block>,
{
/// Creates a new [`EngineApiTreeHandler`].
#[allow(clippy::too_many_arguments)]
pub fn new(
provider: P,
executor_provider: E,
consensus: Arc<dyn Consensus>,
payload_validator: ExecutionPayloadValidator<Spec>,
payload_validator: V,
outgoing: UnboundedSender<EngineApiEvent>,
state: EngineApiTreeState,
canonical_in_memory_state: CanonicalInMemoryState,
Expand Down Expand Up @@ -609,7 +607,7 @@ where
provider: P,
executor_provider: E,
consensus: Arc<dyn Consensus>,
payload_validator: ExecutionPayloadValidator<Spec>,
payload_validator: V,
persistence: PersistenceHandle,
payload_builder: PayloadBuilderHandle<T>,
canonical_in_memory_state: CanonicalInMemoryState,
Expand Down Expand Up @@ -2629,7 +2627,7 @@ mod tests {
use reth_chain_state::{test_utils::TestBlockBuilder, BlockState};
use reth_chainspec::{ChainSpec, HOLESKY, MAINNET};
use reth_engine_primitives::ForkchoiceStatus;
use reth_ethereum_engine_primitives::EthEngineTypes;
use reth_ethereum_engine_primitives::{EthEngineTypes, EthereumEngineValidator};
use reth_evm::test_utils::MockExecutorProvider;
use reth_primitives::{BlockExt, EthPrimitives};
use reth_provider::test_utils::MockEthProvider;
Expand Down Expand Up @@ -2701,7 +2699,7 @@ mod tests {
MockEthProvider,
MockExecutorProvider,
EthEngineTypes,
ChainSpec,
EthereumEngineValidator,
>,
to_tree_tx: Sender<FromEngine<EngineApiRequest<EthEngineTypes>>>,
from_tree_rx: UnboundedReceiver<EngineApiEvent>,
Expand Down Expand Up @@ -2736,7 +2734,7 @@ mod tests {
let provider = MockEthProvider::default();
let executor_provider = MockExecutorProvider::default();

let payload_validator = ExecutionPayloadValidator::new(chain_spec.clone());
let payload_validator = EthereumEngineValidator::new(chain_spec.clone());

let (from_tree_tx, from_tree_rx) = unbounded_channel();

Expand Down
Loading

0 comments on commit cca6372

Please sign in to comment.