Skip to content

Commit

Permalink
feat(jstzd,jstz_node): implement jstz node
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Nov 12, 2024
1 parent 200f77f commit e40bb87
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 3 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.

27 changes: 27 additions & 0 deletions crates/jstz_node/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::path::{Path, PathBuf};

use octez::r#async::endpoint::Endpoint;

#[derive(Clone)]
pub struct JstzNodeConfig {
/// The endpoint of the jstz node.
pub endpoint: Endpoint,
/// Rollup endpoint.
pub rollup_endpoint: Endpoint,
/// The path to the rollup kernel log file.
pub kernel_log_file: PathBuf,
}

impl JstzNodeConfig {
pub fn new(
endpoint: &Endpoint,
rollup_endpoint: &Endpoint,
kernel_log_file: &Path,
) -> Self {
Self {
endpoint: endpoint.clone(),
rollup_endpoint: rollup_endpoint.clone(),
kernel_log_file: kernel_log_file.to_path_buf(),
}
}
}
23 changes: 20 additions & 3 deletions crates/jstz_node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::{path::PathBuf, sync::Arc};

use anyhow::Result;
use api_doc::ApiDoc;
use axum::{http, routing::get};
use config::JstzNodeConfig;
use octez::OctezRollupClient;
use services::{
accounts::AccountsService,
logs::{broadcaster::Broadcaster, db::Db, LogsService},
operations::OperationsService,
};
use std::{path::PathBuf, sync::Arc};
use tokio::net::TcpListener;
use tower_http::cors::{Any, CorsLayer};

Expand All @@ -18,6 +20,7 @@ use tokio_util::sync::CancellationToken;
use utoipa::OpenApi;
use utoipa_axum::router::OpenApiRouter;
use utoipa_scalar::{Scalar, Servable};
pub mod config;

#[derive(Clone)]
pub struct AppState {
Expand All @@ -26,12 +29,25 @@ pub struct AppState {
pub db: Db,
}

pub async fn run_with_config(config: JstzNodeConfig) -> Result<()> {
let endpoint_addr = config.endpoint.host();
let endpoint_port = config.endpoint.port();
let rollup_endpoint = config.rollup_endpoint.to_string();
run(
endpoint_addr,
endpoint_port,
rollup_endpoint,
config.kernel_log_file.to_path_buf(),
)
.await
}

pub async fn run(
addr: &str,
port: u16,
rollup_endpoint: String,
kernel_log_path: PathBuf,
) -> anyhow::Result<()> {
) -> Result<()> {
let rollup_client = OctezRollupClient::new(rollup_endpoint.to_string());

let cancellation_token = CancellationToken::new();
Expand Down Expand Up @@ -66,6 +82,7 @@ fn router() -> OpenApiRouter<AppState> {
.merge(OperationsService::router_with_openapi())
.merge(AccountsService::router_with_openapi())
.merge(LogsService::router_with_openapi())
.route("/health", get(http::StatusCode::OK))
}

pub fn openapi_json_raw() -> anyhow::Result<String> {
Expand Down
1 change: 1 addition & 0 deletions crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ futures.workspace = true
futures-util.workspace = true
http.workspace = true
jstz_crypto = { path = "../jstz_crypto" }
jstz_node = {path = "../jstz_node"}
octez = { path = "../octez" }
regex.workspace = true
reqwest.workspace = true
Expand Down
31 changes: 31 additions & 0 deletions crates/jstzd/src/task/jstz_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use super::Task;
use anyhow::Result;
use async_trait::async_trait;
use jstz_node::{config::JstzNodeConfig, run_with_config};
use tokio::task::JoinHandle;

pub struct JstzNode {
handle: JoinHandle<Result<()>>,
config: JstzNodeConfig,
}

#[async_trait]
impl Task for JstzNode {
type Config = JstzNodeConfig;

async fn spawn(config: Self::Config) -> Result<Self> {
let cfg = config.clone();
let handle = tokio::spawn(async move { run_with_config(cfg).await });
Ok(JstzNode { handle, config })
}

async fn kill(&mut self) -> Result<()> {
self.handle.abort();
Ok(())
}

async fn health_check(&self) -> Result<bool> {
let res = reqwest::get(format!("{}/health", self.config.endpoint)).await;
Ok(res.is_ok_and(|res| res.status().is_success()))
}
}
1 change: 1 addition & 0 deletions crates/jstzd/src/task/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod child_wrapper;
mod file;
pub mod jstz_node;
pub mod jstzd;
pub mod octez_baker;
pub mod octez_node;
Expand Down
22 changes: 22 additions & 0 deletions crates/jstzd/tests/jstz_node_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use jstz_node::config::JstzNodeConfig;
use jstzd::task::{utils::retry, Task};
use octez::{r#async::endpoint::Endpoint, unused_port};
use tempfile::NamedTempFile;

#[tokio::test(flavor = "multi_thread")]
async fn jstz_node_test() {
let endpoint = Endpoint::localhost(unused_port());
let mock_rollup_endpoint = Endpoint::localhost(unused_port());
let tempfile = NamedTempFile::new().unwrap();
let jstz_node_config =
JstzNodeConfig::new(&endpoint, &mock_rollup_endpoint, tempfile.path());
let mut jstz_node = jstzd::task::jstz_node::JstzNode::spawn(jstz_node_config)
.await
.unwrap();
let jstz_node_ready =
retry(10, 1000, || async { jstz_node.health_check().await }).await;
assert!(jstz_node_ready);
jstz_node.kill().await.unwrap();
let is_alive = jstz_node.health_check().await.unwrap();
assert!(!is_alive);
}

0 comments on commit e40bb87

Please sign in to comment.