chain: rename to block::merkle::{Root, Tree}.

This commit is contained in:
Henry de Valence 2020-08-16 11:54:38 -07:00
parent 2712c4b72a
commit 94d6d448bb
4 changed files with 15 additions and 20 deletions

View File

@ -3,7 +3,7 @@ use chrono::{DateTime, Duration, Utc};
use crate::serialization::ZcashSerialize; use crate::serialization::ZcashSerialize;
use crate::work::{difficulty::CompactDifficulty, equihash::Solution}; use crate::work::{difficulty::CompactDifficulty, equihash::Solution};
use super::{merkle::MerkleTreeRootHash, Error, Hash}; use super::{merkle, Error, Hash};
/// Block header. /// Block header.
/// ///
@ -33,7 +33,7 @@ pub struct Header {
/// in this block as assembled in a binary tree, ensuring that /// in this block as assembled in a binary tree, ensuring that
/// none of those transactions can be modied without modifying the /// none of those transactions can be modied without modifying the
/// header. /// header.
pub merkle_root_hash: MerkleTreeRootHash, pub merkle_root: merkle::Root,
/// Some kind of root hash. /// Some kind of root hash.
/// ///

View File

@ -13,17 +13,17 @@ use crate::transaction::Transaction;
/// A binary hash tree of SHA256d (two rounds of SHA256) hashes for /// A binary hash tree of SHA256d (two rounds of SHA256) hashes for
/// node values. /// node values.
#[derive(Default)] #[derive(Default)]
pub struct MerkleTree<T> { pub struct Tree<T> {
_leaves: Vec<T>, _leaves: Vec<T>,
} }
impl<Transaction> ZcashSerialize for MerkleTree<Transaction> { impl<Transaction> ZcashSerialize for Tree<Transaction> {
fn zcash_serialize<W: io::Write>(&self, _writer: W) -> Result<(), io::Error> { fn zcash_serialize<W: io::Write>(&self, _writer: W) -> Result<(), io::Error> {
unimplemented!(); unimplemented!();
} }
} }
impl<Transaction> ZcashDeserialize for MerkleTree<Transaction> { impl<Transaction> ZcashDeserialize for Tree<Transaction> {
fn zcash_deserialize<R: io::Read>(_reader: R) -> Result<Self, SerializationError> { fn zcash_deserialize<R: io::Read>(_reader: R) -> Result<Self, SerializationError> {
unimplemented!(); unimplemented!();
} }
@ -33,10 +33,10 @@ impl<Transaction> ZcashDeserialize for MerkleTree<Transaction> {
/// hashed transactions in a block. /// hashed transactions in a block.
#[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize)] #[derive(Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(test, derive(Arbitrary))] #[cfg_attr(test, derive(Arbitrary))]
pub struct MerkleTreeRootHash(pub [u8; 32]); pub struct Root(pub [u8; 32]);
impl From<MerkleTree<Transaction>> for MerkleTreeRootHash { impl From<Tree<Transaction>> for Root {
fn from(merkle_tree: MerkleTree<Transaction>) -> Self { fn from(merkle_tree: Tree<Transaction>) -> Self {
let mut hash_writer = sha256d::Writer::default(); let mut hash_writer = sha256d::Writer::default();
merkle_tree merkle_tree
.zcash_serialize(&mut hash_writer) .zcash_serialize(&mut hash_writer)
@ -45,10 +45,8 @@ impl From<MerkleTree<Transaction>> for MerkleTreeRootHash {
} }
} }
impl fmt::Debug for MerkleTreeRootHash { impl fmt::Debug for Root {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("MerkleTreeRootHash") f.debug_tuple("Root").field(&hex::encode(&self.0)).finish()
.field(&hex::encode(&self.0))
.finish()
} }
} }

View File

@ -6,10 +6,7 @@ use crate::serialization::ZcashDeserializeInto;
use crate::serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize}; use crate::serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize};
use crate::work::{difficulty::CompactDifficulty, equihash}; use crate::work::{difficulty::CompactDifficulty, equihash};
use super::merkle::MerkleTreeRootHash; use super::{merkle, Block, Hash, Header};
use super::Block;
use super::Hash;
use super::Header;
/// The maximum size of a Zcash block, in bytes. /// The maximum size of a Zcash block, in bytes.
/// ///
@ -23,7 +20,7 @@ impl ZcashSerialize for Header {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> { fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
writer.write_u32::<LittleEndian>(self.version)?; writer.write_u32::<LittleEndian>(self.version)?;
self.previous_block_hash.zcash_serialize(&mut writer)?; self.previous_block_hash.zcash_serialize(&mut writer)?;
writer.write_all(&self.merkle_root_hash.0[..])?; writer.write_all(&self.merkle_root.0[..])?;
writer.write_all(&self.root_bytes[..])?; writer.write_all(&self.root_bytes[..])?;
// this is a truncating cast, rather than a saturating cast // this is a truncating cast, rather than a saturating cast
// but u32 times are valid until 2106, and our block verification time // but u32 times are valid until 2106, and our block verification time
@ -71,7 +68,7 @@ impl ZcashDeserialize for Header {
Ok(Header { Ok(Header {
version, version,
previous_block_hash: Hash::zcash_deserialize(&mut reader)?, previous_block_hash: Hash::zcash_deserialize(&mut reader)?,
merkle_root_hash: MerkleTreeRootHash(reader.read_32_bytes()?), merkle_root: merkle::Root(reader.read_32_bytes()?),
root_bytes: reader.read_32_bytes()?, root_bytes: reader.read_32_bytes()?,
// This can't panic, because all u32 values are valid `Utc.timestamp`s // This can't panic, because all u32 values are valid `Utc.timestamp`s
time: Utc.timestamp(reader.read_u32::<LittleEndian>()? as i64, 0), time: Utc.timestamp(reader.read_u32::<LittleEndian>()? as i64, 0),

View File

@ -31,7 +31,7 @@ impl Arbitrary for Header {
// version is interpreted as i32 in the spec, so we are limited to i32::MAX here // version is interpreted as i32 in the spec, so we are limited to i32::MAX here
(4u32..(i32::MAX as u32)), (4u32..(i32::MAX as u32)),
any::<Hash>(), any::<Hash>(),
any::<merkle::MerkleTreeRootHash>(), any::<merkle::Root>(),
any::<[u8; 32]>(), any::<[u8; 32]>(),
// time is interpreted as u32 in the spec, but rust timestamps are i64 // time is interpreted as u32 in the spec, but rust timestamps are i64
(0i64..(u32::MAX as i64)), (0i64..(u32::MAX as i64)),
@ -52,7 +52,7 @@ impl Arbitrary for Header {
)| Header { )| Header {
version, version,
previous_block_hash, previous_block_hash,
merkle_root_hash, merkle_root: merkle_root_hash,
root_bytes, root_bytes,
time: Utc.timestamp(timestamp, 0), time: Utc.timestamp(timestamp, 0),
difficulty_threshold, difficulty_threshold,