Skip to content

Commit

Permalink
feat: reputation different for each entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
Vid201 committed Jan 29, 2023
1 parent e491cf8 commit 625b0a8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 36 deletions.
5 changes: 4 additions & 1 deletion src/proto/uopool/uopool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ enum GetAllReputationResult {
NOT_GOT_ALL_REPUTATION = 1;
}

message GetAllReputationRequest {}
message GetAllReputationRequest {
types.H160 ep = 1;
}

message GetAllReputationResponse {
GetAllReputationResult result = 1;
Expand All @@ -76,6 +78,7 @@ enum SetReputationResult {

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

message SetReputationResponse {
Expand Down
9 changes: 6 additions & 3 deletions src/rpc/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ impl DebugApiServer for DebugApiServerImpl {
async fn set_reputation(
&self,
reputation_entries: Vec<ReputationEntry>,
_entry_point: Address,
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(),
ep: Some(entry_point.into()),
});

let response = uopool_grpc_client
Expand All @@ -88,10 +89,12 @@ impl DebugApiServer for DebugApiServerImpl {
))
}

async fn dump_reputation(&self, _entry_point: Address) -> RpcResult<Vec<ReputationEntry>> {
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 request = tonic::Request::new(GetAllReputationRequest {
ep: Some(entry_point.into()),
});

let response = uopool_grpc_client
.get_all_reputation(request)
Expand Down
29 changes: 18 additions & 11 deletions src/uopool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,30 +123,37 @@ pub async fn run(opts: UoPoolOpts, entry_points: Vec<Address>, chain_id: U256) -
let mut builder = tonic::transport::Server::builder();

let mut mempools = HashMap::<MempoolId, MempoolBox<Vec<UserOperation>>>::new();
let mut reputations = HashMap::<MempoolId, ReputationBox<Vec<ReputationEntry>>>::new();

for entry_point in entry_points {
let id = mempool_id(entry_point, chain_id);
mempools.insert(id, Box::<MemoryMempool>::default());

reputations.insert(id, Box::<MemoryReputation>::default());
if let Some(reputation) = reputations.get_mut(&id) {
reputation.init(
MIN_INCLUSION_RATE_DENOMINATOR,
THROTTLING_SLACK,
BAN_SLACK,
opts.min_stake,
opts.min_unstake_delay,
);
}
}

let reputation: Arc<RwLock<ReputationBox<Vec<ReputationEntry>>>> =
Arc::new(RwLock::new(Box::<MemoryReputation>::default()));
reputation.write().init(
MIN_INCLUSION_RATE_DENOMINATOR,
THROTTLING_SLACK,
BAN_SLACK,
opts.min_stake,
opts.min_unstake_delay,
);
let reputations = Arc::new(RwLock::new(reputations));

let svc = UoPoolServer::new(UoPoolService::new(
Arc::new(RwLock::new(mempools)),
reputation.clone(),
reputations.clone(),
chain_id,
));

tokio::spawn(async move {
loop {
reputation.write().update_hourly();
for reputation in reputations.write().values_mut() {
reputation.update_hourly();
}
tokio::time::sleep(Duration::from_secs(60 * 60)).await;
}
});
Expand Down
65 changes: 44 additions & 21 deletions src/uopool/services/uopool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
types::{reputation::ReputationEntry, user_operation::UserOperation},
uopool::{
mempool_id,
server::uopool::{
uo_pool_server::UoPool, AddRequest, AddResponse, AddResult, ClearRequest,
ClearResponse, ClearResult, GetAllReputationRequest, GetAllReputationResponse,
Expand All @@ -22,19 +23,19 @@ pub type UoPoolError = ErrorObject<'static>;

pub struct UoPoolService {
pub mempools: Arc<RwLock<HashMap<MempoolId, MempoolBox<Vec<UserOperation>>>>>,
pub reputation: Arc<RwLock<ReputationBox<Vec<ReputationEntry>>>>,
pub reputations: Arc<RwLock<HashMap<MempoolId, ReputationBox<Vec<ReputationEntry>>>>>,
pub chain_id: U256,
}

impl UoPoolService {
pub fn new(
mempools: Arc<RwLock<HashMap<MempoolId, MempoolBox<Vec<UserOperation>>>>>,
reputation: Arc<RwLock<ReputationBox<Vec<ReputationEntry>>>>,
reputations: Arc<RwLock<HashMap<MempoolId, ReputationBox<Vec<ReputationEntry>>>>>,
chain_id: U256,
) -> Self {
Self {
mempools,
reputation,
reputations,
chain_id,
}
}
Expand Down Expand Up @@ -96,7 +97,7 @@ impl UoPool for UoPoolService {
mempool.clear();
}

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

Ok(tonic::Response::new(ClearResponse {
result: ClearResult::Cleared as i32,
Expand All @@ -108,8 +109,6 @@ impl UoPool for UoPoolService {
&self,
request: tonic::Request<GetAllRequest>,
) -> Result<Response<GetAllResponse>, tonic::Status> {
use crate::uopool::mempool_id;

let req = request.into_inner();
let mut res = GetAllResponse::default();

Expand Down Expand Up @@ -147,31 +146,55 @@ impl UoPool for UoPoolService {
let req = request.into_inner();
let mut res = SetReputationResponse::default();

self.reputation
.write()
.set(req.res.iter().map(|re| re.clone().into()).collect());
if let Some(entry_point) = req.ep {
let entry_point: Address = entry_point
.try_into()
.map_err(|_| tonic::Status::invalid_argument("invalid entry point"))?;

res.result = SetReputationResult::SetReputation as i32;
if let Some(reputation) = self
.reputations
.write()
.get_mut(&mempool_id(entry_point, self.chain_id))
{
reputation.set(req.res.iter().map(|re| re.clone().into()).collect());
res.result = SetReputationResult::SetReputation as i32;
} else {
res.result = SetReputationResult::NotSetReputation as i32;
}

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

Ok(tonic::Response::new(res))
Err(tonic::Status::invalid_argument("missing entry point"))
}

#[cfg(debug_assertions)]
async fn get_all_reputation(
&self,
_request: tonic::Request<GetAllReputationRequest>,
request: tonic::Request<GetAllReputationRequest>,
) -> Result<Response<GetAllReputationResponse>, tonic::Status> {
let res = GetAllReputationResponse {
result: GetAllReputationResult::GotAllReputation as i32,
res: self
.reputation
let req = request.into_inner();
let mut res = GetAllReputationResponse::default();

if let Some(entry_point) = req.ep {
let entry_point: Address = entry_point
.try_into()
.map_err(|_| tonic::Status::invalid_argument("invalid entry point"))?;

if let Some(reputation) = self
.reputations
.read()
.get_all()
.iter()
.map(|re| (*re).into())
.collect(),
.get(&mempool_id(entry_point, self.chain_id))
{
res.result = GetAllReputationResult::GotAllReputation as i32;
res.res = reputation.get_all().iter().map(|re| (*re).into()).collect();
} else {
res.result = GetAllReputationResult::NotGotAllReputation as i32;
}

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

Ok(tonic::Response::new(res))
Err(tonic::Status::invalid_argument("missing entry point"))
}
}

0 comments on commit 625b0a8

Please sign in to comment.