Commit Graph

24 Commits

Author SHA1 Message Date
yancy 364e9ff775 Change method return type
Any SignedAmount can now be cast to Amount since the range is the same.
Specifically, the range for SignedAmount is (- 21 million, 21 million)
while the range for Amount is (0, 21 million).  Therefore any value from
Amount can be cast to a SignedAmount and it will work.  Note it's not
the same and still requires checking when going from SignedAmount to
Amount since Amount can't handle the negative range.

As a side effect of changing the return type, TryFrom is no longer valid
and does not compile.  Therefore in addition to changing the return
type, TryFrom is also removed.
2024-12-24 11:31:49 -06:00
merge-script ebe43b6f87
Merge rust-bitcoin/rust-bitcoin#3757: Test types MIN/MAX instead of i64::MIN/i64::MAX
f08d8741d3 Test types MIN/MAX instead of i64::MIN/i64::MAX (yancy)

Pull request description:

  The MIN/MAX for SignedAmount recently changed from i64::MIN and i64::MAX to MAX_MONEY/MIN_MONEY.  Update the tests to reflect this new MIN/MAX since it is no longer valid to create a value above or bellow MAX_MONEY/MIN_MONEY.

ACKs for top commit:
  apoelstra:
    ACK f08d8741d39685b636830680bb891bd414826e88; successfully ran local tests
  tcharding:
    ACK f08d8741d3

Tree-SHA512: 563408240dffaf95f88a9d570e56f9b9b161b422cb59a89828c18b9c784e7acb717f57fe55c80411f104443ac2a3f908f2a98ab1a4b34edab69b6946a723b30c
2024-12-18 14:35:37 +00:00
merge-script c111416401
Merge rust-bitcoin/rust-bitcoin#3704: units: Enable pedantic clippy lints
2513e05501 api: Run just check-api (Tobin C. Harding)
9619f68956 units: Move excluded lints to manifest (Tobin C. Harding)
5290a93a38 units: Add all pedantic lints (Tobin C. Harding)

Pull request description:

  Add all pedantic clippy lints but leave a few marked as TODO.

  The coment on the list claims to it is an exhaustive list of pedantic lints, that claim is going to go stale. I would be nice if we had tooling to catch new lints as they were added.

ACKs for top commit:
  apoelstra:
    ACK 2513e05501e3a014c097f24eb9178c291785db81; successfully ran local tests

Tree-SHA512: 33b2a7448d49d6a5571c9e4e9922b6042ab03aaaa9f7acad243a926f8a03a0ffed75d4f5f37be4705f23862c32f96879582214cd17c7e5ab81e47517a84745e0
2024-12-18 02:38:02 +00:00
Tobin C. Harding 5290a93a38
units: Add all pedantic lints
Add all the pedantic lints to the repository by way of the repository
manifest. Then enable these lints in the `units` manifest.

Some things worth mentioning:

- Fix `needless_pass_by_value` by adding derives to `FormatOptions`.
- Fix lint `cast_lossless` using `cargo clippy --fix``
- While fixing `lint enum_glob_use` introduce a new style to the
codebase; import enums using a single character. Doing so prevents
namespace clashes, improves clarity, and maintains terseness.

Audit:

Use the following lints locally and audit all the warnings, they produce
many false positives so we can't enable them permentently.

- `cast_possible_truncation`
- `cast_possible_lint`
- `cast_sign_loss`
2024-12-18 08:25:12 +11:00
yancy 01af266335 Remove deprecated tests
Unchecked methods have been removed in a previous commit, therefore the
tests are no longer applicable.
2024-12-17 13:12:17 -06:00
yancy f08d8741d3 Test types MIN/MAX instead of i64::MIN/i64::MAX
The MIN/MAX for SignedAmount recently changed from i64::MIN and
i64::MAX to MAX_MONEY/MIN_MONEY.  Update the tests to reflect this new
MIN/MAX since it is no longer valid to create a value above or bellow
MAX_MONEY/MIN_MONEY.
2024-12-17 12:19:17 -06:00
merge-script 8d508e0deb
Merge rust-bitcoin/rust-bitcoin#3728: units: Unify and flesh out ops impls
68f3c3e5d7 api: Run just check-api (Tobin C. Harding)
b956940a6d Add ops unit tests for amount types (Tobin C. Harding)
d88306fb68 units: Implement ops for amount types (Tobin C. Harding)
4a3aa4a08b Add ops unit tests for Weight (Tobin C. Harding)
43ef9a618c units: Implement ops for Weight (Tobin C. Harding)
c5fbc7e117 units: Add assign macros (Tobin C. Harding)
20a79da344 units: Use one level of path for ops traits (Tobin C. Harding)
6ce5385914 units: Add macros for Add and Sub (Tobin C. Harding)
b31ca72b33 units: Use rhs instead of other (Tobin C. Harding)
cd0fa2d1dc Make FeeRate ops impls more terse (Tobin C. Harding)

Pull request description:

  Sort out `Add`, `AddAssign`, `Sub`, and `SubAssign` for the following types:

  - `Amount`
  - `SignedAmount`
  - `FeeRate`
  - `Weight`

  And clean up as we go.

  Note that `BlockInterval` isn't touched in this PR - it looks correct already.

  The unit tests in the two patches "Add ops tests ..." can be moved if you want to verify that they don't build without the patch they follow.

ACKs for top commit:
  apoelstra:
    ACK 68f3c3e5d728a60a4b52ca64523770bcdd32f957; successfully ran local tests

Tree-SHA512: a82d3bf288f61b169b8cff498e81bd2cd123c8dcbf534413233ff03f06102a42508e09b2f7e5b268b21f82d4bf2b3612cd88dea1231b4d3e6455c7e99f82e729
2024-12-17 03:52:24 +00:00
merge-script 61744ef568
Merge rust-bitcoin/rust-bitcoin#3739: Add units tests for cBTC
183ecf1fe6 Add units tests for cBTC (Tobin C. Harding)

Pull request description:

  We added the "cBTC" denomination but forgot to test it.

  Add units test for "cBTC" as we do for the other denominations. Put all the tests lines of code in the same order as the enum variants so a reader can see easily that all cases are tested.

ACKs for top commit:
  apoelstra:
    ACK 183ecf1fe6944187151036e36d6726fae9b64392; successfully ran local tests

Tree-SHA512: 93a3f3e5430f7b6f525d7c55ad3d318f8b30fef8cdef24ea92a1687e898f73d3829875861f026af70dde013743d310d87331b5654b524f4ee90c20f7e6ed7f7c
2024-12-16 23:11:59 +00:00
Tobin C. Harding b956940a6d
Add ops unit tests for amount types
Copy the unit tests from `FeeRate` that prove `Add`, `AddAssign`, `Sub`,
and `SubAssign` for references.
2024-12-17 08:20:47 +11:00
Tobin C. Harding 183ecf1fe6
Add units tests for cBTC
We added the "cBTC" denomination but forgot to test it.

Add units test for "cBTC" as we do for the other denominations. Put all
the tests lines of code in the same order as the enum variants so a
reader can see easily that all cases are tested.
2024-12-16 13:36:54 +11:00
Tobin C. Harding 99b32c95b8
Change paramater type used for whole bitcoin
We have multiple constructors for `Amount`, one of which accepts an
integer representing whole bitcoin.

In an attempt to make the API crystal clear change the parameter type
used for whole bitcoin to either `Into<u64>` or `u32` instead of a
`u64` which may well be mapped to sats in the mind of developers (it
is in mine anyway).
2024-12-16 13:09:28 +11:00
Fmt Bot 18d904d647 2024-12-15 automated rustfmt nightly 2024-12-15 01:35:28 +00:00
Tobin C. Harding edfdd575d5
Test all the valid denomination forms 2024-12-13 09:01:28 +11:00
Jamil Lambert, PhD 4b926e1908
Change`MAX and `MIN` to equal `MAX_MONEY`
To prevent rounding errors converting to and from f64 change
`SignedAmount` `MAX` and `MIN` to +/- `MAX_MONEY` which are within the
limit in f64 that has issues.

Add checks to `from_str_in`, `checked_add`,  `checked_sub` and
`checked_mul` that the result is within MIN and MAX.

Modify tests to work with new `MIN` and `MAX`
2024-12-11 08:15:48 +00:00
merge-script 087427cf85
Merge rust-bitcoin/rust-bitcoin#3679: Support serde serializing Amount as string
c27f443520 Add basic unit tests for Amount serde (Tobin C. Harding)
22530f6a2b Support serde serializing Amount as string (Tobin C. Harding)

Pull request description:

  Sometimes JSON parsers may munge floats. Instead of using `f64` we can serialize BTC amounts as strings.

  Close: #894

ACKs for top commit:
  apoelstra:
    ACK c27f4435208cc3ca7b98580fd7e2784e089b545e; successfully ran local tests
  sanket1729:
    utACK c27f443520.

Tree-SHA512: 084669a0622557b75fceae732fb485e7139ecada48c0b65642d122e1a02f6f7e41564c3579fd10adbf3aa14c82c9f10abc3f9201858e50b729852140b31a4216
2024-12-11 04:18:53 +00:00
Jamil Lambert, PhD 6950c0a7b5
Change `Amount::MAX` to equal `MAX_MONEY`
To prevent rounding errors converting to and from f64 change
`Amount::MAX` to `MAX_MONEY` which is below the limit in f64 that has
issues.

Add checks to `from_str_in`, `checked_add` and `checked_mul` that the
result is below MAX, where previously a u64 overflow was relied on.

Change tests to account for new lower MAX that is within the range of
SignedAmount and does not overflow so easily

Remove overflow tests

`Amount::MAX` is now below `u64::MAX` and within the range of values for
`SignedAmount`.   These tests therefore do not overflow.
In effective_value there is no error with `Amount::MAX` and the correct
value is returned.
In psbt the removed test is effectively the same as the previous test.

Modify `Amount` tests to work with new `MAX`

Tests need to be changed that checked values above the new `MAX` or
`Amount::MAX` was out of range for `SignedAmount` which it isn't anymore
2024-12-04 14:17:00 +00:00
Tobin C. Harding 22530f6a2b
Support serde serializing Amount as string
Sometimes JSON parsers may munge floats. Instead of using `f64` we can
serialize BTC amounts as strings.

Includes addition of `alloc` feature gate to `DisplayFullError` to
remove lint warnings when building with `--no-default-features`.

Close: #894
2024-12-03 08:42:41 +11:00
merge-script 58b087d946
Merge rust-bitcoin/rust-bitcoin#3674: Close `amounts` error types
fd2a5c1ec7 Close amounts error types (Tobin C. Harding)
23c77275b1 Reduce code comment lines (Tobin C. Harding)
d595f421c6 Remove whitespace between enum variants (Tobin C. Harding)

Pull request description:

  Close the two pubic enum error types in `units::amounts`. All the other structs are closed already because they either have private fields or marked `non_exhaustive`.

ACKs for top commit:
  apoelstra:
    ACK fd2a5c1ec79f337fb3695c030c9fb6b4671468f2; successfully ran local tests; thanks!

Tree-SHA512: f8d68ef821449e0829c926cf527df4b226b29c8d1d41b320a016fbf70b4b39cc54c8c218955caa0c3776158eeeae0ebacc1cc89dab67bafc399b94063324ab0e
2024-12-02 03:04:07 +00:00
Tobin C. Harding fd2a5c1ec7
Close amounts error types
Close the two pubic enum error types in `units::amounts`. All the other
structs are closed already because they either have private fields or
marked `non_exhaustive`.
2024-12-02 09:14:14 +11:00
Tobin C. Harding 0369e64b56
Implement Sum for an iterator of references to amounts
We have `iter::Sum` already for `Amount` and `SignedAmount`. Add
an implementation for each to support iterators that yield references.
2024-11-28 15:51:29 +11:00
yancy 8b928a1515 Split checked_div_by_weight into floor and ceiling version 2024-11-07 16:28:44 -06:00
Tobin C. Harding 42e5043b33
Add from_int_btc group of functions
Add/update the from_int group of functions to provide one that errors
and one that is const and panics (errors in const context are not useful
because one cannot call `unwrap` in const context).
2024-11-02 10:19:03 +11:00
Tobin C. Harding 87c9a3fd11
amount: Format tests file
Done as a separate patch so that the diff of the verification code move
was less noisy.
2024-10-31 12:29:21 +11:00
Tobin C. Harding e0bc68042d
amount: Move test code to submodule
In preparation for splitting the two amounts into separate files; move
the `amount` module code to a submodule.

Internal change only.
2024-10-31 12:29:20 +11:00