Skip to content

Commit

Permalink
tests: simulation tests support both storage type
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Nov 26, 2023
1 parent 80ce240 commit 2753871
Show file tree
Hide file tree
Showing 11 changed files with 606 additions and 349 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions crates/grpc/src/uopool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,12 @@ where

let ep = parse_addr(req.ep)?;
let uopool = self.get_uopool(&ep)?;

Ok(Response::new(GetAllResponse {
uos: uopool.get_all().into_iter().map(Into::into).collect(),
}))
match uopool.get_all() {
Ok(uos) => Ok(Response::new(GetAllResponse {
uos: uos.into_iter().map(Into::into).collect(),
})),
Err(err) => Err(Status::unknown(format!("Internal error: {err:?}"))),
}
}

async fn clear_mempool(&self, _req: Request<()>) -> Result<Response<()>, Status> {
Expand Down
21 changes: 8 additions & 13 deletions crates/uopool/src/database/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,14 @@ impl<E: EnvironmentKind> UserOperationOp for DatabaseTable<E, UserOperations> {
.map_err(|e| MempoolError::DBError(DBError::DBInternalError(e)))
}

fn get_all(&self) -> Vec<UserOperation> {
self.env
.tx()
.and_then(|tx| {
let mut c = tx.cursor_read::<UserOperations>()?;
let res: Vec<UserOperation> = c
.walk(Some(WrapUserOperationHash::default()))?
.map(|a| a.map(|(_, v)| v.into()))
.collect::<Result<Vec<_>, _>>()?;
tx.commit()?;
Ok(res)
})
.unwrap_or_else(|_| vec![])
fn get_all(&self) -> Result<Vec<UserOperation>, MempoolError> {
let tx = self.env.tx()?;
let mut c = tx.cursor_read::<UserOperations>()?;
let res: Vec<UserOperation> = c
.walk(Some(WrapUserOperationHash::default()))?
.map(|a| a.map(|(_, v)| v.into()))
.collect::<Result<Vec<_>, _>>()?;
Ok(res)
}
}
macro_rules! impl_user_op_addr_op {
Expand Down
4 changes: 2 additions & 2 deletions crates/uopool/src/memory/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl UserOperationOp for HashMap<UserOperationHash, UserOperation> {
Ok(uos)
}

fn get_all(&self) -> Vec<UserOperation> {
self.values().cloned().collect()
fn get_all(&self) -> Result<Vec<UserOperation>, MempoolError> {
Ok(self.values().cloned().collect())
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/uopool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub trait UserOperationOp {
uo_hash: &UserOperationHash,
) -> Result<Option<UserOperation>, MempoolError>;
fn get_sorted(&self) -> Result<Vec<UserOperation>, MempoolError>;
fn get_all(&self) -> Vec<UserOperation>;
fn get_all(&self) -> Result<Vec<UserOperation>, MempoolError>;
}

impl<T: UserOperationOp> UserOperationOp for Arc<RwLock<T>> {
Expand All @@ -112,7 +112,7 @@ impl<T: UserOperationOp> UserOperationOp for Arc<RwLock<T>> {
self.read().get_sorted()
}

fn get_all(&self) -> Vec<UserOperation> {
fn get_all(&self) -> Result<Vec<UserOperation>, MempoolError> {
self.read().get_all()
}
}
Expand Down Expand Up @@ -341,7 +341,7 @@ where
pub fn get_sorted(&self) -> Result<Vec<UserOperation>, MempoolError> {
self.user_operations.get_sorted()
}
pub fn get_all(&self) -> Vec<UserOperation> {
pub fn get_all(&self) -> Result<Vec<UserOperation>, MempoolError> {
self.user_operations.get_all()
}
pub fn clear(&mut self) {
Expand Down
2 changes: 1 addition & 1 deletion crates/uopool/src/uopool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ where
///
/// # Returns
/// `Vec<UserOperation>` - An array of [UserOperations](UserOperation)
pub fn get_all(&self) -> Vec<UserOperation> {
pub fn get_all(&self) -> Result<Vec<UserOperation>, MempoolError> {
self.mempool.get_all()
}

Expand Down
6 changes: 3 additions & 3 deletions crates/uopool/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,21 +368,21 @@ pub mod tests {
assert_eq!(mempool.get(&uo_hash).unwrap().unwrap(), uo);
}

assert_eq!(mempool.get_all().len(), 7);
assert_eq!(mempool.get_all().unwrap().len(), 7);
assert_eq!(mempool.get_all_by_sender(&senders[0]).len(), 2);
assert_eq!(mempool.get_all_by_sender(&senders[1]).len(), 2);
assert_eq!(mempool.get_all_by_sender(&senders[2]).len(), 3);

assert_eq!(mempool.remove(&uo_hash).unwrap(), true);
assert_eq!(mempool.remove(&H256::random().into()).unwrap(), false);

assert_eq!(mempool.get_all().len(), 6);
assert_eq!(mempool.get_all().unwrap().len(), 6);
assert_eq!(mempool.get_all_by_sender(&senders[0]).len(), 2);
assert_eq!(mempool.get_all_by_sender(&senders[2]).len(), 2);

assert_eq!(mempool.clear(), ());

assert_eq!(mempool.get_all().len(), 0);
assert_eq!(mempool.get_all().unwrap().len(), 0);
assert_eq!(mempool.get_all_by_sender(&senders[0]).len(), 0);

for i in 0..3 {
Expand Down
5 changes: 4 additions & 1 deletion examples/storage/examples/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ async fn main() -> eyre::Result<()> {
println!("Database uopool created!");

// size of mempool
println!("Mempool size: {size}", size = database.get_all().len());
println!(
"Mempool size: {size}",
size = database.get_all().unwrap().len()
);
}

Ok(())
Expand Down
5 changes: 4 additions & 1 deletion examples/storage/examples/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ async fn main() -> eyre::Result<()> {
println!("In-memory uopool created!");

// size of mempool
println!("Mempool size: {size}", size = hashmap.get_all().len());
println!(
"Mempool size: {size}",
size = hashmap.get_all().unwrap().len()
);
};

Ok(())
Expand Down
1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ AA (ERC-4337) bundler integration tests
alloy-chains = { workspace = true }
ethers = { workspace = true }
eyre = { workspace = true }
parking_lot = { workspace = true }
silius-contracts = { path = "../crates/contracts" }
silius-primitives = { path = "../crates/primitives" }
silius-uopool = { path = "../crates/uopool" }
Expand Down
Loading

0 comments on commit 2753871

Please sign in to comment.