diff --git a/zebra-chain/src/block.rs b/zebra-chain/src/block.rs index cf474e45..1619f6d3 100644 --- a/zebra-chain/src/block.rs +++ b/zebra-chain/src/block.rs @@ -3,7 +3,7 @@ use chrono::{DateTime, Utc}; use std::io; -use crate::merkle_tree::MerkleTree; +use crate::merkle_tree::MerkleTreeRootHash; use crate::note_commitment_tree::SaplingNoteTreeRootHash; use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}; use crate::sha256d_writer::Sha256dWriter; @@ -26,33 +26,6 @@ impl From for BlockHeaderHash { } } -/// A SHA-256d hash of the root node of a merkle tree of SHA256-d -/// hashed transactions in a block. -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct MerkleRootHash([u8; 32]); - -impl ZcashSerialize for MerkleTree { - fn zcash_serialize(&self, writer: W) -> Result<(), SerializationError> { - unimplemented!(); - } -} - -impl ZcashDeserialize for MerkleTree { - fn zcash_deserialize(reader: R) -> Result { - unimplemented!(); - } -} - -impl From> for MerkleRootHash { - fn from(merkle_tree: MerkleTree) -> Self { - let mut hash_writer = Sha256dWriter::default(); - merkle_tree - .zcash_serialize(&mut hash_writer) - .expect("The merkle tree of transactions must serialize."); - Self(hash_writer.finish()) - } -} - /// Block header. /// /// How are blocks chained together? They are chained together via the @@ -78,7 +51,7 @@ pub struct BlockHeader { /// in this block as assembled in a binary tree, ensuring that /// none of those transactions can be modied without modifying the /// header. - merkle_root_hash: MerkleRootHash, + merkle_root_hash: MerkleTreeRootHash, /// [Sapling onward] The root LEBS2OSP256(rt) of the Sapling note /// commitment tree corresponding to the finnal Sapling treestate of @@ -114,13 +87,6 @@ pub struct BlockHeader { solution: Vec, } -impl BlockHeader { - /// Get the SHA-256d hash in internal byte order of this block header. - pub fn hash(&self) -> [u8; 32] { - unimplemented!(); - } -} - impl ZcashSerialize for BlockHeader { fn zcash_serialize(&self, writer: W) -> Result<(), SerializationError> { unimplemented!(); diff --git a/zebra-chain/src/equihash_solution.rs b/zebra-chain/src/equihash_solution.rs new file mode 100644 index 00000000..abdd2121 --- /dev/null +++ b/zebra-chain/src/equihash_solution.rs @@ -0,0 +1,31 @@ +//! Equihash Solution and related items. + +use std::fmt; + +use hex::ToHex; + +/// The size of an Equihash solution in bytes (always 1344). +const EQUIHASH_SOLUTION_SIZE = 1344; + +/// Equihash Solution. +/// +/// A wrapper around [u8; 1344] because Rust doesn't implement common +/// traits like `Debug`, `Clone`, etc for collections like array +/// beyond lengths 0 to 32. +/// +/// The size of an Equihash solution in bytes is always 1344 so the +/// length of this type is fixed. +#[derive(Clone)] +pub struct EquihashSolution([u8; EQUIHASH_SOLUTION_SIZE]); + +impl Default for EquihashSolution { + fn default() -> Self { + EquihashSolution([0; EQUIHASH_SOLUTION_SIZE]) + } +} + +impl fmt::Debug for EquihashSolution { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(&self.0.encode_hex::()) + } +} diff --git a/zebra-chain/src/merkle_tree.rs b/zebra-chain/src/merkle_tree.rs index d791578d..cd573bef 100644 --- a/zebra-chain/src/merkle_tree.rs +++ b/zebra-chain/src/merkle_tree.rs @@ -1,8 +1,14 @@ //! A binary hash tree of SHA256d (two rounds of SHA256) hashes for //! node values. +use std::io; + use sha2::Sha256; +use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}; +use crate::sha256d_writer::Sha256dWriter; +use crate::transaction::Transaction; + /// A binary hash tree of SHA256d (two rounds of SHA256) hashes for /// node values. #[derive(Default)] @@ -15,3 +21,30 @@ impl MerkleTree { unimplemented!(); } } + +impl ZcashSerialize for MerkleTree { + fn zcash_serialize(&self, writer: W) -> Result<(), SerializationError> { + unimplemented!(); + } +} + +impl ZcashDeserialize for MerkleTree { + fn zcash_deserialize(reader: R) -> Result { + unimplemented!(); + } +} + +/// A SHA-256d hash of the root node of a merkle tree of SHA256-d +/// hashed transactions in a block. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct MerkleTreeRootHash([u8; 32]); + +impl From> for MerkleTreeRootHash { + fn from(merkle_tree: MerkleTree) -> Self { + let mut hash_writer = Sha256dWriter::default(); + merkle_tree + .zcash_serialize(&mut hash_writer) + .expect("The merkle tree of transactions must serialize."); + Self(hash_writer.finish()) + } +} diff --git a/zebra-network/src/protocol/message.rs b/zebra-network/src/protocol/message.rs index 8ccb3af6..78a98e8b 100644 --- a/zebra-network/src/protocol/message.rs +++ b/zebra-network/src/protocol/message.rs @@ -4,7 +4,7 @@ use std::net; use chrono::{DateTime, Utc}; -use zebra_chain::block::{BlockHeaderHash, MerkleRootHash}; +use zebra_chain::block::Block; use zebra_chain::{transaction::Transaction, types::BlockHeight}; use crate::meta_addr::MetaAddr;