Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leounoki jstz 165/spawn jstz node #667

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion crates/jstzd/src/task/jstzd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{
jstz_node::JstzNode,
octez_baker::OctezBaker,
octez_node::OctezNode,
octez_rollup::OctezRollup,
Expand All @@ -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},
Expand All @@ -25,6 +27,7 @@ struct Jstzd {
octez_node: Arc<RwLock<OctezNode>>,
baker: Arc<RwLock<OctezBaker>>,
rollup: Arc<RwLock<OctezRollup>>,
jstz_node: Arc<RwLock<JstzNode>>,
}

#[derive(Clone)]
Expand All @@ -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,
}

Expand All @@ -42,13 +46,15 @@ impl JstzdConfig {
baker_config: OctezBakerConfig,
octez_client_config: OctezClientConfig,
octez_rollup_config: OctezRollupConfig,
jstz_node_config: JstzNodeConfig,
protocol_params: ProtocolParameter,
) -> Self {
Self {
octez_node_config,
baker_config,
octez_client_config,
octez_rollup_config,
jstz_node_config,
protocol_params,
}
}
Expand All @@ -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)),
})
}

Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
59 changes: 50 additions & 9 deletions crates/jstzd/tests/jstzd_test.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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";
Expand All @@ -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();

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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(),
Expand All @@ -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) {
Expand Down Expand Up @@ -282,3 +311,15 @@ pub async fn rollup_health_check(rollup_rpc_endpoint: &Endpoint) -> Result<bool>
let body = res.json::<HealthCheckResponse>().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<bool> {
let res = reqwest::get(format!("{}/health", jstz_node_rpc_endpoint)).await?;
Ok(res.status().is_success())
}
Loading