2019-09-10 13:27:10 -07:00
|
|
|
|
//! Definitions of block datastructures.
|
|
|
|
|
|
2019-09-24 11:16:41 -07:00
|
|
|
|
use chrono::{DateTime, Utc};
|
2019-10-15 15:51:53 -07:00
|
|
|
|
use hex;
|
|
|
|
|
use std::{fmt, io};
|
2019-09-24 22:14:48 -07:00
|
|
|
|
|
2019-09-26 19:10:39 -07:00
|
|
|
|
use crate::merkle_tree::MerkleTreeRootHash;
|
2019-09-25 23:23:04 -07:00
|
|
|
|
use crate::note_commitment_tree::SaplingNoteTreeRootHash;
|
2019-10-08 16:39:35 -07:00
|
|
|
|
use crate::serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize};
|
2019-09-24 22:14:48 -07:00
|
|
|
|
use crate::sha256d_writer::Sha256dWriter;
|
|
|
|
|
use crate::transaction::Transaction;
|
|
|
|
|
|
|
|
|
|
/// A SHA-256d hash of a BlockHeader.
|
|
|
|
|
///
|
|
|
|
|
/// This is useful when one block header is pointing to its parent
|
|
|
|
|
/// block header in the block chain. ⛓️
|
2019-09-26 19:42:44 -07:00
|
|
|
|
///
|
|
|
|
|
/// This is usually called a 'block hash', as it is frequently used
|
|
|
|
|
/// to identify the entire block, since the hash preimage includes
|
|
|
|
|
/// the merkle root of the transactions in this block. But
|
|
|
|
|
/// _technically_, this is just a hash of the block _header_, not
|
|
|
|
|
/// the direct bytes of the transactions as well as the header. So
|
|
|
|
|
/// for now I want to call it a `BlockHeaderHash` because that's
|
|
|
|
|
/// more explicit.
|
2019-10-15 15:51:53 -07:00
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq)]
|
2019-09-27 18:06:22 -07:00
|
|
|
|
pub struct BlockHeaderHash(pub [u8; 32]);
|
2019-09-24 22:14:48 -07:00
|
|
|
|
|
2019-10-15 15:51:53 -07:00
|
|
|
|
impl fmt::Debug for BlockHeaderHash {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
f.debug_tuple("BlockHeaderHash")
|
|
|
|
|
.field(&hex::encode(&self.0))
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 16:33:12 -07:00
|
|
|
|
impl From<BlockHeader> for BlockHeaderHash {
|
2019-09-24 22:14:48 -07:00
|
|
|
|
fn from(block_header: BlockHeader) -> Self {
|
|
|
|
|
let mut hash_writer = Sha256dWriter::default();
|
|
|
|
|
block_header
|
|
|
|
|
.zcash_serialize(&mut hash_writer)
|
|
|
|
|
.expect("Block headers must serialize.");
|
|
|
|
|
Self(hash_writer.finish())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 16:39:35 -07:00
|
|
|
|
impl ZcashSerialize for BlockHeaderHash {
|
|
|
|
|
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), SerializationError> {
|
|
|
|
|
writer.write_all(&self.0)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ZcashDeserialize for BlockHeaderHash {
|
|
|
|
|
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
|
|
|
|
|
let bytes = reader.read_32_bytes()?;
|
|
|
|
|
Ok(BlockHeaderHash(bytes))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 18:25:46 -07:00
|
|
|
|
/// Block header.
|
|
|
|
|
///
|
|
|
|
|
/// How are blocks chained together? They are chained together via the
|
|
|
|
|
/// backwards reference (previous header hash) present in the block
|
|
|
|
|
/// header. Each block points backwards to its parent, all the way
|
|
|
|
|
/// back to the genesis block (the first block in the blockchain).
|
2019-09-25 23:28:58 -07:00
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2019-09-24 11:16:41 -07:00
|
|
|
|
pub struct BlockHeader {
|
|
|
|
|
/// A SHA-256d hash in internal byte order of the previous block’s
|
|
|
|
|
/// header. This ensures no previous block can be changed without
|
2019-09-25 16:33:12 -07:00
|
|
|
|
/// also changing this block’s header.
|
|
|
|
|
previous_block_hash: BlockHeaderHash,
|
2019-09-24 11:16:41 -07:00
|
|
|
|
|
|
|
|
|
/// A SHA-256d hash in internal byte order. The merkle root is
|
2019-09-24 22:14:48 -07:00
|
|
|
|
/// derived from the SHA256d hashes of all transactions included
|
|
|
|
|
/// in this block as assembled in a binary tree, ensuring that
|
|
|
|
|
/// none of those transactions can be modied without modifying the
|
|
|
|
|
/// header.
|
2019-09-26 19:10:39 -07:00
|
|
|
|
merkle_root_hash: MerkleTreeRootHash,
|
2019-09-24 11:16:41 -07:00
|
|
|
|
|
|
|
|
|
/// [Sapling onward] The root LEBS2OSP256(rt) of the Sapling note
|
2019-09-25 23:23:04 -07:00
|
|
|
|
/// commitment tree corresponding to the finnal Sapling treestate of
|
2019-09-24 11:16:41 -07:00
|
|
|
|
/// this block.
|
2019-09-25 23:23:04 -07:00
|
|
|
|
final_sapling_root_hash: SaplingNoteTreeRootHash,
|
2019-09-24 11:16:41 -07:00
|
|
|
|
|
|
|
|
|
/// The block timestamp is a Unix epoch time (UTC) when the miner
|
|
|
|
|
/// started hashing the header (according to the miner).
|
|
|
|
|
time: DateTime<Utc>,
|
|
|
|
|
|
|
|
|
|
/// An encoded version of the target threshold this block’s header
|
|
|
|
|
/// hash must be less than or equal to, in the same nBits format
|
|
|
|
|
/// used by Bitcoin.
|
|
|
|
|
///
|
|
|
|
|
/// For a block at block height height, bits MUST be equal to
|
|
|
|
|
/// ThresholdBits(height).
|
|
|
|
|
///
|
|
|
|
|
/// [Bitcoin-nBits](https://bitcoin.org/en/developer-reference#target-nbits)
|
|
|
|
|
// pzec has their own wrapper around u32 for this field:
|
|
|
|
|
// https://github.com/ZcashFoundation/zebra/blob/master/zebra-primitives/src/compact.rs
|
|
|
|
|
bits: u32,
|
|
|
|
|
|
|
|
|
|
/// An arbitrary field that miners can change to modify the header
|
|
|
|
|
/// hash in order to produce a hash less than or equal to the
|
|
|
|
|
/// target threshold.
|
|
|
|
|
nonce: [u8; 32],
|
|
|
|
|
|
|
|
|
|
/// The Equihash solution.
|
2019-09-26 00:01:49 -07:00
|
|
|
|
// The solution size when serialized should be in bytes ('always
|
|
|
|
|
// 1344'). I first tried this as a [u8; 1344] but until const
|
|
|
|
|
// generics land we'd have to implement all our common traits
|
|
|
|
|
// manually, like in pzec.
|
|
|
|
|
solution: Vec<u8>,
|
2019-09-24 11:16:41 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-24 22:14:48 -07:00
|
|
|
|
impl ZcashSerialize for BlockHeader {
|
2019-09-30 11:21:53 -07:00
|
|
|
|
fn zcash_serialize<W: io::Write>(&self, _writer: W) -> Result<(), SerializationError> {
|
2019-09-24 22:14:48 -07:00
|
|
|
|
unimplemented!();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ZcashDeserialize for BlockHeader {
|
2019-09-30 11:21:53 -07:00
|
|
|
|
fn zcash_deserialize<R: io::Read>(_reader: R) -> Result<Self, SerializationError> {
|
2019-09-24 22:14:48 -07:00
|
|
|
|
unimplemented!();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-10 13:27:10 -07:00
|
|
|
|
/// A block in your blockchain.
|
2019-09-25 18:25:46 -07:00
|
|
|
|
///
|
|
|
|
|
/// A block is a data structure with two fields:
|
|
|
|
|
///
|
|
|
|
|
/// Block header: a data structure containing the block's metadata
|
|
|
|
|
/// Transactions: an array (vector in Rust) of transactions
|
2019-09-25 23:28:58 -07:00
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2019-09-24 11:16:41 -07:00
|
|
|
|
pub struct Block {
|
|
|
|
|
/// First 80 bytes of the block as defined by the encoding used by
|
2019-09-24 22:14:48 -07:00
|
|
|
|
/// "block" messages.
|
2019-09-24 11:16:41 -07:00
|
|
|
|
pub header: BlockHeader,
|
2019-09-10 13:27:10 -07:00
|
|
|
|
|
2019-09-25 16:33:12 -07:00
|
|
|
|
/// The block transactions.
|
2019-09-24 11:16:41 -07:00
|
|
|
|
pub transactions: Vec<Transaction>,
|
|
|
|
|
}
|
2019-09-26 19:38:18 -07:00
|
|
|
|
|
|
|
|
|
impl ZcashSerialize for Block {
|
2019-09-30 11:21:53 -07:00
|
|
|
|
fn zcash_serialize<W: io::Write>(&self, _writer: W) -> Result<(), SerializationError> {
|
2019-09-26 19:38:18 -07:00
|
|
|
|
unimplemented!();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ZcashDeserialize for Block {
|
2019-09-30 11:21:53 -07:00
|
|
|
|
fn zcash_deserialize<R: io::Read>(_reader: R) -> Result<Self, SerializationError> {
|
2019-09-26 19:38:18 -07:00
|
|
|
|
unimplemented!();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-15 15:51:53 -07:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
|
|
use super::BlockHeaderHash;
|
|
|
|
|
|
|
|
|
|
use crate::sha256d_writer::Sha256dWriter;
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-15 15:59:55 -07:00
|
|
|
|
fn blockheaderhash_debug() {
|
2019-10-15 15:51:53 -07:00
|
|
|
|
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\")"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|