From 510278db63b335a7573ea1c02a4efb1065527061 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 15 Nov 2024 10:55:40 +0000 Subject: [PATCH] feat(jstzd): spawn jstz node in jstzd --- crates/jstzd/src/task/jstzd.rs | 22 +++++++++++- crates/jstzd/tests/jstzd_test.rs | 59 +++++++++++++++++++++++++++----- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/crates/jstzd/src/task/jstzd.rs b/crates/jstzd/src/task/jstzd.rs index 1dfc6ce18..48f3b1aaf 100644 --- a/crates/jstzd/src/task/jstzd.rs +++ b/crates/jstzd/src/task/jstzd.rs @@ -1,4 +1,5 @@ use super::{ + jstz_node::JstzNode, octez_baker::OctezBaker, octez_node::OctezNode, octez_rollup::OctezRollup, @@ -9,6 +10,7 @@ use anyhow::{anyhow, Result}; use async_dropper_simple::AsyncDrop; use async_trait::async_trait; use axum::{extract::State, routing::get, Router}; +use jstz_node::config::JstzNodeConfig; use octez::r#async::{ baker::OctezBakerConfig, client::{OctezClient, OctezClientConfig}, @@ -25,6 +27,7 @@ struct Jstzd { octez_node: Arc>, baker: Arc>, rollup: Arc>, + jstz_node: Arc>, } #[derive(Clone)] @@ -33,6 +36,7 @@ pub struct JstzdConfig { baker_config: OctezBakerConfig, octez_client_config: OctezClientConfig, octez_rollup_config: OctezRollupConfig, + jstz_node_config: JstzNodeConfig, protocol_params: ProtocolParameter, } @@ -42,6 +46,7 @@ impl JstzdConfig { baker_config: OctezBakerConfig, octez_client_config: OctezClientConfig, octez_rollup_config: OctezRollupConfig, + jstz_node_config: JstzNodeConfig, protocol_params: ProtocolParameter, ) -> Self { Self { @@ -49,6 +54,7 @@ impl JstzdConfig { baker_config, octez_client_config, octez_rollup_config, + jstz_node_config, protocol_params, } } @@ -62,17 +68,19 @@ impl Task for Jstzd { let octez_node = OctezNode::spawn(config.octez_node_config.clone()).await?; let octez_client = OctezClient::new(config.octez_client_config.clone()); Self::wait_for_node(&octez_node).await?; - Self::import_activator(&octez_client).await; Self::import_rollup_operator(&octez_client).await; Self::activate_protocol(&octez_client, &config.protocol_params).await?; let baker = OctezBaker::spawn(config.baker_config.clone()).await?; Self::wait_for_block_level(&config.octez_node_config.rpc_endpoint, 3).await?; let rollup = OctezRollup::spawn(config.octez_rollup_config.clone()).await?; + let jstz_node = JstzNode::spawn(config.jstz_node_config).await?; + Self::wait_for_jstz_node(&jstz_node).await?; Ok(Self { octez_node: Arc::new(RwLock::new(octez_node)), baker: Arc::new(RwLock::new(baker)), rollup: Arc::new(RwLock::new(rollup)), + jstz_node: Arc::new(RwLock::new(jstz_node)), }) } @@ -81,6 +89,7 @@ impl Task for Jstzd { self.octez_node.write().await.kill(), self.baker.write().await.kill(), self.rollup.write().await.kill(), + self.jstz_node.write().await.kill(), ]) .await; @@ -103,6 +112,7 @@ impl Task for Jstzd { self.octez_node.read().await.health_check(), self.baker.read().await.health_check(), self.rollup.read().await.health_check(), + self.jstz_node.read().await.health_check(), ]) .await; @@ -186,6 +196,16 @@ impl Jstzd { } Ok(()) } + + async fn wait_for_jstz_node(jstz_node: &JstzNode) -> Result<()> { + let ready = retry(10, 1000, || async { jstz_node.health_check().await }).await; + if !ready { + return Err(anyhow::anyhow!( + "jstz node is still not ready after retries" + )); + } + Ok(()) + } } pub struct JstzdServer { diff --git a/crates/jstzd/tests/jstzd_test.rs b/crates/jstzd/tests/jstzd_test.rs index 852bd0d48..8a19d5776 100644 --- a/crates/jstzd/tests/jstzd_test.rs +++ b/crates/jstzd/tests/jstzd_test.rs @@ -1,9 +1,10 @@ mod utils; -use std::path::PathBuf; +use std::io::Read; use std::str::FromStr; use anyhow::Result; use http::Uri; +use jstz_node::config::JstzNodeConfig; use jstzd::task::jstzd::JstzdConfig; use jstzd::task::utils::{get_block_level, retry}; use jstzd::{EXCHANGER_ADDRESS, JSTZ_NATIVE_BRIDGE_ADDRESS, JSTZ_ROLLUP_ADDRESS}; @@ -21,6 +22,8 @@ use serde::Deserialize; use serde_json::from_str; use std::fs; use std::path::Path; +use std::path::PathBuf; +use tempfile::NamedTempFile; use tezos_crypto_rs::hash::SmartRollupHash; const ACTIVATOR_PK: &str = "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2"; @@ -40,11 +43,15 @@ async fn jstzd_test() { Uri::from_str(&format!("http://127.0.0.1:{}", unused_port())).unwrap(), ) .unwrap(); + let jstz_node_rpc_endpoint = Endpoint::localhost(unused_port()); let jstzd_port = unused_port(); - let (jstzd_config, octez_client_config) = - build_configs(&node_rpc_endpoint, &rollup_rpc_endpoint).await; + let (jstzd_config, octez_client_config, kernel_debug_file) = build_configs( + &node_rpc_endpoint, + &rollup_rpc_endpoint, + &jstz_node_rpc_endpoint, + ) + .await; let mut jstzd = jstzd::task::jstzd::JstzdServer::new(jstzd_config, jstzd_port); - // start jstzd jstzd.run().await.unwrap(); @@ -87,8 +94,20 @@ async fn jstzd_test() { .await; assert!(rollup_running); + let jstz_node_is_running = retry(10, 1000, || async { + let res = jstz_node_health_check(&jstz_node_rpc_endpoint.clone()).await; + Ok(res.is_ok_and(|healthy| healthy)) + }) + .await; + assert!(jstz_node_is_running); + assert!(jstzd.health_check().await); + // check if any logs are written to the kernel_debug_file + let kernel_debug_content = read_file(kernel_debug_file).await; + assert!(kernel_debug_content.contains("Internal message: start of level")); + assert!(kernel_debug_content.contains("Internal message: end of level")); + // stop jstzd jstzd.stop().await.unwrap(); let jstzd_stopped = retry(30, 1000, || async { @@ -101,7 +120,7 @@ async fn jstzd_test() { .await; assert!(jstzd_stopped); - // check if the node, baker, rollup are destroyed + // check if the node, baker, rollup and jstz_node are destroyed let node_destroyed = retry(30, 1000, || async { let res = reqwest::get(&octez_node_health_check_endpoint).await; // Should get an error since the node should have been terminated @@ -137,7 +156,8 @@ async fn run_ps() -> String { async fn build_configs( octez_node_rpc_endpoint: &Endpoint, rollup_rpc_endpoint: &Endpoint, -) -> (JstzdConfig, OctezClientConfig) { + jstz_node_rpc_endpoint: &Endpoint, +) -> (JstzdConfig, OctezClientConfig, NamedTempFile) { let run_options = OctezNodeRunOptionsBuilder::new() .set_synchronisation_threshold(0) .set_network("sandbox") @@ -171,8 +191,9 @@ async fn build_configs( .build() .expect("Failed to build baker config"); + let kernel_debug_file = NamedTempFile::new().unwrap(); let rollup_config = OctezRollupConfigBuilder::new( - octez_node_rpc_endpoint.clone(), + octez_node_config.rpc_endpoint.clone(), octez_client_config.base_dir().into(), SmartRollupHash::from_base58_check(JSTZ_ROLLUP_ADDRESS).unwrap(), JSTZ_ROLLUP_OPERATOR_ALIAS.to_string(), @@ -182,18 +203,26 @@ async fn build_configs( preimages_dir: rollup_preimages_dir, }) .set_rpc_endpoint(rollup_rpc_endpoint) + .set_kernel_debug_file(kernel_debug_file.path()) .build() .expect("failed to build rollup config"); - let config = jstzd::task::jstzd::JstzdConfig::new( + let jstz_node_config = JstzNodeConfig::new( + jstz_node_rpc_endpoint, + &rollup_config.rpc_endpoint, + kernel_debug_file.path(), + ); + + let jstzd_config = jstzd::task::jstzd::JstzdConfig::new( octez_node_config, baker_config, octez_client_config.clone(), rollup_config, + jstz_node_config, protocol_params, ); - (config, octez_client_config) + (jstzd_config, octez_client_config, kernel_debug_file) } fn jstz_rollup_files() -> (PathBuf, PathBuf, PathBuf) { @@ -282,3 +311,15 @@ pub async fn rollup_health_check(rollup_rpc_endpoint: &Endpoint) -> Result let body = res.json::().await?; Ok(body.healthy) } + +async fn read_file(file: NamedTempFile) -> String { + let mut file = file.reopen().unwrap(); + let mut contents = String::new(); + file.read_to_string(&mut contents).unwrap(); + contents +} + +pub async fn jstz_node_health_check(jstz_node_rpc_endpoint: &Endpoint) -> Result { + let res = reqwest::get(format!("{}/health", jstz_node_rpc_endpoint)).await?; + Ok(res.status().is_success()) +}