From f4151c0eb6a9d4e8055981208331278480042e11 Mon Sep 17 00:00:00 2001 From: pistomat Date: Fri, 21 Jul 2023 15:50:14 +0200 Subject: [PATCH 1/8] implement --dev testnet flag for node command --- bin/reth/src/args/dev_args.rs | 88 ++++++++++++++++++++++++++ bin/reth/src/args/mod.rs | 4 ++ bin/reth/src/args/network_args.rs | 2 +- bin/reth/src/args/rpc_server_args.rs | 2 +- bin/reth/src/args/utils.rs | 4 +- bin/reth/src/node/mod.rs | 38 +++++++++-- crates/consensus/auto-seal/src/lib.rs | 14 +++- crates/primitives/res/genesis/dev.json | 75 ++++++++++++++++++++++ crates/primitives/src/chain/mod.rs | 7 +- crates/primitives/src/chain/spec.rs | 37 +++++++++++ crates/primitives/src/constants.rs | 4 ++ crates/primitives/src/lib.rs | 4 +- 12 files changed, 264 insertions(+), 15 deletions(-) create mode 100644 bin/reth/src/args/dev_args.rs create mode 100644 crates/primitives/res/genesis/dev.json diff --git a/bin/reth/src/args/dev_args.rs b/bin/reth/src/args/dev_args.rs new file mode 100644 index 000000000000..118f907fe15b --- /dev/null +++ b/bin/reth/src/args/dev_args.rs @@ -0,0 +1,88 @@ +//! clap [Args](clap::Args) for dev testnet configuration +use std::time::Duration; + +use clap::Args; +use humantime::parse_duration; + +/// Parameters for database configuration +#[derive(Debug, Args, PartialEq, Default, Clone, Copy)] +#[command(next_help_heading = "Dev")] +pub struct DevArgs { + /// Start the node in ephemeral dev mode + /// + /// This will + #[arg(long = "dev", help_heading = "Dev")] + pub dev: bool, + + /// How many block_max_transactions to mine per block. + #[arg( + long = "dev.block_max_transactions", + help_heading = "Dev", + conflicts_with = "block_time" + )] + pub block_max_transactions: Option, + + /// Interval between blocks. + #[arg( + long = "dev.block_time", + help_heading = "Dev", + conflicts_with = "block_max_transactions", + value_parser = parse_duration + )] + pub block_time: Option, +} + +#[cfg(test)] +mod tests { + use super::*; + use clap::Parser; + + /// A helper type to parse Args more easily + #[derive(Parser)] + struct CommandParser { + #[clap(flatten)] + args: T, + } + + #[test] + fn test_parse_dev_args() { + let args = CommandParser::::parse_from(["reth"]).args; + assert_eq!(args, DevArgs { dev: false, block_max_transactions: None, block_time: None }); + + let args = CommandParser::::parse_from(["reth", "--dev"]).args; + assert_eq!(args, DevArgs { dev: true, block_max_transactions: None, block_time: None }); + + let args = CommandParser::::parse_from([ + "reth", + "--dev", + "--dev.block_max_transactions", + "2", + ]) + .args; + assert_eq!(args, DevArgs { dev: true, block_max_transactions: Some(2), block_time: None }); + + let args = + CommandParser::::parse_from(["reth", "--dev", "--dev.block_time", "1s"]).args; + assert_eq!( + args, + DevArgs { + dev: true, + block_max_transactions: None, + block_time: Some(std::time::Duration::from_secs(1).into()) + } + ); + } + + #[test] + fn test_parse_dev_args_conflicts() { + let args = CommandParser::::try_parse_from([ + "reth", + "--dev", + "--dev.block_max_transactions", + "2", + "--dev.block_time", + "1s", + ]); + assert!(args.is_err()); + } +} diff --git a/bin/reth/src/args/mod.rs b/bin/reth/src/args/mod.rs index 05d691e8a371..dd4dd83d0b69 100644 --- a/bin/reth/src/args/mod.rs +++ b/bin/reth/src/args/mod.rs @@ -35,4 +35,8 @@ pub use gas_price_oracle_args::GasPriceOracleArgs; mod txpool_args; pub use txpool_args::TxPoolArgs; +/// DevArgs for configuring the dev testnet +mod dev_args; +pub use dev_args::DevArgs; + pub mod utils; diff --git a/bin/reth/src/args/network_args.rs b/bin/reth/src/args/network_args.rs index 8027c3022ce0..c945fdaf32f6 100644 --- a/bin/reth/src/args/network_args.rs +++ b/bin/reth/src/args/network_args.rs @@ -110,7 +110,7 @@ impl NetworkArgs { #[derive(Debug, Args)] pub struct DiscoveryArgs { /// Disable the discovery service. - #[arg(short, long)] + #[arg(short, long, default_value_if("dev", "true", "true"))] pub disable_discovery: bool, /// Disable the DNS discovery. diff --git a/bin/reth/src/args/rpc_server_args.rs b/bin/reth/src/args/rpc_server_args.rs index a38cd6994450..baf3f94f9f65 100644 --- a/bin/reth/src/args/rpc_server_args.rs +++ b/bin/reth/src/args/rpc_server_args.rs @@ -56,7 +56,7 @@ pub(crate) const RPC_DEFAULT_MAX_TRACING_REQUESTS: u32 = 25; #[command(next_help_heading = "RPC")] pub struct RpcServerArgs { /// Enable the HTTP-RPC server - #[arg(long)] + #[arg(long, default_value_if("dev", "true", "true"))] pub http: bool, /// Http server address to listen on diff --git a/bin/reth/src/args/utils.rs b/bin/reth/src/args/utils.rs index 4a11339450ed..f93e98b7b000 100644 --- a/bin/reth/src/args/utils.rs +++ b/bin/reth/src/args/utils.rs @@ -1,7 +1,7 @@ //! Clap parser utilities use reth_primitives::{ - fs, AllGenesisFormats, BlockHashOrNumber, ChainSpec, GOERLI, MAINNET, SEPOLIA, + fs, AllGenesisFormats, BlockHashOrNumber, ChainSpec, GOERLI, MAINNET, SEPOLIA, DEV, }; use reth_revm::primitives::B256 as H256; use std::{ @@ -25,6 +25,7 @@ pub fn chain_spec_value_parser(s: &str) -> eyre::Result, eyre::Er "mainnet" => MAINNET.clone(), "goerli" => GOERLI.clone(), "sepolia" => SEPOLIA.clone(), + "dev" => DEV.clone(), _ => { let raw = fs::read_to_string(PathBuf::from(shellexpand::full(s)?.into_owned()))?; serde_json::from_str(&raw)? @@ -39,6 +40,7 @@ pub fn genesis_value_parser(s: &str) -> eyre::Result, eyre::Error "mainnet" => MAINNET.clone(), "goerli" => GOERLI.clone(), "sepolia" => SEPOLIA.clone(), + "dev" => DEV.clone(), _ => { let raw = fs::read_to_string(PathBuf::from(shellexpand::full(s)?.into_owned()))?; let genesis: AllGenesisFormats = serde_json::from_str(&raw)?; diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index 1c5cd3a7fce5..1291aa772a25 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -2,7 +2,7 @@ //! //! Starts the client use crate::{ - args::{get_secret_key, DebugArgs, NetworkArgs, RpcServerArgs, TxPoolArgs}, + args::{get_secret_key, DebugArgs, NetworkArgs, RpcServerArgs, TxPoolArgs, DevArgs}, dirs::DataDirPath, init::init_genesis, prometheus_exporter, @@ -111,12 +111,15 @@ pub struct Command { /// - mainnet /// - goerli /// - sepolia + /// - dev #[arg( long, value_name = "CHAIN_OR_PATH", verbatim_doc_comment, default_value = "mainnet", - value_parser = genesis_value_parser + default_value_if("dev", "true", "dev"), + value_parser = genesis_value_parser, + required = false, )] chain: Arc, @@ -144,9 +147,8 @@ pub struct Command { #[clap(flatten)] db: DatabaseArgs, - /// Automatically mine blocks for new transactions - #[arg(long)] - auto_mine: bool, + #[clap(flatten)] + dev: DevArgs, } impl Command { @@ -180,7 +182,7 @@ impl Command { info!(target: "reth::cli", "{}", DisplayHardforks::from(self.chain.hardforks().clone())); - let consensus: Arc = if self.auto_mine { + let consensus: Arc = if self.dev.dev { debug!(target: "reth::cli", "Using auto seal"); Arc::new(AutoSealConsensus::new(Arc::clone(&self.chain))) } else { @@ -303,13 +305,18 @@ impl Command { }; // Configure the pipeline - let (mut pipeline, client) = if self.auto_mine { + let (mut pipeline, client) = if self.dev.dev { + info!(target: "reth::cli", "Starting Reth in dev mode"); + info!(target: "reth::cli", "Dev args: {:?} chain args: {:?}", self.dev, self.chain); + let (_, client, mut task) = AutoSealBuilder::new( Arc::clone(&self.chain), blockchain_db.clone(), transaction_pool.clone(), consensus_engine_tx.clone(), canon_state_notification_sender, + self.dev.block_max_transactions, + self.dev.block_time, ) .build(); @@ -790,6 +797,8 @@ async fn run_network_until_shutdown( #[cfg(test)] mod tests { + use reth_primitives::DEV; + use super::*; use std::{net::IpAddr, path::Path}; @@ -861,4 +870,19 @@ mod tests { let db_path = data_dir.db_path(); assert_eq!(db_path, Path::new("my/custom/path/db")); } + + #[test] + fn parse_dev() { + let cmd = Command::parse_from(["reth", "--dev"]); + let chain = DEV.clone(); + assert_eq!(cmd.chain.chain, chain.chain); + assert_eq!(cmd.chain.genesis_hash, chain.genesis_hash); + assert_eq!(cmd.chain.paris_block_and_final_difficulty, chain.paris_block_and_final_difficulty); + assert_eq!(cmd.chain.hardforks, chain.hardforks); + + assert!(cmd.rpc.http); + assert!(cmd.network.discovery.disable_discovery); + + assert!(cmd.dev.dev); + } } diff --git a/crates/consensus/auto-seal/src/lib.rs b/crates/consensus/auto-seal/src/lib.rs index c9f41a010c6b..00dfdbf83f54 100644 --- a/crates/consensus/auto-seal/src/lib.rs +++ b/crates/consensus/auto-seal/src/lib.rs @@ -37,7 +37,7 @@ use reth_transaction_pool::TransactionPool; use std::{ collections::HashMap, sync::Arc, - time::{SystemTime, UNIX_EPOCH}, + time::{SystemTime, UNIX_EPOCH, Duration}, }; use tokio::sync::{mpsc::UnboundedSender, RwLock, RwLockReadGuard, RwLockWriteGuard}; use tracing::{trace, warn}; @@ -115,13 +115,23 @@ where pool: Pool, to_engine: UnboundedSender, canon_state_notification: CanonStateNotificationSender, + block_max_transactions: Option, + block_time: Option, ) -> Self { let latest_header = client .latest_header() .ok() .flatten() .unwrap_or_else(|| chain_spec.sealed_genesis_header()); - let mode = MiningMode::interval(std::time::Duration::from_secs(1)); + + let mode = if let Some(interval) = block_time { + MiningMode::interval(interval) + } else if let Some(max_transactions) = block_max_transactions { + MiningMode::instant(max_transactions, pool.pending_transactions_listener()) + } else { + warn!("No mining mode specified, defaulting to ReadyTransaction"); + MiningMode::instant(1, pool.pending_transactions_listener()) + }; Self { storage: Storage::new(latest_header), diff --git a/crates/primitives/res/genesis/dev.json b/crates/primitives/res/genesis/dev.json new file mode 100644 index 000000000000..46018b128fb4 --- /dev/null +++ b/crates/primitives/res/genesis/dev.json @@ -0,0 +1,75 @@ +{ + "nonce": "0x0", + "timestamp": "0x6490fdd2", + "extraData": "0x", + "gasLimit": "0x1c9c380", + "difficulty": "0x0", + "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "coinbase": "0x0000000000000000000000000000000000000000", + "stateRoot": "0x5eb6e371a698b8d68f665192350ffcecbbbf322916f4b51bd79bb6887da3f494", + "alloc": { + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x70997970C51812dc3A010C7d01b50e0d17dc79C8": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x90F79bf6EB2c4f870365E785982E1f101E93b906": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x976EA74026E726554dB657fA54763abd0C3a0aa9": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x14dC79964da2C08b23698B3D3cc7Ca32193d9955": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0xa0Ee7A142d267C1f36714E4a8F75612F20a79720": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0xBcd4042DE499D14e55001CcbB24a551F3b954096": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x71bE63f3384f5fb98995898A86B02Fb2426c5788": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0xFABB0ac9d68B0B445fB7357272Ff202C5651694a": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x1CBd3b2770909D4e10f157cABC84C7264073C9Ec": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0xdF3e18d64BC6A983f673Ab319CCaE4f1a57C7097": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0xcd3B766CCDd6AE721141F452C550Ca635964ce71": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x2546BcD3c84621e976D8185a91A922aE77ECEc30": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0xbDA5747bFD65F08deb54cb465eB87D40e51B197E": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0xdD2FD4581271e230360230F9337D5c0430Bf44C0": { + "balance": "0xD3C21BCECCEDA1000000" + }, + "0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199": { + "balance": "0xD3C21BCECCEDA1000000" + } + }, + "number": "0x0", + "gasUsed": "0x0", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" +} \ No newline at end of file diff --git a/crates/primitives/src/chain/mod.rs b/crates/primitives/src/chain/mod.rs index 425939ce44b6..a1b04e35dcf4 100644 --- a/crates/primitives/src/chain/mod.rs +++ b/crates/primitives/src/chain/mod.rs @@ -11,7 +11,7 @@ use std::{fmt, str::FromStr}; mod spec; pub use spec::{ AllGenesisFormats, ChainSpec, ChainSpecBuilder, DisplayHardforks, ForkCondition, - ForkTimestamps, GOERLI, MAINNET, SEPOLIA, + ForkTimestamps, GOERLI, MAINNET, SEPOLIA, DEV, }; // The chain info module. @@ -44,6 +44,11 @@ impl Chain { Chain::Named(ethers_core::types::Chain::Sepolia) } + /// Returns the dev chain. + pub const fn dev() -> Self { + Chain::Named(ethers_core::types::Chain::Dev) + } + /// The id of the chain pub fn id(&self) -> u64 { match self { diff --git a/crates/primitives/src/chain/spec.rs b/crates/primitives/src/chain/spec.rs index 0c84dc275399..6cf33f86e0c3 100644 --- a/crates/primitives/src/chain/spec.rs +++ b/crates/primitives/src/chain/spec.rs @@ -130,6 +130,43 @@ pub static SEPOLIA: Lazy> = Lazy::new(|| { .into() }); +/// Dev testnet spec +pub static DEV: Lazy> = Lazy::new(|| { + ChainSpec { + chain: Chain::dev(), + genesis: serde_json::from_str(include_str!("../../res/genesis/dev.json")) + .expect("Can't deserialize Dev testnet genesis json"), + genesis_hash: Some(H256(hex!( + "25a5cc106eea7138acab33231d7160d69cb777ee0c2c553fcddf5138993e6dd9" + ))), + paris_block_and_final_difficulty: Some((0, U256::from(0))), + fork_timestamps: ForkTimestamps::default().shanghai(1677557088), + hardforks: BTreeMap::from([ + (Hardfork::Frontier, ForkCondition::Block(0)), + (Hardfork::Homestead, ForkCondition::Block(0)), + (Hardfork::Dao, ForkCondition::Block(0)), + (Hardfork::Tangerine, ForkCondition::Block(0)), + (Hardfork::SpuriousDragon, ForkCondition::Block(0)), + (Hardfork::Byzantium, ForkCondition::Block(0)), + (Hardfork::Constantinople, ForkCondition::Block(0)), + (Hardfork::Petersburg, ForkCondition::Block(0)), + (Hardfork::Istanbul, ForkCondition::Block(0)), + (Hardfork::MuirGlacier, ForkCondition::Block(0)), + (Hardfork::Berlin, ForkCondition::Block(0)), + (Hardfork::London, ForkCondition::Block(0)), + ( + Hardfork::Paris, + ForkCondition::TTD { + fork_block: Some(0), + total_difficulty: U256::from(0), + }, + ), + (Hardfork::Shanghai, ForkCondition::Timestamp(0)), + ]), + } + .into() +}); + /// An Ethereum chain specification. /// /// A chain specification describes: diff --git a/crates/primitives/src/constants.rs b/crates/primitives/src/constants.rs index 005d69380ab2..6c454ea0ef40 100644 --- a/crates/primitives/src/constants.rs +++ b/crates/primitives/src/constants.rs @@ -77,6 +77,10 @@ pub const GOERLI_GENESIS: H256 = pub const SEPOLIA_GENESIS: H256 = H256(hex!("25a5cc106eea7138acab33231d7160d69cb777ee0c2c553fcddf5138993e6dd9")); +/// Testnet genesis hash. +pub const DEV_GENESIS: H256 = + H256(hex!("25a5cc106eea7138acab33231d7160d69cb777ee0c2c553fcddf5138993e6dd9")); + /// Keccak256 over empty array. pub const KECCAK_EMPTY: H256 = H256(hex!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")); diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 0248f008a24c..f487568d4e49 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -60,11 +60,11 @@ pub use block::{ pub use bloom::Bloom; pub use chain::{ AllGenesisFormats, Chain, ChainInfo, ChainSpec, ChainSpecBuilder, DisplayHardforks, - ForkCondition, ForkTimestamps, GOERLI, MAINNET, SEPOLIA, + ForkCondition, ForkTimestamps, GOERLI, MAINNET, SEPOLIA, DEV, }; pub use compression::*; pub use constants::{ - EMPTY_OMMER_ROOT, GOERLI_GENESIS, KECCAK_EMPTY, MAINNET_GENESIS, SEPOLIA_GENESIS, + EMPTY_OMMER_ROOT, GOERLI_GENESIS, KECCAK_EMPTY, MAINNET_GENESIS, SEPOLIA_GENESIS, DEV_GENESIS, }; pub use forkid::{ForkFilter, ForkHash, ForkId, ForkTransition, ValidationError}; pub use genesis::{Genesis, GenesisAccount}; From 9ed5916486289c5b361cc1adee3c0e9d56683dc1 Mon Sep 17 00:00:00 2001 From: pistomat Date: Fri, 21 Jul 2023 16:17:42 +0200 Subject: [PATCH 2/8] clean and add comments --- bin/reth/src/args/dev_args.rs | 15 +++++++++------ bin/reth/src/node/mod.rs | 3 +-- crates/primitives/src/chain/spec.rs | 8 +++++--- crates/primitives/src/constants.rs | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/bin/reth/src/args/dev_args.rs b/bin/reth/src/args/dev_args.rs index 118f907fe15b..02c05405025e 100644 --- a/bin/reth/src/args/dev_args.rs +++ b/bin/reth/src/args/dev_args.rs @@ -6,18 +6,21 @@ use humantime::parse_duration; /// Parameters for database configuration #[derive(Debug, Args, PartialEq, Default, Clone, Copy)] -#[command(next_help_heading = "Dev")] +#[command(next_help_heading = "Dev testnet")] pub struct DevArgs { - /// Start the node in ephemeral dev mode + /// Start the node in dev mode /// - /// This will - #[arg(long = "dev", help_heading = "Dev")] + /// This mode uses a local proof-of-authority consensus engine with either fixed block times + /// or automatically mined blocks. + /// Disables network discovery and enables local http server. + /// Prefunds 20 accounts derived by mnemonic "test test test test test test test test test test test junk" with 10 000 ETH each. + #[arg(long = "dev", help_heading = "Dev testnet")] pub dev: bool, /// How many block_max_transactions to mine per block. #[arg( long = "dev.block_max_transactions", - help_heading = "Dev", + help_heading = "Dev testnet", conflicts_with = "block_time" )] pub block_max_transactions: Option, @@ -25,7 +28,7 @@ pub struct DevArgs { /// Interval between blocks. #[arg( long = "dev.block_time", - help_heading = "Dev", + help_heading = "Dev testnet", conflicts_with = "block_max_transactions", value_parser = parse_duration )] diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index 1291aa772a25..d2ae8a8491c9 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -307,8 +307,7 @@ impl Command { // Configure the pipeline let (mut pipeline, client) = if self.dev.dev { info!(target: "reth::cli", "Starting Reth in dev mode"); - info!(target: "reth::cli", "Dev args: {:?} chain args: {:?}", self.dev, self.chain); - + let (_, client, mut task) = AutoSealBuilder::new( Arc::clone(&self.chain), blockchain_db.clone(), diff --git a/crates/primitives/src/chain/spec.rs b/crates/primitives/src/chain/spec.rs index 6cf33f86e0c3..2e8575a440ba 100644 --- a/crates/primitives/src/chain/spec.rs +++ b/crates/primitives/src/chain/spec.rs @@ -130,14 +130,16 @@ pub static SEPOLIA: Lazy> = Lazy::new(|| { .into() }); -/// Dev testnet spec +/// Dev testnet specification +/// +/// Includes 20 prefunded accounts with 10_000 ETH each derived from mnemonic "test test test test test test test test test test test junk". pub static DEV: Lazy> = Lazy::new(|| { ChainSpec { chain: Chain::dev(), genesis: serde_json::from_str(include_str!("../../res/genesis/dev.json")) .expect("Can't deserialize Dev testnet genesis json"), genesis_hash: Some(H256(hex!( - "25a5cc106eea7138acab33231d7160d69cb777ee0c2c553fcddf5138993e6dd9" + "2f980576711e3617a5e4d83dd539548ec0f7792007d505a3d2e9674833af2d7c" ))), paris_block_and_final_difficulty: Some((0, U256::from(0))), fork_timestamps: ForkTimestamps::default().shanghai(1677557088), @@ -925,7 +927,7 @@ mod tests { use crate::{ Address, AllGenesisFormats, Chain, ChainSpec, ChainSpecBuilder, DisplayHardforks, ForkCondition, ForkHash, ForkId, Genesis, Hardfork, Head, GOERLI, H256, MAINNET, SEPOLIA, - U256, + U256, DEV, }; use bytes::BytesMut; use ethers_core::types as EtherType; diff --git a/crates/primitives/src/constants.rs b/crates/primitives/src/constants.rs index 6c454ea0ef40..4f4cceaf4a1d 100644 --- a/crates/primitives/src/constants.rs +++ b/crates/primitives/src/constants.rs @@ -79,7 +79,7 @@ pub const SEPOLIA_GENESIS: H256 = /// Testnet genesis hash. pub const DEV_GENESIS: H256 = - H256(hex!("25a5cc106eea7138acab33231d7160d69cb777ee0c2c553fcddf5138993e6dd9")); + H256(hex!("2f980576711e3617a5e4d83dd539548ec0f7792007d505a3d2e9674833af2d7c")); /// Keccak256 over empty array. pub const KECCAK_EMPTY: H256 = From bff39aac163d99392b04650b62d236ffe0e743ee Mon Sep 17 00:00:00 2001 From: pistomat Date: Fri, 21 Jul 2023 16:24:01 +0200 Subject: [PATCH 3/8] fmt --- bin/reth/src/args/dev_args.rs | 5 +++-- bin/reth/src/args/utils.rs | 2 +- bin/reth/src/node/mod.rs | 9 ++++++--- crates/consensus/auto-seal/src/lib.rs | 2 +- crates/primitives/src/chain/mod.rs | 2 +- crates/primitives/src/chain/spec.rs | 14 ++++++-------- crates/primitives/src/lib.rs | 4 ++-- 7 files changed, 20 insertions(+), 18 deletions(-) diff --git a/bin/reth/src/args/dev_args.rs b/bin/reth/src/args/dev_args.rs index 02c05405025e..02a14c935e58 100644 --- a/bin/reth/src/args/dev_args.rs +++ b/bin/reth/src/args/dev_args.rs @@ -10,10 +10,11 @@ use humantime::parse_duration; pub struct DevArgs { /// Start the node in dev mode /// - /// This mode uses a local proof-of-authority consensus engine with either fixed block times + /// This mode uses a local proof-of-authority consensus engine with either fixed block times /// or automatically mined blocks. /// Disables network discovery and enables local http server. - /// Prefunds 20 accounts derived by mnemonic "test test test test test test test test test test test junk" with 10 000 ETH each. + /// Prefunds 20 accounts derived by mnemonic "test test test test test test test test test test + /// test junk" with 10 000 ETH each. #[arg(long = "dev", help_heading = "Dev testnet")] pub dev: bool, diff --git a/bin/reth/src/args/utils.rs b/bin/reth/src/args/utils.rs index f93e98b7b000..c9fe80685d5d 100644 --- a/bin/reth/src/args/utils.rs +++ b/bin/reth/src/args/utils.rs @@ -1,7 +1,7 @@ //! Clap parser utilities use reth_primitives::{ - fs, AllGenesisFormats, BlockHashOrNumber, ChainSpec, GOERLI, MAINNET, SEPOLIA, DEV, + fs, AllGenesisFormats, BlockHashOrNumber, ChainSpec, DEV, GOERLI, MAINNET, SEPOLIA, }; use reth_revm::primitives::B256 as H256; use std::{ diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index d2ae8a8491c9..a16dad165e1e 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -2,7 +2,7 @@ //! //! Starts the client use crate::{ - args::{get_secret_key, DebugArgs, NetworkArgs, RpcServerArgs, TxPoolArgs, DevArgs}, + args::{get_secret_key, DebugArgs, DevArgs, NetworkArgs, RpcServerArgs, TxPoolArgs}, dirs::DataDirPath, init::init_genesis, prometheus_exporter, @@ -307,7 +307,7 @@ impl Command { // Configure the pipeline let (mut pipeline, client) = if self.dev.dev { info!(target: "reth::cli", "Starting Reth in dev mode"); - + let (_, client, mut task) = AutoSealBuilder::new( Arc::clone(&self.chain), blockchain_db.clone(), @@ -876,7 +876,10 @@ mod tests { let chain = DEV.clone(); assert_eq!(cmd.chain.chain, chain.chain); assert_eq!(cmd.chain.genesis_hash, chain.genesis_hash); - assert_eq!(cmd.chain.paris_block_and_final_difficulty, chain.paris_block_and_final_difficulty); + assert_eq!( + cmd.chain.paris_block_and_final_difficulty, + chain.paris_block_and_final_difficulty + ); assert_eq!(cmd.chain.hardforks, chain.hardforks); assert!(cmd.rpc.http); diff --git a/crates/consensus/auto-seal/src/lib.rs b/crates/consensus/auto-seal/src/lib.rs index 00dfdbf83f54..d08957069e34 100644 --- a/crates/consensus/auto-seal/src/lib.rs +++ b/crates/consensus/auto-seal/src/lib.rs @@ -37,7 +37,7 @@ use reth_transaction_pool::TransactionPool; use std::{ collections::HashMap, sync::Arc, - time::{SystemTime, UNIX_EPOCH, Duration}, + time::{Duration, SystemTime, UNIX_EPOCH}, }; use tokio::sync::{mpsc::UnboundedSender, RwLock, RwLockReadGuard, RwLockWriteGuard}; use tracing::{trace, warn}; diff --git a/crates/primitives/src/chain/mod.rs b/crates/primitives/src/chain/mod.rs index a1b04e35dcf4..b46519f32566 100644 --- a/crates/primitives/src/chain/mod.rs +++ b/crates/primitives/src/chain/mod.rs @@ -11,7 +11,7 @@ use std::{fmt, str::FromStr}; mod spec; pub use spec::{ AllGenesisFormats, ChainSpec, ChainSpecBuilder, DisplayHardforks, ForkCondition, - ForkTimestamps, GOERLI, MAINNET, SEPOLIA, DEV, + ForkTimestamps, DEV, GOERLI, MAINNET, SEPOLIA, }; // The chain info module. diff --git a/crates/primitives/src/chain/spec.rs b/crates/primitives/src/chain/spec.rs index 2e8575a440ba..00f3fd7f9477 100644 --- a/crates/primitives/src/chain/spec.rs +++ b/crates/primitives/src/chain/spec.rs @@ -131,8 +131,9 @@ pub static SEPOLIA: Lazy> = Lazy::new(|| { }); /// Dev testnet specification -/// -/// Includes 20 prefunded accounts with 10_000 ETH each derived from mnemonic "test test test test test test test test test test test junk". +/// +/// Includes 20 prefunded accounts with 10_000 ETH each derived from mnemonic "test test test test +/// test test test test test test test junk". pub static DEV: Lazy> = Lazy::new(|| { ChainSpec { chain: Chain::dev(), @@ -158,10 +159,7 @@ pub static DEV: Lazy> = Lazy::new(|| { (Hardfork::London, ForkCondition::Block(0)), ( Hardfork::Paris, - ForkCondition::TTD { - fork_block: Some(0), - total_difficulty: U256::from(0), - }, + ForkCondition::TTD { fork_block: Some(0), total_difficulty: U256::from(0) }, ), (Hardfork::Shanghai, ForkCondition::Timestamp(0)), ]), @@ -926,8 +924,8 @@ where mod tests { use crate::{ Address, AllGenesisFormats, Chain, ChainSpec, ChainSpecBuilder, DisplayHardforks, - ForkCondition, ForkHash, ForkId, Genesis, Hardfork, Head, GOERLI, H256, MAINNET, SEPOLIA, - U256, DEV, + ForkCondition, ForkHash, ForkId, Genesis, Hardfork, Head, DEV, GOERLI, H256, MAINNET, + SEPOLIA, U256, }; use bytes::BytesMut; use ethers_core::types as EtherType; diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index f487568d4e49..fc149160e502 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -60,11 +60,11 @@ pub use block::{ pub use bloom::Bloom; pub use chain::{ AllGenesisFormats, Chain, ChainInfo, ChainSpec, ChainSpecBuilder, DisplayHardforks, - ForkCondition, ForkTimestamps, GOERLI, MAINNET, SEPOLIA, DEV, + ForkCondition, ForkTimestamps, DEV, GOERLI, MAINNET, SEPOLIA, }; pub use compression::*; pub use constants::{ - EMPTY_OMMER_ROOT, GOERLI_GENESIS, KECCAK_EMPTY, MAINNET_GENESIS, SEPOLIA_GENESIS, DEV_GENESIS, + DEV_GENESIS, EMPTY_OMMER_ROOT, GOERLI_GENESIS, KECCAK_EMPTY, MAINNET_GENESIS, SEPOLIA_GENESIS, }; pub use forkid::{ForkFilter, ForkHash, ForkId, ForkTransition, ValidationError}; pub use genesis::{Genesis, GenesisAccount}; From f4076d60584c17cff3b32c2732159e085240e19b Mon Sep 17 00:00:00 2001 From: pistomat Date: Fri, 21 Jul 2023 16:25:22 +0200 Subject: [PATCH 4/8] clippy --- crates/consensus/auto-seal/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/consensus/auto-seal/src/lib.rs b/crates/consensus/auto-seal/src/lib.rs index d08957069e34..eca71d7eee63 100644 --- a/crates/consensus/auto-seal/src/lib.rs +++ b/crates/consensus/auto-seal/src/lib.rs @@ -302,7 +302,7 @@ impl StorageInner { trace!(target: "consensus::auto", transactions=?&block.body, "executing transactions"); let (post_state, gas_used) = - executor.execute_transactions(block, U256::ZERO, Some(senders.clone()))?; + executor.execute_transactions(block, U256::ZERO, Some(senders))?; // apply post block changes let post_state = executor.apply_post_block_changes(block, U256::ZERO, post_state)?; From 0addc3e7b2f8e6850ec8b307451f5e331c92e413 Mon Sep 17 00:00:00 2001 From: pistomat Date: Fri, 21 Jul 2023 16:29:37 +0200 Subject: [PATCH 5/8] fix comment formatting --- bin/reth/src/args/dev_args.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bin/reth/src/args/dev_args.rs b/bin/reth/src/args/dev_args.rs index 02a14c935e58..ff1312c19558 100644 --- a/bin/reth/src/args/dev_args.rs +++ b/bin/reth/src/args/dev_args.rs @@ -1,10 +1,10 @@ -//! clap [Args](clap::Args) for dev testnet configuration +//! clap [Args](clap::Args) for Dev testnet configuration use std::time::Duration; use clap::Args; use humantime::parse_duration; -/// Parameters for database configuration +/// Parameters for Dev testnet configuration #[derive(Debug, Args, PartialEq, Default, Clone, Copy)] #[command(next_help_heading = "Dev testnet")] pub struct DevArgs { @@ -15,10 +15,10 @@ pub struct DevArgs { /// Disables network discovery and enables local http server. /// Prefunds 20 accounts derived by mnemonic "test test test test test test test test test test /// test junk" with 10 000 ETH each. - #[arg(long = "dev", help_heading = "Dev testnet")] + #[arg(long = "dev", help_heading = "Dev testnet", verbatim_doc_comment)] pub dev: bool, - /// How many block_max_transactions to mine per block. + /// How many transactions to mine per block. #[arg( long = "dev.block_max_transactions", help_heading = "Dev testnet", @@ -27,11 +27,15 @@ pub struct DevArgs { pub block_max_transactions: Option, /// Interval between blocks. + /// + /// Parses strings using [humantime::parse_duration] + /// --dev.block_time 12s #[arg( long = "dev.block_time", help_heading = "Dev testnet", conflicts_with = "block_max_transactions", - value_parser = parse_duration + value_parser = parse_duration, + verbatim_doc_comment )] pub block_time: Option, } From 0a86659ff9eb62fc9cf5a96b12420a3998ca57ca Mon Sep 17 00:00:00 2001 From: pistomat Date: Fri, 21 Jul 2023 21:05:22 +0200 Subject: [PATCH 6/8] fmt --- bin/reth/src/args/dev_args.rs | 4 ++-- crates/primitives/src/chain/spec.rs | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/bin/reth/src/args/dev_args.rs b/bin/reth/src/args/dev_args.rs index ff1312c19558..aac9f7eaadeb 100644 --- a/bin/reth/src/args/dev_args.rs +++ b/bin/reth/src/args/dev_args.rs @@ -27,7 +27,7 @@ pub struct DevArgs { pub block_max_transactions: Option, /// Interval between blocks. - /// + /// /// Parses strings using [humantime::parse_duration] /// --dev.block_time 12s #[arg( @@ -76,7 +76,7 @@ mod tests { DevArgs { dev: true, block_max_transactions: None, - block_time: Some(std::time::Duration::from_secs(1).into()) + block_time: Some(std::time::Duration::from_secs(1)) } ); } diff --git a/crates/primitives/src/chain/spec.rs b/crates/primitives/src/chain/spec.rs index 00f3fd7f9477..356c9409b7e0 100644 --- a/crates/primitives/src/chain/spec.rs +++ b/crates/primitives/src/chain/spec.rs @@ -143,7 +143,7 @@ pub static DEV: Lazy> = Lazy::new(|| { "2f980576711e3617a5e4d83dd539548ec0f7792007d505a3d2e9674833af2d7c" ))), paris_block_and_final_difficulty: Some((0, U256::from(0))), - fork_timestamps: ForkTimestamps::default().shanghai(1677557088), + fork_timestamps: ForkTimestamps::default().shanghai(0), hardforks: BTreeMap::from([ (Hardfork::Frontier, ForkCondition::Block(0)), (Hardfork::Homestead, ForkCondition::Block(0)), @@ -1225,6 +1225,17 @@ Post-merge hard forks (timestamp based): ); } + #[test] + fn dev_forkids() { + test_fork_ids( + &DEV, + &[( + Head { number: 0, ..Default::default() }, + ForkId { hash: ForkHash([0x45, 0xb8, 0x36, 0x12]), next: 0 }, + )], + ) + } + /// Checks that time-based forks work /// /// This is based off of the test vectors here: https://github.com/ethereum/go-ethereum/blob/5c8cc10d1e05c23ff1108022f4150749e73c0ca1/core/forkid/forkid_test.go#L155-L188 From 9072ab4f6482d693610bb70ed84b713e8e19f667 Mon Sep 17 00:00:00 2001 From: pistomat Date: Tue, 25 Jul 2023 10:59:42 +0200 Subject: [PATCH 7/8] add an alias and add mining mode as an argument --- bin/reth/src/args/dev_args.rs | 5 ++++- bin/reth/src/node/mod.rs | 14 +++++++++++--- crates/consensus/auto-seal/src/lib.rs | 12 +----------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/bin/reth/src/args/dev_args.rs b/bin/reth/src/args/dev_args.rs index aac9f7eaadeb..5cc02522d1fa 100644 --- a/bin/reth/src/args/dev_args.rs +++ b/bin/reth/src/args/dev_args.rs @@ -15,7 +15,7 @@ pub struct DevArgs { /// Disables network discovery and enables local http server. /// Prefunds 20 accounts derived by mnemonic "test test test test test test test test test test /// test junk" with 10 000 ETH each. - #[arg(long = "dev", help_heading = "Dev testnet", verbatim_doc_comment)] + #[arg(long = "dev", alias = "auto-mine", help_heading = "Dev testnet", verbatim_doc_comment)] pub dev: bool, /// How many transactions to mine per block. @@ -60,6 +60,9 @@ mod tests { let args = CommandParser::::parse_from(["reth", "--dev"]).args; assert_eq!(args, DevArgs { dev: true, block_max_transactions: None, block_time: None }); + let args = CommandParser::::parse_from(["reth", "--auto-mine"]).args; + assert_eq!(args, DevArgs { dev: true, block_max_transactions: None, block_time: None }); + let args = CommandParser::::parse_from([ "reth", "--dev", diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index a16dad165e1e..d032f66fc36f 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -14,7 +14,7 @@ use clap::Parser; use eyre::Context; use fdlimit::raise_fd_limit; use futures::{future::Either, pin_mut, stream, stream_select, StreamExt}; -use reth_auto_seal_consensus::{AutoSealBuilder, AutoSealConsensus}; +use reth_auto_seal_consensus::{AutoSealBuilder, AutoSealConsensus, MiningMode}; use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig}; use reth_beacon_consensus::{BeaconConsensus, BeaconConsensusEngine, MIN_BLOCKS_FOR_PIPELINE_RUN}; use reth_blockchain_tree::{ @@ -308,14 +308,22 @@ impl Command { let (mut pipeline, client) = if self.dev.dev { info!(target: "reth::cli", "Starting Reth in dev mode"); + let mining_mode = if let Some(interval) = self.dev.block_time { + MiningMode::interval(interval) + } else if let Some(max_transactions) = self.dev.block_max_transactions { + MiningMode::instant(max_transactions, transaction_pool.pending_transactions_listener()) + } else { + info!(target: "reth::cli", "No mining mode specified, defaulting to ReadyTransaction"); + MiningMode::instant(1, transaction_pool.pending_transactions_listener()) + }; + let (_, client, mut task) = AutoSealBuilder::new( Arc::clone(&self.chain), blockchain_db.clone(), transaction_pool.clone(), consensus_engine_tx.clone(), canon_state_notification_sender, - self.dev.block_max_transactions, - self.dev.block_time, + mining_mode ) .build(); diff --git a/crates/consensus/auto-seal/src/lib.rs b/crates/consensus/auto-seal/src/lib.rs index eca71d7eee63..1f74802804ba 100644 --- a/crates/consensus/auto-seal/src/lib.rs +++ b/crates/consensus/auto-seal/src/lib.rs @@ -115,8 +115,7 @@ where pool: Pool, to_engine: UnboundedSender, canon_state_notification: CanonStateNotificationSender, - block_max_transactions: Option, - block_time: Option, + mode: MiningMode, ) -> Self { let latest_header = client .latest_header() @@ -124,15 +123,6 @@ where .flatten() .unwrap_or_else(|| chain_spec.sealed_genesis_header()); - let mode = if let Some(interval) = block_time { - MiningMode::interval(interval) - } else if let Some(max_transactions) = block_max_transactions { - MiningMode::instant(max_transactions, pool.pending_transactions_listener()) - } else { - warn!("No mining mode specified, defaulting to ReadyTransaction"); - MiningMode::instant(1, pool.pending_transactions_listener()) - }; - Self { storage: Storage::new(latest_header), client, From 570e273eb79df508068d3324b88c7266e8ae2a42 Mon Sep 17 00:00:00 2001 From: pistomat Date: Tue, 25 Jul 2023 11:45:09 +0200 Subject: [PATCH 8/8] fmt and clippy --- bin/reth/src/node/mod.rs | 7 +++++-- crates/consensus/auto-seal/src/lib.rs | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index d032f66fc36f..49a3edeb2230 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -311,7 +311,10 @@ impl Command { let mining_mode = if let Some(interval) = self.dev.block_time { MiningMode::interval(interval) } else if let Some(max_transactions) = self.dev.block_max_transactions { - MiningMode::instant(max_transactions, transaction_pool.pending_transactions_listener()) + MiningMode::instant( + max_transactions, + transaction_pool.pending_transactions_listener(), + ) } else { info!(target: "reth::cli", "No mining mode specified, defaulting to ReadyTransaction"); MiningMode::instant(1, transaction_pool.pending_transactions_listener()) @@ -323,7 +326,7 @@ impl Command { transaction_pool.clone(), consensus_engine_tx.clone(), canon_state_notification_sender, - mining_mode + mining_mode, ) .build(); diff --git a/crates/consensus/auto-seal/src/lib.rs b/crates/consensus/auto-seal/src/lib.rs index 1f74802804ba..be33d6622107 100644 --- a/crates/consensus/auto-seal/src/lib.rs +++ b/crates/consensus/auto-seal/src/lib.rs @@ -37,7 +37,7 @@ use reth_transaction_pool::TransactionPool; use std::{ collections::HashMap, sync::Arc, - time::{Duration, SystemTime, UNIX_EPOCH}, + time::{SystemTime, UNIX_EPOCH}, }; use tokio::sync::{mpsc::UnboundedSender, RwLock, RwLockReadGuard, RwLockWriteGuard}; use tracing::{trace, warn};