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: Introduce NodeBuilder #5824

Merged
merged 33 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2e892bc
feat: add NodeConfig to build and launch the node
Rjected Dec 1, 2023
1c533c8
add simple test
Rjected Dec 1, 2023
6e176e6
comment test
Rjected Dec 1, 2023
8f7f4d1
wip: copy paste launch method for NodeConfig
Rjected Dec 1, 2023
fd85c8f
add the other methods
Rjected Dec 1, 2023
10ddd2e
make it compile
Rjected Dec 2, 2023
f723a98
add comments on what we need for the handle
Rjected Dec 2, 2023
9a7f7d7
add rpc server handles to node handle
Rjected Dec 2, 2023
7841c27
add node service
Rjected Dec 2, 2023
0d5e754
use stage config for more things
Rjected Dec 2, 2023
781e578
impl Default for NodeConfig
Rjected Dec 2, 2023
bb262c7
add simple test
Rjected Dec 2, 2023
c96b4e8
hmmmm temp / test db as well as real db, is challenging
Rjected Dec 2, 2023
40ff8ec
rename for optimism
Rjected Dec 2, 2023
6b6c1b9
move to 2-step build pattern
Rjected Dec 5, 2023
9ce2695
start integrating with new generic changes
Rjected Dec 13, 2023
3b7f848
add consensus engine rx
Rjected Dec 14, 2023
c735200
fix more stuff
Rjected Dec 15, 2023
ec3d0c2
fix node config structure and finish launch method
Rjected Dec 18, 2023
363ecb2
make node config work
Rjected Dec 18, 2023
7b413a9
rename to NodeBuilder
Rjected Dec 18, 2023
f3f9f51
move file to node_builder
Rjected Dec 18, 2023
354e86a
add with_instance_number
Rjected Dec 18, 2023
17e975a
integrate PrunerBuilder
Rjected Dec 20, 2023
0dac9bd
fix docs
Rjected Dec 20, 2023
89dc77a
fix example
Rjected Dec 21, 2023
e2ca390
add statics for instance num and prometheus recorder
Rjected Dec 21, 2023
461b8a4
set default subscriber and return guard instead of setting global def…
Rjected Dec 21, 2023
cc63623
use try_init instead of always using set_default
Rjected Dec 21, 2023
0779b10
use manual instance nums
Rjected Dec 21, 2023
484b0a4
add run_until_graceful_shutdown method with shutdown hook
Rjected Dec 26, 2023
5ffc12d
fix docs
Rjected Dec 26, 2023
5bca9c8
improve DatabaseBuilder docs
Rjected Dec 26, 2023
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bin/reth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ metrics-util = "0.15.0"
metrics-process = "1.0.9"
reth-metrics.workspace = true
metrics.workspace = true
once_cell.workspace = true

# test vectors generation
proptest.workspace = true
Expand Down
25 changes: 25 additions & 0 deletions bin/reth/src/args/rpc_server_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ pub struct RpcServerArgs {
}

impl RpcServerArgs {
/// Enables the HTTP-RPC server.
pub fn with_http(mut self) -> Self {
self.http = true;
self
}

/// Enables the WS-RPC server.
pub fn with_ws(mut self) -> Self {
self.ws = true;
self
}

/// Change rpc port numbers based on the instance number.
///
/// Warning: if `instance` is zero, this will panic.
pub fn adjust_instance_ports(&mut self, instance: u16) {
debug_assert_ne!(instance, 0, "instance must be non-zero");
// auth port is scaled by a factor of instance * 100
self.auth_port += instance * 100 - 100;
// http port is scaled by a factor of -instance
self.http_port -= instance - 1;
// ws port is scaled by a factor of instance * 2
self.ws_port += instance * 2 - 2;
}

/// Configures and launches _all_ servers.
///
/// Returns the handles for the launched regular RPC server(s) (if any) and the server handle
Expand Down
108 changes: 108 additions & 0 deletions bin/reth/src/cli/db_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//! A real or test database type

use crate::dirs::{ChainPath, DataDirPath, MaybePlatformPath};
use reth_db::{
init_db,
test_utils::{create_test_rw_db, TempDatabase},
DatabaseEnv,
};
use reth_interfaces::db::LogLevel;
use reth_primitives::Chain;
use std::{str::FromStr, sync::Arc};

/// A type that represents either a _real_ (represented by a path), or _test_ database, which will
/// use a [TempDatabase].
#[derive(Debug)]
pub enum DatabaseType {
/// The real database type
Real(MaybePlatformPath<DataDirPath>),
/// The test database type
Test,
}

/// The [Default] implementation for [DatabaseType] uses the _real_ variant, using the default
/// value for the inner [MaybePlatformPath].
impl Default for DatabaseType {
fn default() -> Self {
Self::Real(MaybePlatformPath::<DataDirPath>::default())
}
}

impl DatabaseType {
/// Creates a _test_ database
pub fn test() -> Self {
Self::Test
}
}

/// Type that represents a [DatabaseType] and [LogLevel], used to build a database type
pub struct DatabaseBuilder {
/// The database type
db_type: DatabaseType,
}

impl DatabaseBuilder {
/// Creates the [DatabaseBuilder] with the given [DatabaseType]
pub fn new(db_type: DatabaseType) -> Self {
Self { db_type }
}

/// Initializes and returns the [DatabaseInstance] depending on the current database type. If
/// the [DatabaseType] is test, the [LogLevel] is not used.
///
/// If the [DatabaseType] is test, then the [ChainPath] constructed will be derived from the db
/// path of the [TempDatabase] and the given chain.
pub fn build_db(
self,
log_level: Option<LogLevel>,
chain: Chain,
) -> eyre::Result<DatabaseInstance> {
match self.db_type {
DatabaseType::Test => {
let db = create_test_rw_db();
let db_path_str = db.path().to_str().expect("Path is not valid unicode");
let path = MaybePlatformPath::<DataDirPath>::from_str(db_path_str)
.expect("Path is not valid");
let data_dir = path.unwrap_or_chain_default(chain);

Ok(DatabaseInstance::Test { db, data_dir })
}
DatabaseType::Real(path) => {
let data_dir = path.unwrap_or_chain_default(chain);

tracing::info!(target: "reth::cli", path = ?data_dir, "Opening database");
let db = Arc::new(init_db(data_dir.clone(), log_level)?);
Ok(DatabaseInstance::Real { db, data_dir })
}
}
}
}

/// A constructed database type, with a [ChainPath].
#[derive(Debug, Clone)]
pub enum DatabaseInstance {
/// The test database
Test {
/// The database
db: Arc<TempDatabase<DatabaseEnv>>,
/// The data dir
data_dir: ChainPath<DataDirPath>,
},
/// The real database
Real {
/// The database
db: Arc<DatabaseEnv>,
/// The data dir
data_dir: ChainPath<DataDirPath>,
},
}

impl DatabaseInstance {
/// Returns the data dir for this database instance
pub fn data_dir(&self) -> &ChainPath<DataDirPath> {
match self {
Self::Test { data_dir, .. } => data_dir,
Self::Real { data_dir, .. } => data_dir,
}
}
}
2 changes: 2 additions & 0 deletions bin/reth/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use std::{fmt, fmt::Display, sync::Arc};

pub mod components;
pub mod config;
pub mod db_type;
pub mod ext;
pub mod node_builder;

/// Default [directives](Directive) for [EnvFilter] which disables high-frequency debug logs from
/// `hyper` and `trust-dns`
Expand Down
Loading
Loading