Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Add print-tx-tree command to gevulot-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
tuommaki committed Feb 6, 2024
1 parent c842fa2 commit 1364409
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clap::Parser;
use clap::Subcommand;
use gevulot_node::rpc_client::RpcClient;
use gevulot_node::types::Hash;
use gevulot_node::types::TransactionTree;
use std::net::SocketAddr;
use std::path::PathBuf;

Expand Down Expand Up @@ -92,6 +94,9 @@ enum ConfCommands {
#[clap(short, long, value_name = "TASK ARRAY")]
tasks: String,
},
PrintTxTree {
hash: String,
},
/// Calculate the Hash of the specified file.
#[command(arg_required_else_help = true)]
CalculateHash {
Expand Down Expand Up @@ -155,5 +160,38 @@ async fn main() {
Err(err) => println!("An error hash calculus Tx :{err}"),
}
}
ConfCommands::PrintTxTree { hash } => {
let hash = Hash::from(hash);
match client.get_tx_tree(&hash).await {
Ok(tx_tree) => print_tx_tree(&tx_tree, 0),
Err(err) => println!("An error while fetching transaction tree: {err}"),
};
}
}
}

fn print_tx_tree(tree: &TransactionTree, indentation: u16) {
match tree {
TransactionTree::Root { children, hash } => {
println!("Root: {hash}");
children
.iter()
.for_each(|x| print_tx_tree(&x, indentation + 1));
}
TransactionTree::Node { children, hash } => {
println!(
"{}Node: {hash}",
(0..indentation).map(|_| "\t").collect::<String>()
);
children
.iter()
.for_each(|x| print_tx_tree(&x, indentation + 1));
}
TransactionTree::Leaf { hash } => {
println!(
"{}Leaf: {hash}",
(0..indentation).map(|_| "\t").collect::<String>()
);
}
}
}

0 comments on commit 1364409

Please sign in to comment.