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

Reverse displayed endianness of transaction and block hashes #1171

Merged
merged 8 commits into from
Oct 21, 2020
9 changes: 7 additions & 2 deletions zebra-chain/src/block/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ 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))
let mut reversed_bytes = self.0;
reversed_bytes.reverse();
f.write_str(&hex::encode(&reversed_bytes))
}
}

impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut reversed_bytes = self.0;
reversed_bytes.reverse();
f.debug_tuple("block::Hash")
.field(&hex::encode(&self.0))
.field(&hex::encode(&reversed_bytes))
.finish()
}
}
Expand Down Expand Up @@ -66,6 +70,7 @@ impl std::str::FromStr for Hash {
if hex::decode_to_slice(s, &mut bytes[..]).is_err() {
Err(SerializationError::Parse("hex decoding error"))
} else {
bytes.reverse();
Ok(Hash(bytes))
}
}
Expand Down
4 changes: 2 additions & 2 deletions zebra-chain/src/block/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn blockheaderhash_debug() {

assert_eq!(
format!("{:?}", hash),
"block::Hash(\"bf46b4b5030752fedac6f884976162bbfb29a9398f104a280b3e34d51b416631\")"
"block::Hash(\"3166411bd5343e0b284a108f39a929fbbb62619784f8c6dafe520703b5b446bf\")"
);
}

Expand All @@ -31,7 +31,7 @@ fn blockheaderhash_from_blockheader() {

assert_eq!(
format!("{:?}", hash),
"block::Hash(\"39c92b8c6b582797830827c78d58674c7205fcb21991887c124d1dbe4b97d6d1\")"
"block::Hash(\"d1d6974bbe1d4d127c889119b2fc05724c67588dc72708839727586b8c2bc939\")"
);

let mut bytes = Cursor::new(Vec::new());
Expand Down
8 changes: 4 additions & 4 deletions zebra-chain/src/parameters/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ pub const GENESIS_PREVIOUS_BLOCK_HASH: block::Hash = block::Hash([0; 32]);
/// Returns the hash for the genesis block in `network`.
pub fn genesis_hash(network: Network) -> block::Hash {
match network {
// zcash-cli getblockhash 0 | zebrad revhex
Network::Mainnet => "08ce3d9731b000c08338455c8a4a6bd05da16e26b11daa1b917184ece80f0400",
// zcash-cli -testnet getblockhash 0 | zebrad revhex
Network::Testnet => "382c4a332661c7ed0671f32a34d724619f086c61873bce7c99859dd9920aa605",
// zcash-cli getblockhash 0
Network::Mainnet => "00040fe8ec8471911baa1db1266ea15dd06b4a8a5c453883c000b031973dce08",
// zcash-cli -testnet getblockhash 0
Network::Testnet => "05a60a92d99d85997cce3b87616c089f6124d7342af37106edc76126334a2c38",
}
.parse()
.expect("hard-coded hash parses")
Expand Down
9 changes: 7 additions & 2 deletions zebra-chain/src/transaction/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ 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))
let mut reversed_bytes = self.0;
reversed_bytes.reverse();
f.write_str(&hex::encode(&reversed_bytes))
}
}

impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut reversed_bytes = self.0;
reversed_bytes.reverse();
f.debug_tuple("TransactionHash")
.field(&hex::encode(&self.0))
.field(&hex::encode(reversed_bytes))
.finish()
}
}
Expand All @@ -49,6 +53,7 @@ impl std::str::FromStr for Hash {
if hex::decode_to_slice(s, &mut bytes[..]).is_err() {
Err(SerializationError::Parse("hex decoding error"))
} else {
bytes.reverse();
Ok(Hash(bytes))
}
}
Expand Down
2 changes: 1 addition & 1 deletion zebra-chain/src/transaction/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn librustzcash_tx_hash() {

// TxID taken from comment in zebra_test::vectors
let hash = tx.hash();
let expected = "3956b54c11736f4ac5e2c474029cba8f5b83dca2e38f355337e20ce37fbdf064"
let expected = "64f0bd7fe30ce23753358fe3a2dc835b8fba9c0274c4e2c54a6f73114cb55639"
.parse::<Hash>()
.expect("hash should parse correctly");

Expand Down
2 changes: 1 addition & 1 deletion zebra-consensus/src/checkpoint/main-checkpoints.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
0 08ce3d9731b000c08338455c8a4a6bd05da16e26b11daa1b917184ece80f0400
0 00040fe8ec8471911baa1db1266ea15dd06b4a8a5c453883c000b031973dce08
2000 8c388b125a9612ebea9c70a5acc57d2f94d633faaf0ece2cec81339406000000
4000 c6504df5ff490ca933ed6d1dd9127bcc005b7f9418d057fab9a3770001000000
6000 f8e6ead5c80244ef07d46948594a490ba5b46e928e4ab705b2e92c4c02000000
Expand Down
2 changes: 1 addition & 1 deletion zebra-consensus/src/checkpoint/test-checkpoints.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
0 382c4a332661c7ed0671f32a34d724619f086c61873bce7c99859dd9920aa605
0 05a60a92d99d85997cce3b87616c089f6124d7342af37106edc76126334a2c38
2000 55c5f32055b79e4803e23645cb4b8739598dc58d931261efd8eb1b4e80330000
4000 c3e3d6371c2f347ec03587a8446b6b87ee75367cde1f774aa1f7bbfc1bb00000
6000 9067a7984b737656ea65892862a5aa0ac130c4b2a582af807a3710314ec90600
Expand Down
18 changes: 2 additions & 16 deletions zebra-utils/src/bin/zebra-checkpoints/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@ use std::os::unix::process::ExitStatusExt;

mod args;

/// Returns the hexadecimal-encoded string `s` in byte-reversed order.
pub fn byte_reverse_hex(s: &str) -> String {
String::from_utf8(
s.as_bytes()
.chunks(2)
.rev()
.map(|c| c.iter())
.flatten()
.cloned()
.collect::<Vec<u8>>(),
)
.expect("input should be ascii")
}

/// We limit the memory usage for each checkpoint, based on the cumulative size of
/// the serialized blocks in the chain. Deserialized blocks are larger, because
/// they contain pointers and non-compact integers. But they should be within a
Expand Down Expand Up @@ -139,7 +125,7 @@ fn main() -> Result<()> {
let v: Value = serde_json::from_str(&output)?;

// get the values we are interested in
let hash: block::Hash = v["hash"].as_str().map(byte_reverse_hex).unwrap().parse()?;
let hash: block::Hash = v["hash"].as_str().unwrap().parse()?;
let height = block::Height(v["height"].as_u64().unwrap() as u32);
assert!(height <= block::Height::MAX);
assert_eq!(x, height.0);
Expand All @@ -155,7 +141,7 @@ fn main() -> Result<()> {
|| height_gap.0 >= zebra_consensus::checkpoint::MAX_CHECKPOINT_HEIGHT_GAP as u32
{
// print to output
println!("{} {}", height.0, &hex::encode(hash.0),);
println!("{} {}", height.0, hash);

// reset counters
cumulative_bytes = 0;
Expand Down
9 changes: 2 additions & 7 deletions zebrad/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//! Zebrad Subcommands

mod generate;
mod revhex;
mod start;
mod version;

use self::ZebradCmd::*;
use self::{generate::GenerateCmd, revhex::RevhexCmd, start::StartCmd, version::VersionCmd};
use self::{generate::GenerateCmd, start::StartCmd, version::VersionCmd};

use crate::config::ZebradConfig;

Expand All @@ -29,10 +28,6 @@ pub enum ZebradCmd {
#[options(help = "get usage information")]
Help(Help<Self>),

/// The `revhex` subcommand
#[options(help = "reverses the endianness of a hex string, like a block or transaction hash")]
Revhex(RevhexCmd),

/// The `start` subcommand
#[options(help = "start the application")]
Start(StartCmd),
Expand All @@ -50,7 +45,7 @@ impl ZebradCmd {
match self {
// List all the commands, so new commands have to make a choice here
Start(_) => true,
Generate(_) | Help(_) | Revhex(_) | Version(_) => false,
Generate(_) | Help(_) | Version(_) => false,
}
}
}
Expand Down
49 changes: 0 additions & 49 deletions zebrad/src/commands/revhex.rs

This file was deleted.

15 changes: 0 additions & 15 deletions zebrad/tests/acceptance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,21 +207,6 @@ fn help_args() -> Result<()> {
Ok(())
}

#[test]
fn revhex_args() -> Result<()> {
zebra_test::init();
let testdir = testdir()?.with_config(default_test_config()?)?;

// Valid
let child = testdir.spawn_child(&["revhex", "33eeff55"])?;
let output = child.wait_with_output()?;
let output = output.assert_success()?;

output.stdout_equals("55ffee33\n")?;

Ok(())
}

#[test]
fn start_no_args() -> Result<()> {
zebra_test::init();
Expand Down