From 15684570913f580e2890b5e9529f08453dc60be7 Mon Sep 17 00:00:00 2001 From: Huan-Cheng Chang Date: Fri, 3 Jan 2025 11:19:06 +0000 Subject: [PATCH] feat(jstzd): print bootstrap account info --- Cargo.lock | 1 + crates/jstzd/Cargo.toml | 1 + crates/jstzd/src/task/jstzd.rs | 71 +++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 39ab394f6..6716357ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3003,6 +3003,7 @@ dependencies = [ "jstz_node", "octez", "predicates", + "prettytable", "rand 0.8.5", "regex", "reqwest", diff --git a/crates/jstzd/Cargo.toml b/crates/jstzd/Cargo.toml index 046ef8b53..7e0ec3242 100644 --- a/crates/jstzd/Cargo.toml +++ b/crates/jstzd/Cargo.toml @@ -21,6 +21,7 @@ indicatif.workspace = true jstz_crypto = { path = "../jstz_crypto" } jstz_node = {path = "../jstz_node"} octez = { path = "../octez" } +prettytable.workspace = true regex.workspace = true reqwest.workspace = true rust-embed.workspace = true diff --git a/crates/jstzd/src/task/jstzd.rs b/crates/jstzd/src/task/jstzd.rs index 5931900a5..5b11f3f43 100644 --- a/crates/jstzd/src/task/jstzd.rs +++ b/crates/jstzd/src/task/jstzd.rs @@ -28,10 +28,12 @@ use octez::r#async::{ client::{OctezClient, OctezClientConfig}, endpoint::Endpoint, node_config::OctezNodeConfig, - protocol::ProtocolParameter, + protocol::{BootstrapAccount, ProtocolParameter}, rollup::OctezRollupConfig, }; +use prettytable::{format::consts::FORMAT_DEFAULT, Cell, Row, Table}; use serde::Serialize; +use std::io::{stdout, Write}; use std::sync::Arc; use tokio::{ net::TcpListener, @@ -391,7 +393,7 @@ impl JstzdServer { } async fn spawn_jstzd(jstzd_config: JstzdConfig, print_info: bool) -> Result { - let mut jstzd = Jstzd::spawn(jstzd_config).await?; + let mut jstzd = Jstzd::spawn(jstzd_config.clone()).await?; let progress_bar = create_progress_bar(print_info); let mut jstzd_healthy = false; @@ -415,6 +417,14 @@ impl JstzdServer { abandon_progress_bar(progress_bar.as_ref()); bail!("jstzd never turns healthy"); } + + if print_info { + print_bootstrap_accounts( + &mut stdout(), + jstzd_config.protocol_params().bootstrap_accounts(), + ); + } + Ok(jstzd) } } @@ -462,6 +472,29 @@ fn abandon_progress_bar(progress_bar: Option<&ProgressBar>) { } } +fn print_bootstrap_accounts(writer: &mut impl Write, accounts: Vec<&BootstrapAccount>) { + let mut table = Table::new(); + table.set_titles(Row::new(vec![ + Cell::new("Address"), + Cell::new("XTZ Balance (mutez)"), + ])); + + for bootstrap_account in accounts { + table.add_row(Row::new(vec![ + Cell::new(&bootstrap_account.address()), + Cell::new(&bootstrap_account.amount().to_string()), + ])); + } + + table.set_format({ + let mut format = *FORMAT_DEFAULT; + format.indent(2); + format + }); + + let _ = writeln!(writer, "{}", table); +} + async fn health_check(state: &ServerState) -> bool { if let Some(v) = &state.server_handle { if !v.is_finished() { @@ -533,6 +566,7 @@ async fn config_handler( #[cfg(test)] mod tests { use indicatif::ProgressBar; + use octez::r#async::protocol::BootstrapAccount; #[test] fn collect_progress() { @@ -576,4 +610,37 @@ mod tests { super::abandon_progress_bar(Some(&bar)); assert!(bar.is_finished()); } + + #[test] + fn print_bootstrap_accounts() { + let mut buf = vec![]; + super::print_bootstrap_accounts( + &mut buf, + vec![ + &BootstrapAccount::new( + "edpkubRfnPoa6ts5vBdTB5syvjeK2AyEF3kyhG4Sx7F9pU3biku4wv", + 1, + ) + .unwrap(), + &BootstrapAccount::new( + "edpkuFrRoDSEbJYgxRtLx2ps82UdaYc1WwfS9sE11yhauZt5DgCHbU", + 2, + ) + .unwrap(), + ], + ); + let s = String::from_utf8(buf).unwrap(); + assert_eq!( + s, + r#" +--------------------------------------+---------------------+ + | Address | XTZ Balance (mutez) | + +======================================+=====================+ + | tz1hGHtks3PnX4SnpqcDNMg5P3AQhTiH1WE4 | 1 | + +--------------------------------------+---------------------+ + | tz1b7tUupMgCNw2cCLpKTkSD1NZzB5TkP2sv | 2 | + +--------------------------------------+---------------------+ + +"# + ); + } }