Skip to content

Commit

Permalink
Use structs instead of plain JSONs.
Browse files Browse the repository at this point in the history
  • Loading branch information
piohei committed Jun 20, 2024
1 parent 66a3a90 commit 5327b54
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 397 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.

8 changes: 8 additions & 0 deletions e2e_tests/scenarios/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
name = "e2e-tests"
version = "0.1.0"
edition = "2021"
description = "A tool that processes WorldID signups on-chain."
keywords = ["worldcoin", "protocol", "signup"]
categories = ["cryptography::cryptocurrencies"]
repository = "https://github.com/worldcoin/signup-sequencer"
readme = "./../../Readme.md"
license-file = "./../../LICENSE.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
signup-sequencer = { path = "./../.." }

anyhow = "1.0"
ethers = { version = "2.0.10" }
hex = "0.4.3"
Expand Down
45 changes: 20 additions & 25 deletions e2e_tests/scenarios/tests/common/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use anyhow::Error;
use hyper::client::HttpConnector;
use hyper::{Body, Client, Request};
use serde_json::{json, Value};
use signup_sequencer::identity_tree::Hash;
use signup_sequencer::server::data::{
DeletionRequest, InclusionProofRequest, InclusionProofResponse, InsertCommitmentRequest,
};
use tracing::debug;

use crate::common::prelude::StatusCode;
Expand All @@ -16,15 +20,12 @@ pub struct RawResponse {
pub async fn insert_identity(
client: &Client<HttpConnector>,
uri: &String,
commitment: &String,
commitment: &Hash,
) -> anyhow::Result<()> {
debug!("Calling /insertIdentity");
let body = Body::from(
json!({
"identityCommitment": format!("0x{}", commitment),
})
.to_string(),
);
let body = Body::from(serde_json::to_string(&InsertCommitmentRequest {
identity_commitment: *commitment,
})?);

let req = Request::builder()
.method("POST")
Expand Down Expand Up @@ -55,15 +56,12 @@ pub async fn insert_identity(
pub async fn delete_identity(
client: &Client<HttpConnector>,
uri: &String,
commitment: &String,
commitment: &Hash,
) -> anyhow::Result<()> {
debug!("Calling /deleteIdentity");
let body = Body::from(
json!({
"identityCommitment": format!("0x{}", commitment),
})
.to_string(),
);
let body = Body::from(serde_json::to_string(&DeletionRequest {
identity_commitment: *commitment,
})?);

let req = Request::builder()
.method("POST")
Expand Down Expand Up @@ -94,15 +92,12 @@ pub async fn delete_identity(
pub async fn inclusion_proof_raw(
client: &Client<HttpConnector>,
uri: &String,
commitment: &String,
commitment: &Hash,
) -> anyhow::Result<RawResponse> {
debug!("Calling /inclusionProof");
let body = Body::from(
json!({
"identityCommitment": format!("0x{}", commitment),
})
.to_string(),
);
let body = Body::from(serde_json::to_string(&InclusionProofRequest {
identity_commitment: *commitment,
})?);

let req = Request::builder()
.method("POST")
Expand Down Expand Up @@ -130,12 +125,12 @@ pub async fn inclusion_proof_raw(
pub async fn inclusion_proof(
client: &Client<HttpConnector>,
uri: &String,
commitment: &String,
) -> anyhow::Result<Value> {
commitment: &Hash,
) -> anyhow::Result<InclusionProofResponse> {
let result = inclusion_proof_raw(client, uri, commitment).await?;

let result_json =
serde_json::from_str::<Value>(&result.body).expect("Failed to parse response as json");
let result_json = serde_json::from_str::<InclusionProofResponse>(&result.body)
.expect("Failed to parse response as json");

Ok(result_json)
}
12 changes: 5 additions & 7 deletions e2e_tests/scenarios/tests/common/docker_compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,11 @@ pub async fn setup(cwd: &str) -> anyhow::Result<DockerComposeGuard> {
}

fn generate_project_name() -> String {
let charset: &[u8] = b"abcdefghijklmnopqrstuvwxyz";
let mut rng = rand::thread_rng();
(0..8)
.map(|_| {
let idx = rng.gen_range(0..charset.len());
charset[idx] as char
})
thread_rng()
.sample_iter(Alphanumeric)
.filter(|c| c.is_ascii_lowercase())
.take(8)
.map(char::from)
.collect()
}

Expand Down
116 changes: 111 additions & 5 deletions e2e_tests/scenarios/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@
// test crates - so some code may not be used in some cases
#![allow(dead_code, clippy::too_many_arguments, unused_imports)]

use std::time::Duration;

use anyhow::anyhow;
use ethers::types::U256;
use hyper::client::HttpConnector;
use hyper::Client;
use serde_json::Value;
use signup_sequencer::identity_tree::ProcessedStatus::Mined;
use signup_sequencer::identity_tree::{Hash, Status};
use signup_sequencer::server::data::InclusionProofResponse;
use tokio::time::sleep;
use tracing::error;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::time::Uptime;

use crate::common::api::{delete_identity, inclusion_proof, inclusion_proof_raw, insert_identity};
use crate::common::prelude::StatusCode;

mod api;
pub mod docker_compose;

Expand All @@ -25,7 +38,11 @@ pub mod prelude {
pub use tracing::{error, info, instrument};
pub use tracing_subscriber::fmt::format;

pub use super::{generate_test_commitments, init_tracing_subscriber};
pub use super::{
bad_request_inclusion_proof_with_retries, delete_identity_with_retries,
generate_test_commitments, init_tracing_subscriber, insert_identity_with_retries,
mined_inclusion_proof_with_retries,
};
pub use crate::common::api::{
delete_identity, inclusion_proof, inclusion_proof_raw, insert_identity,
};
Expand Down Expand Up @@ -67,17 +84,106 @@ pub fn init_tracing_subscriber() {
/// would be used in reality. This is both to make them easier to generate and
/// to ensure that we do not run afoul of the element numeric limit for the
/// snark scalar field.
pub fn generate_test_commitments(count: usize) -> Vec<String> {
pub fn generate_test_commitments(count: usize) -> Vec<Hash> {
let mut commitments = vec![];

for _ in 0..count {
// Generate the identities using the just the last 64 bits (of 256) has so we're
// guaranteed to be less than SNARK_SCALAR_FIELD.
let bytes: [u8; 32] = U256::from(rand::random::<u64>()).into();
let identity_string: String = hex::encode(bytes);
let commitment = Hash::from(rand::random::<u64>());

commitments.push(identity_string);
commitments.push(commitment);
}

commitments
}

pub async fn delete_identity_with_retries(
client: &Client<HttpConnector>,
uri: &String,
commitment: &Hash,
retries_count: usize,
retries_interval: f32,
) -> anyhow::Result<()> {
let mut last_err = None;

for _ in 0..retries_count {
match delete_identity(client, uri, commitment).await {
Ok(_) => return Ok(()),
Err(err) => last_err = Some(err),
}
sleep(Duration::from_secs_f32(retries_interval)).await;
}

Err(last_err.unwrap_or_else(|| anyhow!("All retries failed without error")))
}

pub async fn insert_identity_with_retries(
client: &Client<HttpConnector>,
uri: &String,
commitment: &Hash,
retries_count: usize,
retries_interval: f32,
) -> anyhow::Result<()> {
let mut last_err = None;
for _ in 0..retries_count {
match insert_identity(&client, &uri, &commitment).await {
Ok(_) => return Ok(()),
Err(err) => last_err = Some(err),
}
_ = sleep(Duration::from_secs_f32(retries_interval)).await;
}

Err(last_err.unwrap_or_else(|| anyhow!("All retries failed without error")))
}

pub async fn mined_inclusion_proof_with_retries(
client: &Client<HttpConnector>,
uri: &String,
commitment: &Hash,
retries_count: usize,
retries_interval: f32,
) -> anyhow::Result<InclusionProofResponse> {
let mut last_res = Err(anyhow!("No calls at all"));
for _i in 0..retries_count {
last_res = inclusion_proof(&client, &uri, &commitment).await;

if let Ok(ref inclusion_proof_json) = last_res {
if inclusion_proof_json.0.status == Status::Processed(Mined) {
break;
}
};

_ = sleep(Duration::from_secs_f32(retries_interval)).await;
}

let inclusion_proof_json = last_res?;

assert_eq!(inclusion_proof_json.0.status, Status::Processed(Mined));

Ok(inclusion_proof_json)
}

pub async fn bad_request_inclusion_proof_with_retries(
client: &Client<HttpConnector>,
uri: &String,
commitment: &Hash,
retries_count: usize,
retries_interval: f32,
) -> anyhow::Result<()> {
let mut last_err = None;

for _ in 0..retries_count {
match inclusion_proof_raw(client, uri, commitment).await {
Ok(response) if response.status_code == StatusCode::BAD_REQUEST => return Ok(()),
Err(err) => {
error!("error: {}", err);
last_err = Some(err);
}
_ => {}
}
sleep(Duration::from_secs_f32(retries_interval)).await;
}

Err(last_err.unwrap_or_else(|| anyhow!("All retries failed to return BAD_REQUEST")))
}
56 changes: 1 addition & 55 deletions e2e_tests/scenarios/tests/insert_100.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod common;

use common::prelude::*;
use serde_json::Value;
use tokio::time::sleep;

use crate::common::docker_compose;

Expand All @@ -17,7 +15,7 @@ async fn insert_100() -> anyhow::Result<()> {
let uri = format!("http://{}", docker_compose.get_local_addr());
let client = Client::new();

let identities = generate_test_commitments(100);
let identities = generate_test_commitments(10);

for commitment in identities.iter() {
_ = insert_identity_with_retries(&client, &uri, commitment, 10, 3.0).await?;
Expand All @@ -29,55 +27,3 @@ async fn insert_100() -> anyhow::Result<()> {

Ok(())
}

async fn insert_identity_with_retries(
client: &Client<HttpConnector>,
uri: &String,
commitment: &String,
retries_count: usize,
retries_interval: f32,
) -> anyhow::Result<()> {
let mut last_res = Err(Error::msg("No calls at all"));
for _i in 0..retries_count {
last_res = insert_identity(&client, &uri, &commitment).await;

if last_res.is_ok() {
break;
}

_ = sleep(Duration::from_secs_f32(retries_interval)).await;
}

if let Err(err) = last_res {
return Err(err);
};

last_res
}

async fn mined_inclusion_proof_with_retries(
client: &Client<HttpConnector>,
uri: &String,
commitment: &String,
retries_count: usize,
retries_interval: f32,
) -> anyhow::Result<Value> {
let mut last_res = Err(Error::msg("No calls at all"));
for _i in 0..retries_count {
last_res = inclusion_proof(&client, &uri, &commitment).await;

if let Ok(ref inclusion_proof_json) = last_res {
if inclusion_proof_json["status"] == "mined" {
break;
}
};

_ = sleep(Duration::from_secs_f32(retries_interval)).await;
}

let inclusion_proof_json = last_res?;

assert_eq!(inclusion_proof_json["status"], "mined");

Ok(inclusion_proof_json)
}
Loading

0 comments on commit 5327b54

Please sign in to comment.