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 check to max difficulty transition threshold #2337

Merged

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Jan 14, 2024

When computing the maximum difficulty transition threshold we forgot to check that the returned Target is not bigger than the maximum. This value is network specific so keep the original logic but with _unchecked on the function name.

This was noted in the discussion on #2161

@tcharding
Copy link
Member Author

Note, this is an API breaking change. Done on purpose so users have to decided what to do about the network.

@tcharding tcharding added the API break This PR requires a version bump for the next release label Jan 14, 2024
@tcharding
Copy link
Member Author

Note this cannot be backported because we didn't have the max attainable consts yet in the last release.

@apoelstra
Copy link
Member

We also need to check that our minimum shift doesn't go below 1. (And unlike the maximum, the minimum limit has actually been hit on the mainchain many times.)

Kixunil
Kixunil previously approved these changes Jan 15, 2024
Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK 205b8c7

@Kixunil Kixunil dismissed their stale review January 15, 2024 19:10

Missing minimum check

@Kixunil
Copy link
Collaborator

Kixunil commented Jan 15, 2024

Yeah, ACK with fixed minimum.

Note this cannot be backported because we didn't have the max attainable consts yet in the last release.

We could backport those. But it can't be backported because it's a breaking change. :) And while it's a bugfix it's easily solvable in user code. We should probably actually backport the constants - but only the constants and tell people to manually implement the check for now.

@github-actions github-actions bot added the C-bitcoin PRs modifying the bitcoin crate label Jan 24, 2024
@tcharding
Copy link
Member Author

Add check for not less than 1 to minimun transition threshold.

@coveralls
Copy link

coveralls commented Jan 24, 2024

Pull Request Test Coverage Report for Build 8515679554

Details

  • 12 of 22 (54.55%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 83.138%

Changes Missing Coverage Covered Lines Changed/Added Lines %
bitcoin/src/pow.rs 4 14 28.57%
Totals Coverage Status
Change from base Build 8515631286: 0.002%
Covered Lines: 19101
Relevant Lines: 22975

💛 - Coveralls

Kixunil
Kixunil previously approved these changes Jan 24, 2024
Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK 2499a15

bitcoin/src/pow.rs Outdated Show resolved Hide resolved
Kixunil
Kixunil previously approved these changes Jan 25, 2024
Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK 8f3537b

bitcoin/src/pow.rs Outdated Show resolved Hide resolved
@tcharding tcharding force-pushed the 01-15-difficulty-transition branch 3 times, most recently from 77ea0c3 to 1f2d561 Compare January 28, 2024 20:51
bitcoin/src/pow.rs Outdated Show resolved Hide resolved
bitcoin/src/pow.rs Outdated Show resolved Hide resolved
bitcoin/src/pow.rs Outdated Show resolved Hide resolved
@tcharding tcharding force-pushed the 01-15-difficulty-transition branch 2 times, most recently from 477077f to 7518706 Compare January 29, 2024 21:30
@tcharding
Copy link
Member Author

I've split the PR up into multiple patches so each patch does one conceptual thing.

@apoelstra
Copy link
Member

CI failure looks real.

@github-actions github-actions bot added C-hashes PRs modifying the hashes crate C-units PRs modifying the units crate C-internals PRs modifying the internals crate C-io PRs modifying the io crate labels Mar 19, 2024
@tcharding
Copy link
Member Author

tcharding commented Mar 19, 2024

I added 16e64ef at the front of the PR to do the clippy allow. No other changes.

apoelstra
apoelstra previously approved these changes Mar 20, 2024
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 aa4591d

@tcharding
Copy link
Member Author

@sanket1729 this only has rebases since you last acked.

git range-diff 1165c2c...@
-:  -------- > 1:  b3c28e9c Allow needless-borrows-for-generic-args
1:  31b19e04 = 2:  c00b756e Document current behaviour of difficulty_float
2:  d8de117e = 3:  bc5bd736 Debug assert that target != zero in difficulty calc
3:  0c63d0c2 = 4:  502a76b4 Take Params instead of Network in difficulty function
4:  dc1f6514 = 5:  39c269e7 Rename Params::pow_limit to max_attainable_target
5:  1b34fb06 = 6:  1112b03d Rename difficulty transition threshold functions
6:  1165c2c4 = 7:  aa4591d3 Improve API for max target threshold calculation

apoelstra added a commit that referenced this pull request Mar 25, 2024
0ca5a43 hashes: Bump version to v0.14.0 (Tobin C. Harding)

Pull request description:

  In preparation for release add a changlelog entry and bump the version.

  Note the hashes 0.13.0 dependency stays in the dependency graph because of secp, we can update secp after releasing `hashes` then update the secp dependency in `rust-bitcoin` thereby removing the `hashes v0.13.0` dependency - phew.

  Note we are right to release this immediately, the two open PRs (#2337 and #2541) that touch `hashes` only add a clippy attribute so can safely be ignored.

ACKs for top commit:
  apoelstra:
    ACK 0ca5a43
  sanket1729:
    ACK 0ca5a43

Tree-SHA512: d1d26acb8fbf13f785b25add3f1dac05bb392b5bdbad16ead2bc5dd26f3d668824c4b653c373f88c3562a37e775146766680606cedd19db40e0f197b26ca86b8
@tcharding tcharding added the P-high High priority label Mar 27, 2024
This lint triggers when parsing a reference to a large struct as a
generic argument, which is wrong.

Allow it crate wide because [subjectively] this lint never warns for
anything useful.
Improve rustdocs on the `Target::difficulty_float` function,
specifically the return value if self is zero.
The `difficulty` calculation requires dividing a target value by `self`.
Add an assertion that `self` is not zero to help devs debug this.

Note that this should never really be hit, but its possible there is a
bug somewhere causing the target to be set to zero - so this may help
debugging.

Also, add panics section to rustdocs.
What we really want is the maximum target, but since this is a const in
`Params` use an `AsRef<Params>` argument in the `difficulty` functions.

Requires implementation of `AsRef<Params> for Params`.
The maximum "attainable" target is a `rust-bitcoin` thing, Core use max
unattainable.

Deprecated the `Params::pow_limit` field and add a new field
`max_attainable_target`.

The `Params` type is `non_exhaustive` so this is not an API breaking
change.
These two functions calculate the min/max threshold transition which is
a _target_ not a "difficulty" number. Using "difficulty" in the function
name is unnecessarily confusing.

Rename and deprecate the functions.
The maximum target threshold has a network dependant upper bound.
Currently we are not checking this bound. One complication is that there
is currently heated open debate around the `Network` type.

We can bypass the `Network` issue by using `AsRef<Params>` instead.

Add a function that does the checks based on the `Params` type as well
as an unchecked version.
@tcharding tcharding force-pushed the 01-15-difficulty-transition branch from 1dd3444 to fd6fedc Compare April 2, 2024 00:41
@tcharding
Copy link
Member Author

Rebased, some changes show up in git range-diff due to rustfmt.

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 fd6fedc

@tcharding
Copy link
Member Author

This has minimal changes since @sanket1729 acked (just rebase and the stuff caused by now being on top of a formatted codebase after #2462). Can be merged by way of rule 4 of the one ack carve-out.

Copy link
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

ACK fd6fedc

@@ -245,6 +245,10 @@ impl Target {
///
/// See [`difficulty`] for details.
///
/// # Returns
///
/// Returns [`f64::INFINITY`] if `self` is zero (caused by divide by zero).
Copy link
Member

Choose a reason for hiding this comment

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

Interesting, TIL.

@apoelstra apoelstra merged commit 499f36f into rust-bitcoin:master Apr 2, 2024
168 checks passed
@tcharding tcharding deleted the 01-15-difficulty-transition branch April 2, 2024 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API break This PR requires a version bump for the next release C-bitcoin PRs modifying the bitcoin crate C-hashes PRs modifying the hashes crate C-internals PRs modifying the internals crate C-io PRs modifying the io crate C-units PRs modifying the units crate P-high High priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants