Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add micro-oz binary #609

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

16 changes: 16 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ Fetch the [postgres](https://hub.docker.com/_/postgres) docker image before runn
docker pull postgres
```

## Micro OZ
Sequencer depends on Openzeppelin Defender. In order to run it locally we provide a mock version of this service under the `micro-oz` crate.

To run it, execute the following command:
```shell
cargo run -p micro-oz
```

By default it'll run on port `9876` and start with default values compatible with anvil.
Check out
```shell
cargo run -p micro-oz -- --help
```

for more configuration options.

### Worldcoin id contracts
Worldcoin id contracts are ethereum smart contracts that are used by the sequencer

Expand Down
2 changes: 2 additions & 0 deletions crates/micro-oz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ async-trait = "0.1.71"
axum = "0.6.19"
chrono = "0.4.26"
clap = { version = "4.3.14", features = ["env", "derive"] }
dotenvy = "0.15.0"
ethers = { version = "1.0.0", features = ["openssl"] }
hex = "0.4.3"
hyper = "0.14.27"
oz-api = { path = "../oz-api" }
serde = "1.0.171"
Expand Down
2 changes: 1 addition & 1 deletion crates/micro-oz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod server;

const DEFAULT_GAS_LIMIT: u32 = 1_000_000;

pub use self::server::{spawn, ServerHandle};
pub use self::server::{spawn, spawn_on_random_port, ServerHandle};

type PinheadSigner = SignerMiddleware<Provider<Http>, LocalWallet>;

Expand Down
52 changes: 52 additions & 0 deletions crates/micro-oz/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use clap::Parser;
use ethers::prelude::k256::ecdsa::SigningKey;

#[derive(Debug, Clone, Parser)]
#[clap(rename_all = "kebab-case")]
struct Args {
/// The port at which to serve
///
/// Set to 0 to use a random port
#[clap(short, long, env, default_value = "9876")]
port: u16,
/// The RPC url to use
///
/// Uses a default value compatible with anvil
#[clap(short, long, env, default_value = "http://127.0.0.1:8545")]
rpc_url: String,
/// A hex encoded private key
///
/// By default uses a private key used by anvil
#[clap(
short,
long,
env,
default_value = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
)]
secret_key: String,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenvy::dotenv().ok();
tracing_subscriber::fmt::init();

let args = Args::parse();

let private_key = args.secret_key.trim_start_matches("0x");
let private_key = hex::decode(private_key)?;

let signing_key = SigningKey::from_bytes(&private_key)?;

let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), args.port);

let handle = micro_oz::spawn(addr, args.rpc_url, signing_key).await?;

tracing::info!("Micro OZ listening on {}", handle.endpoint());

handle.wait().await;

Ok(())
}
22 changes: 20 additions & 2 deletions crates/micro-oz/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ impl ServerHandle {
format!("http://{}", self.addr)
}

pub async fn wait(self) {
if let Err(e) = self.server_join_handle.await {
tracing::error!("Server error: {:?}", e);
}
}

pub async fn shutdown(self) {
self.shutdown_notify.notify_waiters();

Expand All @@ -100,15 +106,18 @@ impl ServerHandle {
}
}

pub async fn spawn(rpc_url: String, secret_key: SigningKey) -> anyhow::Result<ServerHandle> {
pub async fn spawn(
addr: SocketAddr,
rpc_url: String,
secret_key: SigningKey,
) -> anyhow::Result<ServerHandle> {
let pinhead = Pinhead::new(rpc_url, secret_key).await?;

let router = Router::new()
.route("/txs", post(send_transaction).get(list_transactions))
.route("/txs/:tx_id", get(query_transaction))
.with_state(pinhead.clone());

let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0);
let listener = TcpListener::bind(addr).context("Failed to bind random port")?;
let local_addr = listener.local_addr()?;

Expand All @@ -132,3 +141,12 @@ pub async fn spawn(rpc_url: String, secret_key: SigningKey) -> anyhow::Result<Se
server_join_handle,
})
}

pub async fn spawn_on_random_port(
rpc_url: String,
secret_key: SigningKey,
) -> anyhow::Result<ServerHandle> {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0);

spawn(addr, rpc_url, secret_key).await
}
2 changes: 1 addition & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,17 @@
(ref_tree.proof(leaf_index).unwrap(), ref_tree.root())
}

#[instrument(skip_all)]
pub async fn test_recover_identity(
uri: &str,
client: &Client<HttpConnector>,
ref_tree: &mut PoseidonTree,
test_leaves: &[Field],
previous_leaf_index: usize,
new_leaf: Field,
new_leaf_index: usize,
expect_failure: bool,
) -> (merkle_tree::Proof<PoseidonHash>, Field) {

Check warning on line 364 in tests/common/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> tests/common/mod.rs:354:1 | 354 | #[instrument(skip_all)] | ^---------------------- | | | _in this procedural macro expansion | | 355 | | pub async fn test_recover_identity( 356 | | uri: &str, 357 | | client: &Client<HttpConnector>, ... | 363 | | expect_failure: bool, 364 | | ) -> (merkle_tree::Proof<PoseidonHash>, Field) { | |______________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default = note: this warning originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

Check warning on line 364 in tests/common/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> tests/common/mod.rs:354:1 | 354 | #[instrument(skip_all)] | ^---------------------- | | | _in this procedural macro expansion | | 355 | | pub async fn test_recover_identity( 356 | | uri: &str, 357 | | client: &Client<HttpConnector>, ... | 363 | | expect_failure: bool, 364 | | ) -> (merkle_tree::Proof<PoseidonHash>, Field) { | |______________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default = note: this warning originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

Check warning on line 364 in tests/common/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> tests/common/mod.rs:354:1 | 354 | #[instrument(skip_all)] | ^---------------------- | | | _in this procedural macro expansion | | 355 | | pub async fn test_recover_identity( 356 | | uri: &str, 357 | | client: &Client<HttpConnector>, ... | 363 | | expect_failure: bool, 364 | | ) -> (merkle_tree::Proof<PoseidonHash>, Field) { | |______________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default = note: this warning originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

Check warning on line 364 in tests/common/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> tests/common/mod.rs:354:1 | 354 | #[instrument(skip_all)] | ^---------------------- | | | _in this procedural macro expansion | | 355 | | pub async fn test_recover_identity( 356 | | uri: &str, 357 | | client: &Client<HttpConnector>, ... | 363 | | expect_failure: bool, 364 | | ) -> (merkle_tree::Proof<PoseidonHash>, Field) { | |______________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default = note: this warning originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

Check warning on line 364 in tests/common/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> tests/common/mod.rs:354:1 | 354 | #[instrument(skip_all)] | ^---------------------- | | | _in this procedural macro expansion | | 355 | | pub async fn test_recover_identity( 356 | | uri: &str, 357 | | client: &Client<HttpConnector>, ... | 363 | | expect_failure: bool, 364 | | ) -> (merkle_tree::Proof<PoseidonHash>, Field) { | |______________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default = note: this warning originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

Check warning on line 364 in tests/common/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> tests/common/mod.rs:354:1 | 354 | #[instrument(skip_all)] | ^---------------------- | | | _in this procedural macro expansion | | 355 | | pub async fn test_recover_identity( 356 | | uri: &str, 357 | | client: &Client<HttpConnector>, ... | 363 | | expect_failure: bool, 364 | | ) -> (merkle_tree::Proof<PoseidonHash>, Field) { | |______________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default = note: this warning originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

Check warning on line 364 in tests/common/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> tests/common/mod.rs:354:1 | 354 | #[instrument(skip_all)] | ^---------------------- | | | _in this procedural macro expansion | | 355 | | pub async fn test_recover_identity( 356 | | uri: &str, 357 | | client: &Client<HttpConnector>, ... | 363 | | expect_failure: bool, 364 | | ) -> (merkle_tree::Proof<PoseidonHash>, Field) { | |______________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default = note: this warning originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

Check warning on line 364 in tests/common/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> tests/common/mod.rs:354:1 | 354 | #[instrument(skip_all)] | ^---------------------- | | | _in this procedural macro expansion | | 355 | | pub async fn test_recover_identity( 356 | | uri: &str, 357 | | client: &Client<HttpConnector>, ... | 363 | | expect_failure: bool, 364 | | ) -> (merkle_tree::Proof<PoseidonHash>, Field) { | |______________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default = note: this warning originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

Check warning on line 364 in tests/common/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> tests/common/mod.rs:354:1 | 354 | #[instrument(skip_all)] | ^---------------------- | | | _in this procedural macro expansion | | 355 | | pub async fn test_recover_identity( 356 | | uri: &str, 357 | | client: &Client<HttpConnector>, ... | 363 | | expect_failure: bool, 364 | | ) -> (merkle_tree::Proof<PoseidonHash>, Field) { | |______________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default = note: this warning originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

Check warning on line 364 in tests/common/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> tests/common/mod.rs:354:1 | 354 | #[instrument(skip_all)] | ^---------------------- | | | _in this procedural macro expansion | | 355 | | pub async fn test_recover_identity( 356 | | uri: &str, 357 | | client: &Client<HttpConnector>, ... | 363 | | expect_failure: bool, 364 | | ) -> (merkle_tree::Proof<PoseidonHash>, Field) { | |______________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default = note: this warning originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)
let previous_leaf = test_leaves[previous_leaf_index];

let body = construct_recover_identity_body(&previous_leaf, &new_leaf);
Expand Down Expand Up @@ -638,7 +638,7 @@
let chain = chain?;

let signing_key = SigningKey::from_bytes(chain.private_key.as_bytes())?;
let micro_oz = micro_oz::spawn(chain.anvil.endpoint(), signing_key).await?;
let micro_oz = micro_oz::spawn_on_random_port(chain.anvil.endpoint(), signing_key).await?;

let insertion_provers = insertion_provers
.into_iter()
Expand Down
Loading