From 006331fdd6791076bf5fe631cd565ff8ed0967a8 Mon Sep 17 00:00:00 2001 From: Piotr Heilman Date: Thu, 11 Jul 2024 16:56:35 +0200 Subject: [PATCH] Remove status also from e2e tests. --- Cargo.lock | 1 + e2e_tests/scenarios/Cargo.toml | 1 + e2e_tests/scenarios/tests/common/abi.rs | 12 ++++ e2e_tests/scenarios/tests/common/chain.rs | 66 +++++++++++++++++++ .../scenarios/tests/common/docker_compose.rs | 41 ++++++++---- e2e_tests/scenarios/tests/common/mod.rs | 29 ++++++-- e2e_tests/scenarios/tests/insert_100.rs | 5 +- .../scenarios/tests/insert_delete_insert.rs | 7 +- .../scenarios/tests/insert_restart_insert.rs | 7 +- 9 files changed, 141 insertions(+), 28 deletions(-) create mode 100644 e2e_tests/scenarios/tests/common/abi.rs create mode 100644 e2e_tests/scenarios/tests/common/chain.rs diff --git a/Cargo.lock b/Cargo.lock index 8b007876..12a9464c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1987,6 +1987,7 @@ dependencies = [ "hyper", "rand", "retry", + "serde", "serde_json", "signup-sequencer", "tokio", diff --git a/e2e_tests/scenarios/Cargo.toml b/e2e_tests/scenarios/Cargo.toml index ec9a6a00..be7f19b2 100644 --- a/e2e_tests/scenarios/Cargo.toml +++ b/e2e_tests/scenarios/Cargo.toml @@ -20,6 +20,7 @@ hex = "0.4.3" 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" diff --git a/e2e_tests/scenarios/tests/common/abi.rs b/e2e_tests/scenarios/tests/common/abi.rs new file mode 100644 index 00000000..8aad7b31 --- /dev/null +++ b/e2e_tests/scenarios/tests/common/abi.rs @@ -0,0 +1,12 @@ +#![allow(clippy::extra_unused_lifetimes)] + +use ethers::prelude::abigen; + +abigen!( + IdentityManagerContract, + 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) +); diff --git a/e2e_tests/scenarios/tests/common/chain.rs b/e2e_tests/scenarios/tests/common/chain.rs new file mode 100644 index 00000000..35d12458 --- /dev/null +++ b/e2e_tests/scenarios/tests/common/chain.rs @@ -0,0 +1,66 @@ +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::prelude::instrument; + +type SpecialisedClient = + NonceManagerMiddleware, Wallet>>; +type SharableClient = Arc; +type SpecialisedFactory = ContractFactory; +pub type SpecialisedContract = Contract; + +pub struct Chain { + pub private_key: SigningKey, + pub identity_manager: SpecialisedContract, +} + +#[instrument(skip_all)] +pub async fn create_chain(chain_addr: String) -> anyhow::Result { + // 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(&[ + 0xd1, 0x06, 0x07, 0x66, 0x2a, 0x85, 0x42, 0x4f, 0x02, 0xa3, 0x3f, 0xb1, 0xe6, 0xd0, 0x95, + 0xbd, 0x0a, 0xc7, 0x15, 0x43, 0x96, 0xff, 0x09, 0x76, 0x2e, 0x41, 0xf8, 0x2f, 0xf2, 0x23, + 0x3a, 0xaa, + ])?; + // 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::::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: SpecialisedContract = Contract::new( + identity_manager_contract_address, + ContractAbi::IDENTITYMANAGERCONTRACT_ABI.clone(), + client.clone(), + ); + + Ok(Chain { + private_key, + identity_manager, + }) +} diff --git a/e2e_tests/scenarios/tests/common/docker_compose.rs b/e2e_tests/scenarios/tests/common/docker_compose.rs index 029358d4..0fa4fa60 100644 --- a/e2e_tests/scenarios/tests/common/docker_compose.rs +++ b/e2e_tests/scenarios/tests/common/docker_compose.rs @@ -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, @@ -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 { + 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> { @@ -147,21 +171,12 @@ pub async fn setup(cwd: &str) -> anyhow::Result { 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) diff --git a/e2e_tests/scenarios/tests/common/mod.rs b/e2e_tests/scenarios/tests/common/mod.rs index 9d1eebe2..c6202881 100644 --- a/e2e_tests/scenarios/tests/common/mod.rs +++ b/e2e_tests/scenarios/tests/common/mod.rs @@ -17,10 +17,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)] @@ -132,28 +136,39 @@ pub async fn insert_identity_with_retries( pub async fn mined_inclusion_proof_with_retries( client: &Client, uri: &String, + chain: &Chain, commitment: &Hash, retries_count: usize, retries_interval: f32, -) -> anyhow::Result { +) -> anyhow::Result<()> { 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; + if let Some(root) = inclusion_proof_json.root { + let res = chain + .identity_manager + .method::("queryRoot", root.into()) + .expect("Failed to create method queryRoot on chain.") + .call() + .await + .expect("Failed to call method queryRoot on chain."); + + if res.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_err() { + return last_res.map(|_| ()); + } - Ok(inclusion_proof_json) + Err(anyhow!("Inclusion proof not found on chain")) } pub async fn bad_request_inclusion_proof_with_retries( diff --git a/e2e_tests/scenarios/tests/insert_100.rs b/e2e_tests/scenarios/tests/insert_100.rs index 2710fc77..10e72eb2 100644 --- a/e2e_tests/scenarios/tests/insert_100.rs +++ b/e2e_tests/scenarios/tests/insert_100.rs @@ -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<()> { @@ -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(); @@ -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(()) diff --git a/e2e_tests/scenarios/tests/insert_delete_insert.rs b/e2e_tests/scenarios/tests/insert_delete_insert.rs index ef604fd5..7f4fbf01 100644 --- a/e2e_tests/scenarios/tests/insert_delete_insert.rs +++ b/e2e_tests/scenarios/tests/insert_delete_insert.rs @@ -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<()> { @@ -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(); @@ -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(); @@ -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(()) diff --git a/e2e_tests/scenarios/tests/insert_restart_insert.rs b/e2e_tests/scenarios/tests/insert_restart_insert.rs index 55948bf6..59229aae 100644 --- a/e2e_tests/scenarios/tests/insert_restart_insert.rs +++ b/e2e_tests/scenarios/tests/insert_restart_insert.rs @@ -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<()> { @@ -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(); @@ -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?; @@ -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(())