From 343a683ceaf5f409f6fbfcfef51fbcf0debb77cd Mon Sep 17 00:00:00 2001 From: teor Date: Thu, 22 Jun 2023 01:02:05 +1000 Subject: [PATCH] cleanup(test): Make test debugging output more readable (#7027) * Fix some debug impls to use hex rather than u8 arrays * Hide extremely long debug data in proptests --- zebra-chain/src/fmt.rs | 2 +- zebra-chain/src/sprout/joinsplit.rs | 21 +++++++++++++++++++-- zebra-chain/src/transaction/joinsplit.rs | 14 +++++++++++++- zebrad/src/components/mempool/tests/prop.rs | 4 ++-- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/zebra-chain/src/fmt.rs b/zebra-chain/src/fmt.rs index 80066314..98923446 100644 --- a/zebra-chain/src/fmt.rs +++ b/zebra-chain/src/fmt.rs @@ -162,7 +162,7 @@ where } /// Wrapper to override `Debug`, redirecting it to hex-encode the type. -/// The type must be hex-encodable. +/// The type must implement `AsRef<[u8]>`. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))] #[serde(transparent)] diff --git a/zebra-chain/src/sprout/joinsplit.rs b/zebra-chain/src/sprout/joinsplit.rs index 059ac4be..ca891e5f 100644 --- a/zebra-chain/src/sprout/joinsplit.rs +++ b/zebra-chain/src/sprout/joinsplit.rs @@ -1,6 +1,6 @@ //! Sprout funds transfers using [`JoinSplit`]s. -use std::io; +use std::{fmt, io}; use serde::{Deserialize, Serialize}; @@ -49,7 +49,7 @@ impl From<&RandomSeed> for [u8; 32] { /// A _JoinSplit Description_, as described in [protocol specification ยง7.2][ps]. /// /// [ps]: https://zips.z.cash/protocol/protocol.pdf#joinsplitencoding -#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Clone, Serialize, Deserialize)] pub struct JoinSplit { /// A value that the JoinSplit transfer removes from the transparent value /// pool. @@ -81,6 +81,23 @@ pub struct JoinSplit { pub enc_ciphertexts: [note::EncryptedNote; 2], } +impl fmt::Debug for JoinSplit

{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("JoinSplit") + .field("vpub_old", &self.vpub_old) + .field("vpub_new", &self.vpub_new) + .field("anchor", &self.anchor) + .field("nullifiers", &self.nullifiers) + .field("commitments", &self.commitments) + .field("ephemeral_key", &HexDebug(self.ephemeral_key.as_bytes())) + .field("random_seed", &self.random_seed) + .field("vmacs", &self.vmacs) + .field("zkproof", &self.zkproof) + .field("enc_ciphertexts", &self.enc_ciphertexts) + .finish() + } +} + impl ZcashSerialize for JoinSplit

{ fn zcash_serialize(&self, mut writer: W) -> Result<(), io::Error> { self.vpub_old.zcash_serialize(&mut writer)?; diff --git a/zebra-chain/src/transaction/joinsplit.rs b/zebra-chain/src/transaction/joinsplit.rs index 0735bb6e..80103b16 100644 --- a/zebra-chain/src/transaction/joinsplit.rs +++ b/zebra-chain/src/transaction/joinsplit.rs @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::{ amount::{self, Amount, NegativeAllowed}, + fmt::HexDebug, primitives::{ed25519, ZkSnarkProof}, sprout::{self, JoinSplit, Nullifier}, }; @@ -16,7 +17,7 @@ use crate::{ /// description with the required signature data, so that an /// `Option` correctly models the presence or absence of any /// JoinSplit data. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct JoinSplitData { /// The first JoinSplit description in the transaction, /// using proofs of type `P`. @@ -48,6 +49,17 @@ pub struct JoinSplitData { pub sig: ed25519::Signature, } +impl fmt::Debug for JoinSplitData

{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("JoinSplitData") + .field("first", &self.first) + .field("rest", &self.rest) + .field("pub_key", &self.pub_key) + .field("sig", &HexDebug(&self.sig.to_bytes())) + .finish() + } +} + impl fmt::Display for JoinSplitData

{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut fmter = diff --git a/zebrad/src/components/mempool/tests/prop.rs b/zebrad/src/components/mempool/tests/prop.rs index 46e85b35..0b0a35cd 100644 --- a/zebrad/src/components/mempool/tests/prop.rs +++ b/zebrad/src/components/mempool/tests/prop.rs @@ -11,7 +11,7 @@ use tower::{buffer::Buffer, util::BoxService}; use zebra_chain::{ block::{self, Block}, - fmt::DisplayToDebug, + fmt::{DisplayToDebug, TypeNameToDebug}, parameters::{Network, NetworkUpgrade}, serialization::ZcashDeserializeInto, transaction::VerifiedUnminedTx, @@ -103,7 +103,7 @@ proptest! { network in any::(), mut previous_chain_tip in any::>(), mut transactions in vec(any::>(), 0..CHAIN_LENGTH), - fake_chain_tips in vec(any::>(), 0..CHAIN_LENGTH), + fake_chain_tips in vec(any::>(), 0..CHAIN_LENGTH), ) { let (runtime, _init_guard) = zebra_test::init_async();