fix(chain): make FromHex consistent with ToHex for tx/block hashes (#3893)
This commit is contained in:
parent
88ab6deeac
commit
b9640fbb55
|
@ -74,7 +74,8 @@ impl FromHex for Hash {
|
||||||
type Error = <[u8; 32] as FromHex>::Error;
|
type Error = <[u8; 32] as FromHex>::Error;
|
||||||
|
|
||||||
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::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())
|
Ok(hash.into())
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ use std::{env, io::ErrorKind};
|
||||||
|
|
||||||
use proptest::{arbitrary::any, prelude::*, test_runner::Config};
|
use proptest::{arbitrary::any, prelude::*, test_runner::Config};
|
||||||
|
|
||||||
|
use hex::{FromHex, ToHex};
|
||||||
|
|
||||||
use zebra_test::prelude::*;
|
use zebra_test::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -43,6 +45,15 @@ proptest! {
|
||||||
prop_assert_eq!(hash, parsed);
|
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]
|
#[test]
|
||||||
fn blockheader_roundtrip(header in any::<Header>()) {
|
fn blockheader_roundtrip(header in any::<Header>()) {
|
||||||
zebra_test::init();
|
zebra_test::init();
|
||||||
|
|
|
@ -137,7 +137,8 @@ impl FromHex for Hash {
|
||||||
type Error = <[u8; 32] as FromHex>::Error;
|
type Error = <[u8; 32] as FromHex>::Error;
|
||||||
|
|
||||||
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::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())
|
Ok(hash.into())
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ use std::io::Cursor;
|
||||||
|
|
||||||
use zebra_test::prelude::*;
|
use zebra_test::prelude::*;
|
||||||
|
|
||||||
|
use hex::{FromHex, ToHex};
|
||||||
|
|
||||||
use super::super::*;
|
use super::super::*;
|
||||||
|
|
||||||
use crate::{
|
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]
|
#[test]
|
||||||
fn transaction_auth_digest_struct_display_roundtrip(auth_digest in any::<AuthDigest>()) {
|
fn transaction_auth_digest_struct_display_roundtrip(auth_digest in any::<AuthDigest>()) {
|
||||||
zebra_test::init();
|
zebra_test::init();
|
||||||
|
|
Loading…
Reference in New Issue