Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(chain): make FromHex consistent with ToHex for tx/block hashes #3893

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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