diff --git a/zebra-chain/Cargo.toml b/zebra-chain/Cargo.toml index 644e54aaa..f86196dd6 100644 --- a/zebra-chain/Cargo.toml +++ b/zebra-chain/Cargo.toml @@ -11,5 +11,6 @@ edition = "2018" thiserror = "1" byteorder = "1.3" chrono = "0.4" -#hex = "0.4" This conflicts with tracing-subscriber? +failure = "0.1" +hex = "0.4" sha2 = "0.8" diff --git a/zebra-chain/src/block.rs b/zebra-chain/src/block.rs index 6e9bb0576..07c4c6ab1 100644 --- a/zebra-chain/src/block.rs +++ b/zebra-chain/src/block.rs @@ -1,7 +1,8 @@ //! Definitions of block datastructures. use chrono::{DateTime, Utc}; -use std::io; +use hex; +use std::{fmt, io}; use crate::merkle_tree::MerkleTreeRootHash; use crate::note_commitment_tree::SaplingNoteTreeRootHash; @@ -21,9 +22,17 @@ use crate::transaction::Transaction; /// the direct bytes of the transactions as well as the header. So /// for now I want to call it a `BlockHeaderHash` because that's /// more explicit. -#[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Eq, PartialEq)] pub struct BlockHeaderHash(pub [u8; 32]); +impl fmt::Debug for BlockHeaderHash { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("BlockHeaderHash") + .field(&hex::encode(&self.0)) + .finish() + } +} + impl From for BlockHeaderHash { fn from(block_header: BlockHeader) -> Self { let mut hash_writer = Sha256dWriter::default(); @@ -141,3 +150,27 @@ impl ZcashDeserialize for Block { unimplemented!(); } } + +#[cfg(test)] +mod tests { + + use std::io::Write; + + use super::BlockHeaderHash; + + use crate::sha256d_writer::Sha256dWriter; + + #[test] + fn test_blockheaderhash_debug() { + let preimage = b"foo bar baz"; + let mut sha_writer = Sha256dWriter::default(); + let _ = sha_writer.write_all(preimage); + + let hash = BlockHeaderHash(sha_writer.finish()); + + assert_eq!( + format!("{:?}", hash), + "BlockHeaderHash(\"bf46b4b5030752fedac6f884976162bbfb29a9398f104a280b3e34d51b416631\")" + ); + } +} diff --git a/zebra-chain/src/sha256d_writer.rs b/zebra-chain/src/sha256d_writer.rs index 5e835cdfc..ca1d71668 100644 --- a/zebra-chain/src/sha256d_writer.rs +++ b/zebra-chain/src/sha256d_writer.rs @@ -27,6 +27,17 @@ impl Write for Sha256dWriter { Ok(buf.len()) } + fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> { + let mut length = 0; + + while length != buf.len() { + length += buf.len(); + self.hash.input(buf); + } + + Ok(()) + } + fn flush(&mut self) -> std::io::Result<()> { Ok(()) }