From eaf5f8bf28f9f7b90096001d33c424c83f00d0c6 Mon Sep 17 00:00:00 2001 From: imabdulbasit Date: Wed, 22 May 2024 08:22:56 +0500 Subject: [PATCH] fix test --- sequencer/src/bin/espresso-dev-node.rs | 118 +++++++++++-------------- 1 file changed, 52 insertions(+), 66 deletions(-) diff --git a/sequencer/src/bin/espresso-dev-node.rs b/sequencer/src/bin/espresso-dev-node.rs index ccfac88da4..8b0c55c590 100644 --- a/sequencer/src/bin/espresso-dev-node.rs +++ b/sequencer/src/bin/espresso-dev-node.rs @@ -52,12 +52,7 @@ struct Args { /// If provided, the service will run a basic HTTP server on the given port. /// /// The server provides healthcheck and version endpoints. - #[clap( - short, - long, - env = "ESPRESSO_COMMITMENT_TASK_PORT", - default_value = "8772" - )] + #[clap(short, long, env = "ESPRESSO_COMMITMENT_TASK_PORT")] commitment_task_port: u16, #[clap(flatten)] @@ -164,13 +159,13 @@ fn start_commitment_server( app.register_module("api", api) .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; - spawn(app.serve(format!("0.0.0.0:{port}"), bind_version)); + spawn(app.serve(format!("localhost:{port}"), bind_version)); Ok(()) } #[cfg(test)] mod tests { - use std::time::Duration; + use std::{process::Child, time::Duration}; use async_compatibility_layer::{ art::async_sleep, @@ -181,14 +176,24 @@ mod tests { use es_version::SequencerVersion; use escargot::CargoBuild; use futures::TryStreamExt; - use hotshot_query_service::{availability::BlockQueryData, data_source::sql::testing::TmpDb}; + use hotshot_query_service::{ + availability::{BlockQueryData, TransactionQueryData}, + data_source::sql::testing::TmpDb, + }; + use jf_merkle_tree::MerkleTreeScheme; use portpicker::pick_unused_port; - use reqwest::StatusCode; - use sequencer::{SeqTypes, Transaction}; - use sequencer_utils::AnvilOptions; + use sequencer::{state::BlockMerkleTree, Header, SeqTypes, Transaction}; use surf_disco::Client; use tide_disco::error::ServerError; + pub struct BackgroundProcess(Child); + + impl Drop for BackgroundProcess { + fn drop(&mut self) { + self.0.kill().unwrap(); + } + } + // If this test failed and you are doing changes on the following stuff, please // sync your changes to [`espresso-sequencer-go`](https://github.com/EspressoSystems/espresso-sequencer-go) // and open a PR. @@ -198,7 +203,7 @@ mod tests { async fn dev_node_test() { setup_logging(); setup_backtrace(); - let anvil = AnvilOptions::default().spawn().await; + let commitment_task_port = pick_unused_port().unwrap(); let api_port = pick_unused_port().unwrap(); @@ -206,14 +211,14 @@ mod tests { let db = TmpDb::init().await; let postgres_port = db.port(); - let mut child_process = CargoBuild::new() + // TODO: drop the child process if test fails + let process = CargoBuild::new() .bin("espresso-dev-node") .features("testing") .current_target() .run() .unwrap() .command() - .env("ESPRESSO_SEQUENCER_L1_PROVIDER", anvil.url().to_string()) .env( "ESPRESSO_COMMITMENT_TASK_PORT", commitment_task_port.to_string(), @@ -229,38 +234,36 @@ mod tests { .spawn() .unwrap(); - let commitment_task_url = format!( - "http://localhost:{}/api/hotshot_contract", - commitment_task_port - ); - println!("commitment task url: {}", commitment_task_url); - - let client = reqwest::Client::new(); + let _process = BackgroundProcess(process); let api_client: Client = Client::new(format!("http://localhost:{api_port}").parse().unwrap()); api_client.connect(None).await; - // Wait until some blocks have been decided. tracing::info!("waiting for blocks"); let _ = api_client .socket("availability/stream/blocks/0") .subscribe::>() .await .unwrap() - .take(4) + .take(10) .try_collect::>() .await .unwrap(); - let hotshot_contract = client - .get(commitment_task_url) + let commitment_api_client: Client = Client::new( + format!("http://localhost:{commitment_task_port}/api") + .parse() + .unwrap(), + ); + commitment_api_client.connect(None).await; + + let hotshot_contract = commitment_api_client + .get::("hotshot_contract") .send() .await - .unwrap() - .text() - .await .unwrap(); + assert!(!hotshot_contract.is_empty()); let tx = Transaction::new(100.into(), vec![1, 2, 3]); @@ -277,51 +280,34 @@ mod tests { assert_eq!(hash, tx_hash); async_sleep(Duration::from_secs(5)).await; - let resp = client - .get(format!( - "http://localhost:{}/availability/transaction/hash/{}", - api_port, tx_hash - )) + let _: TransactionQueryData = api_client + .get(&format!("availability/transaction/hash/{}", tx_hash)) .send() .await - .unwrap() - .status(); - assert_eq!(resp, StatusCode::OK); + .unwrap(); // These endpoints are currently used in `espresso-sequencer-go`. These checks // serve as reminders of syncing the API updates to go client repo when they change. { - api_get_test( - &client, - format!("http://localhost:{}/status/block-height", api_port), - ) - .await; - api_get_test( - &client, - format!("http://localhost:{}/availability/header/1/3", api_port), - ) - .await; - api_get_test( - &client, - format!( - "http://localhost:{}/availability/block/3/namespace/100", - api_port - ), - ) - .await; - api_get_test( - &client, - format!("http://localhost:{}/block-state/2/3", api_port), - ) - .await + api_client + .get::("status/block-height") + .send() + .await + .unwrap(); + + api_client + .get::
("availability/header/3") + .send() + .await + .unwrap(); + + api_client + .get::<::MembershipProof>("block-state/3/2") + .send() + .await + .unwrap(); } - child_process.kill().unwrap(); drop(db); } - - async fn api_get_test(client: &reqwest::Client, url: String) { - let resp_status = client.get(url).send().await.unwrap().status(); - assert_eq!(resp_status, StatusCode::OK); - } }