From 732bf712aa16197fe1a29a56f14be6cd8c34a330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=8B=E3=82=8A=E3=82=93=E3=81=A8=E3=81=86?= Date: Thu, 29 Jan 2026 11:17:51 +0100 Subject: [PATCH] refactor(reth-bench): dedupe read_input and load_jwt_secret helpers (#21555) --- bin/reth-bench/src/bench/helpers.rs | 27 ++++++++++++++++ .../src/bench/send_invalid_payload/mod.rs | 28 +++------------- bin/reth-bench/src/bench/send_payload.rs | 32 +++---------------- 3 files changed, 35 insertions(+), 52 deletions(-) diff --git a/bin/reth-bench/src/bench/helpers.rs b/bin/reth-bench/src/bench/helpers.rs index cb78d1c4e3..8ea3d11a93 100644 --- a/bin/reth-bench/src/bench/helpers.rs +++ b/bin/reth-bench/src/bench/helpers.rs @@ -1,6 +1,33 @@ //! Common helpers for reth-bench commands. use crate::valid_payload::call_forkchoice_updated; +use eyre::Result; +use std::io::{BufReader, Read}; + +/// Read input from either a file path or stdin. +pub(crate) fn read_input(path: Option<&str>) -> Result { + Ok(match path { + Some(path) => reth_fs_util::read_to_string(path)?, + None => String::from_utf8( + BufReader::new(std::io::stdin()).bytes().collect::, _>>()?, + )?, + }) +} + +/// Load JWT secret from either a file or use the provided string directly. +pub(crate) fn load_jwt_secret(jwt_secret: Option<&str>) -> Result> { + match jwt_secret { + Some(secret) => { + // Try to read as file first + match std::fs::read_to_string(secret) { + Ok(contents) => Ok(Some(contents.trim().to_string())), + // If file read fails, use the string directly + Err(_) => Ok(Some(secret.to_string())), + } + } + None => Ok(None), + } +} /// Parses a gas limit value with optional suffix: K for thousand, M for million, G for billion. /// diff --git a/bin/reth-bench/src/bench/send_invalid_payload/mod.rs b/bin/reth-bench/src/bench/send_invalid_payload/mod.rs index 3fb2d9a71c..0a38c246b2 100644 --- a/bin/reth-bench/src/bench/send_invalid_payload/mod.rs +++ b/bin/reth-bench/src/bench/send_invalid_payload/mod.rs @@ -3,6 +3,7 @@ mod invalidation; use invalidation::InvalidationConfig; +use super::helpers::{load_jwt_secret, read_input}; use alloy_primitives::{Address, B256}; use alloy_provider::network::AnyRpcBlock; use alloy_rpc_types_engine::ExecutionPayload; @@ -10,7 +11,7 @@ use clap::Parser; use eyre::{OptionExt, Result}; use op_alloy_consensus::OpTxEnvelope; use reth_cli_runner::CliContext; -use std::io::{BufReader, Read, Write}; +use std::io::Write; /// Command for generating and sending an invalid `engine_newPayload` request. /// @@ -180,27 +181,6 @@ enum Mode { } impl Command { - /// Read input from either a file or stdin - fn read_input(&self) -> Result { - Ok(match &self.path { - Some(path) => reth_fs_util::read_to_string(path)?, - None => String::from_utf8( - BufReader::new(std::io::stdin()).bytes().collect::, _>>()?, - )?, - }) - } - - /// Load JWT secret from either a file or use the provided string directly - fn load_jwt_secret(&self) -> Result> { - match &self.jwt_secret { - Some(secret) => match std::fs::read_to_string(secret) { - Ok(contents) => Ok(Some(contents.trim().to_string())), - Err(_) => Ok(Some(secret.clone())), - }, - None => Ok(None), - } - } - /// Build `InvalidationConfig` from command flags const fn build_invalidation_config(&self) -> InvalidationConfig { InvalidationConfig { @@ -236,8 +216,8 @@ impl Command { /// Execute the command pub async fn execute(self, _ctx: CliContext) -> Result<()> { - let block_json = self.read_input()?; - let jwt_secret = self.load_jwt_secret()?; + let block_json = read_input(self.path.as_deref())?; + let jwt_secret = load_jwt_secret(self.jwt_secret.as_deref())?; let block = serde_json::from_str::(&block_json)? .into_inner() diff --git a/bin/reth-bench/src/bench/send_payload.rs b/bin/reth-bench/src/bench/send_payload.rs index 4f92db69ac..67279e5419 100644 --- a/bin/reth-bench/src/bench/send_payload.rs +++ b/bin/reth-bench/src/bench/send_payload.rs @@ -1,10 +1,11 @@ +use super::helpers::{load_jwt_secret, read_input}; use alloy_provider::network::AnyRpcBlock; use alloy_rpc_types_engine::ExecutionPayload; use clap::Parser; use eyre::{OptionExt, Result}; use op_alloy_consensus::OpTxEnvelope; use reth_cli_runner::CliContext; -use std::io::{BufReader, Read, Write}; +use std::io::Write; /// Command for generating and sending an `engine_newPayload` request constructed from an RPC /// block. @@ -51,38 +52,13 @@ enum Mode { } impl Command { - /// Read input from either a file or stdin - fn read_input(&self) -> Result { - Ok(match &self.path { - Some(path) => reth_fs_util::read_to_string(path)?, - None => String::from_utf8( - BufReader::new(std::io::stdin()).bytes().collect::, _>>()?, - )?, - }) - } - - /// Load JWT secret from either a file or use the provided string directly - fn load_jwt_secret(&self) -> Result> { - match &self.jwt_secret { - Some(secret) => { - // Try to read as file first - match std::fs::read_to_string(secret) { - Ok(contents) => Ok(Some(contents.trim().to_string())), - // If file read fails, use the string directly - Err(_) => Ok(Some(secret.clone())), - } - } - None => Ok(None), - } - } - /// Execute the generate payload command pub async fn execute(self, _ctx: CliContext) -> Result<()> { // Load block - let block_json = self.read_input()?; + let block_json = read_input(self.path.as_deref())?; // Load JWT secret - let jwt_secret = self.load_jwt_secret()?; + let jwt_secret = load_jwt_secret(self.jwt_secret.as_deref())?; // Parse the block let block = serde_json::from_str::(&block_json)?