Skip to content

Commit

Permalink
feat: debug_bundler_addUserOps
Browse files Browse the repository at this point in the history
  • Loading branch information
Vid201 committed Feb 5, 2024
1 parent b52d56c commit 560b796
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 13 deletions.
19 changes: 17 additions & 2 deletions crates/grpc/src/protos/uopool/uopool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ message GetAllReputationResponse {
}

enum SetReputationResult {
SET_REPUTATION = 0;
NOT_SET_REPUTATION = 1;
SET = 0;
NOT_SET = 1;
}

message SetReputationRequest {
Expand All @@ -70,6 +70,20 @@ message SetReputationResponse {
SetReputationResult res = 1;
}

enum AddMempoolResult {
ADDED_MEMPOOL = 0;
NOT_ADDED_MEMPOOL = 1;
}

message AddMempoolRequest {
repeated types.UserOperation uos = 1;
types.H160 ep = 2;
}

message AddMempoolResponse {
AddMempoolResult res = 1;
}

message GetSortedRequest{
types.H160 ep = 1;
}
Expand Down Expand Up @@ -131,4 +145,5 @@ service UoPool {
rpc Clear(google.protobuf.Empty) returns (google.protobuf.Empty);
rpc GetAllReputation(GetAllReputationRequest) returns (GetAllReputationResponse);
rpc SetReputation(SetReputationRequest) returns (SetReputationResponse);
rpc AddMempool(AddMempoolRequest) returns (AddMempoolResponse);
}
26 changes: 24 additions & 2 deletions crates/grpc/src/uopool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,30 @@ where

let res = Response::new(SetReputationResponse {
res: match uopool.set_reputation(req.rep.iter().map(|re| re.clone().into()).collect()) {
Ok(_) => SetReputationResult::SetReputation as i32,
Err(_) => SetReputationResult::NotSetReputation as i32,
Ok(_) => SetReputationResult::Set as i32,
Err(_) => SetReputationResult::NotSet as i32,
},
});

Ok(res)
}

async fn add_mempool(
&self,
req: Request<AddMempoolRequest>,
) -> Result<Response<AddMempoolResponse>, Status> {
let req = req.into_inner();

let ep = parse_addr(req.ep)?;
let mut uopool = self.get_uopool(&ep)?;

let res = Response::new(AddMempoolResponse {
res: match uopool
.add_user_operations(req.uos.into_iter().map(|uo| uo.into()).collect())
.await
{
Ok(_) => AddMempoolResult::AddedMempool as i32,
Err(_) => AddMempoolResult::NotAddedMempool as i32,
},
});

Expand Down
22 changes: 22 additions & 0 deletions crates/mempool/src/uopool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@ where
self.reputation.clear();
}

/// Adds bulk of [UserOperations](UserOperation) into the mempool.
/// The function first validates the [UserOperations](UserOperation) by calling
/// [UoPool::validate_user_operations](UoPool::validate_user_operations).
///
/// # Arguments
/// `user_operations` - The array of [UserOperations](UserOperation) to add
///
/// # Returns
/// `Result<(), MempoolError>` - Ok if the [UserOperations](UserOperation) are added
/// successfully into the mempool
pub async fn add_user_operations(
&mut self,
user_operations: Vec<UserOperation>,
) -> Result<(), MempoolError> {
for uo in user_operations {
let res = self.validate_user_operation(&uo).await;
self.add_user_operation(uo, res).await?;
}

Ok(())
}

/// Validates a single [UserOperation](UserOperation) and returns the validation outcome by
/// calling [UserOperationValidator::validate_user_operation](UserOperationValidator::validate_user_operation)
///
Expand Down
58 changes: 52 additions & 6 deletions crates/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use jsonrpsee::{
types::{error::INTERNAL_ERROR_CODE, ErrorObjectOwned},
};
use silius_grpc::{
bundler_client::BundlerClient, uo_pool_client::UoPoolClient, GetAllReputationRequest,
GetAllRequest, GetStakeInfoRequest, Mode as GrpcMode, SetModeRequest, SetReputationRequest,
SetReputationResult,
bundler_client::BundlerClient, uo_pool_client::UoPoolClient, AddMempoolRequest,
GetAllReputationRequest, GetAllRequest, GetStakeInfoRequest, Mode as GrpcMode, SetModeRequest,
SetReputationRequest, SetReputationResult,
};
use silius_primitives::{
constants::bundler::BUNDLE_INTERVAL,
reputation::{ReputationEntry, StakeInfoResponse},
BundlerMode, UserOperation, UserOperationRequest,
BundlerMode, UserOperation, UserOperationRequest, UserOperationSigned,
};
use tonic::Request;

Expand Down Expand Up @@ -76,14 +76,60 @@ impl DebugApiServer for DebugApiServerImpl {
Ok(ResponseSuccess::Ok)
}

/// Set the mempool for the given array of [UserOperation](UserOperationRequest)
/// and send it to the UoPool gRPC service through the
/// [AddMempoolRequest](SetReputationRequest).
///
/// # Arguments
/// * `user_operations: Vec<UserOperationRequest>` - The [UserOperation](UserOperationRequest)
/// to be set.
/// * `entry_point: Address` - The address of the entry point.
///
/// # Returns
/// * `RpcResult<ResponseSuccess>` - Ok
async fn add_user_ops(
&self,
user_operations: Vec<UserOperationRequest>,
ep: Address,
) -> RpcResult<ResponseSuccess> {
let mut uopool_grpc_client = self.uopool_grpc_client.clone();

let res = uopool_grpc_client
.get_chain_id(Request::new(()))
.await
.map_err(JsonRpcError::from)?
.into_inner();

uopool_grpc_client
.add_mempool(Request::new(AddMempoolRequest {
uos: user_operations
.iter()
.map(|uo| {
let uo: UserOperationSigned = uo.clone().into();
UserOperation::from_user_operation_signed(
uo.hash(&ep, res.chain_id),
uo.clone(),
)
.into()
})
.collect(),
ep: Some(ep.into()),
}))
.await
.map_err(JsonRpcError::from)?
.into_inner();

Ok(ResponseSuccess::Ok)
}

/// Sending an [GetAllRequest](GetAllRequest) to the UoPool gRPC server
/// to get all of the [UserOperation](UserOperationRequest) in the mempool.
///
/// # Arguments
/// * `entry_point: Address` - The address of the entry point.
///
/// # Returns
/// * `RpcResult<Vec<UserOperation>>` - An array of [UserOperation](UserOperationRequest)
/// * `RpcResult<Vec<UserOperationRequest>>` - An array of [UserOperation](UserOperationRequest)
async fn dump_mempool(&self, ep: Address) -> RpcResult<Vec<UserOperationRequest>> {
let mut uopool_grpc_client = self.uopool_grpc_client.clone();

Expand Down Expand Up @@ -126,7 +172,7 @@ impl DebugApiServer for DebugApiServerImpl {
let res =
uopool_grpc_client.set_reputation(req).await.map_err(JsonRpcError::from)?.into_inner();

if res.res == SetReputationResult::SetReputation as i32 {
if res.res == SetReputationResult::Set as i32 {
return Ok(ResponseSuccess::Ok);
}

Expand Down
22 changes: 19 additions & 3 deletions crates/rpc/src/debug_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,30 @@ pub trait DebugApi {
#[method(name = "clearState")]
async fn clear_state(&self) -> RpcResult<ResponseSuccess>;

/// Get all [UserOperations](UserOperation) of the mempool
/// Set the mempool for the given array of [UserOperation](UserOperationRequest)
///
/// # Arguments
/// * `user_operations: Vec<UserOperationRequest>` - The [UserOperation](UserOperationRequest)
/// to be set.
/// * `entry_point: Address` - The address of the entry point.
///
/// # Returns
/// * `RpcResult<Vec<UserOperation>>` - A vector of [UserOperations](UserOperationRequest)
/// returned
/// * `RpcResult<ResponseSuccess>` - Ok
#[method(name = "addUserOps")]
async fn add_user_ops(
&self,
user_operations: Vec<UserOperationRequest>,
entry_point: Address,
) -> RpcResult<ResponseSuccess>;

/// Get all [UserOperations](UserOperationRequest) of the mempool
///
/// # Arguments
/// * `entry_point: Address` - The address of the entry point.
///
/// # Returns
/// * `RpcResult<Vec<UserOperationRequest>>` - A vector of
/// [UserOperations](UserOperationRequest) returned
#[method(name = "dumpMempool")]
async fn dump_mempool(&self, entry_point: Address) -> RpcResult<Vec<UserOperationRequest>>;

Expand Down

0 comments on commit 560b796

Please sign in to comment.