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

refactor: Consensus trait error type #13655

Merged
Merged
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
4 changes: 2 additions & 2 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use reth_cli::chainspec::ChainSpecParser;
use reth_cli_commands::common::{AccessRights, CliNodeTypes, Environment, EnvironmentArgs};
use reth_cli_runner::CliContext;
use reth_consensus::{Consensus, FullConsensus};
use reth_errors::RethResult;
use reth_errors::{ConsensusError, RethResult};
use reth_ethereum_payload_builder::EthereumBuilderConfig;
use reth_evm::execute::{BlockExecutorProvider, Executor};
use reth_execution_types::ExecutionOutcome;
Expand Down Expand Up @@ -128,7 +128,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
) -> eyre::Result<()> {
let Environment { provider_factory, .. } = self.env.init::<N>(AccessRights::RW)?;

let consensus: Arc<dyn FullConsensus> =
let consensus: Arc<dyn FullConsensus<Error = ConsensusError>> =
Arc::new(EthBeaconConsensus::new(provider_factory.chain_spec()));

let executor = EthExecutorProvider::ethereum(provider_factory.chain_spec());
Expand Down
5 changes: 3 additions & 2 deletions bin/reth/src/commands/debug_cmd/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use reth_downloaders::{
bodies::bodies::BodiesDownloaderBuilder,
headers::reverse_headers::ReverseHeadersDownloaderBuilder,
};
use reth_errors::ConsensusError;
use reth_exex::ExExManagerHandle;
use reth_network::{BlockDownloaderProvider, NetworkHandle};
use reth_network_api::NetworkInfo;
Expand Down Expand Up @@ -64,7 +65,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
&self,
config: &Config,
client: Client,
consensus: Arc<dyn Consensus>,
consensus: Arc<dyn Consensus<Error = ConsensusError>>,
provider_factory: ProviderFactory<N>,
task_executor: &TaskExecutor,
static_file_producer: StaticFileProducer<ProviderFactory<N>>,
Expand Down Expand Up @@ -172,7 +173,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
let Environment { provider_factory, config, data_dir } =
self.env.init::<N>(AccessRights::RW)?;

let consensus: Arc<dyn Consensus> =
let consensus: Arc<dyn Consensus<Error = ConsensusError>> =
Arc::new(EthBeaconConsensus::new(provider_factory.chain_spec()));

// Configure and build network
Expand Down
4 changes: 2 additions & 2 deletions bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use reth_cli_commands::common::{AccessRights, CliNodeTypes, Environment, Environ
use reth_cli_runner::CliContext;
use reth_cli_util::get_secret_key;
use reth_config::Config;
use reth_consensus::Consensus;
use reth_consensus::{Consensus, ConsensusError};
use reth_db::tables;
use reth_db_api::{cursor::DbCursorRO, transaction::DbTx};
use reth_evm::execute::{BatchExecutor, BlockExecutorProvider};
Expand Down Expand Up @@ -129,7 +129,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
info!(target: "reth::cli", target_block_number=self.to, "Finished downloading tip of block range");

// build the full block client
let consensus: Arc<dyn Consensus> =
let consensus: Arc<dyn Consensus<Error = ConsensusError>> =
Arc::new(EthBeaconConsensus::new(provider_factory.chain_spec()));
let block_range_client = FullBlockClient::new(fetch_client, consensus);

Expand Down
4 changes: 2 additions & 2 deletions bin/reth/src/commands/debug_cmd/replay_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use reth_cli_commands::common::{AccessRights, CliNodeTypes, Environment, Environ
use reth_cli_runner::CliContext;
use reth_cli_util::get_secret_key;
use reth_config::Config;
use reth_consensus::FullConsensus;
use reth_consensus::{ConsensusError, FullConsensus};
use reth_db::DatabaseEnv;
use reth_engine_util::engine_store::{EngineMessageStore, StoredEngineApiMessage};
use reth_ethereum_payload_builder::EthereumBuilderConfig;
Expand Down Expand Up @@ -97,7 +97,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
let Environment { provider_factory, config, data_dir } =
self.env.init::<N>(AccessRights::RW)?;

let consensus: Arc<dyn FullConsensus> =
let consensus: Arc<dyn FullConsensus<Error = ConsensusError>> =
Arc::new(EthBeaconConsensus::new(provider_factory.chain_spec()));

let executor = EthExecutorProvider::ethereum(provider_factory.chain_spec());
Expand Down
6 changes: 3 additions & 3 deletions crates/blockchain-tree/src/externals.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Blockchain tree externals.

use alloy_primitives::{BlockHash, BlockNumber};
use reth_consensus::FullConsensus;
use reth_consensus::{ConsensusError, FullConsensus};
use reth_db::{static_file::BlockHashMask, tables};
use reth_db_api::{cursor::DbCursorRO, transaction::DbTx};
use reth_node_types::NodeTypesWithDB;
Expand All @@ -28,7 +28,7 @@ pub struct TreeExternals<N: NodeTypesWithDB, E> {
/// The provider factory, used to commit the canonical chain, or unwind it.
pub(crate) provider_factory: ProviderFactory<N>,
/// The consensus engine.
pub(crate) consensus: Arc<dyn FullConsensus>,
pub(crate) consensus: Arc<dyn FullConsensus<Error = ConsensusError>>,
/// The executor factory to execute blocks with.
pub(crate) executor_factory: E,
}
Expand All @@ -37,7 +37,7 @@ impl<N: ProviderNodeTypes, E> TreeExternals<N, E> {
/// Create new tree externals.
pub fn new(
provider_factory: ProviderFactory<N>,
consensus: Arc<dyn FullConsensus>,
consensus: Arc<dyn FullConsensus<Error = ConsensusError>>,
executor_factory: E,
) -> Self {
Self { provider_factory, consensus, executor_factory }
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/commands/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use reth_beacon_consensus::EthBeaconConsensus;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_cli::chainspec::ChainSpecParser;
use reth_config::Config;
use reth_consensus::Consensus;
use reth_consensus::{Consensus, ConsensusError};
use reth_db::tables;
use reth_db_api::transaction::DbTx;
use reth_downloaders::{
Expand Down Expand Up @@ -169,7 +169,7 @@ pub fn build_import_pipeline<N, C, E>(
) -> eyre::Result<(Pipeline<N>, impl Stream<Item = NodeEvent<N::Primitives>>)>
where
N: ProviderNodeTypes + CliNodeTypes,
C: Consensus<HeaderTy<N>, BodyTy<N>> + 'static,
C: Consensus<HeaderTy<N>, BodyTy<N>, Error = ConsensusError> + 'static,
E: BlockExecutorProvider<Primitives = N::Primitives>,
{
if !file_client.has_canonical_blocks() {
Expand Down
15 changes: 8 additions & 7 deletions crates/consensus/beacon/src/engine/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use reth_blockchain_tree::{
};
use reth_chainspec::ChainSpec;
use reth_config::config::StageConfig;
use reth_consensus::{test_utils::TestConsensus, FullConsensus};
use reth_consensus::{test_utils::TestConsensus, ConsensusError, FullConsensus};
use reth_db::{test_utils::TempDatabase, DatabaseEnv as DE};
use reth_downloaders::{
bodies::bodies::BodiesDownloaderBuilder,
Expand Down Expand Up @@ -332,12 +332,13 @@ where
let provider_factory =
create_test_provider_factory_with_chain_spec(self.base_config.chain_spec.clone());

let consensus: Arc<dyn FullConsensus> = match self.base_config.consensus {
TestConsensusConfig::Real => {
Arc::new(EthBeaconConsensus::new(Arc::clone(&self.base_config.chain_spec)))
}
TestConsensusConfig::Test => Arc::new(TestConsensus::default()),
};
let consensus: Arc<dyn FullConsensus<Error = ConsensusError>> =
match self.base_config.consensus {
TestConsensusConfig::Real => {
Arc::new(EthBeaconConsensus::new(Arc::clone(&self.base_config.chain_spec)))
}
TestConsensusConfig::Test => Arc::new(TestConsensus::default()),
};
let payload_builder = spawn_test_payload_service::<EthEngineTypes>();

// use either noop client or a user provided client (for example TestFullBlockClient)
Expand Down
12 changes: 7 additions & 5 deletions crates/consensus/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ pub trait FullConsensus<N: NodePrimitives = EthPrimitives>:
/// Consensus is a protocol that chooses canonical chain.
#[auto_impl::auto_impl(&, Arc)]
pub trait Consensus<H = Header, B = BlockBody>: AsHeaderValidator<H> {
/// The error type related to consensus.
type Error;

/// Ensures that body field values match the header.
fn validate_body_against_header(
&self,
body: &B,
header: &SealedHeader<H>,
) -> Result<(), ConsensusError>;
) -> Result<(), Self::Error>;

/// Validate a block disregarding world state, i.e. things that can be checked before sender
/// recovery and execution.
Expand All @@ -82,8 +85,7 @@ pub trait Consensus<H = Header, B = BlockBody>: AsHeaderValidator<H> {
/// **This should not be called for the genesis block**.
///
/// Note: validating blocks does not include other validations of the Consensus
fn validate_block_pre_execution(&self, block: &SealedBlock<H, B>)
-> Result<(), ConsensusError>;
fn validate_block_pre_execution(&self, block: &SealedBlock<H, B>) -> Result<(), Self::Error>;
}

/// HeaderValidator is a protocol that validates headers and their relationships.
Expand Down Expand Up @@ -170,13 +172,13 @@ impl<T: HeaderValidator<H>, H> AsHeaderValidator<H> for T {
/// Helper trait to cast `Arc<dyn FullConsensus>` to `Arc<dyn Consensus>`
pub trait AsConsensus<H, B>: Consensus<H, B> {
/// Converts the [`Arc`] of self to [`Arc`] of [`HeaderValidator`]
fn as_consensus<'a>(self: Arc<Self>) -> Arc<dyn Consensus<H, B> + 'a>
fn as_consensus<'a>(self: Arc<Self>) -> Arc<dyn Consensus<H, B, Error = Self::Error> + 'a>
where
Self: 'a;
}

impl<T: Consensus<H, B>, H, B> AsConsensus<H, B> for T {
fn as_consensus<'a>(self: Arc<Self>) -> Arc<dyn Consensus<H, B> + 'a>
fn as_consensus<'a>(self: Arc<Self>) -> Arc<dyn Consensus<H, B, Error = Self::Error> + 'a>
where
Self: 'a,
{
Expand Down
11 changes: 5 additions & 6 deletions crates/consensus/consensus/src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ impl<H> HeaderValidator<H> for NoopConsensus {
}

impl<H, B> Consensus<H, B> for NoopConsensus {
type Error = ConsensusError;

fn validate_body_against_header(
&self,
_body: &B,
_header: &SealedHeader<H>,
) -> Result<(), ConsensusError> {
) -> Result<(), Self::Error> {
Ok(())
}

fn validate_block_pre_execution(
&self,
_block: &SealedBlock<H, B>,
) -> Result<(), ConsensusError> {
fn validate_block_pre_execution(&self, _block: &SealedBlock<H, B>) -> Result<(), Self::Error> {
Ok(())
}
}
Expand All @@ -51,7 +50,7 @@ impl<N: NodePrimitives> FullConsensus<N> for NoopConsensus {
&self,
_block: &BlockWithSenders<N::Block>,
_input: PostExecutionInput<'_, N::Receipt>,
) -> Result<(), ConsensusError> {
) -> Result<(), Self::Error> {
Ok(())
}
}
9 changes: 4 additions & 5 deletions crates/consensus/consensus/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,21 @@ impl<N: NodePrimitives> FullConsensus<N> for TestConsensus {
}

impl<H, B> Consensus<H, B> for TestConsensus {
type Error = ConsensusError;

fn validate_body_against_header(
&self,
_body: &B,
_header: &SealedHeader<H>,
) -> Result<(), ConsensusError> {
) -> Result<(), Self::Error> {
if self.fail_body_against_header() {
Err(ConsensusError::BaseFeeMissing)
} else {
Ok(())
}
}

fn validate_block_pre_execution(
&self,
_block: &SealedBlock<H, B>,
) -> Result<(), ConsensusError> {
fn validate_block_pre_execution(&self, _block: &SealedBlock<H, B>) -> Result<(), Self::Error> {
if self.fail_validation() {
Err(ConsensusError::BaseFeeMissing)
} else {
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/local/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::miner::{LocalMiner, MiningMode};
use futures_util::{Stream, StreamExt};
use reth_beacon_consensus::{BeaconConsensusEngineEvent, EngineNodeTypes};
use reth_chainspec::EthChainSpec;
use reth_consensus::FullConsensus;
use reth_consensus::{ConsensusError, FullConsensus};
use reth_engine_primitives::{BeaconEngineMessage, EngineValidator};
use reth_engine_service::service::EngineMessageStream;
use reth_engine_tree::{
Expand Down Expand Up @@ -64,7 +64,7 @@ where
/// Constructor for [`LocalEngineService`].
#[allow(clippy::too_many_arguments)]
pub fn new<B, V>(
consensus: Arc<dyn FullConsensus<N::Primitives>>,
consensus: Arc<dyn FullConsensus<N::Primitives, Error = ConsensusError>>,
executor_factory: impl BlockExecutorProvider<Primitives = N::Primitives>,
provider: ProviderFactory<N>,
blockchain_db: BlockchainProvider2<N>,
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use futures::{Stream, StreamExt};
use pin_project::pin_project;
use reth_beacon_consensus::{BeaconConsensusEngineEvent, EngineNodeTypes};
use reth_chainspec::EthChainSpec;
use reth_consensus::FullConsensus;
use reth_consensus::{ConsensusError, FullConsensus};
use reth_engine_primitives::{BeaconEngineMessage, EngineValidator};
use reth_engine_tree::{
backfill::PipelineSync,
Expand Down Expand Up @@ -69,7 +69,7 @@ where
/// Constructor for `EngineService`.
#[allow(clippy::too_many_arguments)]
pub fn new<V>(
consensus: Arc<dyn FullConsensus<N::Primitives>>,
consensus: Arc<dyn FullConsensus<N::Primitives, Error = ConsensusError>>,
executor_factory: E,
chain_spec: Arc<N::ChainSpec>,
client: Client,
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/tree/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{engine::DownloadRequest, metrics::BlockDownloaderMetrics};
use alloy_consensus::BlockHeader;
use alloy_primitives::B256;
use futures::FutureExt;
use reth_consensus::Consensus;
use reth_consensus::{Consensus, ConsensusError};
use reth_network_p2p::{
full_block::{FetchFullBlockFuture, FetchFullBlockRangeFuture, FullBlockClient},
BlockClient,
Expand Down Expand Up @@ -84,7 +84,7 @@ where
/// Create a new instance
pub fn new(
client: Client,
consensus: Arc<dyn Consensus<Client::Header, Client::Body>>,
consensus: Arc<dyn Consensus<Client::Header, Client::Body, Error = ConsensusError>>,
) -> Self {
Self {
full_block_client: FullBlockClient::new(client, consensus),
Expand Down
6 changes: 3 additions & 3 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ where
{
provider: P,
executor_provider: E,
consensus: Arc<dyn FullConsensus<N>>,
consensus: Arc<dyn FullConsensus<N, Error = ConsensusError>>,
payload_validator: V,
/// Keeps track of internals such as executed and buffered blocks.
state: EngineApiTreeState<N>,
Expand Down Expand Up @@ -561,7 +561,7 @@ where
pub fn new(
provider: P,
executor_provider: E,
consensus: Arc<dyn FullConsensus<N>>,
consensus: Arc<dyn FullConsensus<N, Error = ConsensusError>>,
payload_validator: V,
outgoing: UnboundedSender<EngineApiEvent<N>>,
state: EngineApiTreeState<N>,
Expand Down Expand Up @@ -609,7 +609,7 @@ where
pub fn spawn_new(
provider: P,
executor_provider: E,
consensus: Arc<dyn FullConsensus<N>>,
consensus: Arc<dyn FullConsensus<N, Error = ConsensusError>>,
payload_validator: V,
persistence: PersistenceHandle<N>,
payload_builder: PayloadBuilderHandle<T>,
Expand Down
9 changes: 4 additions & 5 deletions crates/ethereum/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,17 @@ where
H: BlockHeader,
B: BlockBody,
{
type Error = ConsensusError;

fn validate_body_against_header(
&self,
body: &B,
header: &SealedHeader<H>,
) -> Result<(), ConsensusError> {
) -> Result<(), Self::Error> {
validate_body_against_header(body, header.header())
}

fn validate_block_pre_execution(
&self,
block: &SealedBlock<H, B>,
) -> Result<(), ConsensusError> {
fn validate_block_pre_execution(&self, block: &SealedBlock<H, B>) -> Result<(), Self::Error> {
validate_block_pre_execution(block, &self.chain_spec)
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/ethereum/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::{EthEngineTypes, EthEvmConfig};
use reth_beacon_consensus::EthBeaconConsensus;
use reth_chainspec::ChainSpec;
use reth_consensus::{ConsensusError, FullConsensus};
use reth_ethereum_engine_primitives::{
EthBuiltPayload, EthPayloadAttributes, EthPayloadBuilderAttributes,
};
Expand Down Expand Up @@ -252,7 +253,7 @@ impl<Node> ConsensusBuilder<Node> for EthereumConsensusBuilder
where
Node: FullNodeTypes<Types: NodeTypes<ChainSpec = ChainSpec, Primitives = EthPrimitives>>,
{
type Consensus = Arc<dyn reth_consensus::FullConsensus>;
type Consensus = Arc<dyn FullConsensus<Error = ConsensusError>>;

async fn build_consensus(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::Consensus> {
Ok(Arc::new(EthBeaconConsensus::new(ctx.chain_spec())))
Expand Down
Loading
Loading