Skip to content

Commit

Permalink
fix(chain): make FromHex consistent with ToHex for tx/block hashes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
conradoplg authored Mar 17, 2022
1 parent 88ab6de commit b9640fb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion zebra-chain/src/block/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ impl FromHex for Hash {
type Error = <[u8; 32] as FromHex>::Error;

fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
let hash = <[u8; 32]>::from_hex(hex)?;
let mut hash = <[u8; 32]>::from_hex(hex)?;
hash.reverse();

Ok(hash.into())
}
Expand Down
11 changes: 11 additions & 0 deletions zebra-chain/src/block/tests/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{env, io::ErrorKind};

use proptest::{arbitrary::any, prelude::*, test_runner::Config};

use hex::{FromHex, ToHex};

use zebra_test::prelude::*;

use crate::{
Expand Down Expand Up @@ -43,6 +45,15 @@ proptest! {
prop_assert_eq!(hash, parsed);
}

#[test]
fn block_hash_hex_roundtrip(hash in any::<Hash>()) {
zebra_test::init();

let hex_hash: String = hash.encode_hex();
let new_hash = Hash::from_hex(hex_hash).expect("hex hash should parse");
prop_assert_eq!(hash, new_hash);
}

#[test]
fn blockheader_roundtrip(header in any::<Header>()) {
zebra_test::init();
Expand Down
3 changes: 2 additions & 1 deletion zebra-chain/src/transaction/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ impl FromHex for Hash {
type Error = <[u8; 32] as FromHex>::Error;

fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
let hash = <[u8; 32]>::from_hex(hex)?;
let mut hash = <[u8; 32]>::from_hex(hex)?;
hash.reverse();

Ok(hash.into())
}
Expand Down
11 changes: 11 additions & 0 deletions zebra-chain/src/transaction/tests/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::io::Cursor;

use zebra_test::prelude::*;

use hex::{FromHex, ToHex};

use super::super::*;

use crate::{
Expand Down Expand Up @@ -51,6 +53,15 @@ proptest! {
}
}

#[test]
fn transaction_hash_hex_roundtrip(hash in any::<Hash>()) {
zebra_test::init();

let hex_hash: String = hash.encode_hex();
let new_hash = Hash::from_hex(hex_hash).expect("hex hash should parse");
prop_assert_eq!(hash, new_hash);
}

#[test]
fn transaction_auth_digest_struct_display_roundtrip(auth_digest in any::<AuthDigest>()) {
zebra_test::init();
Expand Down

0 comments on commit b9640fb

Please sign in to comment.