chain: rename BlockHeader to block::Header

This commit is contained in:
Henry de Valence 2020-08-16 11:48:00 -07:00
parent 103b663c40
commit 2712c4b72a
12 changed files with 34 additions and 33 deletions

View File

@ -12,7 +12,7 @@ pub mod merkle;
mod tests; mod tests;
pub use hash::Hash; pub use hash::Hash;
pub use header::BlockHeader; pub use header::Header;
pub use height::Height; pub use height::Height;
pub use root_hash::RootHash; pub use root_hash::RootHash;
@ -38,7 +38,7 @@ use proptest_derive::Arbitrary;
#[cfg_attr(test, derive(Arbitrary))] #[cfg_attr(test, derive(Arbitrary))]
pub struct Block { pub struct Block {
/// The block header, containing block metadata. /// The block header, containing block metadata.
pub header: BlockHeader, pub header: Header,
/// The block transactions. /// The block transactions.
pub transactions: Vec<std::sync::Arc<Transaction>>, pub transactions: Vec<std::sync::Arc<Transaction>>,
} }

View File

@ -8,7 +8,7 @@ use crate::serialization::{
sha256d, ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize, sha256d, ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize,
}; };
use super::BlockHeader; use super::Header;
/// A SHA-256d hash of a BlockHeader. /// A SHA-256d hash of a BlockHeader.
/// ///
@ -34,8 +34,8 @@ impl fmt::Debug for Hash {
} }
} }
impl<'a> From<&'a BlockHeader> for Hash { impl<'a> From<&'a Header> for Hash {
fn from(block_header: &'a BlockHeader) -> Self { fn from(block_header: &'a Header) -> Self {
let mut hash_writer = sha256d::Writer::default(); let mut hash_writer = sha256d::Writer::default();
block_header block_header
.zcash_serialize(&mut hash_writer) .zcash_serialize(&mut hash_writer)

View File

@ -12,7 +12,7 @@ use super::{merkle::MerkleTreeRootHash, Error, Hash};
/// header. Each block points backwards to its parent, all the way /// header. Each block points backwards to its parent, all the way
/// back to the genesis block (the first block in the blockchain). /// back to the genesis block (the first block in the blockchain).
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct BlockHeader { pub struct Header {
/// The block's version field. This is supposed to be `4`: /// The block's version field. This is supposed to be `4`:
/// ///
/// > The current and only defined block version number for Zcash is 4. /// > The current and only defined block version number for Zcash is 4.
@ -66,7 +66,7 @@ pub struct BlockHeader {
pub solution: Solution, pub solution: Solution,
} }
impl BlockHeader { impl Header {
/// Returns true if the header is valid based on its `EquihashSolution` /// Returns true if the header is valid based on its `EquihashSolution`
pub fn is_equihash_solution_valid(&self) -> Result<(), EquihashError> { pub fn is_equihash_solution_valid(&self) -> Result<(), EquihashError> {
let n = 200; let n = 200;

View File

@ -8,8 +8,8 @@ use crate::work::{difficulty::CompactDifficulty, equihash};
use super::merkle::MerkleTreeRootHash; use super::merkle::MerkleTreeRootHash;
use super::Block; use super::Block;
use super::BlockHeader;
use super::Hash; use super::Hash;
use super::Header;
/// The maximum size of a Zcash block, in bytes. /// The maximum size of a Zcash block, in bytes.
/// ///
@ -19,7 +19,7 @@ use super::Hash;
/// transaction in the chain is approximately 1.5 kB smaller.) /// transaction in the chain is approximately 1.5 kB smaller.)
pub const MAX_BLOCK_BYTES: u64 = 2_000_000; pub const MAX_BLOCK_BYTES: u64 = 2_000_000;
impl ZcashSerialize for BlockHeader { 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)?;
@ -36,7 +36,7 @@ impl ZcashSerialize for BlockHeader {
} }
} }
impl ZcashDeserialize for BlockHeader { impl ZcashDeserialize for Header {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> { fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
// The Zcash specification says that // The Zcash specification says that
// "The current and only defined block version number for Zcash is 4." // "The current and only defined block version number for Zcash is 4."
@ -68,7 +68,7 @@ impl ZcashDeserialize for BlockHeader {
return Err(SerializationError::Parse("version must be at least 4")); return Err(SerializationError::Parse("version must be at least 4"));
} }
Ok(BlockHeader { 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_hash: MerkleTreeRootHash(reader.read_32_bytes()?),

View File

@ -23,7 +23,7 @@ impl Arbitrary for RootHash {
type Strategy = BoxedStrategy<Self>; type Strategy = BoxedStrategy<Self>;
} }
impl Arbitrary for BlockHeader { impl Arbitrary for Header {
type Parameters = (); type Parameters = ();
fn arbitrary_with(_args: ()) -> Self::Strategy { fn arbitrary_with(_args: ()) -> Self::Strategy {
@ -49,7 +49,7 @@ impl Arbitrary for BlockHeader {
difficulty_threshold, difficulty_threshold,
nonce, nonce,
solution, solution,
)| BlockHeader { )| Header {
version, version,
previous_block_hash, previous_block_hash,
merkle_root_hash, merkle_root_hash,

View File

@ -7,11 +7,11 @@ use crate::{
transaction::{LockTime, Transaction, TransparentInput, TransparentOutput}, transaction::{LockTime, Transaction, TransparentInput, TransparentOutput},
}; };
use super::super::{serialize::MAX_BLOCK_BYTES, Block, BlockHeader}; use super::super::{serialize::MAX_BLOCK_BYTES, Block, Header};
/// Generate a block header /// Generate a block header
pub fn block_header() -> BlockHeader { pub fn block_header() -> Header {
BlockHeader::zcash_deserialize(&zebra_test::vectors::DUMMY_HEADER[..]).unwrap() Header::zcash_deserialize(&zebra_test::vectors::DUMMY_HEADER[..]).unwrap()
} }
/// Generate a block with multiple transactions just below limit /// Generate a block with multiple transactions just below limit

View File

@ -18,7 +18,7 @@ proptest! {
} }
#[test] #[test]
fn blockheader_roundtrip(header in any::<BlockHeader>()) { fn blockheader_roundtrip(header in any::<Header>()) {
let bytes = header.zcash_serialize_to_vec()?; let bytes = header.zcash_serialize_to_vec()?;
let other_header = bytes.zcash_deserialize_into()?; let other_header = bytes.zcash_deserialize_into()?;

View File

@ -54,7 +54,7 @@ fn blockheaderhash_from_blockheader() {
fn deserialize_blockheader() { fn deserialize_blockheader() {
// https://explorer.zcha.in/blocks/415000 // https://explorer.zcha.in/blocks/415000
let _header = zebra_test::vectors::HEADER_MAINNET_415000_BYTES let _header = zebra_test::vectors::HEADER_MAINNET_415000_BYTES
.zcash_deserialize_into::<BlockHeader>() .zcash_deserialize_into::<Header>()
.expect("blockheader test vector should deserialize"); .expect("blockheader test vector should deserialize");
} }

View File

@ -1,6 +1,6 @@
use proptest::prelude::*; use proptest::prelude::*;
use crate::block::{Block, BlockHeader}; use crate::block::{self, Block};
use crate::serialization::{ZcashDeserialize, ZcashDeserializeInto, ZcashSerialize}; use crate::serialization::{ZcashDeserialize, ZcashDeserializeInto, ZcashSerialize};
use super::super::*; use super::super::*;
@ -20,12 +20,12 @@ fn equihash_solution_roundtrip() {
} }
prop_compose! { prop_compose! {
fn randomized_solutions(real_header: BlockHeader) fn randomized_solutions(real_header: block::Header)
(fake_solution in any::<equihash::Solution>() (fake_solution in any::<equihash::Solution>()
.prop_filter("solution must not be the actual solution", move |s| { .prop_filter("solution must not be the actual solution", move |s| {
s != &real_header.solution s != &real_header.solution
}) })
) -> BlockHeader { ) -> block::Header {
let mut fake_header = real_header; let mut fake_header = real_header;
fake_header.solution = fake_solution; fake_header.solution = fake_solution;
fake_header fake_header
@ -52,12 +52,12 @@ fn equihash_prop_test_solution() -> color_eyre::eyre::Result<()> {
} }
prop_compose! { prop_compose! {
fn randomized_nonce(real_header: BlockHeader) fn randomized_nonce(real_header: block::Header)
(fake_nonce in proptest::array::uniform32(any::<u8>()) (fake_nonce in proptest::array::uniform32(any::<u8>())
.prop_filter("nonce must not be the actual nonce", move |fake_nonce| { .prop_filter("nonce must not be the actual nonce", move |fake_nonce| {
fake_nonce != &real_header.nonce fake_nonce != &real_header.nonce
}) })
) -> BlockHeader { ) -> block::Header {
let mut fake_header = real_header; let mut fake_header = real_header;
fake_header.nonce = fake_nonce; fake_header.nonce = fake_nonce;
fake_header fake_header
@ -84,8 +84,8 @@ fn equihash_prop_test_nonce() -> color_eyre::eyre::Result<()> {
} }
prop_compose! { prop_compose! {
fn randomized_input(real_header: BlockHeader) fn randomized_input(real_header: block::Header)
(fake_header in any::<BlockHeader>() (fake_header in any::<block::Header>()
.prop_map(move |mut fake_header| { .prop_map(move |mut fake_header| {
fake_header.nonce = real_header.nonce; fake_header.nonce = real_header.nonce;
fake_header.solution = real_header.solution; fake_header.solution = real_header.solution;
@ -94,7 +94,7 @@ prop_compose! {
.prop_filter("input must not be the actual input", move |fake_header| { .prop_filter("input must not be the actual input", move |fake_header| {
fake_header != &real_header fake_header != &real_header
}) })
) -> BlockHeader { ) -> block::Header {
fake_header fake_header
} }
} }

View File

@ -6,7 +6,6 @@ use chrono::Utc;
use color_eyre::eyre::{eyre, Report}; use color_eyre::eyre::{eyre, Report};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use zebra_chain::block::BlockHeader;
use zebra_chain::block::{self, Block}; use zebra_chain::block::{self, Block};
use zebra_chain::serialization::{ZcashDeserialize, ZcashDeserializeInto}; use zebra_chain::serialization::{ZcashDeserialize, ZcashDeserializeInto};
use zebra_test::transcript::{TransError, Transcript}; use zebra_test::transcript::{TransError, Transcript};
@ -55,7 +54,7 @@ static INVALID_HEADER_SOLUTION_TRANSCRIPT: Lazy<
static INVALID_COINBASE_TRANSCRIPT: Lazy<Vec<(Arc<Block>, Result<block::Hash, TransError>)>> = static INVALID_COINBASE_TRANSCRIPT: Lazy<Vec<(Arc<Block>, Result<block::Hash, TransError>)>> =
Lazy::new(|| { Lazy::new(|| {
let header = let header =
BlockHeader::zcash_deserialize(&zebra_test::vectors::DUMMY_HEADER[..]).unwrap(); block::Header::zcash_deserialize(&zebra_test::vectors::DUMMY_HEADER[..]).unwrap();
// Test 1: Empty transaction // Test 1: Empty transaction
let block1 = Block { let block1 = Block {

View File

@ -11,7 +11,7 @@ use tower::{layer::Layer, timeout::TimeoutLayer, Service, ServiceExt};
use tracing_futures::Instrument; use tracing_futures::Instrument;
use zebra_chain::{ use zebra_chain::{
block::{self, Block, BlockHeader}, block::{self, Block},
parameters::Network, parameters::Network,
serialization::ZcashDeserialize, serialization::ZcashDeserialize,
}; };
@ -39,7 +39,7 @@ const VERIFY_TIMEOUT_SECONDS: u64 = 10;
/// The generated block should fail validation. /// The generated block should fail validation.
pub fn block_no_transactions() -> Block { pub fn block_no_transactions() -> Block {
Block { Block {
header: BlockHeader::zcash_deserialize(&zebra_test::vectors::DUMMY_HEADER[..]).unwrap(), header: block::Header::zcash_deserialize(&zebra_test::vectors::DUMMY_HEADER[..]).unwrap(),
transactions: Vec::new(), transactions: Vec::new(),
} }
} }

View File

@ -5,8 +5,10 @@ use std::{net, sync::Arc};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use zebra_chain::block::{self, Block}; use zebra_chain::{
use zebra_chain::{block::BlockHeader, transaction::Transaction}; block::{self, Block},
transaction::Transaction,
};
use super::inv::InventoryHash; use super::inv::InventoryHash;
use super::types::*; use super::types::*;
@ -178,7 +180,7 @@ pub enum Message {
// transaction count (a var_int, so there can be more than 81 // transaction count (a var_int, so there can be more than 81
// bytes per header) as opposed to the block headers that are // bytes per header) as opposed to the block headers that are
// hashed by miners. // hashed by miners.
Headers(Vec<BlockHeader>), Headers(Vec<block::Header>),
/// A `getheaders` message. /// A `getheaders` message.
/// ///