Skip to content

Commit

Permalink
feat: add manual mode to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Vid201 committed Oct 19, 2024
1 parent a93bcd7 commit 1fdc576
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 60 deletions.
26 changes: 14 additions & 12 deletions bin/silius/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use silius_mempool::{
};
use silius_metrics::{launch_metrics_exporter, mempool::MetricsHandler};
use silius_primitives::{
bundler::SendStrategy,
bundler::BundleStrategy,
constants::{
entry_point, fastlane_relay_endpoints, flashbots_relay_endpoints,
storage::DATABASE_FOLDER_NAME,
Expand Down Expand Up @@ -123,7 +123,7 @@ where
let wallet: Wallet;

if let Some(mnemonic_file) = args.mnemonic_file {
if args.send_bundle_mode == SendStrategy::Flashbots {
if args.bundle_strategy == BundleStrategy::Flashbots {
wallet = Wallet::from_file(mnemonic_file.into(), chain_id, true)
.map_err(|error| eyre::format_err!("Could not load mnemonic file: {}", error))?;
info!("Wallet Signer {:?}", wallet.signer);
Expand All @@ -134,7 +134,7 @@ where
info!("{:?}", wallet.signer);
}
} else if let Some(private_key) = args.private_key {
if args.send_bundle_mode == SendStrategy::Flashbots {
if args.bundle_strategy == BundleStrategy::Flashbots {
wallet = Wallet::from_private_key(
private_key.as_str(),
chain_id,
Expand Down Expand Up @@ -162,8 +162,10 @@ where
let uopool_grpc_client = UoPoolClient::connect(uopool_grpc_listen_address).await?;
info!("Connected to uopool gRPC service");

match args.send_bundle_mode {
SendStrategy::EthereumClient => {
let bundle_interval = if args.manual_bundle_mode { None } else { Some(args.bundle_interval) };

match args.bundle_strategy {
BundleStrategy::EthereumClient => {
let client = Arc::new(EthereumClient::new(eth_client.clone(), wallet.clone()));
bundler_service_run(
SocketAddr::new(args.bundler_addr, args.bundler_port),
Expand All @@ -172,15 +174,15 @@ where
chain_conn,
args.beneficiary,
args.min_balance,
args.bundle_interval,
bundle_interval,
eth_client,
client,
uopool_grpc_client,
metrics_args.enable_metrics,
args.enable_access_list,
);
}
SendStrategy::Conditional => {
BundleStrategy::Conditional => {
let client = Arc::new(ConditionalClient::new(eth_client.clone(), wallet.clone()));
bundler_service_run(
SocketAddr::new(args.bundler_addr, args.bundler_port),
Expand All @@ -189,15 +191,15 @@ where
chain_conn,
args.beneficiary,
args.min_balance,
args.bundle_interval,
bundle_interval,
eth_client,
client,
uopool_grpc_client,
metrics_args.enable_metrics,
args.enable_access_list,
);
}
SendStrategy::Flashbots => {
BundleStrategy::Flashbots => {
let relay_endpoints: Vec<String> = match chain_conn
.named()
.expect("Flashbots is only supported on Mainnet and Sepolia")
Expand All @@ -223,15 +225,15 @@ where
chain_conn,
args.beneficiary,
args.min_balance,
args.bundle_interval,
bundle_interval,
eth_client,
client,
uopool_grpc_client,
metrics_args.enable_metrics,
args.enable_access_list,
);
}
SendStrategy::Fastlane => {
BundleStrategy::Fastlane => {
let relay_endpoints: Vec<String> =
match chain_conn.named().expect("Fastlane is only supported on Polygon") {
NamedChain::Polygon => {
Expand All @@ -249,7 +251,7 @@ where
chain_conn,
args.beneficiary,
args.min_balance,
args.bundle_interval,
bundle_interval,
eth_client,
client,
uopool_grpc_client,
Expand Down
32 changes: 21 additions & 11 deletions bin/silius/src/cli/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::{
parse_address, parse_duration, parse_enr, parse_label_value, parse_send_bundle_mode,
parse_u256, parse_uopool_mode,
parse_address, parse_bundle_strategy, parse_duration, parse_enr, parse_label_value, parse_u256,
parse_uopool_mode,
};
use alloy_chains::{Chain, NamedChain};
use clap::{ArgGroup, Parser, ValueEnum};
Expand All @@ -13,7 +13,7 @@ use silius_p2p::{
listen_addr::{ListenAddr, ListenAddress},
};
use silius_primitives::{
bundler::SendStrategy,
bundler::BundleStrategy,
chain::ChainSpec,
constants::{
bundler::BUNDLE_INTERVAL,
Expand Down Expand Up @@ -69,17 +69,25 @@ pub struct BundlerArgs {
#[clap(long, default_value = "100000000000000000", value_parser=parse_u256)]
pub min_balance: U256,

/// Whether the bundler should send bundles manually.
///
/// By default, this option is set to false.
/// - To enable: `--manual-bundle-mode`.
/// - To disable: no `--manual-bundle-mode` flag.
#[clap(long)]
pub manual_bundle_mode: bool,

/// The bundle interval in seconds.
///
/// By default the interval time is set to 10
#[clap(long, default_value_t = BUNDLE_INTERVAL)]
pub bundle_interval: u64,

/// Sets the send bundle mode.
/// Sets the bundle strategy.
///
/// By default, this option is set to `ethereum-client`.
#[clap(long, default_value = "ethereum-client", value_parser=parse_send_bundle_mode)]
pub send_bundle_mode: SendStrategy,
#[clap(long, default_value = "ethereum-client", value_parser=parse_bundle_strategy)]
pub bundle_strategy: BundleStrategy,

/// Indicates whether the access list is enabled.
#[clap(long)]
Expand Down Expand Up @@ -381,8 +389,9 @@ mod tests {
beneficiary: Address::from_str("0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990")
.unwrap(),
min_balance: U256::from(100000000000000000_u64),
manual_bundle_mode: false,
bundle_interval: 10,
send_bundle_mode: SendStrategy::EthereumClient,
bundle_strategy: BundleStrategy::EthereumClient,
bundler_addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
bundler_port: 3002,
enable_access_list: false,
Expand Down Expand Up @@ -421,8 +430,9 @@ mod tests {
beneficiary: Address::from_str("0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990")
.unwrap(),
min_balance: U256::from(100000000000000000_u64),
manual_bundle_mode: false,
bundle_interval: 10,
send_bundle_mode: SendStrategy::EthereumClient,
bundle_strategy: BundleStrategy::EthereumClient,
bundler_addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
bundler_port: 3002,
enable_access_list: false,
Expand All @@ -447,8 +457,7 @@ mod tests {
"127.0.0.1",
"--bundler.port",
"3002",
"--bundle-interval",
"10",
"--manual-bundle-mode",
];
assert_eq!(
BundlerArgs {
Expand All @@ -468,8 +477,9 @@ mod tests {
beneficiary: Address::from_str("0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990")
.unwrap(),
min_balance: U256::from(100000000000000000_u64),
manual_bundle_mode: true,
bundle_interval: 10,
send_bundle_mode: SendStrategy::EthereumClient,
bundle_strategy: BundleStrategy::EthereumClient,
bundler_addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
bundler_port: 3002,
enable_access_list: false,
Expand Down
8 changes: 4 additions & 4 deletions 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_metrics::label::LabelValue;
use silius_primitives::{bundler::SendStrategy, UoPoolMode};
use silius_primitives::{bundler::BundleStrategy, UoPoolMode};
use std::{future::Future, str::FromStr, time::Duration};
use tracing::info;

Expand All @@ -30,9 +30,9 @@ pub fn parse_u256(s: &str) -> Result<U256, String> {
U256::from_str_radix(s, 10).map_err(|_| format!("String {s} is not a valid U256"))
}

/// Parses SendBundleMode from string
pub fn parse_send_bundle_mode(s: &str) -> Result<SendStrategy, String> {
SendStrategy::from_str(s).map_err(|_| format!("String {s} is not a valid SendBundleMode"))
/// Parses BundleStrategy from string
pub fn parse_bundle_strategy(s: &str) -> Result<BundleStrategy, String> {
BundleStrategy::from_str(s).map_err(|_| format!("String {s} is not a valid BundleStrategy"))
}

/// Parses UoPoolMode from string
Expand Down
16 changes: 9 additions & 7 deletions crates/grpc/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,21 @@ where
M: Middleware + Clone + 'static,
S: SendBundleOp + Clone + 'static,
{
async fn set_bundler_mode(
async fn set_bundle_mode(
&self,
req: Request<SetModeRequest>,
) -> Result<Response<SetModeResponse>, Status> {
req: Request<SetBundleModeRequest>,
) -> Result<Response<SetBundleModeResponse>, Status> {
let req = req.into_inner();

match req.mode() {
Mode::Manual => {
self.stop_bundling();
Ok(Response::new(SetModeResponse { res: SetModeResult::Ok.into() }))
Ok(Response::new(SetBundleModeResponse { res: SetBundleModeResult::Ok.into() }))
}
Mode::Auto => {
let int = req.interval;
self.start_bundling(int);
Ok(Response::new(SetModeResponse { res: SetModeResult::Ok.into() }))
Ok(Response::new(SetBundleModeResponse { res: SetBundleModeResult::Ok.into() }))
}
}
}
Expand Down Expand Up @@ -218,7 +218,7 @@ pub fn bundler_service_run<M, S>(
chain: Chain,
beneficiary: Address,
min_balance: U256,
bundle_interval: u64,
bundle_interval: Option<u64>,
eth_client: Arc<M>,
client: Arc<S>,
uopool_grpc_client: UoPoolClient<tonic::transport::Channel>,
Expand All @@ -245,7 +245,9 @@ pub fn bundler_service_run<M, S>(
.collect();

let bundler_service = BundlerService::new(bundlers, uopool_grpc_client);
bundler_service.start_bundling(bundle_interval);
if let Some(bundle_interval) = bundle_interval {
bundler_service.start_bundling(bundle_interval);
}

tokio::spawn(async move {
let mut builder = tonic::transport::Server::builder();
Expand Down
12 changes: 6 additions & 6 deletions crates/grpc/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,11 @@ pub mod uopool {
}

pub mod bundler {
use silius_primitives::BundlerMode;
use silius_primitives::BundleMode;

tonic::include_proto!("bundler");

impl From<Mode> for BundlerMode {
impl From<Mode> for BundleMode {
fn from(value: Mode) -> Self {
match value {
Mode::Auto => Self::Auto(0),
Expand All @@ -442,11 +442,11 @@ pub mod bundler {
}
}

impl From<BundlerMode> for Mode {
fn from(value: BundlerMode) -> Self {
impl From<BundleMode> for Mode {
fn from(value: BundleMode) -> Self {
match value {
BundlerMode::Auto(_) => Self::Auto,
BundlerMode::Manual => Self::Manual,
BundleMode::Auto(_) => Self::Auto,
BundleMode::Manual => Self::Manual,
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/grpc/src/protos/bundler/bundler.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ enum Mode {
MANUAL = 1;
}

message SetModeRequest {
message SetBundleModeRequest {
Mode mode = 1;
uint64 interval = 2;
}

enum SetModeResult{
enum SetBundleModeResult {
OK = 0;
}

message SetModeResponse{
SetModeResult res = 1;
message SetBundleModeResponse {
SetBundleModeResult res = 1;
}

message SendBundleNowResponse{
message SendBundleNowResponse {
types.H256 res = 1;
}

service Bundler {
// debug
rpc SetBundlerMode(SetModeRequest) returns (SetModeResponse);
rpc SetBundleMode(SetBundleModeRequest) returns (SetBundleModeResponse);
rpc SendBundleNow(google.protobuf.Empty) returns (SendBundleNowResponse);
}
6 changes: 3 additions & 3 deletions crates/primitives/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use serde::Deserialize;
use strum_macros::{EnumString, EnumVariantNames};

/// Bundler modes
/// Bundle modes
#[derive(Debug, Deserialize)]
pub enum Mode {
pub enum BundleMode {
/// Sends bundles automatically every x seconds
#[serde(rename = "auto")]
Auto(u64),
Expand All @@ -17,7 +17,7 @@ pub enum Mode {
/// Determines the mode how bundler sends the bundle
#[derive(Clone, Copy, Debug, EnumString, EnumVariantNames, PartialEq, Eq)]
#[strum(serialize_all = "kebab_case")]
pub enum SendStrategy {
pub enum BundleStrategy {
/// Sends the bundle to the Ethereum execution client
EthereumClient,
/// Sends the bundle to the Flashbots relay
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod user_operation;
mod utils;
mod wallet;

pub use bundler::Mode as BundlerMode;
pub use bundler::BundleMode;
pub use mempool::Mode as UoPoolMode;
pub use p2p::{MempoolConfig, VerifiedUserOperation};
pub use user_operation::{
Expand Down
14 changes: 7 additions & 7 deletions crates/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use jsonrpsee::{
};
use silius_grpc::{
bundler_client::BundlerClient, uo_pool_client::UoPoolClient, AddMempoolRequest,
GetAllReputationRequest, GetAllRequest, GetStakeInfoRequest, Mode as GrpcMode, SetModeRequest,
SetReputationRequest, SetReputationResult,
GetAllReputationRequest, GetAllRequest, GetStakeInfoRequest, Mode as GrpcMode,
SetBundleModeRequest, SetReputationRequest, SetReputationResult,
};
use silius_primitives::{
constants::bundler::BUNDLE_INTERVAL,
reputation::{ReputationEntry, StakeInfoResponse},
BundlerMode, UserOperation, UserOperationRequest, UserOperationSigned,
BundleMode, UserOperation, UserOperationRequest, UserOperationSigned,
};
use tonic::Request;

Expand Down Expand Up @@ -208,19 +208,19 @@ impl DebugApiServer for DebugApiServerImpl {
/// Set the bundling mode.
///
/// # Arguments
/// * `mode: BundlerMode` - The [BundlerMode](BundlerMode) to be set.
/// * `mode: BundleMode` - The [BundleMode](BundleMode) to be set.
///
/// # Returns
/// * `RpcResult<ResponseSuccess>` - Ok
async fn set_bundling_mode(&self, mode: BundlerMode) -> RpcResult<ResponseSuccess> {
async fn set_bundling_mode(&self, mode: BundleMode) -> RpcResult<ResponseSuccess> {
let mut bundler_grpc_client = self.bundler_grpc_client.clone();

let req = Request::new(SetModeRequest {
let req = Request::new(SetBundleModeRequest {
mode: Into::<GrpcMode>::into(mode).into(),
interval: BUNDLE_INTERVAL,
});

match bundler_grpc_client.set_bundler_mode(req).await {
match bundler_grpc_client.set_bundle_mode(req).await {
Ok(_) => Ok(ResponseSuccess::Ok),
Err(s) => Err(JsonRpcError::from(s).into()),
}
Expand Down
Loading

0 comments on commit 1fdc576

Please sign in to comment.