2020-08-15 16:12:26 -07:00
|
|
|
//! Blocks and block-related structures (heights, headers, etc.)
|
2020-08-16 18:31:28 -07:00
|
|
|
#![allow(clippy::unit_arg)]
|
2019-09-10 13:27:10 -07:00
|
|
|
|
2020-06-25 09:18:05 -07:00
|
|
|
mod hash;
|
|
|
|
mod header;
|
2020-08-14 23:51:41 -07:00
|
|
|
mod height;
|
2020-08-15 16:12:26 -07:00
|
|
|
mod root_hash;
|
2020-06-25 09:18:05 -07:00
|
|
|
mod serialize;
|
|
|
|
|
2020-08-15 20:57:59 -07:00
|
|
|
pub mod merkle;
|
|
|
|
|
2020-09-23 18:52:52 -07:00
|
|
|
#[cfg(any(test, feature = "proptest-impl"))]
|
|
|
|
mod arbitrary;
|
2020-01-31 21:27:51 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2020-01-31 18:02:02 -08:00
|
|
|
|
2020-10-07 20:07:32 -07:00
|
|
|
use std::fmt;
|
|
|
|
|
2020-08-15 23:20:01 -07:00
|
|
|
pub use hash::Hash;
|
2020-08-16 11:48:00 -07:00
|
|
|
pub use header::Header;
|
2020-08-16 11:42:02 -07:00
|
|
|
pub use height::Height;
|
2020-08-15 16:12:26 -07:00
|
|
|
pub use root_hash::RootHash;
|
2019-09-24 22:14:48 -07:00
|
|
|
|
2020-07-14 20:21:06 -07:00
|
|
|
/// The error type for Block checks.
|
2020-08-15 15:56:33 -07:00
|
|
|
// XXX try to remove this -- block checks should be done in zebra-consensus
|
|
|
|
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
|
2020-08-15 22:25:30 -07:00
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2020-08-17 01:24:33 -07:00
|
|
|
use crate::{parameters::Network, transaction::Transaction, transparent};
|
2020-08-15 22:25:30 -07:00
|
|
|
|
2020-08-16 12:08:24 -07:00
|
|
|
/// A Zcash block, containing a header and a list of transactions.
|
2020-08-15 22:25:30 -07:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Block {
|
|
|
|
/// The block header, containing block metadata.
|
2020-08-16 11:48:00 -07:00
|
|
|
pub header: Header,
|
2020-08-15 22:25:30 -07:00
|
|
|
/// The block transactions.
|
|
|
|
pub transactions: Vec<std::sync::Arc<Transaction>>,
|
|
|
|
}
|
|
|
|
|
2020-10-07 20:07:32 -07:00
|
|
|
impl fmt::Display for Block {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let mut fmter = f.debug_struct("Block");
|
|
|
|
if let Some(height) = self.coinbase_height() {
|
|
|
|
fmter.field("height", &height);
|
|
|
|
}
|
|
|
|
|
|
|
|
fmter.field("hash", &DisplayToDebug(self.hash())).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DisplayToDebug<T>(T);
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for DisplayToDebug<T>
|
|
|
|
where
|
|
|
|
T: fmt::Display,
|
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 22:25:30 -07:00
|
|
|
impl Block {
|
|
|
|
/// Return the block height reported in the coinbase transaction, if any.
|
2020-08-16 11:42:02 -07:00
|
|
|
pub fn coinbase_height(&self) -> Option<Height> {
|
2020-08-15 22:25:30 -07:00
|
|
|
self.transactions
|
|
|
|
.get(0)
|
|
|
|
.and_then(|tx| tx.inputs().get(0))
|
|
|
|
.and_then(|input| match input {
|
2020-08-17 01:24:33 -07:00
|
|
|
transparent::Input::Coinbase { ref height, .. } => Some(*height),
|
2020-08-15 22:25:30 -07:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-02 14:59:16 -07:00
|
|
|
/// Compute the hash of this block.
|
2020-08-15 23:20:01 -07:00
|
|
|
pub fn hash(&self) -> Hash {
|
|
|
|
Hash::from(self)
|
2020-08-15 22:25:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the parsed root hash for this block.
|
|
|
|
///
|
|
|
|
/// The interpretation of the root hash depends on the
|
|
|
|
/// configured `network`, and this block's height.
|
|
|
|
///
|
|
|
|
/// Returns None if this block does not have a block height.
|
|
|
|
pub fn root_hash(&self, network: Network) -> Option<RootHash> {
|
|
|
|
match self.coinbase_height() {
|
|
|
|
Some(height) => Some(RootHash::from_bytes(
|
|
|
|
self.header.root_bytes,
|
|
|
|
network,
|
|
|
|
height,
|
|
|
|
)),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 23:20:01 -07:00
|
|
|
impl<'a> From<&'a Block> for Hash {
|
|
|
|
fn from(block: &'a Block) -> Hash {
|
2020-08-15 22:25:30 -07:00
|
|
|
(&block.header).into()
|
|
|
|
}
|
|
|
|
}
|