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

Add a method to pow::Target for returning difficulty as an f64. #1707

Merged

Conversation

junderw
Copy link
Contributor

@junderw junderw commented Mar 10, 2023

Closes #1703

This adds a conversion function to U256 to get an f64. We use the method shown in the following blog post.

https://blog.m-ou.se/floats/

Target::MAX was converted to a f64 and set as a const that is verified in a unit test.

The code is rather confusing, so I took a crack at explaining it in my comments as well. Please let me know if you want it cleaned up some more.

This adds a conversion function to U256 to get an f64. We use the method shown in the following blog post. https://blog.m-ou.se/floats/
Target::MAX was converted to a f64 and set as a const that is verified in a unit test.
@junderw junderw force-pushed the junderw/feature/target-difficulty-f64 branch from 93ac83a to 2158f88 Compare March 10, 2023 14:54
@junderw
Copy link
Contributor Author

junderw commented Mar 10, 2023

I also noticed that Work::log2 could use this method as well.

@junderw
Copy link
Contributor Author

junderw commented Mar 10, 2023

The method used in Work::log2 is about 2.6x faster, but slightly less precise.

Perhaps the call is just moving the Work::log2 logic for converting to a f64 to U256, and using it for Target::difficulty as well?

It depends on how precise we need it to be (probably not very).

@apoelstra
Copy link
Member

@junderw I think we'd prefer precision over speed. But log2 can be changed in a different PR.

@apoelstra
Copy link
Member

concept ACK. Will test and provide a real ACK soon. This is a neat approach. I hadn't considered just constructing an IEEE float directly like this.

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 2158f88

Copy link
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 2158f88

FTR I did not read the blog post to understand how the to_f64 function works - just relied on the unit test values being correct. This would be a good function to attack with mutagen.

Comment on lines +236 to +240
/// Computes the popular "difficulty" measure for mining and returns a float value of f64.
///
/// See [`difficulty`] for details.
///
/// [`difficulty`]: Target::difficulty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit but perhaps this is cleaner

    /// Computes the popular "difficulty" measure for mining and returns a float value.
    ///
    /// See [`Target::difficulty`] for details.

@apoelstra apoelstra merged commit 37b0219 into rust-bitcoin:master Mar 14, 2023
22 checks passed
///
/// [`difficulty`]: Target::difficulty
#[cfg_attr(all(test, mutate), mutate)]
pub fn difficulty_float(&self) -> f64 { TARGET_MAX_F64 / self.0.to_f64() }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just change the type above?

}

// Target::MAX as a float value. Calculated with U256::to_f64.
// This is validated in the unit tests as well.
const TARGET_MAX_F64: f64 = 2.695953529101131e67;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a bit of additional work you could make to_f64 const fn so the magic constant wouldn't be needed. Maybe something for the future.

apoelstra added a commit that referenced this pull request Mar 18, 2023
4924148 Swap out `Work::log2` implementation for `U256::to_f64` (junderw)

Pull request description:

  See: #1707 (comment)

ACKs for top commit:
  Kixunil:
    ACK 4924148
  apoelstra:
    ACK 4924148

Tree-SHA512: 39d5d10b65c9597df1103df1639c57d432f04798b4b2be990b59275b8bd1f08560f72db8cda9edafad202ed0e4bf16cef7798279841b2d713ed482855498d1fe
shesek added a commit to shesek/electrs that referenced this pull request Aug 31, 2023
shesek added a commit to shesek/electrs that referenced this pull request Aug 31, 2023
shesek added a commit to shesek/electrs that referenced this pull request Aug 31, 2023
shesek added a commit to Blockstream/electrs that referenced this pull request Feb 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add BlockHeader method for returning difficulty as an f64
4 participants