Skip to content

Commit 05fdead

Browse files
committed
Feature: Add difficulty_float method for block::Header.
1 parent d830e8d commit 05fdead

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

bitcoin/src/blockdata/block.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ impl Header {
7676
self.target().difficulty()
7777
}
7878

79+
/// Computes the popular "difficulty" measure for mining and returns a float value of f64.
80+
pub fn difficulty_float(&self) -> f64 {
81+
self.target().difficulty_float()
82+
}
83+
7984
/// Checks that the proof-of-work for the block is valid, returning the block hash.
8085
pub fn validate_pow(&self, required_target: Target) -> Result<BlockHash, Error> {
8186
let target = self.target();
@@ -466,6 +471,7 @@ mod tests {
466471
assert_eq!(real_decode.header.work(), work);
467472
assert_eq!(real_decode.header.validate_pow(real_decode.header.target()).unwrap(), real_decode.block_hash());
468473
assert_eq!(real_decode.header.difficulty(), 1);
474+
assert_eq!(real_decode.header.difficulty_float(), 1.0);
469475
// [test] TODO: check the transaction data
470476

471477
assert_eq!(real_decode.size(), some_block.len());
@@ -501,6 +507,7 @@ mod tests {
501507
assert_eq!(real_decode.header.work(), work);
502508
assert_eq!(real_decode.header.validate_pow(real_decode.header.target()).unwrap(), real_decode.block_hash());
503509
assert_eq!(real_decode.header.difficulty(), 2456598);
510+
assert_eq!(real_decode.header.difficulty_float(), 2456598.4399242126);
504511
// [test] TODO: check the transaction data
505512

506513
assert_eq!(real_decode.size(), segwit_block.len());

0 commit comments

Comments
 (0)