From 7d9510d9f97ed945a9fed79fa4a6c1cacd85900e Mon Sep 17 00:00:00 2001 From: Huan-Cheng Chang Date: Tue, 31 Dec 2024 16:38:12 +0000 Subject: [PATCH] feat(octez): write baker log to file --- crates/jstzd/src/config.rs | 13 ++++++--- crates/jstzd/tests/main_test.rs | 6 +--- crates/octez/src/async/baker.rs | 52 ++++++++++++++++++++++++--------- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/crates/jstzd/src/config.rs b/crates/jstzd/src/config.rs index bba058a1b..1914dfdf4 100644 --- a/crates/jstzd/src/config.rs +++ b/crates/jstzd/src/config.rs @@ -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)) @@ -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([ @@ -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, @@ -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() ); diff --git a/crates/jstzd/tests/main_test.rs b/crates/jstzd/tests/main_test.rs index f629c5b1e..3d285fcad 100644 --- a/crates/jstzd/tests/main_test.rs +++ b/crates/jstzd/tests/main_test.rs @@ -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(); diff --git a/crates/octez/src/async/baker.rs b/crates/octez/src/async/baker.rs index eaa0520a6..3f3a0f17f 100644 --- a/crates/octez/src/async/baker.rs +++ b/crates/octez/src/async/baker.rs @@ -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 { @@ -45,6 +51,7 @@ pub struct OctezBakerConfig { binary_path: BakerBinaryPath, octez_client_base_dir: PathBuf, octez_node_endpoint: Endpoint, + log_file: Arc, } #[derive(Default, Deserialize, Debug, PartialEq)] @@ -52,6 +59,8 @@ pub struct OctezBakerConfigBuilder { binary_path: Option, octez_client_base_dir: Option, octez_node_endpoint: Option, + /// Path to the log file. + log_file: Option, } impl OctezBakerConfigBuilder { @@ -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 { Ok(OctezBakerConfig { binary_path: self.binary_path.ok_or(anyhow!("binary path not set"))?, @@ -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(), + }), }) } } @@ -105,16 +123,19 @@ pub struct OctezBaker; impl OctezBaker { pub async fn run(config: OctezBakerConfig) -> Result { 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()?) } } @@ -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() { @@ -182,10 +203,12 @@ 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!( @@ -193,7 +216,8 @@ mod test { 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() }) ) }