Manually implement block::Header::block_hash

We want to move the `block_hash` function to `primitives` but it uses
`Encodable` which currently lives in `bitcoin`. Just implement it
manually.

We added a regression test already in a previous commit to check that
this is correct.
This commit is contained in:
Tobin C. Harding 2024-10-15 10:46:28 +11:00
parent b02ab25342
commit 65925117a0
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 8 additions and 1 deletions

View File

@ -66,9 +66,16 @@ impl Header {
pub const SIZE: usize = 4 + 32 + 32 + 4 + 4 + 4; // 80 pub const SIZE: usize = 4 + 32 + 32 + 4 + 4 + 4; // 80
/// Returns the block hash. /// Returns the block hash.
// This is the same as `Encodable` but done manually because `Encodable` isn't in `primitives`.
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"); engine.input(&self.version.to_consensus().to_le_bytes());
engine.input(self.prev_blockhash.as_byte_array());
engine.input(self.merkle_root.as_byte_array());
engine.input(&self.time.to_le_bytes());
engine.input(&self.bits.to_consensus().to_le_bytes());
engine.input(&self.nonce.to_le_bytes());
BlockHash::from_byte_array(sha256d::Hash::from_engine(engine).to_byte_array()) BlockHash::from_byte_array(sha256d::Hash::from_engine(engine).to_byte_array())
} }