mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat(bin): simple reth node command (#158)
* feat(bin): simple reth node command * use silent flag * small chore
This commit is contained in:
@ -7,13 +7,27 @@ repository = "https://github.com/foundry-rs/reth"
|
||||
readme = "README.md"
|
||||
|
||||
[dependencies]
|
||||
# reth
|
||||
reth-primitives = { path = "../../crates/primitives" }
|
||||
reth-db = {path = "../../crates/db"}
|
||||
reth-stages = {path = "../../crates/stages"}
|
||||
reth-interfaces = {path = "../../crates/interfaces"}
|
||||
reth-transaction-pool = {path = "../../crates/transaction-pool"}
|
||||
reth-consensus = {path = "../../crates/consensus"}
|
||||
reth-rpc = {path = "../../crates/net/rpc"}
|
||||
|
||||
# tracing
|
||||
tracing = "0.1"
|
||||
tracing-futures = "0.2"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
# mics
|
||||
shellexpand = "2.1"
|
||||
|
||||
eyre = "0.6.8"
|
||||
clap = { version = "4.0", features = ["derive"] }
|
||||
thiserror = "1.0"
|
||||
tracing = "0.1"
|
||||
tracing-futures = "0.2"
|
||||
tokio = { version = "1.21", features = ["sync", "macros", "rt-multi-thread"] }
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
reth-primitives = { path = "../../crates/primitives" }
|
||||
walkdir = "2.3"
|
||||
|
||||
@ -1,12 +1,23 @@
|
||||
use clap::{ArgAction, Parser, Subcommand};
|
||||
//! CLI definition and entrypoint to executable
|
||||
|
||||
use crate::test_eth_chain;
|
||||
use clap::{ArgAction, Parser, Subcommand};
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
|
||||
use crate::{
|
||||
node, test_eth_chain,
|
||||
util::reth_tracing::{self, TracingMode},
|
||||
};
|
||||
|
||||
/// main function that parses cli and runs command
|
||||
pub async fn run() -> eyre::Result<()> {
|
||||
let opt = Cli::parse();
|
||||
|
||||
let tracing = if opt.silent { TracingMode::Silent } else { TracingMode::All };
|
||||
|
||||
reth_tracing::build_subscriber(tracing).init();
|
||||
|
||||
match opt.command {
|
||||
Commands::Node(command) => command.execute().await,
|
||||
Commands::TestEthChain(command) => command.execute().await,
|
||||
}
|
||||
}
|
||||
@ -14,6 +25,9 @@ pub async fn run() -> eyre::Result<()> {
|
||||
/// Commands to be executed
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
/// Main node command
|
||||
#[command(name = "node")]
|
||||
Node(node::Command),
|
||||
/// Runs Ethereum blockchain tests
|
||||
#[command(name = "test-chain")]
|
||||
TestEthChain(test_eth_chain::Command),
|
||||
|
||||
@ -4,14 +4,9 @@
|
||||
no_crate_inject,
|
||||
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
|
||||
))]
|
||||
|
||||
//! Rust Ethereum (reth) binary executable.
|
||||
|
||||
/// CLI definition and entrypoint
|
||||
pub mod cli;
|
||||
|
||||
/// Utility functions.
|
||||
pub mod util;
|
||||
|
||||
/// Command for executing Ethereum blockchain tests
|
||||
pub mod node;
|
||||
pub mod test_eth_chain;
|
||||
pub mod util;
|
||||
|
||||
48
bin/reth/src/node/mod.rs
Normal file
48
bin/reth/src/node/mod.rs
Normal file
@ -0,0 +1,48 @@
|
||||
//! Main node command
|
||||
//!
|
||||
//! Starts the client
|
||||
|
||||
use clap::Parser;
|
||||
use std::{path::Path, sync::Arc};
|
||||
use tracing::info;
|
||||
|
||||
/// Execute Ethereum blockchain tests by specifying path to json files
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct Command {
|
||||
/// Path to database folder
|
||||
#[arg(long, default_value = "~/.reth/db")]
|
||||
db: String,
|
||||
}
|
||||
|
||||
impl Command {
|
||||
/// Execute `node` command
|
||||
pub async fn execute(&self) -> eyre::Result<()> {
|
||||
info!("Rust Ethereum client");
|
||||
|
||||
info!("Initialize components:");
|
||||
|
||||
let path = shellexpand::full(&self.db)?.into_owned();
|
||||
let expanded_db_path = Path::new(&path);
|
||||
std::fs::create_dir_all(expanded_db_path)?;
|
||||
let db = Arc::new(reth_db::kv::Env::<reth_db::mdbx::WriteMap>::open(
|
||||
expanded_db_path,
|
||||
reth_db::kv::EnvKind::RW,
|
||||
)?);
|
||||
info!("DB opened");
|
||||
|
||||
// let _p2p = ();
|
||||
// let _consensus = ();
|
||||
// let _rpc = ();
|
||||
|
||||
let mut pipeline = reth_stages::Pipeline::new();
|
||||
|
||||
// define all stages here
|
||||
|
||||
// run pipeline
|
||||
info!("Pipeline started:");
|
||||
pipeline.run(db.clone()).await?;
|
||||
|
||||
info!("Finishing");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,5 @@
|
||||
//! Command for running Ethereum chain tests.
|
||||
|
||||
use crate::util;
|
||||
use clap::Parser;
|
||||
use std::path::PathBuf;
|
||||
@ -21,7 +23,7 @@ impl Command {
|
||||
.path
|
||||
.iter()
|
||||
.map(|item| {
|
||||
util::find_all_json_tests(item).into_iter().map(|file| {
|
||||
util::find_all_files_with_postfix(item, ".json").into_iter().map(|file| {
|
||||
let tfile = file.clone();
|
||||
let join = tokio::spawn(async move { runner::run_test(tfile.as_path()).await });
|
||||
(join, file)
|
||||
|
||||
@ -1,11 +1,52 @@
|
||||
//! Utility functions.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use walkdir::{DirEntry, WalkDir};
|
||||
|
||||
pub(crate) fn find_all_json_tests(path: &Path) -> Vec<PathBuf> {
|
||||
pub(crate) fn find_all_files_with_postfix(path: &Path, postfix: &str) -> Vec<PathBuf> {
|
||||
WalkDir::new(path)
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|e| e.file_name().to_string_lossy().ends_with(".json"))
|
||||
.filter(|e| e.file_name().to_string_lossy().ends_with(postfix))
|
||||
.map(DirEntry::into_path)
|
||||
.collect::<Vec<PathBuf>>()
|
||||
}
|
||||
|
||||
/// Tracing utility
|
||||
pub mod reth_tracing {
|
||||
use tracing::Subscriber;
|
||||
use tracing_subscriber::{prelude::*, EnvFilter};
|
||||
|
||||
/// Tracing modes
|
||||
pub enum TracingMode {
|
||||
/// Enable all info traces.
|
||||
All,
|
||||
/// Disable tracing
|
||||
Silent,
|
||||
}
|
||||
|
||||
impl TracingMode {
|
||||
fn into_env_filter(self) -> EnvFilter {
|
||||
match self {
|
||||
Self::All => EnvFilter::new("reth=info"),
|
||||
Self::Silent => EnvFilter::new(""),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Build subscriber
|
||||
pub fn build_subscriber(mods: TracingMode) -> impl Subscriber {
|
||||
let nocolor = std::env::var("RUST_LOG_STYLE").map(|val| val == "never").unwrap_or(false);
|
||||
|
||||
// Take env over config
|
||||
let filter = if std::env::var(EnvFilter::DEFAULT_ENV).unwrap_or_default().is_empty() {
|
||||
mods.into_env_filter()
|
||||
} else {
|
||||
EnvFilter::from_default_env()
|
||||
};
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(tracing_subscriber::fmt::layer().with_ansi(!nocolor).with_target(false))
|
||||
.with(filter)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user