Commit Graph

354 Commits

Author SHA1 Message Date
Martin Habovstiak 5dd0c9253d Remove a bunch of `try_into().expect()`
Previously we've used `try_into().expect()` because const generics were
unavailable. Then they became available but we didn't realize we could
already convert a bunch of code to not use panicking conversions. But we
can (and could for a while).

This adds an extension trait for arrays to provide basic non-panicking
operations returning arrays, so they can be composed with other
functions accepting arrays without any conversions. It also refactors a
bunch of code to use the non-panicking constructs but it's certainly not
all of it. That could be done later. This just aims at removing the
ugliest offenders and demonstrate the usefulness of this approach.

Aside from this, to avoid a bunch of duplicated work, this refactors
BIP32 key parsing to use a common method where xpub and xpriv are
encoded the same. Not doing this already led to a mistake where xpriv
implemented some additional checks that were missing in xpub. Thus this
change also indirectly fixes that bug.
2025-03-20 20:19:50 +01:00
merge-script 8aa5556e4f
Merge rust-bitcoin/rust-bitcoin#4253: primitives: Increase test coverage in LockTime
7b114e3893 Increase test coverage in relative.rs (Jamil Lambert, PhD)
c97bebdcba Increase test coverage in absolute.rs (Jamil Lambert, PhD)

Pull request description:

  Increase the test coverage in `primitives/src/locktime/*`.

  Modify existing tests and add new ones where required to increase the test coverage to 100% for all features excluding `arbitrary` and `serde`.

ACKs for top commit:
  tcharding:
    ACK 7b114e3893
  apoelstra:
    ACK 7b114e38935f9c688c1259793e7ffda822f25c57; successfully ran local tests

Tree-SHA512: 9090565c358a23855f5bfa5c31202bfd78ed9dc36303210b64d502cacf692b010793ba0e5b2cc40c8da336429f6ffc07b6d4499cd8d03c0c5c7bb7bb868c4236
2025-03-19 21:02:12 +00:00
Jamil Lambert, PhD 7b114e3893
Increase test coverage in relative.rs
Increase test coverage in `locktime/relative.rs`
2025-03-18 14:30:50 +00:00
Jamil Lambert, PhD c97bebdcba
Increase test coverage in absolute.rs
Modify existing tests and add new ones to increase test coverage in
`locktime/absolute.rs`.
2025-03-18 14:28:52 +00:00
Fmt Bot a74e08a53d 2025-03-16 automated rustfmt nightly 2025-03-16 01:25:25 +00:00
merge-script 0ca9fcfd0e
Merge rust-bitcoin/rust-bitcoin#4157: Enforce MAX_MONEY invariant in amount types
ab4ea7c13d Enforce the MAX_MONEY invariant in amount types (Tobin C. Harding)

Pull request description:

  Enforcing the `MAX_MONEY` invariant is quite involved because it means multiple things:

  - Constructing amounts is now fallible
  - Converting from unsigned to signed is now infallible
  - Taking the absolute value is now infallible
  - Integer overflow is eliminated in various places

  Details:
  - Update `from_sat` to check the invariant
  - Fix all docs including examples
  - Use the unchecked constructor in test code
  - Comment any other use of the unchecked constructor
  - Deprecate `unchecked_abs`
  - Fail serde (using the horrible string error variant)
  - Try not to use the unchecked constructor in rustdocs, no need to encourage unsuspecting users to use it.
  - Use `?` in rustdoc examples (required by Rust API guidlines)
  - Remove `TryFrom<Amount> for SignedAmount` because the conversion is now infallible. Add a `From` impl.
  - Fix the arbitrary impls
  - Maintain correct formatting
  - Remove private `check_max` function as its no longer needed

  Close #620

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

Tree-SHA512: bec963d8ea69e202f399cd19bca864b06f3e86323d376c2d2126d74093598f8bbbf19792b2327dba0862ef6f0201202778014a2be7a14991f02917d8ca312afb
2025-03-13 23:35:40 +00:00
merge-script d1365eb376
Merge rust-bitcoin/rust-bitcoin#4219: Rename then new `Timestamp` type
c707b959b7 Rename timestamp module to time (Tobin C. Harding)
e2dee4900f Re-name Timestamp to BlockTime (Tobin C. Harding)

Pull request description:

  Done in two patches so we can bikeshed the name of the type and separately the name of the module.

  - Rename type: `Timestamp` to `BlockTime`
  - Rename module: `timestamp` to `time`

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

Tree-SHA512: de3855b38445a58b6767a6081919eecb81c6c12aee3f6699f3bfa10efaf5770b54fb412da23991a9ee734e14dfb642af670f0218d1886cdc8c8d3f393ef65d7e
2025-03-13 15:13:31 +00:00
Tobin C. Harding ab4ea7c13d
Enforce the MAX_MONEY invariant in amount types
Enforcing the MAX_MONEY invariant is quite involved because it means
multiple things:

- Constructing amounts is now fallible
- Converting from unsigned to signed is now infallible
- Taking the absolute value is now infallible
- Integer overflow is illuminated in various places

Details:

- Update from_sat to check the invariant
- Fix all docs including examples
- Use the unchecked constructor in test code
- Comment any other use of the unchecked constructor
- Deprecate unchecked_abs
- Fail serde (using the horrible string error variant)
- Try not to use the unchecked constructor in rustdocs, no need to encourage unsuspecting users to use it.
- Use ? in rustdoc examples (required by Rust API guidlines)
- Remove TryFrom<Amount> for SignedAmount because the conversion is now infallible. Add a From impl.
- Fix the arbitrary impls
- Maintain correct formatting
- Remove private check_max function as its no longer needed
2025-03-13 09:07:14 +11:00
Jamil Lambert, PhD c1ea20b958
Increase test coverage in block.rs
Add tests to `primitive::block` module to increase test coverage.
2025-03-12 09:29:01 +00:00
Jamil Lambert, PhD 3e750d7ba1
Create test helper function to create a header
Move the header creation to the helper function so it can be used in
further tests.
2025-03-12 09:28:00 +00:00
merge-script c23402c330
Merge rust-bitcoin/rust-bitcoin#4228: docs: Update README to replace use of mutagen with cargo-mutants
199f57849a Remove references to cfg(mutate) from lint allow - no longer allowed (AM)
a65d1d8b95 docs: Update README to replace use of mutagen with cargo-mutants (AM)

Pull request description:

  Hey there!

  I am just getting up to speed with the project and in following the README discovered that there are still references to the previous mutation testing tool `mutagen`. I updated the README to refer to the new tool, `cargo-mutation`.

  I'm suggesting the user use the same command, `cargo mutants --in-place --no-shuffle`, as is run in the weekly CI workflow.

  I noticed that there are still references to the old `mutate` attribute in the following files. I removed these as well as per [feedback](https://github.com/rust-bitcoin/rust-bitcoin/pull/4228#issuecomment-2709407253).

  `primitives/Cargo.toml`:
  ```
  [lints.rust]
  unexpected_cfgs = { level = "deny", check-cfg = ['cfg(mutate)'] }
  ```

  and

  `bitcoin/Cargo.toml`:
  ```
  unexpected_cfgs = { level = "deny", check-cfg = ['cfg(bench)', 'cfg(fuzzing)', 'cfg(kani)', 'cfg(mutate)'] }
  ```

  Update to PR: removed incorrect understanding of logic in PR description as per [feedback](https://github.com/rust-bitcoin/rust-bitcoin/pull/4228#issuecomment-2709408598) and removed `cfg(mutate)` from above 2 files.

ACKs for top commit:
  tcharding:
    ACK 199f57849a
  apoelstra:
    ACK 199f57849acd9845902a8090ad6490a61ee03d24; successfully ran local tests

Tree-SHA512: e154c504aa5283f1da05d0120ea8dda97d1159389e692b0d57d7d864032ecb2b48c496054ede5500477367bc732dc34b0870f2709b8bd6e7b3f5c18a10f7a29e
2025-03-11 14:00:59 +00:00
merge-script 3bb1a7acea
Merge rust-bitcoin/rust-bitcoin#4223: primitives: Feature gate import
0428554585 primitives: Feature gate import (Tobin C. Harding)

Pull request description:

  Feature gate the `Infallible` import. Found with `clippy`.

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

Tree-SHA512: 23bf7d62d81af87b67395ddee251cef82140895be76026c43c774997c29f2ed2707bf42e82d0a155a5cff83412538cb2ed5251f10c0e0cb746434a8d7db8e554
2025-03-10 15:26:57 +00:00
AM 199f57849a Remove references to cfg(mutate) from lint allow - no longer allowed
Also fix incorrect spelling of honggfuzz
2025-03-10 12:45:57 +08:00
merge-script aefcf2bb02
Merge rust-bitcoin/rust-bitcoin#4178: primitives: Enable pedantic lints
df500e9b71 primitives: Enable pedantic lints (Tobin C. Harding)

Pull request description:

  Draft to check the subjective ones please, then I'll squash.

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

Tree-SHA512: 8cc8c9b369a63c1b2b26461e288a818e3b74e0f9b7359c964c1650028d3161db1d79369c74f18e79958873bf4d223ee72fa481708600f0297d79377d97a84dda
2025-03-09 13:36:07 +00:00
Tobin C. Harding 0428554585
primitives: Feature gate import
Feature gate the `Infallible` import. Found with `clippy`.
2025-03-09 05:57:03 +11:00
Tobin C. Harding c707b959b7
Rename timestamp module to time
We just re-named `Timestamp` to `BlockTime`. We have a `units::block`
module but it currently holds abstractions (`BlockHeight` and
`BlockInterval`) that are not onchain abstractions and therefore
somewhat different from the `BlockTime`. Instead of making `block` a
block 'utils' module instead re-name the `timestamp` module to `time`.
2025-03-08 08:30:10 +11:00
Tobin C. Harding e2dee4900f
Re-name Timestamp to BlockTime
We just added a `Timestamp` type without knowing that there was a push
by OpenTimestamps to also create a timestamp and that our new type may
lead to confusion. Our timestamp is explicitly for the `time` field in a
block so we can call it `BlockTime`. This name change makes the module
name stale but we will change that in a following patch to ease review.
2025-03-08 08:28:46 +11:00
merge-script 9d7190e581
Merge rust-bitcoin/rust-bitcoin#4190: Kill mutants in primitives and units
90d909becc Kill mutants in primitives and units (Shing Him Ng)

Pull request description:

  This kills 15 mutants found with the mutants workflow. Ran `cargo mutants` locally to confirm

  Closes #4156
  Closes #4106

ACKs for top commit:
  jamillambert:
    ACK 90d909becc
  tcharding:
    ACK 90d909becc
  apoelstra:
    ACK 90d909becc4638c03003845154e9cc1eb5f3ad41; successfully ran local tests
  Kixunil:
    ACK 90d909becc

Tree-SHA512: e5c95a1c4054cf1c60c940ea605eec84dffcbff292f9c7c4d96813c6389e807c318f6c5f8f69ceeb9ffcab3c3e45aa0d5a8fda7335d540c6f070aab92bae7a0f
2025-03-07 02:53:55 +00:00
merge-script 631ea1a2a9
Merge rust-bitcoin/rust-bitcoin#4126: Add additional re-exports
5da506b506 Add additional re-exports (Tobin C. Harding)

Pull request description:

  As we do for other types add two new alias' at the crate root of `primitives` and mirror it in `bitcoin`:

  - `BlockVersion`
  - `TransactionVersion`

ACKs for top commit:
  apoelstra:
    ACK 5da506b506aa41b88aa7e8ecdffdcc0478ec72b6; successfully ran local tests
  Kixunil:
    ACK 5da506b506

Tree-SHA512: 5f91e3aae87b1128256b528d20d9aab562bf054131ed8028e0db789e2b487863ad4c89aaca080d0fbcf760dd160815a1843046c5fab0ed1483230fdedc66402e
2025-03-06 23:13:42 +00:00
merge-script fa8eaf0257
Merge rust-bitcoin/rust-bitcoin#4092: Add `Timestamp` newtype
b3f122b399 Add Timestamp newtype (Tobin C. Harding)

Pull request description:

  Bitcoin block headers have a timestamp. Currently we are using a `u32`. While this functions correctly it gives the compiler no chance to enforce type safety.

  Add a `Timestamp` newtype that is a thin wrapper around a `u32`. Document it and test the API surface in `api.rs`.

ACKs for top commit:
  apoelstra:
    ACK b3f122b3996c1a73479be2f95b7f2ae642c9c56f; successfully ran local tests
  Kixunil:
    ACK b3f122b399

Tree-SHA512: 6f4a4a588bc836243ae28f3d36be6c0ae264cb2b7a0061277910b107d05e5ca0e679497d2890208f5d8ec148f37bf263bcd0b0410f9e5e6370d8e763ff30b78a
2025-03-06 19:34:11 +00:00
merge-script febce17eff
Merge rust-bitcoin/rust-bitcoin#4186: Clean up Witness API
Pull request description:

  Enhance Witness struct element access methods:

  - Rename `nth()` to `get()` for clearer slice-like element retrieval
  - Introduce `get_back()` method for flexible reverse indexing
  - Remove redundant `second_to_last()` and `third_to_last()` methods
  - Add `#[track_caller]` to index implementation for better error tracking
  - Update all references to use new method names
  - Improve documentation with usage examples

  The changes provide a more intuitive and consistent approach to accessing witness elements.

  Close #4098

ACKs for top commit:
  Kixunil:
    ACK 3ca3218c23
  tcharding:
    ACK 3ca3218c23
  apoelstra:
    ACK 3ca3218c236c63a9b006047524e2b47e310f07d9; successfully ran local tests

Tree-SHA512: 163e7457f3fe5141373e27a6df5fe1da6f2f05f02e877ef96243510d030d832c0fa86ade781e015a3c392f004651170b60438a83d330f1059457e5ade6478af7
2025-03-06 18:43:27 +00:00
merge-script a27a6b9592
Merge rust-bitcoin/rust-bitcoin#4201: Make `hex` in `internals` optional
d1d483491f Make `hex` in `internals` optional (Martin Habovstiak)

Pull request description:

  The `hex` crate is not always desirable - e.g. when the consumer wants to work with raw data only. We already had this optional in `hashes` but if `hashes` is going to depend on `internals` it would break this property.

  This change makes `hash` optional, since it's easy: there's just one struct that depends on it.

ACKs for top commit:
  tcharding:
    ACK d1d483491f
  apoelstra:
    ACK d1d483491f5f0181e60778e557fadd99b44b5d30; successfully ran local tests; nice

Tree-SHA512: c20091e6febb49b9114273c280580ddcdafc91893f3c73288b374f51990f09f035a044806dd26777148f2e4341ad082ab05f1b49f8ceb3bcd24eb210ffa5fd5f
2025-03-06 16:50:12 +00:00
Tobin C. Harding df500e9b71
primitives: Enable pedantic lints
Enable all the pedantic lints and fix warnings.

Notable items:

- `enum_glob_used` import types with a single character alias
- `doc_markdown`: add a whitelist that includes SegWit and OpenSSL
2025-03-06 10:58:17 +11:00
Shing Him Ng 90d909becc Kill mutants in primitives and units
This kills 15 mutants found with the mutants workflow
2025-03-04 21:35:10 -06:00
merge-script ed772d4fc9
Merge rust-bitcoin/rust-bitcoin#4168: Move `opcodes` back to bitcoin
a74393324b Move opcodes back to bitcoin (Tobin C. Harding)

Pull request description:

  Duplicate `opcodes` in `bitcoin` and hide it in `primitives` so we do not have to commit to the API.

  We use opcodes in `impl fmt::Display for Script`.

  Close: #4144

ACKs for top commit:
  apoelstra:
    ACK a74393324bd47f89fd47281d567ab15ab6bcb2ba; successfully ran local tests; sure
  Kixunil:
    ACK a74393324b

Tree-SHA512: 738685b9cd2288a581daa6219e3b21bd48bb4845ea627bf6b8085e0e48f5649ac5ec616a3421d10cd37543f76b66d31f94fd55bf94effc2fb8f91d1ecf5c8611
2025-03-05 00:57:46 +00:00
Erick Cestari 3ca3218c23 witness: Improve element access methods with get() and get_back()
Enhance Witness struct element access methods:

- Rename `nth()` to `get()` for clearer slice-like element retrieval
- Introduce `get_back()` method for flexible reverse indexing
- Remove redundant `second_to_last()` and `third_to_last()` methods
- Add `#[track_caller]` to index implementation for better error tracking
- Update all references to use new method names
- Improve documentation with usage examples

The changes provide a more intuitive and consistent approach to
accessing witness elements.
2025-03-04 12:16:48 -03:00
merge-script 95c012ee53
Merge rust-bitcoin/rust-bitcoin#4143: primitives: Inline small functions
b656d7a16c Inline small functions (Jamil Lambert, PhD)

Pull request description:

  Functions that fit the below criteria should be inline:

  > Basically, if a function jut delegates into another function and passing through arguments, it should be inline. Also when doing assignment of `u32` or similarly trivial operation. Also if it checks the validity of argument(s) and then performs something simple (like a call to `_unchecked`).

  _Originally posted by Kixunil in https://github.com/rust-bitcoin/rust-bitcoin/pull/4099#discussion_r1966156399_

  Add `#[inline]` to all functions in `primitives` that fit the criteria.

ACKs for top commit:
  tcharding:
    ACK b656d7a16c
  apoelstra:
    ACK b656d7a16c2d0cdd6d8a94fffeac842ffc0fabf4; successfully ran local tests

Tree-SHA512: 0059aa25252e634e55482b201a9414d19a01435fea801a20dc38b4b701a52f4a14565a1cfb0ff4ed7a771a25629eb192fd69cb4ee81e78f6a0ef79d56db0ef5b
2025-03-04 14:35:14 +00:00
Martin Habovstiak d1d483491f Make `hex` in `internals` optional
The `hex` crate is not always desirable - e.g. when the consumer wants
to work with raw data only. We already had this optional in `hashes` but
if `hashes` is going to depend on `internals` it would break this
property.

This change makes `hash` optional, since it's easy: there's just one
struct that depends on it.
2025-03-04 12:33:04 +01:00
Jamil Lambert, PhD b656d7a16c
Inline small functions
Functions that just delegate or do trivial operations should be inline.

Add `#[inline]` to all functions in `primitives` that fit the criteria.
2025-03-03 12:26:52 +00: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
Tobin C. Harding a74393324b
Move opcodes back to bitcoin
Duplicate `opcodes` in `bitcoin` and hide it in `primitives` so we do
not have to commit to the API.

We use opcodes in `impl fmt::Display for Script`.

Close: #4144
2025-03-03 11:51:28 +11:00
Tobin C. Harding 5da506b506
Add additional re-exports
As we do for other types add two new alias' at the crate root of
`primitives` and mirror it in `bitcoin`:

- `BlockVersion`
- `TransactionVersion`
2025-03-03 08:25:58 +11:00
Fmt Bot 166e37161a 2025-03-02 automated rustfmt nightly 2025-03-02 01:22:29 +00:00
merge-script ef9a24eadf
Merge rust-bitcoin/rust-bitcoin#4091: primitives: Hide error internals
43ae9d7516 primitives: Hide script error internals (Tobin C. Harding)
2d8227f091 Hide relative locktime error internals (Tobin C. Harding)

Pull request description:

  Make the struct fields private and add getters.

ACKs for top commit:
  apoelstra:
    ACK 43ae9d751622c7bef548a469466d74cf01284129; successfully ran local tests; nice! Way easier to understand these types with the new incompatible / expected names

Tree-SHA512: cfe67d60ea61a2a4c27b09071a6b11739ca281bf0b4a655121f90215ce38c3a637acf53a6e01aa2ef26fa80004cd919bf3b3334dbd9566ee2f594cab7750b563
2025-03-01 04:44:01 +00:00
Tobin C. Harding cf12ba262a
Move taproot back to bitcoin crate
I don't know what I was thinking when I move the taproot hash types to
`primitives`. As correctly pointed out by Kix we agreed to only have
blockdata in `primitives`.

Move the taproot hash types back to `bitcoin::taproot` and remove the
extension traits.
2025-02-26 15:45:49 +11:00
Tobin C. Harding 43ae9d7516
primitives: Hide script error internals
As part of the 1.0 effort and forward maintainability hide the internals
of the two error types in the `script` module. Add getters to get at the
invalid size.
2025-02-26 11:05:07 +11:00
Tobin C. Harding 2d8227f091
Hide relative locktime error internals
As part of the 1.0 effort and forward maintainability hide the internals
of the two error types in the `relative` locktime module. Doing so
allows us to remove the `non_exhaustive` attribute. Add getters to get
at the error innards.
2025-02-26 11:04:58 +11:00
Tobin C. Harding b3f122b399
Add Timestamp newtype
Bitcoin block headers have a timestamp. Currently we are using a
`u32`. while this functions correctly it gives the compiler no chance
to enforce type safety.

Add a `Timestamp` newtype that is a thin wrapper around a `u32`.
Document it and test the API surface in `api.rs`.
2025-02-25 10:03:28 +11:00
merge-script 6c286e32d4
Merge rust-bitcoin/rust-bitcoin#4089: Script improvements
5680b4e870 Refer to `Script{Buf}` as `Self` where relevant (Martin Habovstiak)
ce55dd5b70 Make `ScriptHash` & `WScriptHash` obey sanity rule (Martin Habovstiak)
9ec9adc71d Add a note about Electrum's script hashes (Martin Habovstiak)
82f553aada Expose `ScriptBuf`'s `capacity` (Martin Habovstiak)
6b9d439dc1 Remove stale FIXME comments (Martin Habovstiak)
0567e6fe1d Put `#[inline]` on most `Script{Buf}` methods (Martin Habovstiak)
b7e2af1b6b Implement `Arbitrary` for `&'a Script` (Martin Habovstiak)
bca2864084 Move `Deref{Mut}` from common module to `owned` (Martin Habovstiak)
3b15e900f0 Add `const` to some `Script` methods (Martin Habovstiak)
277223da6a Make `Script` and `ScriptBuf` obey sanity rules (Martin Habovstiak)

Pull request description:

  This implements various improvements related to `Script`. Please refer to the individual commits for details.

  This is a part of #4059

ACKs for top commit:
  tcharding:
    ACK 5680b4e870
  apoelstra:
    ACK 5680b4e870ba3b7340432256c24d37d2b6ead15a; successfully ran local tests

Tree-SHA512: 5daa8bf6c0b439a579d31d23944077e4a7fa89e14052003d2b81c745f225147f8f6f693d068e0567830027cefea7dda2516596da632bc817199352fa29af0a9b
2025-02-24 21:50:48 +00:00
merge-script 81deddd0a9
Merge rust-bitcoin/rust-bitcoin#4099: Make transaction::Version field private
cb270eae8e Make transaction::Version field private (jrakibi)
6c69b66b0d Use Version constant (jrakibi)

Pull request description:

  This commit addresses #4041 by making the `transaction::Version` field private.

  This forces people to either use the associated constants (`Version::ONE/TWO/THREE`) or the `non_standard`/`from_consensus` methods for any other transaction version.

  This aligns with our approach to `block::Version`

ACKs for top commit:
  tcharding:
    ACK cb270eae8e
  Kixunil:
    ACK cb270eae8e

Tree-SHA512: dcf5b50dfeda04e56fec350acd735dcb7099989b552afce4261d559a8a846c0eb3705369ad635ef9bbbfb2373d203a2c3641178925de6685426aa91245db9a8c
2025-02-24 16:37:16 +00:00
jrakibi cb270eae8e Make transaction::Version field private
This commit addresses #4041 by making the transaction::Version field private

Changes:
- Make the `Version` field private with `pub(crate)`
- Rename `non_standard` to `maybe_non_standard` for clarity since it accepts both standard and non-standard versions
- Add `#[inline]` attributes to small, frequently used methods:
  - `as_u32`
  - `maybe_non_standard`

Users now must use either:
- Constants (`Version::ONE/TWO/THREE`) for standard versions
- `maybe_non_standard` method for any version (standard or non-standard)
2025-02-24 06:54:21 +07:00
Martin Habovstiak 5680b4e870 Refer to `Script{Buf}` as `Self` where relevant
Using `Self` instead of specific type name can make some refactorings
easier.
2025-02-20 19:30:07 +01:00
Martin Habovstiak ce55dd5b70 Make `ScriptHash` & `WScriptHash` obey sanity rule
These were re-implementing hashing after the check rather than calling
the `_unchecked` method, so this replaces the manual implementation with
the method.
2025-02-20 18:10:53 +01:00
Martin Habovstiak 9ec9adc71d Add a note about Electrum's script hashes
The Electrum protocol uses hashes of `script_pubkey` that might look
similar to the ones we have in the crate and could be confused. This
change notes that to hopefully avoid the confusion.

Resolves https://github.com/rust-bitcoin/rust-bitcoin/discussions/3997
2025-02-20 18:01:46 +01:00
Martin Habovstiak 82f553aada Expose `ScriptBuf`'s `capacity`
There are already several methods referring to capacity but none to
retrieve it. Those methods also promise certain behavior that mandates
having *a* capacity field inside the struct, so no changes in layout
will ever remove it. Thus it's OK to expose the field.

Aside from exposing the field, this also fixes up the tests to obey the
sanity rules.
2025-02-20 16:30:31 +01:00
Martin Habovstiak 6b9d439dc1 Remove stale FIXME comments
These comments said that the modules should be private but they already
are. Also, the internals of the newtypes became private a few commits
ago.
2025-02-20 16:23:26 +01:00
Martin Habovstiak 0567e6fe1d Put `#[inline]` on most `Script{Buf}` methods
These methods are either newtype casts that should compile to no-ops or
directly calling into some other function with at most some pointer
adjustments. As such making them `#[inline]` is definitely benefitial.
There are also methods that check length and then call some other
function. These are also worth inlining since the length could be known
at compile time and the check could be eliminated.
2025-02-20 16:18:21 +01:00
Martin Habovstiak b7e2af1b6b Implement `Arbitrary` for `&'a Script`
The `Arbitrary` trait allows zero-copy implementations for borrowed
types, so this adds the implementation for borrowed `Script`.
2025-02-20 16:02:22 +01:00
Martin Habovstiak bca2864084 Move `Deref{Mut}` from common module to `owned`
We have several trait implementations for `Script` and `ScriptBuf` in a
common module so that it's easy to verify that they are same but `Deref`
and `DerefMut` should *not* be implemented for `Script` so having them
in the common module is not helpful. This moves them to the appropriate
`Owned` module.
2025-02-20 16:00:10 +01:00
Martin Habovstiak 3b15e900f0 Add `const` to some `Script` methods
We're confident these methods will never perform syscalls, so it's fine
to add `const` to them.
2025-02-20 15:49:46 +01:00
Martin Habovstiak 277223da6a Make `Script` and `ScriptBuf` obey sanity rules
The newtype sanity rules (a name I came up with):
* Newtypes should have at most one constructor that directly references
  the inner field.
* Newtypes should have at most three accessor methods that directly
  reference the ineer field: one for owned access, the second for
  borrowed and the third for mutably borrowed.
* All other methods should use the methods above to perform operations
  on the newtype and not directly access the fields.

This commit makes `Script` and `ScriptBuf` obey these except for
`reserve` and `reserve_exact` since we don't have `as_mut_vec` method.
As a side effect it also adds `const` to `ScriptBuf::from_bytes`.
2025-02-20 15:35:17 +01:00
Erick Cestari 4eb3177fab Abstract out "debug-print hex fields" using WrapDebug
This commit introduces `WrapDebug` in `internals` and updates `Witness`
debug implementation to use it. The previous `DebugElements` struct has
been removed in favor of an ad-hoc closure inside `WrapDebug`, which
formats witness elements as a debug list of hex-encoded values.

By abstracting out the "debug-print hex fields" pattern, we reduce
code duplication and improve maintainability.
2025-02-20 10:56:12 -03:00
merge-script 38c8c90765
Merge rust-bitcoin/rust-bitcoin#4065: locktimes: Remove `PartialOrd` and `ArbitraryOrd`
4259dab93a Remove rust-ordered dependency (Tobin C. Harding)
d392cdbd7d Remove PartialOrd from locktimes (Tobin C. Harding)

Pull request description:

  These two things are related so we remove them both in the same PR. Please see commit logs for full explanation.

  For more context see discussion below and also:

  - #2500
  - #4002
  - #3881

  Close: #4029

ACKs for top commit:
  Kixunil:
    ACK 4259dab93a
  apoelstra:
    ACK 4259dab93ab52795305db4b889b9151595bcee51; successfully ran local tests

Tree-SHA512: 7526d4faaa9edf8017d2af412c41a33f33d851ad5130c9a745bba86d9c71dc1db7f20d07377aaf3a25fec2c0de79f3ffabc2c538a5a366e415c7a6eaa730153c
2025-02-18 19:55:40 +00:00
Tobin C. Harding 4259dab93a
Remove rust-ordered dependency
It has turned out that the `rust-ordered` crate and it's
`ArbitraryOrd` trait are only useful for locktimes and only marginally
useful for them at best.

Remove the `ArbitraryOrd` impls and the `rust-ordered` dependency.

This topic was discussed in various places including:

- #2500
- #4002
- #3881

Close: #4029
2025-02-18 13:36:07 +11:00
Tobin C. Harding d392cdbd7d
Remove PartialOrd from locktimes
Currently it is possible to write

```rust
if this_locktime < that_locktime {
   do_this();
}
```

with the hope that this code means if a locktime is satisfied by the
value in the other locktime. This is a footgun because locktimes are
incommensurate.

We provide the `is_satisfied_by` API to help users do locktime
comparisons.

Remove the `PartialOrd` implementation from both locktime types.
2025-02-18 13:30:42 +11:00
merge-script fab1a97a9c
Merge rust-bitcoin/rust-bitcoin#4061: Make Debug representation of Witness to be slice of hex-encoded bytes strings to improve readability
8d8edd2c77 make Debug representation of Witness to be slice of hex-encoded bytes strings (Erick Cestari)

Pull request description:

  This PR updates the Debug implementation for the Witness type to improve its readability by displaying the witness data as a slice of hex-encoded strings rather than a concatenated blob or list of raw u8 values. The changes include:

  - Improved Output:
  The debug output now shows pseudo-fields such as the number of elements and the total length of all elements, making it easier to understand the underlying data without exposing internal indices like indices_start.

  - Hex-Encoding:
  Each witness element is displayed as a hex-encoded string, similar to Bitcoin Core's output style, which enhances clarity during debugging sessions.

  These changes should provide a more developer-friendly view of the witness data and align with similar patterns used elsewhere in the ecosystem.

  Closes #4023.

  Example display:

  ```
  Witness {
      num_elements: 3,
      total_bytes: 5,
      elements: [
          0b,
          1516,
          1f20,
      ],
  }
  ```
  ```
  Witness { num_elements: 3, total_bytes: 5, elements: [0b, 1516, 1f20] }
  ```

ACKs for top commit:
  tcharding:
    ACK 8d8edd2c77
  Kixunil:
    ACK 8d8edd2c77
  apoelstra:
    ACK 8d8edd2c77de9b0423533fc70802171803761fcd; successfully ran local tests

Tree-SHA512: ffcdf67542049f405317eecd74876b51972d27ec552eec8e9c7b6324f18f31f4721fc4d2be1e596232c39af90a8d169c082f9b0636e5aa1a80fe1b063d645456
2025-02-17 23:21:39 +00:00
Erick Cestari 8d8edd2c77 make Debug representation of Witness to be slice of hex-encoded bytes strings 2025-02-17 00:08:55 -03:00
Fmt Bot 0fbfe68c72 2025-02-16 automated rustfmt nightly 2025-02-16 01:21:38 +00:00
merge-script 0a19d0140c
Merge rust-bitcoin/rust-bitcoin#4043: Implement Default for Script
ab2f709181 Implement Default for Script (jrakibi)

Pull request description:

  This PR implements Default for `Script` to return an empty script.

  *Note: ScriptBuf already has `#[derive(Default)]` in the code so it's already handled*
  Resolves #3735

ACKs for top commit:
  tcharding:
    ACK ab2f709181
  Kixunil:
    ACK ab2f709181
  apoelstra:
    ACK ab2f7091814333b20669d41f1f78e0e52795df08; successfully ran local tests; neat!

Tree-SHA512: c06ba98d9bf8568e323ef9082a7f06756586360d6bef2b93721db7f6e28a777852e494c86319c97b0fd5444a0010d6c679625753534c0e1c8116e452ce8fa9cc
2025-02-15 12:57:15 +00:00
merge-script 007f4d3362
Merge rust-bitcoin/rust-bitcoin#4037: primitives: Add tests to LockTime
6cecc40ae4 Test LockTime PartialOrd (Jamil Lambert, PhD)

Pull request description:

  Add tests to kill the mutants in both relative and absolute PartialOrd.

ACKs for top commit:
  tcharding:
    ACK 6cecc40ae4
  apoelstra:
    ACK 6cecc40ae4d52b711f58998315155bc8c6b19d7b; successfully ran local tests; thanks!

Tree-SHA512: dba7d90e3f6e62f0d3417bacc09d38145dd29bf654f84c2d3bc68af30c0e65b105146466a384bd35ef4326913ca414fd31f92daa3d7ffe3ff409c49bd1c05d96
2025-02-15 03:55:05 +00:00
merge-script fce0cf1692
Merge rust-bitcoin/rust-bitcoin#4036: primitives: Add tests to witness
7e66091e1e Add from impl tests (Jamil Lambert, PhD)
2f95064cfd Add from_parts test (Jamil Lambert, PhD)
3ee66c5bb8 Modify push test (Jamil Lambert, PhD)

Pull request description:

  Add tests to kill the mutants in `primitives/src/witness.rs`

ACKs for top commit:
  tcharding:
    ACK 7e66091e1e
  apoelstra:
    ACK 7e66091e1e8b6cdd3e40d001ea1824125f7175e7; successfully ran local tests

Tree-SHA512: 57b2b0e4dbd93023d1a6a9709a02fa843e3ef9b25e7293ad641726b9c335e220a4ed87b717ec5dda999217677a916b86ac7daa9aaaec077afbfee4789836344e
2025-02-14 01:41:08 +00:00
merge-script 7a369aef2b
Merge rust-bitcoin/rust-bitcoin#4033: primitives: Add tests to transaction
435750f292 Add a parse_vout test (Jamil Lambert, PhD)
957be3c978 Add OutPoint test (Jamil Lambert, PhD)
a4ef027134 Add tests of transaction functions (Jamil Lambert, PhD)

Pull request description:

  Add tests to kill the mutants in `primitives/src/transaction.rs`

ACKs for top commit:
  tcharding:
    ACK 435750f292

Tree-SHA512: 78baf40ad6ed1cd5b3a33346b4702c3a7efbb03ae6eec9a802d3dea99910373cf4e8053c73e9fdc02ce54852d3ee43253f2ff0c149c86870ecfed2fc909e5bcf
2025-02-13 19:00:07 +00:00
jrakibi ab2f709181 Implement Default for Script
Add Default implementation for Script that returns an empty script.

ScriptBuf already has #[derive(Default)] in the code so it's already handled
2025-02-13 03:22:37 +05:30
Andrew Toth 0acd2b5e89
primitives: store transaction::Version as u32 instead of i32 2025-02-12 09:55:33 -05:00
Jamil Lambert, PhD 6cecc40ae4
Test LockTime PartialOrd
Add tests to kill the mutants in both relative and absolute PartialOrd.
2025-02-11 17:39:06 +00:00
Jamil Lambert, PhD 7e66091e1e
Add from impl tests
Add tests for the witness from implementations to kill all the mutants
found by cargo mutants.
2025-02-11 17:31:24 +00:00
Jamil Lambert, PhD 2f95064cfd
Add from_parts test
Add a test to kill the mutant in `from_parts__unstable`
2025-02-11 17:08:30 +00:00
Jamil Lambert, PhD 3ee66c5bb8
Modify push test
Cargo mutants found mutants in witness.

Add to the existing test `push` to kill the mutants from `is_empty` and
`third_to_last`.

Change the dummy values to make the progression of `elements` down the
test easier to follow.
2025-02-11 16:37:31 +00:00
Jamil Lambert, PhD 435750f292
Add a parse_vout test
Add a test to kill the mutants in parse_vout().
2025-02-11 15:58:05 +00:00
Jamil Lambert, PhD 957be3c978
Add OutPoint test
Cargo mutant found mutants in OutPoint.

Add a test to kill them.
2025-02-11 15:57:55 +00:00
Jamil Lambert, PhD a4ef027134
Add tests of transaction functions
Cargo mutants found mutants in Transaction.

Add a test to kill them.
2025-02-11 09:08:46 +00:00
Tobin C. Harding fd4586eaae
Invert dependency between io and hashes
Currently in order to release `hashes v1.0` we need to 1.0 `io` as well.
For multiple reasons, many out of our control, the `io` crate may not
stabalise any time soon.

Instead we can invert the dependency between the two crates.

This is an ingenious idea, props to Kixunil for coming up with it.

Notes

- `io` does not currently re-export the `hashes` crate.
- This work highlights that we cannot call `hash_reader` on a siphash.
- The `Hmac::hash_reader` uses the default key which may not be obvious.

Signed-off-by: Tobin C. Harding <me@tobin.cc>
2025-02-11 09:17:21 +11:00
Tobin C. Harding 9833ca32ce
primitives: Do not depend on bitcoin_io
Currently the `primitives` crate does not use the `io` dependency. I
don't know if this is just a mistake of if it used to and the manifest
is just stale.
2025-02-10 10:50:59 +11:00
merge-script a1bd90eb52
Merge rust-bitcoin/rust-bitcoin#4024: primitives: Add tests to Sequence
963dba2bdf Test sequence formatting (Jamil Lambert, PhD)
6c3d7f6443 Test Sequence properties (Jamil Lambert, PhD)

Pull request description:

  Add tests to kill the mutants found in Sequence.

ACKs for top commit:
  tcharding:
    ACK 963dba2bdf
  apoelstra:
    ACK 963dba2bdf102b2015393014d06cc56b8473520a; successfully ran local tests

Tree-SHA512: f03a02cd49df2b29232bf4f9baf663a7b51b0de504de88ae4d62de96d4e672800ac2387d0675aa48280dcbdfc0738f80484842f98b58324a09462c01956566cf
2025-02-08 15:10:46 +00:00
merge-script b70dd554be
Merge rust-bitcoin/rust-bitcoin#4018: Add missing re-exports to primitives crate root
e5c74c38a2 Add missing re-exports to primitives crate root (Jamil Lambert, PhD)

Pull request description:

  There are re-exports in the bitcoin root from primitives that were not re-exported in the primitives crate root.

  Add the missing re-exports to primitives crate root.

  First mentioned here https://github.com/rust-bitcoin/rust-bitcoin/pull/4012#discussion_r1943895283

ACKs for top commit:
  tcharding:
    ACK e5c74c38a2
  apoelstra:
    ACK e5c74c38a20e153f0919b43755d58dff8f2c8da2; successfully ran local tests

Tree-SHA512: c3355981ccd3a5db32423f6f44a6e169062c8cbce1f5e358ae2791e24801459a9db22065b83e54950dd3fd745ac4af4dcb1e1bd4625b5f25a9dd4f8fd4e6b860
2025-02-08 04:30:32 +00:00
merge-script 9aaf6ae358
Merge rust-bitcoin/rust-bitcoin#4020: primitives: Add tests to CompactTarget
b2d0737acc Add tests to CompactTarget (Jamil Lambert, PhD)

Pull request description:

  Add tests to pow.rs to kill the mutants found in CompactTarget.

ACKs for top commit:
  tcharding:
    ACK b2d0737acc
  apoelstra:
    ACK b2d0737accb998071e0f19cda6f988956b6c097a; successfully ran local tests

Tree-SHA512: 8245b2342fabb7142cc0369cc53f4213f8f685dd0ed9d357214b2f692237b3484236462a96315d2c9409625fcf59484fc505674859542311893c23e50da6ffdf
2025-02-07 21:44:22 +00:00
merge-script 9aef27a52b
Merge rust-bitcoin/rust-bitcoin#4019: primitives: Add test to opcodes
486d55f042 Add test to opcodes (Jamil Lambert, PhD)

Pull request description:

  Cargo mutant found a mutant in opcodes.

  Add a test to kill it.

ACKs for top commit:
  tcharding:
    ACK 486d55f042
  apoelstra:
    ACK 486d55f0421a05c0aaa8f883bc15cacd212dcc19; successfully ran local tests

Tree-SHA512: 8bd28d56d6e5d79a3531f0a3391ac26021f8351a77e632dcceb8a57b2f1cce05aa02ec5e37d5b5771c3a42a2a7010a241d8f232521678604cc6ecb4bdb3327a5
2025-02-07 20:11:16 +00:00
Jamil Lambert, PhD 963dba2bdf
Test sequence formatting
Add a test to kill the mutants in Sequence formatting.
2025-02-07 17:50:45 +00:00
Jamil Lambert, PhD 6c3d7f6443
Test Sequence properties
Cargo mutants found multiple mutants in the functions that return true
or false for various Sequence properties.

Add a test to kill the mutants.
2025-02-07 17:48:17 +00:00
Jamil Lambert, PhD e5c74c38a2
Add missing re-exports to primitives crate root
There are re-exports in the bitcoin root from primitives that were not
re-exported in the primitives crate root.

Add the missing re-exports to primitives crate root.
2025-02-07 08:01:46 +00:00
merge-script 21d1defced
Merge rust-bitcoin/rust-bitcoin#4011: Update example imports to use `relative::*`
a4fe67645a Use relative::* or absolute::* in locktime example (ndungudedan)

Pull request description:

  Aims to resolve #3976

  Apart from the examples in the `relative.rs` file, I see others in the **examples** folder. If this patch is good, I can continue with them.

  I will happily appreciate a review.

ACKs for top commit:
  tcharding:
    ACK a4fe67645a
  jamillambert:
    ACK a4fe67645a

Tree-SHA512: a9551c77bf91fdc212126d387adb110a7dc71635620f4efa25dc1e4306a9953968a4ad0e1528c990d5083bd9833702e62ae819db474a49f1284327fd042fb1c2
2025-02-07 06:09:16 +00:00
ndungudedan a4fe67645a
Use relative::* or absolute::* in locktime example
Users should be encouraged to explicitly use relative::* or

absolute::* instead of just LockTime, Time, or Height so that

it is clear when looking at the code if it is a relative or

absolute locktime.
2025-02-06 22:28:13 +03:00
Jamil Lambert, PhD b2d0737acc
Add tests to CompactTarget
Add tests to pow.rs to kill the mutants found in CompactTarget.
2025-02-06 17:06:41 +00:00
Jamil Lambert, PhD 486d55f042
Add test to opcodes
Cargo mutant found a mutant in opcodes.

Add a test to kill it.
2025-02-06 16:30:52 +00:00
Jamil Lambert, PhD ff3b3ddb67
Add a test for Header size
Calculate the header size and test it is equal to `Header::SIZE`
2025-02-06 14:28:22 +00:00
Jamil Lambert, PhD 0df15d5cfd
Add version test
Mutant found in Version.

Add a round trip test for version::from_consensus to kill the mutant.
2025-02-06 14:17:28 +00:00
Jamil Lambert, PhD 927251ef8e
Expand version signalling test
Add a check for max soft fork signalling bit 28
2025-02-06 14:11:07 +00:00
merge-script f81d10b2b6
Merge rust-bitcoin/rust-bitcoin#3996: Automated nightly rustfmt (2025-02-02)
282bc14d6e 2025-02-02 automated rustfmt nightly (Fmt Bot)

Pull request description:

  Automated nightly `rustfmt` changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action

ACKs for top commit:
  apoelstra:
    ACK 282bc14d6e38ec78b0df01b1169b7ed42a2265cf; successfully ran local tests
  tcharding:
    ACK 282bc14d6e

Tree-SHA512: 88771b5ed72cd357e1bf0bd12d2bbe12b2bb469e667172c8ab4d7beeab46df2270471a586e9b10d7296ef62fc92f7932e4fa7da5905136a7055d42902d580ee2
2025-02-02 23:48:53 +00:00
merge-script 4e355b3843
Merge rust-bitcoin/rust-bitcoin#3987: primitives: Add `script` tests
12a1c3c4b7 Add `script` tests (Jamil Lambert, PhD)

Pull request description:

  Together with #3971 this fixes all of the mutants in `primitives/src/script` except for those related to `serde`.

ACKs for top commit:
  tcharding:
    ACK 12a1c3c4b7
  apoelstra:
    ACK 12a1c3c4b7bc097553fbfb05a0e92320ae42975c; successfully ran local tests
  Kixunil:
    ACK 12a1c3c4b7

Tree-SHA512: c0b757ae672999799e105befb6a2907b8cecb54c3e6d86dbf3e877ddf54e6fd102ebb2604381d0fa89bce0b3b4f70db6cb9ed3b5c85076e13de8369e81431926
2025-02-02 20:58:10 +00:00
Fmt Bot 282bc14d6e 2025-02-02 automated rustfmt nightly 2025-02-02 01:18:01 +00:00
merge-script bc1e3c0bfc
Merge rust-bitcoin/rust-bitcoin#3968: Add `LockTime` examples
c16eab7723 Add examples to `absolute::LockTime` (Jamil Lambert, PhD)
2d73746ad1 Improve `from_consensus` example (Jamil Lambert, PhD)
25a6742573 Add examples to `relative::LockTime` (Jamil Lambert, PhD)

Pull request description:

  To conform to API guidelines add examples to both `relative` and `absolute` `LockTime`.

  As discussed in #3967 add an example that doesn't round trip in `relative::LockTime::from_consensus`.

ACKs for top commit:
  tcharding:
    ACK c16eab7723
  apoelstra:
    ACK c16eab772375a0b8f82789a3453f7e983523fd50; successfully ran local tests

Tree-SHA512: 898db2dbcccfc54971c65bb72030505dbcfffb035aa74eb376cc1eb8d3619c205338899a58fb9f267fca192c11e7ae48fc17a2d9019c5ff4bd34bf029fa9a48f
2025-01-31 23:37:40 +00:00
Jamil Lambert, PhD 12a1c3c4b7
Add `script` tests
Add tests to kill all the mutants found by `cargo mutants` in `scripts`,
except for those related to `serde`.
2025-01-30 21:46:11 +00:00
Martin Habovstiak dd2df2bf10 Delete `TxOut::NULL`
We've stopped using `TxOut::NULL` in the code and we want to restrict
`Amount` to 21M BTC, so we are now deleting this constant without
deprecation. Deprecation can be backported if needed.
2025-01-29 23:02:51 +01:00
merge-script 98db7bca74
Merge rust-bitcoin/rust-bitcoin#3971: primitives: Add tests to `script::borrowed` and `script::owned`
6cde537d9b Add `ScriptBuf` tests (Jamil Lambert, PhD)
566a6e5da6 Add `Script` tests (Jamil Lambert, PhD)

Pull request description:

  Add tests to both `primitives/src/script/borrowed.rs` and `primitives/src/script/owned.rs` to kill the mutants found by running `cargo mutants`.

ACKs for top commit:
  tcharding:
    ACK 6cde537d9b
  apoelstra:
    ACK 6cde537d9ba9bd56495ca47afb3f9a560e9b4358; successfully ran local tests
  Kixunil:
    ACK 6cde537d9b

Tree-SHA512: fc36c1e9249753ebcb0f72e8195c85a7eccd3a0b8b3e4e753102d405494e1f72cd1cdfb6f12af41a9aa6f2ff10142b95827039fa428997b96510a5e262007b25
2025-01-29 19:20:33 +00:00
Jamil Lambert, PhD c16eab7723
Add examples to `absolute::LockTime` 2025-01-29 16:07:49 +00:00
Jamil Lambert, PhD 2d73746ad1
Improve `from_consensus` example
The existing example at first look seemed to contradict the doc above
that the function would not round trip.

Update the example to show the useage for both a time and height based
lock time.

Replace unwrap() with ?
2025-01-29 16:07:49 +00:00
Jamil Lambert, PhD 25a6742573
Add examples to `relative::LockTime` 2025-01-29 16:07:35 +00:00
Jamil Lambert, PhD 6cde537d9b
Add `ScriptBuf` tests
Add tests to `owned` to kill the mutants found by `cargo mutants`.
2025-01-28 20:31:17 +00:00
Jamil Lambert, PhD 566a6e5da6
Add `Script` tests
Add tests to `borrowed` to kill the mutants found by `cargo mutants`.
Tests for `to_bytes` were not added since it is deprecated.
2025-01-28 20:30:31 +00:00
merge-script 28b867d8de
Merge rust-bitcoin/rust-bitcoin#3966: Change `#[must_use]` to be the same as stdlib
cb5ffde9ee Change `#[must_use]` to be the same as stdlib (Jamil Lambert, PhD)

Pull request description:

  Change the iterator `#[must_use]` message to be the same as stdlib uses.

  Message taken from https://doc.rust-lang.org/src/core/iter/traits/iterator.rs.html#38

  Close #3962

ACKs for top commit:
  Kixunil:
    ACK cb5ffde9ee
  tcharding:
    ACK cb5ffde9ee

Tree-SHA512: 4866d9bddccdc28e2ba2b15cb4041851abb1512cea0317f20d2e2306df6dcd1addb828d61a84a2083d3ef47abb5cf9276e857cf098de2d28b4706ecd0a1f24fa
2025-01-28 16:53:58 +00:00
Jamil Lambert, PhD cb5ffde9ee
Change `#[must_use]` to be the same as stdlib 2025-01-27 20:13:29 +00:00
Jamil Lambert, PhD bc8a378187
Rename `day_` variables to `time_`
To be consistent with similar lock height test function rename
`day_after/before` to `time_after/before` and change the same `time` to
`time_same`.
2025-01-27 16:45:24 +00:00
Jamil Lambert, PhD 922392268f
Combine satisfied by into singe tests
Include the satisfied by same time/height into the previous test that
checks above and below.
2025-01-27 16:44:41 +00:00
Jamil Lambert, PhD 33a7a35bbb
Make naming consistent in tests
Rename all occurrences of LockTime to specify if it is by height or
time.
2025-01-27 16:42:45 +00:00
Fmt Bot a3d2b2a93a 2025-01-26 automated rustfmt nightly 2025-01-26 01:16:37 +00:00
Jamil Lambert, PhD 40dc896932
Improve relative::LockTime tests
Cargo mutants found some untested mutants.

Add test cases to kill the mutants.
2025-01-23 19:29:42 +00:00
Jamil Lambert, PhD 2c73546454
Improve absolute::LockTime tests
Cargo mutants found some untested mutants.

Add test cases and refactor to improve readability.
2025-01-23 19:29:35 +00:00
Tobin C. Harding 7277092af4
Remove mutagen
Back in 2022 we elected to use `mutagen` for mutation testing. Since
then `cargo mutants` has progressed to a point where we would now like
to use it instead.

Remove all the `mutagen` stuff and update the lock files.

Close: #2829
2025-01-21 09:43:12 +11:00
Tobin C. Harding 39523ea1f5
units: Remove InputString from the public API
Currently `InputString` is in the public API of `units` because of the
trait bound on `parse::int()`. We can just do the monomorphisisation
manually to remove it.

This patch renames `int` to have three different names, one for `&str`
one for `String`, and one for `Box<str>`.
2025-01-15 08:09:38 +11:00
Fmt Bot 762f6630fe 2025-01-05 automated rustfmt nightly 2025-01-05 01:22:00 +00:00
merge-script 3b2363b2c6
Merge rust-bitcoin/rust-bitcoin#3846: Remove `test_` prefix from unit tests
85e04315d5 Remove test_ prefix from unit tests (Tobin C. Harding)

Pull request description:

  There is a loose convention in Rust to not use `test_` prefix. The reason being that `cargo test` outputs 'test <test name>' using the prefix makes the output stutter.

  This patch smells a bit like code-churn but having the prefix in some places and not others is confusing to new contributors and is leading me to explain this many times now. Lets just fix it.

  Remove the prefix unless doing so breaks the code.

ACKs for top commit:
  shinghim:
    ACK 85e04315d5
  apoelstra:
    ACK 85e04315d5eb90075ce55bf18fab8876a4583def; successfully ran local tests

Tree-SHA512: d90ae5ef75cc5e5a8f43f60819544f1a447f13cbe660ba71e84b8f27bfcc04a11d3afde0ed56e4eea5c73ebc3925024b800a1b995f73142cab892f97a414f14a
2025-01-04 00:23:46 +00:00
merge-script dddb63f4ad
Merge rust-bitcoin/rust-bitcoin#3842: Remove usage of impl_from_infallible in leaf crates
0d8e9ef096 Remove usage of impl_from_infallible in leaf crates (Tobin C. Harding)

Pull request description:

  Rust macros, while at times useful, are a maintenance nightmare. And we have been bitten by calling macros from other crates multiple times in the past.

  In a push to just use less macros remove the usage of the `impl_from_infallible` macro in all the leaf crates and just write the code.

ACKs for top commit:
  apoelstra:
    ACK 0d8e9ef096fd60fcdb7928697c8f554bf428b754; successfully ran local tests; this is a great change

Tree-SHA512: c4cff4517f7846d91142f26d451c2bcafc014a0921d11ac1487eab087449f12023c3b4fc57e1bc72ed3ea58406b4c3d24bbd846df21353f5d7ecb4a4b8bfb0b2
2025-01-03 23:42:21 +00:00
merge-script 192ea44d87
Merge rust-bitcoin/rust-bitcoin#3841: Remove macro `debug_from_display`
fd8d563b87 Remove macro debug_from_display (Tobin C. Harding)

Pull request description:

  Rust macros, while at times useful, are a maintenance nightmare. And we have been bitten by calling macros from other crates multiple times in the past.

  In a push to just use less macros remove the `debug_from_display` macro and just write the code.

  This is an API breaking change to `internals` but an internal change only to any of the _real_ crates.

ACKs for top commit:
  apoelstra:
    ACK fd8d563b873c87a996d82062285169e16e3f0c13; successfully ran local tests; this is a great change

Tree-SHA512: 26faa6645d010c1b5873d584c36d0fc52b73553d88be3226937431210ccc2479548757593b8a151936e9fa280cb3c604b241511f24ef0aa5cbf3f424d7ecf215
2025-01-03 23:01:11 +00:00
merge-script 93241c3cc8
Merge rust-bitcoin/rust-bitcoin#3838: primitives: Add core re-exports
79791e7444 primitives: Add core re-exports (Tobin C. Harding)

Pull request description:

  This is not strictly needed but we would like to copy the public macros from `units` to primitives and they use `$crate::_export` paths.

ACKs for top commit:
  apoelstra:
    ACK 79791e7444326d284bdf42c40926f6b05a6b65f7; successfully ran local tests; sure, lgtm

Tree-SHA512: cdf683041dca07bc53ae8e70806cfb74ef79e94b8699112774e3bfb3efd29cc270bd24ab03a448197edd1d143dec07d18d9a3a92d74097d87478d40954392ee4
2025-01-03 17:53:22 +00:00
Tobin C. Harding 85e04315d5
Remove test_ prefix from unit tests
There is a loose convention in Rust to not use `test_` prefix. The
reason being that `cargo test` outputs 'test <test name>' using the
prefix makes the output stutter.

This patch smells a bit like code-churn but having the prefix in some
places and not others is confusing to new contributors and is leading me
to explain this many times now. Lets just fix it.

Remove the prefix unless doing so breaks the code.
2025-01-02 10:06:50 +11:00
Tobin C. Harding 0d8e9ef096
Remove usage of impl_from_infallible in leaf crates
Rust macros, while at times useful, are a maintenance nightmare. And
we have been bitten by calling macros from other crates multiple times
in the past.

In a push to just use less macros remove the usage of the
`impl_from_infallible` macro in all the leaf crates and just write the
code.
2025-01-02 07:51:21 +11:00
Tobin C. Harding fd8d563b87
Remove macro debug_from_display
Rust macros, while at times useful, are a maintenance nightmare. And
we have been bitten by calling macros from other crates multiple times
in the past.

In a push to just use less macros remove the `debug_from_display`
macro and just write the code.

This is an API breaking change to `internals` but an internal change
only to any of the _real_ crates.
2025-01-02 07:31:13 +11:00
Tobin C. Harding 1a8f5b19fb
Update to rust-ordered 0.4.0
We just released a version of `ordered` that makes `ArbitraryOrd` object
safe - use it.

Upgrade to the latest version of `rust-ordered` - `v0.4.0`.
2024-12-31 10:42:24 +11:00
Tobin C. Harding 20b798175c
Update to latest rust-ordered
I just went to town on the `rust-ordered` crate to get it ready for
releasing a `1.0` version. None of the changes effect our usage here in
`rust-bitcoin`.

Upgrade to the latest version of `rust-ordered` - `v0.3.0`.
2024-12-30 08:35:01 +11:00
Tobin C. Harding 79791e7444
primitives: Add core re-exports
This is not strictly needed but we would like to copy the public macros
from `units` to primitives and they use `$crate::_export` paths.
2024-12-28 09:03:42 +11:00
merge-script f4069fcd61
Merge rust-bitcoin/rust-bitcoin#3780: Use uniform capitalisation of SegWit in rustdocs
e56f461916 Make capitalization of SegWit uniform in strings (Jamil Lambert, PhD)
3520e832ac Make capitalization of SegWit uniform in rustdocs (Jamil Lambert, PhD)

Pull request description:

  Fixes #3770

  - Searched and replaced all occurrences of `//` * `segwit` (case insensitive) with `//` * `SegWit`
  - Searched and replaced all occurrences of `"` * `segwit` (case insensitive) with `"` * `SegWit`

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

Tree-SHA512: c56704102d8e86f26378bb302d5fdc7ea21d41fd49f55606e589ec32ff896f1adfb1960d8fb29abccfbf298b90fc357ed609d80fd0163dcb4ff01573646b02c3
2024-12-17 18:10:44 +00:00
Jamil Lambert, PhD 3520e832ac
Make capitalization of SegWit uniform in rustdocs 2024-12-17 14:28:28 +00:00
Tobin C. Harding b5f553d866
hashes: Bump version to 0.16.0
We need to do a quick release because it turns out I was wrong in
thinking that making `hex` an optional dependency is not a breaking
change.

```
the package `bitcoin` depends on `bitcoin_hashes`, with features: `hex`
but `bitcoin_hashes` does not have these features. It has a required
dependency with that name, but only optional dependencies can be used as
features.
```

Add a changelog, bump the version, depend on the new version repo wide,
and update the lock files - like its our job.
2024-12-16 12:41:17 +11:00
merge-script fcbe970ddb
Merge rust-bitcoin/rust-bitcoin#3734: Move script hash types to `primitives`
6e01383f16 api: Run just check-api (Tobin C. Harding)
3855d3cc83 Move script hashes to primitives (Tobin C. Harding)
d1dd63d6d4 Remove wildcard in script re-exports (Tobin C. Harding)

Pull request description:

  Woops, this should have been done before v0.101.0 was released.

  Move the `ScriptHash` and `WScriptHash` types to `primitives`.

  Requires moving constants and error types as well. We re-export the errors because they are in the `mod.rs` file so they should appear in both `primitives::script::FooError` and `bitcoin::script::FooError`.

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

Tree-SHA512: 4ef838ee9c4cb3eb308ffa855ea5d6f5ad46f81c17b9413faa493a46a262afc18b47f28a0fdd5fc675eea31b895d0eb0e2505a523e820504ec88d9334d6874b4
2024-12-14 06:14:39 +00:00
merge-script 3c25a8acbc
Merge rust-bitcoin/rust-bitcoin#3753: Update to arbitrary v1.4
463fbd16f1 Update to arbitrary v1.4 (Tobin C. Harding)

Pull request description:

  Out with the old in with the new, update to the latest minor version of arbitrary.

  No real reason to do this except that I wanted to add the minor version instead of using just `1` so that we don't accidentally pull a new minor version in during a patch release (after we 1.0).

  (I'm unsure if this is necessary and did not test it against MSRV.)

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

Tree-SHA512: 88913f993b97abba224ba94ee9d5057894bb7142fef07e557de03a423da314a9f21632932fb64c4c27c676051bbb6e90d9b90bfa944af810e834bf42707d3cff
2024-12-14 01:39:38 +00:00
Tobin C. Harding 463fbd16f1
Update to arbitrary v1.4
Out with the old in with the new, update to the latest minor version of
arbitrary.

No real reason to do this except that I wanted to add the minor version
instead of using just `1` so that we don't accidentally pull a new minor
version in during a patch release (after we 1.0).
2024-12-12 15:44:45 +11:00
Tobin C. Harding 38c329c1b7
Add additional catagories
Add `no-std` category to the soon-to-be-released leaf crates.
Also add `cryptography` to the `bitcoin_hashes` crate.

Close: #3731
2024-12-12 15:14:26 +11:00
Tobin C. Harding 3855d3cc83
Move script hashes to primitives
Woops, this should have been done before v0.101.0 was released.

Move the `ScriptHash` and `WScriptHash` types to `primitives`.

Requires moving constants and error types as well. We re-export the
errors because they are in the `mod.rs` file so they should appear in
both `primitives::script::FooError` and `bitcoin::script::FooError`.
2024-12-12 15:14:00 +11:00
merge-script 33f6d028ab
Merge rust-bitcoin/rust-bitcoin#3722: clippy: Set avoid-breaking-exported-api to false
adaf4ac086 Set avoid-breaking-exported-api to false (Tobin C. Harding)

Pull request description:

  These lints are valuable, lets get at em.

ACKs for top commit:
  apoelstra:
    ACK adaf4ac086553674803fadfde776d6dd013a7964; successfully ran local tests; will probably be a problem repeatedly for the next few days until we get through all currently-open PRs

Tree-SHA512: 56617a2f4aeafceef72008232cee817d45b62c040d7f1938631291c5d73627851cfbefc4b4000cd380ecb5c7e1379d1022f6cc90a4b68c819c78fb883bee0b3a
2024-12-11 15:41:06 +00:00
merge-script 540c712dc7
Merge rust-bitcoin/rust-bitcoin#3712: primitives: Add `must_use`
549be547ac primitives: Add must_use (Tobin C. Harding)

Pull request description:

  Enable lint `clippy::return_self_not_must_use` and add attribute `must_use` as required.

  Also run the linter with `clippy::must_use_candidate` enabled and manually check every warning site.

  Done as part of #3185

ACKs for top commit:
  apoelstra:
    ACK 549be547accdb13037bf3ef60310ca30d045dce0; successfully ran local tests; nice! will one-ACK merge
  sanket1729:
    ACK 549be547ac

Tree-SHA512: b22a6849fd0f4a7b65e1a9816efd47d411dcf2a5d5d46ae75b2b4d2389d3c9f46ab271314b112de9cd6fdc52cac7b53a632642e9bd90092d7065a8646e1362ec
2024-12-11 12:55:48 +00:00
Tobin C. Harding adaf4ac086
Set avoid-breaking-exported-api to false
These lints are valuable, lets get at em.

Changes are API breaking but because the changes make functions consume
self for types that are `Copy` downstream should not notice the breaks.
2024-12-11 10:11:50 +11:00
Tobin C. Harding 549be547ac
primitives: Add must_use
Enable lint `clippy::return_self_not_must_use` and add attribute
`must_use` as required.

Also run the linter with `clippy::must_use_candidate` enabled and
manually check every warning site.

While we are at it change the current `must_use` usages to have no
message. We can always add a message later if needed.
2024-12-10 11:02:06 +11:00
Tobin C. Harding 6ae35be0db
primitives: Reduce alloc requirements
Recently we reduced the `alloc` requirements in `units` but we did not
propagate these changes up to `primitives`.

Remove a bunch of `alloc` feature gating from `primitives`.
2024-12-09 13:57:26 +11:00
Tobin C. Harding 77085a1fa1
units: Remove serde from amounts
The `Amount` and `SignedAmount` were not supposed to implement `serde`
traits by design because doing so implicitly uses sats. We provide two
modules `as_sat` and `as_btc` to allow users to explicitly serialize in
their preferred format.

In commit: `d57ec019d5 Use Amount type for TxOut value field` derives
were added for `serde` and we did not notice it during review.
2024-11-28 12:55:43 +11:00
Fmt Bot 500cd10802 2024-11-17 automated rustfmt nightly 2024-11-17 01:23:55 +00:00
merge-script d32422cdb7
Merge rust-bitcoin/rust-bitcoin#3611: Hashes: Make `hex` dependency optional
ec06028f63 hashes: Make hex dependency optional (Tobin C. Harding)
9dce0b4b8c Remove hex string trait bounds from GeneralHash (Tobin C. Harding)
766f498b33 Pull serde stuff out of impl_bytelike_traits macro (Tobin C. Harding)

Pull request description:

  This is done in 3 parts:

  1. Pull the `serde` stuff out of `impl_bytelike_traits` to fix the bug described here: https://github.com/rust-bitcoin/rust-bitcoin/issues/2654#issuecomment-2470716693
  2. Prepare the `hashes` trait by removing string/hex trait bounds from `GeneralHash` and also pull the hex/string stuff out of `impl_bytelike_traits`
  3. Make hex optional, including adding custom debug logic when `hex` feature is not enabled

  Patch 3 is tested in `hashes/embedded`, by the new `debug` unit test, and there is a `Midstate` unit test as well that covers the `Debug` impl.

  Close: #2654 - BOOM!

ACKs for top commit:
  apoelstra:
    ACK ec06028f63ba591a14c3a15cdfd410bb5ff1c09b; successfully ran local tests; nice!

Tree-SHA512: 85eb10d36a4581af6cd700f7ff876585bcc114c60e9864906e65659f3b3ee550fe6d9f40ca4230d870a9e23f0720723e11443ec329f16e40259a259b9be57466
2024-11-15 18:31:42 +00:00
Tobin C. Harding 53d3ab18b9
Bump version of bitcoin-primitives to 0.101.0
In preparation for releasing `primitives v0.101.0` bump the version
number, add a changelog entry, update the lock files, and depend on the
new version in all crates that depend on `primitives`.
2024-11-15 10:58:46 +11:00
Tobin C. Harding 175171d339
Make primitives re-exports uniform
Remove the whitespace, it should never have been there and put the
attributes in the same order.

Internal change only.
2024-11-15 10:58:46 +11:00
Tobin C. Harding 3e9267071f
primitives: Update crate level docs re allocator
We slightly improved the situation but much of the functionality is
still behind the `alloc` feature.
2024-11-15 10:58:45 +11:00
Tobin C. Harding 7819e50055
Move Block to primitives
On the way re-design the API by doing:

- Introduce `Checked` and `Unchecked` tags
- Rename the `txdata` field to `transactions`
- Make the `Block` fields private
- Add getters for `header` and `transactions` fields
- Move the various `compute_` methods to be free standing functions
- Make the `check_` functions private
- Introduce extension traits
2024-11-15 07:16:21 +11:00
Tobin C. Harding ec06028f63
hashes: Make hex dependency optional
The only reason we need `hex-conservative` is to parse strings and
format them as hex. For users that do not require this functionality we
can make the `hex-conservative` crate an optional dependency.

The `serde` feature requires `Display` so we enable `hex` from the
`serde` feature.

If `hex` feature is not enabled we still need to be able to debug so
provide `fmt::Debug` functionality by way of macros.

Close: #2654
2024-11-14 09:36:55 +11:00
merge-script 3d17ec962f
Merge rust-bitcoin/rust-bitcoin#3576: transaction: Add plural field getters
be553217f1 transaction: Add plural field getters (Tobin C. Harding)

Pull request description:

  The `Transaction` type was created way back when, and the field names were named using singular (`input` and `output`). In hindsite plural probably should have been used since its more idiomatic Rust but the fields have been around so long now that re-naming them is going annoy a whole bunch of people.

  As a compromise add getters that use plural, immutable and mutable versions, for both the `input` and `output` fields.

  Close: #822

ACKs for top commit:
  apoelstra:
    ACK be553217f19069dd9ca28c3ba5953059df9caa1b; successfully ran local tests; though will let this sit a couple days before merging
  sanket1729:
    ACK be553217f1

Tree-SHA512: 4df9db179a69c9eea2940008c9eca9f9b9b9d9f27e2a174db39bfb3d5c32a9d06b26e5349e5baf5af78600ae128d1b534559baf7335aec6df1238de96a508764
2024-11-13 17:42:55 +00:00
Tobin C. Harding 9dce0b4b8c
Remove hex string trait bounds from GeneralHash
For the `hashes` crate we would like to make `hex` an optional
dependency. In preparation for doing so do the following:

- Remove the trait bounds from `GeneralHash`
- Split the hex/string stuff out of `impl_bytelike_traits` into a
  separate macro.
2024-11-13 14:01:13 +11:00
Tobin C. Harding 766f498b33
Pull serde stuff out of impl_bytelike_traits macro
The `impl_bytelike_traits` macro is public and it is used in the
`hash_newtype` macro, also public.

Currently if a user calls the `hash_newtype` macro in a crate that
depends on `hashes` without the `serde` feature enabled and with no
`serde` dependency everything works. However if the user then adds a
dependency that happens to enable the `serde` feature in `hashes` their
build will blow up because `serde` code will start getting called from
the original crate's call to `hash_newtype`.

Pull the serde stuff out of `hash_newtype` and provide a macro to
implement it `impl_serde_for_newtype`.
2024-11-13 12:28:47 +11:00
Tobin C. Harding 195615c14d
Fix bug in witness stack getters
In #2646 we introduced a bug in the taproot witness stack getter
functions, of which we have three:

- `tapscript`
- `taproot_control_block`
- `taproot_annex`

Each returns `Some` if a possible bytes slice is found (with no other
guarantees).

Use `taproot_annex` combined with getters from `primitives` to implement
the other two getters. This simplifies the code and fixes the bug.

Add an additional getter to `primitives` `Witness::third_from_last`.

Fix: #3598
2024-11-13 10:55:51 +11:00
Tobin C. Harding 727c519efa
Re-export amount from primitives
We are trying to make it so that what ever crate a user uses they see
the same module/type tree (if the module or type exists).

E.g., one can do either of these.

If they use `bitcoin`:

```
use bitcoin::{
    amount, block, fee_rate, weight, merkle_tree, opcodes,
    pow, script, sequence, transaction, witness,
};
```

Or if they use `primitives`:

```
use bitcoin_primitives::{amount, block, fee_rate, weight};
```

The above imports were tested and `amount` was found to be missing.
2024-11-11 14:21:47 +11:00
Jamil Lambert, PhD 1649b68589
Standardize wording to `constructs a new`
There is a range of different wordings used in the docs of constructor
type functions.

Change all to start with `Constructs a new` or `Constructs an empty`.
2024-11-05 13:02:26 +00:00
Jamil Lambert, PhD 27f94d5540
Replace `creates` with `constructs`
In functions that act like constructors there is a mixture of the usage
of `creates` and `constructs`.

Replace all occurrences of `creates` with `constructs` in the first line
of docs of constructor like functions.
2024-11-05 12:47:28 +00:00
Tobin C. Harding be553217f1
transaction: Add plural field getters
The `Transaction` type was created way back when, and the field names
were named using singular (`input` and `output`). In hindsite plural
probably should have been used since its more idiomatic Rust but the
fields have been around so long now that re-naming them is going annoy a
whole bunch of people.

As a compromise add getters that use plural, immutable and mutable
versions, for both the `input` and `output` fields.

Close: #822
2024-11-04 15:07:54 +11:00
Tobin C. Harding 6555143478
bitcoin: Re-format crate level re-exports
Is there any advantage trying to lay out the re-exports to give users an
idea of the crate structure?

We have the explicit aim that users who depend on `bitcoin` do not ever
need to reach directly into `primitives` (or `units`) however it is kind
of nice to know where things come from, saves jumping to multiple files
looking for them (for those of us that jump to files manually).

I do not know how all the re-exports interact with other folks IDEs, I
personally open files manually and just remember where stuff is.
2024-11-04 08:49:03 +11:00