From b5515123eb1fc89858cefff7bd07bb35b522ca69 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Thu, 19 Nov 2020 17:32:27 -0800 Subject: [PATCH] chain: add custom Debug for CoinbaseData The derived Debug impl just shows u8s as numbers, which isn't what we want. There are basically two reasonable options here: 1. Hex-encoded bytes 2. Escaped ASCII I picked (2) because a lot of coinbase data has ascii text in it. --- zebra-chain/src/transparent.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/zebra-chain/src/transparent.rs b/zebra-chain/src/transparent.rs index 93aacc3e..97a09683 100644 --- a/zebra-chain/src/transparent.rs +++ b/zebra-chain/src/transparent.rs @@ -23,7 +23,7 @@ use crate::{ }; /// Arbitrary data inserted by miners into a coinbase transaction. -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)] pub struct CoinbaseData( /// Invariant: this vec, together with the coinbase height, must be less than /// 100 bytes. We enforce this by only constructing CoinbaseData fields by @@ -40,6 +40,20 @@ impl AsRef<[u8]> for CoinbaseData { } } +impl std::fmt::Debug for CoinbaseData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let escaped = String::from_utf8( + self.0 + .iter() + .cloned() + .flat_map(std::ascii::escape_default) + .collect(), + ) + .expect("ascii::escape_default produces utf8"); + f.debug_tuple("CoinbaseData").field(&escaped).finish() + } +} + /// OutPoint /// /// A particular transaction output reference.