Skip to content

Commit

Permalink
feat(octez): write baker log to file
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Dec 31, 2024
1 parent 60a8fe0 commit 7d9510d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
13 changes: 9 additions & 4 deletions crates/jstzd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ mod tests {

#[test]
fn populate_baker_config() {
let log_file = NamedTempFile::new().unwrap().into_temp_path();
let tmp_dir = tempdir().unwrap();
let node_config = OctezNodeConfigBuilder::new()
.set_rpc_endpoint(&Endpoint::localhost(5678))
Expand All @@ -421,7 +422,7 @@ mod tests {
.set_base_dir(tmp_dir.path().to_path_buf())
.build()
.unwrap();
let baker_builder = OctezBakerConfigBuilder::new();
let baker_builder = OctezBakerConfigBuilder::new().set_log_file(&log_file);
let protocol_params = ProtocolParameterBuilder::new()
.set_protocol(Protocol::ParisC)
.set_bootstrap_accounts([
Expand All @@ -445,14 +446,17 @@ mod tests {
.set_binary_path(BakerBinaryPath::Env(Protocol::ParisC))
.set_octez_client_base_dir(tmp_dir.path().to_str().unwrap())
.set_octez_node_endpoint(&Endpoint::localhost(5678))
.set_log_file(&log_file)
.build()
.unwrap()
);

// baker path is provided in the config, so the builder takes that path and ignores protocol_params
let baker_builder = OctezBakerConfigBuilder::new().set_binary_path(
BakerBinaryPath::Custom(PathBuf::from_str("/foo/bar").unwrap()),
);
let baker_builder = OctezBakerConfigBuilder::new()
.set_binary_path(BakerBinaryPath::Custom(
PathBuf::from_str("/foo/bar").unwrap(),
))
.set_log_file(&log_file);
let baker_config = super::populate_baker_config(
baker_builder,
&node_config,
Expand All @@ -468,6 +472,7 @@ mod tests {
))
.set_octez_client_base_dir(tmp_dir.path().to_str().unwrap())
.set_octez_node_endpoint(&Endpoint::localhost(5678))
.set_log_file(&log_file)
.build()
.unwrap()
);
Expand Down
6 changes: 1 addition & 5 deletions crates/jstzd/tests/main_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ fn valid_config_file() {
.unwrap()
.args(["run", &tmp_file.path().to_string_lossy()])
.assert()
.success()
// baker log writes to stderr
.stderr(predicate::str::contains(
"block ready for delegate: activator",
));
.success();
});

let client = reqwest::blocking::Client::new();
Expand Down
52 changes: 38 additions & 14 deletions crates/octez/src/async/baker.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::{fmt::Display, path::PathBuf, str::FromStr};
use std::{
fmt::Display,
path::{Path, PathBuf},
process::Stdio,
str::FromStr,
sync::Arc,
};
use tokio::process::{Child, Command};

use super::{endpoint::Endpoint, protocol::Protocol};
use super::{endpoint::Endpoint, file::FileWrapper, protocol::Protocol};

#[derive(PartialEq, Debug, Clone, SerializeDisplay, DeserializeFromStr)]
pub enum BakerBinaryPath {
Expand Down Expand Up @@ -45,13 +51,16 @@ pub struct OctezBakerConfig {
binary_path: BakerBinaryPath,
octez_client_base_dir: PathBuf,
octez_node_endpoint: Endpoint,
log_file: Arc<FileWrapper>,
}

#[derive(Default, Deserialize, Debug, PartialEq)]
pub struct OctezBakerConfigBuilder {
binary_path: Option<BakerBinaryPath>,
octez_client_base_dir: Option<PathBuf>,
octez_node_endpoint: Option<Endpoint>,
/// Path to the log file.
log_file: Option<PathBuf>,
}

impl OctezBakerConfigBuilder {
Expand Down Expand Up @@ -86,6 +95,11 @@ impl OctezBakerConfigBuilder {
&self.octez_node_endpoint
}

pub fn set_log_file(mut self, path: &Path) -> Self {
self.log_file.replace(path.into());
self
}

pub fn build(self) -> Result<OctezBakerConfig> {
Ok(OctezBakerConfig {
binary_path: self.binary_path.ok_or(anyhow!("binary path not set"))?,
Expand All @@ -95,6 +109,10 @@ impl OctezBakerConfigBuilder {
octez_node_endpoint: self
.octez_node_endpoint
.ok_or(anyhow!("octez_node_endpoint not set"))?,
log_file: Arc::new(match self.log_file {
Some(v) => FileWrapper::try_from(v)?,
None => FileWrapper::default(),
}),
})
}
}
Expand All @@ -105,16 +123,19 @@ pub struct OctezBaker;
impl OctezBaker {
pub async fn run(config: OctezBakerConfig) -> Result<Child> {
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",
]);
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",
])
.stdout(Stdio::from(config.log_file.as_file().try_clone()?))
.stderr(Stdio::from(config.log_file.as_file().try_clone()?));
Ok(command.spawn()?)
}
}
Expand All @@ -126,7 +147,7 @@ mod test {
use super::*;
use crate::r#async::endpoint::Endpoint;
use http::Uri;
use tempfile::TempDir;
use tempfile::{NamedTempFile, TempDir};

#[test]
fn test_octez_baker_config_builder() {
Expand Down Expand Up @@ -182,18 +203,21 @@ mod test {
let base_dir = TempDir::new().unwrap();
let endpoint =
Endpoint::try_from(Uri::from_static("http://localhost:8732")).unwrap();
let log_file = NamedTempFile::new().unwrap().into_temp_path();
let config = OctezBakerConfigBuilder::new()
.set_binary_path(BakerBinaryPath::Env(Protocol::Alpha))
.set_octez_client_base_dir(base_dir.path().to_str().unwrap())
.set_octez_node_endpoint(&endpoint)
.set_log_file(log_file.to_path_buf().as_path())
.build()
.unwrap();
assert_eq!(
serde_json::to_value(&config).unwrap(),
serde_json::json!({
"octez_client_base_dir": base_dir.path().to_string_lossy(),
"octez_node_endpoint": "http://localhost:8732",
"binary_path": "octez-baker-alpha"
"binary_path": "octez-baker-alpha",
"log_file": log_file.to_string_lossy()
})
)
}
Expand Down

0 comments on commit 7d9510d

Please sign in to comment.