From e8d153613ac87681d0ec8088644bc7a65e6fc534 Mon Sep 17 00:00:00 2001 From: Vid Kersic Date: Sat, 21 Oct 2023 22:39:47 +0200 Subject: [PATCH] chore: move builder from grpc to uopool, refactor examples, storage examples --- Cargo.lock | 58 +++++++++++++------ Cargo.toml | 2 +- crates/grpc/Cargo.toml | 1 - crates/grpc/src/lib.rs | 1 - crates/grpc/src/uopool.rs | 3 +- crates/uopool/Cargo.toml | 1 + crates/{grpc => uopool}/src/builder.rs | 8 +-- crates/uopool/src/lib.rs | 2 + crates/uopool/src/validate/validator.rs | 2 + examples/Cargo.toml | 44 -------------- examples/README.md | 26 +++++++-- examples/simple-account/Cargo.toml | 39 +++++++++++++ .../examples}/create.rs | 4 +- .../examples}/deposit.rs | 0 .../examples}/transfer.rs | 4 +- examples/{ => simple-account}/src/lib.rs | 0 examples/storage/Cargo.toml | 28 +++++++++ examples/storage/examples/database.rs | 45 ++++++++++++++ examples/storage/examples/memory.rs | 38 ++++++++++++ examples/user-operation/Cargo.toml | 21 +++++++ .../examples}/user_operation.rs | 0 21 files changed, 250 insertions(+), 77 deletions(-) rename crates/{grpc => uopool}/src/builder.rs (99%) delete mode 100644 examples/Cargo.toml create mode 100644 examples/simple-account/Cargo.toml rename examples/{simple_account => simple-account/examples}/create.rs (97%) rename examples/{simple_account => simple-account/examples}/deposit.rs (100%) rename examples/{simple_account => simple-account/examples}/transfer.rs (97%) rename examples/{ => simple-account}/src/lib.rs (100%) create mode 100644 examples/storage/Cargo.toml create mode 100644 examples/storage/examples/database.rs create mode 100644 examples/storage/examples/memory.rs create mode 100644 examples/user-operation/Cargo.toml rename examples/{ => user-operation/examples}/user_operation.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 29f2d500..901206cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2119,6 +2119,45 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +[[package]] +name = "examples-simple-account" +version = "0.3.0-alpha" +dependencies = [ + "alloy-primitives 0.3.3", + "alloy-sol-types 0.3.2", + "ethers 2.0.8", + "eyre", + "reqwest", + "serde", + "serde_json", + "silius-contracts", + "silius-primitives", + "silius-tests", + "tokio", +] + +[[package]] +name = "examples-storage" +version = "0.3.0-alpha" +dependencies = [ + "ethers 2.0.8", + "eyre", + "silius-primitives", + "silius-uopool", + "tempdir", + "tokio", +] + +[[package]] +name = "examples-user-operation" +version = "0.3.0-alpha" +dependencies = [ + "ethers 2.0.8", + "eyre", + "silius-primitives", + "tokio", +] + [[package]] name = "expanded-pathbuf" version = "0.1.2" @@ -5369,23 +5408,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "silius-examples" -version = "0.3.0-alpha" -dependencies = [ - "alloy-primitives 0.3.3", - "alloy-sol-types 0.3.2", - "ethers 2.0.8", - "eyre", - "reqwest", - "serde", - "serde_json", - "silius-contracts", - "silius-primitives", - "silius-tests", - "tokio", -] - [[package]] name = "silius-grpc" version = "0.3.0-alpha" @@ -5396,7 +5418,6 @@ dependencies = [ "ethers 2.0.8", "expanded-pathbuf", "eyre", - "futures-util", "parking_lot", "prost", "prost-build", @@ -5478,6 +5499,7 @@ dependencies = [ "enumset", "ethers 2.0.8", "eyre", + "futures-util", "page_size 0.5.0", "parking_lot", "prost", diff --git a/Cargo.toml b/Cargo.toml index 64232086..3d784cf0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ members = [ "crates/primitives", "crates/rpc", "crates/uopool", - "examples", + "examples/*", "tests", ] default-members = ["bin/silius"] diff --git a/crates/grpc/Cargo.toml b/crates/grpc/Cargo.toml index fe60ad33..73ae2cd0 100644 --- a/crates/grpc/Cargo.toml +++ b/crates/grpc/Cargo.toml @@ -17,7 +17,6 @@ dashmap = "5.4.0" ethers = { workspace = true } expanded-pathbuf = { workspace = true } eyre = { workspace = true } -futures-util = { workspace = true } parking_lot = { workspace = true } prost = "0.11" serde_json = { workspace = true } diff --git a/crates/grpc/src/lib.rs b/crates/grpc/src/lib.rs index af3ed69b..59fb1493 100644 --- a/crates/grpc/src/lib.rs +++ b/crates/grpc/src/lib.rs @@ -1,6 +1,5 @@ #![allow(dead_code)] -mod builder; mod bundler; mod proto; mod uopool; diff --git a/crates/grpc/src/uopool.rs b/crates/grpc/src/uopool.rs index 5e23b311..cf4290d6 100644 --- a/crates/grpc/src/uopool.rs +++ b/crates/grpc/src/uopool.rs @@ -1,6 +1,5 @@ use crate::proto::uopool::*; use crate::{ - builder::UoPoolBuilder, proto::types::{GetChainIdResponse, GetSupportedEntryPointsResponse}, utils::{parse_addr, parse_hash, parse_uo}, }; @@ -20,7 +19,7 @@ use silius_uopool::{ }; use silius_uopool::{ mempool_id, validate::validator::StandardUserOperationValidator, MempoolId, Reputation, - UoPool as UserOperationPool, + UoPool as UserOperationPool, UoPoolBuilder, }; use std::fmt::{Debug, Display}; use std::{net::SocketAddr, sync::Arc, time::Duration}; diff --git a/crates/uopool/Cargo.toml b/crates/uopool/Cargo.toml index 0d4b55a5..fbad6289 100644 --- a/crates/uopool/Cargo.toml +++ b/crates/uopool/Cargo.toml @@ -16,6 +16,7 @@ educe = { version = "0.4", features = ["Debug", "Default"] } enumset = "1.1.2" ethers = { workspace = true } eyre = { workspace = true } +futures-util = { workspace = true } page_size = "0.5.0" parking_lot = { workspace = true } prost = "0.11" diff --git a/crates/grpc/src/builder.rs b/crates/uopool/src/builder.rs similarity index 99% rename from crates/grpc/src/builder.rs rename to crates/uopool/src/builder.rs index 1375b408..4c233a40 100644 --- a/crates/grpc/src/builder.rs +++ b/crates/uopool/src/builder.rs @@ -1,3 +1,7 @@ +use crate::{ + validate::validator::StandardUserOperationValidator, Mempool, MempoolBox, Reputation, + ReputationBox, UoPool, VecCh, VecUo, +}; use ethers::{ providers::Middleware, types::{Address, H256, U256}, @@ -14,10 +18,6 @@ use silius_primitives::{ reputation::ReputationEntry, Chain, UserOperation, }; -use silius_uopool::{ - validate::validator::StandardUserOperationValidator, Mempool, MempoolBox, Reputation, - ReputationBox, UoPool, VecCh, VecUo, -}; use std::sync::Arc; use std::{ fmt::{Debug, Display}, diff --git a/crates/uopool/src/lib.rs b/crates/uopool/src/lib.rs index 1174133e..357bcbe0 100644 --- a/crates/uopool/src/lib.rs +++ b/crates/uopool/src/lib.rs @@ -1,6 +1,7 @@ //! The UserOperation alternative mempool implementation according to the [ERC-4337 specifications](https://eips.ethereum.org/EIPS/eip-4337#Alternative%20Mempools). #![allow(dead_code)] +mod builder; mod database; mod memory; mod mempool; @@ -9,6 +10,7 @@ mod uopool; mod utils; pub mod validate; +pub use builder::UoPoolBuilder; pub use database::{ init_env, mempool::DatabaseMempool, reputation::DatabaseReputation, DBError, WriteMap, }; diff --git a/crates/uopool/src/validate/validator.rs b/crates/uopool/src/validate/validator.rs index 2b5deb48..d78405ba 100644 --- a/crates/uopool/src/validate/validator.rs +++ b/crates/uopool/src/validate/validator.rs @@ -152,6 +152,8 @@ where .with_sanity_check(Paymaster) .with_sanity_check(Entities) .with_sanity_check(UnstakedEntities) + .with_simulation_check(Signature) + .with_simulation_check(Timestamp) } /// Simulates validation of a [UserOperation](UserOperation) via the [simulate_validation](crate::entry_point::EntryPoint::simulate_validation) method of the [entry_point](crate::entry_point::EntryPoint). diff --git a/examples/Cargo.toml b/examples/Cargo.toml deleted file mode 100644 index df6a8836..00000000 --- a/examples/Cargo.toml +++ /dev/null @@ -1,44 +0,0 @@ -[package] -name = "silius-examples" -version = { workspace = true } -authors = { workspace = true } -edition = { workspace = true } -license = { workspace = true } -repository = { workspace = true } -rust-version = { workspace = true } -readme = "README.md" -description = """ -AA (ERC-4337) bundler examples -""" - -[dependencies] -alloy-primitives = "0.3" -alloy-sol-types = "0.3" -ethers = { workspace = true } -eyre = { workspace = true } -reqwest = { version = "0.11.4", features = ["json"] } -serde = "1" -serde_json = "1" -silius-contracts = { path = "../crates/contracts" } -silius-primitives = { path = "../crates/primitives" } -silius-tests = { path = "../tests" } -tokio = { workspace = true } - -[package.metadata.cargo-udeps.ignore] -normal = ["eyre", "silius-contracts", "silius-primitives", "silius-tests"] - -[[example]] -name = "user-operation" -path = "user_operation.rs" - -[[example]] -name = "simple-account-create" -path = "simple_account/create.rs" - -[[example]] -name = "simple-account-deposit" -path = "simple_account/deposit.rs" - -[[example]] -name = "simple-account-transfer" -path = "simple_account/transfer.rs" diff --git a/examples/README.md b/examples/README.md index 9fc73bed..a7844c2b 100644 --- a/examples/README.md +++ b/examples/README.md @@ -3,23 +3,41 @@ ### Creating your first user operation ```bash +cd user-operation cargo run --example user-operation ``` ### Simple account - create ```bash -SEED_PHRASE="test test test test test test test test test test test junk" BUNDLER_URL="http://127.0.0.1:3000" cargo run --example simple-account-create +cd simple-account +SEED_PHRASE="test test test test test test test test test test test junk" BUNDLER_URL="http://127.0.0.1:3000" cargo run --example create ``` ### Simple account - deposit funds to entrypoint ```bash -SEED_PHRASE="test test test test test test test test test test test junk" PROVIDER_URL="http://127.0.0.1:3000" cargo run --example simple-account-deposit +cd simple-account +SEED_PHRASE="test test test test test test test test test test test junk" PROVIDER_URL="http://127.0.0.1:3000" cargo run --example deposit ``` ### Simple account - transfer ```bash -SEED_PHRASE="test test test test test test test test test test test junk" BUNDLER_URL="http://127.0.0.1:3000" cargo run --example simple-account-transfer -``` \ No newline at end of file +cd simple-account +SEED_PHRASE="test test test test test test test test test test test junk" BUNDLER_URL="http://127.0.0.1:3000" cargo run --example transfer +``` + +### Storage - memory + +```bash +cd storage +PROVIDER_URL="http://127.0.0.1:8545" cargo run --example memory +``` + +### Storage - database + +```bash +cd storage +PROVIDER_URL="http://127.0.0.1:8545" cargo run --example database +``` diff --git a/examples/simple-account/Cargo.toml b/examples/simple-account/Cargo.toml new file mode 100644 index 00000000..472b0e98 --- /dev/null +++ b/examples/simple-account/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "examples-simple-account" +version = { workspace = true } +authors = { workspace = true } +edition = { workspace = true } +license = { workspace = true } +repository = { workspace = true } +rust-version = { workspace = true } +description = """ +AA (ERC-4337) bundler examples - simple account +""" + +[dependencies] +alloy-primitives = "0.3" +alloy-sol-types = "0.3" +ethers = { workspace = true } +eyre = { workspace = true } +reqwest = { version = "0.11.4", features = ["json"] } +serde = "1" +serde_json = "1" +silius-contracts = { path = "../../crates/contracts" } +silius-primitives = { path = "../../crates/primitives" } +silius-tests = { path = "../../tests" } +tokio = { workspace = true } + +[package.metadata.cargo-udeps.ignore] +normal = ["silius-contracts", "silius-primitives", "silius-tests"] + +[[example]] +name = "create" +path = "examples/create.rs" + +[[example]] +name = "deposit" +path = "examples/deposit.rs" + +[[example]] +name = "transfer" +path = "examples/transfer.rs" diff --git a/examples/simple_account/create.rs b/examples/simple-account/examples/create.rs similarity index 97% rename from examples/simple_account/create.rs rename to examples/simple-account/examples/create.rs index 5e7aa50a..1eb5ab89 100644 --- a/examples/simple_account/create.rs +++ b/examples/simple-account/examples/create.rs @@ -4,8 +4,10 @@ use ethers::{ signers::{coins_bip39::English, MnemonicBuilder, Signer}, types::{transaction::eip2718::TypedTransaction, Address, Bytes, U256}, }; +use examples_simple_account::{ + simple_account::SimpleAccountExecute, EstimateResult, Request, Response, +}; use reqwest; -use silius_examples::{simple_account::SimpleAccountExecute, EstimateResult, Request, Response}; use silius_primitives::consts::entry_point::ADDRESS; use silius_primitives::UserOperation; use silius_primitives::Wallet as UoWallet; diff --git a/examples/simple_account/deposit.rs b/examples/simple-account/examples/deposit.rs similarity index 100% rename from examples/simple_account/deposit.rs rename to examples/simple-account/examples/deposit.rs diff --git a/examples/simple_account/transfer.rs b/examples/simple-account/examples/transfer.rs similarity index 97% rename from examples/simple_account/transfer.rs rename to examples/simple-account/examples/transfer.rs index 445aaed5..7aa3f428 100644 --- a/examples/simple_account/transfer.rs +++ b/examples/simple-account/examples/transfer.rs @@ -5,9 +5,11 @@ use ethers::{ types::{Address, Bytes, U256}, utils::parse_ether, }; +use examples_simple_account::{ + simple_account::SimpleAccountExecute, EstimateResult, Request, Response, +}; use reqwest; use silius_contracts::EntryPoint; -use silius_examples::{simple_account::SimpleAccountExecute, EstimateResult, Request, Response}; use silius_primitives::consts::entry_point::ADDRESS; use silius_primitives::UserOperation; use silius_primitives::Wallet as UoWallet; diff --git a/examples/src/lib.rs b/examples/simple-account/src/lib.rs similarity index 100% rename from examples/src/lib.rs rename to examples/simple-account/src/lib.rs diff --git a/examples/storage/Cargo.toml b/examples/storage/Cargo.toml new file mode 100644 index 00000000..be857998 --- /dev/null +++ b/examples/storage/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "examples-storage" +version = { workspace = true } +authors = { workspace = true } +edition = { workspace = true } +license = { workspace = true } +repository = { workspace = true } +rust-version = { workspace = true } +description = """ +AA (ERC-4337) bundler examples - storage +""" + +[dependencies] +ethers = { workspace = true } +eyre = { workspace = true } +silius-primitives = { path = "../../crates/primitives" } +silius-uopool = { path = "../../crates/uopool" } +tempdir = "0.3.7" +tokio = { workspace = true } + +[[example]] +name = "memory" +path = "examples/memory.rs" + +[[example]] +name = "database" +path = "examples/database.rs" + diff --git a/examples/storage/examples/database.rs b/examples/storage/examples/database.rs new file mode 100644 index 00000000..5fcc407f --- /dev/null +++ b/examples/storage/examples/database.rs @@ -0,0 +1,45 @@ +use ethers::types::{Address, U256}; +use silius_primitives::{consts::entry_point::ADDRESS, provider::create_http_provider, Chain}; +use silius_uopool::{init_env, DatabaseMempool, DatabaseReputation, UoPoolBuilder, WriteMap}; +use std::{env, str::FromStr, sync::Arc}; +use tempdir::TempDir; + +#[tokio::main] +async fn main() -> eyre::Result<()> { + // uopool needs connection to the execution client + let provider_url = env::var("PROVIDER_URL").unwrap(); + + // initialize database env + let dir = TempDir::new("silius-db").unwrap(); + let env = Arc::new(init_env::(dir.into_path()).expect("Init mdbx failed")); + env.create_tables() + .expect("Create mdbx database tables failed"); + + // creating uopool with builder + let builder = UoPoolBuilder::new( + false, // whether uoppol is in unsafe mode + Arc::new(create_http_provider(provider_url.as_str()).await?), // provider + Address::from_str(ADDRESS)?, // entry point address + Chain::Named(ethers::types::Chain::Dev), // chain information + U256::from(5000000), // max verification gas + U256::from(1), // min stake + U256::from(0), // min priority fee per gas + vec![], // whitelisted entities + DatabaseMempool::new(env.clone()), // database mempool of user operations + DatabaseReputation::new(env), // database reputation + ); + + // optional: subscription to block updates and reputation updates + // builder.register_block_updates(block_stream); + // builder.register_reputation_updates(); + + println!("Database uopool created!"); + + // size of mempool + println!( + "Mempool size: {size}", + size = builder.uopool().get_all().len() + ); + + Ok(()) +} diff --git a/examples/storage/examples/memory.rs b/examples/storage/examples/memory.rs new file mode 100644 index 00000000..a0e50129 --- /dev/null +++ b/examples/storage/examples/memory.rs @@ -0,0 +1,38 @@ +use ethers::types::{Address, U256}; +use silius_primitives::{consts::entry_point::ADDRESS, provider::create_http_provider, Chain}; +use silius_uopool::{MemoryMempool, MemoryReputation, UoPoolBuilder}; +use std::{env, str::FromStr, sync::Arc}; + +#[tokio::main] +async fn main() -> eyre::Result<()> { + // uopool needs connection to the execution client + let provider_url = env::var("PROVIDER_URL").unwrap(); + + // creating uopool with builder + let builder = UoPoolBuilder::new( + false, // whether uoppol is in unsafe mode + Arc::new(create_http_provider(provider_url.as_str()).await?), // provider + Address::from_str(ADDRESS)?, // entry point address + Chain::Named(ethers::types::Chain::Dev), // chain information + U256::from(5000000), // max verification gas + U256::from(1), // min stake + U256::from(0), // min priority fee per gas + vec![], // whitelisted entities + MemoryMempool::default(), // in-memory mempool of user operations + MemoryReputation::default(), // in-memory reputation + ); + + // optional: subscription to block updates and reputation updates + // builder.register_block_updates(block_stream); + // builder.register_reputation_updates(); + + println!("In-memory uopool created!"); + + // size of mempool + println!( + "Mempool size: {size}", + size = builder.uopool().get_all().len() + ); + + Ok(()) +} diff --git a/examples/user-operation/Cargo.toml b/examples/user-operation/Cargo.toml new file mode 100644 index 00000000..f2bacaec --- /dev/null +++ b/examples/user-operation/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "examples-user-operation" +version = { workspace = true } +authors = { workspace = true } +edition = { workspace = true } +license = { workspace = true } +repository = { workspace = true } +rust-version = { workspace = true } +description = """ +AA (ERC-4337) bundler examples - user operation +""" + +[dependencies] +ethers = { workspace = true } +eyre = { workspace = true } +silius-primitives = { path = "../../crates/primitives" } +tokio = { workspace = true } + +[[example]] +name = "user-operation" +path = "examples/user_operation.rs" diff --git a/examples/user_operation.rs b/examples/user-operation/examples/user_operation.rs similarity index 100% rename from examples/user_operation.rs rename to examples/user-operation/examples/user_operation.rs