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: add custom reth node #17

Merged
merged 5 commits into from
May 13, 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
355 changes: 171 additions & 184 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ reth-node-core = { git = "https://github.com/0xEigenLabs/reth", package = "reth-
reth-node-api = { git = "https://github.com/0xEigenLabs/reth", package = "reth-node-api", rev = "8cffebd72" }
reth-db = { git = "https://github.com/0xEigenLabs/reth", package = "reth-db", rev = "8cffebd72" }
reth-blockchain-tree = { git = "https://github.com/0xEigenLabs/reth", package = "reth-blockchain-tree", rev = "8cffebd72" }

reth-beacon-consensus = { git = "https://github.com/0xEigenLabs/reth", rev = "8cffebd72" }
reth-revm = { git = "https://github.com/0xEigenLabs/reth", rev = "8cffebd72" }
reth-interfaces = { git = "https://github.com/0xEigenLabs/reth", rev = "8cffebd72" }
# Database
reth-libmdbx = { git = "https://github.com/0xEigenLabs/reth", package = "reth-libmdbx", rev = "8cffebd72" }

Expand Down
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,26 @@ reth init --datadir /tmp/layer1/chain --chain testdata/layer1/chain.json
RUST_LOG="debug,evm=trace,consensus::auto=trace,consensus::engine=trace,rpc::eth=trace" reth node -d --chain testdata/layer1/chain.json --datadir /tmp/layer1/chain --auto-mine --http --http.port 8545 --http.api debug,eth,net,trace,web3,rpc --port 30303 --authrpc.port 8551
```

* Init the chain and run the RPC-only node
* Init the layer2 chain and rollup service

```
rm -rf /tmp/layer2/chain
reth init --datadir /tmp/layer2/chain --chain testdata/layer2/chain.json
RUST_LOG="debug,evm=trace,consensus::auto=trace,consensus::engine=trace,rpc::eth=trace" reth node -d --chain testdata/layer2/chain.json --datadir /tmp/layer2/chain --auto-mine --http --http.port 8546 --http.api debug,eth,net,trace,web3,rpc --port 30304 --authrpc.port 8552

RUST_LOG="rpc::eth=trace" ZETH_DB_PATH=/tmp/chain PROVER_ADDR=http://localhost:50061 ZETH_L2_ADDR=http://localhost:8546 HOST=0.0.0.0:8182 cargo run -r -- run --database mdbx
cargo run -r -- init --datadir /tmp/layer2/chain --chain testdata/layer2/chain.json
PROVER_ADDR=http://localhost:50061 cargo run -r -- run --database mdbx --log-level debug --chain testdata/layer2/chain.json --http --http.port 8546 --http.api debug,eth,net,trace,web3,rpc --authrpc.port 8552 --port 30304 --datadir /tmp/layer2/chain --auto-mine
```


* Call custom method
```
curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eigenrpc_customMethod","params":[],"id": 10}' 127.0.0.1:8545
curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eigenrpc_customMethod","params":[],"id": 10}' 127.0.0.1:8546

curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eigenrpc_getBlockByNumber","params":[0],"id": 10}' 127.0.0.1:8545
curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eigenrpc_getBlockByNumber","params":[0],"id": 10}' 127.0.0.1:8546
```

You can also use [cast](https://github.com/foundry-rs/foundry/releases).

```
cast rpc eigenrpc_customMethod
cast rpc --rpc-url http://localhost:8546 eigenrpc_customMethod

cast rpc eigenrpc_getBlockByNumber 0
cast rpc --rpc-url http://localhost:8546 eigenrpc_getBlockByNumber 0
```
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::commands::init::InitCmd;
use crate::commands::{chain_info::ChainInfoCmd, config::ConfigCmd, run::RunCmd};
use anyhow::{bail, Result};

Expand All @@ -14,12 +15,14 @@ pub enum SubCommand {
Run(RunCmd),
ChainInfo(ChainInfoCmd),
Config(ConfigCmd),
Init(InitCmd),
}

impl Cli {
pub async fn run(&self) -> Result<()> {
match &self.subcommand {
Some(SubCommand::Run(cmd)) => cmd.run().await,
Some(SubCommand::Init(cmd)) => cmd.run().await,
Some(SubCommand::ChainInfo(cmd)) => cmd.run().await,
Some(SubCommand::Config(cmd)) => cmd.run().await,
None => {
Expand Down
58 changes: 58 additions & 0 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use anyhow::anyhow;
use anyhow::Result;
use jsonrpsee::tracing::info;
use reth_db::init_db;
use reth_db::mdbx::DatabaseArguments;
use reth_node_core::args::utils::{chain_help, genesis_value_parser, SUPPORTED_CHAINS};
use reth_node_core::dirs::{DataDirPath, MaybePlatformPath};
use reth_node_core::init::init_genesis;
use reth_primitives::ChainSpec;
use reth_provider::ProviderFactory;
use std::sync::Arc;

#[derive(Debug, Clone, clap::Args)]
#[command(version, author, about, long_about)]
pub struct InitCmd {
#[arg(long, value_name = "DATA_DIR", verbatim_doc_comment, default_value_t)]
datadir: MaybePlatformPath<DataDirPath>,

/// The chain this node is running.
///
/// Possible values are either a built-in chain or the path to a chain specification file.
#[arg(
long,
value_name = "CHAIN_OR_PATH",
long_help = chain_help(),
default_value = SUPPORTED_CHAINS[0],
value_parser = genesis_value_parser
)]
chain: Arc<ChainSpec>,
}

impl InitCmd {
pub async fn run(&self) -> Result<()> {
info!(target: "zeth::cli", "zeth's layer2 chain init starting");

// add network name to data dir
let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain);
let db_path = data_dir.db_path();
info!(target: "zeth::cli", path = ?db_path, "Opening database");
let db_arguments = DatabaseArguments::default();
let db = Arc::new(
init_db(db_path.clone(), db_arguments)
.map_err(|e| anyhow!(e))?
.with_metrics(),
);
info!(target: "zeth::cli", "Database opened");

let provider_factory =
ProviderFactory::new(db, self.chain.clone(), data_dir.static_files_path())?;
info!(target: "zeth::cli", "Writing genesis block");

let hash = init_genesis(provider_factory)?;

info!(target: "zeth::cli", hash = ?hash, "Genesis block written");

Ok(())
}
}
2 changes: 2 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub(crate) mod chain_info;
pub(crate) mod config;
pub(crate) mod init;
pub(crate) mod reth;
pub(crate) mod run;
34 changes: 34 additions & 0 deletions src/commands/reth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use reth_node_core::args::utils::{chain_help, genesis_value_parser, SUPPORTED_CHAINS};
use reth_node_core::args::{DevArgs, NetworkArgs, RpcServerArgs};
use reth_node_core::dirs::{DataDirPath, MaybePlatformPath};
use reth_primitives::ChainSpec;
use std::sync::Arc;

#[derive(Debug, Clone, clap::Args)]
pub struct RethCmd {
#[arg(long, value_name = "DATA_DIR", verbatim_doc_comment, default_value_t)]
pub datadir: MaybePlatformPath<DataDirPath>,

/// The chain this node is running.
///
/// Possible values are either a built-in chain or the path to a chain specification file.
#[arg(
long,
value_name = "CHAIN_OR_PATH",
long_help = chain_help(),
default_value = SUPPORTED_CHAINS[0],
default_value_if("dev", "true", "dev"),
value_parser = genesis_value_parser,
required = false,
)]
pub chain: Arc<ChainSpec>,

#[command(flatten)]
pub rpc: RpcServerArgs,

#[command(flatten)]
pub network: NetworkArgs,

#[command(flatten)]
pub dev: DevArgs,
}
57 changes: 35 additions & 22 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use tokio::select;
use tokio::signal::unix::{signal, SignalKind};
use tokio::sync::mpsc;

use crate::commands::reth::RethCmd;
use crate::config::env::GLOBAL_ENV;
use crate::custom_reth;
use crate::db::lfs;
Expand All @@ -13,9 +14,11 @@ use crate::settlement::ethereum::EthereumSettlementConfig;
use crate::settlement::NetworkSpec;

/// The `RunCmd` struct is a command that runs the eigen-zeth.
#[derive(clap::Args, Debug, Clone, PartialEq, Eq)]
#[derive(clap::Args, Debug, Clone)]
#[command(version, author, about, long_about)]
pub struct RunCmd {
#[clap(flatten)]
pub reth_cmd: RethCmd,
/// The log level of the node.
#[arg(
long,
Expand Down Expand Up @@ -181,21 +184,12 @@ impl RunCmd {
self.base_params.aggregator_addr
);

// Initialize the operator
// let mut op = operator::Operator::new(
// &GLOBAL_ENV.l2addr,
// &GLOBAL_ENV.prover_addr,
// settlement_spec,
// db_config,
// aggregator_addr,
// )
// .unwrap();

let mut sigterm = signal(SignalKind::terminate()).unwrap();
let mut sigint = signal(SignalKind::interrupt()).unwrap();

// initialize the signal channel
let (stop_tx, stop_rx) = mpsc::channel::<()>(1);
let (reth_stop_tx, reth_stop_rx) = mpsc::channel::<()>(1);

// Handle the SIGTERM and SIGINT signals
tokio::spawn(async move {
Expand All @@ -214,19 +208,38 @@ impl RunCmd {
};
}
stop_tx.send(()).await.unwrap();
reth_stop_tx.send(()).await.unwrap();
});

let (reth_started_signal_tx, reth_started_signal_rx) = mpsc::channel::<()>(1);
let a = aggregator_addr.clone();
tokio::spawn(async move {
// Run the operator
Operator::run(
&GLOBAL_ENV.l2addr,
&GLOBAL_ENV.prover_addr,
settlement_spec.clone(),
db_config.clone(),
a.as_str(),
stop_rx,
reth_started_signal_rx,
)
.await
});

// Launch the custom reth service
custom_reth::launch_custom_node().await?;

// Run the operator
Operator::run(
&GLOBAL_ENV.l2addr,
&GLOBAL_ENV.prover_addr,
settlement_spec,
db_config,
aggregator_addr,
stop_rx,
let chain_spec = self.reth_cmd.chain.clone();
let rpc_args = self.reth_cmd.rpc.clone();
let dev_args = self.reth_cmd.dev;
let data_dir = self.reth_cmd.datadir.clone();

// Launch the custom reth
custom_reth::launch_custom_node(
reth_stop_rx,
reth_started_signal_tx,
chain_spec,
rpc_args,
data_dir,
dev_args,
)
.await
}
Expand Down
4 changes: 0 additions & 4 deletions src/config/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ pub struct GlobalEnv {
pub l2addr: String,
pub prover_addr: String,
pub curve_type: String,
pub host: String,
pub zeth_db_path: String,
pub chain_id: u64,
pub program_name: String,
}
Expand All @@ -20,8 +18,6 @@ pub static GLOBAL_ENV: Lazy<GlobalEnv> = Lazy::new(|| GlobalEnv {
l2addr: std::env::var("ZETH_L2_ADDR").unwrap_or("http://localhost:8546".to_string()),
prover_addr: std::env::var("PROVER_ADDR").unwrap_or("http://127.0.0.1:50061".to_string()),
curve_type: std::env::var("CURVE_TYPE").unwrap_or("BN128".to_string()),
host: std::env::var("HOST").unwrap_or("0.0.0.0:8182".to_string()),
zeth_db_path: std::env::var("ZETH_DB_PATH").unwrap_or("/tmp/layer2/chain".to_string()),
chain_id: std::env::var("CHAIN_ID")
.unwrap_or("12345".to_string())
.parse::<u64>()
Expand Down
1 change: 1 addition & 0 deletions src/custom_reth/eigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ where
/// using the provider.
fn custom_methhod(&self) -> EthResult<Option<Block>> {
let block = self.provider.block_by_number(0)?;
log::info!("custom method called, block: {:?}", block);
// check if its confirmed on L1 and update the block's status
Ok(block)
}
Expand Down
Loading
Loading