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

feat: use OpChainSpec in OptimismNode and its components #11304

Merged
merged 10 commits into from
Sep 28, 2024
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions crates/chainspec/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::{ChainSpec, DepositContract};
use alloc::vec::Vec;
use alloy_chains::Chain;
use alloy_eips::eip1559::BaseFeeParams;
use alloy_genesis::Genesis;
use alloy_primitives::B256;
use core::fmt::{Debug, Display};
use reth_network_peers::NodeRecord;
use reth_primitives_traits::Header;

/// Trait representing type configuring a chain spec.
Expand Down Expand Up @@ -41,6 +43,9 @@ pub trait EthChainSpec: Send + Sync + Unpin + Debug {

/// The block gas limit.
fn max_gas_limit(&self) -> u64;

/// The bootnodes for the chain, if any.
fn bootnodes(&self) -> Option<Vec<NodeRecord>>;
}

impl EthChainSpec for ChainSpec {
Expand Down Expand Up @@ -83,4 +88,8 @@ impl EthChainSpec for ChainSpec {
fn max_gas_limit(&self) -> u64 {
self.max_gas_limit
}

fn bootnodes(&self) -> Option<Vec<NodeRecord>> {
self.bootnodes()
}
}
4 changes: 2 additions & 2 deletions crates/chainspec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,13 +825,13 @@ fn into_optimism_chain_spec(genesis: Genesis) -> ChainSpec {
}
}

/// A trait for reading the current [`ChainSpec`].
/// A trait for reading the current chainspec.
#[auto_impl::auto_impl(&, Arc)]
pub trait ChainSpecProvider: Send + Sync {
/// The chain spec type.
type ChainSpec: EthChainSpec + 'static;

/// Get an [`Arc`] to the [`ChainSpec`].
/// Get an [`Arc`] to the chainspec.
fn chain_spec(&self) -> Arc<Self::ChainSpec>;
}

Expand Down
8 changes: 4 additions & 4 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::ChainSpec;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
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 @@ -50,14 +50,14 @@ pub struct EnvironmentArgs<C: ChainSpecParser> {
pub db: DatabaseArgs,
}

impl<C: ChainSpecParser<ChainSpec = ChainSpec>> EnvironmentArgs<C> {
impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> EnvironmentArgs<C> {
Copy link
Collaborator

@mattsse mattsse Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're using this combo a few times,
what we could do is introduce a helper trait that combines both, but perhaps not useful. maybe we introduce EthereumChainSpec and rename EthChainSpec

/// Initializes environment according to [`AccessRights`] and returns an instance of
/// [`Environment`].
pub fn init<N: NodeTypesWithEngine<ChainSpec = C::ChainSpec>>(
&self,
access: AccessRights,
) -> eyre::Result<Environment<N>> {
let data_dir = self.datadir.clone().resolve_datadir(self.chain.chain);
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 @@ -93,7 +93,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> EnvironmentArgs<C> {

let provider_factory = self.create_provider_factory(&config, db, sfp)?;
if access.is_read_write() {
debug!(target: "reth::cli", chain=%self.chain.chain, genesis=?self.chain.genesis_hash(), "Initializing genesis");
debug!(target: "reth::cli", chain=%self.chain.chain(), genesis=?self.chain.genesis_hash(), "Initializing genesis");
init_genesis(&provider_factory)?;
}

Expand Down
9 changes: 4 additions & 5 deletions crates/cli/commands/src/db/checksum.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::db::get::{maybe_json_value_parser, table_key};
use ahash::RandomState;
use clap::Parser;
use reth_chainspec::ChainSpec;
use reth_chainspec::EthereumHardforks;
use reth_db::{DatabaseEnv, RawKey, RawTable, RawValue, TableViewer, Tables};
use reth_db_api::{cursor::DbCursorRO, table::Table, transaction::DbTx};
use reth_db_common::DbTool;
use reth_node_builder::{NodeTypesWithDB, NodeTypesWithDBAdapter, NodeTypesWithEngine};
use reth_provider::providers::ProviderNodeTypes;
use std::{
hash::{BuildHasher, Hasher},
sync::Arc,
Expand Down Expand Up @@ -35,7 +36,7 @@ pub struct Command {

impl Command {
/// Execute `db checksum` command
pub fn execute<N: NodeTypesWithEngine<ChainSpec = ChainSpec>>(
pub fn execute<N: NodeTypesWithEngine<ChainSpec: EthereumHardforks>>(
self,
tool: &DbTool<NodeTypesWithDBAdapter<N, Arc<DatabaseEnv>>>,
) -> eyre::Result<()> {
Expand Down Expand Up @@ -63,9 +64,7 @@ impl<N: NodeTypesWithDB> ChecksumViewer<'_, N> {
}
}

impl<N: NodeTypesWithDB<ChainSpec = ChainSpec>> TableViewer<(u64, Duration)>
for ChecksumViewer<'_, N>
{
impl<N: ProviderNodeTypes> TableViewer<(u64, Duration)> for ChecksumViewer<'_, N> {
type Error = eyre::Report;

fn view<T: Table>(&self) -> Result<(u64, Duration), Self::Error> {
Expand Down
14 changes: 5 additions & 9 deletions crates/cli/commands/src/db/get.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use alloy_primitives::BlockHash;
use alloy_primitives::{hex, BlockHash};
use clap::Parser;
use reth_chainspec::ChainSpec;
use reth_db::{
static_file::{ColumnSelectorOne, ColumnSelectorTwo, HeaderMask, ReceiptMask, TransactionMask},
tables, RawKey, RawTable, Receipts, TableViewer, Transactions,
};
use reth_db_api::table::{Decompress, DupSort, Table};
use reth_db_common::DbTool;
use reth_node_builder::NodeTypesWithDB;
use reth_primitives::{hex, Header};
use reth_provider::StaticFileProviderFactory;
use reth_primitives::Header;
use reth_provider::{providers::ProviderNodeTypes, StaticFileProviderFactory};
use reth_static_file_types::StaticFileSegment;
use tracing::error;

Expand Down Expand Up @@ -54,10 +53,7 @@ enum Subcommand {

impl Command {
/// Execute `db get` command
pub fn execute<N: NodeTypesWithDB<ChainSpec = ChainSpec>>(
self,
tool: &DbTool<N>,
) -> eyre::Result<()> {
pub fn execute<N: ProviderNodeTypes>(self, tool: &DbTool<N>) -> eyre::Result<()> {
match self.subcommand {
Subcommand::Mdbx { table, key, subkey, raw } => {
table.view(&GetValueViewer { tool, key, subkey, raw })?
Expand Down Expand Up @@ -148,7 +144,7 @@ struct GetValueViewer<'a, N: NodeTypesWithDB> {
raw: bool,
}

impl<N: NodeTypesWithDB<ChainSpec = ChainSpec>> TableViewer<()> for GetValueViewer<'_, N> {
impl<N: ProviderNodeTypes> TableViewer<()> for GetValueViewer<'_, N> {
type Error = eyre::Report;

fn view<T: Table>(&self) -> Result<(), Self::Error> {
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/commands/src/db/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::tui::DbListTUI;
use alloy_primitives::hex;
use clap::Parser;
use eyre::WrapErr;
use reth_chainspec::ChainSpec;
use reth_chainspec::EthereumHardforks;
use reth_db::{DatabaseEnv, RawValue, TableViewer, Tables};
use reth_db_api::{database::Database, table::Table};
use reth_db_common::{DbTool, ListFilter};
Expand Down Expand Up @@ -53,7 +53,7 @@ pub struct Command {

impl Command {
/// Execute `db list` command
pub fn execute<N: NodeTypesWithEngine<ChainSpec = ChainSpec>>(
pub fn execute<N: NodeTypesWithEngine<ChainSpec: EthereumHardforks>>(
self,
tool: &DbTool<NodeTypesWithDBAdapter<N, Arc<DatabaseEnv>>>,
) -> eyre::Result<()> {
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/commands/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::{AccessRights, Environment, EnvironmentArgs};
use clap::{Parser, Subcommand};
use reth_chainspec::ChainSpec;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_cli::chainspec::ChainSpecParser;
use reth_db::version::{get_db_version, DatabaseVersionError, DB_VERSION};
use reth_db_common::DbTool;
Expand Down Expand Up @@ -63,12 +63,12 @@ macro_rules! db_ro_exec {
};
}

impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C> {
/// Execute `db` command
pub async fn execute<N: NodeTypesWithEngine<ChainSpec = C::ChainSpec>>(
self,
) -> eyre::Result<()> {
let data_dir = self.env.datadir.clone().resolve_datadir(self.env.chain.chain);
let data_dir = self.env.datadir.clone().resolve_datadir(self.env.chain.chain());
let db_path = data_dir.db();
let static_files_path = data_dir.static_files();

Expand Down
11 changes: 4 additions & 7 deletions crates/cli/commands/src/db/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use comfy_table::{Cell, Row, Table as ComfyTable};
use eyre::WrapErr;
use human_bytes::human_bytes;
use itertools::Itertools;
use reth_chainspec::ChainSpec;
use reth_chainspec::EthereumHardforks;
use reth_db::{mdbx, static_file::iter_static_files, DatabaseEnv, TableViewer, Tables};
use reth_db_api::database::Database;
use reth_db_common::DbTool;
use reth_fs_util as fs;
use reth_node_builder::{NodeTypesWithDB, NodeTypesWithDBAdapter, NodeTypesWithEngine};
use reth_node_core::dirs::{ChainPath, DataDirPath};
use reth_provider::providers::StaticFileProvider;
use reth_provider::providers::{ProviderNodeTypes, StaticFileProvider};
use reth_static_file_types::SegmentRangeInclusive;
use std::{sync::Arc, time::Duration};

Expand All @@ -38,7 +38,7 @@ pub struct Command {

impl Command {
/// Execute `db stats` command
pub fn execute<N: NodeTypesWithEngine<ChainSpec = ChainSpec>>(
pub fn execute<N: NodeTypesWithEngine<ChainSpec: EthereumHardforks>>(
self,
data_dir: ChainPath<DataDirPath>,
tool: &DbTool<NodeTypesWithDBAdapter<N, Arc<DatabaseEnv>>>,
Expand Down Expand Up @@ -325,10 +325,7 @@ impl Command {
Ok(table)
}

fn checksum_report<N: NodeTypesWithDB<ChainSpec = ChainSpec>>(
&self,
tool: &DbTool<N>,
) -> eyre::Result<ComfyTable> {
fn checksum_report<N: ProviderNodeTypes>(&self, tool: &DbTool<N>) -> eyre::Result<ComfyTable> {
let mut table = ComfyTable::new();
table.load_preset(comfy_table::presets::ASCII_MARKDOWN);
table.set_header(vec![Cell::new("Table"), Cell::new("Checksum"), Cell::new("Elapsed")]);
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/commands/src/dump_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::sync::Arc;

use clap::Parser;
use reth_chainspec::ChainSpec;
use reth_chainspec::EthChainSpec;
use reth_cli::chainspec::ChainSpecParser;

/// Dumps genesis block JSON configuration to stdout
Expand All @@ -21,7 +21,7 @@ pub struct DumpGenesisCommand<C: ChainSpecParser> {
chain: Arc<C::ChainSpec>,
}

impl<C: ChainSpecParser<ChainSpec = ChainSpec>> DumpGenesisCommand<C> {
impl<C: ChainSpecParser<ChainSpec: EthChainSpec>> DumpGenesisCommand<C> {
/// Execute the `dump-genesis` command
pub async fn execute(self) -> eyre::Result<()> {
println!("{}", serde_json::to_string_pretty(self.chain.genesis())?);
Expand Down
12 changes: 6 additions & 6 deletions crates/cli/commands/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloy_primitives::B256;
use clap::Parser;
use futures::{Stream, StreamExt};
use reth_beacon_consensus::EthBeaconConsensus;
use reth_chainspec::ChainSpec;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_cli::chainspec::ChainSpecParser;
use reth_config::Config;
use reth_consensus::Consensus;
Expand All @@ -20,12 +20,12 @@ use reth_network_p2p::{
bodies::downloader::BodyDownloader,
headers::downloader::{HeaderDownloader, SyncTarget},
};
use reth_node_builder::{NodeTypesWithDB, NodeTypesWithEngine};
use reth_node_builder::NodeTypesWithEngine;
use reth_node_core::version::SHORT_VERSION;
use reth_node_events::node::NodeEvent;
use reth_provider::{
BlockNumReader, ChainSpecProvider, HeaderProvider, ProviderError, ProviderFactory,
StageCheckpointReader,
providers::ProviderNodeTypes, BlockNumReader, ChainSpecProvider, HeaderProvider, ProviderError,
ProviderFactory, StageCheckpointReader,
};
use reth_prune::PruneModes;
use reth_stages::{prelude::*, Pipeline, StageId, StageSet};
Expand Down Expand Up @@ -56,7 +56,7 @@ pub struct ImportCommand<C: ChainSpecParser> {
path: PathBuf,
}

impl<C: ChainSpecParser<ChainSpec = ChainSpec>> ImportCommand<C> {
impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> ImportCommand<C> {
/// Execute `import` command
pub async fn execute<N, E, F>(self, executor: F) -> eyre::Result<()>
where
Expand Down Expand Up @@ -168,7 +168,7 @@ pub fn build_import_pipeline<N, C, E>(
executor: E,
) -> eyre::Result<(Pipeline<N>, impl Stream<Item = NodeEvent>)>
where
N: NodeTypesWithDB<ChainSpec = ChainSpec>,
N: ProviderNodeTypes,
C: Consensus + 'static,
E: BlockExecutorProvider,
{
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/commands/src/init_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::common::{AccessRights, Environment, EnvironmentArgs};
use clap::Parser;
use reth_chainspec::ChainSpec;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_cli::chainspec::ChainSpecParser;
use reth_node_builder::NodeTypesWithEngine;
use reth_provider::BlockHashReader;
Expand All @@ -15,7 +15,7 @@ pub struct InitCommand<C: ChainSpecParser> {
env: EnvironmentArgs<C>,
}

impl<C: ChainSpecParser<ChainSpec = ChainSpec>> InitCommand<C> {
impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> InitCommand<C> {
/// Execute the `init` command
pub async fn execute<N: NodeTypesWithEngine<ChainSpec = C::ChainSpec>>(
self,
Expand Down
10 changes: 5 additions & 5 deletions crates/cli/commands/src/init_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
use crate::common::{AccessRights, Environment, EnvironmentArgs};
use alloy_primitives::B256;
use clap::Parser;
use reth_chainspec::ChainSpec;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_cli::chainspec::ChainSpecParser;
use reth_config::config::EtlConfig;
use reth_db_common::init::init_from_state_dump;
use reth_node_builder::{NodeTypesWithDB, NodeTypesWithEngine};
use reth_provider::ProviderFactory;
use reth_node_builder::NodeTypesWithEngine;
use reth_provider::{providers::ProviderNodeTypes, ProviderFactory};

use std::{fs::File, io::BufReader, path::PathBuf};
use tracing::info;
Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct InitStateCommand<C: ChainSpecParser> {
pub state: PathBuf,
}

impl<C: ChainSpecParser<ChainSpec = ChainSpec>> InitStateCommand<C> {
impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> InitStateCommand<C> {
/// Execute the `init` command
pub async fn execute<N: NodeTypesWithEngine<ChainSpec = C::ChainSpec>>(
self,
Expand All @@ -59,7 +59,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> InitStateCommand<C> {
}

/// Initialize chain with state at specific block, from a file with state dump.
pub fn init_at_state<N: NodeTypesWithDB<ChainSpec = ChainSpec>>(
pub fn init_at_state<N: ProviderNodeTypes>(
state_dump_path: PathBuf,
factory: ProviderFactory<N>,
etl_config: EtlConfig,
Expand Down
10 changes: 7 additions & 3 deletions crates/cli/commands/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Main node command for launching a node

use clap::{value_parser, Args, Parser};
use reth_chainspec::ChainSpec;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_runner::CliContext;
use reth_cli_util::parse_socket_address;
Expand Down Expand Up @@ -112,7 +112,7 @@ pub struct NodeCommand<
pub ext: Ext,
}

impl<C: ChainSpecParser<ChainSpec = ChainSpec>> NodeCommand<C> {
impl<C: ChainSpecParser> NodeCommand<C> {
/// Parsers only the default CLI arguments
pub fn parse_args() -> Self {
Self::parse()
Expand All @@ -128,7 +128,11 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> NodeCommand<C> {
}
}

impl<C: ChainSpecParser<ChainSpec = ChainSpec>, Ext: clap::Args + fmt::Debug> NodeCommand<C, Ext> {
impl<
C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>,
Ext: clap::Args + fmt::Debug,
> NodeCommand<C, Ext>
{
/// Launches the node
///
/// This transforms the node command into a node config and launches the node using the given
Expand Down
Loading
Loading