Skip to content

Commit

Permalink
Remove status field from all responses. (#749)
Browse files Browse the repository at this point in the history
* Remove status from responses. Added sleep to flaky test.

* Change recovery process to get data from db instead of tree.

* Remove unused trees (mined + processed).

* Fix testing inclusion proofs.

* Removed debug code.

* Remove commented code.

* Revert "Remove unused trees (mined + processed)."

This reverts commit c5c01c2.

* Remove mined tree.

* Fix tests.

* Remove status also from e2e tests.

* Fix e2e-tests.

* Code-review changes.

* Fmt changes.

* Fix e2e scenario.
  • Loading branch information
piohei authored Jul 25, 2024
1 parent 81fa44e commit f1ae274
Show file tree
Hide file tree
Showing 33 changed files with 608 additions and 336 deletions.
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.

2 changes: 2 additions & 0 deletions e2e_tests/scenarios/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ signup-sequencer = { path = "./../.." }
anyhow = "1.0"
ethers = { version = "2.0.10" }
hex = "0.4.3"
hex-literal = "0.4.1"
hyper = { version = "^0.14.17", features = ["tcp", "http1", "http2", "client"] }
rand = "0.8.5"
retry = "2.0.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
Expand Down
12 changes: 12 additions & 0 deletions e2e_tests/scenarios/tests/common/abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![allow(clippy::extra_unused_lifetimes)]

use ethers::prelude::abigen;

abigen!(
IWorldIDIdentityManager,
r#"[
struct RootInfo { uint256 root; uint128 supersededTimestamp; bool isValid }
function queryRoot(uint256 root) public view virtual returns (RootInfo memory)
]"#,
event_derives(serde::Deserialize, serde::Serialize)
);
62 changes: 62 additions & 0 deletions e2e_tests/scenarios/tests/common/chain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::fs::File;
use std::io::BufReader;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

use ethers::contract::{Contract, ContractFactory};
use ethers::core::k256::ecdsa::SigningKey;
use ethers::middleware::{Middleware, NonceManagerMiddleware, SignerMiddleware};
use ethers::prelude::{Bytes, LocalWallet, Signer, U256};
use ethers::providers::{Http, Provider};
use ethers::signers::Wallet;
use ethers::types::H160;
use ethers::utils::{Anvil, AnvilInstance};
use tracing::info;

use super::abi as ContractAbi;
use crate::common::abi::IWorldIDIdentityManager;
use crate::common::prelude::instrument;

type SpecialisedClient =
NonceManagerMiddleware<SignerMiddleware<Provider<Http>, Wallet<SigningKey>>>;
type SharableClient = Arc<SpecialisedClient>;
type SpecialisedFactory = ContractFactory<SpecialisedClient>;
pub type SpecialisedContract = Contract<SpecialisedClient>;

pub struct Chain {
pub private_key: SigningKey,
pub identity_manager: IWorldIDIdentityManager<SpecialisedClient>,
}

#[instrument(skip_all)]
pub async fn create_chain(chain_addr: String) -> anyhow::Result<Chain> {
// This private key is taken from tx-sitter configuration in compose.yaml.
// Env name: TX_SITTER__SERVICE__PREDEFINED__RELAYER__KEY_ID
let private_key = SigningKey::from_slice(&hex_literal::hex!(
"d10607662a85424f02a33fb1e6d095bd0ac7154396ff09762e41f82ff2233aaa"
))?;
// This address is taken from signup-sequencer configuration in config.toml.
// Section: [network], param name: identity_manager_address
let identity_manager_contract_address =
H160::from_str("0x48483748eb0446A16cAE79141D0688e3F624Cb73")?;

let wallet = LocalWallet::from(private_key.clone()).with_chain_id(31337u64);

let provider = Provider::<Http>::try_from(format!("http://{}", chain_addr))
.expect("Failed to initialize chain endpoint")
.interval(Duration::from_millis(500u64));

// connect the wallet to the provider
let client = SignerMiddleware::new(provider, wallet.clone());
let client = NonceManagerMiddleware::new(client, wallet.address());
let client = Arc::new(client);

let identity_manager =
IWorldIDIdentityManager::new(identity_manager_contract_address, client.clone());

Ok(Chain {
private_key,
identity_manager,
})
}
41 changes: 28 additions & 13 deletions e2e_tests/scenarios/tests/common/docker_compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl<'a> DockerComposeGuard<'a> {
format!("{}:{}", LOCAL_ADDR, self.signup_sequencer_balancer_port)
}

pub fn get_chain_addr(&self) -> String {
format!("{}:{}", LOCAL_ADDR, self.chain_port)
}

pub async fn restart_sequencer(&self) -> anyhow::Result<()> {
let (stdout, stderr) = run_cmd_to_output(
self.cwd,
Expand Down Expand Up @@ -96,6 +100,26 @@ impl<'a> DockerComposeGuard<'a> {
fn update_balancer_port(&mut self, signup_sequencer_balancer_port: u32) {
self.signup_sequencer_balancer_port = signup_sequencer_balancer_port
}

fn update_chain_port(&mut self, chain_port: u32) {
self.chain_port = chain_port
}

fn get_mapped_port(&self, service_name: &str, port: u32) -> anyhow::Result<u32> {
let (stdout, stderr) = run_cmd_to_output(
self.cwd,
self.envs_with_ports(),
self.generate_command(format!("port {service_name} {port}").as_str()),
)
.context("Looking for balancer selected port.")?;

debug!(
"Docker compose starting output:\n stdout:\n{}\nstderr:\n{}\n",
stdout, stderr
);

Ok(parse_exposed_port(stdout))
}
}

impl<'a> Drop for DockerComposeGuard<'a> {
Expand Down Expand Up @@ -147,21 +171,12 @@ pub async fn setup(cwd: &str) -> anyhow::Result<DockerComposeGuard> {

tokio::time::sleep(Duration::from_secs(1)).await;

let (stdout, stderr) = run_cmd_to_output(
res.cwd,
res.envs_with_ports(),
res.generate_command("port signup-sequencer-balancer 8080"),
)
.context("Looking for balancer selected port.")?;

debug!(
"Docker compose starting output:\n stdout:\n{}\nstderr:\n{}\n",
stdout, stderr
);

let balancer_port = parse_exposed_port(stdout);
let balancer_port = res.get_mapped_port("signup-sequencer-balancer", 8080)?;
res.update_balancer_port(balancer_port);

let chain_port = res.get_mapped_port("chain", 8545)?;
res.update_chain_port(chain_port);

await_running(&res).await?;

Ok(res)
Expand Down
36 changes: 25 additions & 11 deletions e2e_tests/scenarios/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use std::time::Duration;

use anyhow::anyhow;
use ethers::providers::Middleware;
use ethers::types::U256;
use hyper::client::HttpConnector;
use hyper::Client;
Expand All @@ -17,10 +18,14 @@ use tracing::error;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::time::Uptime;

use crate::common::abi::RootInfo;
use crate::common::api::{delete_identity, inclusion_proof, inclusion_proof_raw, insert_identity};
use crate::common::chain::Chain;
use crate::common::prelude::StatusCode;

mod abi;
mod api;
pub mod chain;
pub mod docker_compose;

#[allow(unused)]
Expand Down Expand Up @@ -132,28 +137,37 @@ pub async fn insert_identity_with_retries(
pub async fn mined_inclusion_proof_with_retries(
client: &Client<HttpConnector>,
uri: &String,
chain: &Chain,
commitment: &Hash,
retries_count: usize,
retries_interval: f32,
) -> anyhow::Result<InclusionProofResponse> {
let mut last_res = Err(anyhow!("No calls at all"));
) -> anyhow::Result<()> {
let mut last_res = None;
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;
last_res = Some(inclusion_proof(client, uri, commitment).await?);

if let Some(ref inclusion_proof_json) = last_res {
if let Some(root) = inclusion_proof_json.0.root {
let (root, ..) = chain
.identity_manager
.query_root(root.into())
.call()
.await?;

if root != U256::zero() {
return Ok(());
}
}
};

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

let inclusion_proof_json = last_res?;

assert_eq!(inclusion_proof_json.0.status, Status::Processed(Mined));
if last_res.is_none() {
return Err(anyhow!("No calls at all"));
}

Ok(inclusion_proof_json)
Err(anyhow!("Inclusion proof not found on chain"))
}

pub async fn bad_request_inclusion_proof_with_retries(
Expand Down
5 changes: 3 additions & 2 deletions e2e_tests/scenarios/tests/insert_100.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod common;

use common::prelude::*;

use crate::common::docker_compose;
use crate::common::{chain, docker_compose};

#[tokio::test]
async fn insert_100() -> anyhow::Result<()> {
Expand All @@ -11,6 +11,7 @@ async fn insert_100() -> anyhow::Result<()> {
info!("Starting e2e test");

let docker_compose = docker_compose::setup("./../docker-compose").await?;
let chain = chain::create_chain(docker_compose.get_chain_addr()).await?;

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

for commitment in identities.iter() {
mined_inclusion_proof_with_retries(&client, &uri, commitment, 60, 10.0).await?;
mined_inclusion_proof_with_retries(&client, &uri, &chain, commitment, 60, 10.0).await?;
}

Ok(())
Expand Down
7 changes: 4 additions & 3 deletions e2e_tests/scenarios/tests/insert_delete_insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod common;

use common::prelude::*;

use crate::common::docker_compose;
use crate::common::{chain, docker_compose};

#[tokio::test]
async fn insert_delete_insert() -> anyhow::Result<()> {
Expand All @@ -11,6 +11,7 @@ async fn insert_delete_insert() -> anyhow::Result<()> {
info!("Starting e2e test");

let docker_compose = docker_compose::setup("./../docker-compose").await?;
let chain = chain::create_chain(docker_compose.get_chain_addr()).await?;

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

for commitment in identities.iter() {
mined_inclusion_proof_with_retries(&client, &uri, commitment, 60, 10.0).await?;
mined_inclusion_proof_with_retries(&client, &uri, &chain, commitment, 60, 10.0).await?;
}

let first_commitment = identities.first().unwrap();
Expand All @@ -37,7 +38,7 @@ async fn insert_delete_insert() -> anyhow::Result<()> {
}

for commitment in new_identities.iter() {
mined_inclusion_proof_with_retries(&client, &uri, commitment, 60, 10.0).await?;
mined_inclusion_proof_with_retries(&client, &uri, &chain, commitment, 60, 10.0).await?;
}

Ok(())
Expand Down
7 changes: 4 additions & 3 deletions e2e_tests/scenarios/tests/insert_restart_insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod common;

use common::prelude::*;

use crate::common::docker_compose;
use crate::common::{chain, docker_compose};

#[tokio::test]
async fn insert_restart_insert() -> anyhow::Result<()> {
Expand All @@ -11,6 +11,7 @@ async fn insert_restart_insert() -> anyhow::Result<()> {
info!("Starting e2e test");

let docker_compose = docker_compose::setup("./../docker-compose").await?;
let chain = chain::create_chain(docker_compose.get_chain_addr()).await?;

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

for commitment in identities.iter() {
mined_inclusion_proof_with_retries(&client, &uri, commitment, 60, 10.0).await?;
mined_inclusion_proof_with_retries(&client, &uri, &chain, commitment, 60, 10.0).await?;
}

docker_compose.restart_sequencer().await?;
Expand All @@ -34,7 +35,7 @@ async fn insert_restart_insert() -> anyhow::Result<()> {
}

for commitment in identities.iter() {
mined_inclusion_proof_with_retries(&client, &uri, commitment, 60, 10.0).await?;
mined_inclusion_proof_with_retries(&client, &uri, &chain, commitment, 60, 10.0).await?;
}

Ok(())
Expand Down
Loading

0 comments on commit f1ae274

Please sign in to comment.