Skip to content

Commit

Permalink
feat(rpc): add debug_bundler_clearMempool endpoint for spec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-miao committed Jan 25, 2024
1 parent c2c2f98 commit 6e42fdc
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 11 deletions.
12 changes: 12 additions & 0 deletions crates/pool/proto/op_pool/op_pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ service OpPool {
// Clears the bundler mempool and reputation data of paymasters/accounts/factories/aggregators
rpc DebugClearState (DebugClearStateRequest) returns (DebugClearStateResponse);

// Clears the bundler mempool but not reputation data
rpc DebugClearMempool (DebugClearMempoolRequest) returns (DebugClearMempoolResponse);

// Dumps the current UserOperations mempool
rpc DebugDumpMempool (DebugDumpMempoolRequest) returns (DebugDumpMempoolResponse);

Expand Down Expand Up @@ -299,6 +302,15 @@ message DebugClearStateResponse {
}
message DebugClearStateSuccess {}

message DebugClearMempoolRequest {}
message DebugClearMempoolResponse {
oneof result {
DebugClearMempoolSuccess success = 1;
MempoolError failure = 2;
}
}
message DebugClearMempoolSuccess {}

message DebugDumpMempoolRequest {
bytes entry_point = 1;
}
Expand Down
8 changes: 7 additions & 1 deletion crates/pool/src/mempool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,15 @@ pub trait Mempool: Send + Sync + 'static {

/// Debug methods

/// Clears the mempool
/// Clears both the mempool and reputation
fn clear(&self);

// Clears the reputation system
fn clear_reputation(&self);

/// Clears the mempool
fn clear_mempool(&self);

/// Dumps the mempool's reputation tracking
fn dump_reputation(&self) -> Vec<Reputation>;

Expand Down
11 changes: 11 additions & 0 deletions crates/pool/src/mempool/reputation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ pub(crate) trait ReputationManager: Send + Sync + 'static {

/// Get the ops allowed for an unstaked entity
fn get_ops_allowed(&self, address: Address) -> u64;

/// Clear all reputation values
fn clear(&self);
}

#[derive(Debug)]
Expand Down Expand Up @@ -187,6 +190,10 @@ impl ReputationManager for HourlyMovingAverageReputation {
fn get_ops_allowed(&self, address: Address) -> u64 {
self.reputation.read().get_ops_allowed(address)
}

fn clear(&self) {
self.reputation.write().clear()
}
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -335,6 +342,10 @@ impl AddressReputation {
self.counts
.retain(|_, count| count.ops_seen > 0 || count.ops_included > 0);
}

fn clear(&mut self) {
self.counts.clear();
}
}

#[derive(Debug, Default, Clone)]
Expand Down
14 changes: 14 additions & 0 deletions crates/pool/src/mempool/uo_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,15 @@ where
}

fn clear(&self) {
self.clear_mempool();
self.clear_reputation();
}

fn clear_reputation(&self) {
self.reputation.clear()
}

fn clear_mempool(&self) {
self.state.write().pool.clear()
}

Expand Down Expand Up @@ -1333,5 +1342,10 @@ mod tests {
// return ops allowed, as defined by UREP-020
self.same_unstaked_entity_mempool_count + inclusion_based_count
}

fn clear(&self) {
self.counts.write().seen.clear();
self.counts.write().included.clear();
}
}
}
24 changes: 24 additions & 0 deletions crates/pool/src/server/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ impl PoolServer for LocalPoolHandle {
}
}

async fn debug_clear_mempool(&self) -> Result<(), PoolServerError> {
let req = ServerRequestKind::DebugClearMempool;
let resp = self.send(req).await?;
match resp {
ServerResponse::DebugClearMempool => Ok(()),
_ => Err(PoolServerError::UnexpectedResponse),
}
}

async fn debug_dump_mempool(&self, entry_point: Address) -> PoolResult<Vec<PoolOperation>> {
let req = ServerRequestKind::DebugDumpMempool { entry_point };
let resp = self.send(req).await?;
Expand Down Expand Up @@ -369,6 +378,13 @@ where
Ok(())
}

fn debug_clear_mempool(&self) -> PoolResult<()> {
for mempool in self.mempools.values() {
mempool.clear_mempool();
}
Ok(())
}

fn debug_dump_mempool(&self, entry_point: Address) -> PoolResult<Vec<PoolOperation>> {
let mempool = self.get_pool(entry_point)?;
Ok(mempool
Expand Down Expand Up @@ -483,6 +499,12 @@ where
Err(e) => Err(e),
}
},
ServerRequestKind::DebugClearMempool => {
match self.debug_clear_mempool() {
Ok(_) => Ok(ServerResponse::DebugClearMempool),
Err(e) => Err(e),
}
},
ServerRequestKind::DebugDumpMempool { entry_point } => {
match self.debug_dump_mempool(entry_point) {
Ok(ops) => Ok(ServerResponse::DebugDumpMempool { ops }),
Expand Down Expand Up @@ -571,6 +593,7 @@ enum ServerRequestKind {
entity_updates: Vec<EntityUpdate>,
},
DebugClearState,
DebugClearMempool,
DebugDumpMempool {
entry_point: Address,
},
Expand Down Expand Up @@ -609,6 +632,7 @@ enum ServerResponse {
RemoveOps,
UpdateEntities,
DebugClearState,
DebugClearMempool,
DebugDumpMempool {
ops: Vec<PoolOperation>,
},
Expand Down
3 changes: 3 additions & 0 deletions crates/pool/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ pub trait PoolServer: Send + Sync + 'static {
/// Clear the pool state, used for debug methods
async fn debug_clear_state(&self) -> PoolResult<()>;

/// Clear the mempool state, used for debug methods
async fn debug_clear_mempool(&self) -> PoolResult<()>;

/// Dump all operations in the pool, used for debug methods
async fn debug_dump_mempool(&self, entry_point: Address) -> PoolResult<Vec<PoolOperation>>;

Expand Down
33 changes: 26 additions & 7 deletions crates/pool/src/server/remote/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ use tonic_health::{
};

use super::protos::{
self, add_op_response, debug_clear_state_response, debug_dump_mempool_response,
debug_dump_reputation_response, debug_set_reputation_response, get_op_by_hash_response,
get_ops_response, get_reputation_status_response, get_stake_status_response,
op_pool_client::OpPoolClient, remove_ops_response, update_entities_response, AddOpRequest,
DebugClearStateRequest, DebugDumpMempoolRequest, DebugDumpReputationRequest,
DebugSetReputationRequest, GetOpsRequest, GetReputationStatusRequest, GetStakeStatusRequest,
RemoveOpsRequest, SubscribeNewHeadsRequest, SubscribeNewHeadsResponse, UpdateEntitiesRequest,
self, add_op_response, debug_clear_mempool_response, debug_clear_state_response,
debug_dump_mempool_response, debug_dump_reputation_response, debug_set_reputation_response,
get_op_by_hash_response, get_ops_response, get_reputation_status_response,
get_stake_status_response, op_pool_client::OpPoolClient, remove_ops_response,
update_entities_response, AddOpRequest, DebugClearMempoolRequest, DebugClearStateRequest,
DebugDumpMempoolRequest, DebugDumpReputationRequest, DebugSetReputationRequest, GetOpsRequest,
GetReputationStatusRequest, GetStakeStatusRequest, RemoveOpsRequest, SubscribeNewHeadsRequest,
SubscribeNewHeadsResponse, UpdateEntitiesRequest,
};
use crate::{
mempool::{PoolOperation, Reputation, StakeStatus},
Expand Down Expand Up @@ -282,6 +283,24 @@ impl PoolServer for RemotePoolClient {
}
}

async fn debug_clear_mempool(&self) -> PoolResult<()> {
let res = self
.op_pool_client
.clone()
.debug_clear_mempool(DebugClearMempoolRequest {})
.await?
.into_inner()
.result;

match res {
Some(debug_clear_mempool_response::Result::Success(_)) => Ok(()),
Some(debug_clear_mempool_response::Result::Failure(f)) => Err(f.try_into()?),
None => Err(PoolServerError::Other(anyhow::anyhow!(
"should have received result from op pool"
)))?,
}
}

async fn debug_dump_mempool(&self, entry_point: Address) -> PoolResult<Vec<PoolOperation>> {
let res = self
.op_pool_client
Expand Down
26 changes: 23 additions & 3 deletions crates/pool/src/server/remote/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ use tokio_util::sync::CancellationToken;
use tonic::{transport::Server, Request, Response, Result, Status};

use super::protos::{
add_op_response, debug_clear_state_response, debug_dump_mempool_response,
debug_dump_reputation_response, debug_set_reputation_response, get_op_by_hash_response,
get_ops_response, get_reputation_status_response, get_stake_status_response,
add_op_response, debug_clear_mempool_response, debug_clear_state_response,
debug_dump_mempool_response, debug_dump_reputation_response, debug_set_reputation_response,
get_op_by_hash_response, get_ops_response, get_reputation_status_response,
get_stake_status_response,
op_pool_server::{OpPool, OpPoolServer},
remove_ops_response, update_entities_response, AddOpRequest, AddOpResponse, AddOpSuccess,
DebugClearMempoolRequest, DebugClearMempoolResponse, DebugClearMempoolSuccess,
DebugClearStateRequest, DebugClearStateResponse, DebugClearStateSuccess,
DebugDumpMempoolRequest, DebugDumpMempoolResponse, DebugDumpMempoolSuccess,
DebugDumpReputationRequest, DebugDumpReputationResponse, DebugDumpReputationSuccess,
Expand Down Expand Up @@ -285,6 +287,24 @@ impl OpPool for OpPoolImpl {
Ok(Response::new(resp))
}

async fn debug_clear_mempool(
&self,
_request: Request<DebugClearMempoolRequest>,
) -> Result<Response<DebugClearMempoolResponse>> {
let resp = match self.local_pool.debug_clear_mempool().await {
Ok(_) => DebugClearMempoolResponse {
result: Some(debug_clear_mempool_response::Result::Success(
DebugClearMempoolSuccess {},
)),
},
Err(error) => DebugClearMempoolResponse {
result: Some(debug_clear_mempool_response::Result::Failure(error.into())),
},
};

Ok(Response::new(resp))
}

async fn debug_dump_mempool(
&self,
request: Request<DebugDumpMempoolRequest>,
Expand Down
14 changes: 14 additions & 0 deletions crates/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub trait DebugApi {
#[method(name = "bundler_clearState")]
async fn bundler_clear_state(&self) -> RpcResult<String>;

/// Clears the state of the mempool without affect reputations.
#[method(name = "bundler_clearMempool")]
async fn bundler_clear_mempool(&self) -> RpcResult<String>;

/// Dumps the mempool.
#[method(name = "bundler_dumpMempool")]
async fn bundler_dump_mempool(&self, entry_point: Address) -> RpcResult<Vec<RpcUserOperation>>;
Expand Down Expand Up @@ -97,6 +101,16 @@ where
Ok("ok".to_string())
}

async fn bundler_clear_mempool(&self) -> RpcResult<String> {
let _ = self
.pool
.debug_clear_mempool()
.await
.map_err(|e| rpc_err(INTERNAL_ERROR_CODE, e.to_string()))?;

Ok("ok".to_string())
}

async fn bundler_dump_mempool(&self, entry_point: Address) -> RpcResult<Vec<RpcUserOperation>> {
Ok(self
.pool
Expand Down

0 comments on commit 6e42fdc

Please sign in to comment.