Skip to content

Commit

Permalink
feat: add Header AT to EthChainSpec (#13046)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Dec 2, 2024
1 parent 7f88e62 commit 3f93f35
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
9 changes: 7 additions & 2 deletions crates/chainspec/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub trait EthChainSpec: Send + Sync + Unpin + Debug {
// todo: make chain spec type generic over hardfork
//type Hardfork: Clone + Copy + 'static;

/// The header type of the network.
type Header;

/// Returns the [`Chain`] object this spec targets.
fn chain(&self) -> Chain;

Expand Down Expand Up @@ -41,7 +44,7 @@ pub trait EthChainSpec: Send + Sync + Unpin + Debug {
fn display_hardforks(&self) -> Box<dyn Display>;

/// The genesis header.
fn genesis_header(&self) -> &Header;
fn genesis_header(&self) -> &Self::Header;

/// The genesis block specification.
fn genesis(&self) -> &Genesis;
Expand All @@ -64,6 +67,8 @@ pub trait EthChainSpec: Send + Sync + Unpin + Debug {
}

impl EthChainSpec for ChainSpec {
type Header = Header;

fn chain(&self) -> Chain {
self.chain
}
Expand Down Expand Up @@ -92,7 +97,7 @@ impl EthChainSpec for ChainSpec {
Box::new(Self::display_hardforks(self))
}

fn genesis_header(&self) -> &Header {
fn genesis_header(&self) -> &Self::Header {
self.genesis_header()
}

Expand Down
19 changes: 11 additions & 8 deletions crates/cli/commands/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use alloy_primitives::B256;
use clap::Parser;
use reth_beacon_consensus::EthBeaconConsensus;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_chainspec::EthChainSpec;
use reth_cli::chainspec::ChainSpecParser;
use reth_config::{config::EtlConfig, Config};
use reth_db::{init_db, open_db_read_only, DatabaseEnv};
Expand Down Expand Up @@ -54,13 +54,13 @@ pub struct EnvironmentArgs<C: ChainSpecParser> {
pub db: DatabaseArgs,
}

impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> EnvironmentArgs<C> {
impl<C: ChainSpecParser> EnvironmentArgs<C> {
/// Initializes environment according to [`AccessRights`] and returns an instance of
/// [`Environment`].
pub fn init<N: CliNodeTypes<ChainSpec = C::ChainSpec>>(
&self,
access: AccessRights,
) -> eyre::Result<Environment<N>> {
pub fn init<N: CliNodeTypes>(&self, access: AccessRights) -> eyre::Result<Environment<N>>
where
C: ChainSpecParser<ChainSpec = N::ChainSpec>,
{
let data_dir = self.datadir.clone().resolve_datadir(self.chain.chain());
let db_path = data_dir.db();
let sf_path = data_dir.static_files();
Expand Down Expand Up @@ -109,12 +109,15 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Environmen
/// If it's a read-write environment and an issue is found, it will attempt to heal (including a
/// pipeline unwind). Otherwise, it will print out an warning, advising the user to restart the
/// node to heal.
fn create_provider_factory<N: CliNodeTypes<ChainSpec = C::ChainSpec>>(
fn create_provider_factory<N: CliNodeTypes>(
&self,
config: &Config,
db: Arc<DatabaseEnv>,
static_file_provider: StaticFileProvider<N::Primitives>,
) -> eyre::Result<ProviderFactory<NodeTypesWithDBAdapter<N, Arc<DatabaseEnv>>>> {
) -> eyre::Result<ProviderFactory<NodeTypesWithDBAdapter<N, Arc<DatabaseEnv>>>>
where
C: ChainSpecParser<ChainSpec = N::ChainSpec>,
{
let has_receipt_pruning = config.prune.as_ref().is_some_and(|a| a.has_receipts_pruning());
let prune_modes =
config.prune.as_ref().map(|prune| prune.segments.clone()).unwrap_or_default();
Expand Down
9 changes: 6 additions & 3 deletions crates/cli/commands/src/stage/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::common::{AccessRights, CliNodeTypes, Environment, EnvironmentArgs};
use clap::Parser;
use itertools::Itertools;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_chainspec::EthChainSpec;
use reth_cli::chainspec::ChainSpecParser;
use reth_db::{mdbx::tx::Tx, static_file::iter_static_files, tables, DatabaseError};
use reth_db_api::transaction::{DbTx, DbTxMut};
Expand All @@ -27,9 +27,12 @@ pub struct Command<C: ChainSpecParser> {
stage: StageEnum,
}

impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C> {
impl<C: ChainSpecParser> Command<C> {
/// Execute `db` command
pub async fn execute<N: CliNodeTypes<ChainSpec = C::ChainSpec>>(self) -> eyre::Result<()> {
pub async fn execute<N: CliNodeTypes>(self) -> eyre::Result<()>
where
C: ChainSpecParser<ChainSpec = N::ChainSpec>,
{
let Environment { provider_factory, .. } = self.env.init::<N>(AccessRights::RW)?;

let tool = DbTool::new(provider_factory)?;
Expand Down
9 changes: 5 additions & 4 deletions crates/exex/exex/src/dyn_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::fmt::Debug;

use reth_chainspec::{EthChainSpec, Head};
use reth_node_api::{FullNodeComponents, NodePrimitives, NodeTypes};
use reth_node_api::{FullNodeComponents, HeaderTy, NodePrimitives, NodeTypes};
use reth_node_core::node_config::NodeConfig;
use reth_primitives::EthPrimitives;
use reth_provider::BlockReader;
Expand All @@ -18,7 +18,7 @@ pub struct ExExContextDyn<N: NodePrimitives = EthPrimitives> {
/// The current head of the blockchain at launch.
pub head: Head,
/// The config of the node
pub config: NodeConfig<Box<dyn EthChainSpec + 'static>>,
pub config: NodeConfig<Box<dyn EthChainSpec<Header = N::BlockHeader> + 'static>>,
/// The loaded node config
pub reth_config: reth_config::Config,
/// Channel used to send [`ExExEvent`]s to the rest of the node.
Expand Down Expand Up @@ -57,8 +57,9 @@ where
Node::Executor: Debug,
{
fn from(ctx: ExExContext<Node>) -> Self {
let config =
ctx.config.map_chainspec(|chainspec| Box::new(chainspec) as Box<dyn EthChainSpec>);
let config = ctx.config.map_chainspec(|chainspec| {
Box::new(chainspec) as Box<dyn EthChainSpec<Header = HeaderTy<Node::Types>>>
});
let notifications = Box::new(ctx.notifications) as Box<_>;

Self {
Expand Down
8 changes: 4 additions & 4 deletions crates/node/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait NodeTypes: Send + Sync + Unpin + 'static {
/// The node's primitive types, defining basic operations and structures.
type Primitives: NodePrimitives;
/// The type used for configuration of the EVM.
type ChainSpec: EthChainSpec;
type ChainSpec: EthChainSpec<Header = <Self::Primitives as NodePrimitives>::BlockHeader>;
/// The type used to perform state commitment operations.
type StateCommitment: StateCommitment;
/// The type responsible for writing chain primitives to storage.
Expand Down Expand Up @@ -151,7 +151,7 @@ impl<P, C, SC, S> AnyNodeTypes<P, C, SC, S> {
impl<P, C, SC, S> NodeTypes for AnyNodeTypes<P, C, SC, S>
where
P: NodePrimitives + Send + Sync + Unpin + 'static,
C: EthChainSpec + 'static,
C: EthChainSpec<Header = P::BlockHeader> + 'static,
SC: StateCommitment,
S: Default + Send + Sync + Unpin + Debug + 'static,
{
Expand Down Expand Up @@ -212,7 +212,7 @@ impl<P, E, C, SC, S> NodeTypes for AnyNodeTypesWithEngine<P, E, C, SC, S>
where
P: NodePrimitives + Send + Sync + Unpin + 'static,
E: EngineTypes + Send + Sync + Unpin,
C: EthChainSpec + 'static,
C: EthChainSpec<Header = P::BlockHeader> + 'static,
SC: StateCommitment,
S: Default + Send + Sync + Unpin + Debug + 'static,
{
Expand All @@ -226,7 +226,7 @@ impl<P, E, C, SC, S> NodeTypesWithEngine for AnyNodeTypesWithEngine<P, E, C, SC,
where
P: NodePrimitives + Send + Sync + Unpin + 'static,
E: EngineTypes + Send + Sync + Unpin,
C: EthChainSpec + 'static,
C: EthChainSpec<Header = P::BlockHeader> + 'static,
SC: StateCommitment,
S: Default + Send + Sync + Unpin + Debug + 'static,
{
Expand Down
4 changes: 3 additions & 1 deletion crates/optimism/chainspec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ pub fn decode_holocene_1559_params(extra_data: Bytes) -> Result<(u32, u32), Deco
}

impl EthChainSpec for OpChainSpec {
type Header = Header;

fn chain(&self) -> alloy_chains::Chain {
self.inner.chain()
}
Expand Down Expand Up @@ -281,7 +283,7 @@ impl EthChainSpec for OpChainSpec {
Box::new(ChainSpec::display_hardforks(self))
}

fn genesis_header(&self) -> &Header {
fn genesis_header(&self) -> &Self::Header {
self.inner.genesis_header()
}

Expand Down
5 changes: 3 additions & 2 deletions crates/storage/db-common/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ impl From<DatabaseError> for InitDatabaseError {
pub fn init_genesis<PF>(factory: &PF) -> Result<B256, InitDatabaseError>
where
PF: DatabaseProviderFactory + StaticFileProviderFactory + ChainSpecProvider + BlockHashReader,
PF::ProviderRW: StaticFileProviderFactory
PF::ProviderRW: StaticFileProviderFactory<Primitives = PF::Primitives>
+ StageCheckpointWriter
+ HistoryWriter
+ HeaderProvider
+ HashingWriter
+ StateWriter
+ StateWriter
+ AsRef<PF::ProviderRW>,
PF::ChainSpec: EthChainSpec<Header = reth_primitives::Header>,
{
let chain = factory.chain_spec();

Expand Down Expand Up @@ -307,7 +308,7 @@ pub fn insert_genesis_header<Provider, Spec>(
) -> ProviderResult<()>
where
Provider: StaticFileProviderFactory + DBProvider<Tx: DbTxMut>,
Spec: EthChainSpec,
Spec: EthChainSpec<Header = reth_primitives::Header>,
{
let (header, block_hash) = (chain.genesis_header(), chain.genesis_hash());
let static_file_provider = provider.static_file_provider();
Expand Down

0 comments on commit 3f93f35

Please sign in to comment.