Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jstz): use file wrapper for kernel debug log in config #735

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions crates/jstzd/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use octez::unused_port;
use rust_embed::Embed;
use std::path::Path;

use crate::task::jstzd::JstzdConfig;
use crate::{
Expand All @@ -15,6 +14,7 @@ use octez::r#async::protocol::{
use octez::r#async::{
baker::{BakerBinaryPath, OctezBakerConfig, OctezBakerConfigBuilder},
client::{OctezClientConfig, OctezClientConfigBuilder},
file::FileWrapper,
node_config::{OctezNodeConfig, OctezNodeConfigBuilder},
protocol::{BootstrapAccount, ProtocolParameterBuilder},
rollup::{OctezRollupConfigBuilder, RollupDataDir},
Expand All @@ -24,7 +24,6 @@ use tezos_crypto_rs::hash::SmartRollupHash;
use tokio::io::AsyncReadExt;

const DEFAULT_JSTZD_SERVER_PORT: u16 = 55555;
const KERNEL_DEBUG_FILE: &str = "kernel.log";
const ACTIVATOR_PK: &str = "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2";
pub const BOOTSTRAP_CONTRACT_NAMES: [(&str, &str); 2] = [
("exchanger", EXCHANGER_ADDRESS),
Expand Down Expand Up @@ -93,8 +92,9 @@ pub(crate) async fn build_config(
&protocol_params,
)?;

let kernel_debug_file = Path::new(KERNEL_DEBUG_FILE);
let octez_node_endpoint = octez_node_config.rpc_endpoint.clone();
let kernel_debug_file = FileWrapper::default();
let kernel_debug_file_path = kernel_debug_file.path();
let octez_rollup_config = OctezRollupConfigBuilder::new(
octez_node_endpoint,
octez_client_config.base_dir().into(),
Expand All @@ -113,7 +113,7 @@ pub(crate) async fn build_config(
let jstz_node_config = JstzNodeConfig::new(
&jstz_node_rpc_endpoint,
&octez_rollup_config.rpc_endpoint,
kernel_debug_file,
&kernel_debug_file_path,
);

let server_port = config.server_port.unwrap_or(DEFAULT_JSTZD_SERVER_PORT);
Expand Down Expand Up @@ -236,7 +236,7 @@ mod tests {
use tezos_crypto_rs::hash::ContractKt1Hash;
use tokio::io::AsyncReadExt;

use super::{jstz_rollup_path, ACTIVATOR_PK, JSTZ_ROLLUP_ADDRESS, KERNEL_DEBUG_FILE};
use super::{jstz_rollup_path, ACTIVATOR_PK, JSTZ_ROLLUP_ADDRESS};

use super::Config;

Expand Down Expand Up @@ -546,16 +546,6 @@ mod tests {
config.octez_rollup_config().address.to_base58_check(),
JSTZ_ROLLUP_ADDRESS
);
assert_eq!(
config
.octez_rollup_config()
.kernel_debug_file
.as_ref()
.unwrap()
.to_str()
.unwrap(),
KERNEL_DEBUG_FILE,
);
assert_eq!(
config.octez_rollup_config().data_dir,
RollupDataDir::TempWithPreImages {
Expand All @@ -566,10 +556,6 @@ mod tests {
config.octez_rollup_config().boot_sector_file,
jstz_rollup_path::kernel_installer_path()
);
assert_eq!(
config.jstz_node_config().kernel_log_file.to_str().unwrap(),
KERNEL_DEBUG_FILE
);

assert_eq!(
config.jstz_node_config().rollup_endpoint,
Expand Down
18 changes: 12 additions & 6 deletions crates/jstzd/src/task/octez_rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,18 @@ impl Task for OctezRollup {
&config.rpc_endpoint,
&config.log_file,
);
let inner = ChildWrapper::new_shared(rollup.run(
&config.address,
&config.operator,
Some(&config.boot_sector_file),
config.kernel_debug_file.as_deref(),
)?);
let inner = ChildWrapper::new_shared(
rollup.run(
&config.address,
&config.operator,
Some(&config.boot_sector_file),
config
.kernel_debug_file
.as_ref()
.map(|v| v.path())
.as_deref(),
)?,
);
Ok(Self {
inner,
config,
Expand Down
31 changes: 19 additions & 12 deletions crates/jstzd/tests/jstzd_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use jstzd::{BOOTSTRAP_CONTRACT_NAMES, JSTZ_ROLLUP_ADDRESS};
use octez::r#async::baker::{BakerBinaryPath, OctezBakerConfigBuilder};
use octez::r#async::client::{OctezClient, OctezClientConfigBuilder};
use octez::r#async::endpoint::Endpoint;
use octez::r#async::file::FileWrapper;
use octez::r#async::node_config::{OctezNodeConfigBuilder, OctezNodeRunOptionsBuilder};
use octez::r#async::protocol::{
BootstrapAccount, BootstrapContract, BootstrapSmartRollup, ProtocolParameterBuilder,
Expand Down Expand Up @@ -43,7 +44,7 @@ async fn jstzd_test() {
.unwrap();
let jstz_node_rpc_endpoint = Endpoint::localhost(unused_port());
let jstzd_port = unused_port();
let (mut jstzd, config, kernel_debug_file) = create_jstzd_server(
let (mut jstzd, config) = create_jstzd_server(
&octez_node_rpc_endpoint,
&rollup_rpc_endpoint,
&jstz_node_rpc_endpoint,
Expand All @@ -54,7 +55,16 @@ async fn jstzd_test() {
jstzd.run(false).await.unwrap();
ensure_jstzd_components_are_up(&jstzd, &octez_node_rpc_endpoint, jstzd_port).await;

ensure_rollup_is_logging_to(kernel_debug_file).await;
match config
.octez_rollup_config()
.kernel_debug_file
.as_ref()
.unwrap()
.as_ref()
{
FileWrapper::TempFile(v) => ensure_rollup_is_logging_to(v).await,
_ => panic!("kernel_debug_file should be a temp file"),
}

let octez_client = OctezClient::new(config.octez_client_config().clone());
check_bootstrap_contracts(&octez_client).await;
Expand Down Expand Up @@ -88,7 +98,7 @@ async fn create_jstzd_server(
rollup_rpc_endpoint: &Endpoint,
jstz_node_rpc_endpoint: &Endpoint,
jstzd_port: u16,
) -> (JstzdServer, JstzdConfig, NamedTempFile) {
) -> (JstzdServer, JstzdConfig) {
let run_options = OctezNodeRunOptionsBuilder::new()
.set_synchronisation_threshold(0)
.set_network("sandbox")
Expand Down Expand Up @@ -140,7 +150,8 @@ async fn create_jstzd_server(
.set_octez_node_endpoint(&octez_node_config.rpc_endpoint)
.build()
.expect("Failed to build baker config");
let kernel_debug_file = NamedTempFile::new().unwrap();
let kernel_debug_file = FileWrapper::default();
let kernel_debug_file_path = kernel_debug_file.path();
let rollup_config = OctezRollupConfigBuilder::new(
octez_node_rpc_endpoint.clone(),
octez_client_config.base_dir().into(),
Expand All @@ -152,13 +163,13 @@ async fn create_jstzd_server(
preimages_dir: rollup_preimages_dir,
})
.set_rpc_endpoint(rollup_rpc_endpoint)
.set_kernel_debug_file(kernel_debug_file.path())
.set_kernel_debug_file(kernel_debug_file)
.build()
.expect("failed to build rollup config");
let jstz_node_config = JstzNodeConfig::new(
jstz_node_rpc_endpoint,
&rollup_config.rpc_endpoint,
kernel_debug_file.path(),
&kernel_debug_file_path,
);
let config = JstzdConfig::new(
octez_node_config,
Expand All @@ -168,11 +179,7 @@ async fn create_jstzd_server(
jstz_node_config,
protocol_params,
);
(
JstzdServer::new(config.clone(), jstzd_port),
config,
kernel_debug_file,
)
(JstzdServer::new(config.clone(), jstzd_port), config)
}

async fn ensure_jstzd_components_are_up(
Expand Down Expand Up @@ -344,7 +351,7 @@ async fn check_bootstrap_contracts(octez_client: &OctezClient) {
);
}
}
async fn ensure_rollup_is_logging_to(kernel_debug_file: NamedTempFile) {
async fn ensure_rollup_is_logging_to(kernel_debug_file: &NamedTempFile) {
let mut file = kernel_debug_file.reopen().unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
Expand Down
27 changes: 17 additions & 10 deletions crates/octez/src/async/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ pub struct OctezRollupConfigBuilder {
/// if None, use localhost with random port
pub rpc_endpoint: Option<Endpoint>,
/// The path to the kernel debug file
kernel_debug_file: Option<PathBuf>,
#[serde(skip_deserializing)]
kernel_debug_file: Option<FileWrapper>,
/// Path to the log file.
log_file: Option<PathBuf>,
}
Expand Down Expand Up @@ -99,8 +100,8 @@ impl OctezRollupConfigBuilder {
self
}

pub fn set_kernel_debug_file(mut self, kernel_debug_file: &Path) -> Self {
self.kernel_debug_file = Some(kernel_debug_file.to_path_buf());
pub fn set_kernel_debug_file(mut self, kernel_debug_file: FileWrapper) -> Self {
self.kernel_debug_file.replace(kernel_debug_file);
self
}

Expand All @@ -125,7 +126,7 @@ impl OctezRollupConfigBuilder {
let uri = Uri::from_str(&format!("127.0.0.1:{}", unused_port())).unwrap();
Endpoint::try_from(uri).unwrap()
}),
kernel_debug_file: self.kernel_debug_file,
kernel_debug_file: self.kernel_debug_file.map(Arc::new),
log_file: Arc::new(match self.log_file {
Some(v) => FileWrapper::try_from(v)?,
None => FileWrapper::default(),
Expand All @@ -147,7 +148,7 @@ pub struct OctezRollupConfig {
pub boot_sector_file: PathBuf,
pub rpc_endpoint: Endpoint,
pub pvm_kind: SmartRollupPvmKind,
pub kernel_debug_file: Option<PathBuf>,
pub kernel_debug_file: Option<Arc<FileWrapper>>,
pub log_file: Arc<FileWrapper>,
}

Expand Down Expand Up @@ -237,17 +238,21 @@ impl OctezRollup {

#[cfg(test)]
mod test {
use tempfile::NamedTempFile;

use super::*;
#[test]
fn builds_rollup_config() {
let kernel_debug_file = NamedTempFile::new().unwrap();
let kernel_debug_file_path = kernel_debug_file.path().to_path_buf();
let rollup_config = OctezRollupConfigBuilder::new(
Endpoint::localhost(1234),
PathBuf::from("/base_dir"),
SmartRollupHash::from_str("sr1PuFMgaRUN12rKQ3J2ae5psNtwCxPNmGNK").unwrap(),
"operator".to_owned(),
PathBuf::from("/tmp/boot_sector.hex"),
)
.set_kernel_debug_file(Path::new("/tmp/kernel_debug.log"))
.set_kernel_debug_file(FileWrapper::TempFile(kernel_debug_file))
.build()
.unwrap();
assert_eq!(rollup_config.pvm_kind, SmartRollupPvmKind::Wasm);
Expand Down Expand Up @@ -277,13 +282,15 @@ mod test {
format!("http://127.0.0.1:{}", port)
);
assert_eq!(
rollup_config.kernel_debug_file,
Some(PathBuf::from("/tmp/kernel_debug.log"))
rollup_config.kernel_debug_file.map(|v| v.path()),
Some(kernel_debug_file_path)
);
}

#[test]
fn serialize_config() {
let kernel_debug_file = NamedTempFile::new().unwrap();
let kernel_debug_file_path = kernel_debug_file.path().to_path_buf();
let log_file = tempfile::NamedTempFile::new().unwrap().into_temp_path();
let config = OctezRollupConfigBuilder::new(
Endpoint::localhost(1234),
Expand All @@ -292,7 +299,7 @@ mod test {
"operator".to_owned(),
PathBuf::from("/tmp/boot_sector.hex"),
)
.set_kernel_debug_file(Path::new("/tmp/kernel_debug.log"))
.set_kernel_debug_file(FileWrapper::TempFile(kernel_debug_file))
.set_data_dir(RollupDataDir::TempWithPreImages {
preimages_dir: PathBuf::from("/tmp/pre_images"),
})
Expand All @@ -312,7 +319,7 @@ mod test {
"operator": "operator",
"boot_sector_file": "/tmp/boot_sector.hex",
"rpc_endpoint": format!("http://127.0.0.1:{}", config.rpc_endpoint.port()),
"kernel_debug_file": "/tmp/kernel_debug.log",
"kernel_debug_file": kernel_debug_file_path.to_string_lossy(),
"log_file": log_file.to_string_lossy()
})
);
Expand Down
Loading