Commit Graph

873 Commits

Author SHA1 Message Date
Matt Corallo 9e1cd372cb Use `io::Error::get_ref()` over `std::error::Error::source()` 2023-11-07 17:35:18 +00:00
Matt Corallo 3caaadf9bb [IO] Replace the `io::Cursor` re-export with our own `Cursor` 2023-11-07 17:35:17 +00:00
Matt Corallo 141343edb4 [IO] Move to custom `Read` trait mirroring `std::io::Read`
In order to move towards our own I/O traits in the `rust-bitcoin`
ecosystem, we have to slowly replace our use of the `std` and
`core2` traits.

Here we take the second big step, replacing
`{std,core2}::io::Read` with our own `bitcoin_io::io::Read`. We
provide a blanket impl for our trait for all `std::io::Read`, if
the `std` feature is enabled, allowing users who use their own
streams or `std` streams to call `rust-bitcoin` methods directly.
2023-11-07 05:51:39 +00:00
Matt Corallo 7395093f94 Stop relying on `Take`'s `by_ref` method 2023-11-07 05:51:37 +00:00
Matt Corallo 2364e1a877 Stop relying on blanket Read impl for all &mut Read 2023-11-07 05:51:36 +00:00
Matt Corallo 6aa7ccf841 [IO] Replace `std::io::Sink` usage with our own trivial impl 2023-11-07 05:51:36 +00:00
Matt Corallo 5f2395ce56 Add missing `?Sized` bounds to `io::Write` parameters
Since we are no longer relying on the blanket `io::Write` impl for
`&mut io::Write`, we should now ensure that we do not require
`Sized` for our `io::Write` bounds, as its unnecessarily
restrictive and can no longer be worked around by simply adding an
`&mut`.
2023-11-07 05:50:51 +00:00
Matt Corallo 2348449d2a Stop relying on `std::io::Write`'s `&mut Write` blanket impl
`std::io::Write` is implemented for all `&mut std::io::Write`. This
makes it easy to have APIs that mix and match owned `Write`s with
mutable references to `Write`s.

However, in the next commit we add our own `Write` trait which we
intend to implement for all `std::io::Write`. Sadly, this is
mutually exclusive with a blanket implementation on our own
`&mut Write`, as that would conflict with an `std::io::Write`
blanket impl.

Thus, in order to use the `Write for all &mut Write` blanket impl
in rust-bitcoin, we'd have to bound all `Write`s by
`std::io::Write`, as we're unable to provide a blanket
`Write for &mut Write` impl.

Here we stop relying on that blanket impl in order to introduce the
new trait in the next commit.
2023-11-07 05:50:47 +00:00
Matt Corallo 5e0209569c Use `io::sink` rather than our custom `EmptyWrite` utility 2023-11-07 05:50:45 +00:00
Matt Corallo 27c7c4e26a Add a `bitcoin_io` crate
In order to support standard (de)serialization of structs, the
`rust-bitcoin` ecosystem uses the standard `std::io::{Read,Write}`
traits. This works great for environments with `std`, however sadly
the `std::io` module has not yet been added to the `core` crate.

Thus, in `no-std`, the `rust-bitcoin` ecosystem has historically
used the `core2` crate to provide copies of the `std::io` module
without any major dependencies. Sadly, its one dependency,
`memchr`, recently broke our MSRV.

Worse, because we didn't want to take on any excess dependencies
for `std` builds, `rust-bitcoin` has had to have
mutually-exclusive `std` and `no-std` builds. This breaks general
assumptions about how features work in Rust, causing substantial
pain for applications far downstream of `rust-bitcoin` crates.

Here, we add a new `bitcoin_io` crate, making it an unconditional
dependency and using its `io` module in the in-repository crates
in place of `std::io` and `core2::io`. As it is not substantial
additional code, the `hashes` io implementations are no longer
feature-gated.

This doesn't actually accomplish anything on its own, only adding
the new crate which still depends on `core2`.
2023-11-07 05:50:42 +00:00
Tobin C. Harding 2c33744baa
Remove code deprecated since v0.31.0
We only deprecate for a single release.

Remove all code deprecated since `v0.31.0`.
2023-11-04 12:14:54 +11:00
Andrew Poelstra 071208ccdd
Merge rust-bitcoin/rust-bitcoin#2042: Split Prevouts errors out into specific error types
e21ee381bc Split Prevouts errors out into specific error types (Tobin C. Harding)

Pull request description:

  Done as part of the great error clean up.

  Currently we are returning a general `Error` from `Prevouts` functions, this is un-informative, we can do better by returning specific types that indicate the exact error path.

ACKs for top commit:
  Kixunil:
    ACK e21ee381bc
  apoelstra:
    ACK e21ee381bc

Tree-SHA512: 2a4900f9e31584ad2b6faafa17ea98742fff9206ee1bf77ed29624e0c7b05e655b3b6bf3710e2da26b0b2b8bd5eb36fdd81decbb1f55b41f153f0fbcc4a9165e
2023-11-01 14:20:24 +00:00
Andrew Poelstra df28e2f679
Merge rust-bitcoin/rust-bitcoin#2151: Do trivial docs fixes
d6298fe711 Use capital B for Bitcoin in rustdoc (Tobin C. Harding)
bcfabc3556 Fix typo, missing word (Tobin C. Harding)

Pull request description:

  In an effort to make review merge quicker push these two changes up as a separate PR.

  Totally trivial.

ACKs for top commit:
  Kixunil:
    ACK d6298fe711
  apoelstra:
    ACK d6298fe711

Tree-SHA512: 34635ecd87c918f106694a81f50e69dda233000ac616557744466eb58422a2742f35cadbb059f0f81449efd2f61a37ceb71840bf216009914ba2162834e13fc6
2023-10-31 14:30:58 +00:00
Andrew Poelstra 0b1fe094e4
Merge rust-bitcoin/rust-bitcoin#2146: Create uniform build script
fde6479c6a Create uniform build script (yancy)

Pull request description:

  Previously, each unique compiler cfg attribute that appeared in the codebase was hard coded and emitted to stdout at compile time.  This meant keeping the file up to date as different compiler cfg attributes changed.  It's inconsequential to emit a compiler version that's not used, so this change just emits all possibilities to reduce the maintenance burden of the build script.

  Note that there is a bit of diff noise in one of the `build.rs` since I simply did a copy of one to the other to make them uniform.

ACKs for top commit:
  Kixunil:
    ACK fde6479c6a
  tcharding:
    ACK fde6479c6a
  apoelstra:
    ACK fde6479c6a

Tree-SHA512: 401134c168a604a092b72c3980fae6d20adda761273ea47a887cf4c01838536241a45f310cbc2b5941311d533e1d10c848062198af70c0ed619a57973e842840
2023-10-31 14:04:19 +00:00
Tobin C. Harding e21ee381bc
Split Prevouts errors out into specific error types
Done as part of the great error clean up.

Currently we are returning a general `Error` from `Prevouts` functions,
this is un-informative, we can do better by returning specific types
that indicate the exact error path.
2023-10-31 14:21:24 +11:00
Tobin C. Harding d6298fe711
Use capital B for Bitcoin in rustdoc
As we do in another part of this file use capital 'B' for Bitcoin.
2023-10-31 08:37:11 +11:00
Tobin C. Harding bcfabc3556
Fix typo, missing word
Add a 'by' to fix the grammar in sentence.
2023-10-31 08:36:09 +11:00
yancy fde6479c6a Create uniform build script
Previously, each unique compiler cfg attribute that appeared in the
codebase was hard coded and emitted to stdout at compile time. This
meant keeping the file up to date as different compiler cfg attributes
changed. It's inconsequential to emit a compiler version that's not
used, so this change just emits all possibilities to reduce the
maintenance burden of the build script.
2023-10-30 14:40:41 +01:00
Tobin C. Harding efedf862b0
bitcoin: Bump version number to v0.31.0
In preparation for release of v0.31.0 bump the version number.

The changelog is already up to date because we have done two RC
releases.
2023-10-28 08:32:30 +11:00
Andrew Poelstra 7246585588
Merge rust-bitcoin/rust-bitcoin#2096: Move psbt macro to the psbt test module
2ecab31f94 Remove stale comment and map_err (yancy)
b166442fb0 Replace hex_psbt macro with test helper function (yancy)
9e4a784b8b Move psbt macro to the psbt test module (yancy)

Pull request description:

  Remove `#[cfg(test)]` and the macro `psbt_with_values` from macros.rs and place it in the tests module for psbt.

ACKs for top commit:
  apoelstra:
    ACK 2ecab31f94
  tcharding:
    ACK 2ecab31f94

Tree-SHA512: 06a55056e864befac8b33968bf4e469c3c7bc20e651ad5bb3b80aa76749169af1266e1d4101d3e9e9bbffe7c860e8b9fcd675a78ca7ae67dc09892c75fba0dd0
2023-10-27 13:17:07 +00:00
lateminer 2a891e0be8
Add a missing link to #2006 in 'bitcoin/CHANGELOG.md' 2023-10-26 21:06:48 +02:00
Andrew Poelstra 1eb6e0c7d8
Merge rust-bitcoin/rust-bitcoin#2134: Add clippy exceptions for needless_question_mark lint
875545517d Add clippy exceptions for needless_question_mark lint (Steven Roose)

Pull request description:

  This lint forces you to write semantically different code that is in most cases inferior, just to save you 5 characters.

  The reason why the code is inferior is because it doesn't do error conversion so it would break when either of the two function signatures changes while in the original code using the `?` operator, nothing would break if the inner error can be converted into the outer error.

ACKs for top commit:
  apoelstra:
    ACK 875545517d
  tcharding:
    ACK 875545517d

Tree-SHA512: 8429e0fb7d759a3d19231e7bcaed61b0988172d931e758a9522d7c994854fd403408bb93b06778a5c09746cd38b6a96d3d2e0a862fb4516f2dbfffffe8735ce0
2023-10-24 01:17:32 +00:00
Steven Roose 875545517d
Add clippy exceptions for needless_question_mark lint 2023-10-23 16:45:52 +01:00
Steven Roose 750ee2ba56
Remove unnecessary clippy attribute on is_sighash_single_bug 2023-10-23 01:41:44 +01:00
Steven Roose f522a0290c
Remove unnecessary clippy attribute on relative::LockTime 2023-10-23 01:37:50 +01:00
Steven Roose b7f11d4493
Remove unnecessary clippy attribute on absolute::LockTime
I ran the clippy locally without it and it doesn't seem to be necessary
anymore.
2023-10-23 01:36:19 +01:00
Andrew Poelstra e436b5b900
Merge rust-bitcoin/rust-bitcoin#2132: Replace helper macro with helper function
b163d9b59a Replace hex_script macro with a helper function (yancy)
7f26439e20 Add track_caller to test helper functions (yancy)
bf08ee4499 Replace helper macro with helper function (yancy)

Pull request description:

  Remove the macro hex_psbt and replace with a function.  Using track_caller to accurately report the test name and line number during a panic is used in place of a macro.

  This is a refactor commit to the test suit.

ACKs for top commit:
  apoelstra:
    ACK b163d9b59a
  tcharding:
    ACK b163d9b59a

Tree-SHA512: 6955c966d1c37020515ae990e5e0ec154f591ce025321dee71264e4e53cbab38afdb681ca193e626efdb7be4b4e84763cd8baf41b2a1258d34c38acd58713254
2023-10-22 13:16:42 +00:00
yancy b163d9b59a Replace hex_script macro with a helper function
Remove the macro hex_script and replace with a function.  Using
track_caller to accuretly report the test name and line number
during a panic is used in place of a macro.
2023-10-20 10:20:23 +02:00
yancy 7f26439e20 Add track_caller to test helper functions
The test helper files can panic when calling hex_psbt().  hex_psbt()
will report the calling function, instead of the offending test.  Adding
track_caller to functions that call hex_psbt will report the line number
of the failing test.
2023-10-20 09:41:50 +02:00
yancy bf08ee4499 Replace helper macro with helper function
Remove the macro hex_psbt and replace with a function.  Using
track_caller to accuretly report the test name and line number
during a panic is used in place of a macro.
2023-10-20 09:25:00 +02:00
Andrew Poelstra fe07c13bcc
Merge rust-bitcoin/rust-bitcoin#2131: encode: Implement encoding traits on Vec<BlockHeader>
10c1f7b9c6 encode: Implement encoding traits on Vec<BlockHeader> (Steven Roose)

Pull request description:

ACKs for top commit:
  clarkmoody:
    ACK 10c1f7b9c6
  apoelstra:
    ACK 10c1f7b9c6
  vincenzopalazzo:
    ACK 10c1f7b9c6

Tree-SHA512: 32182f4b18a2e4aee20efc4c649827be4e2f61991fae48d7e1360e2c8224bcd98ef89391b50ec2d4e12a720daf7350ca0beb23094153313b668a54cee9cda278
2023-10-19 15:38:54 +00:00
Andrew Poelstra 3a95365f46
Merge rust-bitcoin/rust-bitcoin#2124: Tracking PR for `v0.31.0-rc2` release
38960ab5a5 Bump version to 0.31.0-rc2 (Tobin C. Harding)
79dfe8d270 justfile: Add update-lock-files command (Tobin C. Harding)
178069c13e Add changelog entries for v0.31.0 (Tobin C. Harding)

Pull request description:

  Prepare for the v0.31.0-rc2 release.

  - Add changlog entries
  - Add a `just update-lock-files` command
  - Bump the version ready for next RC release

ACKs for top commit:
  apoelstra:
    ACK 38960ab5a5
  clarkmoody:
    ACK 38960ab5a5

Tree-SHA512: d2d459666117992689ea2bde2ba4a5d9a9cd36a85c5f0df443ceca3bf9d4ec351baffb4be09fbe25bf0767145d11a59b97031e7c8f9182fa9348a6883420b8fe
2023-10-19 13:32:03 +00:00
Steven Roose 10c1f7b9c6
encode: Implement encoding traits on Vec<BlockHeader> 2023-10-18 23:34:56 +01:00
Tobin C. Harding 38960ab5a5
Bump version to 0.31.0-rc2
In preparation for doing the next RC release bump the version.
2023-10-18 13:52:49 +11:00
Tobin C. Harding 178069c13e
Add changelog entries for v0.31.0
Add the remaining changelog entries for the v0.31.0 release. Includes
everything released in v0.31.0-rc1
2023-10-18 13:46:42 +11:00
Andrew Poelstra 37daf4620c
Merge rust-bitcoin/rust-bitcoin#2126: Implement manual fmt::Debug for BlockHeader to include block hash
b108ffa2ec Implement manual fmt::Debug for BlockHeader to include block hash (Steven Roose)

Pull request description:

  I think it makes sense for a block header to have the block hash shown in the debug print.

ACKs for top commit:
  tcharding:
    ACK b108ffa2ec
  vincenzopalazzo:
    ACK b108ffa2ec
  apoelstra:
    ACK b108ffa2ec

Tree-SHA512: 37b77f8b2d8da3903bc5ff329abd45dcb5f189ba2ac38fc309fa477b0eb569b01dd85c41f160e073bb56b4a599b7c4774fbfbd8da4b82ddb41540d60bfeafea3
2023-10-17 23:41:07 +00:00
Steven Roose b108ffa2ec
Implement manual fmt::Debug for BlockHeader to include block hash 2023-10-17 02:00:42 +01:00
yancy 2ecab31f94 Remove stale comment and map_err
The current MSRV is greater than rustc 0.29.
2023-10-16 14:48:14 +02:00
yancy b166442fb0 Replace hex_psbt macro with test helper function
hex_psbt was added as a macro so that a panic would reveal the line
number of the failing test by expanding the macro at the test location.
However, a stack trace can be used to reveal  the test that caused the
failure using RUST_BACKTRACE=1.  Furthermore, the track_caller macro is
added to the helper methods which will reveal the line number of the
calling function (the offending test). More detailed information for
debugging has been added to hex_psbt() so that the offending string
will be included in the panic message.
2023-10-16 14:48:00 +02:00
yancy 9e4a784b8b Move psbt macro to the psbt test module
The macro psbt_with_values is used by the psbt test module.  Since
there is no pre-processing required, there is no reason to use
metaprogramming here, so this commit moves the logic from a
macro to common function in the test module.
2023-10-16 11:31:01 +02:00
Tobin C. Harding 6731e85dc1
bitcoin: Bump version to 0.31.0-rc1
In preparation for the first rc release; bump the version number.
2023-10-13 08:34:46 +11:00
Andrew Poelstra e0ddead255
Merge rust-bitcoin/rust-bitcoin#2107: Use `Target` as type for `pow_limit`
38005f6aa7 Use Target for pow_limit (Tobin C. Harding)

Pull request description:

  The `Params::pow_limit` field is currently a `Work` type, this is incorrect. The proof of work limit is the highest _target_ not the lowest work (even though these have a relationship).

  Note that we use the highest _attainable_ target, this differs from Bitcoin Core and the reasoning is already documented in the code.

  Add new consts and document where they came from as well as how they differ to Core.

  Use the new consts in the various network specific `Params` types.

  Fix: #2106

ACKs for top commit:
  junderw:
    ACK 38005f6aa7
  apoelstra:
    ACK 38005f6aa7

Tree-SHA512: 5e71f69cdd555fd95a52fc1479e27b5e11226772f6432758c1364107a068bd1271486da6db1ece694da0287ce10cfbc18d28d6e3dbb0b9d387ff968eea43ab18
2023-10-12 15:05:51 +00:00
Tobin C. Harding 38005f6aa7
Use Target for pow_limit
The `Params::pow_limit` field is currently a `Work` type, this is
incorrect. The proof of work limit is the highest _target_ not the
lowest work (even though these have a relationship).

Note that we use the highest _attainable_ target, this differs from
Bitcoin Core and the reasoning is already documented in the code.

Add new consts and document where they came from as well as how they
differ to Core.

Use the new consts in the various network specific `Params` types.
2023-10-11 08:50:42 +11:00
Tobin C. Harding 678eee8d73
Use Message::from_digest
We have a new API function available with recent version of `secp256k1`
to create a `Message` directly from a sighash byte array.

Use `Message::from_digest(sighash.to_byte_array())` to construct
messages ready to sign.
2023-10-11 08:16:25 +11:00
Tobin C. Harding 6f30ac9d02
Upgrade the secp dependency
Upgrade the `secp256k1` dependency to the newly released `v0.28.0`.

FTR this includes two simple changes:
- Use `Message::from_digest_slice` instead of `Message::from_slice`.
- Use `secp256k1::Keypair` instead of `secp256k1::KeyPair`.
2023-10-10 10:04:31 +11:00
Tobin C. Harding 818a3c5424
Use Keypair instead of KeyPair in identifiers
In preparation for updating the secp dependency to v0.28.0, which
includes a change of `KeyPair` to `Keypair`, change our identifier usage
to indicate that "keypair" is a single word.

Deprecate the old forms.
2023-10-10 10:02:09 +11:00
Tobin C. Harding 33ee49f8e5
Update bech32 dependency
Update the `bech32` dependency to use the newly release beta version.

The main fix here is silent, a bug fix in `bech32` that was being hit by
our fuzzing suite.
2023-10-10 06:47:58 +11:00
Andrew Poelstra 342a35b4c4
Merge rust-bitcoin/rust-bitcoin#2109: ci: bump rustc to 1.60 for fuzz test
6b5d06f23e ci: fix the byteorder to 1.4.3 for edition 2018 (Vincenzo Palazzo)
98513ef151 clippy: more worning fixes (Vincenzo Palazzo)
05d3dc5d72 Remove redundant guard (Tobin C. Harding)
4537634e7e ci: bump rustc to 1.60 for fuzz test (Vincenzo Palazzo)

Pull request description:

  Ci looks like broken, so this should fix
  it

ACKs for top commit:
  apoelstra:
    ACK 6b5d06f23e

Tree-SHA512: bfa0eaf8cbc02a671237d99221db8c21264ce9df91301818c95c41dcc5ad4935e91254b0b3fa8f36738a9d71b6541fb8784ac8280d67057960a3d20e385a9f17
2023-10-07 16:40:36 +00:00
Vincenzo Palazzo 98513ef151
clippy: more worning fixes
error[E0308]: mismatched types
  --> bitcoin/src/psbt/raw.rs:87:24
   |
87 |               return Err(encode::Error::OversizedVectorAllocation {
   |  ________________________^
88 | |                 requested: key_byte_size as usize,
89 | |                 max: MAX_VEC_SIZE,
90 | |             });
   | |_____________^ expected enum `psbt::error::Error`, found enum `consensus::encode::Error`
   |
help: try wrapping the expression in `psbt::error::Error::ConsensusEncoding`
   |
87 ~             return Err(psbt::error::Error::ConsensusEncoding(encode::Error::OversizedVectorAllocation {
88 |                 requested: key_byte_size as usize,
89 |                 max: MAX_VEC_SIZE,
90 ~             }));
   |

----

  Compiling bitcoin v0.30.0 (/home/vincent/github/work/rust-btc/rust-bitcoin/bitcoin)
    Checking bitcoin-fuzz v0.0.1 (/home/vincent/github/work/rust-btc/rust-bitcoin/fuzz)
error: redundant clone
   --> bitcoin/examples/taproot-psbt.rs:453:77
    |
453 |             witness_utxo: { Some(TxOut { value, script_pubkey: script_pubkey.clone() }) },
    |                                                                             ^^^^^^^^ help: remove this
    |
    = note: `-D clippy::redundant-clone` implied by `-D warnings`
note: this value is dropped without further use
   --> bitcoin/examples/taproot-psbt.rs:453:64
    |
453 |             witness_utxo: { Some(TxOut { value, script_pubkey: script_pubkey.clone() }) },
    |                                                                ^^^^^^^^^^^^^
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

error: could not compile `bitcoin` due to previous error
warning: build failed, waiting for other jobs to finish...
error: redundant clone
    --> bitcoin/src/psbt/mod.rs:1095:13
     |
1095 |             .clone()
     |             ^^^^^^^^ help: remove this
     |
     = note: `-D clippy::redundant-clone` implied by `-D warnings`
note: this value is dropped without further use
    --> bitcoin/src/psbt/mod.rs:1094:17
     |
1094 |           assert!(psbt
     |  _________________^
1095 | |             .clone()
     | |____________^
     = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2023-10-07 11:40:44 +02:00
Tobin C. Harding 05d3dc5d72 Remove redundant guard
Clippy warns: redundant guard

As suggested, remove the redundant guard.
2023-10-07 11:19:47 +02:00