From 727d99ef86e0c39aec7233e61899460a7e7dacc5 Mon Sep 17 00:00:00 2001 From: dancoombs Date: Wed, 19 Jul 2023 13:45:18 -0400 Subject: [PATCH] refactor(pool): only use protos reputation type at edge --- src/op_pool/mempool/mod.rs | 7 ++----- src/op_pool/mempool/uo_pool.rs | 14 +++++--------- src/op_pool/reputation.rs | 22 +++++++++++++++++----- src/op_pool/server.rs | 10 ++++------ src/op_pool/types.rs | 31 +++++++++++++++++++++++++++++-- 5 files changed, 57 insertions(+), 27 deletions(-) diff --git a/src/op_pool/mempool/mod.rs b/src/op_pool/mempool/mod.rs index a393be327..527f36416 100644 --- a/src/op_pool/mempool/mod.rs +++ b/src/op_pool/mempool/mod.rs @@ -9,11 +9,8 @@ use ethers::types::{Address, H256}; use strum::IntoEnumIterator; use self::error::MempoolResult; -use super::event::NewBlockEvent; -use crate::common::{ - protos::op_pool::Reputation, - types::{Entity, EntityType, UserOperation, ValidTimeRange}, -}; +use super::{event::NewBlockEvent, reputation::Reputation}; +use crate::common::types::{Entity, EntityType, UserOperation, ValidTimeRange}; /// In-memory operation pool pub trait Mempool: Send + Sync { diff --git a/src/op_pool/mempool/uo_pool.rs b/src/op_pool/mempool/uo_pool.rs index 4c1ba6d5b..60ee461fb 100644 --- a/src/op_pool/mempool/uo_pool.rs +++ b/src/op_pool/mempool/uo_pool.rs @@ -14,12 +14,11 @@ use super::{ Mempool, OperationOrigin, PoolConfig, PoolOperation, }; use crate::{ - common::{ - contracts::i_entry_point::IEntryPointEvents, - protos::op_pool::{Reputation, ReputationStatus}, - types::Entity, + common::{contracts::i_entry_point::IEntryPointEvents, types::Entity}, + op_pool::{ + event::NewBlockEvent, + reputation::{Reputation, ReputationManager, ReputationStatus}, }, - op_pool::{event::NewBlockEvent, reputation::ReputationManager}, }; /// The number of blocks that a throttled operation is allowed to be in the mempool @@ -213,10 +212,7 @@ where #[cfg(test)] mod tests { use super::*; - use crate::common::{ - protos::op_pool::{Reputation, ReputationStatus}, - types::UserOperation, - }; + use crate::common::types::UserOperation; #[test] fn add_single_op() { diff --git a/src/op_pool/reputation.rs b/src/op_pool/reputation.rs index 3cf24070c..b2de9ac59 100644 --- a/src/op_pool/reputation.rs +++ b/src/op_pool/reputation.rs @@ -7,7 +7,20 @@ use ethers::types::Address; use parking_lot::RwLock; use tokio::time::interval; -use crate::common::protos::op_pool::{Reputation, ReputationStatus}; +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum ReputationStatus { + Ok, + Throttled, + Banned, +} + +#[derive(Debug, Clone)] +pub struct Reputation { + pub address: Address, + pub status: ReputationStatus, + pub ops_seen: u64, + pub ops_included: u64, +} /// Reputation manager trait /// @@ -79,8 +92,8 @@ impl ReputationManager for HourlyMovingAverageReputation { .counts .iter() .map(|(address, count)| Reputation { - address: address.as_bytes().to_vec(), - status: reputation.status(*address).into(), + address: *address, + status: reputation.status(*address), ops_seen: count.ops_seen, ops_included: count.ops_included, }) @@ -354,8 +367,7 @@ mod tests { for rep in reps { assert_eq!(rep.ops_seen, 1000); assert_eq!(rep.ops_included, 1000); - let a = Address::from_slice(&rep.address); - assert!(addrs.contains(&a)); + assert!(addrs.contains(&rep.address)); } } } diff --git a/src/op_pool/server.rs b/src/op_pool/server.rs index f8e281cfe..a2e43c49f 100644 --- a/src/op_pool/server.rs +++ b/src/op_pool/server.rs @@ -199,7 +199,7 @@ where let reps = mempool.dump_reputation(); Ok(Response::new(DebugDumpReputationResponse { - reputations: reps, + reputations: reps.into_iter().map(Into::into).collect(), })) } } @@ -275,13 +275,11 @@ mod tests { ]; use crate::{ - common::{ - protos::op_pool::{self, Reputation}, - types::Entity, - }, + common::types::Entity, op_pool::{ event::NewBlockEvent, mempool::{error::MempoolResult, PoolOperation}, + reputation::Reputation, }, }; @@ -327,7 +325,7 @@ mod tests { let oppool = given_oppool(); let request = Request::new(AddOpRequest { entry_point: TEST_ADDRESS_ARR.to_vec(), - op: Some(op_pool::MempoolOp::default()), + op: Some(MempoolOp::default()), }); let result = oppool.add_op(request).await; diff --git a/src/op_pool/types.rs b/src/op_pool/types.rs index 42a017ea0..dd777207e 100644 --- a/src/op_pool/types.rs +++ b/src/op_pool/types.rs @@ -1,11 +1,17 @@ use anyhow::Context; use ethers::types::{Address, H256}; -use super::mempool::PoolOperation; +use super::{ + mempool::PoolOperation, + reputation::{Reputation, ReputationStatus}, +}; use crate::common::{ protos::{ self, - op_pool::{EntityType as ProtoEntityType, MempoolOp, UserOperation}, + op_pool::{ + EntityType as ProtoEntityType, MempoolOp, Reputation as ProtoReputation, + ReputationStatus as ProtoReputationStatus, UserOperation, + }, ConversionError, }, types::ValidTimeRange, @@ -70,6 +76,27 @@ impl TryFrom for PoolOperation { } } +impl From for ProtoReputationStatus { + fn from(status: ReputationStatus) -> Self { + match status { + ReputationStatus::Ok => ProtoReputationStatus::Ok, + ReputationStatus::Throttled => ProtoReputationStatus::Throttled, + ReputationStatus::Banned => ProtoReputationStatus::Banned, + } + } +} + +impl From for ProtoReputation { + fn from(rep: Reputation) -> Self { + ProtoReputation { + address: rep.address.as_bytes().to_vec(), + status: ProtoReputationStatus::from(rep.status).into(), + ops_seen: rep.ops_seen, + ops_included: rep.ops_included, + } + } +} + #[cfg(test)] mod tests { use super::*;