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.
This commit is contained in:
Henry de Valence 2020-11-19 17:32:27 -08:00 committed by Deirdre Connolly
parent d1ee7f263a
commit b5515123eb
1 changed files with 15 additions and 1 deletions

View File

@ -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.