Skip to content

Commit

Permalink
feat(jstzd): print bootstrap account info
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Jan 3, 2025
1 parent e6425f1 commit 1568457
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 69 additions & 2 deletions crates/jstzd/src/task/jstzd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -391,7 +393,7 @@ impl JstzdServer {
}

async fn spawn_jstzd(jstzd_config: JstzdConfig, print_info: bool) -> Result<Jstzd> {
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;
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 |
+--------------------------------------+---------------------+
"#
);
}
}

0 comments on commit 1568457

Please sign in to comment.