From 9e3c4dbfe901ae754c67426d564b7f9dc74170cb Mon Sep 17 00:00:00 2001 From: mm-near <91919554+mm-near@users.noreply.github.com> Date: Mon, 30 May 2022 20:58:59 +0200 Subject: [PATCH] state viewer changes: show full hashes, print number of state changes (#6859) * state viewer changes: show full hashes, print number of state changes * review feedback. Co-authored-by: near-bulldozer[bot] <73298989+near-bulldozer[bot]@users.noreply.github.com> --- tools/state-viewer/src/apply_chain_range.rs | 7 ++++--- tools/state-viewer/src/cli.rs | 13 +++++++++++- tools/state-viewer/src/commands.rs | 23 ++++++++++++++------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/tools/state-viewer/src/apply_chain_range.rs b/tools/state-viewer/src/apply_chain_range.rs index 18b220bd318..bfdf4b89653 100644 --- a/tools/state-viewer/src/apply_chain_range.rs +++ b/tools/state-viewer/src/apply_chain_range.rs @@ -309,7 +309,7 @@ fn apply_block_from_range( maybe_add_to_csv( csv_file_mutex, &format!( - "{},{},{},{},{},{},{},{},{},{}", + "{},{},{},{},{},{},{},{},{},{},{}", height, block_hash, block_author, @@ -319,7 +319,8 @@ fn apply_block_from_range( apply_result.total_gas_burnt, chunk_present, apply_result.processed_delayed_receipts.len(), - delayed_indices.map_or(0, |d| d.next_available_index - d.first_index) + delayed_indices.map_or(0, |d| d.next_available_index - d.first_index), + apply_result.trie_changes.state_changes().len(), ), ); progress_reporter.inc_and_report_progress(apply_result.total_gas_burnt); @@ -358,7 +359,7 @@ pub fn apply_chain_range( println!("Printing results including outcomes of applying receipts"); let csv_file_mutex = Arc::new(Mutex::new(csv_file)); - maybe_add_to_csv(&csv_file_mutex, "Height,Hash,Author,#Tx,#Receipt,Timestamp,GasUsed,ChunkPresent,#ProcessedDelayedReceipts,#DelayedReceipts"); + maybe_add_to_csv(&csv_file_mutex, "Height,Hash,Author,#Tx,#Receipt,Timestamp,GasUsed,ChunkPresent,#ProcessedDelayedReceipts,#DelayedReceipts,#StateChanges"); let range = start_height..=end_height; let progress_reporter = ProgressReporter { diff --git a/tools/state-viewer/src/cli.rs b/tools/state-viewer/src/cli.rs index 49314e5ae88..eefa4d9f7a4 100644 --- a/tools/state-viewer/src/cli.rs +++ b/tools/state-viewer/src/cli.rs @@ -142,11 +142,22 @@ pub struct ChainCmd { start_index: BlockHeight, #[clap(long)] end_index: BlockHeight, + // If true, show the full hash (block hash and chunk hash) when printing. + // If false, show only first couple chars. + #[clap(long)] + show_full_hashes: bool, } impl ChainCmd { pub fn run(self, home_dir: &Path, near_config: NearConfig, store: Store) { - print_chain(self.start_index, self.end_index, home_dir, near_config, store); + print_chain( + self.start_index, + self.end_index, + home_dir, + near_config, + store, + self.show_full_hashes, + ); } } diff --git a/tools/state-viewer/src/commands.rs b/tools/state-viewer/src/commands.rs index 3d34bdc067c..442612473bd 100644 --- a/tools/state-viewer/src/commands.rs +++ b/tools/state-viewer/src/commands.rs @@ -216,6 +216,7 @@ pub(crate) fn print_chain( home_dir: &Path, near_config: NearConfig, store: Store, + show_full_hashes: bool, ) { let chain_store = ChainStore::new( store.clone(), @@ -235,7 +236,11 @@ pub(crate) fn print_chain( if let Ok(block_hash) = chain_store.get_block_hash_by_height(height) { let header = chain_store.get_block_header(&block_hash).unwrap().clone(); if height == 0 { - println!("{: >3} {}", header.height(), format_hash(*header.hash())); + println!( + "{: >3} {}", + header.height(), + format_hash(*header.hash(), show_full_hashes) + ); } else { let parent_header = chain_store.get_block_header(header.prev_hash()).unwrap().clone(); @@ -246,7 +251,7 @@ pub(crate) fn print_chain( account_id_to_blocks = HashMap::new(); println!( "Epoch {} Validators {:?}", - format_hash(epoch_id.0), + format_hash(epoch_id.0, show_full_hashes), runtime .get_epoch_block_producers_ordered(&epoch_id, header.hash()) .unwrap() @@ -272,7 +277,7 @@ pub(crate) fn print_chain( chunk_debug_str.push(format!( "{}: {} {: >3} Tgas ", shard_id, - format_hash(chunk.chunk_hash().0), + format_hash(chunk.chunk_hash().0, show_full_hashes), chunk.cloned_header().gas_used() / (1024 * 1024 * 1024 * 1024) )); } @@ -281,10 +286,10 @@ pub(crate) fn print_chain( println!( "{: >3} {} | {: >10} | parent: {: >3} {} | {} {}", header.height(), - format_hash(*header.hash()), + format_hash(*header.hash(), show_full_hashes), block_producer, parent_header.height(), - format_hash(*parent_header.hash()), + format_hash(*parent_header.hash(), show_full_hashes), chunk_mask_to_str(header.chunk_mask()), chunk_debug_str.join("|") ); @@ -728,8 +733,12 @@ fn load_trie_stop_at_height( (runtime, state_roots, last_block.header().clone()) } -pub fn format_hash(h: CryptoHash) -> String { - to_base(&h)[..7].to_string() +pub fn format_hash(h: CryptoHash, show_full_hashes: bool) -> String { + if show_full_hashes { + to_base(&h).to_string() + } else { + to_base(&h)[..7].to_string() + } } pub fn chunk_mask_to_str(mask: &[bool]) -> String {