Skip to content

Commit

Permalink
feat: implement debug reputation endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Vid201 committed Jan 28, 2023
1 parent 1c3ade9 commit e491cf8
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 20 deletions.
13 changes: 13 additions & 0 deletions src/proto/types/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,16 @@ message UserOperation {
bytes paymaster_and_data = 10;
bytes signature = 11;
}

enum ReputationStatus {
OK = 0;
THROTTLED = 1;
BANNED = 2;
}

message ReputationEntry {
types.H160 address = 1;
uint64 uo_seen = 2;
uint64 uo_included = 3;
ReputationStatus status = 4;
}
27 changes: 27 additions & 0 deletions src/proto/uopool/uopool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,38 @@ message ClearResponse {
ClearResult result = 1;
}

enum GetAllReputationResult {
GOT_ALL_REPUTATION = 0;
NOT_GOT_ALL_REPUTATION = 1;
}

message GetAllReputationRequest {}

message GetAllReputationResponse {
GetAllReputationResult result = 1;
repeated types.ReputationEntry res = 2;
}

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

message SetReputationRequest {
repeated types.ReputationEntry res = 1;
}

message SetReputationResponse {
SetReputationResult result = 1;
}

service UoPool {
rpc Add(AddRequest) returns (AddResponse);
rpc Remove(RemoveRequest) returns (RemoveResponse);

// debug
rpc GetAll(GetAllRequest) returns (GetAllResponse);
rpc Clear(ClearRequest) returns (ClearResponse);
rpc GetAllReputation(GetAllReputationRequest) returns (GetAllReputationResponse);
rpc SetReputation(SetReputationRequest) returns (SetReputationResponse);
}
51 changes: 45 additions & 6 deletions src/rpc/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use super::debug_api::DebugApiServer;
use crate::{
types::{reputation::ReputationEntry, user_operation::UserOperation},
uopool::server::uopool::{
uo_pool_client::UoPoolClient, ClearRequest, ClearResult, GetAllRequest,
GetAllResult,
uo_pool_client::UoPoolClient, ClearRequest, ClearResult, GetAllReputationRequest,
GetAllReputationResult, GetAllRequest, GetAllResult, SetReputationRequest,
SetReputationResult,
},
};
use anyhow::format_err;
Expand Down Expand Up @@ -61,11 +62,49 @@ impl DebugApiServer for DebugApiServerImpl {
))
}

async fn set_reputation(&self, _reputation_entries: Vec<ReputationEntry>) -> RpcResult<()> {
todo!()
async fn set_reputation(
&self,
reputation_entries: Vec<ReputationEntry>,
_entry_point: Address,
) -> RpcResult<()> {
let mut uopool_grpc_client = self.uopool_grpc_client.clone();

let request = tonic::Request::new(SetReputationRequest {
res: reputation_entries.iter().map(|re| (*re).into()).collect(),
});

let response = uopool_grpc_client
.set_reputation(request)
.await
.map_err(|status| format_err!("GRPC error (uopool): {}", status.message()))?
.into_inner();

if response.result == SetReputationResult::SetReputation as i32 {
return Ok(());
}

Err(jsonrpsee::core::Error::Custom(
"error setting reputation".to_string(),
))
}

async fn dump_reputation(&self) -> RpcResult<Vec<ReputationEntry>> {
todo!()
async fn dump_reputation(&self, _entry_point: Address) -> RpcResult<Vec<ReputationEntry>> {
let mut uopool_grpc_client = self.uopool_grpc_client.clone();

let request = tonic::Request::new(GetAllReputationRequest {});

let response = uopool_grpc_client
.get_all_reputation(request)
.await
.map_err(|status| format_err!("GRPC error (uopool): {}", status.message()))?
.into_inner();

if response.result == GetAllReputationResult::GotAllReputation as i32 {
return Ok(response.res.iter().map(|re| re.clone().into()).collect());
}

Err(jsonrpsee::core::Error::Custom(
"error getting reputation".to_string(),
))
}
}
8 changes: 6 additions & 2 deletions src/rpc/debug_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ pub trait DebugApi {
async fn dump_mempool(&self, entry_point: Address) -> RpcResult<Vec<UserOperation>>;

#[method(name = "setReputation")]
async fn set_reputation(&self, reputation_entries: Vec<ReputationEntry>) -> RpcResult<()>;
async fn set_reputation(
&self,
reputation_entries: Vec<ReputationEntry>,
entry_point: Address,
) -> RpcResult<()>;

#[method(name = "dumpReputation")]
async fn dump_reputation(&self) -> RpcResult<Vec<ReputationEntry>>;
async fn dump_reputation(&self, entry_point: Address) -> RpcResult<Vec<ReputationEntry>>;
}
45 changes: 45 additions & 0 deletions src/uopool/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,51 @@ pub mod types {
}
}
}

impl From<crate::types::reputation::ReputationEntry> for ReputationEntry {
fn from(reputation_entry: crate::types::reputation::ReputationEntry) -> Self {
Self {
address: Some(reputation_entry.address.into()),
uo_seen: reputation_entry.uo_seen,
uo_included: reputation_entry.uo_included,
status: match reputation_entry.status {
crate::types::reputation::ReputationStatus::OK => ReputationStatus::Ok,
crate::types::reputation::ReputationStatus::THROTTLED => {
ReputationStatus::Throttled
}
crate::types::reputation::ReputationStatus::BANNED => ReputationStatus::Banned,
} as i32,
}
}
}

impl From<ReputationEntry> for crate::types::reputation::ReputationEntry {
fn from(reputation_entry: ReputationEntry) -> Self {
Self {
address: {
if let Some(address) = reputation_entry.address {
address.into()
} else {
Address::zero()
}
},
uo_seen: reputation_entry.uo_seen,
uo_included: reputation_entry.uo_included,
status: match reputation_entry.status {
_ if reputation_entry.status == ReputationStatus::Ok as i32 => {
crate::types::reputation::ReputationStatus::OK
}
_ if reputation_entry.status == ReputationStatus::Throttled as i32 => {
crate::types::reputation::ReputationStatus::THROTTLED
}
_ if reputation_entry.status == ReputationStatus::Banned as i32 => {
crate::types::reputation::ReputationStatus::BANNED
}
_ => crate::types::reputation::ReputationStatus::OK,
},
}
}
}
}

pub mod uopool {
Expand Down
61 changes: 49 additions & 12 deletions src/uopool/services/uopool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::{
uopool::{
server::uopool::{
uo_pool_server::UoPool, AddRequest, AddResponse, AddResult, ClearRequest,
ClearResponse, ClearResult, GetAllRequest, GetAllResponse, GetAllResult, RemoveRequest,
RemoveResponse,
ClearResponse, ClearResult, GetAllReputationRequest, GetAllReputationResponse,
GetAllReputationResult, GetAllRequest, GetAllResponse, GetAllResult, RemoveRequest,
RemoveResponse, SetReputationRequest, SetReputationResponse, SetReputationResult,
},
MempoolBox, MempoolId, ReputationBox,
},
Expand Down Expand Up @@ -86,6 +87,22 @@ impl UoPool for UoPoolService {
Err(tonic::Status::unimplemented("todo"))
}

#[cfg(debug_assertions)]
async fn clear(
&self,
_request: tonic::Request<ClearRequest>,
) -> Result<Response<ClearResponse>, tonic::Status> {
for mempool in self.mempools.write().values_mut() {
mempool.clear();
}

self.reputation.write().clear();

Ok(tonic::Response::new(ClearResponse {
result: ClearResult::Cleared as i32,
}))
}

#[cfg(debug_assertions)]
async fn get_all(
&self,
Expand Down Expand Up @@ -123,18 +140,38 @@ impl UoPool for UoPoolService {
}

#[cfg(debug_assertions)]
async fn clear(
async fn set_reputation(
&self,
_request: tonic::Request<ClearRequest>,
) -> Result<Response<ClearResponse>, tonic::Status> {
for mempool in self.mempools.write().values_mut() {
mempool.clear();
}
request: tonic::Request<SetReputationRequest>,
) -> Result<Response<SetReputationResponse>, tonic::Status> {
let req = request.into_inner();
let mut res = SetReputationResponse::default();

self.reputation.write().clear();
self.reputation
.write()
.set(req.res.iter().map(|re| re.clone().into()).collect());

Ok(tonic::Response::new(ClearResponse {
result: ClearResult::Cleared as i32,
}))
res.result = SetReputationResult::SetReputation as i32;

Ok(tonic::Response::new(res))
}

#[cfg(debug_assertions)]
async fn get_all_reputation(
&self,
_request: tonic::Request<GetAllReputationRequest>,
) -> Result<Response<GetAllReputationResponse>, tonic::Status> {
let res = GetAllReputationResponse {
result: GetAllReputationResult::GotAllReputation as i32,
res: self
.reputation
.read()
.get_all()
.iter()
.map(|re| (*re).into())
.collect(),
};

Ok(tonic::Response::new(res))
}
}

0 comments on commit e491cf8

Please sign in to comment.