Merge rust-bitcoin/rust-bitcoin#2443: Print hex in Debug for Sequence

c084afa8b2 Print hex in Debug for Sequence (Tobin C. Harding)

Pull request description:

  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)

ACKs for top commit:
  Kixunil:
    ACK c084afa8b2
  apoelstra:
    ACK c084afa8b2

Tree-SHA512: d60cd8896ca56a30fc8bd030cf3dd1bc1fd3a1609e99bfc2f26b9bd665b11c34c9df93b3f3ad731506d916513ca4a192dde476e16d99f2d4c4b2697f70a7bc98
This commit is contained in:
Andrew Poelstra 2024-02-06 13:34:33 +00:00
commit 0f669404c4
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
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.
@ -2406,6 +2413,12 @@ mod tests {
InputWeightPrediction::P2PKH_COMPRESSED_MAX.weight()
);
}
#[test]
fn sequence_debug_output() {
let seq = Sequence::from_seconds_floor(1000);
println!("{:?}", seq)
}
}
#[cfg(bench)]