From c66a536dbdf83d8580d4d4d76e4b397611b62b1a Mon Sep 17 00:00:00 2001 From: Alex Miao Date: Wed, 24 Jan 2024 22:17:34 -0800 Subject: [PATCH] feat(rpc): add debug_bundler_clearMempool endpoint for spec tests --- crates/pool/proto/op_pool/op_pool.proto | 12 +++++++++ crates/pool/src/mempool/mod.rs | 8 +++++- crates/pool/src/mempool/reputation.rs | 11 +++++++++ crates/pool/src/mempool/uo_pool.rs | 14 +++++++++++ crates/pool/src/server/local.rs | 24 ++++++++++++++++++ crates/pool/src/server/mod.rs | 3 +++ crates/pool/src/server/remote/client.rs | 33 +++++++++++++++++++------ crates/pool/src/server/remote/server.rs | 26 ++++++++++++++++--- crates/rpc/src/debug.rs | 14 +++++++++++ 9 files changed, 134 insertions(+), 11 deletions(-) diff --git a/crates/pool/proto/op_pool/op_pool.proto b/crates/pool/proto/op_pool/op_pool.proto index fc0697f40..db82f6b36 100644 --- a/crates/pool/proto/op_pool/op_pool.proto +++ b/crates/pool/proto/op_pool/op_pool.proto @@ -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); @@ -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; } diff --git a/crates/pool/src/mempool/mod.rs b/crates/pool/src/mempool/mod.rs index 232d910f7..f3d30847e 100644 --- a/crates/pool/src/mempool/mod.rs +++ b/crates/pool/src/mempool/mod.rs @@ -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; 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 c3e0a18a8..667652a89 100644 --- a/crates/pool/src/mempool/uo_pool.rs +++ b/crates/pool/src/mempool/uo_pool.rs @@ -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() } @@ -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(); + } } } diff --git a/crates/pool/src/server/local.rs b/crates/pool/src/server/local.rs index 566b81614..da243103c 100644 --- a/crates/pool/src/server/local.rs +++ b/crates/pool/src/server/local.rs @@ -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> { let req = ServerRequestKind::DebugDumpMempool { entry_point }; let resp = self.send(req).await?; @@ -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> { let mempool = self.get_pool(entry_point)?; Ok(mempool @@ -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 }), @@ -571,6 +593,7 @@ enum ServerRequestKind { entity_updates: Vec, }, DebugClearState, + DebugClearMempool, DebugDumpMempool { entry_point: Address, }, @@ -609,6 +632,7 @@ enum ServerResponse { RemoveOps, UpdateEntities, DebugClearState, + DebugClearMempool, DebugDumpMempool { ops: Vec, }, diff --git a/crates/pool/src/server/mod.rs b/crates/pool/src/server/mod.rs index c854e8be4..c6dbf0f10 100644 --- a/crates/pool/src/server/mod.rs +++ b/crates/pool/src/server/mod.rs @@ -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>; diff --git a/crates/pool/src/server/remote/client.rs b/crates/pool/src/server/remote/client.rs index ad5a2e62a..7af13eb33 100644 --- a/crates/pool/src/server/remote/client.rs +++ b/crates/pool/src/server/remote/client.rs @@ -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}, @@ -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> { let res = self .op_pool_client diff --git a/crates/pool/src/server/remote/server.rs b/crates/pool/src/server/remote/server.rs index 956dd415b..6b24d7690 100644 --- a/crates/pool/src/server/remote/server.rs +++ b/crates/pool/src/server/remote/server.rs @@ -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, @@ -285,6 +287,24 @@ impl OpPool for OpPoolImpl { Ok(Response::new(resp)) } + async fn debug_clear_mempool( + &self, + _request: Request, + ) -> Result> { + 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, diff --git a/crates/rpc/src/debug.rs b/crates/rpc/src/debug.rs index c3d100c7d..0e3a99256 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>; @@ -97,6 +101,16 @@ where Ok("ok".to_string()) } + async fn bundler_clear_mempool(&self) -> RpcResult { + 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> { Ok(self .pool