Skip to content

Commit

Permalink
feat(jstzd): add octez client
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Sep 27, 2024
1 parent 6882443 commit e7a3c25
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/jstzd/src/task/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]
use http::{uri::Scheme, Uri};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Endpoint {
scheme: String,
host: String,
Expand Down
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 async_octez_node;
pub mod endpoint;
pub mod octez_client;
pub mod octez_node;

use anyhow::Result;
Expand Down
199 changes: 199 additions & 0 deletions crates/jstzd/src/task/octez_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
use super::{endpoint::Endpoint, octez_node::DEFAULT_RPC_ENDPOINT};
use anyhow::{bail, Result};
use http::Uri;
use std::{path::PathBuf, str::FromStr};
use tempfile::{tempdir, TempDir};

const DEFAULT_BINARY_PATH: &str = "octez-client";
#[derive(Default)]
pub struct OctezClientBuilder {
// if None, use the binary in $PATH
binary_path: Option<PathBuf>,
// if None, use temp directory
base_dir: Option<PathBuf>,
// if None, use localhost:8732 (the default endpoint for octez-node)
endpoint: Option<Endpoint>,
disable_unsafe_disclaimer: bool,
}

impl OctezClientBuilder {
pub fn new() -> Self {
OctezClientBuilder::default()
}
pub fn set_path(mut self, path: PathBuf) -> Self {
self.binary_path = Some(path);
self
}
pub fn set_base_dir(mut self, base_dir: PathBuf) -> Self {
self.base_dir = Some(base_dir);
self
}
pub fn set_endpoint(mut self, endpoint: Endpoint) -> Self {
self.endpoint = Some(endpoint);
self
}
pub fn set_disable_unsafe_disclaimer(
mut self,
disable_unsafe_disclaimer: bool,
) -> Self {
self.disable_unsafe_disclaimer = disable_unsafe_disclaimer;
self
}
pub fn build(self) -> Result<OctezClient> {
self.validate_binary_path()?;
self.validate_base_dir()?;
let node_default_endpoint = format!("http://{}", DEFAULT_RPC_ENDPOINT);
Ok(OctezClient {
binary_path: self.binary_path.unwrap_or(DEFAULT_BINARY_PATH.into()),
base_dir: self
.base_dir
.map_or(Directory::TempDir(tempdir()?), Directory::Path),
endpoint: self
.endpoint
.unwrap_or(Endpoint::try_from(Uri::from_str(&node_default_endpoint)?)?),
disable_unsafe_disclaimer: self.disable_unsafe_disclaimer,
})
}
fn validate_binary_path(&self) -> Result<()> {
if let Some(binary_path) = &self.binary_path {
if !binary_path.exists() {
bail!("Binary path does not exist");
}
if !binary_path.is_file() {
bail!("Binary path is not a file");
}
}
Ok(())
}
fn validate_base_dir(&self) -> Result<()> {
if let Some(base_dir) = &self.base_dir {
if !base_dir.exists() {
bail!("Base directory does not exist");
}
if !base_dir.is_dir() {
bail!("Base directory is not a directory");
}
}
Ok(())
}
}

// TODO: remove later
#[allow(dead_code)]
enum Directory {
TempDir(TempDir),
Path(PathBuf),
}

// TODO: remove later
#[allow(dead_code)]
pub struct OctezClient {
binary_path: PathBuf,
base_dir: Directory,
endpoint: Endpoint,
disable_unsafe_disclaimer: bool,
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn builds_default_octez_client() {
let octez_client = OctezClientBuilder::new().build().unwrap();
assert_eq!(
octez_client.binary_path.to_str().unwrap(),
DEFAULT_BINARY_PATH
);
assert!(matches!(octez_client.base_dir, Directory::TempDir(_)));
assert!(!octez_client.disable_unsafe_disclaimer);
assert_eq!(octez_client.endpoint, Endpoint::localhost(8732))
}

#[test]
fn temp_dir_is_created_by_default() {
let octez_client = OctezClientBuilder::new().build().unwrap();
match octez_client.base_dir {
Directory::TempDir(temp_dir) => assert!(temp_dir.path().exists()),
_ => panic!("Expected TempDir"),
};
}

#[test]
fn temp_dir_is_removed_on_drop() {
let octez_client = OctezClientBuilder::new().build().unwrap();
match &octez_client.base_dir {
Directory::TempDir(temp_dir) => {
let temp_dir_path = temp_dir.path().to_path_buf();
drop(octez_client);
assert!(!temp_dir_path.exists());
}
_ => panic!("Expected TempDir"),
};
}

#[test]
fn sets_custom_binary_and_dir_path() {
let binary_path = PathBuf::from("/tmp/binary1");
let dir_path = PathBuf::from("/tmp/dir1");
let _ = std::fs::remove_dir(&binary_path);
let _ = std::fs::remove_file(&binary_path);
let _ = std::fs::remove_dir(&dir_path);
let _ = std::fs::remove_file(&dir_path);
std::fs::File::create(&binary_path).unwrap();
std::fs::create_dir(&dir_path).unwrap();
let octez_client = OctezClientBuilder::new()
.set_path(binary_path.clone())
.set_base_dir(dir_path.clone())
.build()
.unwrap();
assert_eq!(octez_client.binary_path, binary_path);
match octez_client.base_dir {
Directory::Path(path) => {
assert_eq!(path, dir_path)
}
_ => panic!("Expected Path"),
}
}

#[test]
fn validates_base_dir() {
let dir_path = PathBuf::from("/tmp/dir2");
let _ = std::fs::remove_dir_all(&dir_path);
let _ = std::fs::remove_file(&dir_path);
let octez_client = OctezClientBuilder::new()
.set_base_dir(dir_path.clone())
.build();
assert!(octez_client.is_err());
assert!(octez_client
.is_err_and(|e| &e.to_string() == "Base directory does not exist"));
std::fs::File::create(&dir_path).unwrap();
let octez_client = OctezClientBuilder::new()
.set_base_dir(dir_path.clone())
.build();
assert!(octez_client.is_err());
assert!(octez_client
.is_err_and(|e| &e.to_string() == "Base directory is not a directory"));
}

#[test]
fn validates_binary_path() {
let binary_path = PathBuf::from("/tmp/binary3");
let _ = std::fs::remove_dir_all(&binary_path);
let _ = std::fs::remove_file(&binary_path);
let octez_client = OctezClientBuilder::new()
.set_path(binary_path.clone())
.build();
assert!(octez_client.is_err());
assert!(
octez_client.is_err_and(|e| &e.to_string() == "Binary path does not exist")
);
std::fs::create_dir(&binary_path).unwrap();
let octez_client = OctezClientBuilder::new()
.set_path(binary_path.clone())
.build();
assert!(octez_client.is_err());
assert!(
octez_client.is_err_and(|e| &e.to_string() == "Binary path is not a file")
);
}
}
2 changes: 1 addition & 1 deletion crates/jstzd/src/task/octez_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio::sync::RwLock;
use crate::task::async_octez_node::AsyncOctezNode;
use tokio::process::Child;

const DEFAULT_RPC_ENDPOINT: &str = "localhost:8732";
pub const DEFAULT_RPC_ENDPOINT: &str = "localhost:8732";
const DEFAULT_NETWORK: &str = "sandbox";
const DEFAULT_BINARY_PATH: &str = "octez-node";

Expand Down

0 comments on commit e7a3c25

Please sign in to comment.