Commit Graph

32 Commits

Author SHA1 Message Date
Tobin C. Harding 7d538c830d
units: Implement ops::Neg for SignedAmount
Its useful to be able to do `let x = -btc_amount;`

Implement `core::ops::Neg for SignedAmount`, returning a `SignedAmount`.

Fix: #2470
2024-02-12 13:06:34 +11:00
Andrew Poelstra 343510d3a0
kani: fix Amount overflow test 2024-02-05 18:52:13 +00:00
Andrew Poelstra a3c4194c3f
Merge rust-bitcoin/rust-bitcoin#2428: Remove the remaining TODOs
c69caafefc Remove attribute comments (Tobin C. Harding)
3e83ef9276 Remove consensus error wrapper TODO (Tobin C. Harding)
bfabea94e9 Remove unwrap comment (Tobin C. Harding)
8bdaf4a34d Remove carrying_mul TODO (Tobin C. Harding)

Pull request description:

  Add issues and remove the TODOs from the code.

  Resolves: #2368

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

Tree-SHA512: b10a3de8da7ace890735023f8441605dd11b0227c27a2357556b8aaa8276a7f34ed220e3bcbc93aad4b35357319318ff7de27210e8f60dd90f6c55af23e21470
2024-02-02 23:39:16 +00:00
Tobin C. Harding c69caafefc
Remove attribute comments
Add an issue and remove the TODO from the code as well as the attribute
comments, leave a single comment as an explanation of why the unusual
code block.

ref: https://github.com/rust-bitcoin/rust-bitcoin/issues/2427
2024-02-02 06:22:02 +11:00
Martin Habovstiak 5c15ed5441
CI: Epic overhaul
Re-write the whole CI pipeline.

Co-developed-by: Martin Habovstiak <martin.habovstiak@gmail.com>
2024-02-02 05:57:23 +11:00
Andrew Poelstra 7b937acf17
Merge rust-bitcoin/rust-bitcoin#2403: Make crate level attributes uniform
0997382772 io: Enable alloc from std (Tobin C. Harding)
ba1166a63b Make crate level attributes uniform (Tobin C. Harding)

Pull request description:

  Make the trait level attributes uniform across all released crates in the repo. Excludes things that are obviously not needed, eg, bench stuff if there is not bench code.

  - Remove `uninhabited_references` - this is allow by default now.
  - Remove `unconditional_recursion` and mark the single false positive we have with an `allow`.

  Note, this does not add `missing_docs` to the `io` crate. There is an open PR at the moment to add that along with the required docs.

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

Tree-SHA512: ef1f638aca171536287cce369be98998e871d26468ad2d8c39d9004db610b406471809c283540a4a19bcede78b12b8976a1bb37e5d431fbff8c8a3e53a64d4e3
2024-02-01 14:17:16 +00:00
Tobin C. Harding 96d3bbd065
Fix kani test
Recently (in #2379) we patched the `ParseAmountError` but we don't check
kani code on every pull request so we broke it.

Fix kani test to use the new `OutOfRangeError`.

Close: #2424
2024-02-01 15:13:50 +11:00
Tobin C. Harding bfabea94e9
Remove unwrap comment
Add an issue and remove the TODO from the code.

ref: https://github.com/rust-bitcoin/rust-bitcoin/issues/2426
2024-02-01 12:32:42 +11:00
Tobin C. Harding ba1166a63b
Make crate level attributes uniform
Make the trait level attributes uniform across all released crates in
the repo. Excludes things that are obviously not needed, eg, bench stuff
if there is not bench code.

- Remove `uninhabited_references` - this is allow by default now.
- Remove `unconditional_recursion` and mark the single false positive we
  have with an `allow`.

Note, this does not add `missing_docs` to the `io` crate. There is an
open PR at the moment to add that along with the required docs.
2024-01-31 11:32:46 +11:00
Martin Habovstiak ac26171c32 Clean up `no_std` usage
Previously the crate used negative reasoning to enable `std` which was
hard to understand, required the `prelude` module and wasn't really
needed because it's only needed when a crate wants to add `alloc`
feature-backwards compatibly and this crate always had the feature.

This cleans up usage to unconditionally use `#[no_std]` and then just
add `extern crate` on top as needed by activated features.
2024-01-27 13:25:40 +01:00
Martin Habovstiak fce03cec85 Provide `Amount` & co in no-alloc
Using the crate without allocation was previously disabled making the
crate empty without the feature. This chage makes it more fine-grained:
it only disables string and float conversions which use allocator. We
could later provide float conversions by using a sufficiently-long
`ArrayString`.
2024-01-27 12:46:55 +01:00
Andrew Poelstra e2b9555070
Merge rust-bitcoin/rust-bitcoin#2370: Improve units
7bf478373a Model `TooBig` and `Negative` as `OutOfRange` (Martin Habovstiak)
54cbbf804f Express `i64::MAX + 1` as `i64::MIN.unsigned_abs()` (Martin Habovstiak)
b562a18914 Move denomination error out of `ParseAmountError` (Martin Habovstiak)
5e6c65bc1a Clean up `unsigned_abs` (Martin Habovstiak)

Pull request description:

  Closes #2265
  Closes #2266

  Disclaimer: I did this in December and don't remember why I haven't pushed it. Maybe because it's somehow broken but I don't see how so please review a bit more carefully just in case.

ACKs for top commit:
  tcharding:
    ACK 7bf478373a
  apoelstra:
    ACK 7bf478373a

Tree-SHA512: 1f6e9adae9168bd045c9b09f06d9a69efd47ccc7709ac9ecaf48cb86e265b448b9b52a199ac5e6838d5029f5bc7514c5d7deb15a4d7c8a4606a353f390745570
2024-01-26 13:18:57 +00:00
Tobin C. Harding abe2241828
units: Remove "alloc" TODO
Remove the TODO and add an issue:

  https://github.com/rust-bitcoin/rust-bitcoin/issues/2389
2024-01-25 16:59:55 +11:00
Martin Habovstiak 7bf478373a Model `TooBig` and `Negative` as `OutOfRange`
The error returned when parsing amount had a `Negative` variant which
was weird/unreachable when parsing `SignedAmount`. Also weirdly, parsing
would return `TooBig` when the amount was negative - too low.

To resolve this we merge them into one `OutOfRange` variant that nuges
the consumers to make principled decisions and print error messages as
amounts being more than or less than a specific value which is easier to
understand for the users. Notably, the API still allows getting
information about which type was parsed and which bound was crossed but
in a less obvious way. This is OK since users who have a very good
reason can use this information but most won't.

Closes #2266
2024-01-24 11:37:46 +01:00
Martin Habovstiak 54cbbf804f Express `i64::MAX + 1` as `i64::MIN.unsigned_abs()`
This better conveys the intention that we're checking the lower bound.
2024-01-24 11:25:09 +01:00
Martin Habovstiak b562a18914 Move denomination error out of `ParseAmountError`
The `from_str_in` methods on amounts returned `ParseAmountError` which
contained `InvalidDenomination` among its variants. This one was never
returned because the method doesn't parse denomination.

This change separates the error out.

Closes #2265
2024-01-24 11:25:09 +01:00
Martin Habovstiak 5e6c65bc1a Clean up `unsigned_abs`
Previousle we copied `unsigned_abs` method from `core` because it was
unstable in older MSRV. Our current MSRV allows using the method
directly so this removes our old one and uses the one from standard
library instead.
2024-01-24 11:25:09 +01:00
Andrew Poelstra d08d3efdfa
Merge rust-bitcoin/rust-bitcoin#2336: units: Enable parsing Amount from `u64::MAX`
b2344e019d units: Assert roundtrip SignedAmount/str overflows (Tobin C. Harding)
baadcf4c0a units: Test that SignedAmount float conversion overflows (Tobin C. Harding)
d768f25da8 units: Remove duplicate assertion (Tobin C. Harding)
1d536ac8b2 units: Enable parsing Amount from u64::MAX (Tobin C. Harding)

Pull request description:

  Our `Amount` type uses an internal `u64` and we maintain no invariants on the inner value. Therefore we should be able to parse `u64::MAX`.

  Fix the parsing code by removing the explicit, incorrect check and fix unit tests to mirror this behaviour.

  Fix: #2297

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

Tree-SHA512: 944f8d0bfedc559f0444f75eca7d3fba042fbc204c4c032d09ff0edc29be280a3707f5b363dbc04f0d7bdf64701c0c4619e2e0de683d804a2663c2a20ac963f6
2024-01-22 19:13:12 +00:00
Tobin C. Harding b2344e019d
units: Assert roundtrip SignedAmount/str overflows
Add a unit test to prove that attempting to roundtrip a `SignedAmount`
greater than `MAX` through a string fails.
2024-01-22 09:14:26 +11:00
Tobin C. Harding baadcf4c0a
units: Test that SignedAmount float conversion overflows
We should not be able to roundtrip a `SignedAmount` value greater than
`MAX`, add a test to prove so.

While we are at it document the assertion above that proves we can parse
a float representing an `Amount` greater than `SignedAmount::MAX`.
2024-01-22 09:08:54 +11:00
Tobin C. Harding d768f25da8
units: Remove duplicate assertion
Unit test has a duplicate assertion, remove it.
2024-01-22 09:06:26 +11:00
Martin Habovstiak 22747149a9 Add convenience constants to `Denomination`
`Denomination::Bitcoin` and `Denomination::Satoshi` are often used,
especially in test code so this change adds `BTC` and `SAT` - short,
readable constants. Notably this doesn't add the other constants as that
would lead to either unidiomatic names or confusing casing (MSAT meaning
millisat not megasat) and they are not used that much anyway.
2024-01-20 22:12:52 +01:00
Tobin C. Harding 1d536ac8b2
units: Enable parsing Amount from u64::MAX
Our `Amount` type uses an internal `u64` and we maintain no invariants
on the inner value. Therefore we should be able to parse `u64::MAX`.

Fix the parsing code by removing the explicit, incorrect check and fix
unit tests to mirror this behaviour.

Fix: #2297
2024-01-15 08:56:57 +11:00
yancy 278229def5 Add allow for out of bounds indexing
Out of bounds indexing is a workaround for const panic until MSRV +1.57
2024-01-01 10:35:52 +01:00
Fmt Bot 5af7727250 2023-12-17 automated rustfmt nightly 2023-12-17 00:59:05 +00:00
Andrew Poelstra 2a6b4c1f43
Merge rust-bitcoin/rust-bitcoin#2262: Clean up `io` usage
f06d12455f bitcoin: Remove the custom sink (Tobin C. Harding)
b503aa1544 Run the formatter (Tobin C. Harding)
3ca55fb163 Remove qualifying path from Read and Write (Tobin C. Harding)
ebeb21fa7a Import fmt::Write using underscore (Tobin C. Harding)
e2dbcb1d28 Use W for writer generic type (Tobin C. Harding)
8704d9f0ae docs: Fix grammar (Tobin C. Harding)

Pull request description:

  A few cleanups to how we use the `io` crate, this is reasonably trivial but commit `a6c7e696 Remove qualifying path from Read and Write` is big, I have however gone to some effort to make sure it is easy to flick through the diff.

  Done in preparation for another go at the `BufRead` stuff.

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

Tree-SHA512: 751c489c67901c7563f1cc91f7761a4e3c276ae1981010338134e8c13200720ba69fcc74948c1dc1e6e65390197da0da27b2b69b86034029748321b404142cba
2023-12-12 20:07:06 +00:00
Jiri Jakes daa47b2061
Allow `SignedAmount` parse values equal to i64::MIN
Previously, parsing such textual value returned 'too big' error. This
change fixes it and adds relevant tests.
2023-12-12 18:33:44 +08:00
Tobin C. Harding ebeb21fa7a
Import fmt::Write using underscore
When we use the `fmt::Write` trait it is just to call its methods, we
can therefore, without any change to the logic, use `as _` when
importing the trait. This prevents naming conflicts.

Done in preparation for importing the `io::Write` trait.
2023-12-12 11:48:29 +11:00
Tobin C. Harding ae07bdbdbc
Remove ToOwned from prelude
We are not using the `ToOwned` trait, remove it.

Found by clippy.
2023-12-12 08:55:03 +11:00
Tobin C. Harding 396e049a7a
Use InputString instead of String
Done so that we can correctly support builds without an allocator.

Add two new error types that wrap `InputString`.
2023-12-11 08:53:13 +11:00
Tobin C. Harding acacf45edf
Add ParseDenominationError
We have two variants in the `ParseAmountError` that both come from
parsing a denomination, these should be a separate error.
2023-12-11 08:53:11 +11:00
Tobin C. Harding 69e56a64ed
Add bitcoin-units crate
Add a new crate `bitcoin-units`, move the `amount` module over to it and
re-export all types from `bitcoin::amount` so this as not a breaking
change.
2023-12-11 08:52:31 +11:00