Skip to content

Commit

Permalink
Remove status also from e2e tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
piohei committed Jul 11, 2024
1 parent 035623b commit 006331f
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 28 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.

1 change: 1 addition & 0 deletions e2e_tests/scenarios/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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!(
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)
);
66 changes: 66 additions & 0 deletions e2e_tests/scenarios/tests/common/chain.rs
Original file line number Diff line number Diff line change
@@ -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<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: SpecialisedContract,
}

#[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(&[
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::<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: SpecialisedContract = Contract::new(
identity_manager_contract_address,
ContractAbi::IDENTITYMANAGERCONTRACT_ABI.clone(),
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
29 changes: 22 additions & 7 deletions e2e_tests/scenarios/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -132,28 +136,39 @@ 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> {
) -> 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::<U256, RootInfo>("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(
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

0 comments on commit 006331f

Please sign in to comment.