chain: impl Display for {block, transaction}::Hash

Also add a Display/FromStr round-trip proptest.
This commit is contained in:
Henry de Valence 2020-09-04 13:47:42 -07:00 committed by Deirdre Connolly
parent 0a1878d9c3
commit e96a472099
4 changed files with 26 additions and 0 deletions

View File

@ -22,6 +22,12 @@ use super::Header;
#[cfg_attr(test, derive(Arbitrary))]
pub struct Hash(pub [u8; 32]);
impl fmt::Display for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&hex::encode(&self.0))
}
}
impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("BlockHeaderHash")

View File

@ -17,6 +17,13 @@ proptest! {
prop_assert_eq![hash, other_hash];
}
#[test]
fn block_hash_display_fromstr_roundtrip(hash in any::<Hash>()) {
let display = format!("{}", hash);
let parsed = display.parse::<Hash>().expect("hash should parse");
prop_assert_eq!(hash, parsed);
}
#[test]
fn blockheader_roundtrip(header in any::<Header>()) {
let bytes = header.zcash_serialize_to_vec()?;

View File

@ -27,6 +27,12 @@ impl<'a> From<&'a Transaction> for Hash {
}
}
impl fmt::Display for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&hex::encode(&self.0))
}
}
impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("TransactionHash")

View File

@ -14,6 +14,13 @@ proptest! {
prop_assert_eq![tx, tx2];
}
#[test]
fn transaction_hash_display_fromstr_roundtrip(hash in any::<Hash>()) {
let display = format!("{}", hash);
let parsed = display.parse::<Hash>().expect("hash should parse");
prop_assert_eq!(hash, parsed);
}
#[test]
fn locktime_roundtrip(locktime in any::<LockTime>()) {
let mut bytes = Cursor::new(Vec::new());