Merge rust-bitcoin/rust-bitcoin#1720: Feature: Add difficulty_float method for block::Header.

05fdead2a4 Feature: Add difficulty_float method for block::Header. (junderw)

Pull request description:

  Header had a passthrough method for difficulty so I added one for difficulty_float as well.

ACKs for top commit:
  apoelstra:
    ACK 05fdead2a4
  sanket1729:
    ACK 05fdead2a4. Don't mind the floating point comparison as it is in tests.
  Kixunil:
    ACK 05fdead2a4
  tcharding:
    ACK 05fdead2a4

Tree-SHA512: 1cbb9627a1d56a3ed29a836081fe7c55b26a8cbef0068a2df6d4960f302519f82e396460e3627f556e8231f06b4d321d3f2af36c065d8c0ef316b71c59aad3c9
This commit is contained in:
Andrew Poelstra 2023-03-21 22:01:15 +00:00
commit 7b4ed3f1b5
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 7 additions and 0 deletions

View File

@ -76,6 +76,11 @@ impl Header {
self.target().difficulty() self.target().difficulty()
} }
/// Computes the popular "difficulty" measure for mining and returns a float value of f64.
pub fn difficulty_float(&self) -> f64 {
self.target().difficulty_float()
}
/// Checks that the proof-of-work for the block is valid, returning the block hash. /// Checks that the proof-of-work for the block is valid, returning the block hash.
pub fn validate_pow(&self, required_target: Target) -> Result<BlockHash, Error> { pub fn validate_pow(&self, required_target: Target) -> Result<BlockHash, Error> {
let target = self.target(); let target = self.target();
@ -466,6 +471,7 @@ mod tests {
assert_eq!(real_decode.header.work(), work); assert_eq!(real_decode.header.work(), work);
assert_eq!(real_decode.header.validate_pow(real_decode.header.target()).unwrap(), real_decode.block_hash()); assert_eq!(real_decode.header.validate_pow(real_decode.header.target()).unwrap(), real_decode.block_hash());
assert_eq!(real_decode.header.difficulty(), 1); assert_eq!(real_decode.header.difficulty(), 1);
assert_eq!(real_decode.header.difficulty_float(), 1.0);
// [test] TODO: check the transaction data // [test] TODO: check the transaction data
assert_eq!(real_decode.size(), some_block.len()); assert_eq!(real_decode.size(), some_block.len());
@ -501,6 +507,7 @@ mod tests {
assert_eq!(real_decode.header.work(), work); assert_eq!(real_decode.header.work(), work);
assert_eq!(real_decode.header.validate_pow(real_decode.header.target()).unwrap(), real_decode.block_hash()); assert_eq!(real_decode.header.validate_pow(real_decode.header.target()).unwrap(), real_decode.block_hash());
assert_eq!(real_decode.header.difficulty(), 2456598); assert_eq!(real_decode.header.difficulty(), 2456598);
assert_eq!(real_decode.header.difficulty_float(), 2456598.4399242126);
// [test] TODO: check the transaction data // [test] TODO: check the transaction data
assert_eq!(real_decode.size(), segwit_block.len()); assert_eq!(real_decode.size(), segwit_block.len());