Skip to content

Commit

Permalink
feat: add poll interval arg
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Dec 28, 2023
1 parent 102e8d8 commit 10a326b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
33 changes: 32 additions & 1 deletion bin/silius/src/cli/args.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,6 +19,7 @@ use silius_primitives::{
use std::{
net::{IpAddr, Ipv4Addr},
path::PathBuf,
time::Duration,
};

#[derive(ValueEnum, Debug, Clone)]
Expand Down Expand Up @@ -124,6 +125,10 @@ pub struct BundlerAndUoPoolArgs {
/// Entry point addresses.
#[clap(long, value_delimiter=',', value_parser=parse_address)]
pub entry_points: Vec<Address>,

/// 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
Expand Down Expand Up @@ -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![
Expand Down
15 changes: 12 additions & 3 deletions bin/silius/src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 6 additions & 1 deletion bin/silius/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,6 +44,11 @@ pub fn parse_enr(enr: &str) -> Result<Enr, String> {
Enr::from_str(enr).map_err(|_| format!("Enr {enr} is not a valid enr."))
}

pub fn parse_duration(duration: &str) -> Result<Duration, String> {
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).
Expand Down
15 changes: 6 additions & 9 deletions crates/primitives/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
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};

pub type BlockStream = Pin<Box<dyn Stream<Item = eyre::Result<H256>> + Send>>;

/// Creates ethers provider with HTTP connection
pub async fn create_http_provider(addr: &str) -> eyre::Result<Provider<Http>> {
pub async fn create_http_provider(
addr: &str,
poll_interval: Duration,
) -> eyre::Result<Provider<Http>> {
let provider = Provider::<Http>::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
Expand Down
4 changes: 3 additions & 1 deletion examples/storage/examples/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::{
env,
str::FromStr,
sync::Arc,
time::Duration,
};
use tempdir::TempDir;

Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion examples/storage/examples/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 10a326b

Please sign in to comment.