From 22da7aeddedfaf0ec6c5da5dccc9714a750e4eb2 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sat, 29 Jul 2023 03:56:59 -0400 Subject: [PATCH] Implement a pretty Debug for various objects --- coins/monero/src/lib.rs | 8 ++++++- coins/monero/src/wallet/address.rs | 15 ++++++++++++- coins/monero/src/wallet/scan.rs | 34 +++++++++++++++++++++++++++--- processor/src/main.rs | 1 + processor/src/plan.rs | 13 +++++++++++- 5 files changed, 65 insertions(+), 6 deletions(-) diff --git a/coins/monero/src/lib.rs b/coins/monero/src/lib.rs index 43b8ddc1..d784f87e 100644 --- a/coins/monero/src/lib.rs +++ b/coins/monero/src/lib.rs @@ -170,12 +170,18 @@ impl Protocol { /// Transparent structure representing a Pedersen commitment's contents. #[allow(non_snake_case)] -#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)] +#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)] pub struct Commitment { pub mask: Scalar, pub amount: u64, } +impl core::fmt::Debug for Commitment { + fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { + fmt.debug_struct("Commitment").field("amount", &self.amount).finish_non_exhaustive() + } +} + impl Commitment { /// A commitment to zero, defined with a mask of 1 (as to not be the identity). pub fn zero() -> Commitment { diff --git a/coins/monero/src/wallet/address.rs b/coins/monero/src/wallet/address.rs index bfd0096e..e95090f7 100644 --- a/coins/monero/src/wallet/address.rs +++ b/coins/monero/src/wallet/address.rs @@ -182,13 +182,26 @@ impl AddressMeta { } /// A Monero address, composed of metadata and a spend/view key. -#[derive(Clone, Copy, PartialEq, Eq, Debug)] +#[derive(Clone, Copy, PartialEq, Eq)] pub struct Address { pub meta: AddressMeta, pub spend: EdwardsPoint, pub view: EdwardsPoint, } +impl core::fmt::Debug for Address { + fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { + fmt + .debug_struct("Address") + .field("meta", &self.meta) + .field("spend", &hex::encode(self.spend.compress().0)) + .field("view", &hex::encode(self.view.compress().0)) + // This is not a real field yet is the most valuable thing to know when debugging + .field("(address)", &self.to_string()) + .finish() + } +} + impl Zeroize for Address { fn zeroize(&mut self) { self.meta.zeroize(); diff --git a/coins/monero/src/wallet/scan.rs b/coins/monero/src/wallet/scan.rs index 86cd147b..b1807c9b 100644 --- a/coins/monero/src/wallet/scan.rs +++ b/coins/monero/src/wallet/scan.rs @@ -20,12 +20,18 @@ use crate::{ }; /// An absolute output ID, defined as its transaction hash and output index. -#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)] +#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)] pub struct AbsoluteId { pub tx: [u8; 32], pub o: u8, } +impl core::fmt::Debug for AbsoluteId { + fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { + fmt.debug_struct("AbsoluteId").field("tx", &hex::encode(self.tx)).field("o", &self.o).finish() + } +} + impl AbsoluteId { pub fn write(&self, w: &mut W) -> io::Result<()> { w.write_all(&self.tx)?; @@ -44,7 +50,7 @@ impl AbsoluteId { } /// The data contained with an output. -#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)] +#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)] pub struct OutputData { pub key: EdwardsPoint, /// Absolute difference between the spend key and the key in this output @@ -52,6 +58,17 @@ pub struct OutputData { pub commitment: Commitment, } +impl core::fmt::Debug for OutputData { + fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { + fmt + .debug_struct("OutputData") + .field("key", &hex::encode(self.key.compress().0)) + .field("key_offset", &hex::encode(self.key_offset.to_bytes())) + .field("commitment", &self.commitment) + .finish() + } +} + impl OutputData { pub fn write(&self, w: &mut W) -> io::Result<()> { w.write_all(&self.key.compress().to_bytes())?; @@ -76,7 +93,7 @@ impl OutputData { } /// The metadata for an output. -#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)] +#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)] pub struct Metadata { /// The subaddress this output was sent to. pub subaddress: Option, @@ -89,6 +106,17 @@ pub struct Metadata { pub arbitrary_data: Vec>, } +impl core::fmt::Debug for Metadata { + fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { + fmt + .debug_struct("Metadata") + .field("subaddress", &self.subaddress) + .field("payment_id", &hex::encode(self.payment_id)) + .field("arbitrary_data", &self.arbitrary_data.iter().map(hex::encode).collect::>()) + .finish() + } +} + impl Metadata { pub fn write(&self, w: &mut W) -> io::Result<()> { if let Some(subaddress) = self.subaddress { diff --git a/processor/src/main.rs b/processor/src/main.rs index 45a55dae..2cb9f93e 100644 --- a/processor/src/main.rs +++ b/processor/src/main.rs @@ -702,6 +702,7 @@ async fn run(mut raw_db: D, coin: C, mut coordi info!("created batch {} ({} instructions)", batch.id, batch.instructions.len()); // Start signing this batch + // TODO: Don't reload both sets of keys in full just to get the Substrate public key tributary_mutable .substrate_signers .get_mut(tributary_mutable.key_gen.keys(&key).0.group_key().to_bytes().as_slice()) diff --git a/processor/src/plan.rs b/processor/src/plan.rs index ea90006d..6a0f08aa 100644 --- a/processor/src/plan.rs +++ b/processor/src/plan.rs @@ -69,13 +69,24 @@ impl Payment { } } -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq)] pub struct Plan { pub key: ::G, pub inputs: Vec, pub payments: Vec>, pub change: Option<::G>, } +impl core::fmt::Debug for Plan { + fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { + fmt + .debug_struct("Plan") + .field("key", &hex::encode(self.key.to_bytes())) + .field("inputs", &self.inputs) + .field("payments", &self.payments) + .field("change", &self.change.map(|change| hex::encode(change.to_bytes()))) + .finish() + } +} impl Plan { pub fn transcript(&self) -> RecommendedTranscript {