feat: add Header AT to EthChainSpec (#13046)

This commit is contained in:
Arsenii Kulikov
2024-12-02 04:56:00 +04:00
committed by GitHub
parent 7f88e62781
commit 3f93f35c20
7 changed files with 39 additions and 24 deletions

View File

@ -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;
@ -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;
@ -64,6 +67,8 @@ pub trait EthChainSpec: Send + Sync + Unpin + Debug {
}
impl EthChainSpec for ChainSpec {
type Header = Header;
fn chain(&self) -> Chain {
self.chain
}
@ -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()
}

View File

@ -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};
@ -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();
@ -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();

View File

@ -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};
@ -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)?;

View File

@ -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;
@ -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.
@ -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 {

View File

@ -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.
@ -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,
{
@ -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,
{
@ -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,
{

View File

@ -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()
}
@ -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()
}

View File

@ -70,7 +70,7 @@ 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
@ -78,6 +78,7 @@ where
+ StateWriter
+ StateWriter
+ AsRef<PF::ProviderRW>,
PF::ChainSpec: EthChainSpec<Header = reth_primitives::Header>,
{
let chain = factory.chain_spec();
@ -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();