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

This commit is contained in:
Conrado Gouvea 2022-03-17 17:24:48 -03:00 committed by GitHub
parent 88ab6deeac
commit b9640fbb55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 2 deletions

View File

@ -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())
}

View File

@ -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::{
@ -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();

View File

@ -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())
}

View File

@ -6,6 +6,8 @@ use std::io::Cursor;
use zebra_test::prelude::*;
use hex::{FromHex, ToHex};
use super::super::*;
use crate::{
@ -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();