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 absolute fee rate convenience functions to FeeRate #1947

Merged
merged 3 commits into from Aug 2, 2023

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Jul 19, 2023

  • Patch 1 is docs cleanup
  • Patch 2 adds two functions to FeeRate

From the commit log of patch 2:

Calculating the absolute fee from a fee rate can currently be achieved
by creating a `Weight` object and using
`FeeRate::checked_mul_by_weight`. This is kind of hard to discover, we
can add two public convenience functions that make discovery of the
functionality easier.

Add two functions for calculating the absolute fee by multiplying by
weight units (`Weight`) and virtual bytes (by first converting to weight
units).

This seems like an obvious thing so I'm inclined to think that Kixunil left it out for a reason. (Mentioning you here Kix so even if this merges you'll see it in notifications later on.)

Make the docs use the same form as everywhere else, which also mimics
stdlib docs on integer types.
apoelstra
apoelstra previously approved these changes Jul 20, 2023
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 89c8119

@tcharding tcharding changed the title Add absolute fee rate convenient functions to FeeRate Add absolute fee rate convenience functions to FeeRate Aug 1, 2023
/// `None` if overflow occurred.
///
/// This is equivalent to `Self::checked_mul_by_weight()`.
pub fn absolute_fee_wu(self, weight: Weight) -> Option<Amount> {
Copy link
Contributor

Choose a reason for hiding this comment

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

why not just use checked_mul_by_weight()?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, why absolute fee instead of fee_wu? if the name checked_mul_by_weight is confusing, maybe just rename that function instead of adding this wrapper function?

Copy link
Contributor

Choose a reason for hiding this comment

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

Personally I like the absolute_fee_wu name, I've always found bdk's fee_wu to be quite confusing. I don't have any strong opinion on renaming vs adding a wrapper.

Copy link
Contributor

Choose a reason for hiding this comment

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

What does the word absolute mean in the context of fee? Is there such a thing as a non-absolute fee?

Copy link
Member Author

Choose a reason for hiding this comment

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

I dug around math and english definitions of "absolute" and, contrary to my gut feeling, it does not add any additional meaning although it was also [mis]used in this Bitocin Core PR.

I've removed the absolute_ prefix.

///
/// This is equivalent to converting `vb` to `weight` using `Weight::from_vb` and then calling
/// `Self::checked_mul_by_weight(weight)`.
pub fn absolute_fee_vb(self, vb: u64) -> Option<Amount> {
Copy link
Contributor

Choose a reason for hiding this comment

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

an additional test would be nice to go along with this.

Copy link
Contributor

@danielabrozzoni danielabrozzoni left a comment

Choose a reason for hiding this comment

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

ACK 89c8119

/// `None` if overflow occurred.
///
/// This is equivalent to `Self::checked_mul_by_weight()`.
pub fn absolute_fee_wu(self, weight: Weight) -> Option<Amount> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally I like the absolute_fee_wu name, I've always found bdk's fee_wu to be quite confusing. I don't have any strong opinion on renaming vs adding a wrapper.

@tcharding
Copy link
Member Author

Changes in force push:

  • Added an additional trivial rustdoc fix patch (as patch 2)
  • Rename functions by dropping the "absolute_" prefix
  • Added a unit test to verify the two convenience functions agree

Calculating the absolute fee from a fee rate can currently be achieved
by creating a `Weight` object and using
`FeeRate::checked_mul_by_weight`. This is kind of hard to discover, we
can add two public convenience functions that make discovery of the
functionality easier.

Add two functions for calculating the absolute fee by multiplying by
weight units (`Weight`) and virtual bytes (by first converting to weight
units).
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.

utACK 9eff2f2

@@ -199,4 +225,20 @@ mod tests {
let fee_rate = FeeRate(10).checked_div(0);
assert!(fee_rate.is_none());
}

#[test]
fn fee_convenience_functions_agree() {
Copy link
Contributor

Choose a reason for hiding this comment

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

this test could be boilled down to like two lines:

fn fee_convenience_functions_agree() {
    let rate = FeeRate::from_sat_per_vb(1).expect("1 sat/byte is valid");
    assert_eq!(rate.fee_vb(1), rate.fee_wu(Weight::from_wu(4)));                           
}

after all, no need to test all of the transaction serialization stuff again here.

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 9eff2f2

@apoelstra
Copy link
Member

Removed the @ mention from the PR description because it's likely to cause more noise than intended.

@apoelstra apoelstra merged commit 49fa879 into rust-bitcoin:master Aug 2, 2023
29 checks passed
@tcharding
Copy link
Member Author

Oh I thought it only spammed if in the commit not in the PR?

@apoelstra
Copy link
Member

@tcharding the merge script copies the PR message into the commit.

@tcharding
Copy link
Member Author

Oh of course. Thanks.

@tcharding tcharding deleted the 07-20-absolute-fee branch August 3, 2023 23:04
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.

None yet

5 participants