Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return BlockHash from BlockHeader::validate_pow #572

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/blockdata/block.rs
Expand Up @@ -128,17 +128,17 @@ impl BlockHeader {
(max_target(network) / self.target()).low_u64()
}

/// Checks that the proof-of-work for the block is valid.
pub fn validate_pow(&self, required_target: &Uint256) -> Result<(), util::Error> {
/// Checks that the proof-of-work for the block is valid, returning the block hash.
pub fn validate_pow(&self, required_target: &Uint256) -> Result<BlockHash, util::Error> {
let target = &self.target();
if target != required_target {
return Err(BlockBadTarget);
}
let data: [u8; 32] = self.block_hash().into_inner();
let block_hash = self.block_hash();
let mut ret = [0u64; 4];
util::endian::bytes_to_u64_slice_le(&data, &mut ret);
util::endian::bytes_to_u64_slice_le(block_hash.as_inner(), &mut ret);
let hash = &Uint256(ret);
if hash <= target { Ok(()) } else { Err(BlockBadProofOfWork) }
if hash <= target { Ok(block_hash) } else { Err(BlockBadProofOfWork) }
}

/// Returns the total work of the block
Expand Down Expand Up @@ -364,7 +364,7 @@ mod tests {
assert_eq!(real_decode.header.bits, 486604799);
assert_eq!(real_decode.header.nonce, 2067413810);
assert_eq!(real_decode.header.work(), work);
assert_eq!(real_decode.header.validate_pow(&real_decode.header.target()).unwrap(), ());
assert_eq!(real_decode.header.validate_pow(&real_decode.header.target()).unwrap(), real_decode.block_hash());
assert_eq!(real_decode.header.difficulty(Network::Bitcoin), 1);
// [test] TODO: check the transaction data

Expand Down Expand Up @@ -398,7 +398,7 @@ mod tests {
assert_eq!(real_decode.header.bits, 0x1a06d450);
assert_eq!(real_decode.header.nonce, 1879759182);
assert_eq!(real_decode.header.work(), work);
assert_eq!(real_decode.header.validate_pow(&real_decode.header.target()).unwrap(), ());
assert_eq!(real_decode.header.validate_pow(&real_decode.header.target()).unwrap(), real_decode.block_hash());
assert_eq!(real_decode.header.difficulty(Network::Testnet), 2456598);
// [test] TODO: check the transaction data

Expand Down Expand Up @@ -429,7 +429,7 @@ mod tests {
fn validate_pow_test() {
let some_header = Vec::from_hex("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b").unwrap();
let some_header: BlockHeader = deserialize(&some_header).expect("Can't deserialize correct block header");
assert_eq!(some_header.validate_pow(&some_header.target()).unwrap(), ());
assert_eq!(some_header.validate_pow(&some_header.target()).unwrap(), some_header.block_hash());

// test with zero target
match some_header.validate_pow(&Uint256::default()) {
Expand Down