Enhance ledger-tool
Add a command-line argument (min-hashes) to restrict the entries processed by ledger-tool. For example, --min-hashes 1 will strip "empty" Entries, i.e. those with num_hashes = 0. Add basic ledger tool test
This commit is contained in:
committed by
Greg Fitzgerald
parent
b26906df1b
commit
84cc240f34
@ -11,7 +11,12 @@ homepage = "https://solana.com/"
|
||||
clap = "2.32.0"
|
||||
serde_json = "1.0.34"
|
||||
solana = { path = "..", version = "0.12.0" }
|
||||
solana-sdk = { path = "../sdk", version = "0.12.0" }
|
||||
solana-logger = { path = "../logger", version = "0.12.0" }
|
||||
|
||||
[dev-dependencies]
|
||||
assert_cmd = "0.10"
|
||||
predicates = "1"
|
||||
|
||||
[features]
|
||||
cuda = []
|
||||
|
@ -25,6 +25,14 @@ fn main() {
|
||||
.takes_value(true)
|
||||
.help("Limit to at most the first NUM entries in ledger\n (only applies to verify, print, json commands)"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("min-hashes")
|
||||
.short("h")
|
||||
.long("min-hashes")
|
||||
.value_name("NUM")
|
||||
.takes_value(true)
|
||||
.help("Skip entries with fewer than NUM hashes\n (only applies to print and json commands)"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("precheck")
|
||||
.short("p")
|
||||
@ -64,6 +72,13 @@ fn main() {
|
||||
None => <usize>::max_value(),
|
||||
};
|
||||
|
||||
let min_hashes = match matches.value_of("min-hashes") {
|
||||
Some(hashes) => hashes
|
||||
.parse()
|
||||
.expect("please pass a number for --min-hashes"),
|
||||
None => 0,
|
||||
} as u64;
|
||||
|
||||
match matches.subcommand() {
|
||||
("print", _) => {
|
||||
let entries = match read_ledger(ledger_path, true) {
|
||||
@ -78,6 +93,9 @@ fn main() {
|
||||
break;
|
||||
}
|
||||
let entry = entry.unwrap();
|
||||
if entry.num_hashes < min_hashes {
|
||||
continue;
|
||||
}
|
||||
println!("{:?}", entry);
|
||||
}
|
||||
}
|
||||
@ -88,6 +106,9 @@ fn main() {
|
||||
break;
|
||||
}
|
||||
let entry = entry.unwrap();
|
||||
if entry.num_hashes < min_hashes {
|
||||
continue;
|
||||
}
|
||||
serde_json::to_writer(stdout(), &entry).expect("serialize");
|
||||
stdout().write_all(b",\n").expect("newline");
|
||||
}
|
||||
|
57
ledger-tool/tests/basic.rs
Normal file
57
ledger-tool/tests/basic.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use solana::ledger::create_tmp_sample_ledger;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
|
||||
use assert_cmd::prelude::*;
|
||||
use std::process::Command;
|
||||
use std::process::Output;
|
||||
use std::sync::Arc;
|
||||
|
||||
fn run_ledger_tool(args: &[&str]) -> Output {
|
||||
Command::main_binary().unwrap().args(args).output().unwrap()
|
||||
}
|
||||
|
||||
fn count_newlines(chars: &[u8]) -> usize {
|
||||
chars.iter().filter(|&c| *c == '\n' as u8).count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_arguments() {
|
||||
// At least a ledger path is required
|
||||
assert!(!run_ledger_tool(&[]).status.success());
|
||||
|
||||
// Invalid ledger path should fail
|
||||
assert!(!run_ledger_tool(&["-l", "invalid_ledger", "verify"])
|
||||
.status
|
||||
.success());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nominal() {
|
||||
let keypair = Arc::new(Keypair::new());
|
||||
let (_mint, ledger_path, _genesis_entries) =
|
||||
create_tmp_sample_ledger("test_ledger_tool_nominal", 100, 10, keypair.pubkey(), 50);
|
||||
|
||||
// Basic validation
|
||||
let output = run_ledger_tool(&["-l", &ledger_path, "verify"]);
|
||||
assert!(output.status.success());
|
||||
|
||||
// Print everything
|
||||
let output = run_ledger_tool(&["-l", &ledger_path, "print"]);
|
||||
assert!(output.status.success());
|
||||
assert_eq!(count_newlines(&output.stdout), 13);
|
||||
|
||||
// Only print the first 5 items
|
||||
let output = run_ledger_tool(&["-l", &ledger_path, "-n", "5", "print"]);
|
||||
assert!(output.status.success());
|
||||
assert_eq!(count_newlines(&output.stdout), 5);
|
||||
|
||||
// Skip entries with no hashes (first entry)
|
||||
let output = run_ledger_tool(&["-l", &ledger_path, "-h", "1", "print"]);
|
||||
assert!(output.status.success());
|
||||
assert_eq!(count_newlines(&output.stdout), 12);
|
||||
|
||||
// Skip entries with fewer than 2 hashes (skip everything)
|
||||
let output = run_ledger_tool(&["-l", &ledger_path, "-h", "2", "print"]);
|
||||
assert!(output.status.success());
|
||||
assert_eq!(count_newlines(&output.stdout), 0);
|
||||
}
|
Reference in New Issue
Block a user