Print hex in Debug for Sequence

Printing the `Sequence` as a decimal is not super useful when debugging,
print it in hex instead.

Using code:

        let seq = Sequence::from_consensus(0xFFFFFFFF);
        println!("sequence: {:?}", seq);

Before applying this patch we get:

        sequence: Sequence(4294967295)

And after applying we get:

        sequence: Sequence(0xffffffff)
This commit is contained in:
Tobin C. Harding 2024-02-05 16:57:47 +11:00
parent a3c4194c3f
commit c084afa8b2
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 14 additions and 1 deletions

View File

@ -315,7 +315,7 @@ impl Default for TxIn {
/// [BIP-65]: <https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki>
/// [BIP-68]: <https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki>
/// [BIP-125]: <https://github.com/bitcoin/bips/blob/master/bip-0125.mediawiki>
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
pub struct Sequence(pub u32);
@ -504,6 +504,13 @@ impl fmt::UpperHex for Sequence {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::UpperHex::fmt(&self.0, f) }
}
impl fmt::Debug for Sequence {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// 10 because its 8 digits + 2 for the '0x'
write!(f, "Sequence({:#010x})", self.0)
}
}
impl_parse_str_from_int_infallible!(Sequence, u32, from_consensus);
/// Bitcoin transaction output.
@ -2408,6 +2415,12 @@ mod tests {
InputWeightPrediction::P2PKH_COMPRESSED_MAX.weight()
);
}
#[test]
fn sequence_debug_output() {
let seq = Sequence::from_seconds_floor(1000);
println!("{:?}", seq)
}
}
#[cfg(bench)]