Skip to content

Commit

Permalink
refactor(jstzd): move baker/client to octez
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago authored and zcabter committed Oct 24, 2024
1 parent 3202cd9 commit 9118cab
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 243 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ http.workspace = true
octez = { path = "../octez" }
regex.workspace = true
reqwest.workspace = true
rust-embed.workspace = true
serde.workspace = true
serde_json.workspace = true
tempfile.workspace = true
Expand Down
1 change: 0 additions & 1 deletion crates/jstzd/src/jstzd.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/jstzd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pub mod docker;
pub mod jstzd;
pub mod protocol;
pub mod task;

/// The `main` function for running jstzd
Expand Down
2 changes: 0 additions & 2 deletions crates/jstzd/src/task/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
mod child_wrapper;
pub mod directory;
pub mod octez_baker;
pub mod octez_client;
pub mod octez_node;

use anyhow::Result;
Expand Down
195 changes: 3 additions & 192 deletions crates/jstzd/src/task/octez_baker.rs
Original file line number Diff line number Diff line change
@@ -1,107 +1,10 @@
use crate::protocol::Protocol;

use super::{
child_wrapper::{ChildWrapper, SharedChildWrapper},
octez_client::OctezClient,
Task,
};
use anyhow::{anyhow, Result};
use anyhow::Result;
use async_trait::async_trait;
use octez::{Endpoint, OctezNodeConfig};

use std::{fmt::Display, path::PathBuf};
use tokio::process::Command;

#[derive(PartialEq, Debug, Clone)]
pub enum BakerBinaryPath {
BuiltIn(Protocol), // The binary exists in $PATH
Custom(PathBuf), // The binary is at the given path
}

impl Display for BakerBinaryPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BakerBinaryPath::BuiltIn(Protocol::Alpha) => write!(f, "octez-baker-alpha"),
BakerBinaryPath::BuiltIn(Protocol::ParisC) => {
write!(f, "octez-baker-PsParisC")
}
BakerBinaryPath::BuiltIn(Protocol::Quebec) => {
write!(f, "octez-baker-PsQuebec")
}
BakerBinaryPath::Custom(path) => write!(f, "{}", path.to_string_lossy()),
}
}
}

#[allow(dead_code)]
pub struct OctezBakerConfig {
binary_path: BakerBinaryPath,
octez_client_base_dir: PathBuf,
octez_node_data_dir: PathBuf,
octez_node_endpoint: Endpoint,
}

#[derive(Default)]
pub struct OctezBakerConfigBuilder {
binary_path: Option<BakerBinaryPath>,
octez_client_base_dir: Option<PathBuf>,
octez_node_data_dir: Option<PathBuf>,
octez_node_endpoint: Option<Endpoint>,
}

impl OctezBakerConfigBuilder {
pub fn new() -> Self {
OctezBakerConfigBuilder::default()
}

pub fn set_binary_path(mut self, binary_path: BakerBinaryPath) -> Self {
self.binary_path = Some(binary_path);
self
}

pub fn set_octez_client_base_dir(mut self, base_dir: &str) -> Self {
self.octez_client_base_dir = Some(PathBuf::from(base_dir));
self
}

pub fn set_octez_node_data_dir(mut self, data_dir: &str) -> Self {
self.octez_node_data_dir = Some(PathBuf::from(data_dir));
self
}

pub fn set_octez_node_endpoint(mut self, endpoint: &Endpoint) -> Self {
self.octez_node_endpoint = Some(endpoint.clone());
self
}

pub fn with_node_and_client(
mut self,
node_config: &OctezNodeConfig,
client: &OctezClient,
) -> Self {
self.octez_node_data_dir = Some(node_config.data_dir.clone());
let endpoint = &node_config.rpc_endpoint;
self.octez_node_endpoint = Some(endpoint.clone());
self.octez_client_base_dir = Some(PathBuf::try_from(client.base_dir()).unwrap());
self
}

pub fn build(self) -> Result<OctezBakerConfig> {
Ok(OctezBakerConfig {
binary_path: self.binary_path.ok_or(anyhow!("binary path not set"))?,
octez_client_base_dir: self
.octez_client_base_dir
.ok_or(anyhow!("octez_client_base_dir not set"))?,
octez_node_data_dir: self
.octez_node_data_dir
.clone()
.ok_or(anyhow!("octez_node_data_dir not set"))?,
octez_node_endpoint: self
.octez_node_endpoint
.ok_or(anyhow!("octez_node_endpoint not set"))?,
})
}
}
use octez::r#async::baker::{self, OctezBakerConfig};

#[allow(dead_code)]
pub struct OctezBaker {
Expand All @@ -113,18 +16,7 @@ impl Task for OctezBaker {
type Config = OctezBakerConfig;

async fn spawn(config: Self::Config) -> Result<Self> {
let mut command = Command::new(config.binary_path.to_string());
command.args([
"--base-dir",
&config.octez_client_base_dir.to_string_lossy(),
"--endpoint",
&config.octez_node_endpoint.to_string(),
"run",
"remotely",
"--liquidity-baking-toggle-vote",
"pass",
]);
let child = command.spawn()?;
let child = baker::OctezBaker::run(config).await?;
let inner = ChildWrapper::new_shared(child);
Ok(OctezBaker { inner })
}
Expand All @@ -139,84 +31,3 @@ impl Task for OctezBaker {
Ok(lock.inner_mut().is_running().await)
}
}

#[cfg(test)]
mod test {
use http::Uri;
use octez::OctezNodeConfigBuilder;
use tempfile::TempDir;

use crate::task::octez_client::OctezClientBuilder;

use super::*;
#[test]
fn test_octez_baker_config_builder() {
let base_dir = TempDir::new().unwrap();
let data_dir = TempDir::new().unwrap();
let endpoint =
Endpoint::try_from(Uri::from_static("http://localhost:8732")).unwrap();
let config: OctezBakerConfig = OctezBakerConfigBuilder::new()
.set_binary_path(BakerBinaryPath::BuiltIn(Protocol::Alpha))
.set_octez_client_base_dir(base_dir.path().to_str().unwrap())
.set_octez_node_data_dir(data_dir.path().to_str().unwrap())
.set_octez_node_endpoint(&endpoint)
.build()
.unwrap();
assert_eq!(
config.binary_path,
BakerBinaryPath::BuiltIn(Protocol::Alpha)
);
assert_eq!(config.octez_client_base_dir, base_dir.path());
assert_eq!(config.octez_node_data_dir, data_dir.path());
assert_eq!(config.octez_node_endpoint, endpoint);
}

#[test]
fn octez_baker_config_builder_fails_without_binary_path() {
let base_dir = TempDir::new().unwrap();
let data_dir = TempDir::new().unwrap();
let endpoint =
Endpoint::try_from(Uri::from_static("http://localhost:8732")).unwrap();
let config: Result<OctezBakerConfig> = OctezBakerConfigBuilder::new()
.set_octez_client_base_dir(base_dir.path().to_str().unwrap())
.set_octez_node_data_dir(data_dir.path().to_str().unwrap())
.set_octez_node_endpoint(&endpoint)
.build();
assert!(config.is_err_and(|e| e.to_string().contains("binary path not set")));
}

#[tokio::test]
async fn test_with_node_config_and_client() {
let node_endpoint =
Endpoint::try_from(Uri::from_static("http://localhost:8732")).unwrap();
let temp_dir = TempDir::new().unwrap();
let data_dir: &std::path::Path = temp_dir.path();
let node_config = OctezNodeConfigBuilder::new()
.set_binary_path("octez-node")
.set_network("sandbox")
.set_rpc_endpoint(&node_endpoint)
.set_data_dir(data_dir.to_str().unwrap())
.build()
.expect("Failed to build node config");

let temp_dir = TempDir::new().unwrap();
let base_dir: std::path::PathBuf = temp_dir.path().to_path_buf();
let octez_client = OctezClientBuilder::new()
.set_endpoint(node_endpoint.clone())
.set_base_dir(base_dir.clone())
.build()
.expect("Failed to build octez client");
let config: OctezBakerConfig = OctezBakerConfigBuilder::new()
.set_binary_path(BakerBinaryPath::BuiltIn(Protocol::Alpha))
.with_node_and_client(&node_config, &octez_client)
.build()
.unwrap();
assert_eq!(
config.binary_path,
BakerBinaryPath::BuiltIn(Protocol::Alpha)
);
assert_eq!(config.octez_client_base_dir, base_dir);
assert_eq!(config.octez_node_data_dir, data_dir);
assert_eq!(config.octez_node_endpoint, node_endpoint);
}
}
8 changes: 4 additions & 4 deletions crates/jstzd/src/task/octez_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use super::child_wrapper::{ChildWrapper, SharedChildWrapper};
use super::Task;
use anyhow::Result;
use async_trait::async_trait;
use octez::{Endpoint, OctezNodeConfig};
use octez::r#async::endpoint::Endpoint;
use octez::r#async::node;
use octez::r#async::node_config::OctezNodeConfig;
use std::fs::File;

use octez::AsyncOctezNode;

#[derive(Default, Clone)]
pub struct OctezNode {
inner: SharedChildWrapper,
Expand All @@ -29,7 +29,7 @@ impl Task for OctezNode {

/// Spins up the task with the given config.
async fn spawn(config: Self::Config) -> Result<Self> {
let node = AsyncOctezNode {
let node = node::OctezNode {
octez_node_bin: Some(config.binary_path.clone()),
octez_node_dir: config.data_dir.clone(),
};
Expand Down
15 changes: 7 additions & 8 deletions crates/jstzd/tests/octez_baker_test.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::path::Path;

use jstzd::{
use jstzd::task::{octez_baker::OctezBaker, octez_node, Task};

use octez::r#async::{
baker::{BakerBinaryPath, OctezBakerConfigBuilder},
client::{OctezClient, OctezClientBuilder},
node_config::{OctezNodeConfigBuilder, OctezNodeRunOptionsBuilder},
protocol::Protocol,
task::{
octez_baker::{BakerBinaryPath, OctezBaker, OctezBakerConfigBuilder},
octez_client::{OctezClient, OctezClientBuilder},
octez_node, Task,
},
};
use octez::{OctezNodeConfigBuilder, OctezNodeRunOptionsBuilder};
use regex::Regex;
use tempfile::TempDir;
mod utils;
Expand All @@ -32,7 +31,7 @@ async fn test_baker() {
import_bootstrap_keys(&octez_client).await;
// 4. start baker
let baker_config = OctezBakerConfigBuilder::new()
.set_binary_path(BakerBinaryPath::BuiltIn(Protocol::Alpha))
.set_binary_path(BakerBinaryPath::Env(Protocol::Alpha))
.with_node_and_client(octez_node.config(), &octez_client)
.build()
.expect("Failed to build baker config");
Expand Down
18 changes: 10 additions & 8 deletions crates/jstzd/tests/octez_client_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use jstz_crypto::public_key_hash::PublicKeyHash;
use jstzd::task::{octez_client::OctezClientBuilder, octez_node, Task};
use octez::Endpoint;
use jstzd::task::{octez_node, Task};
use octez::r#async::{
client::{OctezClientBuilder, Signature},
endpoint::Endpoint,
node_config::{OctezNodeConfigBuilder, OctezNodeRunOptionsBuilder},
};
use serde_json::Value;
use std::{
fs::{read_to_string, remove_file},
Expand All @@ -26,7 +30,7 @@ const SECRET_KEY: &str =
async fn config_init() {
let temp_dir = TempDir::new().unwrap();
let expected_base_dir = temp_dir.path().to_path_buf();
let expected_endpoint: Endpoint = Endpoint::localhost(3000);
let expected_endpoint = Endpoint::localhost(3000);
let config_file = NamedTempFile::new().unwrap();
let _ = remove_file(config_file.path());
let octez_client = OctezClientBuilder::new()
Expand Down Expand Up @@ -122,9 +126,7 @@ async fn generates_keys_with_custom_signature() {
.build()
.unwrap();
let alias = "test_alias".to_string();
let res = octez_client
.gen_keys(&alias, Some(jstzd::task::octez_client::Signature::BLS))
.await;
let res = octez_client.gen_keys(&alias, Some(Signature::BLS)).await;
assert!(res.is_ok());
let hashes = first_item(read_file(&base_dir.join("public_key_hashs")));
let pub_keys = first_item(read_file(&base_dir.join("public_keys")));
Expand Down Expand Up @@ -292,8 +294,8 @@ async fn activate_protocol() {
async fn spawn_octez_node() -> (octez_node::OctezNode, TempDir) {
let temp_dir = TempDir::new().unwrap();
let data_dir = temp_dir.path();
let mut config_builder = octez::OctezNodeConfigBuilder::new();
let mut run_option_builder = octez::OctezNodeRunOptionsBuilder::new();
let mut config_builder = OctezNodeConfigBuilder::new();
let mut run_option_builder = OctezNodeRunOptionsBuilder::new();
config_builder
.set_binary_path("octez-node")
.set_network("sandbox")
Expand Down
12 changes: 9 additions & 3 deletions crates/jstzd/tests/octez_node_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use jstzd::task::{octez_node, Task};
mod utils;
use octez::{unused_port, Endpoint};
use octez::{
r#async::{
endpoint::Endpoint,
node_config::{OctezNodeConfigBuilder, OctezNodeRunOptionsBuilder},
},
unused_port,
};
use utils::retry;

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -9,12 +15,12 @@ async fn octez_node_test() {
let log_file = tempfile::NamedTempFile::new().unwrap();
let rpc_endpoint = Endpoint::localhost(unused_port());

let mut run_option_builder = octez::OctezNodeRunOptionsBuilder::new();
let mut run_option_builder = OctezNodeRunOptionsBuilder::new();
let run_options = run_option_builder
.set_synchronisation_threshold(0)
.set_network("sandbox")
.build();
let mut config_builer = octez::OctezNodeConfigBuilder::new();
let mut config_builer = OctezNodeConfigBuilder::new();
config_builer
.set_binary_path("octez-node")
.set_data_dir(data_dir.path().to_str().unwrap())
Expand Down
2 changes: 2 additions & 0 deletions crates/octez/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ description = "Bindings to Octez (Tezos) binaries"
anyhow.workspace = true
hex.workspace = true
http.workspace = true
jstz_crypto = {path = "../jstz_crypto"}
regex.workspace = true
reqwest.workspace = true
rust-embed.workspace = true
serde.workspace = true
serde_json.workspace = true
signal-hook.workspace = true
Expand Down
Loading

0 comments on commit 9118cab

Please sign in to comment.