Skip to content

Commit ba30ce0

Browse files
committed
Merge rust-bitcoin#4889: Manually print InvalidChecksum
cde4736 Manually print InvalidChecksum error (Tobin C. Harding) d1e430b Add regression tests for Display of ParseError (Tobin C. Harding) Pull request description: Currently the `ParseError` is a mess. The correct fix is likely to use an associated type in `Decodable` for the error but that can wait. Right now we are using `hex` in the `Display` impl for `ParseError`. This will cause trouble when we move the error type to `consenus_encoding` because `hex` is a feature. Even if we do not do so this code is a mainitenance burden. Just manually print hex characters by accessing the array. Logic is already tested with a static string, this patch does not change the output. ACKs for top commit: apoelstra: ACK cde4736; successfully ran local tests Tree-SHA512: f43cb3f2ffa5fb7ed8ce9172acd1f05c29841ae05af5fa124e22d1123b18c86013db0ff6db37f9e944f63016b5bdf8fe91fecb129ce0fad282f04ef5d4401055
2 parents b0708d9 + cde4736 commit ba30ce0

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

bitcoin/src/consensus/error.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use core::convert::Infallible;
66
use core::fmt;
77

88
use hex::error::{InvalidCharError, OddLengthStringError};
9-
use hex::DisplayHex as _;
109
use internals::write_err;
1110

1211
#[cfg(doc)]
@@ -188,8 +187,11 @@ impl fmt::Display for ParseError {
188187
MissingData => write!(f, "missing data (early end of file or slice too short)"),
189188
OversizedVectorAllocation { requested: ref r, max: ref m } =>
190189
write!(f, "allocation of oversized vector: requested {}, maximum {}", r, m),
191-
InvalidChecksum { expected: ref e, actual: ref a } =>
192-
write!(f, "invalid checksum: expected {:x}, actual {:x}", e.as_hex(), a.as_hex()),
190+
InvalidChecksum { expected: ref e, actual: ref a } => write!(
191+
f,
192+
"invalid checksum: expected {:02x}{:02x}{:02x}{:02x}, actual {:02x}{:02x}{:02x}{:02x}",
193+
e[0], e[1], e[2], e[3], a[0], a[1], a[2], a[3],
194+
),
193195
NonMinimalCompactSize => write!(f, "non-minimal compact size"),
194196
ParseFailed(ref s) => write!(f, "parse failed: {}", s),
195197
UnsupportedSegwitFlag(ref swflag) =>
@@ -257,3 +259,44 @@ impl From<OddLengthStringError> for FromHexError {
257259
pub(crate) fn parse_failed_error(msg: &'static str) -> Error {
258260
Error::Parse(ParseError::ParseFailed(msg))
259261
}
262+
263+
#[cfg(test)]
264+
mod tests {
265+
use super::*;
266+
267+
#[test]
268+
fn invalid_checksum_display() {
269+
let e = ParseError::InvalidChecksum {
270+
expected: [0xde, 0xad, 0xbe, 0xef],
271+
actual: [0xca, 0xfe, 0xba, 0xbe],
272+
};
273+
274+
let want = "invalid checksum: expected deadbeef, actual cafebabe";
275+
let got = format!("{}", e);
276+
assert_eq!(got, want);
277+
}
278+
279+
#[test]
280+
fn invalid_checksum_display_expected_leading_zeros() {
281+
let e = ParseError::InvalidChecksum {
282+
expected: [0x00, 0x00, 0x00, 0x0f],
283+
actual: [0xca, 0xfe, 0xba, 0xbe],
284+
};
285+
286+
let want = "invalid checksum: expected 0000000f, actual cafebabe";
287+
let got = format!("{}", e);
288+
assert_eq!(got, want);
289+
}
290+
291+
#[test]
292+
fn invalid_checksum_display_actual_leading_zeros() {
293+
let e = ParseError::InvalidChecksum {
294+
expected: [0xde, 0xad, 0xbe, 0xef],
295+
actual: [0x00, 0x00, 0x00, 0x0e],
296+
};
297+
298+
let want = "invalid checksum: expected deadbeef, actual 0000000e";
299+
let got = format!("{}", e);
300+
assert_eq!(got, want);
301+
}
302+
}

0 commit comments

Comments
 (0)