mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
fix: use INVALID_PAYLOAD_ATTRIBUTES code for attribute validation errors (#7059)
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
@ -2,27 +2,23 @@
|
||||
use thiserror::Error;
|
||||
|
||||
/// Thrown when the payload or attributes are known to be invalid before processing.
|
||||
///
|
||||
/// This is used mainly for
|
||||
/// [validate_version_specific_fields](crate::validate_version_specific_fields), which validates
|
||||
/// both execution payloads and forkchoice update attributes with respect to a method version.
|
||||
#[derive(Error, Debug)]
|
||||
pub enum AttributesValidationError {
|
||||
/// Thrown if `PayloadAttributes` provided in engine_forkchoiceUpdated before V3 contains a
|
||||
/// parent beacon block root
|
||||
#[error("parent beacon block root not supported before V3")]
|
||||
ParentBeaconBlockRootNotSupportedBeforeV3,
|
||||
/// Thrown if engine_forkchoiceUpdatedV1 contains withdrawals
|
||||
#[error("withdrawals not supported in V1")]
|
||||
WithdrawalsNotSupportedInV1,
|
||||
/// Thrown if engine_forkchoiceUpdated contains no withdrawals after Shanghai
|
||||
#[error("no withdrawals post-Shanghai")]
|
||||
NoWithdrawalsPostShanghai,
|
||||
/// Thrown if engine_forkchoiceUpdated contains withdrawals before Shanghai
|
||||
#[error("withdrawals pre-Shanghai")]
|
||||
HasWithdrawalsPreShanghai,
|
||||
/// Thrown if the `PayloadAttributes` provided in engine_forkchoiceUpdated contains no parent
|
||||
/// beacon block root after Cancun
|
||||
#[error("no parent beacon block root post-cancun")]
|
||||
NoParentBeaconBlockRootPostCancun,
|
||||
/// Thrown if `PayloadAttributes` were provided with a timestamp, but the version of the engine
|
||||
/// method called is meant for a fork that occurs after the provided timestamp.
|
||||
pub enum EngineObjectValidationError {
|
||||
/// Thrown when the underlying validation error occured while validating an `ExecutionPayload`.
|
||||
#[error("Payload validation error: {0}")]
|
||||
Payload(VersionSpecificValidationError),
|
||||
|
||||
/// Thrown when the underlying validation error occured while validating a `PayloadAttributes`.
|
||||
#[error("Payload attributes validation error: {0}")]
|
||||
PayloadAttributes(VersionSpecificValidationError),
|
||||
|
||||
/// Thrown if `PayloadAttributes` or `ExecutionPayload` were provided with a timestamp, but the
|
||||
/// version of the engine method called is meant for a fork that occurs after the provided
|
||||
/// timestamp.
|
||||
#[error("Unsupported fork")]
|
||||
UnsupportedFork,
|
||||
/// Another type of error that is not covered by the above variants.
|
||||
@ -30,7 +26,33 @@ pub enum AttributesValidationError {
|
||||
InvalidParams(#[from] Box<dyn std::error::Error + Send + Sync>),
|
||||
}
|
||||
|
||||
impl AttributesValidationError {
|
||||
/// Thrown when validating an execution payload OR payload attributes fails due to:
|
||||
/// * The existence of a new field that is not supported in the given engine method version, or
|
||||
/// * The absence of a field that is required in the given engine method version
|
||||
#[derive(Error, Debug)]
|
||||
pub enum VersionSpecificValidationError {
|
||||
/// Thrown if the pre-V3 `PayloadAttributes` or `ExecutionPayload` contains a parent beacon
|
||||
/// block root
|
||||
#[error("parent beacon block root not supported before V3")]
|
||||
ParentBeaconBlockRootNotSupportedBeforeV3,
|
||||
/// Thrown if engine_forkchoiceUpdatedV1 or engine_newPayloadV1 contains withdrawals
|
||||
#[error("withdrawals not supported in V1")]
|
||||
WithdrawalsNotSupportedInV1,
|
||||
/// Thrown if engine_forkchoiceUpdated or engine_newPayload contains no withdrawals after
|
||||
/// Shanghai
|
||||
#[error("no withdrawals post-Shanghai")]
|
||||
NoWithdrawalsPostShanghai,
|
||||
/// Thrown if engine_forkchoiceUpdated or engine_newPayload contains withdrawals before
|
||||
/// Shanghai
|
||||
#[error("withdrawals pre-Shanghai")]
|
||||
HasWithdrawalsPreShanghai,
|
||||
/// Thrown if the `PayloadAttributes` or `ExecutionPayload` contains no parent beacon block
|
||||
/// root after Cancun
|
||||
#[error("no parent beacon block root post-cancun")]
|
||||
NoParentBeaconBlockRootPostCancun,
|
||||
}
|
||||
|
||||
impl EngineObjectValidationError {
|
||||
/// Creates an instance of the `InvalidParams` variant with the given error.
|
||||
pub fn invalid_params<E>(error: E) -> Self
|
||||
where
|
||||
|
||||
@ -11,7 +11,7 @@ pub use traits::{BuiltPayload, PayloadAttributes, PayloadBuilderAttributes};
|
||||
|
||||
/// Contains error types used in the traits defined in this crate.
|
||||
pub mod error;
|
||||
pub use error::AttributesValidationError;
|
||||
pub use error::{EngineObjectValidationError, VersionSpecificValidationError};
|
||||
|
||||
/// Contains types used in implementations of the [PayloadAttributes] trait.
|
||||
pub mod payload;
|
||||
@ -50,7 +50,7 @@ pub trait EngineTypes:
|
||||
chain_spec: &ChainSpec,
|
||||
version: EngineApiMessageVersion,
|
||||
payload_or_attrs: PayloadOrAttributes<'_, Self::PayloadAttributes>,
|
||||
) -> Result<(), AttributesValidationError>;
|
||||
) -> Result<(), EngineObjectValidationError>;
|
||||
}
|
||||
|
||||
/// Validates the timestamp depending on the version called:
|
||||
@ -58,12 +58,12 @@ pub trait EngineTypes:
|
||||
/// * If V2, this ensure that the payload timestamp is pre-Cancun.
|
||||
/// * If V3, this ensures that the payload timestamp is within the Cancun timestamp.
|
||||
///
|
||||
/// Otherwise, this will return [AttributesValidationError::UnsupportedFork].
|
||||
/// Otherwise, this will return [EngineObjectValidationError::UnsupportedFork].
|
||||
pub fn validate_payload_timestamp(
|
||||
chain_spec: &ChainSpec,
|
||||
version: EngineApiMessageVersion,
|
||||
timestamp: u64,
|
||||
) -> Result<(), AttributesValidationError> {
|
||||
) -> Result<(), EngineObjectValidationError> {
|
||||
let is_cancun = chain_spec.is_cancun_active_at_timestamp(timestamp);
|
||||
if version == EngineApiMessageVersion::V2 && is_cancun {
|
||||
// From the Engine API spec:
|
||||
@ -83,7 +83,7 @@ pub fn validate_payload_timestamp(
|
||||
//
|
||||
// 1. Client software **MUST** return `-38005: Unsupported fork` error if the `timestamp` of
|
||||
// payload or payloadAttributes is greater or equal to the Cancun activation timestamp.
|
||||
return Err(AttributesValidationError::UnsupportedFork)
|
||||
return Err(EngineObjectValidationError::UnsupportedFork)
|
||||
}
|
||||
|
||||
if version == EngineApiMessageVersion::V3 && !is_cancun {
|
||||
@ -105,7 +105,7 @@ pub fn validate_payload_timestamp(
|
||||
//
|
||||
// 2. Client software **MUST** return `-38005: Unsupported fork` error if the `timestamp` of
|
||||
// the payload does not fall within the time frame of the Cancun fork.
|
||||
return Err(AttributesValidationError::UnsupportedFork)
|
||||
return Err(EngineObjectValidationError::UnsupportedFork)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -116,26 +116,31 @@ pub fn validate_payload_timestamp(
|
||||
pub fn validate_withdrawals_presence(
|
||||
chain_spec: &ChainSpec,
|
||||
version: EngineApiMessageVersion,
|
||||
message_validation_kind: MessageValidationKind,
|
||||
timestamp: u64,
|
||||
has_withdrawals: bool,
|
||||
) -> Result<(), AttributesValidationError> {
|
||||
) -> Result<(), EngineObjectValidationError> {
|
||||
let is_shanghai = chain_spec.is_shanghai_active_at_timestamp(timestamp);
|
||||
|
||||
match version {
|
||||
EngineApiMessageVersion::V1 => {
|
||||
if has_withdrawals {
|
||||
return Err(AttributesValidationError::WithdrawalsNotSupportedInV1)
|
||||
return Err(message_validation_kind
|
||||
.to_error(VersionSpecificValidationError::WithdrawalsNotSupportedInV1))
|
||||
}
|
||||
if is_shanghai {
|
||||
return Err(AttributesValidationError::NoWithdrawalsPostShanghai)
|
||||
return Err(message_validation_kind
|
||||
.to_error(VersionSpecificValidationError::NoWithdrawalsPostShanghai))
|
||||
}
|
||||
}
|
||||
EngineApiMessageVersion::V2 | EngineApiMessageVersion::V3 => {
|
||||
if is_shanghai && !has_withdrawals {
|
||||
return Err(AttributesValidationError::NoWithdrawalsPostShanghai)
|
||||
return Err(message_validation_kind
|
||||
.to_error(VersionSpecificValidationError::NoWithdrawalsPostShanghai))
|
||||
}
|
||||
if !is_shanghai && has_withdrawals {
|
||||
return Err(AttributesValidationError::HasWithdrawalsPreShanghai)
|
||||
return Err(message_validation_kind
|
||||
.to_error(VersionSpecificValidationError::HasWithdrawalsPreShanghai))
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -151,13 +156,13 @@ pub fn validate_withdrawals_presence(
|
||||
/// Before Cancun, the `parentBeaconBlockRoot` field must be [None].
|
||||
///
|
||||
/// If the engine API message version is V1 or V2, and the timestamp is post-Cancun, then this will
|
||||
/// return [AttributesValidationError::UnsupportedFork].
|
||||
/// return [EngineObjectValidationError::UnsupportedFork].
|
||||
///
|
||||
/// If the timestamp is before the Cancun fork and the engine API message version is V3, then this
|
||||
/// will return [AttributesValidationError::UnsupportedFork].
|
||||
/// will return [EngineObjectValidationError::UnsupportedFork].
|
||||
///
|
||||
/// If the engine API message version is V3, but the `parentBeaconBlockRoot` is [None], then
|
||||
/// this will return [AttributesValidationError::NoParentBeaconBlockRootPostCancun].
|
||||
/// this will return [VersionSpecificValidationError::NoParentBeaconBlockRootPostCancun].
|
||||
///
|
||||
/// This implements the following Engine API spec rules:
|
||||
///
|
||||
@ -167,32 +172,67 @@ pub fn validate_withdrawals_presence(
|
||||
///
|
||||
/// For `engine_forkchoiceUpdatedV3`:
|
||||
///
|
||||
/// 2. Client software **MUST** return `-38005: Unsupported fork` error if the `payloadAttributes`
|
||||
/// is set and the `payloadAttributes.timestamp` does not fall within the time frame of the
|
||||
/// Cancun fork.
|
||||
/// 1. Client software **MUST** check that provided set of parameters and their fields strictly
|
||||
/// matches the expected one and return `-32602: Invalid params` error if this check fails. Any
|
||||
/// field having `null` value **MUST** be considered as not provided.
|
||||
///
|
||||
/// 2. Extend point (7) of the `engine_forkchoiceUpdatedV1` specification by defining the following
|
||||
/// sequence of checks that **MUST** be run over `payloadAttributes`:
|
||||
/// 1. `payloadAttributes` matches the `PayloadAttributesV3` structure, return `-38003: Invalid
|
||||
/// payload attributes` on failure.
|
||||
/// 2. `payloadAttributes.timestamp` falls within the time frame of the Cancun fork, return
|
||||
/// `-38005: Unsupported fork` on failure.
|
||||
/// 3. `payloadAttributes.timestamp` is greater than `timestamp` of a block referenced by
|
||||
/// `forkchoiceState.headBlockHash`, return `-38003: Invalid payload attributes` on failure.
|
||||
/// 4. If any of the above checks fails, the `forkchoiceState` update **MUST NOT** be rolled
|
||||
/// back.
|
||||
///
|
||||
/// For `engine_newPayloadV3`:
|
||||
///
|
||||
/// 2. Client software **MUST** return `-38005: Unsupported fork` error if the `timestamp` of the
|
||||
/// payload does not fall within the time frame of the Cancun fork.
|
||||
///
|
||||
/// Returning the right error code (ie, if the client should return `-38003: Invalid payload
|
||||
/// attributes` is handled by the `message_validation_kind` parameter. If the parameter is
|
||||
/// `MessageValidationKind::Payload`, then the error code will be `-32602: Invalid params`. If the
|
||||
/// parameter is `MessageValidationKind::PayloadAttributes`, then the error code will be `-38003:
|
||||
/// Invalid payload attributes`.
|
||||
pub fn validate_parent_beacon_block_root_presence(
|
||||
chain_spec: &ChainSpec,
|
||||
version: EngineApiMessageVersion,
|
||||
validation_kind: MessageValidationKind,
|
||||
timestamp: u64,
|
||||
has_parent_beacon_block_root: bool,
|
||||
) -> Result<(), AttributesValidationError> {
|
||||
) -> Result<(), EngineObjectValidationError> {
|
||||
// 1. Client software **MUST** check that provided set of parameters and their fields strictly
|
||||
// matches the expected one and return `-32602: Invalid params` error if this check fails.
|
||||
// Any field having `null` value **MUST** be considered as not provided.
|
||||
//
|
||||
// For `engine_forkchoiceUpdatedV3`:
|
||||
//
|
||||
// 2. Extend point (7) of the `engine_forkchoiceUpdatedV1` specification by defining the
|
||||
// following sequence of checks that **MUST** be run over `payloadAttributes`:
|
||||
// 1. `payloadAttributes` matches the `PayloadAttributesV3` structure, return `-38003:
|
||||
// Invalid payload attributes` on failure.
|
||||
// 2. `payloadAttributes.timestamp` falls within the time frame of the Cancun fork, return
|
||||
// `-38005: Unsupported fork` on failure.
|
||||
// 3. `payloadAttributes.timestamp` is greater than `timestamp` of a block referenced by
|
||||
// `forkchoiceState.headBlockHash`, return `-38003: Invalid payload attributes` on
|
||||
// failure.
|
||||
// 4. If any of the above checks fails, the `forkchoiceState` update **MUST NOT** be rolled
|
||||
// back.
|
||||
match version {
|
||||
EngineApiMessageVersion::V1 | EngineApiMessageVersion::V2 => {
|
||||
if has_parent_beacon_block_root {
|
||||
return Err(AttributesValidationError::ParentBeaconBlockRootNotSupportedBeforeV3)
|
||||
return Err(validation_kind.to_error(
|
||||
VersionSpecificValidationError::ParentBeaconBlockRootNotSupportedBeforeV3,
|
||||
))
|
||||
}
|
||||
}
|
||||
EngineApiMessageVersion::V3 => {
|
||||
if !has_parent_beacon_block_root {
|
||||
return Err(AttributesValidationError::NoParentBeaconBlockRootPostCancun)
|
||||
return Err(validation_kind
|
||||
.to_error(VersionSpecificValidationError::NoParentBeaconBlockRootPostCancun))
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -212,25 +252,55 @@ pub fn validate_parent_beacon_block_root_presence(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validates the presence or exclusion of fork-specific fields based on the ethereum payload
|
||||
/// attributes and the message version.
|
||||
/// A type that represents whether or not we are validating a payload or payload attributes.
|
||||
///
|
||||
/// This is used to ensure that the correct error code is returned when validating the payload or
|
||||
/// payload attributes.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum MessageValidationKind {
|
||||
/// We are validating fields of a payload attributes.
|
||||
PayloadAttributes,
|
||||
/// We are validating fields of a payload.
|
||||
Payload,
|
||||
}
|
||||
|
||||
impl MessageValidationKind {
|
||||
/// Returns an `EngineObjectValidationError` based on the given
|
||||
/// `VersionSpecificValidationError` and the current validation kind.
|
||||
pub fn to_error(self, error: VersionSpecificValidationError) -> EngineObjectValidationError {
|
||||
match self {
|
||||
Self::Payload => EngineObjectValidationError::Payload(error),
|
||||
Self::PayloadAttributes => EngineObjectValidationError::PayloadAttributes(error),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates the presence or exclusion of fork-specific fields based on the ethereum execution
|
||||
/// payload, or payload attributes, and the message version.
|
||||
///
|
||||
/// The object being validated is provided by the [PayloadOrAttributes] argument, which can be
|
||||
/// either an execution payload, or payload attributes.
|
||||
///
|
||||
/// The version is provided by the [EngineApiMessageVersion] argument.
|
||||
pub fn validate_version_specific_fields<Type>(
|
||||
chain_spec: &ChainSpec,
|
||||
version: EngineApiMessageVersion,
|
||||
payload_or_attrs: PayloadOrAttributes<'_, Type>,
|
||||
) -> Result<(), AttributesValidationError>
|
||||
) -> Result<(), EngineObjectValidationError>
|
||||
where
|
||||
Type: PayloadAttributes,
|
||||
{
|
||||
validate_withdrawals_presence(
|
||||
chain_spec,
|
||||
version,
|
||||
payload_or_attrs.message_validation_kind(),
|
||||
payload_or_attrs.timestamp(),
|
||||
payload_or_attrs.withdrawals().is_some(),
|
||||
)?;
|
||||
validate_parent_beacon_block_root_presence(
|
||||
chain_spec,
|
||||
version,
|
||||
payload_or_attrs.message_validation_kind(),
|
||||
payload_or_attrs.timestamp(),
|
||||
payload_or_attrs.parent_beacon_block_root().is_some(),
|
||||
)
|
||||
|
||||
@ -2,6 +2,8 @@ use crate::PayloadAttributes;
|
||||
use reth_primitives::B256;
|
||||
use reth_rpc_types::engine::ExecutionPayload;
|
||||
|
||||
use super::MessageValidationKind;
|
||||
|
||||
/// Either an [ExecutionPayload] or a types that implements the [PayloadAttributes] trait.
|
||||
#[derive(Debug)]
|
||||
pub enum PayloadOrAttributes<'a, AttributesType> {
|
||||
@ -52,6 +54,14 @@ where
|
||||
Self::PayloadAttributes(attributes) => attributes.parent_beacon_block_root(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a [MessageValidationKind] for the payload or attributes.
|
||||
pub fn message_validation_kind(&self) -> MessageValidationKind {
|
||||
match self {
|
||||
Self::ExecutionPayload { .. } => MessageValidationKind::Payload,
|
||||
Self::PayloadAttributes(_) => MessageValidationKind::PayloadAttributes,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, AttributesType> From<&'a AttributesType> for PayloadOrAttributes<'a, AttributesType>
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
use crate::{validate_version_specific_fields, AttributesValidationError, EngineApiMessageVersion};
|
||||
use crate::{
|
||||
validate_version_specific_fields, EngineApiMessageVersion, EngineObjectValidationError,
|
||||
};
|
||||
use reth_primitives::{
|
||||
revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg},
|
||||
Address, ChainSpec, Header, SealedBlock, Withdrawals, B256, U256,
|
||||
@ -98,7 +100,7 @@ pub trait PayloadAttributes:
|
||||
&self,
|
||||
chain_spec: &ChainSpec,
|
||||
version: EngineApiMessageVersion,
|
||||
) -> Result<(), AttributesValidationError>;
|
||||
) -> Result<(), EngineObjectValidationError>;
|
||||
}
|
||||
|
||||
impl PayloadAttributes for EthPayloadAttributes {
|
||||
@ -118,7 +120,7 @@ impl PayloadAttributes for EthPayloadAttributes {
|
||||
&self,
|
||||
chain_spec: &ChainSpec,
|
||||
version: EngineApiMessageVersion,
|
||||
) -> Result<(), AttributesValidationError> {
|
||||
) -> Result<(), EngineObjectValidationError> {
|
||||
validate_version_specific_fields(chain_spec, version, self.into())
|
||||
}
|
||||
}
|
||||
@ -140,11 +142,11 @@ impl PayloadAttributes for OptimismPayloadAttributes {
|
||||
&self,
|
||||
chain_spec: &ChainSpec,
|
||||
version: EngineApiMessageVersion,
|
||||
) -> Result<(), AttributesValidationError> {
|
||||
) -> Result<(), EngineObjectValidationError> {
|
||||
validate_version_specific_fields(chain_spec, version, self.into())?;
|
||||
|
||||
if self.gas_limit.is_none() && chain_spec.is_optimism() {
|
||||
return Err(AttributesValidationError::InvalidParams(
|
||||
return Err(EngineObjectValidationError::InvalidParams(
|
||||
"MissingGasLimitInPayloadAttributes".to_string().into(),
|
||||
))
|
||||
}
|
||||
|
||||
@ -14,8 +14,9 @@
|
||||
pub mod engine;
|
||||
pub use engine::{
|
||||
validate_payload_timestamp, validate_version_specific_fields, validate_withdrawals_presence,
|
||||
AttributesValidationError, BuiltPayload, EngineApiMessageVersion, EngineTypes,
|
||||
PayloadAttributes, PayloadBuilderAttributes, PayloadOrAttributes,
|
||||
BuiltPayload, EngineApiMessageVersion, EngineObjectValidationError, EngineTypes,
|
||||
MessageValidationKind, PayloadAttributes, PayloadBuilderAttributes, PayloadOrAttributes,
|
||||
VersionSpecificValidationError,
|
||||
};
|
||||
|
||||
/// Traits and helper types used to abstract over EVM methods and types.
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use reth_node_api::{
|
||||
validate_version_specific_fields, AttributesValidationError, EngineApiMessageVersion,
|
||||
validate_version_specific_fields, EngineApiMessageVersion, EngineObjectValidationError,
|
||||
EngineTypes, PayloadOrAttributes,
|
||||
};
|
||||
use reth_payload_builder::{EthBuiltPayload, EthPayloadBuilderAttributes};
|
||||
@ -29,7 +29,7 @@ impl EngineTypes for EthEngineTypes {
|
||||
chain_spec: &ChainSpec,
|
||||
version: EngineApiMessageVersion,
|
||||
payload_or_attrs: PayloadOrAttributes<'_, EthPayloadAttributes>,
|
||||
) -> Result<(), AttributesValidationError> {
|
||||
) -> Result<(), EngineObjectValidationError> {
|
||||
validate_version_specific_fields(chain_spec, version, payload_or_attrs)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
use reth_node_api::{
|
||||
engine::validate_parent_beacon_block_root_presence, AttributesValidationError,
|
||||
EngineApiMessageVersion, EngineTypes, PayloadOrAttributes,
|
||||
engine::validate_parent_beacon_block_root_presence, EngineApiMessageVersion,
|
||||
EngineObjectValidationError, EngineTypes, MessageValidationKind, PayloadOrAttributes,
|
||||
VersionSpecificValidationError,
|
||||
};
|
||||
use reth_payload_builder::{OptimismBuiltPayload, OptimismPayloadBuilderAttributes};
|
||||
use reth_primitives::{ChainSpec, Hardfork};
|
||||
@ -28,16 +29,18 @@ impl EngineTypes for OptimismEngineTypes {
|
||||
chain_spec: &ChainSpec,
|
||||
version: EngineApiMessageVersion,
|
||||
payload_or_attrs: PayloadOrAttributes<'_, Self::PayloadAttributes>,
|
||||
) -> Result<(), AttributesValidationError> {
|
||||
) -> Result<(), EngineObjectValidationError> {
|
||||
validate_withdrawals_presence(
|
||||
chain_spec,
|
||||
version,
|
||||
payload_or_attrs.message_validation_kind(),
|
||||
payload_or_attrs.timestamp(),
|
||||
payload_or_attrs.withdrawals().is_some(),
|
||||
)?;
|
||||
validate_parent_beacon_block_root_presence(
|
||||
chain_spec,
|
||||
version,
|
||||
payload_or_attrs.message_validation_kind(),
|
||||
payload_or_attrs.timestamp(),
|
||||
payload_or_attrs.parent_beacon_block_root().is_some(),
|
||||
)
|
||||
@ -54,26 +57,31 @@ impl EngineTypes for OptimismEngineTypes {
|
||||
pub fn validate_withdrawals_presence(
|
||||
chain_spec: &ChainSpec,
|
||||
version: EngineApiMessageVersion,
|
||||
message_validation_kind: MessageValidationKind,
|
||||
timestamp: u64,
|
||||
has_withdrawals: bool,
|
||||
) -> Result<(), AttributesValidationError> {
|
||||
) -> Result<(), EngineObjectValidationError> {
|
||||
let is_shanghai = chain_spec.fork(Hardfork::Canyon).active_at_timestamp(timestamp);
|
||||
|
||||
match version {
|
||||
EngineApiMessageVersion::V1 => {
|
||||
if has_withdrawals {
|
||||
return Err(AttributesValidationError::WithdrawalsNotSupportedInV1)
|
||||
return Err(message_validation_kind
|
||||
.to_error(VersionSpecificValidationError::WithdrawalsNotSupportedInV1))
|
||||
}
|
||||
if is_shanghai {
|
||||
return Err(AttributesValidationError::NoWithdrawalsPostShanghai)
|
||||
return Err(message_validation_kind
|
||||
.to_error(VersionSpecificValidationError::NoWithdrawalsPostShanghai))
|
||||
}
|
||||
}
|
||||
EngineApiMessageVersion::V2 | EngineApiMessageVersion::V3 => {
|
||||
if is_shanghai && !has_withdrawals {
|
||||
return Err(AttributesValidationError::NoWithdrawalsPostShanghai)
|
||||
return Err(message_validation_kind
|
||||
.to_error(VersionSpecificValidationError::NoWithdrawalsPostShanghai))
|
||||
}
|
||||
if !is_shanghai && has_withdrawals {
|
||||
return Err(AttributesValidationError::HasWithdrawalsPreShanghai)
|
||||
return Err(message_validation_kind
|
||||
.to_error(VersionSpecificValidationError::HasWithdrawalsPreShanghai))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -2,7 +2,7 @@ use jsonrpsee_types::error::{
|
||||
INTERNAL_ERROR_CODE, INVALID_PARAMS_CODE, INVALID_PARAMS_MSG, SERVER_ERROR_MSG,
|
||||
};
|
||||
use reth_beacon_consensus::{BeaconForkChoiceUpdateError, BeaconOnNewPayloadError};
|
||||
use reth_node_api::AttributesValidationError;
|
||||
use reth_node_api::EngineObjectValidationError;
|
||||
use reth_payload_builder::error::PayloadBuilderError;
|
||||
use reth_primitives::{B256, U256};
|
||||
use thiserror::Error;
|
||||
@ -10,6 +10,8 @@ use thiserror::Error;
|
||||
/// The Engine API result type
|
||||
pub type EngineApiResult<Ok> = Result<Ok, EngineApiError>;
|
||||
|
||||
/// Invalid payload attributes code.
|
||||
pub const INVALID_PAYLOAD_ATTRIBUTES: i32 = -38003;
|
||||
/// Payload unsupported fork code.
|
||||
pub const UNSUPPORTED_FORK_CODE: i32 = -38005;
|
||||
/// Payload unknown error code.
|
||||
@ -20,6 +22,9 @@ pub const REQUEST_TOO_LARGE_CODE: i32 = -38004;
|
||||
/// Error message for the request too large error.
|
||||
const REQUEST_TOO_LARGE_MESSAGE: &str = "Too large request";
|
||||
|
||||
/// Error message for the request too large error.
|
||||
const INVALID_PAYLOAD_ATTRIBUTES_MSG: &str = "Invalid payload attributes";
|
||||
|
||||
/// Error returned by [`EngineApi`][crate::EngineApi]
|
||||
///
|
||||
/// Note: This is a high-fidelity error type which can be converted to an RPC error that adheres to
|
||||
@ -80,7 +85,7 @@ pub enum EngineApiError {
|
||||
GetPayloadError(#[from] PayloadBuilderError),
|
||||
/// The payload or attributes are known to be malformed before processing.
|
||||
#[error(transparent)]
|
||||
AttributesValidationError(#[from] AttributesValidationError),
|
||||
EngineObjectValidationError(#[from] EngineObjectValidationError),
|
||||
/// If the optimism feature flag is enabled, the payload attributes must have a present
|
||||
/// gas limit for the forkchoice updated method.
|
||||
#[cfg(feature = "optimism")]
|
||||
@ -106,23 +111,11 @@ impl From<EngineApiError> for jsonrpsee_types::error::ErrorObject<'static> {
|
||||
fn from(error: EngineApiError) -> Self {
|
||||
match error {
|
||||
EngineApiError::InvalidBodiesRange { .. } |
|
||||
EngineApiError::AttributesValidationError(
|
||||
AttributesValidationError::WithdrawalsNotSupportedInV1,
|
||||
) |
|
||||
EngineApiError::AttributesValidationError(
|
||||
AttributesValidationError::ParentBeaconBlockRootNotSupportedBeforeV3,
|
||||
) |
|
||||
EngineApiError::AttributesValidationError(
|
||||
AttributesValidationError::NoParentBeaconBlockRootPostCancun,
|
||||
) |
|
||||
EngineApiError::AttributesValidationError(
|
||||
AttributesValidationError::NoWithdrawalsPostShanghai,
|
||||
) |
|
||||
EngineApiError::AttributesValidationError(
|
||||
AttributesValidationError::HasWithdrawalsPreShanghai,
|
||||
) |
|
||||
EngineApiError::AttributesValidationError(
|
||||
AttributesValidationError::InvalidParams(_),
|
||||
EngineApiError::EngineObjectValidationError(EngineObjectValidationError::Payload(
|
||||
_,
|
||||
)) |
|
||||
EngineApiError::EngineObjectValidationError(
|
||||
EngineObjectValidationError::InvalidParams(_),
|
||||
) => {
|
||||
// Note: the data field is not required by the spec, but is also included by other
|
||||
// clients
|
||||
@ -132,6 +125,17 @@ impl From<EngineApiError> for jsonrpsee_types::error::ErrorObject<'static> {
|
||||
Some(ErrorData::new(error)),
|
||||
)
|
||||
}
|
||||
EngineApiError::EngineObjectValidationError(
|
||||
EngineObjectValidationError::PayloadAttributes(_),
|
||||
) => {
|
||||
// Note: the data field is not required by the spec, but is also included by other
|
||||
// clients
|
||||
jsonrpsee_types::error::ErrorObject::owned(
|
||||
INVALID_PAYLOAD_ATTRIBUTES,
|
||||
INVALID_PAYLOAD_ATTRIBUTES_MSG,
|
||||
Some(ErrorData::new(error)),
|
||||
)
|
||||
}
|
||||
EngineApiError::UnknownPayload => jsonrpsee_types::error::ErrorObject::owned(
|
||||
UNKNOWN_PAYLOAD_CODE,
|
||||
error.to_string(),
|
||||
@ -144,8 +148,8 @@ impl From<EngineApiError> for jsonrpsee_types::error::ErrorObject<'static> {
|
||||
Some(ErrorData::new(error)),
|
||||
)
|
||||
}
|
||||
EngineApiError::AttributesValidationError(
|
||||
AttributesValidationError::UnsupportedFork,
|
||||
EngineApiError::EngineObjectValidationError(
|
||||
EngineObjectValidationError::UnsupportedFork,
|
||||
) => jsonrpsee_types::error::ErrorObject::owned(
|
||||
UNSUPPORTED_FORK_CODE,
|
||||
error.to_string(),
|
||||
@ -236,7 +240,9 @@ mod tests {
|
||||
ensure_engine_rpc_error(
|
||||
UNSUPPORTED_FORK_CODE,
|
||||
"Unsupported fork",
|
||||
EngineApiError::AttributesValidationError(AttributesValidationError::UnsupportedFork),
|
||||
EngineApiError::EngineObjectValidationError(
|
||||
EngineObjectValidationError::UnsupportedFork,
|
||||
),
|
||||
);
|
||||
|
||||
ensure_engine_rpc_error(
|
||||
|
||||
Reference in New Issue
Block a user