Commit Graph

692 Commits

Author SHA1 Message Date
Fmt Bot 81dbfae0a8 2025-06-15 automated rustfmt nightly 2025-06-15 01:46:36 +00:00
merge-script 052514e6ff
Merge rust-bitcoin/rust-bitcoin#4615: Remove reachable unreachable call in psbt
e7c90c57e7 Remove reachable unreachable call in psbt (Tobin C. Harding)
c736e73ae0 Add unwrap_or and unwrap_or_else to NumOpResult (Tobin C. Harding)

Pull request description:

  A bunch of changes have been implemented lately in the fee calculation logic. As a result of this a at once time `unreachable` statement is now reachable - bad rust-bitcoin devs, no biscuit.

  Saturate to `FeeRate::MAX` when calculating the fee rate, check against the limit arg, and win.

  (Patch 1 adds the new combinators to `NumOpResult`. This was pulled out of #4610.)

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

Tree-SHA512: d33b3d5d4442fb17cae4c52880bc5dafdd611d686c6923744bc5f218761e8bff32fc5ab169af9f2ed2f02011516ed850ac8c6bd4ce1d6de40c0ac0b17486bbb4
2025-06-13 18:04:41 +00:00
Tobin C. Harding 20c84ce444
units: Make fee module public
The `fee` module is now empty as far as API surface. It still holds the
`core::ops` impls for fee calculation. It also does have useful
rustdocs which are not currently visible online because module is
private.

Make the module public. Functionally all this does is provide a place
for the module level docs under a discoverable name.

Improve the docs by adding a bunch of links to fee related functions.
2025-06-13 08:47:19 +10:00
Tobin C. Harding 251e6a85da
Inline checked mul function back into weight module
A while back we move all the 'fee' stuff into a separate module
because I thought it would help with clarity - I was wrong.

Move the checked mul function back into the `weight` module on the main
`Weight` impl block.

Internal change only - code move.
2025-06-13 08:47:16 +10:00
Tobin C. Harding a8610a937b
Inline checked mul / to fee back into fee_rate module
A while back we move all the 'fee' stuff into a separate module
because I thought it would help with clarity - I was wrong.

Move the checked mul and to fee functions back into the `fee_rate`
module on the main `FeeRate` impl block.

Internal change only - code move.
2025-06-13 08:47:15 +10:00
Tobin C. Harding e17c391a3c
Inline checked div functions back into unsigned module
A while back we move all the 'fee' stuff into a separate module
because I thought it would help with clarity - I was wrong.

Move the checked div functions back into the `unsigned` module on the
main `Amount` impl block.

Internal change only - code move.
2025-06-13 08:45:57 +10:00
Tobin C. Harding c736e73ae0
Add unwrap_or and unwrap_or_else to NumOpResult
Two useful combinators, add them.

I copied the function signature and docs from stdlib and wrote the
functions myself - thereby operating within licensing requirements.
2025-06-12 11:33:16 +10:00
Tobin C. Harding 6ed3fd6234
Add fee rate constructors that take Amount as arg
Some users may find it more ergonomic to pass in an `Amount` when
constructing fee rates. Also the strong type adds some semantic meaning
as well as imposes the `Amount::MAX` limit.

Add an equivalent constructor for each of the existing ones that uses an
argument of type `Amount` instead of `u64` sats.
2025-06-12 10:45:18 +10:00
Tobin C. Harding c1a760bf60
units: Use singular in rustdoc
Satoshis per virtual byte is grammatically better than satoshis per
virtual bytes - I think.
2025-06-12 10:44:53 +10:00
merge-script 9b88d87020
Merge rust-bitcoin/rust-bitcoin#4606: units: Improve docs
7fbe07a6e0 Use uniform docs for overflow (Tobin C. Harding)
153a6a2f3c Make Weight docs uniform with FeeRate (Tobin C. Harding)
c87f7292be Fix rustdocs on Weight (Tobin C. Harding)
02b523a8ad Remove whitespace from encapsulate module (Tobin C. Harding)

Pull request description:

  Make a sweep of the `units` crate's rustdocs.

ACKs for top commit:
  apoelstra:
    ACK 7fbe07a6e0b3e398aca845d64ec86f3f0068edf4; successfully ran local tests

Tree-SHA512: ba50f3afb94dda43f89d04eb53c6e85df302292d4647fe81a20e3f7d1ca75e8ee8cdf6548864b2f3c33ed661205d109dbd763db1061ea45a59eab25f134191f8
2025-06-10 19:41:46 +00:00
Tobin C. Harding 7fbe07a6e0
Use uniform docs for overflow
Trivial change but make all the docs (rustdocs and code comments) use
the same phrase when describing overflow.
2025-06-10 06:43:52 +10:00
Tobin C. Harding 153a6a2f3c
Make Weight docs uniform with FeeRate 2025-06-10 06:39:30 +10:00
Tobin C. Harding c87f7292be
Fix rustdocs on Weight
There is no point linking to `Weight` in the rustdocs of `Weight`. Also
`wu` does not exist in this context, prefer 'weight units'.
2025-06-10 06:39:30 +10:00
Tobin C. Harding 02b523a8ad
Remove whitespace from encapsulate module
Whitespace here is unnecessary. Whitespace only, no logic change.
2025-06-10 06:39:30 +10:00
Tobin C. Harding b843f1356d
units: Access with getters instead of inner field
The `block` module does not follow the `encapsulate` pattern but we can
still use the getters instead of accessing the inner field directly.

Refactor, no logic change.
2025-06-10 06:38:08 +10:00
merge-script c6c690a8f0
Merge rust-bitcoin/rust-bitcoin#4603: units: Add `must_use` to checked arithmetic functions
afc0ce6175 units: Add must_use to checked arithmetic functions (Tobin C. Harding)

Pull request description:

  The checked arithmetic functions all consume self so we use `must_use` to help users not miss this point.

  Most are done, add the missing ones.

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

Tree-SHA512: 7105affff43827ed47a1c0b6e41a996aa538c7d53b891faf03e79a83164706d7e86db5fb184ac740fdf57bb43f8401a496cc64ea4da0da71eaa8c8cca16444c7
2025-06-09 12:36:54 +00:00
Tobin C. Harding afc0ce6175
units: Add must_use to checked arithmetic functions
The checked arithmetic functions all consume self so we use `must_use`
to help users not miss this point.

Most are done, add the missing ones.
2025-06-09 00:09:46 +10:00
Fmt Bot 4b5c6dd547 2025-06-08 automated rustfmt nightly 2025-06-08 01:44:53 +00:00
merge-script a1be2fdf73
Merge rust-bitcoin/rust-bitcoin#4593: units: Add `is_satisfied_by` locktime tests
2fa5c062d5 Add is_satisfied_by locktime tests (Jamil Lambert, PhD)

Pull request description:

  Weekly mutation testing found new mutants in both `Height::is_satisfied_by` and `MedianTimePast::is_satisfied_by` functions.

  Test these two functions and kill the mutants.

  Closes #4587

ACKs for top commit:
  tcharding:
    ACK 2fa5c062d5
  apoelstra:
    ACK 2fa5c062d5e07580bdb7ea5e4c58e4607c716ecc; successfully ran local tests

Tree-SHA512: 28d69cdf575bb17eff6d685b1fee05e3f9a821c8796c82655b2d2eda6ee1d9dc79043853fbe0475f6bdb548cef52ac710b3c632f7784788035392e29e70ce48e
2025-06-06 18:25:58 +00:00
Jamil Lambert, PhD 2fa5c062d5
Add is_satisfied_by locktime tests
Weekly mutation testing found new mutants in both height and median time
past `is_satisfied_by` functions.

Test these two functions and kill the mutants.
2025-06-05 11:27:25 +01:00
merge-script fa07198f21
Merge rust-bitcoin/rust-bitcoin#4511: Modify `locktime` `serde` implementations
4621d2bde1 Modify locktime serde implemenations (Tobin C. Harding)
200c276315 bitcoin: Make test code spacing uniform (Tobin C. Harding)

Pull request description:

  Patch 1 is preparatory clean up. Patch 2 is the meat and potatoes. See commit log there for full explanation.

  Briefly:

      - Remove `serde` stuff from `units::locktime`
      - Manually implement `serde` traits on `relative::LockTime`
      - Fix the regression test to use the new format

ACKs for top commit:
  apoelstra:
    ACK 4621d2bde14d71b3d6ef0b14258a7479c049ba3b; successfully ran local tests

Tree-SHA512: bc96d79dd4d9f5a124c22f0d3be4750cb7b6e86ba448b31e233982567578337d331b2582c6e1f953c00e8393c4a4552c4b574fe8a55323c911115b269b24090e
2025-06-04 17:32:02 +00:00
merge-script a13ba99c11
Merge rust-bitcoin/rust-bitcoin#4534: Make `FeeRate` use MvB internally
56516757ad Add code comment to amount calculation (Tobin C. Harding)
8cf1dc39b5 Fix incorrect code comment (Tobin C. Harding)
7c186e6081 Refactor fee functions (Tobin C. Harding)
bf0776e3dd Remove panic using checked arithmetic (Tobin C. Harding)
b65860067f Make fee functions const (Tobin C. Harding)
9b2fc021b2 Improve rustdocs on FeeRate (Tobin C. Harding)
1bd1e89458 Re-introduce FeeRate encapsulate module (Tobin C. Harding)
b27d8e5819 Change the internal representation of FeeRate (Tobin C. Harding)
2e0b88ba76 bitcoin: Fix dust 'fee' identifiers (Tobin C. Harding)
399bca531c Reduce the FeeRate::MAX value (Tobin C. Harding)
d174c06a4a Saturate to_fee to Amount::MAX (Tobin C. Harding)
64ac33754f Add missing argument docs (Tobin C. Harding)
fe0a448e78 Temporarily remove const from fee calc function (Tobin C. Harding)
b929022d56 Add floor/ceil versions of to_sat_per_kwu (Tobin C. Harding)
64098e4578 Remove encapsulate module from fee rate (Tobin C. Harding)

Pull request description:

  The `FeeRate` is a bit entangled with amount and weight.  Also we have an off-by-one bug caused by rounding errors and the fact that we use kwu internally.

  We can get more precision in the fee rate by internally using per million virtual bytes.

  - Fix: #4516
  - Fix: #4497
  - Fix: #3806

ACKs for top commit:
  apoelstra:
    ACK 56516757ad8874d8121dba468946546bb8fd7d4e; successfully ran local tests

Tree-SHA512: 0c6e7e2c9420886d5332e32519838d6ea415d269abda916e51d5847aa2475c87fa4abfc29b5f75e8b10c44df67ae29d823aa93f7cfabbc89eb27f2173b103242
2025-06-04 16:42:55 +00:00
Tobin C. Harding 4621d2bde1
Modify locktime serde implemenations
The `units::locktime` types are used for two things:

- They are the inner types of `primitives` `LockTime`s
- They are used ephemerally for checking satisfaction

Neither of these use cases requires `serde` impls for the `units` types.
Since we are trying to release 1.0 with minimal amounts of code we
should remove them.

For `LockTime`s that need to be stored on disk or go over the wire we
can manually implement the `serde` traits. For `absolute::LockTime` this
is done already and there is no reason the `relative::LockTime` impl
cannot be the same [0]. This differs from the current `serde` trait impls
but we have already decided that in 0.33 we are going to accept breakage
and direct users to use 0.32 to handle it.

- Remove `serde` stuff from `units::locktime`
- Manually implement `serde` traits on `relative::LockTime`
- Fix the regression test to use the new format

While we are at it use a uniform terse call in `serialize`.

[0] This is because there is an unambiguous encoding for the whole set
of locktimes - consensus encoding.
2025-06-01 14:07:33 +01:00
merge-script 6d8299e8b8
Merge rust-bitcoin/rust-bitcoin#4468: Improve lock times - fix off-by-one bug
4ccecf5dec Fix stale Height type link (Tobin C. Harding)
caebb1bf73 units: relative: Do minor rustdocs fixes (Tobin C. Harding)
40bb177bc2 Put is_satisfied_by functions together (Tobin C. Harding)
480a2cd62a Favour new function `from_mtp` over deprecated (Tobin C. Harding)
f9d6453d5b Shorten locktime type term (Tobin C. Harding)
727047bd39 Fix off-by-one-bug in absolute locktime (Tobin C. Harding)
3ffdc54ca5 Fix off-by-one bug in relative locktime (Tobin C. Harding)
a2ff8ddbbb Improve relative::LockTime is_satisfied_by_{height, time} (Tobin C. Harding)

Pull request description:

  Make the APIs uniform in relative and absolute locktimes in relation to the `is_satisfied_by` functions. In doing so improve the API and fix an off-by-one bug when checking satisfaction of locks by height.

  Done in three patches but maybe should be squashed? Probably easiest to review by looking at all the `is_satisfied_by*` functions and convincing yourself we got it right.

  EDIT: Now has 5 cleanup patches also (mostly docs cleanups).

ACKs for top commit:
  apoelstra:
    ACK 4ccecf5decfead9818b74fbdee73115c349e2f3e; successfully ran local tests

Tree-SHA512: 9206cb464a06647510a35a7d564062823117e75df60251969be458616f4f5d04acf0aada53dbf7d493a2a2a72d26b3a300417a6499e45413d5f2a011538b7826
2025-05-31 15:48:29 +00:00
Tobin C. Harding 7c186e6081
Refactor fee functions
The `fee` functions are a bit convoluted because of the usage of
`const`. Refactor a couple of them to be easier to read.

Internal change only.
2025-05-31 07:52:35 +01:00
Tobin C. Harding bf0776e3dd
Remove panic using checked arithmetic
During dev I introduced a pancic, remove it.
2025-05-31 07:52:35 +01:00
Tobin C. Harding b65860067f
Make fee functions const
We temporarily removed `const` in the `fee` module to make patching and
reviewing easier. Now add it back in.
2025-05-31 07:52:35 +01:00
Tobin C. Harding 9b2fc021b2
Improve rustdocs on FeeRate
Remove the unit from associated consts and then make all the rustdocs on
the various consts use 'The'.
2025-05-31 07:52:34 +01:00
Tobin C. Harding 1bd1e89458
Re-introduce FeeRate encapsulate module
Now we have the `fee_rate` module clened up re-introduce the
`encapsulate` module using MvB.
2025-05-31 07:52:34 +01:00
Tobin C. Harding b27d8e5819
Change the internal representation of FeeRate
To get more precision use sats per million virtual bytes.

To make review easier keep most calls in tests using
`FeeRate::from_sats_per_kwu` and just unwrap. These can likely be
cleaned up later on if we want to.

For `serde` just change the module to `_floor` and leave it at that. The
serde stuff likely needs re-visiting before release anyways.
2025-05-31 07:52:31 +01:00
Tobin C. Harding 2e0b88ba76
bitcoin: Fix dust 'fee' identifiers
Currently we get the fee_rate per kwu then multiply it by 4. Instead
lets add a per_kvb function to `FeeRate`. We are about to change the
internal representation of `FeeRate` to use MvB so for now just panic on
ovelflow.

Also these are fee _rates_ - we already have suboptimal names from Core
in the consts in `policy`, no need to let them infect other identifiers.
2025-05-31 07:52:01 +01:00
Tobin C. Harding 399bca531c
Reduce the FeeRate::MAX value
In preparation for changing the internal representation of `FeeRate` to
use MvB reduce the max value by 4_000.

Done separately to make the change explicit.
2025-05-31 07:52:00 +01:00
Tobin C. Harding d174c06a4a
Saturate to_fee to Amount::MAX
The `FeeRate::checked_mul_by_weight` function currently returns a
`NumOpResult` but all other `checked_` functions return an `Option`.
This is surprising and adds no additional information.

Change `checked_mul_by_weight` to return `None` on overflow. But in
`to_fee` saturate to `Amount::MAX` because doing so makes a few APIs
better without any risk since a fee must be checked anyway so
`Amount::MAX` as a fee is equally descriptive in the error case.

This leads to removing the `NumOpResult` from `effective_value` also.

Note that sadly we remove the very nice docs on `NumOpResult::map`
because they no longer work.

Fix: #4497
2025-05-31 07:52:00 +01:00
Tobin C. Harding fe0a448e78
Temporarily remove const from fee calc function
Code that works with `const` is annoying to use and hard to reason
about. Just remove all the consts for now so we can hack on `FeeRate`.

Introduces two lint warnings about manual implementation of `map` but
they will go away later.
2025-05-31 07:51:59 +01:00
Tobin C. Harding b929022d56
Add floor/ceil versions of to_sat_per_kwu
In preparation for changing the inner representation of `FeeRate` add
floor and ceil versions of the getter function `to_sat_per_kwu`.

For now both functions return the same thing but still call the
correct one so that when we change the representation we do not need
to re-visit them.
2025-05-31 07:51:59 +01:00
Tobin C. Harding 64098e4578
Remove encapsulate module from fee rate
This module is a PITA to work on, just remove it until the dust settles
on fee rate.

While we are at it make the rustdocs on the getter more terse.
2025-05-31 07:51:59 +01:00
yancy 7b52b9c3a3 Fix typo 2025-05-28 20:06:31 -05:00
merge-script 6c228a3626
Merge rust-bitcoin/rust-bitcoin#4507: units: Move code out of wrapper macro
2a3e606d89 units: Move some things out of impl_u32_macro (Tobin C. Harding)

Pull request description:

  Recently we added a private `impl_u32_macro`. It included a bunch of associated consts and a pair of u32 constructor/getter functions.

  We overlooked the fact that the macro produces incorrect docs.

  Move the offending code out of the macro and into the already existent impl block for each type.

  Docs only, no other logic change.

ACKs for top commit:
  apoelstra:
    ACK 2a3e606d89d25ce54ff91b38b0aa29f7f733b9d9; successfully ran local tests

Tree-SHA512: ad4bdbb35bc674e9664e293841e14dc2374c8baddf3e795edb666c75860f02728e939ef5a93ede6f4c951e92c5dd5368d6a6b9662cf6d5b268f73ab5ac97e2cc
2025-05-28 15:24:55 +00:00
Tobin C. Harding 2a3e606d89
units: Move some things out of impl_u32_macro
Recently we added a private `impl_u32_macro`. It included a bunch of
associated consts and a pair of u32 constructor/getter functions.

We overlooked the fact that the macro produces incorrect docs.

Move the offending code out of the macro and into the already existent
impl block for each type.

Docs only, no other logic change.
2025-05-27 08:36:18 +01:00
yancy 9dac4d69e0 Implement CheckedSum for Weight iterator
Expose `checked_sum` method to sum an iterator of Weights checked.
2025-05-26 19:01:56 -05:00
yancy 0dbcd09bbc Move CheckedSum trait to crate root
In order to add other types to CheckedSum, remove from the Amount
module.  In so doing, other types added to CheeckSum do not need to be
imported into Amount.
2025-05-26 19:01:42 -05:00
Fmt Bot c73131c8f0 2025-05-25 automated rustfmt nightly 2025-05-25 01:42:57 +00:00
Tobin C. Harding dc2cbc21f9
Make fee_rate test identifiers uniform
We have a bunch of unit tests, some use `f` and some use `fee_rate`.
Uniform would be better.

Elect to use the longer form just because there are only 4 instances of
the shorter one (although I personally prefer the shorter form).
2025-05-24 11:37:19 +10:00
Tobin C. Harding 2e16dbb7e5
fee_rate: Put test assertions in correct order
By convention we put assertions in the order `got` then `want`. This
makes debugging easier.
2025-05-24 11:36:28 +10:00
merge-script 1c07916777
Merge rust-bitcoin/rust-bitcoin#4538: Use `_u32` in `FeeRate` constructor instead of `_unchecked`
a1ce2d1ac8 Use _u32 in FeeRate constructor instead of _unchecked (Tobin C. Harding)

Pull request description:

  When constructing an `Amount` it was observed recently that a `u32` is often big enough. For the same reason we can use a `u32` to construct a fee rate instead of overflowing.

  Use `_u32`in the constructor and remove the panic.

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

Tree-SHA512: 339974c954ad613b0be684f7b2fa1402f59fe401969f3785a702ffbb14ac913ecf4c8228240d068ba4b482e38263590154167a071d823ccc9b4d0691036ca484
2025-05-23 16:18:59 +00:00
merge-script efae4cac8a
Merge rust-bitcoin/rust-bitcoin#4544: use Self:: instead of type aliases in error impls
0a0e23fedf style: use Self:: instead of type aliases in error impls (frankomosh)

Pull request description:

  Replace `use FooError as E` with `Self::` , during pattern matching
  on the same type in Display/Error trait implementations.

  close #4536

ACKs for top commit:
  tcharding:
    ACK 0a0e23fedf
  apoelstra:
    ACK 0a0e23fedf3552df677b379d86a1e0ac6b063152; successfully ran local tests

Tree-SHA512: 25c45a890635f990afc3bc09096929f8793007852ac435f061348bff2bd79e3faabf034d1e1e277596f4f7365477f477798f1b1e0c4b918c5b0fa08f8c49e732
2025-05-23 12:39:35 +00:00
merge-script 7dcff506dd
Merge rust-bitcoin/rust-bitcoin#4542: Fix FeeRate::checked_add/sub
395252c6d9 Fix FeeRate::checked_add/sub (Tobin C. Harding)

Pull request description:

  Currently the `checked_add` and `checked_sub` functions use a `u64` for the right hand side. This leaks the inner unit because the RHS value is implicitly in the same unit.

  Make the functions have `FeeRate` on the RHS.

ACKs for top commit:
  apoelstra:
    ACK 395252c6d95fdd581ce5e8ad5d7978515aafea23; successfully ran local tests; will one-ACK merge as this is units-only

Tree-SHA512: 1f0893f75c47f720ac741ace0274171ed24efcb6d2724d0fed941899d43b213e165b97aa050d6e40eea78c527af45d090a81b9d6cbd95835ef7105585786fca6
2025-05-23 03:57:18 +00:00
merge-script 180d9286d5
Merge rust-bitcoin/rust-bitcoin#4540: Remove `impl From<u64> for FeeRate`
c63f80baec Remove impl From<u64> for FeeRate (Tobin C. Harding)

Pull request description:

  This function leaks the inner unit of `FeeRate`. We want to change the unit, best to break downstream so they notice.

ACKs for top commit:
  apoelstra:
    ACK c63f80baec0780622d70e4c8699369b0a972cb62; successfully ran local tests; will one-ACK merge as this is units-only

Tree-SHA512: 5748f2a4cb29d6554226fe20c5479cb53081da8c2788ac31abfe1a2edb4d17f13a3b3037a840fbdc29e842af3e1accc27835fd5429c7355c1351eb8883943fdc
2025-05-23 02:51:00 +00:00
frankomosh 0a0e23fedf style: use Self:: instead of type aliases in error impls
Use  instead of 'use FooError as E' when pattern matching
on the same type in Display/Error trait implementations.
2025-05-22 20:28:33 +03:00
Tobin C. Harding c63f80baec
Remove impl From<u64> for FeeRate
This function leaks the inner unit of `FeeRate`. We want to change the
unit, best to break downstream so they notice.
2025-05-22 10:55:41 +10:00