Skip to content

Commit

Permalink
Add more debug and trace logging
Browse files Browse the repository at this point in the history
The `Debug` impl for `sapling::Node` is updated to output hex-encoded
bytes for readability.
  • Loading branch information
str4d committed Jul 13, 2023
1 parent 6a648b3 commit f5479da
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
18 changes: 17 additions & 1 deletion zcash_client_backend/src/data_api/scanning.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt;
use std::ops::Range;

use zcash_primitives::consensus::BlockHeight;
Expand Down Expand Up @@ -26,10 +27,25 @@ pub struct ScanRange {
priority: ScanPriority,
}

impl fmt::Display for ScanRange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{:?}({}..{})",
self.priority, self.block_range.start, self.block_range.end,
)
}
}

impl ScanRange {
/// Constructs a scan range from its constituent parts.
pub fn from_parts(block_range: Range<BlockHeight>, priority: ScanPriority) -> Self {
assert!(block_range.end >= block_range.start);
assert!(
block_range.end >= block_range.start,
"{:?} is invalid for ScanRange({:?})",
block_range,
priority,
);
ScanRange {
block_range,
priority,
Expand Down
2 changes: 2 additions & 0 deletions zcash_client_sqlite/src/wallet/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use either::Either;
use incrementalmerkletree::Retention;
use std::{collections::HashMap, fmt, io};
use tracing::debug;

use rusqlite::{self, types::ToSql};
use schemer::{Migrator, MigratorError};
Expand Down Expand Up @@ -318,6 +319,7 @@ pub fn init_blocks_table<P: consensus::Parameters>(
)?;

if let Some(nonempty_frontier) = block_end_tree.to_frontier().value() {
debug!("Inserting frontier into ShardTree: {:?}", nonempty_frontier);
let shard_store =
SqliteShardStore::<_, sapling::Node, SAPLING_SHARD_HEIGHT>::from_connection(
wdb.conn.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rusqlite::{self, named_params, params};
use schemer;
use schemer_rusqlite::RusqliteMigration;
use shardtree::ShardTree;
use tracing::trace;
use uuid::Uuid;

use zcash_client_backend::data_api::{
Expand Down Expand Up @@ -61,13 +62,15 @@ impl RusqliteMigration for Migration {

fn up(&self, transaction: &rusqlite::Transaction) -> Result<(), WalletMigrationError> {
// Add commitment tree sizes to block metadata.
trace!("Adding new columns");
transaction.execute_batch(
"ALTER TABLE blocks ADD COLUMN sapling_commitment_tree_size INTEGER;
ALTER TABLE blocks ADD COLUMN orchard_commitment_tree_size INTEGER;
ALTER TABLE sapling_received_notes ADD COLUMN commitment_tree_position INTEGER;",
)?;

// Add shard persistence
trace!("Creating tables for shard persistence");
transaction.execute_batch(
"CREATE TABLE sapling_tree_shards (
shard_index INTEGER PRIMARY KEY,
Expand All @@ -86,6 +89,7 @@ impl RusqliteMigration for Migration {
)?;

// Add checkpoint persistence
trace!("Creating tables for checkpoint persistence");
transaction.execute_batch(
"CREATE TABLE sapling_tree_checkpoints (
checkpoint_id INTEGER PRIMARY KEY,
Expand Down Expand Up @@ -133,10 +137,20 @@ impl RusqliteMigration for Migration {
)
})?;

trace!(
height = block_height,
size = block_end_tree.size(),
"Storing Sapling commitment tree size"
);
stmt_update_block_sapling_tree_size
.execute(params![block_end_tree.size(), block_height])?;

if let Some(nonempty_frontier) = block_end_tree.to_frontier().value() {
trace!(
height = block_height,
frontier = ?nonempty_frontier,
"Inserting frontier nodes",
);
shard_tree.insert_frontier_nodes(
nonempty_frontier.clone(),
Retention::Checkpoint {
Expand Down
8 changes: 8 additions & 0 deletions zcash_client_sqlite/src/wallet/scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::cmp::{max, min, Ordering};
use std::collections::BTreeSet;
use std::ops::{Not, Range};
use std::rc::Rc;
use tracing::{debug, info, trace};

Check failure on line 6 in zcash_client_sqlite/src/wallet/scanning.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `info`

error: unused import: `info` --> zcash_client_sqlite/src/wallet/scanning.rs:6:22 | 6 | use tracing::{debug, info, trace}; | ^^^^ | = note: `-D unused-imports` implied by `-D warnings`

Check failure on line 6 in zcash_client_sqlite/src/wallet/scanning.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `info`

error: unused import: `info` --> zcash_client_sqlite/src/wallet/scanning.rs:6:22 | 6 | use tracing::{debug, info, trace}; | ^^^^ | = note: `-D unused-imports` implied by `-D warnings`
use zcash_client_backend::data_api::scanning::{ScanPriority, ScanRange};

use incrementalmerkletree::{Address, Position};
Expand Down Expand Up @@ -421,6 +422,7 @@ pub(crate) fn insert_queue_entries<'a>(
)?;

for entry in entries {
trace!("Inserting queue entry {}", entry);
if !entry.is_empty() {
stmt.execute(named_params![
":block_range_start": u32::from(entry.block_range().start) ,
Expand Down Expand Up @@ -664,6 +666,12 @@ pub(crate) fn update_chain_tip<P: consensus::Parameters>(
}
}
});
if let Some(entry) = &shard_entry {
debug!("{} will update latest shard", entry);
}
if let Some(entry) = &tip_entry {
debug!("{} will connect prior tip to new tip", entry);
}

let query_range = match (shard_entry.as_ref(), tip_entry.as_ref()) {
(Some(se), Some(te)) => Some(Range {
Expand Down
12 changes: 11 additions & 1 deletion zcash_primitives/src/sapling/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use bitvec::{order::Lsb0, view::AsBits};
use group::{ff::PrimeField, Curve};
use incrementalmerkletree::{Hashable, Level};
use lazy_static::lazy_static;

use std::fmt;
use std::io::{self, Read, Write};

use super::{
Expand Down Expand Up @@ -64,11 +66,19 @@ pub fn merkle_hash(depth: usize, lhs: &[u8; 32], rhs: &[u8; 32]) -> [u8; 32] {
}

/// A node within the Sapling commitment tree.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Node {
pub(super) repr: [u8; 32],
}

impl fmt::Debug for Node {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Node")
.field("repr", &hex::encode(self.repr))
.finish()
}
}

impl Node {
#[cfg(test)]
pub(crate) fn new(repr: [u8; 32]) -> Self {
Expand Down

0 comments on commit f5479da

Please sign in to comment.