Skip to content

feat: add Amount division by FeeRate#3893

Merged
apoelstra merged 2 commits intorust-bitcoin:masterfrom
jrakibi:implement-max-transaction-weight
Jan 22, 2025
Merged

feat: add Amount division by FeeRate#3893
apoelstra merged 2 commits intorust-bitcoin:masterfrom
jrakibi:implement-max-transaction-weight

Conversation

@jrakibi
Copy link
Copy Markdown
Contributor

@jrakibi jrakibi commented Jan 11, 2025

Add checked_div_by_fee_rate method to Amount that computes the maximum weight for a transaction with a given fee rate. This complements the existing fee = fee_rate * weight and fee_rate = fee / weight operations

  • Add checked_div_by_fee_rate method that returns Option
  • Implement Div<FeeRate> for Amount for operator syntax support
  • Use floor division to ensure weight doesn't exceed intended fee

This allows calculating the maximum transaction weight possible for a given fee amount and fee rate.

Closes #3814

@github-actions github-actions bot added the C-units PRs modifying the units crate label Jan 11, 2025
@coveralls
Copy link
Copy Markdown

coveralls commented Jan 11, 2025

Pull Request Test Coverage Report for Build 12886609170

Details

  • 68 of 70 (97.14%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 83.111%

Changes Missing Coverage Covered Lines Changed/Added Lines %
units/src/fee.rs 39 41 95.12%
Totals Coverage Status
Change from base Build 12880082219: 0.04%
Covered Lines: 20914
Relevant Lines: 25164

💛 - Coveralls

@tcharding
Copy link
Copy Markdown
Member

Mad, thanks for your contribution. High level ack from me except that we agreed over here that we should add two functions, one for floor and one for ceiling division (using _floor and _ceil suffixes).

Also

  • lint warning is valid, need backticks in rustdocs
  • to get past the API CI job you can run just check-api; ci -a -m 'api: Run just check-api'

Thanks again!

@tcharding
Copy link
Copy Markdown
Member

Also, perhaps we should add unit tests that test the truncation functionality. Good work on the docs, your description is better than my attempt.

@jrakibi
Copy link
Copy Markdown
Contributor Author

jrakibi commented Jan 18, 2025

@tcharding Thanks for your feedback!
I believe I've addressed all the changes:

  • Added both floor and ceiling division methods
  • Added tests for truncation behavior
  • Updated documentation and fix Lint warnings

lmk if anything else needs adjustment

Copy link
Copy Markdown
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.

Mad! A few comments (I'm basing my review style on the fact that you've been active in bitcoin-dev-project so you're keen to learn).

As far as code logic goes it all looks correct to me. To get this into a state that I can ack it all we need is to squash the commits. In this repo we keep the PRs consisting of discreet patches by rebasing and force pushing to implement review suggestions. This is as opposed to the development style of creating new patches on top of old ones to implement review suggestions. Ask if that explanation is not clear. There is more than one way to do things but if I was doing this PR I would have a single patch that adds the new functions and unit tests, then a patch that runs the check api script. (If it helps I tend to use the command just check-api; ci -a -m 'api: Run just check-api')

If however you want to improve your Rust skills I would suggest attempting to refactor the functions because there are a bunch of lines of code that are unnecessary. (Hint: try thinking what happens in the checked ops when dividing by zero). Since you have pretty good test coverage already you can refactor wildly and just run the tests to see you didn't break anything.

(nit: There are a bunch of trailing whitespaces too. If you can't see them I can share some git config options that might make them show up in your terminal, I'm not exactly sure what in my config file enables that though.)

Good luck and thanks for the contribution!

tcharding added a commit to tcharding/rust-bitcoin that referenced this pull request Jan 20, 2025
In preparation for release add a changelog entry, bump the version, and
update the lock files.

`v1.0` here we come.

Before we merge this we should add:

- rust-bitcoin#3934 or rust-bitcoin#3866
- rust-bitcoin#3933
- rust-bitcoin#3932
- rust-bitcoin#3929
- rust-bitcoin#3926
- rust-bitcoin#3923
- rust-bitcoin#3893
- rust-bitcoin#3866
- rust-bitcoin#3794
@jrakibi jrakibi force-pushed the implement-max-transaction-weight branch from 926e4ab to 9c94acc Compare January 20, 2025 21:53
@jrakibi
Copy link
Copy Markdown
Contributor Author

jrakibi commented Jan 20, 2025

Thank you for the detailed review
I believe I've implemented the suggested changes

@jrakibi jrakibi force-pushed the implement-max-transaction-weight branch from 9c94acc to be07dfc Compare January 20, 2025 22:20
@tcharding
Copy link
Copy Markdown
Member

Nice, thanks man. This is looking really good. Sorry to be a nuisance but can re-write the git log of the first commit so it is well formed. Here is a nice blog post if you'd like to learn how (and why) to do so: https://cbea.ms/git-commit/

Implement `checked_div_by_fee_rate_floor` and `checked_div_by_fee_rate_ceil` methods to compute the maximum and minimum transaction weights.
These methods provide precise weight calculations using both floor and ceiling divisions.

- Introduce `checked_div_by_fee_rate_floor` for floor-based weight division.
- Add `checked_div_by_fee_rate_ceil` for ceiling-based weight division.
- Update `ops::Div` implementation for `Amount` to use floor division by default.
- Include unit tests to validate both floor and ceiling division methods.
@jrakibi jrakibi force-pushed the implement-max-transaction-weight branch from be07dfc to 7482fcd Compare January 21, 2025 12:08
@jrakibi
Copy link
Copy Markdown
Contributor Author

jrakibi commented Jan 21, 2025

Thanks for your continuous feedback
done

Copy link
Copy Markdown
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 7482fcd; successfully ran local tests

Copy link
Copy Markdown
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 7482fcd

@tcharding
Copy link
Copy Markdown
Member

Nice thanks bro!

  • Update ops::Div implementation for Amount to use floor division by default.

This made me double take because it made me think the PR changed the current ops::Div impl but really it adds a new impl.

Don't change the PR though, its good to go as is. Appreciate you taking on board my feedback! Hope to see you doing more rust-bitcoin patches, we have plenty of work to do.

@apoelstra apoelstra merged commit 11f5012 into rust-bitcoin:master Jan 22, 2025
tcharding added a commit to tcharding/rust-bitcoin that referenced this pull request Feb 24, 2025
We are trying a new strategy to get to 1.0 more quickly - remove
`amount` and `fee` and release everything else.

In preparation for release add a changelog entry, bump the version, and
update the lock files.

`v1.0` here we come.

Before we merge this we should add:

- rust-bitcoin#3934 or rust-bitcoin#3866
- rust-bitcoin#3933
- rust-bitcoin#3932
- rust-bitcoin#3929
- rust-bitcoin#3926
- rust-bitcoin#3923
- rust-bitcoin#3893
- rust-bitcoin#3866
- rust-bitcoin#3794
tcharding added a commit to tcharding/rust-bitcoin that referenced this pull request Feb 24, 2025
We are trying a new strategy to get to 1.0 more quickly - remove
`amount` and `fee` and release everything else.

In preparation for release add a changelog entry, bump the version, and
update the lock files.

`v1.0` here we come.

Before we merge this we should add:

- rust-bitcoin#3934 or rust-bitcoin#3866
- rust-bitcoin#3933
- rust-bitcoin#3932
- rust-bitcoin#3929
- rust-bitcoin#3926
- rust-bitcoin#3923
- rust-bitcoin#3893
- rust-bitcoin#3866
- rust-bitcoin#3794
tcharding added a commit to tcharding/rust-bitcoin that referenced this pull request Feb 24, 2025
We are trying a new strategy to get to 1.0 more quickly - remove
`amount` and `fee` and release everything else.

In preparation for release add a changelog entry, bump the version, and
update the lock files.

`v1.0` here we come.

Before we merge this we should add:

- rust-bitcoin#3934 or rust-bitcoin#3866
- rust-bitcoin#3933
- rust-bitcoin#3932
- rust-bitcoin#3929
- rust-bitcoin#3926
- rust-bitcoin#3923
- rust-bitcoin#3893
- rust-bitcoin#3866
- rust-bitcoin#3794
tcharding added a commit to tcharding/rust-bitcoin that referenced this pull request Feb 24, 2025
We are trying a new strategy to get to 1.0 more quickly - remove
`amount` and `fee` and release everything else.

In preparation for release add a changelog entry, bump the version, and
update the lock files.

`v1.0` here we come.

Before we merge this we should add:

- rust-bitcoin#3934 or rust-bitcoin#3866
- rust-bitcoin#3933
- rust-bitcoin#3932
- rust-bitcoin#3929
- rust-bitcoin#3926
- rust-bitcoin#3923
- rust-bitcoin#3893
- rust-bitcoin#3866
- rust-bitcoin#3794
tcharding added a commit to tcharding/rust-bitcoin that referenced this pull request Feb 24, 2025
We are trying a new strategy to get to 1.0 more quickly - remove
`amount` and `fee` and release everything else.

In preparation for release add a changelog entry, bump the version, and
update the lock files.

`v1.0` here we come.

Before we merge this we should add:

- rust-bitcoin#3934 or rust-bitcoin#3866
- rust-bitcoin#3933
- rust-bitcoin#3932
- rust-bitcoin#3929
- rust-bitcoin#3926
- rust-bitcoin#3923
- rust-bitcoin#3893
- rust-bitcoin#3866
- rust-bitcoin#3794
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-units PRs modifying the units crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement max transaction weight = fee / fee_rate

4 participants