Skip to content

Commit

Permalink
Merge pull request #216 from Vid201/chore/http_ws
Browse files Browse the repository at this point in the history
chore: support both http and ws when connecting to eth execution client
  • Loading branch information
Vid201 authored Sep 26, 2023
2 parents b1841aa + caca08c commit 17047e6
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 79 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ build:
cargo build --release

run-silius:
cargo run --release -- bundler --eth-client-address ws://127.0.0.1:8546 --mnemonic-file ${HOME}/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --ws
cargo run --release -- bundler --eth-client-address http://127.0.0.1:8545 --mnemonic-file ${HOME}/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --ws

run-silius-bundling:
cargo run --release -- bundling --eth-client-address ws://127.0.0.1:8546 --mnemonic-file ${HOME}/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789
cargo run --release -- bundling --eth-client-address http://127.0.0.1:8545 --mnemonic-file ${HOME}/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789

run-silius-uopool:
cargo run --release -- uopool --eth-client-address ws://127.0.0.1:8546 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789
cargo run --release -- uopool --eth-client-address http://127.0.0.1:8545 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789

run-silius-rpc:
cargo run --release -- rpc --http --ws
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ cargo run --release -- create-wallet --output-path ${HOME}/.silius --chain-id 5
Run bundler (with user operation pool and JSON-RPC API):

```bash
cargo run --release -- bundler --eth-client-address ws://127.0.0.1:8546 --mnemonic-file ${HOME}/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --ws
cargo run --release -- bundler --eth-client-address http://127.0.0.1:8545 --mnemonic-file ${HOME}/.silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --ws
```

Run only bundling component:
Expand All @@ -69,7 +69,7 @@ cargo run --release -- rpc --http --ws
### Docker

```bash
docker run --net=host -v ./bundler-spec-tests/keys/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266:/data/silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 -v ./db:/data/silius/db ghcr.io/vid201/silius:latest bundler --eth-client-address ws://127.0.0.1:8546 --datadir data/silius --mnemonic-file data/silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789 --http --http.addr 0.0.0.0 --http.port 3000 --http.api eth,debug,web3 --ws --ws.addr 0.0.0.0 --ws.port 3001 --ws.api eth,debug,web3 --eth-client-proxy-address http://127.0.0.1:8545
docker run --net=host -v ./bundler-spec-tests/keys/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266:/data/silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 -v ./db:/data/silius/db ghcr.io/vid201/silius:latest bundler --eth-client-address http://127.0.0.1:8545 --datadir data/silius --mnemonic-file data/silius/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789 --http --http.addr 0.0.0.0 --http.port 3000 --http.api eth,debug,web3 --ws --ws.addr 0.0.0.0 --ws.port 3001 --ws.api eth,debug,web3 --eth-client-proxy-address http://127.0.0.1:8545
```

## Supported networks
Expand Down
40 changes: 23 additions & 17 deletions bin/silius/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use crate::{
cli::args::{BundlerAndUoPoolArgs, BundlerArgs, CreateWalletArgs, RpcArgs, UoPoolArgs},
utils::unwrap_path_or_home,
};
use ethers::{
providers::{Middleware, Provider, Ws},
types::Address,
};
use ethers::{providers::Middleware, types::Address};
use silius_grpc::{
bundler_client::BundlerClient, bundler_service_run, uo_pool_client::UoPoolClient,
uopool_service_run,
Expand All @@ -22,13 +19,16 @@ use silius_rpc::{
use std::{collections::HashSet, future::pending, net::SocketAddr, sync::Arc};
use tracing::info;

pub async fn launch_bundler(
pub async fn launch_bundler<M>(
bundler_args: BundlerArgs,
uopool_args: UoPoolArgs,
common_args: BundlerAndUoPoolArgs,
rpc_args: RpcArgs,
eth_client: Arc<Provider<Ws>>,
) -> eyre::Result<()> {
eth_client: Arc<M>,
) -> eyre::Result<()>
where
M: Middleware + Clone + 'static,
{
launch_uopool(
uopool_args.clone(),
eth_client.clone(),
Expand Down Expand Up @@ -65,13 +65,16 @@ pub async fn launch_bundler(
Ok(())
}

pub async fn launch_bundling(
pub async fn launch_bundling<M>(
args: BundlerArgs,
eth_client: Arc<Provider<Ws>>,
eth_client: Arc<M>,
chain: Option<String>,
entry_points: Vec<Address>,
uopool_grpc_listen_address: String,
) -> eyre::Result<()> {
) -> eyre::Result<()>
where
M: Middleware + Clone + 'static,
{
info!("Starting bundling gRPC service...");

let eth_client_version = check_connected_chain(eth_client.clone(), chain).await?;
Expand Down Expand Up @@ -125,12 +128,15 @@ pub async fn launch_bundling(
Ok(())
}

pub async fn launch_uopool(
pub async fn launch_uopool<M>(
args: UoPoolArgs,
eth_client: Arc<Provider<Ws>>,
eth_client: Arc<M>,
chain: Option<String>,
entry_points: Vec<Address>,
) -> eyre::Result<()> {
) -> eyre::Result<()>
where
M: Middleware + Clone + 'static,
{
info!("Starting uopool gRPC service...");

let eth_client_version = check_connected_chain(eth_client.clone(), chain).await?;
Expand Down Expand Up @@ -288,10 +294,10 @@ pub fn create_wallet(args: CreateWalletArgs) -> eyre::Result<()> {
Ok(())
}

async fn check_connected_chain(
eth_client: Arc<Provider<Ws>>,
chain: Option<String>,
) -> eyre::Result<String> {
async fn check_connected_chain<M>(eth_client: Arc<M>, chain: Option<String>) -> eyre::Result<String>
where
M: Middleware + Clone + 'static,
{
let chain_id = eth_client.get_chainid().await?;
let chain_conn = Chain::from(chain_id);

Expand Down
2 changes: 1 addition & 1 deletion bin/silius/src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub struct UoPoolArgs {
#[derive(Debug, Clone, Parser)]
pub struct BundlerAndUoPoolArgs {
/// Ethereum execution client RPC endpoint.
#[clap(long, default_value = "ws://127.0.0.1:8546")]
#[clap(long, default_value = "http://127.0.0.1:8545")]
pub eth_client_address: String,

/// Chain information.
Expand Down
71 changes: 51 additions & 20 deletions bin/silius/src/cli/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::args::{BundlerAndUoPoolArgs, BundlerArgs, CreateWalletArgs, RpcArgs, UoPoolArgs};
use crate::{
bundler::{create_wallet, launch_bundler, launch_bundling, launch_rpc, launch_uopool},
utils::create_ws_provider,
utils::{create_http_provider, create_ws_provider},
};
use clap::Parser;
use std::{future::pending, sync::Arc};
Expand Down Expand Up @@ -29,8 +29,14 @@ pub struct BundlerCommand {
impl BundlerCommand {
/// Execute the command
pub async fn execute(self) -> eyre::Result<()> {
let eth_client = Arc::new(create_ws_provider(&self.common.eth_client_address).await?);
launch_bundler(self.bundler, self.uopool, self.common, self.rpc, eth_client).await?;
if self.common.eth_client_address.clone().starts_with("http") {
let eth_client = Arc::new(create_http_provider(&self.common.eth_client_address)?);
launch_bundler(self.bundler, self.uopool, self.common, self.rpc, eth_client).await?;
} else {
let eth_client = Arc::new(create_ws_provider(&self.common.eth_client_address).await?);
launch_bundler(self.bundler, self.uopool, self.common, self.rpc, eth_client).await?;
}

pending().await
}
}
Expand All @@ -54,15 +60,28 @@ pub struct BundlingCommand {
impl BundlingCommand {
/// Execute the command
pub async fn execute(self) -> eyre::Result<()> {
let eth_client = Arc::new(create_ws_provider(&self.common.eth_client_address).await?);
launch_bundling(
self.bundler,
eth_client,
self.common.chain,
self.common.entry_points,
self.uopool_grpc_listen_address,
)
.await?;
if self.common.eth_client_address.clone().starts_with("http") {
let eth_client = Arc::new(create_http_provider(&self.common.eth_client_address)?);
launch_bundling(
self.bundler,
eth_client,
self.common.chain,
self.common.entry_points,
self.uopool_grpc_listen_address,
)
.await?;
} else {
let eth_client = Arc::new(create_ws_provider(&self.common.eth_client_address).await?);
launch_bundling(
self.bundler,
eth_client,
self.common.chain,
self.common.entry_points,
self.uopool_grpc_listen_address,
)
.await?;
}

pending().await
}
}
Expand All @@ -82,14 +101,26 @@ pub struct UoPoolCommand {
impl UoPoolCommand {
/// Execute the command
pub async fn execute(self) -> eyre::Result<()> {
let eth_client = Arc::new(create_ws_provider(&self.common.eth_client_address).await?);
launch_uopool(
self.uopool,
eth_client,
self.common.chain,
self.common.entry_points,
)
.await?;
if self.common.eth_client_address.clone().starts_with("http") {
let eth_client = Arc::new(create_http_provider(&self.common.eth_client_address)?);
launch_uopool(
self.uopool,
eth_client,
self.common.chain,
self.common.entry_points,
)
.await?;
} else {
let eth_client = Arc::new(create_ws_provider(&self.common.eth_client_address).await?);
launch_uopool(
self.uopool,
eth_client,
self.common.chain,
self.common.entry_points,
)
.await?;
}

pending().await
}
}
Expand Down
8 changes: 7 additions & 1 deletion bin/silius/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dirs::home_dir;
use ethers::{
providers::{Provider, Ws},
providers::{Http, Provider, Ws},
types::{Address, U256},
};
use expanded_pathbuf::ExpandedPathBuf;
Expand Down Expand Up @@ -68,6 +68,12 @@ where
Ok(())
}

/// Creates ethers provider with HTTP connection
pub fn create_http_provider(addr: &str) -> eyre::Result<Provider<Http>> {
let provider = Provider::<Http>::try_from(addr)?;
Ok(provider)
}

/// Creates ethers provider with WebSockets connection
pub async fn create_ws_provider(addr: &str) -> eyre::Result<Provider<Ws>> {
let provider = Provider::<Ws>::connect_with_reconnects(addr, usize::MAX).await?;
Expand Down
7 changes: 2 additions & 5 deletions crates/bundler/src/bundler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ethers::{
prelude::{LocalWallet, SignerMiddleware},
providers::{Middleware, PubsubClient},
providers::Middleware,
signers::Signer,
types::{
transaction::eip2718::TypedTransaction, Address, Eip1559TransactionRequest, H256, U256, U64,
Expand Down Expand Up @@ -31,7 +31,6 @@ type EthClientType<M> = Arc<SignerMiddleware<Arc<M>, LocalWallet>>;
pub struct Bundler<M>
where
M: Middleware + 'static,
<M as Middleware>::Provider: PubsubClient,
{
/// Wallet instance representing the bundler's wallet.
pub wallet: Wallet,
Expand All @@ -56,7 +55,6 @@ where
impl<M> Bundler<M>
where
M: Middleware + 'static,
<M as Middleware>::Provider: PubsubClient,
{
/// Create a new `Bundler` instance
/// if `send_bundle_mode` is `SendBundleMode::Flashbots` and `relay_endpoints` is `None`, the default Flashbots relay endpoint will be used
Expand Down Expand Up @@ -467,7 +465,7 @@ mod test {
use alloy_sol_types::{sol, SolCall};
use ethers::{
contract::abigen,
providers::{Http, Middleware, Provider, PubsubClient, Ws},
providers::{Http, Middleware, Provider, Ws},
signers::Signer,
types::{
transaction::eip2718::TypedTransaction, Address, Eip1559TransactionRequest,
Expand Down Expand Up @@ -519,7 +517,6 @@ mod test {
struct TestContext<M>
where
M: Middleware + 'static,
<M as Middleware>::Provider: PubsubClient,
{
pub bundler: Bundler<M>,
pub entry_point: Address,
Expand Down
4 changes: 1 addition & 3 deletions crates/grpc/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::uopool::{GAS_INCREASE_PERC, MAX_UOS_PER_UNSTAKED_SENDER};
use ethers::{
providers::{Middleware, PubsubClient},
providers::Middleware,
types::{Address, U256},
};
use silius_contracts::EntryPoint;
Expand All @@ -18,7 +18,6 @@ use std::sync::Arc;
pub struct UoPoolBuilder<M, P, R, E>
where
M: Middleware + Clone + 'static,
<M as Middleware>::Provider: PubsubClient,
P: Mempool<UserOperations = VecUo, CodeHashes = VecCh, Error = E> + Send + Sync,
R: Reputation<ReputationEntries = Vec<ReputationEntry>, Error = E> + Send + Sync,
{
Expand All @@ -38,7 +37,6 @@ where
impl<M, P, R, E> UoPoolBuilder<M, P, R, E>
where
M: Middleware + Clone + 'static,
<M as Middleware>::Provider: PubsubClient,
P: Mempool<UserOperations = VecUo, CodeHashes = VecCh, Error = E> + Send + Sync,
R: Reputation<ReputationEntries = Vec<ReputationEntry>, Error = E> + Send + Sync,
E: Debug + Display,
Expand Down
6 changes: 1 addition & 5 deletions crates/grpc/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::proto::bundler::*;
use crate::proto::uopool::{GetSortedRequest, HandlePastEventRequest};
use crate::uo_pool_client::UoPoolClient;
use async_trait::async_trait;
use ethers::providers::{Middleware, PubsubClient};
use ethers::providers::Middleware;
use ethers::types::{Address, H256, U256};
use parking_lot::Mutex;
use silius_bundler::Bundler;
Expand All @@ -14,7 +14,6 @@ use tracing::{error, info, warn};
pub struct BundlerService<M>
where
M: Middleware + Clone + 'static,
<M as Middleware>::Provider: PubsubClient,
{
pub bundlers: Vec<Bundler<M>>,
pub running: Arc<Mutex<bool>>,
Expand All @@ -29,7 +28,6 @@ fn is_running(running: Arc<Mutex<bool>>) -> bool {
impl<M> BundlerService<M>
where
M: Middleware + Clone + 'static,
<M as Middleware>::Provider: PubsubClient,
{
pub fn new(
bundlers: Vec<Bundler<M>>,
Expand Down Expand Up @@ -163,7 +161,6 @@ where
impl<M> bundler_server::Bundler for BundlerService<M>
where
M: Middleware + Clone + 'static,
<M as Middleware>::Provider: PubsubClient,
{
async fn set_bundler_mode(
&self,
Expand Down Expand Up @@ -217,7 +214,6 @@ pub fn bundler_service_run<M>(
relay_endpoints: Option<Vec<String>>,
) where
M: Middleware + Clone + 'static,
<M as Middleware>::Provider: PubsubClient,
{
let bundlers: Vec<Bundler<M>> = eps
.iter()
Expand Down
Loading

0 comments on commit 17047e6

Please sign in to comment.