Skip to content

Commit

Permalink
refactor(pool): only use protos reputation type at edge
Browse files Browse the repository at this point in the history
  • Loading branch information
dancoombs committed Jul 20, 2023
1 parent 0f60013 commit 718387f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
7 changes: 2 additions & 5 deletions src/op_pool/mempool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 5 additions & 9 deletions src/op_pool/mempool/uo_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
22 changes: 17 additions & 5 deletions src/op_pool/reputation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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));
}
}
}
10 changes: 4 additions & 6 deletions src/op_pool/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ where

let reps = mempool.dump_reputation();
Ok(Response::new(DebugDumpReputationResponse {
reputations: reps,
reputations: reps.into_iter().map(Into::into).collect(),
}))
}
}
Expand Down Expand Up @@ -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,
},
};

Expand Down Expand Up @@ -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;
Expand Down
31 changes: 29 additions & 2 deletions src/op_pool/types.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -70,6 +76,27 @@ impl TryFrom<MempoolOp> for PoolOperation {
}
}

impl From<ReputationStatus> for ProtoReputationStatus {
fn from(status: ReputationStatus) -> Self {
match status {
ReputationStatus::Ok => ProtoReputationStatus::Ok,
ReputationStatus::Throttled => ProtoReputationStatus::Throttled,
ReputationStatus::Banned => ProtoReputationStatus::Banned,
}
}
}

impl From<Reputation> 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::*;
Expand Down

0 comments on commit 718387f

Please sign in to comment.