From 10a326b767149175b2e94003df682838d75679fc Mon Sep 17 00:00:00 2001 From: WillQ Date: Thu, 28 Dec 2023 22:26:52 +0800 Subject: [PATCH] feat: add poll interval arg --- bin/silius/src/cli/args.rs | 33 ++++++++++++++++++++++++++- bin/silius/src/cli/commands.rs | 15 +++++++++--- bin/silius/src/utils.rs | 7 +++++- crates/primitives/src/provider.rs | 15 +++++------- examples/storage/examples/database.rs | 4 +++- examples/storage/examples/memory.rs | 3 ++- 6 files changed, 61 insertions(+), 16 deletions(-) diff --git a/bin/silius/src/cli/args.rs b/bin/silius/src/cli/args.rs index a50f85d5..192d606b 100644 --- a/bin/silius/src/cli/args.rs +++ b/bin/silius/src/cli/args.rs @@ -1,5 +1,5 @@ use crate::utils::{ - parse_address, parse_enr, parse_send_bundle_mode, parse_u256, parse_uopool_mode, + parse_address, parse_duration, parse_enr, parse_send_bundle_mode, parse_u256, parse_uopool_mode, }; use clap::{Parser, ValueEnum}; use discv5::Enr; @@ -19,6 +19,7 @@ use silius_primitives::{ use std::{ net::{IpAddr, Ipv4Addr}, path::PathBuf, + time::Duration, }; #[derive(ValueEnum, Debug, Clone)] @@ -124,6 +125,10 @@ pub struct BundlerAndUoPoolArgs { /// Entry point addresses. #[clap(long, value_delimiter=',', value_parser=parse_address)] pub entry_points: Vec
, + + /// Poll interval event filters and pending transactions in milliseconds. + #[clap(long, default_value = "500", value_parser= parse_duration)] + pub poll_interval: Duration, } /// RPC CLI args @@ -328,6 +333,32 @@ mod tests { ); } + #[test] + fn bundler_and_uopool_args() { + let args = vec![ + "bundleranduopoolargs", + "--eth-client-address", + "http://127.0.0.1:8545", + "--chain", + "holesky", + "--entry-points", + "0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990", + "--poll-interval", + "5000", + ]; + assert_eq!( + BundlerAndUoPoolArgs { + eth_client_address: String::from("http://127.0.0.1:8545"), + chain: Some(NamedChain::Holesky), + entry_points: vec![ + Address::from_str("0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990").unwrap() + ], + poll_interval: Duration::from_millis(5000), + }, + BundlerAndUoPoolArgs::try_parse_from(args).unwrap() + ); + } + #[test] fn rpc_args_when_http_and_ws_flag() { let args = vec![ diff --git a/bin/silius/src/cli/commands.rs b/bin/silius/src/cli/commands.rs index e0aaa6f2..55a9a590 100644 --- a/bin/silius/src/cli/commands.rs +++ b/bin/silius/src/cli/commands.rs @@ -30,7 +30,10 @@ impl NodeCommand { /// Execute the command pub async fn execute(self) -> eyre::Result<()> { if self.common.eth_client_address.clone().starts_with("http") { - let eth_client = Arc::new(create_http_provider(&self.common.eth_client_address).await?); + let eth_client = Arc::new( + create_http_provider(&self.common.eth_client_address, self.common.poll_interval) + .await?, + ); let block_streams = create_http_block_streams(eth_client.clone(), self.common.entry_points.len()).await; launch_bundler( @@ -81,7 +84,10 @@ impl BundlerCommand { /// Execute the command pub async fn execute(self) -> eyre::Result<()> { if self.common.eth_client_address.clone().starts_with("http") { - let eth_client = Arc::new(create_http_provider(&self.common.eth_client_address).await?); + let eth_client = Arc::new( + create_http_provider(&self.common.eth_client_address, self.common.poll_interval) + .await?, + ); launch_bundling( self.bundler, eth_client, @@ -122,7 +128,10 @@ impl UoPoolCommand { /// Execute the command pub async fn execute(self) -> eyre::Result<()> { if self.common.eth_client_address.clone().starts_with("http") { - let eth_client = Arc::new(create_http_provider(&self.common.eth_client_address).await?); + let eth_client = Arc::new( + create_http_provider(&self.common.eth_client_address, self.common.poll_interval) + .await?, + ); let block_streams = create_http_block_streams(eth_client.clone(), self.common.entry_points.len()).await; launch_uopool( diff --git a/bin/silius/src/utils.rs b/bin/silius/src/utils.rs index 59cac4a4..bc935f70 100644 --- a/bin/silius/src/utils.rs +++ b/bin/silius/src/utils.rs @@ -4,7 +4,7 @@ use ethers::types::{Address, U256}; use expanded_pathbuf::ExpandedPathBuf; use pin_utils::pin_mut; use silius_primitives::{bundler::SendStrategy, UoPoolMode}; -use std::{future::Future, str::FromStr}; +use std::{future::Future, str::FromStr, time::Duration}; use tracing::info; /// Unwrap path or returns home directory @@ -44,6 +44,11 @@ pub fn parse_enr(enr: &str) -> Result { Enr::from_str(enr).map_err(|_| format!("Enr {enr} is not a valid enr.")) } +pub fn parse_duration(duration: &str) -> Result { + let seconds: u64 = duration.parse().map_err(|_| format!("{duration} must be unsigned int"))?; + Ok(Duration::from_millis(seconds)) +} + /// Runs the future to completion or until: /// - `ctrl-c` is received. /// - `SIGTERM` is received (unix only). diff --git a/crates/primitives/src/provider.rs b/crates/primitives/src/provider.rs index 02956088..6fef105b 100644 --- a/crates/primitives/src/provider.rs +++ b/crates/primitives/src/provider.rs @@ -3,7 +3,7 @@ use async_stream::stream; use ethers::{ providers::{Http, Middleware, Provider, Ws}, - types::{Chain, H256}, + types::H256, }; use futures_util::{Stream, StreamExt}; use std::{pin::Pin, sync::Arc, time::Duration}; @@ -11,16 +11,13 @@ use std::{pin::Pin, sync::Arc, time::Duration}; pub type BlockStream = Pin> + Send>>; /// Creates ethers provider with HTTP connection -pub async fn create_http_provider(addr: &str) -> eyre::Result> { +pub async fn create_http_provider( + addr: &str, + poll_interval: Duration, +) -> eyre::Result> { let provider = Provider::::try_from(addr)?; - let chain_id = provider.get_chainid().await?; - - Ok(provider.interval(if chain_id == Chain::Dev.into() { - Duration::from_millis(5u64) - } else { - Duration::from_millis(500u64) - })) + Ok(provider.interval(poll_interval)) } /// Creates ethers provider with WebSockets connection diff --git a/examples/storage/examples/database.rs b/examples/storage/examples/database.rs index a1eddfb6..6f787d79 100644 --- a/examples/storage/examples/database.rs +++ b/examples/storage/examples/database.rs @@ -21,6 +21,7 @@ use std::{ env, str::FromStr, sync::Arc, + time::Duration, }; use tempdir::TempDir; @@ -34,7 +35,8 @@ async fn main() -> eyre::Result<()> { env.create_tables().expect("Create mdbx database tables failed"); println!("Database uopool created!"); - let provider = Arc::new(create_http_provider(provider_url.as_str()).await?); + let provider = + Arc::new(create_http_provider(provider_url.as_str(), Duration::from_secs(1)).await?); let ep = Address::from_str(ADDRESS)?; let chain = Chain::dev(); let entry_point = EntryPoint::new(provider.clone(), ep); diff --git a/examples/storage/examples/memory.rs b/examples/storage/examples/memory.rs index a4dd7845..7b44ed5e 100644 --- a/examples/storage/examples/memory.rs +++ b/examples/storage/examples/memory.rs @@ -26,7 +26,8 @@ use std::{ async fn main() -> eyre::Result<()> { // uopool needs connection to the execution client if let Ok(provider_url) = env::var("PROVIDER_URL") { - let provider = Arc::new(create_http_provider(provider_url.as_str()).await?); + let provider = + Arc::new(create_http_provider(provider_url.as_str(), Duration::from_secs(1)).await?); let ep = Address::from_str(ADDRESS)?; let chain = Chain::dev(); let entry_point = EntryPoint::new(provider.clone(), ep);