Skip to content

Commit

Permalink
add block_hash method for BlockHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedberg authored and dr-orlovsky committed May 6, 2024
1 parent 78a77ed commit 5786f4c
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion consensus/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use std::fmt::{Formatter, LowerHex};
use std::str::FromStr;

use amplify::hex::{FromHex, ToHex};
use amplify::{Bytes32StrRev, Wrapper};
use amplify::{ByteArray, Bytes32StrRev, Wrapper};
use commit_verify::{DigestExt, Sha256};

use crate::{BlockDataParseError, ConsensusDecode, ConsensusEncode, LIB_NAME_BITCOIN};

Expand Down Expand Up @@ -97,6 +98,17 @@ impl FromStr for BlockHeader {
}
}

impl BlockHeader {
pub fn block_hash(&self) -> BlockHash {
let mut enc = Sha256::default();
self.consensus_encode(&mut enc)
.expect("engines don't error");
let mut double = Sha256::default();
double.input_raw(&enc.finish());
BlockHash::from_byte_array(double.finish())
}
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -120,5 +132,9 @@ mod test {
assert_eq!(header.nonce, 0xad54cd0b);
assert_eq!(header.time, 1710668837);
assert_eq!(header.to_string(), header_str);
assert_eq!(
header.block_hash().to_string(),
"00000000000000000000a885d748631afdf2408d2db66e616e963d08c31a65df"
);
}
}

0 comments on commit 5786f4c

Please sign in to comment.