From 6eacfacf4eff3fef7db3a2d99195581818556988 Mon Sep 17 00:00:00 2001 From: Marcella Hastings Date: Fri, 25 Feb 2022 16:13:49 -0500 Subject: [PATCH] force tests to wait until blockchain is ready #69 --- Dockerfile | 2 +- integration_tests/common.rs | 42 +++++++++++++++++++++++++++++++++++++ integration_tests/main.rs | 4 ++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 90af41ac..ba0e38e9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,7 +30,7 @@ RUN git clone https://github.com/boltlabs-inc/zeekoe.git WORKDIR /root/zeekoe RUN git submodule update --init --recursive -RUN ./dev/generate-certificates; CARGO_NET_GIT_FETCH_WITH_CLI=true cargo build --features "allow_explicit_certificate_trust" +RUN ./dev/generate-certificates; CARGO_NET_GIT_FETCH_WITH_CLI=true cargo build --features "allow_explicit_certificate_trust allow_custom_self_delay" RUN wget https://github.com/serokell/tezos-packaging/releases/latest/download/tezos-client RUN chmod +x tezos-client diff --git a/integration_tests/common.rs b/integration_tests/common.rs index fec1501b..fd0330d2 100644 --- a/integration_tests/common.rs +++ b/integration_tests/common.rs @@ -259,3 +259,45 @@ fn write_config_file(path: &str, contents: String) { .write_all(contents.as_bytes()) .unwrap_or_else(|_| panic!("Failed to write to config file: {}", path)); } + +/// Wrapper type to deserialize blockchain level. +#[derive(Debug, serde::Deserialize)] +struct BlockchainLevel { + level: BlockchainLevelDetail, +} + +/// Inner type used to deserialize blockchain level. +#[derive(Debug, serde::Deserialize)] +struct BlockchainLevelDetail { + level: u64, +} + +/// The minimum required depth to originate contracts on the Tezos blockchain. +static MINIMUM_LEVEL: u64 = 120; + +pub async fn await_leveled_blockchain( + config: &zeekoe::customer::Config, +) -> Result<(), anyhow::Error> { + loop { + let body = reqwest::get(format!( + "{}/chains/main/blocks/head/metadata", + config.tezos_uri + )) + .await? + .text() + .await?; + + let level = serde_json::from_str::(&body)?.level.level; + eprintln!("current: {:?} minimum: {}", level, MINIMUM_LEVEL); + + if level >= MINIMUM_LEVEL { + break; + } + eprintln!(" wait time: {}", (MINIMUM_LEVEL - level) * 2); + tokio::time::sleep(tokio::time::Duration::from_secs( + (MINIMUM_LEVEL - level) * 2, + )) + .await; + } + Ok(()) +} diff --git a/integration_tests/main.rs b/integration_tests/main.rs index 58c80ad0..a0dc5d54 100644 --- a/integration_tests/main.rs +++ b/integration_tests/main.rs @@ -33,6 +33,10 @@ pub async fn main() { .await .expect("Failed to load merchant config"); + common::await_leveled_blockchain(&customer_config) + .await + .expect("Failed to check blockchain level"); + // Run every test, printing out details if it fails let tests = tests(); println!("Executing {} tests", tests.len());