-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jstzd): implement async OctezNode
- Loading branch information
1 parent
97903fe
commit 5bac336
Showing
5 changed files
with
114 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::{fs::File, path::PathBuf, process::Stdio}; | ||
|
||
use tokio::process::{Child, Command}; | ||
|
||
use anyhow::{anyhow, Result}; | ||
|
||
fn path_or_default<'a>(path: Option<&'a PathBuf>, default: &'a str) -> &'a str { | ||
path.and_then(|bin| bin.to_str()).unwrap_or(default) | ||
} | ||
|
||
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()?) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::path_or_default; | ||
use std::path::PathBuf; | ||
|
||
#[test] | ||
fn test_path_or_default() { | ||
let path = PathBuf::from("/foo/bar"); | ||
assert_eq!(path_or_default(Some(&path), "not_this_one"), "/foo/bar"); | ||
assert_eq!( | ||
path_or_default(None, "should_return_this"), | ||
"should_return_this" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod async_octez_node; | ||
pub mod octez_node; | ||
|
||
use anyhow::Result; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters