Skip to content

Commit

Permalink
feat: add sendBundleNow api
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Mar 29, 2023
1 parent 9af95c1 commit b55bd8b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
39 changes: 32 additions & 7 deletions src/bundler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use ethers::{
prelude::SignerMiddleware,
providers::{Http, Middleware, Provider},
signers::Signer,
types::{transaction::eip2718::TypedTransaction, Address, U256},
types::{transaction::eip2718::TypedTransaction, Address, H256, U256},
};
use parking_lot::Mutex;
use serde::Deserialize;
use tracing::{debug, error};
use tracing::{debug, error, info};

use crate::{
contracts::gen::EntryPointAPI,
Expand Down Expand Up @@ -103,8 +103,10 @@ impl Bundler {
Ok(user_operations)
}

async fn send_next_bundle(&self) -> anyhow::Result<()> {
async fn send_next_bundle(&self) -> anyhow::Result<H256> {
info!("Creating the next bundle");
let bundles = self.create_bundle().await?;
info!("Got {} bundles", bundles.len());
let provider = Provider::<Http>::try_from(self.eth_client_address.clone())?;
let client = Arc::new(SignerMiddleware::new(provider, self.wallet.signer.clone()));
let entry_point = EntryPointAPI::new(self.entry_point, client.clone());
Expand All @@ -131,10 +133,13 @@ impl Bundler {
tx.set_gas_price(max_fee_per_gas);
}
};
let res = client.send_transaction(tx, None).await?.await?;
let tx = client.send_transaction(tx, None).await?;
let tx_hash = tx.tx_hash();
debug!("Send bundles with transaction: {tx:?}");

debug!("Send bundles with ret: {res:?}");
Ok(())
let res = tx.await?;
debug!("Send bundles with receipt: {res:?}");
Ok(tx_hash)
}
}

Expand Down Expand Up @@ -178,9 +183,25 @@ impl BundlerManager {
}
}

pub fn start_server(&self) {}
pub async fn send_bundles_now(&self) -> anyhow::Result<H256> {
info!("Sending bundle now");
let mut tx_hashes: Vec<H256> = vec![];
for bundler in self.bundlers.iter() {
info!("Sending bundle for entry point: {:?}", bundler.entry_point);
let tx_hash = bundler.send_next_bundle().await?;
tx_hashes.push(tx_hash)
}

// FIXME: Because currently the bundler support multiple bundler and
// we don't have a way to know which bundler is the one that is
Ok(tx_hashes
.into_iter()
.next()
.expect("Must have at least one tx hash"))
}

pub fn stop_bundling(&self) {
info!("Stopping auto bundling");
let mut r = self.running.lock();
*r = false;
}
Expand All @@ -192,6 +213,10 @@ impl BundlerManager {
pub fn start_bundling(&self) {
if !self.is_running() {
for bundler in self.bundlers.iter() {
info!(
"Starting auto bundling process for entry point: {:?}",
bundler.entry_point
);
let bundler_own = bundler.clone();
let interval = self.bundle_interval;
let running_lock = self.running.clone();
Expand Down
17 changes: 16 additions & 1 deletion src/bundler/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use std::net::SocketAddr;

use async_trait::async_trait;
use tonic::Response;
use tracing::info;

use crate::uopool::server::{
bundler::{
bundler_server::{Bundler, BundlerServer},
Mode, SetModeRequest, SetModeResponse, SetModeResult,
Mode, SendBundleNowResponse, SetModeRequest, SetModeResponse, SetModeResult,
},
types::{GetChainIdResponse, GetSupportedEntryPointsResponse},
};
Expand Down Expand Up @@ -36,6 +37,7 @@ impl Bundler for BundlerManager {
let req = request.into_inner();
match req.mode() {
Mode::Manual => {
info!("Stopping auto bundling");
self.stop_bundling();
Ok(Response::new(SetModeResponse {
result: SetModeResult::Ok.into(),
Expand All @@ -49,6 +51,19 @@ impl Bundler for BundlerManager {
}
}
}

async fn send_bundle_now(
&self,
_request: tonic::Request<()>,
) -> Result<Response<SendBundleNowResponse>, tonic::Status> {
let res = self
.send_bundles_now()
.await
.map_err(|e| tonic::Status::internal(format!("Send bundle now with error: {e:?}")))?;
Ok(Response::new(SendBundleNowResponse {
result: Some(res.into()),
}))
}
}

pub fn run_server(bundler_manager: BundlerManager, listen_address: SocketAddr) {
Expand Down
5 changes: 5 additions & 0 deletions src/proto/bundler/bundler.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ message SetModeResponse{
SetModeResult result = 1;
}

message SendBundleNowResponse{
types.H256 result = 1;
}


service Bundler {
rpc ChainId(google.protobuf.Empty) returns (types.GetChainIdResponse);
rpc SupportedEntryPoints(google.protobuf.Empty) returns (types.GetSupportedEntryPointsResponse);
// debug
rpc SetBundlerMode(SetModeRequest) returns (SetModeResponse);
rpc SendBundleNow(google.protobuf.Empty) returns (SendBundleNowResponse);
}
18 changes: 17 additions & 1 deletion src/rpc/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
};
use anyhow::format_err;
use async_trait::async_trait;
use ethers::types::Address;
use ethers::types::{Address, H256};
use jsonrpsee::core::RpcResult;
use tracing::{debug, trace};

Expand Down Expand Up @@ -128,4 +128,20 @@ impl DebugApiServer for DebugApiServerImpl {
))),
}
}

async fn send_bundle_now(&self) -> RpcResult<H256> {
let mut bundler_grpc_client = self.bundler_grpc_client.clone();
let request = tonic::Request::new(());
match bundler_grpc_client.send_bundle_now(request).await {
Ok(response) => Ok(response
.into_inner()
.result
.expect("Must return send bundle now tx data")
.into()),
Err(status) => Err(jsonrpsee::core::Error::Custom(format!(
"GRPC error (bundler): {}",
status.message()
))),
}
}
}
5 changes: 4 additions & 1 deletion src/rpc/debug_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
bundler::Mode,
types::{reputation::ReputationEntry, user_operation::UserOperation},
};
use ethers::types::Address;
use ethers::types::{Address, H256};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

#[rpc(server, namespace = "debug_bundler")]
Expand All @@ -25,4 +25,7 @@ pub trait DebugApi {

#[method(name = "setBundlingMode")]
async fn set_bundling_mode(&self, mode: Mode) -> RpcResult<()>;

#[method(name = "sendBundleNow")]
async fn send_bundle_now(&self) -> RpcResult<H256>;
}

0 comments on commit b55bd8b

Please sign in to comment.