chain: impl Display for {block, transaction}::Hash
Also add a Display/FromStr round-trip proptest.
This commit is contained in:
parent
0a1878d9c3
commit
e96a472099
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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()?;
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
Loading…
Reference in New Issue