From 009937097ddce796da4cfb86c6923b8f1afd9d1e Mon Sep 17 00:00:00 2001 From: Alex <36385732+alex-miao@users.noreply.github.com> Date: Mon, 29 Jan 2024 09:10:04 -0800 Subject: [PATCH] feat(rpc): add debug_bundler_clearMempool endpoint (#568) --- crates/pool/proto/op_pool/op_pool.proto | 5 ++++- crates/pool/src/mempool/mod.rs | 4 ++-- crates/pool/src/mempool/reputation.rs | 11 +++++++++++ crates/pool/src/mempool/uo_pool.rs | 16 +++++++++++++--- crates/pool/src/server/local.rs | 24 +++++++++++++++++------- crates/pool/src/server/mod.rs | 6 +++++- crates/pool/src/server/remote/client.rs | 11 +++++++++-- crates/pool/src/server/remote/server.rs | 9 +++++++-- crates/rpc/src/debug.rs | 16 +++++++++++++++- 9 files changed, 83 insertions(+), 19 deletions(-) diff --git a/crates/pool/proto/op_pool/op_pool.proto b/crates/pool/proto/op_pool/op_pool.proto index f5b4df0c5..d30505268 100644 --- a/crates/pool/proto/op_pool/op_pool.proto +++ b/crates/pool/proto/op_pool/op_pool.proto @@ -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; diff --git a/crates/pool/src/mempool/mod.rs b/crates/pool/src/mempool/mod.rs index 0a8827570..036e26e84 100644 --- a/crates/pool/src/mempool/mod.rs +++ b/crates/pool/src/mempool/mod.rs @@ -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; diff --git a/crates/pool/src/mempool/reputation.rs b/crates/pool/src/mempool/reputation.rs index 0020d2678..4fc5fcc0b 100644 --- a/crates/pool/src/mempool/reputation.rs +++ b/crates/pool/src/mempool/reputation.rs @@ -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)] @@ -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)] @@ -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)] diff --git a/crates/pool/src/mempool/uo_pool.rs b/crates/pool/src/mempool/uo_pool.rs index e4d820706..f55f509de 100644 --- a/crates/pool/src/mempool/uo_pool.rs +++ b/crates/pool/src/mempool/uo_pool.rs @@ -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 { @@ -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![]); } @@ -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(); + } } } diff --git a/crates/pool/src/server/local.rs b/crates/pool/src/server/local.rs index 566b81614..b3c0f571f 100644 --- a/crates/pool/src/server/local.rs +++ b/crates/pool/src/server/local.rs @@ -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(()), @@ -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(()) } @@ -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), } @@ -570,7 +577,10 @@ enum ServerRequestKind { entry_point: Address, entity_updates: Vec, }, - DebugClearState, + DebugClearState { + clear_mempool: bool, + clear_reputation: bool, + }, DebugDumpMempool { entry_point: Address, }, diff --git a/crates/pool/src/server/mod.rs b/crates/pool/src/server/mod.rs index c854e8be4..622158618 100644 --- a/crates/pool/src/server/mod.rs +++ b/crates/pool/src/server/mod.rs @@ -91,7 +91,11 @@ pub trait PoolServer: Send + Sync + 'static { async fn subscribe_new_heads(&self) -> PoolResult + 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>; diff --git a/crates/pool/src/server/remote/client.rs b/crates/pool/src/server/remote/client.rs index ad5a2e62a..79a275be9 100644 --- a/crates/pool/src/server/remote/client.rs +++ b/crates/pool/src/server/remote/client.rs @@ -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; diff --git a/crates/pool/src/server/remote/server.rs b/crates/pool/src/server/remote/server.rs index 956dd415b..7438923d9 100644 --- a/crates/pool/src/server/remote/server.rs +++ b/crates/pool/src/server/remote/server.rs @@ -269,9 +269,14 @@ impl OpPool for OpPoolImpl { async fn debug_clear_state( &self, - _request: Request, + request: Request, ) -> Result> { - 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 {}, diff --git a/crates/rpc/src/debug.rs b/crates/rpc/src/debug.rs index c3d100c7d..8bb92d013 100644 --- a/crates/rpc/src/debug.rs +++ b/crates/rpc/src/debug.rs @@ -32,6 +32,10 @@ pub trait DebugApi { #[method(name = "bundler_clearState")] async fn bundler_clear_state(&self) -> RpcResult; + /// Clears the state of the mempool without affect reputations. + #[method(name = "bundler_clearMempool")] + async fn bundler_clear_mempool(&self) -> RpcResult; + /// Dumps the mempool. #[method(name = "bundler_dumpMempool")] async fn bundler_dump_mempool(&self, entry_point: Address) -> RpcResult>; @@ -90,7 +94,17 @@ where async fn bundler_clear_state(&self) -> RpcResult { 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 { + let _ = self + .pool + .debug_clear_state(true, false) .await .map_err(|e| rpc_err(INTERNAL_ERROR_CODE, e.to_string()))?;