Commit Graph

1211 Commits

Author SHA1 Message Date
Tobin C. Harding a2b019f823
Enable internals "alloc" feature
We have 2 crates that require an allocator, `bitcoin` and `base58ck` -
these crates should enable the "alloc" feature when depending on
`internals`.

For `units` we use the `internals::error::InputString` but do not enable
the "alloc" feature - this is a bug, it means that the parsed string is
being lost from the error types that use `InputString`.

Enable "alloc" for `bitcoin`, `base58ck`, and `units`.

- `bitcoin` and `base56ck` is just for good measure so we don't get
  bitten later on.
- `units` is a bug fix and requires a point release.
2024-04-04 08:18:51 +11:00
Andrew Poelstra b5fbdcd68a
Merge rust-bitcoin/rust-bitcoin#2541: Reduce usage of `Network` in the public API
f6467ac98d Minimize usage of Network in public API (Tobin C. Harding)
3ec5eff56e Add Magic::from_params (Tobin C. Harding)

Pull request description:

  Minimize usage of the `Network` enum in the public API.

  See #2225 for context, and https://github.com/rust-bitcoin/rust-bitcoin/pull/1291#discussion_r1492993788 for an interpretation of that long discussion.

  Close: #2169

ACKs for top commit:
  sanket1729:
    reACK f6467ac98d.
  apoelstra:
    ACK f6467ac98d

Tree-SHA512: f12ecd9578371b3162382a9181f7f982e4d0661915af3cfdc21516192cc4abb745e1ff452649a0862445e91232f74287f98eb7e9fc68ed1581ff1a97b7216b6a
2024-04-03 13:50:47 +00:00
Tobin C. Harding f6467ac98d
Minimize usage of Network in public API
A release or so ago we added `non_exhaustive` to the `Network` enum,
this turned out to make usage of the enum un-ergonomic for downstream
users. After much debate we decided that a way forward was to just
minimize the usage of the enum in the public API by instead use
`AsRef<Params>` so that downstream could define their own network enum
based on the networks they support.

Minimize usage of `Network` by using `AsRef<Params>` as a parameter type
instead. "minimize" because the `Network` still appears in some places.
2024-04-03 13:32:39 +11:00
Tobin C. Harding 1bb32febbd
Use manual docs attributes
Currently we are using `Self` (in backticks) in the docs to functions
defined by the `do_iml` macro, this is a bit lazy, we can do better than
that.

Use `doc` attribute and the `$ty` macro variable to construct the docs
to use the type name.
2024-04-03 11:14:32 +11:00
Tobin C. Harding 19f70959e1
Document private from_hex_internal function
Use a code comment to document the calling restrictions of private
function `from_hex_internal`. (Code comment because comment is not well
formed as per convention in this codebase.)
2024-04-03 11:12:45 +11:00
Tobin C. Harding 81a704302c
Improve rustdocs on U256 type
Improve the rustdocs on the private `U256` type by doing:

- Remove link to self within constructors, just use backticks
- Use `U256` instead of `Self` or `self`
- Fix incorrect usage of `CompactTarget` [0]

[0] We knew this was wrong when we merged it but because the docs are
private we elected to do this follow up patch.
2024-04-03 11:09:47 +11:00
Tobin C. Harding e1869340be
Upgrade secp dependency
Upgrade `rust-secp256k1` to the latest version `v0.29.0`. This removes
the duplicate deps as well.
2024-04-03 09:24:41 +11:00
Tobin C. Harding 3ec5eff56e
Add Magic::from_params
Currently `Magic` has per network consts but no way to dynamically get
the magic bytes for a network. Note also that we are currently trying to
reduce the usage of `Network` in the public API.

Add a public constructor to the `Magic` type that accepts a `Params`
parameter to determine the network to use.
2024-04-03 07:58:33 +11:00
Tobin C. Harding f019e24f1f
Add hex parsing to pow types
The `pow` types implement `fmt::LowerHex` but do not implement hex
parsing.

Add inherent methods `from_hex` and `from_prefixed_hex` to the
`pow` types.
2024-04-03 07:10:20 +11:00
Andrew Poelstra 499f36f972
Merge rust-bitcoin/rust-bitcoin#2337: Add check to max difficulty transition threshold
fd6fedc3ad Improve API for max target threshold calculation (Tobin C. Harding)
6e47d57744 Rename difficulty transition threshold functions (Tobin C. Harding)
4121c9a09f Rename Params::pow_limit to max_attainable_target (Tobin C. Harding)
f0f6d3f162 Take Params instead of Network in difficulty function (Tobin C. Harding)
104dee9376 Debug assert that target != zero in difficulty calc (Tobin C. Harding)
c1ba496a07 Document current behaviour of difficulty_float (Tobin C. Harding)
3d01146374 Allow needless-borrows-for-generic-args (Tobin C. Harding)
2a6821b426 Use link to CompactTarget in rustdoc (Tobin C. Harding)

Pull request description:

  When computing the maximum difficulty transition threshold we forgot to check that the returned `Target` is not bigger than the maximum. This value is network specific so keep the original logic but with `_unchecked` on the function name.

  This was noted in the discussion on #2161

ACKs for top commit:
  apoelstra:
    ACK fd6fedc3ad
  sanket1729:
    ACK fd6fedc3ad

Tree-SHA512: 520ee2a07edb251c84b5ce8b48ed6e5a5c1945126dc7bcdb5570e97101ec4a3dc63fa7992725194869e22b21ee4f5955579d5e2499fcb48167637fd1fb3ae74d
2024-04-02 13:18:29 +00:00
Tobin C. Harding fd6fedc3ad
Improve API for max target threshold calculation
The maximum target threshold has a network dependant upper bound.
Currently we are not checking this bound. One complication is that there
is currently heated open debate around the `Network` type.

We can bypass the `Network` issue by using `AsRef<Params>` instead.

Add a function that does the checks based on the `Params` type as well
as an unchecked version.
2024-04-02 11:41:49 +11:00
Tobin C. Harding 6e47d57744
Rename difficulty transition threshold functions
These two functions calculate the min/max threshold transition which is
a _target_ not a "difficulty" number. Using "difficulty" in the function
name is unnecessarily confusing.

Rename and deprecate the functions.
2024-04-02 11:41:38 +11:00
Tobin C. Harding 4121c9a09f
Rename Params::pow_limit to max_attainable_target
The maximum "attainable" target is a `rust-bitcoin` thing, Core use max
unattainable.

Deprecated the `Params::pow_limit` field and add a new field
`max_attainable_target`.

The `Params` type is `non_exhaustive` so this is not an API breaking
change.
2024-04-02 11:41:38 +11:00
Tobin C. Harding f0f6d3f162
Take Params instead of Network in difficulty function
What we really want is the maximum target, but since this is a const in
`Params` use an `AsRef<Params>` argument in the `difficulty` functions.

Requires implementation of `AsRef<Params> for Params`.
2024-04-02 11:41:34 +11:00
Tobin C. Harding 104dee9376
Debug assert that target != zero in difficulty calc
The `difficulty` calculation requires dividing a target value by `self`.
Add an assertion that `self` is not zero to help devs debug this.

Note that this should never really be hit, but its possible there is a
bug somewhere causing the target to be set to zero - so this may help
debugging.

Also, add panics section to rustdocs.
2024-04-02 11:40:46 +11:00
Tobin C. Harding c1ba496a07
Document current behaviour of difficulty_float
Improve rustdocs on the `Target::difficulty_float` function,
specifically the return value if self is zero.
2024-04-02 11:40:45 +11:00
Tobin C. Harding 3d01146374
Allow needless-borrows-for-generic-args
This lint triggers when parsing a reference to a large struct as a
generic argument, which is wrong.

Allow it crate wide because [subjectively] this lint never warns for
anything useful.
2024-04-02 11:40:41 +11:00
Andrew Poelstra 6a2fd96ff6
Merge rust-bitcoin/rust-bitcoin#2581: Implement ArbitraryOrd for relative::LockTime
d91cdd20bf docs: Document ordered feature (Tobin C. Harding)
3520f550f0 Implement ArbitraryOrd for relative::LockTime (Tobin C. Harding)

Pull request description:

  TL;DR As we do for `absolute::LockTime` and for the same reasons; implement `ArbitraryOrd` for `relative::LockTime`.

  locktimes do not have a semantic ordering if they differ (blocks, time) so we do not derive `Ord` however it is useful for downstream to be able to order structs that contain lock times. This is exactly what the `ArbitraryOrd` trait is for.

  Fix: #2566

ACKs for top commit:
  sanket1729:
    ACK d91cdd20bf
  apoelstra:
    ACK d91cdd20bf

Tree-SHA512: 52ace9222e765dfa266d003b4aff3e93e35d1414c9fd579c4a4a36998d6d1b08bf6d4964a6f1c1d769068d65e47a882495daa4aacf254909a35dce8e01c99a9e
2024-04-02 00:36:12 +00:00
Tobin C. Harding 2a6821b426
Use link to CompactTarget in rustdoc 2024-04-02 11:33:26 +11:00
Andrew Poelstra 684b453b7c
Merge rust-bitcoin/rust-bitcoin#2632: internals: Release tracking PR `v0.3.0`
af6dc1db02 internals: Bump version to 0.3.0 (Tobin C. Harding)

Pull request description:

  In preparation for release add a changelog and bump the version number.

  Please note, the changelog is pretty terse.

ACKs for top commit:
  apoelstra:
    ACK af6dc1db02
  sanket1729:
    ACK af6dc1db02

Tree-SHA512: b70d4b9de7de90aba3cbff90dd7f25c5ac801d020dbdfe3e64af4c079347cba726aa783a94fc777e7bf177db8402b54948c2dfd4a766d90c1a7a7a6bdfd36136
2024-04-02 00:09:04 +00:00
Tobin C. Harding d91cdd20bf
docs: Document ordered feature
Add "ordered" to the list of features in the `bitcoin` crate level docs.
2024-04-02 08:10:38 +11:00
Fmt Bot a565db9fdd 2024-03-31 automated rustfmt nightly 2024-03-31 01:03:18 +00:00
Tobin C. Harding af6dc1db02
internals: Bump version to 0.3.0
In preparation for release add a changelog and bump the version number.
2024-03-27 09:44:30 +11:00
Tobin C. Harding 3520f550f0
Implement ArbitraryOrd for relative::LockTime
TL;DR As we do for `absolute::LockTime` and for the same reasons;
implement `ArbitraryOrd` for `relative::LockTime`.

locktimes do not have a semantic ordering if they differ (blocks, time)
so we do not derive `Ord` however it is useful for downstream to be able
to order structs that contain lock times. This is exactly what the
`ArbitraryOrd` trait is for.

Update the rustdocs in `relative` and mirror the docs changes in
`absolute`.

Fix: #2566
2024-03-26 10:36:50 +11:00
Andrew Poelstra 9df59639ce
Merge rust-bitcoin/rust-bitcoin#2621: Release tracking PR: `hashes v0.14.0`
0ca5a43ce5 hashes: Bump version to v0.14.0 (Tobin C. Harding)

Pull request description:

  In preparation for release add a changlelog entry and bump the version.

  Note the hashes 0.13.0 dependency stays in the dependency graph because of secp, we can update secp after releasing `hashes` then update the secp dependency in `rust-bitcoin` thereby removing the `hashes v0.13.0` dependency - phew.

  Note we are right to release this immediately, the two open PRs (#2337 and #2541) that touch `hashes` only add a clippy attribute so can safely be ignored.

ACKs for top commit:
  apoelstra:
    ACK 0ca5a43ce5
  sanket1729:
    ACK 0ca5a43ce5

Tree-SHA512: d1d26acb8fbf13f785b25add3f1dac05bb392b5bdbad16ead2bc5dd26f3d668824c4b653c373f88c3562a37e775146766680606cedd19db40e0f197b26ca86b8
2024-03-25 22:23:30 +00:00
Tobin C. Harding a7a99e06bb
Add a validation variant to `ParseError`
`require_network` is typically called as part of parsing, often in the
same line of code. Counter to our normal errors, it makes
`require_network` more ergonomic to use if we just return a `ParseError`
variant.

Close: #2507
2024-03-25 08:45:19 +11:00
Tobin C. Harding d5c52618a9
Move NetworkValidationError within file
Done in preparation for adding the `NetworkValidationError` as a variant
of `ParseError`.

Move the `NetworkValidationError` type to beneath `ParseError`.

Code move only, no other changes.
2024-03-25 06:54:12 +11:00
Andrew Poelstra c211e7be78
Merge rust-bitcoin/rust-bitcoin#2626: Replace TBD with 0.32.0
fd040f5e38 Replace TBD with 0.32.0 (Tobin C. Harding)

Pull request description:

  We are gearing up for the 0.32.0 release; replace all instances of TBD with the version number of the upcoming release.

ACKs for top commit:
  sanket1729:
    ACK fd040f5e38
  apoelstra:
    ACK fd040f5e38

Tree-SHA512: fe73fd47a794557742f618b21434cd3cc18cde0e861216716723bfcc9135accf63590e1ea60bfeda066acec7312c8b9f1bf09e7454e7161ccaba5ebe60af66fd
2024-03-24 15:15:09 +00:00
Andrew Poelstra bfd5255ae8
Merge rust-bitcoin/rust-bitcoin#2481: Improve base58 crate
af49841433 Hide base58::Error internals (Tobin C. Harding)
4f68e79da0 bitcoin: Stop using base58 errors (Tobin C. Harding)
669d5e8fc6 base58: Add InvalidCharacterError for decoding (Tobin C. Harding)
ec8609393b base58: Add error module (Tobin C. Harding)
42fabbab03 base58: Run the formatter (Tobin C. Harding)

Pull request description:

  Improve the error code in the new `base58` crate.

ACKs for top commit:
  apoelstra:
    ACK af49841433
  sanket1729:
    ACK af49841433

Tree-SHA512: c05479f02a9a58c7c98fd5987e760288562372e16cceeeb655f0a5385b4a8605945a3b6f7fcf473a7132a40f8dc90d204bc5e9e64fd2cc0bdc37dbcabb4ddc5c
2024-03-24 14:20:29 +00:00
Andrew Poelstra 26248b28ac
Merge rust-bitcoin/rust-bitcoin#2625: Put back in deprecated dust_value
c17db32df3 Pub back in deprecated dust_value (Tobin C. Harding)

Pull request description:

  When we renamed `dust_value` to `minimal_non_dust` we forgot to keep the original and deprecated it, doing so assists with the upgrade path.

  Put back in deprecated `dust_value`, linking to the rename.

  Renamed in #2255, found while testing upgrade of downstream software.

ACKs for top commit:
  tcharding:
    > ACK [c17db32](c17db32df3) I _think_ this matches the behavior of the old version
  apoelstra:
    ACK c17db32df3 I *think* this matches the behavior of the old version
  sanket1729:
    ACK c17db32df3

Tree-SHA512: 28e1bd2e1a0fd13c78c70ad2667b72b3bf649c293201b79c86c00f09d0126389ebaeb430b8dd32aeeec3d60cbd8761ae949f5784a5ea7756b1b9ae77ec96ce61
2024-03-24 13:56:34 +00:00
Andrew Poelstra 1656e1a09a
Merge rust-bitcoin/rust-bitcoin#2619: Call is_witness_program from witness_version
dec05b63e9 Refactor witness_version and is_witness_program (Tobin C. Harding)
dac552b436 Add unit tests for shortest/longest witness program (Tobin C. Harding)

Pull request description:

  Refactor `witness_version` and `is_witness_program`.

  - Patch 2 adds a couple of preparatory unit tests.
  - Patch 2 does the refactor

  Fix: #2618

ACKs for top commit:
  apoelstra:
    ACK dec05b63e9
  sanket1729:
    ACK dec05b63e9

Tree-SHA512: 3db0a1d8175cbb2fd18f3254854d02db3ad7efa2620b12f08d9727ef6bb5854f0a015917e57023cd2196a36d13276e80536a0e96318c44a1173da4f6793ca370
2024-03-22 23:52:42 +00:00
Tobin C. Harding fd040f5e38
Replace TBD with 0.32.0
We are gearing up for the 0.32.0 release; replace all instances of TBD
with the version number of the upcoming release.
2024-03-23 05:36:52 +11:00
Tobin C. Harding c17db32df3
Pub back in deprecated dust_value
When we renamed `dust_value` to `minimal_non_dust` we forgot to keep the
original and deprecated it, doing so assists with the upgrade path.

Pub back in deprecated `dust_value`, linking to the rename.
2024-03-23 05:32:15 +11:00
Andrew Poelstra 04715e3e60
absolute: make is_* methods uniform with the ones from relative 2024-03-21 22:10:17 +00:00
Andrew Poelstra 878b865f85
relative locktime: introduce is_* methods to check units
Copy these from absolute::LockTime. While we are at it, make the
functions in absolute::LockTime const.
2024-03-21 22:10:17 +00:00
Andrew Poelstra c2f87c7ab3
relative locktime: add is_implied_by method for sequences
This gives a way to determine whether a CSV will pass, given a sequence
number, in a type-safe way where you can't get the two things backward.
2024-03-21 22:10:17 +00:00
Andrew Poelstra 319e102fed
relative locktime: use From/TryFrom to convert between relative locktimes and Sequence 2024-03-21 22:10:17 +00:00
Andrew Poelstra 0ed26915f6
relative locktime: add conversions to/from sequence 2024-03-21 22:10:17 +00:00
Andrew Poelstra 5c8fb5c11b
relative locktime: add consensus encode/decode functions 2024-03-21 22:10:13 +00:00
Andrew Poelstra ac968e02b6
relative locktime: constify a bunch of constructors 2024-03-21 22:07:29 +00:00
Andrew Poelstra f27e675e1e
relative locktime: add "obvious" constructors
Adds constructors to allow directly creating locktimes from time or
block counts; adds a flooring constructor to Time to match the ceiling
one; adds an explicit constructor to Height since the From<u16> was not
very discoverable.
2024-03-21 22:07:25 +00:00
Andrew Poelstra f02b1dac5b
relative locktime: copy comments and PartialOrd impl from absolute locktimes 2024-03-21 22:07:25 +00:00
Andrew Poelstra 2ff5085e70
locktimes: run cargo fmt 2024-03-21 22:07:25 +00:00
Tobin C. Harding dec05b63e9
Refactor witness_version and is_witness_program
These two functions are related. We cannot, by definition, get the
witness version from a script that is not a witness program but
currently the code is not linking these two things.

Refactor by doing:

- Move the check of the witness program bip rules to `witness_version`
- Call `witness_version().is_some()` in the predicate

Improve the docs while we are at it to include the bip text in the
rustdoc. Note I didn't bother referencing the segwit bip number, this
bip text is pretty well known.
2024-03-22 07:07:07 +11:00
Tobin C. Harding dac552b436
Add unit tests for shortest/longest witness program
Add two unit tests that verify we can correctly determine if a
shortest allowed and longest allowed script is a witness program.

Done in preparation for patching the `witness_version` function.
2024-03-22 07:03:30 +11:00
Tobin C. Harding 0ca5a43ce5
hashes: Bump version to v0.14.0
In preparation for release add a changlelog entry and bump the version.

I'm not 100% sure that this release is API breaking, dependencies
definitely changed. The rest might be only additives but I didn't bother
looking exactly because I think its better to bump the minor version and
err on the side of caution.

Note the hashes 0.13.0 dependency stays in the dependency graph because
of secp, we can update secp after releasing `hashes` then update the
secp dependency in `rust-bitcoin` thereby removing the `hashes v0.13.0`
dependency - phew.
2024-03-22 06:04:40 +11:00
Tobin C. Harding 4f68e79da0
bitcoin: Stop using base58 errors
We are currently using the `base58::Error` type to create errors in
`bitcoin`, these are bitcoin errors not `base58` errors.

Note that we add what looks like duplicate
`InvalidBase58PayloadLengthError` types but they are different because
of the expected length. This could have been a field but I elected not
to do so for two reasons:

1. We will need to do so anyways if we crate smash more
2. The `crypto::key` one can have one of two values 33 or 34.

With this applied we can remove the now unused error variants from
`base58::Error`.
2024-03-21 06:22:51 +11:00
Andrew Poelstra 6ff850539a
Merge rust-bitcoin/rust-bitcoin#2503: base58: Re-name crate to base58ck
6b09857f55 base58: Re-name crate to base58ck (Tobin C. Harding)

Pull request description:

  The current name `base58check` is taken, as is `base58`. Use `base58ck` instead.

  Add a brief section to the readme about the crate naming.

ACKs for top commit:
  apoelstra:
    ACK 6b09857f55
  sanket1729:
    ACK 6b09857f55

Tree-SHA512: 86ee08105906a6f3403dc2602e827b0d46226798ecdedb420ad3ac4b657d6a00e25eabcdfbdb9f8e89bdc3a38e608189f1e073e65593f89a2ad853e8ff027f69
2024-03-20 18:56:22 +00:00
Andrew Poelstra 4d90e0b2c4
Merge rust-bitcoin/rust-bitcoin#2528: hash_types: add unit tests for display of all hash types in the library
b816c0bb01 hash_types: add unit tests for display of all hash types in the library (Andrew Poelstra)

Pull request description:

  This can be checked against the 0.29.x branch, and against the commit prior to #1659 (40c246743b^) and you will see that it is consistent EXCEPT:

  * In rust-bitcoin 0.29.x we did not have multiple sighash types, only `Sighash`; we now have `LegacySighash`, `SegwitV0Sighash`, and `TapSighash`.
  * In #1565 we deliberately changed the display direction of the sighashes, to match BIP 143.

  Fixes #2495.

ACKs for top commit:
  tcharding:
    That's a win. ACK b816c0bb01

Tree-SHA512: 5b44f40165699910ea9ba945657cd1f960cf00a0b4dfa44c513feb3b74cda33ed80d3551042c15b85d6e57c30a54242db202eefd9ec8c8b6e1498b5578e52800
2024-03-20 16:33:58 +00:00
Andrew Poelstra 59a58fdca8
Merge rust-bitcoin/rust-bitcoin#2518: Re-export `address::error::P2shError` from `address` module
0d517dcfdd Re-export P2shError (Tobin C. Harding)
646ee1a837 Put re-exports in alphabetic order (Tobin C. Harding)

Pull request description:

  As with the rest of the errors in `address::error` that are returned by a pubic function from the `address` module.

  Note please, this PR just makes the `address/mod.rs` file uniform, debating the merit of the re-exports is out of scope.

ACKs for top commit:
  apoelstra:
    ACK 0d517dcfdd

Tree-SHA512: a65ebe6de62b83c6d3723c7c97a0953ddb8da51964ef54e69865855502ae5fa51b2b49c6fd408c452414b56518f4c7ab763faca925e8d81f0fe806af1df92aa2
2024-03-20 13:43:52 +00:00