Commit Graph

470 Commits

Author SHA1 Message Date
merge-script 5f1700e79f
Merge rust-bitcoin/rust-bitcoin#4181: Implement `Rem` for `Weight`
8007840676 Add a test for remainder (Jamil Lambert, PhD)
4787aa1f89 Implement Rem for Weight (Jamil Lambert, PhD)

Pull request description:

  Weight implements `Div` but not `Rem`.

  Add the `Rem` implementation.

  Add a test for the remainder operation on `Weight`

  Close #4171

ACKs for top commit:
  Kixunil:
    ACK 8007840676
  tcharding:
    ACK 8007840676
  apoelstra:
    ACK 80078406768aa5f8e4d21d42cc1d1fe9abeed1ea; successfully ran local tests

Tree-SHA512: cfcbc49a944146f7ba24a2f7b7f79ed2e2758ba2a23e9e69d9662afb5379dd74c646ff787c0e8218053e70410353cb74115e4c03bfdaafc8a1f9a4b0e51a3e15
2025-03-04 17:42:14 +00:00
Jamil Lambert, PhD 8007840676
Add a test for remainder
A remainder operation has been implemented for `Weight`.

Test the functionality of remainder with both a `Weight` and `u64`
2025-03-03 20:47:01 +00:00
Jamil Lambert, PhD 4787aa1f89
Implement Rem for Weight
`Weight` implements `Div` but not `Rem`

Add `Rem` impl for `Weight`
2025-03-03 20:43:21 +00:00
merge-script 6483244280
Merge rust-bitcoin/rust-bitcoin#4170: Improve crate docs headings
83d071e54b chacha20: Add whitespace (Tobin C. Harding)
4451724d31 chacha20: Add a docs heading (Tobin C. Harding)
d4417f9666 io: Improve crate docs heading (Tobin C. Harding)
c466554948 hashes: Improve crate docs heading (Tobin C. Harding)
6f4eb60936 Improve docs crate headings (Tobin C. Harding)

Pull request description:

  Make them all uniform after taking 2 minutes online to try find a nice format.

ACKs for top commit:
  apoelstra:
    ACK 83d071e54be0bc4ebd760a490a3ca887c0bf90a8; successfully ran local tests; lgtm

Tree-SHA512: 6f08c6cda91a7a870f1080b497f89607ac3d6b3c0234cbd2ba2da8710d46816398acac0bca2a49a3bc9466b814ae446842d3d304a3735df9f983e3ff5df005db
2025-03-03 20:05:29 +00:00
merge-script 43814eb9e5
Merge rust-bitcoin/rust-bitcoin#4134: chore: fix some typos in comments
ce19d40a80 chore: fix some typos in comments (looklose)

Pull request description:

  fix some typos in comments

ACKs for top commit:
  apoelstra:
    ACK ce19d40a805e2b67f6199cce61a73c6592019b5c; successfully ran local tests
  tcharding:
    ACK ce19d40a80

Tree-SHA512: 4bf5d339e4e73a6f1378881ad62b17f78796e6000f24eea6b3a381d3e03f4fd8b9775710a7eb5a087bfe0d557d1ab1a8291dcc7b86a2c9ded3318774cf2e5b68
2025-03-03 17:33:28 +00:00
Tobin C. Harding 85612908af
Use uniform return statement in docs
We have a bunch of 'Returns [`None`] if .. ' statements. Make the whole
module uniform.
2025-03-03 14:06:01 +11:00
Tobin C. Harding 6f4eb60936
Improve docs crate headings
I took a look at the rendered HMTL of `bitcoin`, `primitives`, `units`,
`serde`, and `tokio` and picked a header style that I thought looked
good.

Use it for `primitives` and `units`.
2025-03-03 13:28:01 +11:00
merge-script 22cecce99d
Merge rust-bitcoin/rust-bitcoin#4149: Fix typos in code comments and documentation
bb8f833ca0 Update instruction.rs (kilavvy)
0ce622e668 Update message.rs (kilavvy)
f61941bbe6 Update serialized_signature.rs (kilavvy)
1d2de62e01 Update mod.rs (kilavvy)

Pull request description:

  This PR fixes several typos in comments across multiple files:

  - Fixed typo `interpretted` -> `interpreted` in `blockdata/script/instruction.rs`
  - Fixed typo `neccessity` -> `necessity` in `p2p/message.rs`
  - Fixed typo `underlflow` -> `underflow` in `taproot/serialized_signature.rs`
  - Fixed typo `ambigous` -> `ambiguous"` in `units/src/amount/mod.rs`

  These changes only affect comments and documentation, no functional code changes.

ACKs for top commit:
  apoelstra:
    ACK bb8f833ca01688eaae75e0fa322f698d34243185; successfully ran local tests; though all these commits could be squashed IMO

Tree-SHA512: d73dc2a86b20de87c0c5efb3e5042e3901c846236670e3a6501f4c93fd54328fef08bfeca276b93642e7b51d04cb8b9c8e1af558f3aabc3c924d60a61e58b031
2025-03-02 15:42:57 +00:00
merge-script c2741c8eee
Merge rust-bitcoin/rust-bitcoin#4138: units: Improve code comment on macros
8787304425 units: Improve code comment on macros (Tobin C. Harding)

Pull request description:

  We have two macros that are hidden because they are code de-duplication tools. However the output they produce is, and has to be, stable so that we can use them in `units` and `primitives` without inadvertently breaking semver in `primitives`.

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

Tree-SHA512: 564bb27dcb608f391be8e50cbf2da02d2f339ddf575e5b5fd432a93df96233b50541616167c7c84a134ed31455a64149c822aea72e61c042fb1aeb8fab20d4ce
2025-03-01 19:46:31 +00:00
kilavvy 1d2de62e01
Update mod.rs 2025-02-28 12:34:21 +01:00
looklose ce19d40a80 chore: fix some typos in comments
Signed-off-by: looklose <shishuaiqun@yeah.net>
2025-02-28 17:55:31 +08:00
Tobin C. Harding 8787304425
units: Improve code comment on macros
We have two macros that are hidden because they are code de-duplication
tools. However the output they produce is, and has to be, stable so that
we can use them in `units` and `primitives` without inadvertently
breaking semver in `primitives`.
2025-02-28 08:47:11 +11:00
Tobin C. Harding 814685e551
Macroise the NumOpResult tests
Macroise the tests improving coverage along the way.

Includes a TODO to remind us to do `Neg` more fully.
2025-02-25 20:46:01 +00:00
Andrew Poelstra a44a9d31f6
Add a few impls to the result macro
Add a few missing impls to the `impl_op_for_references` macro.

Includes a minor whitespace change so that traits are grouped together.
2025-02-25 20:45:56 +00:00
Andrew Poelstra 353c23fa01
units: pull generic op impls on NumOpResult into macro 2025-02-25 20:44:30 +00:00
Andrew Poelstra 21ac5aefe0
units: extend op reference macro to handle generics and where clauses
This is a bit ugly and requires that we put our where-clauses in
parentheses because the macro_rules parser sucks, but it allows us to
move the blanket-impls on NumOpResult into the macro.

This commit moves one instance and updates the macro; the next commits
will change the rest.
2025-02-25 20:31:45 +00:00
Andrew Poelstra ad9564895b
units: replace a gazillion more macro calls with new macro
Looks like a large diff but if you run

    git show --color-moved-ws=allow-indentation-change

you will see that it's 100% moves (though moves of code into the
reference macro). May be easier to just look at src/amount/result.rs
after this; it's pretty short now.
2025-02-25 20:31:45 +00:00
Andrew Poelstra 0dc7f6cebd
units: rearrange a bit of code to prep the next commit
The next commit changes a lot of code, but almost entirely by moving and
indenting it. We try to do the moves here ahead of time, so it the diff
for the next commit will be just deletions and indentations.
2025-02-25 20:31:45 +00:00
Andrew Poelstra a358e79a85
units: allow multiple invocations in impl_op_for_references macro
This is not too complicated a change to support and it will reduce the
noise in the following commits a fair bit.
2025-02-25 20:31:45 +00:00
Andrew Poelstra 2da332e04a
units: introduce impl_op_for_references and use it in three places
This macro can generally handle a lot of different cases where we
implement "the same trait but on references". We introduce it here and
use it in two places. We will use it in many more, but I wanted to make
the diff small on this commit, which introduces the actual macro code
and might take a bit of reading to understand.

You may want to use --color-moved-ws=allow-indentation-change to review
this, and the next commit.

The next set of changes will mechanically delete other macros that are
made redundant by this.
2025-02-25 20:31:45 +00:00
Tobin C. Harding c90559de8e
Derive Copy for NumOpResult
The `NumOpResult` type is way more ergonomic to use if it derives
`Copy`. This restricts the `NumOpResult` to being `Copy` as well.

This does restrict what we can include in the error type in the future.

Derive Copy for `NumOpResult` and `NumOpResult`.
2025-02-25 20:31:45 +00:00
Jamil Lambert, PhD 914730d7f1
Add inline to small functions in result module 2025-02-17 12:13:28 +00:00
merge-script 987a74cd96
Merge rust-bitcoin/rust-bitcoin#4007: Introduce monadic `NumOpResult` type
6244cb75fa Introduce monadic AmountOpResult (Tobin C. Harding)

Pull request description:

  We would like to return an error when doing math ops on amount types. We cannot however use the stdlib `Result` or `Option` because we want to implement ops on the result type.

  Add an `NumOpResult` type. Return this type from all math operations on `Amount` and `SignedAmount`.

  Implement `core::iter::Sum` for the new type to allow summing iterators of amounts.

  Note please this removes `AddAssign` impls for amount types.

ACKs for top commit:
  apoelstra:
    ACK 6244cb75faf62aed4b47d63a59d14cb766e4e7a8; successfully ran local tests; let's do it -- but definitely want the followup issues addressed
  Kixunil:
    ACK 6244cb75fa

Tree-SHA512: 7a105acb1aa492ab3e97d94ae182ac4c30a364edd183f71cc320cf80d85060049e8caf1e5736ef6d1af32f39c3376f21382afe35ac65ea1b8c15130c622d9d64
2025-02-16 17:04:16 +00:00
merge-script e1cb0f1199
Merge rust-bitcoin/rust-bitcoin#4044: units: Remove alloc from fee module
43a7c66f50 units: Remove alloc from fee module (jrakibi)

Pull request description:

  This PR removes the `alloc` feature gating from fee module

  Closes #3815

ACKs for top commit:
  tcharding:
    ACK 43a7c66f50
  apoelstra:
    ACK 43a7c66f50b663aee503c958c5158127fa0b8d5c; successfully ran local tests; nice!

Tree-SHA512: 645d50cd6cde915972a576d7282a5dfc9aa27a8c3a3b44d3f3eb7a7f066cb3a697bed7e757bc86766498d92cc534607960caf20c90a1ac6fabf9246db4b30249
2025-02-15 00:32:56 +00:00
Jamil Lambert, PhD 854a4cf511
Kill mutant in checked_mul_by_fee_rate
Use `checked_mul_by_fee_rate` in existing test to kill the mutant.
2025-02-13 18:05:03 +00:00
jrakibi 43a7c66f50 units: Remove alloc from fee module
The fee module methods behind alloc perform only arithmetic operations, so they don't require heap allocation
2025-02-11 19:00:46 +03:00
Tobin C. Harding 6244cb75fa
Introduce monadic AmountOpResult
We would like to return an error when doing math ops on amount types.
We cannot however use the stdlib `Result` or `Option` because we want to
implement ops on the result type.

Add an `AmountOpResult` type. Return this type from all math operations
on `Amount` and `SignedAmount`.

Implement `core::iter::Sum` for the new type to allow summing iterators
of amounts - somewhat ugly to use, see tests for example usage.
2025-02-10 10:06:59 +11:00
Tobin C. Harding 94f9bac6aa
Return Self::Output from ops::Rem
The ops traits return `Self::Output` not `Self`. The current code builds
because `Self` and `Self::Output` are both the same type.

Use `Self::Output` as the return value of `ops::Rem`.
2025-02-07 11:16:50 +11:00
yancy a7526b6a70 Remove `fee_vb`
This is redundant given Weight::from_vb is provided.  After converting
to a weight_unit, use to_fee().
2025-02-05 09:05:51 -06:00
yancy 73b14d03b9 Add `to_fee` in place of `fee_wu`
Use the more idiomatic to_fee instead of `fee_wu`.  Since the method
takes a strongly typed argument, remove `wu` from the method name
to improve clarity.
2025-02-04 18:34:52 -06:00
jrakibi 134c146748 Add Weight::checked_mul_by_fee_rate method
Mirror FeeRate::checked_mul_by_weight functionality by adding a symmetrical method to Weight.
This allows users to calculate fees starting from either Weight or FeeRate with consistent behavior.
2025-01-31 02:13:43 +03:00
merge-script 571402657d
Merge rust-bitcoin/rust-bitcoin#3951: Improve examples on `Denomination`
5f75bfaa63 Improve examples on Denomination (Tobin C. Harding)

Pull request description:

  Reduce the noise in the examples.

ACKs for top commit:
  apoelstra:
    ACK 5f75bfaa63309c7526136d430ca8092197ab7c8e; successfully ran local tests; yeah, agreed, this is nicer to read

Tree-SHA512: 01c5863f8712a8ca3b38d3f96be9d08078ca28d8cfc3dd8e8528c388e5f82406a0d43def552b7b53f034c9bf440f7d2d0fec6a760cf69a245b109d0ce4e288c3
2025-01-26 16:50:08 +00:00
merge-script 2f09da8e47
Merge rust-bitcoin/rust-bitcoin#3950: Use MAX_MONEY in serde regression test
4dad4730a8 Add unreleased changelog entry (Tobin C. Harding)
08bb57e499 Use MAX_MONEY in serde regression test (Tobin C. Harding)

Pull request description:

  We plan on enforcing MAX_MONEY in the amount types. In preparation use MAX_MONEY in the serde regression test instead of the arbitrary, and too large DEADBEEFCAFEBABE value.

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

Tree-SHA512: 10fefdb75289f4c93537e031410eb729420729563b262d6104948a0176a78c4f6130956b550cc08ffc924b1bce1c42f0544542879a4d44165022196ef695718a
2025-01-26 14:54:13 +00:00
merge-script 72e2b00721
Merge rust-bitcoin/rust-bitcoin#3953: Refactor amounts
13a3f490b8 Use Self instead of amount type (Tobin C. Harding)
34e3049ae0 Use sats instead of satoshi (Tobin C. Harding)
00b71a670f Use from_sat_unchecked for hardcoded ints (Tobin C. Harding)
8fdec67f7d Change local var ua to sat (Tobin C. Harding)
c6f056672b Change local var sa to ssat (Tobin C. Harding)
f3e853e07a units: Do trivial refactor of amount::tests (Tobin C. Harding)
dbec9807f9 Shorten identifiers by removing _in_sats (Tobin C. Harding)
154a4420fc Stop using FQP on Amount type (Tobin C. Harding)
8e16a48252 Run the formatter (Tobin C. Harding)

Pull request description:

  Do a bunch of refactorings to tease out changes from #3794.

  The first 8 are uncontroversial. The 9th one is subjective. The last one is unusual but IMO worth doing because of the relationship between the two amount modules.

  Do note that this PR is 100% internal changes - please please don't bike shed this to death.

ACKs for top commit:
  apoelstra:
    ACK 13a3f490b80e4c8f8e1753111a914315eefd73e6; successfully ran local tests; lgtm

Tree-SHA512: e2ef0e7fbdaaf632a9840920a227a901fbeb55a29398013cd6cb764b1ff7c0a7c5a1648fd8f606e8b5f7523943886f5eff54cf4054d24349feb72f0b4de05b91
2025-01-26 01:57:30 +00:00
Tobin C. Harding 4dad4730a8
Add unreleased changelog entry
We want to be sure to mention the new MAX_MONEY invariant and the fact
that we silently broke serde while doing it.
2025-01-25 06:52:26 +11:00
Tobin C. Harding 13a3f490b8
Use Self instead of amount type
I claim that if the two amount modules are coded as similarly as
possible it will be easier to ensure that we have the API's uniform and
bug free. To make auditing the modules easier and less error prone use
`Self` instead of the explicit type. This makes it easier to see
differences in the modules and to ensure the differences are correct and
required.

Internal change, no logic changes whatsoever.
2025-01-25 06:46:57 +11:00
Tobin C. Harding 34e3049ae0
Use sats instead of satoshi
No one says that, just use `sats`.

Internal change only.
2025-01-24 12:16:29 +11:00
Tobin C. Harding 5f75bfaa63
Improve examples on Denomination
Reduce the noise in the examples.
2025-01-24 12:09:24 +11:00
Tobin C. Harding 00b71a670f
Use from_sat_unchecked for hardcoded ints
We have an `_unchecked` amount constructor that makes no assumptions
about the argument. We would like to start enforcing MAX_MONEY but the
diff to introduce this is massive. In an effort to make it smaller we
can do all the hardcoded ints first. We did this already but a bunch
more snuck in or were missed.

In any amount constructor that passes in a hardcoded const as a decimal
integer (i.e., not hex) use the `_unchecked` version.

Done in preparation for enforcing MAX_MONEY.
2025-01-24 09:05:00 +11:00
Tobin C. Harding 8fdec67f7d
Change local var ua to sat
As we did for the signed amount; match other identifiers in the `amount`
test module.
2025-01-24 08:58:54 +11:00
Tobin C. Harding c6f056672b
Change local var sa to ssat
Match other identifiers in the `amount` test module. Only done for the
signed amount to make review brain dead easy.
2025-01-24 08:58:08 +11:00
Tobin C. Harding f3e853e07a
units: Do trivial refactor of amount::tests
We are trying to make changes to `units::amount` but the diff is too
big - do the remove-whitespace change on its own.

Whitespace only.
2025-01-24 08:54:24 +11:00
Tobin C. Harding 8e16a48252
Run the formatter
Manually run `just fmt` - no other changes.
2025-01-24 08:54:24 +11:00
Jamil Lambert, PhD 420e8c043e
Fix mutants in units
Cargo mutant found missed mutants in the bitcoin consts in both signed
and unsigned amounts.

Add a test to check their values.
2025-01-23 12:14:16 +00:00
merge-script 67f3d498af
Merge rust-bitcoin/rust-bitcoin#3943: Add `µBTC` as a recognized `str` form of a MicroBitcoin Denomination
4dcdf73cfa Add `µBTC` and `µbtc` to tests (Jamil Lambert, PhD)
afba28e188 Change `uBTC` to `µBTC` in rustdocs (Jamil Lambert, PhD)
2ca24f00f2 Add `µBTC` as a recognized `str` form of `uBTC` (Jamil Lambert, PhD)

Pull request description:

  `µ` is the correct letter for the SI unit micro but is not on most standard keyboards.  `u` was used instead because it looks similar.

  Add `µBTC` to the list of recognized strings for MicroBitcoin.  This is an addition only, `uBTC` still works as normal.

  Change `uBTC` to `µBTC` in the rustdocs.  The examples have been left as `uBTC` since this is easier for most people to use.

  Add `µBTC` and `µbtc` to the tests.

  Close #3941

ACKs for top commit:
  apoelstra:
    ACK 4dcdf73cfa896b2c095cda9064c6e0a0e9aeec2b; successfully ran local tests
  storopoli:
    ACK 4dcdf73cfa
  tcharding:
    ACK 4dcdf73cfa

Tree-SHA512: 0f6e8b8b9c04f1a4dc6536c0420b2ded568ab96d2301b7d488807cb26003b91a787a6cf9023705c731682580f73ae5247f3f3b1e8646e4eb720c5a65da582933
2025-01-23 03:44:59 +00:00
merge-script f064a6e5a1
Merge rust-bitcoin/rust-bitcoin#3804: Introduce cargo-mutants workflow
bfba2a85dd Kill remaining mutants (Shing Him Ng)
871fa08f61 Fix typo in serde docs (Shing Him Ng)
462c7a1130 Add weekly cargo-mutants workflow (Shing Him Ng)

Pull request description:

  This PR introduces `cargo-mutants` via a Github weekly workflow, similar to how the formatter job runs. It can also be updated to run against [incremental changes in a PR](https://mutants.rs/pr-diff.html) or to create an issues that list the new mutants. To address #3796, I've configured it to only run in `units` for now since that's nearing 1.0.

  Here's a [sample run](https://github.com/shinghim/rust-bitcoin/actions/runs/12457984710) i did in my fork, if anyone would like to see what's in the `mutants-out` artifact that gets generated.

ACKs for top commit:
  tcharding:
    ACK bfba2a85dd
  apoelstra:
    ACK bfba2a85ddaad6b366a7502cbda1ff2462dfd4c7; successfully ran local tests

Tree-SHA512: e4a44b6f5121e4238c1c3576616f551f4f43349cf5fd5ac1d6331f958a4458753a55519bdafc16965cb2e67201ef6c91b188c79ffcc222f780c421df9a701063
2025-01-22 21:21:54 +00:00
merge-script 11f5012758
Merge rust-bitcoin/rust-bitcoin#3893: feat: add Amount division by FeeRate
7482fcd934 Run just check-api (jrakibi)
bcc38c40e0 Add Amount division by FeeRate (jrakibi)

Pull request description:

  Add checked_div_by_fee_rate method to Amount that computes the maximum weight for a transaction with a given fee rate. This complements the existing `fee = fee_rate * weight `and `fee_rate = fee / weight` operations

  - Add `checked_div_by_fee_rate` method that returns Option<Weight>
  - Implement` Div<FeeRate>` for Amount for operator syntax support
  - Use `floor` division to ensure weight doesn't exceed intended fee

  This allows calculating the maximum transaction weight possible for a given fee amount and fee rate.

  Closes #3814

ACKs for top commit:
  apoelstra:
    ACK 7482fcd934c09e3cd6cd25fd4328960b02f8e976; successfully ran local tests
  tcharding:
    ACK 7482fcd934

Tree-SHA512: 622ca42bde1f67782a3c2996efcba0132fedb5e984f594603aece974de6acdeb4b22d63239d29d46fb8623c8082841c33b1d5b9ad2ea51e2f63e6f5d859daa7e
2025-01-22 14:26:39 +00:00
Jamil Lambert, PhD 4dcdf73cfa
Add `µBTC` and `µbtc` to tests 2025-01-22 13:26:36 +00:00
Jamil Lambert, PhD afba28e188
Change `uBTC` to `µBTC` in rustdocs
Clippy thinks `µBTC` in the docs needs backticks. Allow it without
backticks.
2025-01-22 13:26:03 +00:00
Jamil Lambert, PhD 2ca24f00f2
Add `µBTC` as a recognized `str` form of `uBTC`
`µ` is the correct letter for the SI unit micro but is not on most
standard keyboards.  `u` was used instead because it looks similar.

Add `µBTC` to the list of recognized strings for MicroBitcoin and to the
rustdocs.
2025-01-22 13:12:40 +00:00