Skip to content

Commit

Permalink
refactor(node-builder): use datadir provided by the config (#8592)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Jun 4, 2024
1 parent d59fcf5 commit ae9ab69
Show file tree
Hide file tree
Showing 26 changed files with 71 additions and 116 deletions.
2 changes: 1 addition & 1 deletion bin/reth/src/commands/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<Ext: clap::Args + fmt::Debug> NodeCommand<Ext> {

let builder = NodeBuilder::new(node_config)
.with_database(database)
.with_launch_context(ctx.task_executor, data_dir);
.with_launch_context(ctx.task_executor);

launcher(builder, ext).await
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ where
type Pool = EthTransactionPool<Node::Provider, DiskFileBlobStore>;

async fn build_pool(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::Pool> {
let data_dir = ctx.data_dir();
let data_dir = ctx.config().datadir();
let pool_config = ctx.pool_config();
let blob_store = DiskFileBlobStore::open(data_dir.blobstore(), Default::default())?;
let validator = TransactionValidationTaskExecutor::eth_builder(ctx.chain_spec())
Expand Down
7 changes: 1 addition & 6 deletions crates/exex/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use reth_node_api::{FullNodeComponents, FullNodeTypes, NodeTypes};
use reth_node_core::{
dirs::{ChainPath, DataDirPath},
node_config::NodeConfig,
};
use reth_node_core::node_config::NodeConfig;
use reth_primitives::Head;
use reth_tasks::TaskExecutor;
use tokio::sync::mpsc::{Receiver, UnboundedSender};
Expand All @@ -14,8 +11,6 @@ use crate::{ExExEvent, ExExNotification};
pub struct ExExContext<Node: FullNodeComponents> {
/// The current head of the blockchain at launch.
pub head: Head,
/// The data dir of the node.
pub data_dir: ChainPath<DataDirPath>,
/// The config of the node
pub config: NodeConfig,
/// The loaded node config
Expand Down
6 changes: 3 additions & 3 deletions crates/node-core/src/node_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,10 @@ impl NodeConfig {
client: C,
executor: TaskExecutor,
head: Head,
data_dir: &ChainPath<DataDirPath>,
data_dir: ChainPath<DataDirPath>,
) -> eyre::Result<NetworkConfig<C>> {
info!(target: "reth::cli", "Connecting to P2P network");
let secret_key = self.network_secret(data_dir)?;
let secret_key = self.network_secret(&data_dir)?;
let default_peers_path = data_dir.known_peers();
Ok(self.load_network_config(config, client, executor, head, secret_key, default_peers_path))
}
Expand All @@ -326,7 +326,7 @@ impl NodeConfig {
client: C,
executor: TaskExecutor,
head: Head,
data_dir: &ChainPath<DataDirPath>,
data_dir: ChainPath<DataDirPath>,
) -> eyre::Result<NetworkBuilder<C, (), ()>>
where
C: BlockNumReader,
Expand Down
72 changes: 17 additions & 55 deletions crates/node/builder/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use reth_network::{NetworkBuilder, NetworkConfig, NetworkHandle};
use reth_node_api::{FullNodeTypes, FullNodeTypesAdapter, NodeTypes};
use reth_node_core::{
cli::config::{PayloadBuilderConfig, RethTransactionPoolConfig},
dirs::{ChainPath, DataDirPath, MaybePlatformPath},
dirs::{DataDirPath, MaybePlatformPath},
node_config::NodeConfig,
primitives::{kzg::KzgSettings, Head},
utils::write_peers_to_file,
Expand Down Expand Up @@ -161,26 +161,24 @@ impl<DB> NodeBuilder<DB> {
/// Preconfigure the builder with the context to launch the node.
///
/// This provides the task executor and the data directory for the node.
pub const fn with_launch_context(
self,
task_executor: TaskExecutor,
data_dir: ChainPath<DataDirPath>,
) -> WithLaunchContext<Self> {
WithLaunchContext { builder: self, task_executor, data_dir }
pub const fn with_launch_context(self, task_executor: TaskExecutor) -> WithLaunchContext<Self> {
WithLaunchContext { builder: self, task_executor }
}

/// Creates an _ephemeral_ preconfigured node for testing purposes.
pub fn testing_node(
self,
mut self,
task_executor: TaskExecutor,
) -> WithLaunchContext<NodeBuilder<Arc<TempDatabase<DatabaseEnv>>>> {
let path = MaybePlatformPath::<DataDirPath>::from(tempdir_path());
self.config.datadir.datadir = path.clone();

let data_dir =
path.unwrap_or_chain_default(self.config.chain.chain, self.config.datadir.clone());

let db = create_test_rw_db_with_path(data_dir.db());

WithLaunchContext { builder: self.with_database(db), task_executor, data_dir }
WithLaunchContext { builder: self.with_database(db), task_executor }
}
}

Expand Down Expand Up @@ -217,19 +215,13 @@ where
pub struct WithLaunchContext<Builder> {
builder: Builder,
task_executor: TaskExecutor,
data_dir: ChainPath<DataDirPath>,
}

impl<Builder> WithLaunchContext<Builder> {
/// Returns a reference to the task executor.
pub const fn task_executor(&self) -> &TaskExecutor {
&self.task_executor
}

/// Returns a reference to the data directory.
pub const fn data_dir(&self) -> &ChainPath<DataDirPath> {
&self.data_dir
}
}

impl<DB> WithLaunchContext<NodeBuilder<DB>>
Expand All @@ -246,11 +238,7 @@ where
where
T: NodeTypes,
{
WithLaunchContext {
builder: self.builder.with_types(),
task_executor: self.task_executor,
data_dir: self.data_dir,
}
WithLaunchContext { builder: self.builder.with_types(), task_executor: self.task_executor }
}

/// Preconfigures the node with a specific node implementation.
Expand Down Expand Up @@ -305,7 +293,6 @@ where
WithLaunchContext {
builder: self.builder.with_components(components_builder),
task_executor: self.task_executor,
data_dir: self.data_dir,
}
}
}
Expand All @@ -326,7 +313,6 @@ where
Self {
builder: self.builder.on_component_initialized(hook),
task_executor: self.task_executor,
data_dir: self.data_dir,
}
}

Expand All @@ -339,11 +325,7 @@ where
+ Send
+ 'static,
{
Self {
builder: self.builder.on_node_started(hook),
task_executor: self.task_executor,
data_dir: self.data_dir,
}
Self { builder: self.builder.on_node_started(hook), task_executor: self.task_executor }
}

/// Sets the hook that is run once the rpc server is started.
Expand All @@ -356,11 +338,7 @@ where
+ Send
+ 'static,
{
Self {
builder: self.builder.on_rpc_started(hook),
task_executor: self.task_executor,
data_dir: self.data_dir,
}
Self { builder: self.builder.on_rpc_started(hook), task_executor: self.task_executor }
}

/// Sets the hook that is run to configure the rpc modules.
Expand All @@ -372,11 +350,7 @@ where
+ Send
+ 'static,
{
Self {
builder: self.builder.extend_rpc_modules(hook),
task_executor: self.task_executor,
data_dir: self.data_dir,
}
Self { builder: self.builder.extend_rpc_modules(hook), task_executor: self.task_executor }
}

/// Installs an `ExEx` (Execution Extension) in the node.
Expand All @@ -395,17 +369,16 @@ where
Self {
builder: self.builder.install_exex(exex_id, exex),
task_executor: self.task_executor,
data_dir: self.data_dir,
}
}

/// Launches the node and returns a handle to it.
pub async fn launch(
self,
) -> eyre::Result<NodeHandle<NodeAdapter<RethFullAdapter<DB, T>, CB::Components>>> {
let Self { builder, task_executor, data_dir } = self;
let Self { builder, task_executor } = self;

let launcher = DefaultNodeLauncher::new(task_executor, data_dir);
let launcher = DefaultNodeLauncher::new(task_executor, builder.config.datadir());
builder.launch_with(launcher).await
}

Expand All @@ -425,8 +398,6 @@ pub struct BuilderContext<Node: FullNodeTypes> {
pub(crate) provider: Node::Provider,
/// The executor of the node.
pub(crate) executor: TaskExecutor,
/// The data dir of the node.
pub(crate) data_dir: ChainPath<DataDirPath>,
/// The config of the node
pub(crate) config: NodeConfig,
/// loaded config
Expand All @@ -439,11 +410,10 @@ impl<Node: FullNodeTypes> BuilderContext<Node> {
head: Head,
provider: Node::Provider,
executor: TaskExecutor,
data_dir: ChainPath<DataDirPath>,
config: NodeConfig,
reth_config: reth_config::Config,
) -> Self {
Self { head, provider, executor, data_dir, config, reth_config }
Self { head, provider, executor, config, reth_config }
}

/// Returns the configured provider to interact with the blockchain.
Expand All @@ -461,13 +431,6 @@ impl<Node: FullNodeTypes> BuilderContext<Node> {
&self.config
}

/// Returns the data dir of the node.
///
/// This gives access to all relevant files and directories of the node's datadir.
pub const fn data_dir(&self) -> &ChainPath<DataDirPath> {
&self.data_dir
}

/// Returns the executor of the node.
///
/// This can be used to execute async tasks or functions during the setup.
Expand Down Expand Up @@ -502,7 +465,7 @@ impl<Node: FullNodeTypes> BuilderContext<Node> {
self.provider.clone(),
self.executor.clone(),
self.head,
self.data_dir(),
self.config.datadir(),
)
}

Expand All @@ -514,7 +477,7 @@ impl<Node: FullNodeTypes> BuilderContext<Node> {
self.provider.clone(),
self.executor.clone(),
self.head,
self.data_dir(),
self.config.datadir(),
)
.await
}
Expand All @@ -539,7 +502,7 @@ impl<Node: FullNodeTypes> BuilderContext<Node> {
self.executor.spawn_critical("p2p txpool", txpool);
self.executor.spawn_critical("p2p eth request handler", eth);

let default_peers_path = self.data_dir().known_peers();
let default_peers_path = self.config.datadir().known_peers();
let known_peers_file = self.config.network.persistent_peers_file(default_peers_path);
self.executor.spawn_critical_with_graceful_shutdown_signal(
"p2p network task",
Expand All @@ -560,7 +523,6 @@ impl<Node: FullNodeTypes> std::fmt::Debug for BuilderContext<Node> {
.field("head", &self.head)
.field("provider", &std::any::type_name::<Node::Provider>())
.field("executor", &self.executor)
.field("data_dir", &self.data_dir)
.field("config", &self.config)
.finish()
}
Expand Down
2 changes: 0 additions & 2 deletions crates/node/builder/src/launch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ where
head,
blockchain_db.clone(),
ctx.task_executor().clone(),
ctx.data_dir().clone(),
ctx.node_config().clone(),
ctx.toml_config().clone(),
);
Expand Down Expand Up @@ -195,7 +194,6 @@ where
// create the launch context for the exex
let context = ExExContext {
head,
data_dir: ctx.data_dir().clone(),
config: ctx.node_config().clone(),
reth_config: ctx.toml_config().clone(),
components: node_adapter.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ where
type Pool = OpTransactionPool<Node::Provider, DiskFileBlobStore>;

async fn build_pool(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::Pool> {
let data_dir = ctx.data_dir();
let data_dir = ctx.config().datadir();
let blob_store = DiskFileBlobStore::open(data_dir.blobstore(), Default::default())?;

let validator = TransactionValidationTaskExecutor::eth_builder(ctx.chain_spec())
Expand Down
6 changes: 3 additions & 3 deletions crates/primitives/src/transaction/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl BlobTransaction {
// decode the _first_ list header for the rest of the transaction
let outer_header = Header::decode(data)?;
if !outer_header.list {
return Err(RlpError::Custom("PooledTransactions blob tx must be encoded as a list"));
return Err(RlpError::Custom("PooledTransactions blob tx must be encoded as a list"))
}

let outer_remaining_len = data.len();
Expand All @@ -225,7 +225,7 @@ impl BlobTransaction {
if !inner_header.list {
return Err(RlpError::Custom(
"PooledTransactions inner blob tx must be encoded as a list",
));
))
}

let inner_remaining_len = data.len();
Expand All @@ -239,7 +239,7 @@ impl BlobTransaction {
// the inner header only decodes the transaction and signature, so we check the length here
let inner_consumed = inner_remaining_len - data.len();
if inner_consumed != inner_header.payload_length {
return Err(RlpError::UnexpectedLength);
return Err(RlpError::UnexpectedLength)
}

// All that's left are the blobs, commitments, and proofs
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc/src/eth/api/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ where
Err(EthApiError::InvalidTransaction(RpcInvalidTransactionError::GasTooHigh))
if tx_request_gas_limit.is_some() || tx_request_gas_price.is_some() =>
{
return Err(self.map_out_of_gas_err(block_env_gas_limit, env, &mut db));
return Err(self.map_out_of_gas_err(block_env_gas_limit, env, &mut db))
}
// Propagate other results (successful or other errors).
ethres => ethres?,
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/codecs/derive/src/compact/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) fn generate_flag_struct(
.iter()
.filter_map(|f| {
if let FieldTypes::StructField(f) = f {
return Some(f);
return Some(f)
}
None
})
Expand All @@ -36,7 +36,7 @@ pub(crate) fn generate_flag_struct(
};

if total_bits == 0 {
return placeholder_flag_struct(ident, &flags_ident);
return placeholder_flag_struct(ident, &flags_ident)
}

let (total_bytes, unused_bits) = pad_flag_struct(total_bits, &mut field_flags);
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/codecs/derive/src/compact/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn generate_from_compact(fields: &FieldList, ident: &Ident, is_zstd: bool) -> To
let ident = format_ident!("{name}");
return Some(quote! {
#ident: #ident,
});
})
}
None
});
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/codecs/derive/src/compact/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn should_use_alt_impl(ftype: &String, segment: &syn::PathSegment) -> bool {
]
.contains(&path.ident.to_string().as_str())
{
return true;
return true
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/codecs/derive/src/compact/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'a> StructHandler<'a> {
})
}

return;
return
}

let name = format_ident!("{name}");
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/libmdbx-rs/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl TableObject for ObjectLength {
impl<const LEN: usize> TableObject for [u8; LEN] {
fn decode(data_val: &[u8]) -> Result<Self, Error> {
if data_val.len() != LEN {
return Err(Error::DecodeErrorLenDiff);
return Err(Error::DecodeErrorLenDiff)
}
let mut a = [0; LEN];
a[..].copy_from_slice(data_val);
Expand Down
Loading

0 comments on commit ae9ab69

Please sign in to comment.