Skip to content

Commit

Permalink
feat: EngineValidator (#11144)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Sep 24, 2024
1 parent 73962b1 commit c851a17
Show file tree
Hide file tree
Showing 18 changed files with 414 additions and 169 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.

17 changes: 12 additions & 5 deletions crates/engine/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ pub trait EngineTypes:
+ Serialize
+ 'static
{
/// The chain specification of the node.
type ChainSpec: Send + Sync;

/// Execution Payload V1 type.
type ExecutionPayloadV1: DeserializeOwned + Serialize + Clone + Unpin + Send + Sync + 'static;
/// Execution Payload V2 type.
Expand All @@ -42,12 +39,22 @@ pub trait EngineTypes:
type ExecutionPayloadV3: DeserializeOwned + Serialize + Clone + Unpin + Send + Sync + 'static;
/// Execution Payload V4 type.
type ExecutionPayloadV4: DeserializeOwned + Serialize + Clone + Unpin + Send + Sync + 'static;
}

/// Type that validates the payloads sent to the engine.
pub trait EngineValidator<Types: EngineTypes>: Clone + Send + Sync + Unpin + 'static {
/// Validates the presence or exclusion of fork-specific fields based on the payload attributes
/// and the message version.
fn validate_version_specific_fields(
chain_spec: &Self::ChainSpec,
&self,
version: EngineApiMessageVersion,
payload_or_attrs: PayloadOrAttributes<'_, <Types as PayloadTypes>::PayloadAttributes>,
) -> Result<(), EngineObjectValidationError>;

/// Ensures that the payload attributes are valid for the given [`EngineApiMessageVersion`].
fn ensure_well_formed_attributes(
&self,
version: EngineApiMessageVersion,
payload_or_attrs: PayloadOrAttributes<'_, Self::PayloadAttributes>,
attributes: &<Types as PayloadTypes>::PayloadAttributes,
) -> Result<(), EngineObjectValidationError>;
}
36 changes: 31 additions & 5 deletions crates/ethereum/engine-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

mod payload;
use std::sync::Arc;

pub use payload::{EthBuiltPayload, EthPayloadBuilderAttributes};
use reth_chainspec::ChainSpec;
use reth_engine_primitives::EngineTypes;
use reth_engine_primitives::{EngineTypes, EngineValidator};
use reth_payload_primitives::{
validate_version_specific_fields, EngineApiMessageVersion, EngineObjectValidationError,
PayloadOrAttributes, PayloadTypes,
Expand All @@ -36,18 +38,42 @@ impl PayloadTypes for EthEngineTypes {
}

impl EngineTypes for EthEngineTypes {
type ChainSpec = ChainSpec;

type ExecutionPayloadV1 = ExecutionPayloadV1;
type ExecutionPayloadV2 = ExecutionPayloadEnvelopeV2;
type ExecutionPayloadV3 = ExecutionPayloadEnvelopeV3;
type ExecutionPayloadV4 = ExecutionPayloadEnvelopeV4;
}

/// Validator for the ethereum engine API.
#[derive(Debug, Clone)]
pub struct EthereumEngineValidator {
chain_spec: Arc<ChainSpec>,
}

impl EthereumEngineValidator {
/// Instantiates a new validator.
pub const fn new(chain_spec: Arc<ChainSpec>) -> Self {
Self { chain_spec }
}
}

impl<Types> EngineValidator<Types> for EthereumEngineValidator
where
Types: EngineTypes<PayloadAttributes = EthPayloadAttributes>,
{
fn validate_version_specific_fields(
chain_spec: &ChainSpec,
&self,
version: EngineApiMessageVersion,
payload_or_attrs: PayloadOrAttributes<'_, EthPayloadAttributes>,
) -> Result<(), EngineObjectValidationError> {
validate_version_specific_fields(chain_spec, version, payload_or_attrs)
validate_version_specific_fields(&self.chain_spec, version, payload_or_attrs)
}

fn ensure_well_formed_attributes(
&self,
version: EngineApiMessageVersion,
attributes: &EthPayloadAttributes,
) -> Result<(), EngineObjectValidationError> {
validate_version_specific_fields(&self.chain_spec, version, attributes.into())
}
}
29 changes: 25 additions & 4 deletions crates/ethereum/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGenera
use reth_beacon_consensus::EthBeaconConsensus;
use reth_chainspec::ChainSpec;
use reth_ethereum_engine_primitives::{
EthBuiltPayload, EthPayloadAttributes, EthPayloadBuilderAttributes,
EthBuiltPayload, EthPayloadAttributes, EthPayloadBuilderAttributes, EthereumEngineValidator,
};
use reth_evm_ethereum::execute::EthExecutorProvider;
use reth_network::NetworkHandle;
use reth_node_api::{ConfigureEvm, FullNodeComponents, NodeAddOns};
use reth_node_api::{ConfigureEvm, EngineValidator, FullNodeComponents, NodeAddOns};
use reth_node_builder::{
components::{
ComponentsBuilder, ConsensusBuilder, ExecutorBuilder, NetworkBuilder,
PayloadServiceBuilder, PoolBuilder,
ComponentsBuilder, ConsensusBuilder, EngineValidatorBuilder, ExecutorBuilder,
NetworkBuilder, PayloadServiceBuilder, PoolBuilder,
},
node::{FullNodeTypes, NodeTypes, NodeTypesWithEngine},
BuilderContext, Node, PayloadBuilderConfig, PayloadTypes,
Expand Down Expand Up @@ -46,6 +46,7 @@ impl EthereumNode {
EthereumNetworkBuilder,
EthereumExecutorBuilder,
EthereumConsensusBuilder,
EthereumEngineValidatorBuilder,
>
where
Node: FullNodeTypes<Types: NodeTypes<ChainSpec = ChainSpec>>,
Expand All @@ -62,6 +63,7 @@ impl EthereumNode {
.network(EthereumNetworkBuilder::default())
.executor(EthereumExecutorBuilder::default())
.consensus(EthereumConsensusBuilder::default())
.engine_validator(EthereumEngineValidatorBuilder::default())
}
}

Expand Down Expand Up @@ -94,6 +96,7 @@ where
EthereumNetworkBuilder,
EthereumExecutorBuilder,
EthereumConsensusBuilder,
EthereumEngineValidatorBuilder,
>;

type AddOns = EthereumAddOns;
Expand Down Expand Up @@ -316,3 +319,21 @@ where
}
}
}

/// Builder for [`EthereumEngineValidator`].
#[derive(Debug, Default, Clone)]
#[non_exhaustive]
pub struct EthereumEngineValidatorBuilder;

impl<Node, Types> EngineValidatorBuilder<Node> for EthereumEngineValidatorBuilder
where
Types: NodeTypesWithEngine<ChainSpec = ChainSpec>,
Node: FullNodeTypes<Types = Types>,
EthereumEngineValidator: EngineValidator<Types::Engine>,
{
type Validator = EthereumEngineValidator;

async fn build_validator(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::Validator> {
Ok(EthereumEngineValidator::new(ctx.chain_spec()))
}
}
1 change: 1 addition & 0 deletions crates/exex/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ reth-primitives.workspace = true
reth-provider = { workspace = true, features = ["test-utils"] }
reth-tasks.workspace = true
reth-transaction-pool = { workspace = true, features = ["test-utils"] }
reth-ethereum-engine-primitives.workspace = true

## async
futures-util.workspace = true
Expand Down
13 changes: 11 additions & 2 deletions crates/exex/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use reth_db::{
DatabaseEnv,
};
use reth_db_common::init::init_genesis;
use reth_ethereum_engine_primitives::EthereumEngineValidator;
use reth_evm::test_utils::MockExecutorProvider;
use reth_execution_types::Chain;
use reth_exex::{ExExContext, ExExEvent, ExExNotification, ExExNotifications};
Expand All @@ -33,7 +34,10 @@ use reth_node_builder::{
};
use reth_node_core::node_config::NodeConfig;
use reth_node_ethereum::{
node::{EthereumAddOns, EthereumNetworkBuilder, EthereumPayloadBuilder},
node::{
EthereumAddOns, EthereumEngineValidatorBuilder, EthereumNetworkBuilder,
EthereumPayloadBuilder,
},
EthEngineTypes, EthEvmConfig,
};
use reth_payload_builder::noop::NoopPayloadBuilderService;
Expand Down Expand Up @@ -133,6 +137,7 @@ where
EthereumNetworkBuilder,
TestExecutorBuilder,
TestConsensusBuilder,
EthereumEngineValidatorBuilder,
>;
type AddOns = EthereumAddOns;

Expand All @@ -144,6 +149,7 @@ where
.network(EthereumNetworkBuilder::default())
.executor(TestExecutorBuilder::default())
.consensus(TestConsensusBuilder::default())
.engine_validator(EthereumEngineValidatorBuilder::default())
}
}

Expand Down Expand Up @@ -246,7 +252,7 @@ pub async fn test_exex_context_with_chain_spec(
let db = create_test_rw_db();
let provider_factory = ProviderFactory::new(
db,
chain_spec,
chain_spec.clone(),
StaticFileProvider::read_write(static_dir.into_path()).expect("static file provider"),
);

Expand All @@ -266,6 +272,8 @@ pub async fn test_exex_context_with_chain_spec(
let tasks = TaskManager::current();
let task_executor = tasks.executor();

let engine_validator = EthereumEngineValidator::new(chain_spec.clone());

let components = NodeAdapter::<FullNodeTypesAdapter<NodeTypesWithDBAdapter<TestNode, _>, _>, _> {
components: Components {
transaction_pool,
Expand All @@ -274,6 +282,7 @@ pub async fn test_exex_context_with_chain_spec(
consensus,
network,
payload_builder,
engine_validator,
},
task_executor,
provider,
Expand Down
Loading

0 comments on commit c851a17

Please sign in to comment.