Skip to content

Commit

Permalink
feat(jstzd): implement async OctezNode
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Sep 27, 2024
1 parent 5098423 commit 2aaf43b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 10 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.

18 changes: 9 additions & 9 deletions crates/jstzd/src/task/octez_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use async_trait::async_trait;
use std::{fs::File, path::PathBuf, sync::Arc};
use tokio::sync::RwLock;

use octez::OctezNode as InnerOctezNode;
use std::process::Child;
use octez::AsyncOctezNode;
use tokio::process::Child;

const DEFAULT_RPC_ENDPOINT: &str = "localhost:8732";
const DEFAULT_NETWORK: &str = "sandbox";
Expand Down Expand Up @@ -116,7 +116,7 @@ struct ChildWrapper {
impl ChildWrapper {
pub async fn kill(&mut self) -> anyhow::Result<()> {
if let Some(mut v) = self.inner.take() {
return Ok(v.kill()?);
v.kill().await?;
}
Ok(())
}
Expand All @@ -140,15 +140,14 @@ impl Task for OctezNode {

/// Spins up the task with the given config.
async fn spawn(config: Self::Config) -> Result<Self> {
let node = InnerOctezNode {
let node = AsyncOctezNode {
octez_node_bin: Some(config.binary_path),
octez_node_dir: config.data_dir,
};

// localhost:8731 refers to the peer http endpoint. This will be removed
// when we switch to the async node implementation where this option is removed
node.config_init(&config.network, "localhost:8731", &config.rpc_endpoint, 0)?;
node.generate_identity()?;
node.generate_identity().await?;
node.config_init(&config.network, &config.rpc_endpoint, 0)
.await?;
Ok(OctezNode {
inner: Arc::new(RwLock::new(AsyncDropper::new(ChildWrapper {
inner: Some(
Expand All @@ -160,7 +159,8 @@ impl Task for OctezNode {
.map(|s| s as &str)
.collect::<Vec<&str>>()
.as_slice(),
)?,
)
.await?,
),
}))),
})
Expand Down
3 changes: 2 additions & 1 deletion crates/octez/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
signal-hook.workspace = true
tezos-smart-rollup-encoding.workspace = true
tezos-smart-rollup-encoding.workspace = true
tokio.workspace = true
83 changes: 83 additions & 0 deletions crates/octez/src/async_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::{fs::File, path::PathBuf, process::Stdio};

use tokio::process::{Child, Command};

use super::path_or_default;
use anyhow::{anyhow, Result};

async fn run_command(command: &mut Command) -> Result<()> {
let output = command.output().await?;

if !output.status.success() {
return Err(anyhow!(
"Command {:?} failed:\n {}",
command,
String::from_utf8_lossy(&output.stderr)
));
}

Ok(())
}

pub struct AsyncOctezNode {
/// Path to the octez-node binary
/// If None, the binary will inside PATH will be used
pub octez_node_bin: Option<PathBuf>,
/// Path to the octez-node directory
pub octez_node_dir: PathBuf,
}

impl AsyncOctezNode {
fn command(&self) -> Command {
Command::new(path_or_default(self.octez_node_bin.as_ref(), "octez-node"))
}

pub async fn config_init(
&self,
network: &str,
rpc_endpoint: &str,
num_connections: u32,
) -> Result<()> {
run_command(self.command().args([
"config",
"init",
"--network",
network,
"--data-dir",
self.octez_node_dir.to_str().expect("Invalid path"),
"--rpc-addr",
rpc_endpoint,
"--connections",
num_connections.to_string().as_str(),
]))
.await
}

pub async fn generate_identity(&self) -> Result<()> {
run_command(self.command().args([
"identity",
"generate",
"0",
"--data-dir",
self.octez_node_dir.to_str().expect("Invalid path"),
]))
.await
}

pub async fn run(&self, log_file: &File, options: &[&str]) -> Result<Child> {
let mut command = self.command();

command
.args([
"run",
"--data-dir",
self.octez_node_dir.to_str().expect("Invalid path"),
"--singleprocess",
])
.args(options)
.stdout(Stdio::from(log_file.try_clone()?))
.stderr(Stdio::from(log_file.try_clone()?));

Ok(command.spawn()?)
}
}
2 changes: 2 additions & 0 deletions crates/octez/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use std::{path::PathBuf, process::Command};

use anyhow::{anyhow, Result};

mod async_node;
mod client;
mod node;
mod rollup;
mod thread;

pub use async_node::*;
pub use client::*;
pub use node::*;
pub use rollup::*;
Expand Down

0 comments on commit 2aaf43b

Please sign in to comment.