Implement custom Debug impl for BlockHeaderHash
Includes a testcase and write_all implementation for Sha256dWriter. Related to #63
This commit is contained in:
parent
f6e62b0f5e
commit
539a16979b
|
@ -11,5 +11,6 @@ edition = "2018"
|
||||||
thiserror = "1"
|
thiserror = "1"
|
||||||
byteorder = "1.3"
|
byteorder = "1.3"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
#hex = "0.4" This conflicts with tracing-subscriber?
|
failure = "0.1"
|
||||||
|
hex = "0.4"
|
||||||
sha2 = "0.8"
|
sha2 = "0.8"
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
//! Definitions of block datastructures.
|
//! Definitions of block datastructures.
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use std::io;
|
use hex;
|
||||||
|
use std::{fmt, io};
|
||||||
|
|
||||||
use crate::merkle_tree::MerkleTreeRootHash;
|
use crate::merkle_tree::MerkleTreeRootHash;
|
||||||
use crate::note_commitment_tree::SaplingNoteTreeRootHash;
|
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
|
/// the direct bytes of the transactions as well as the header. So
|
||||||
/// for now I want to call it a `BlockHeaderHash` because that's
|
/// for now I want to call it a `BlockHeaderHash` because that's
|
||||||
/// more explicit.
|
/// more explicit.
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||||
pub struct BlockHeaderHash(pub [u8; 32]);
|
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<BlockHeader> for BlockHeaderHash {
|
impl From<BlockHeader> for BlockHeaderHash {
|
||||||
fn from(block_header: BlockHeader) -> Self {
|
fn from(block_header: BlockHeader) -> Self {
|
||||||
let mut hash_writer = Sha256dWriter::default();
|
let mut hash_writer = Sha256dWriter::default();
|
||||||
|
@ -141,3 +150,27 @@ impl ZcashDeserialize for Block {
|
||||||
unimplemented!();
|
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\")"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -27,6 +27,17 @@ impl Write for Sha256dWriter {
|
||||||
Ok(buf.len())
|
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<()> {
|
fn flush(&mut self) -> std::io::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue