Move the block hash types to primitives

Move the `BlockHash` and `WitnessCommitment` hash types over to
`primitives`.
This commit is contained in:
Tobin C. Harding 2024-09-23 13:56:04 +10:00
parent 6b9429ac7b
commit 2d8c613340
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
3 changed files with 30 additions and 13 deletions

View File

@ -23,18 +23,11 @@ use crate::script::{self, ScriptExt as _};
use crate::transaction::{Transaction, Wtxid}; use crate::transaction::{Transaction, Wtxid};
use crate::VarInt; use crate::VarInt;
hashes::hash_newtype! { #[rustfmt::skip] // Keep public re-exports separate.
/// A bitcoin block hash. #[doc(inline)]
pub struct BlockHash(sha256d::Hash); pub use primitives::block::*;
/// A hash corresponding to the witness structure commitment in the coinbase transaction.
pub struct WitnessCommitment(sha256d::Hash);
}
impl_hashencode!(BlockHash);
impl BlockHash { impl_hashencode!(BlockHash);
/// Dummy hash used as the previous blockhash of the genesis block.
pub const GENESIS_PREVIOUS_BLOCK_HASH: Self = Self::from_byte_array([0; 32]);
}
/// Bitcoin block header. /// Bitcoin block header.
/// ///
@ -74,7 +67,7 @@ impl Header {
pub fn block_hash(&self) -> BlockHash { pub fn block_hash(&self) -> BlockHash {
let mut engine = sha256d::Hash::engine(); let mut engine = sha256d::Hash::engine();
self.consensus_encode(&mut engine).expect("engines don't error"); self.consensus_encode(&mut engine).expect("engines don't error");
BlockHash(sha256d::Hash::from_engine(engine)) BlockHash::from_byte_array(sha256d::Hash::from_engine(engine).to_byte_array())
} }
/// Computes the target (range [0, T] inclusive) that a blockhash must land in to be valid. /// Computes the target (range [0, T] inclusive) that a blockhash must land in to be valid.
@ -296,7 +289,7 @@ impl Block {
let mut encoder = sha256d::Hash::engine(); let mut encoder = sha256d::Hash::engine();
witness_root.consensus_encode(&mut encoder).expect("engines don't error"); witness_root.consensus_encode(&mut encoder).expect("engines don't error");
encoder.input(witness_reserved_value); encoder.input(witness_reserved_value);
WitnessCommitment(sha256d::Hash::from_engine(encoder)) WitnessCommitment::from_byte_array(sha256d::Hash::from_engine(encoder).to_byte_array())
} }
/// Computes the Merkle root of transactions hashed for witness. /// Computes the Merkle root of transactions hashed for witness.

22
primitives/src/block.rs Normal file
View File

@ -0,0 +1,22 @@
// SPDX-License-Identifier: CC0-1.0
//! Bitcoin blocks.
//!
//! A block is a bundle of transactions with a proof-of-work attached,
//! which commits to an earlier block to form the blockchain. This
//! module describes structures and functions needed to describe
//! these blocks and the blockchain.
use hashes::sha256d;
hashes::hash_newtype! {
/// A bitcoin block hash.
pub struct BlockHash(sha256d::Hash);
/// A hash corresponding to the witness structure commitment in the coinbase transaction.
pub struct WitnessCommitment(sha256d::Hash);
}
impl BlockHash {
/// Dummy hash used as the previous blockhash of the genesis block.
pub const GENESIS_PREVIOUS_BLOCK_HASH: Self = Self::from_byte_array([0; 32]);
}

View File

@ -29,6 +29,7 @@ extern crate std;
#[macro_use] #[macro_use]
extern crate serde; extern crate serde;
pub mod block;
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
pub mod locktime; pub mod locktime;
pub mod opcodes; pub mod opcodes;
@ -44,6 +45,7 @@ pub use units::*;
pub use self::locktime::{absolute, relative}; pub use self::locktime::{absolute, relative};
#[doc(inline)] #[doc(inline)]
pub use self::{ pub use self::{
block::{BlockHash, WitnessCommitment},
pow::CompactTarget, pow::CompactTarget,
sequence::Sequence, sequence::Sequence,
transaction::{Txid, Wtxid}, transaction::{Txid, Wtxid},