From 65925117a037dd27722da8bdfdf0406b3a596c9d Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 15 Oct 2024 10:46:28 +1100 Subject: [PATCH] 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. --- bitcoin/src/blockdata/block.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 38bb8a079..b58b02fc1 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -66,9 +66,16 @@ impl Header { pub const SIZE: usize = 4 + 32 + 32 + 4 + 4 + 4; // 80 /// 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 { 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()) }