Add from_int_btc method to Amount#1870
Conversation
f435a2f to
c57a7e0
Compare
|
hmm this compiles fine on my local machine but I didn't notice the warning "invalid feature rust_v_1_57 .." which causes CI to fail. Looks like this needs more work. |
|
Should be using conditional configuration option not a feature (that means |
|
We could also tack an Even without |
|
We could also just add 'unchecked` to the end and let the default behavior of overflow in release mode or panic in debug mode happen. I'm fine with that as long as unchecked is added. I think it could happen if applications don't sanitize user input. |
Well, we can't panic under any circumstances because we're trying to make a We could overflow in both release and debug mode, I suppose. |
|
I'm afk right now so I can't test, however I was thinking const fn will still panic in debug mode if for example |
|
I tested on the rust playground and I guess since it's const fn the compiler won't allow overflows to compile. Although I'm not sure how the sementics work with const fn if user args are provided. Maybe that's not possible. |
This is potentially very cool, if we can exploit that by transforming arbitrary "invalid inputs" into integer overflows. |
|
How about this, it leaks the internal representation in the docs but every dev knows that bitcoin is stored in sats as a u64. /// Creates an [`Amount`] from a value representing a whole number of bitcoin.
///
/// Internally computes the number of satoshis (ie, `btc * 100_000_000`)
/// saturating at the numeric bounds of `u64`.
pub const fn saturating_from_int_btc(btc: u64) -> Amount {
Amount(btc.saturating_mul(100_000_000))
}and a unit test #[test]
fn const_saturating_from_int_btc_overflow() {
let got = Amount::saturating_from_int_btc(u64::MAX - 10);
let want = Amount(u64::MAX);
assert_eq!(got, want);
} |
|
OT: Why don't we have a const |
|
If we make a saturating one, I'd expect it to saturate to a whole BTC... maybe even 21 mil (MAX_BTC) u64::MAX is kind of weird, since it's not a whole bitcoin. |
Let's not have the whole "what does max bitcoin amount mean" debate here, its been argued before and is unlikely to be resolved IMO.
I was under the impression that everyone knows that bitcoin (in general not just in rust-bitcoin) uses ints not floats so this behaviour is obvious but that's coming from the perspective of a low level C guy with a history of disliking floats - am I wrong? |
|
I just think it's weird that
Since the whole point of the function is to turn But again, I think this is just personal preference, and if it's documented anything is fine. |
|
Cool, thanks for getting the bounds. So if I understand you clearly this leads to something like (excluding the exact name): pub const: u64 MAX_EXPRESSIBLE_WHOLE_BITCOIN_IN_SATS = 18446744073700000000;and we saturate to that. I agree with the sentiment, the reason I'd like to avoid this is because it provides an answer to the "what is the meaning of max bitcoin" question. Currently we implicitly define max bitcoin as What if, in this PR, we saturate to |
|
Sounds perfect. BTC_MAX (21 mil BTC) and MAX seems confusing though. |
|
concept ACK from me on saturating as long as it's documented. The warning should probably be in bold, and we should briefly explain that we'd like to just panic but can't in a constfn at our current MSRV. I don't care where we saturate at. I mildly prefer we not provide an exposed constant for this because that will commit us to it. (Though OTOH if we commit to anything, u64::MAX seems like the most technically justifiable value.) I also think that we should explore whether we can just use |
|
You continue prodding finally tweaked my interest, it behaves as we want - see: #1883 |
|
I'm not convinced saturating is that useful, we should just use the array out of bounds trick to panic in const. |
|
I also don't see why we should bother with saurating_mul. As I understand, const context evaluates when compiled, and if there is an overflow it's better to have a compiler error and not compile instead of saturating. |
0046bb8 Fix usage of cfg(rust_1_53) (Tobin C. Harding) c3450f3 Remove stale usage of doc(cfg) (Tobin C. Harding) Pull request description: These build cfg options are not features, fix broken usage. And remove stale docsrs attribute while we are at it. Bad rust-bitcoin devs. Found while reviewing #1870 ACKs for top commit: apoelstra: ACK 0046bb8 Kixunil: ACK 0046bb8 Tree-SHA512: 7053affef6654ff203c93590bf081e165f019feb040aa8c55259ffe4e15eaf0e7522f6e5a4f6f62e8f578420b0313f4b7b17c46b741b7fcfd05750e5c5976589
c57a7e0 to
d833bfc
Compare
It looks like there is a lint that prevents the array oob trick 🤦 |
|
If the OOB trick isn't possible, maybe feature gate this so it's only available in 1.57+? |
|
You could either disable the lint or just remove the |
d833bfc to
b7b5e04
Compare
|
Sorry for the delay, was afk for a bit |
tcharding
left a comment
There was a problem hiding this comment.
Thanks man, looks good!
ACK b7b5e0452f337c442640bb870488b666059b32d3
There are a bunch more places this new function could be used if you get super motivated (git grep _000_000).
bitcoin/Cargo.toml
Outdated
There was a problem hiding this comment.
This shouldn't really be in here but I won't hold you up for it :)
b7b5e04 to
12a50a6
Compare
Oh nice, git grep is dope. Or better yet, |
701b24d to
88df011
Compare
|
I use a shell alias to |
tcharding
left a comment
There was a problem hiding this comment.
ACK 88df0115f5b919195fd6dced10f56902ee380bb9
bitcoin/src/amount.rs
Outdated
There was a problem hiding this comment.
Should have # Panics section.
There was a problem hiding this comment.
Lucky we have @Kixunil on review to catch the things that I miss :)
There was a problem hiding this comment.
Thanks, I added a panic section
f370406 to
c99c3db
Compare
This is the |
|
|
bitcoin/src/amount.rs
Outdated
There was a problem hiding this comment.
Did you mean to put this in here? Its unrelated to the PR.
There was a problem hiding this comment.
Crap, looks like a bad git rebase.
bitcoin/src/amount.rs
Outdated
There was a problem hiding this comment.
| #[should_panic] | |
| #[test] | |
| #[should_panic] | |
| #[test] |
bitcoin/src/amount.rs
Outdated
There was a problem hiding this comment.
| fn from_int_btc_panic() { | |
| fn from_int_btc_overflow() { |
c99c3db to
5746009
Compare
Sorry about that. It looks like I pulled in some old state from master on accident. |
5746009 to
9f7449b
Compare
|
ACK 9f7449b |
Followup PR from #1811
Added a
constassociated functionfrom_int_btc()for Amount.panic()in const context is only available after 1.57+ so a work around is provided.