Skip to content

Commit

Permalink
feat(rpc): add debug_bundler_clearMempool endpoint (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-miao authored Jan 29, 2024
1 parent cc23515 commit 0099370
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 19 deletions.
5 changes: 4 additions & 1 deletion crates/pool/proto/op_pool/op_pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ message UpdateEntitiesResponse {
}
message UpdateEntitiesSuccess {}

message DebugClearStateRequest {}
message DebugClearStateRequest {
bool clear_mempool = 1;
bool clear_reputation = 2;
}
message DebugClearStateResponse {
oneof result {
DebugClearStateSuccess success = 1;
Expand Down
4 changes: 2 additions & 2 deletions crates/pool/src/mempool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ pub trait Mempool: Send + Sync + 'static {

/// Debug methods

/// Clears the mempool
fn clear(&self);
/// Clears the mempool of UOs or reputation of all addresses
fn clear_state(&self, clear_mempool: bool, clear_reputation: bool);

/// 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
16 changes: 13 additions & 3 deletions crates/pool/src/mempool/uo_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,13 @@ where
self.state.read().pool.get_operation_by_hash(hash)
}

fn clear(&self) {
self.state.write().pool.clear()
fn clear_state(&self, clear_mempool: bool, clear_reputation: bool) {
if clear_mempool {
self.state.write().pool.clear()
}
if clear_reputation {
self.reputation.clear()
}
}

fn dump_reputation(&self) -> Vec<Reputation> {
Expand Down Expand Up @@ -727,7 +732,7 @@ mod tests {
.unwrap();
}
check_ops(pool.best_operations(3, 0).unwrap(), uos);
pool.clear();
pool.clear_state(true, true);
assert_eq!(pool.best_operations(3, 0).unwrap(), vec![]);
}

Expand Down Expand Up @@ -1563,5 +1568,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: 17 additions & 7 deletions crates/pool/src/server/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,15 @@ impl PoolServer for LocalPoolHandle {
}
}

async fn debug_clear_state(&self) -> Result<(), PoolServerError> {
let req = ServerRequestKind::DebugClearState;
async fn debug_clear_state(
&self,
clear_mempool: bool,
clear_reputation: bool,
) -> Result<(), PoolServerError> {
let req = ServerRequestKind::DebugClearState {
clear_mempool,
clear_reputation,
};
let resp = self.send(req).await?;
match resp {
ServerResponse::DebugClearState => Ok(()),
Expand Down Expand Up @@ -362,9 +369,9 @@ where
Ok(())
}

fn debug_clear_state(&self) -> PoolResult<()> {
fn debug_clear_state(&self, clear_mempool: bool, clear_reputation: bool) -> PoolResult<()> {
for mempool in self.mempools.values() {
mempool.clear();
mempool.clear_state(clear_mempool, clear_reputation);
}
Ok(())
}
Expand Down Expand Up @@ -477,8 +484,8 @@ where
Err(e) => Err(e),
}
},
ServerRequestKind::DebugClearState => {
match self.debug_clear_state() {
ServerRequestKind::DebugClearState { clear_mempool, clear_reputation } => {
match self.debug_clear_state(clear_mempool, clear_reputation) {
Ok(_) => Ok(ServerResponse::DebugClearState),
Err(e) => Err(e),
}
Expand Down Expand Up @@ -570,7 +577,10 @@ enum ServerRequestKind {
entry_point: Address,
entity_updates: Vec<EntityUpdate>,
},
DebugClearState,
DebugClearState {
clear_mempool: bool,
clear_reputation: bool,
},
DebugDumpMempool {
entry_point: Address,
},
Expand Down
6 changes: 5 additions & 1 deletion crates/pool/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ pub trait PoolServer: Send + Sync + 'static {
async fn subscribe_new_heads(&self) -> PoolResult<Pin<Box<dyn Stream<Item = NewHead> + Send>>>;

/// Clear the pool state, used for debug methods
async fn debug_clear_state(&self) -> PoolResult<()>;
async fn debug_clear_state(
&self,
clear_mempool: bool,
clear_reputation: bool,
) -> 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
11 changes: 9 additions & 2 deletions crates/pool/src/server/remote/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,18 @@ impl PoolServer for RemotePoolClient {
}
}

async fn debug_clear_state(&self) -> PoolResult<()> {
async fn debug_clear_state(
&self,
clear_mempool: bool,
clear_reputation: bool,
) -> PoolResult<()> {
let res = self
.op_pool_client
.clone()
.debug_clear_state(DebugClearStateRequest {})
.debug_clear_state(DebugClearStateRequest {
clear_mempool,
clear_reputation,
})
.await?
.into_inner()
.result;
Expand Down
9 changes: 7 additions & 2 deletions crates/pool/src/server/remote/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,14 @@ impl OpPool for OpPoolImpl {

async fn debug_clear_state(
&self,
_request: Request<DebugClearStateRequest>,
request: Request<DebugClearStateRequest>,
) -> Result<Response<DebugClearStateResponse>> {
let resp = match self.local_pool.debug_clear_state().await {
let req = request.into_inner();
let resp = match self
.local_pool
.debug_clear_state(req.clear_mempool, req.clear_reputation)
.await
{
Ok(_) => DebugClearStateResponse {
result: Some(debug_clear_state_response::Result::Success(
DebugClearStateSuccess {},
Expand Down
16 changes: 15 additions & 1 deletion 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 @@ -90,7 +94,17 @@ where
async fn bundler_clear_state(&self) -> RpcResult<String> {
let _ = self
.pool
.debug_clear_state()
.debug_clear_state(true, true)
.await
.map_err(|e| rpc_err(INTERNAL_ERROR_CODE, e.to_string()))?;

Ok("ok".to_string())
}

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

Expand Down

0 comments on commit 0099370

Please sign in to comment.