8000 Overflow check for feerate traits by yancyribbens · Pull Request #1849 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Overflow check for feerate traits #1849

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

Conversation

yancyribbens
Copy link
Contributor

Add checks for overflow for fee_rate div and mul traits. Also I'm not 100% but I think there is a trait that isn't useful so I've removed it in a separate commit.

@yancyribbens yancyribbens force-pushed the overflow-check-for-feerate-traits branch 2 times, most recently from d45468b to ef12c78 Compare May 11, 2023 14:25
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.

Removing the trait is absolute no-go.

I also think that overflow shouldn't happen in general. The values are normally very small. If we want to be paranoid we could just panic regardless of overflow_checks.

type Output = Amount;

fn mul(self, rhs: Weight) -> Self::Output { rhs * self }
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This absolutely cannot be removed - it's the primary way to compute the fee!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK good to know. Can you provide an example of how this is used? I tried to come up with a way to test this but found it confusing.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You literally write let fee = fee_rate * weight;

fn div(self, rhs: Weight) -> Self::Output { FeeRate(self.to_sat() * 1000 / rhs.to_wu()) }
fn div(self, rhs: Weight) -> Self::Output {
self.to_sat().checked_mul(1_000).map(|s| FeeRate(s / rhs.to_wu())) 8000
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't like this much. It's not idiomatic and perhaps we could actually guarantee that Weight is non-zero? Or have NonZeroWeight type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is checking for a multiplication overflow. Although maybe it should also be performing a checked_div also in case of zero? I'm less concerned about this right now since I'm not using it atm. Just figured I'd add check for overflows while I'm at it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mean returning Option.


fn mul(self, rhs: FeeRate) -> Self::Output {
Amount::from_sat((rhs.to_sat_per_kwu() * self.to_wu() + 999) / 1000)
rhs.to_sat_per_kwu().checked_mul(self.to_wu()).map(|s| Amount::from_sat((s + 999) / 1_000))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same issue as before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one I think should be checked for overflow since FeeRate may be something provided as user input.

@yancyribbens
Copy link
Contributor Author

I also think that overflow shouldn't happen in general. The values are normally very small. If we want to be paranoid we could just panic regardless of overflow_checks.

@Kixunil The issue is that something like fee_rate may be provided via user input. If the downstream app that uses this lib passes user input, someone may just type in values that will cause the app to crash. Another idea is to add a checked version of this someplace else instead if you're really opposed to doing a checked version here.

@Kixunil
Copy link
Collaborator
Kixunil commented May 14, 2023

Yeah, checked versions just like all other integers have make more sense.

@yancyribbens
Copy link
Contributor Author

closing in favor of #1864

apoelstra added a commit that referenced this pull request May 22, 2023
b03c24d Add a checked version of weight mul fee_rate (yancy)

Pull request description:

  Add a checked version of fee_rate * weight.  While I like the trait version of just being able to multiply `feerate * weight`, it's not really very useful imo since a large input feerate could cause an overflow.  Instead of changing the trait in #1849 (not idiomatic enough I guess) I added a `checked_weight_mul` method to `FeeRate`.

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

Tree-SHA512: 231ade94291becadcea9ea2a40a5daf96b77f01a29cca2494d7bbe4f7de5b412fa8fc816ea249268569f5378410185d9349fd687533bf3a422a752997e107a2b
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.

2 participants
0