Commit Graph

357 Commits

Author SHA1 Message Date
Tobin C. Harding f23155aa16
Do not capitalize error messages
As per convention; do not capitalize error messages.
2023-08-01 16:25:24 +10:00
Andrew Poelstra cdf3e30b9d
Merge rust-bitcoin/rust-bitcoin#1964: script: Move some inspector methods from ScriptBuf to Script
07041d584d Apply rustfmt (The rustfmt Tyranny)
dada6d65b7 script: Move some inspector methods from ScriptBuf to Script (Steven Roose)

Pull request description:

  Noticed that these methods belong in Script.

ACKs for top commit:
  tcharding:
    ACK 07041d584d
  sanket1729:
    ACK 07041d584d.
  apoelstra:
    ACK 07041d584d

Tree-SHA512: cdcbdf22f0457123205621ec2834164c4598be1e5b221cf859d60e88110b19f8c1e484e86f60653af237e9c2acbcdbe5d2b4c98ccf239924386639c4ba6222f7
2023-07-31 14:54:25 +00:00
Steven Roose dada6d65b7
script: Move some inspector methods from ScriptBuf to Script 2023-07-27 23:50:20 +01:00
Tobin C. Harding a70b1b9c6c
Use standard set of derives on all error types
As part of an ongoing effort to make our error types stable and useful
add a stand set of derives to all error types in the library.

    `#[derive(Debug, Clone, PartialEq, Eq)]`

Add `Copy` if possible and the error type does not include
`#[non_exhaustive]`.

If an error type includes `io::Error` it only gets `#[derive(Debug)]`.
2023-07-28 06:15:49 +10:00
Andrew Poelstra 04976eddcf
Merge rust-bitcoin/rust-bitcoin#1833: Use new `hex-conservative` crate
bb8bd16302 internals: Remove hex module (Tobin C. Harding)
2268b44911 Depend on hex-conservative (Tobin C. Harding)
db50509cd3 Add usage docs to the "core2" feature (Tobin C. Harding)

Pull request description:

  Use the newly released `hex-conservative` crate, by doing the following:

  - Depend on `hex-conservative` in `bitcoin` and `hashes`
  - Re-export `hex-conservative` as `hex` from both crate roots.
  - Remove all the old hex code from `hashes`
  - Remove all the old hex code from `internals`
  - Remove the now unused `internals::prelude`
  - Fix all the import statements (makes up the bulk of the lines changes in this patch)

ACKs for top commit:
  apoelstra:
    ACK bb8bd16302
  sanket1729:
    utACK bb8bd16302

Tree-SHA512: ec83b3941cae6f32272471779f28461bb04959a3f6a126a68bbf2c748d83ff9518ff8932d9e937a6f389c10028bf3eb58c6b6d71ea066924dd7a34faaec7a087
2023-07-27 16:27:01 +00:00
Tobin C. Harding 50ada8298f
Move EncodeSigningDataResult to sighash module
This type was defined in the `transaction` module because it was
originally used in a function that had been deprecated in favour of
moving the logic to the `sighash` module.

We just removed the deprecated code so we can now move this type to the
`sighash` module where it is used.
2023-07-27 09:21:34 +10:00
Tobin C. Harding 1b7dc51ccb
Remove deprecated code
We only keep deprecated code around for one release so we can now remove
code deprecated in v0.30.0

Done in preparation as we gear up for v0.31.0 release.
2023-07-27 09:20:19 +10:00
Tobin C. Harding 1edc5f4098
Fill in deprecated since NEXT-RELEASE placeholder
As we gear up for the v0.31.0 release we can fill in the NEXT-RELEASE
placeholders.
2023-07-27 07:26:21 +10:00
Steven Roose 56343bd7b5
Merge pull request #1912 from tcharding/06-20-consensus-validation
Separate out bitcoinconsensus validation code
2023-07-25 11:16:53 -05:00
Tobin C. Harding 2268b44911
Depend on hex-conservative
We have just released the `hex-conservative` crate, we can now use it.

Do the following:

- Depend on `hex-conservative` in `bitcoin` and `hashes`
- Re-export `hex-conservative` as `hex` from both crate roots.
- Remove all the old hex code from `hashes`
- Fix all the import statements (makes up the bulk of the lines changed
  in this patch)
2023-07-21 10:59:46 +10:00
Tobin C. Harding f3412325ea
weight: Make docs uniform and terse
Make the docs use the same form as everywhere else, which also mimics
stdlib docs on integer types.
2023-07-20 09:32:23 +10:00
Andrew Poelstra 894c926f56
Merge rust-bitcoin/rust-bitcoin#1935: Infrastructure fixes
7b402e930c schemars: Add pinning docs (Tobin C. Harding)
0848ab7e25 Fix clippy warnings for embedded build (Tobin C. Harding)
5b1443a91c hashes/embedded: Add script dir and README (Tobin C. Harding)
94732aecbf Add patch section to test crates (Tobin C. Harding)
512d982275 Remove path field from internals dependency (Tobin C. Harding)

Pull request description:

  Do a bunch of infrastructure fixes that either are needed for adding additional crate deps (hex) or updating deps (internals, hashes), or just make the tests more maintainable.

ACKs for top commit:
  apoelstra:
    ACK 7b402e930c
  sanket1729:
    ACK 7b402e930c

Tree-SHA512: 9349bb20225363914acc774cca672a23e6562fb02aea644777c558074d5eeb65289d68a93b5be59a93e9b32167e2494f6599caedc8a0d9cfbee2f94d406edbfc
2023-07-19 16:11:40 +00:00
Riccardo Casatta 28f6ad80cb
Merge rust-bitcoin/rust-bitcoin#1942: witness: clean up Debug implementation
e30c492faf witness: clean up Debug implementation (Andrew Poelstra)

Pull request description:

  The previous code seems to have been rebased/iterated on too many times, and had room for significant simplification. By inlining the indentation logic we can eliminate 40 LOC and also clean up the output by removing trailing spaces.

  Fixes #1937

  It is not good form to add unit tests for debug output but you can test this locally with the patch
  ```
  diff --git a/bitcoin/src/blockdata/witness.rs b/bitcoin/src/blockdata/witness.rs
  index d0b7408c..a2c38af0 100644
  --- a/bitcoin/src/blockdata/witness.rs
  +++ b/bitcoin/src/blockdata/witness.rs
  @@ -619,6 +619,9 @@ mod test {
               "304402207c800d698f4b0298c5aac830b822f011bb02df41eb114ade9a6702f364d5e39c0220366900d2a60cab903e77ef7dd415d46509b1f78ac78906e3296f495aa1b1b54101")
               ];
           assert_eq!(witness.to_vec(), expected_witness);
  +
  +            println!("{:?}", witness);
  +            panic!();
       }

       #[test]
  ```
  And by sticking `{:#?}` in there to see the alternate output.

ACKs for top commit:
  tcharding:
    tACK e30c492faf
  RCasatta:
    ACK e30c492faf

Tree-SHA512: 0ec07885f5c75f3f34965852cf5b42b63290295d1f56e9fef7d5b3610b8ac8d318cbf8f184da5b8a9ed5b352bb2c0402797b41714cb9d5488e93c2e290340c2a
2023-07-18 20:18:04 +02:00
Andrew Poelstra 9a34f0cf5e
Merge rust-bitcoin/rust-bitcoin#1925: Rename `Script::empty` to `Script::new`
9787ba6c96 Rename Script::empty to Script::new (Tobin C. Harding)

Pull request description:

  The `empty` constructor is mis-named for the following reasons:

  - Non-uniform with `ScriptBuf::new`
  - Non-standard with respect to stdlib which uses `Path::new` and `PathBuf::new` (on which we based the `Scritp`/`ScriptBuf`)

  Rename the function to `new`, put it at the top of the impl block while we are at it.

ACKs for top commit:
  apoelstra:
    ACK 9787ba6c96
  RCasatta:
    ACK 9787ba6c96

Tree-SHA512: 2dee0f61fa9097a48369a0df802ebf238b00ad3e9ed520fbf31affa1cb2a1820cbb910b525be63513e4586acb2aa0b593cecddcad0b6cd894cdac0ba7fcf0871
2023-07-18 16:49:00 +00:00
Tobin C. Harding dae2b508bc
Separate out bitcoinconsensus validation code
Pull all the code that depends on `bitcoinconsensus` out into a separate
module `consensus::validation`.

Leave transaction testing of bitcoinconsensus code in the transaction
module.
2023-07-18 10:29:51 +10:00
Tobin C. Harding 0cd1af1f56
Remove unused import statement
This import is not used, our CI obviously does not warn for all feature
combinations.
2023-07-18 10:29:50 +10:00
Tobin C. Harding ca38dbd16d
transaction:: Return custom error from verify function
There is not need to return the general `script::Error` from the
transaction verify functions. We can better describe the error path by
returning a custom error.
2023-07-18 10:29:50 +10:00
Tobin C. Harding ca2512f471
script: Return bitcoinconsensus::Error from verify functions
There is no need no nest the `bitcoinconsensus::Error` type within the
`script::Error`, it is the only error type returned by the verify
functions so just return it directly.
2023-07-18 10:29:50 +10:00
Tobin C. Harding 0848ab7e25
Fix clippy warnings for embedded build
When building the `embedded` test crate `clippy` emits two warnings
of form:

  warning: unused variable: `foo`

Use `_` instead of a named variable.
2023-07-18 10:27:48 +10:00
Andrew Poelstra e30c492faf
witness: clean up Debug implementation
The previous code seems to have been rebased/iterated on too many times,
and had room for significant simplification. By inlining the indentation
logic we can eliminate 40 LOC and also clean up the output by removing
trailing spaces.
2023-07-14 20:37:11 +00:00
Tobin C. Harding 81a42536f9
Use hex_lit::hex in benches
Currently the test `hex` macro is only available when the `test`
compiler configuration option is set but we are using it in benches
code, this works for use because `cargo bench` sets `test` for the
current crate, however it breaks downstream crates.

Fix: #1830
2023-07-14 15:30:22 +10:00
Andrew Poelstra a7fe0f5695
Merge rust-bitcoin/rust-bitcoin#1913: Manually implement Debug on Witness
d45dbef3e7 Manually implement Debug on Witness (Tobin C. Harding)

Pull request description:

  The current derived debug implementation on `Witness` prints the content field as an array of integers. We can do better than this by manually implementing `Debug`.

  With this applied `Witness` is printed as follows: (first line is `{:?}` and the next is `{:#?}`):

  Using `{:?}`:
  ```
  Witness: { indices: 3, indices_start: 8, witnesses: [[0x00], [0x02, 0x03], [0x04, 0x05]] }
  ```

  Using `{:#?}`:
  ```
  Witness: {
      indices: 3,
      indices_start: 8,
      witnesses: [
          [0x00],
          [0x02, 0x03],
          [0x04, 0x05],
       ],
  }
  ```

ACKs for top commit:
  sanket1729:
    tested ACK d45dbef3e7. This would be helpful for debugging downstream.
  apoelstra:
    ACK d45dbef3e7

Tree-SHA512: eacf4fa8e3f38c4e9ddc45de78afb8eab5b5b196b77a6612f61860e0e4e7ba96de2e7f434b92816e0b00532e73c05378cafc046ec9c34108e9d9216fb36c524a
2023-07-13 23:08:45 +00:00
Tobin C. Harding 552f19abe3
Add more rustdocs to WitnessProgram
Add rustdocs to `WitnessProgram` commenting on why we carry the witness
version number around with the witness program. This is mainly a dev
comment but it helps document the invariants so make it a rustdoc
comment.
2023-07-13 09:53:33 +10:00
Tobin C. Harding 89303c1464
Move witness types to the script module
From BIP 141:

> A scriptPubKey (or redeemScript as defined in BIP16/P2SH) that
> consists of a 1-byte push opcode (for 0 to 16) followed by a data push
> between 2 and 40 bytes gets a new special meaning. The value of the
> first push is called the "version byte". The following byte vector
> pushed is called the "witness program".

`WitnessVersion` and `WitnessProgram` are scriptPubkey concerns and
scriptPubkey is basically synonymous with address so in one way it makes
sense that these types are in `address` however we are in the process of
overhauling the `Address` (and `AddressInner`) types so lets move the
witness stuff to `script` and put it in individual sub-modules.

This move helps simplify the address error type also.

Note please, there are a bunch of formatting changes in here in the
error type that I cannot explain and could not remove.
2023-07-13 09:51:40 +10:00
Tobin C. Harding d45dbef3e7
Manually implement Debug on Witness
The current derived debug implementation on `Witness` prints the content
field as an array of integers. We can do better than this by manually
implementing `Debug`.

With this applied `Witness` is printed as follows: (first line is `{:?}`
and the next is `{:#?}`):

Using `{:?}`:
```
Witness: { indices: 3, indices_start: 8, witnesses: [[0x00], [0x02, 0x03], [0x04, 0x05]] }
```

Using `{:#?}`:
```
Witness: {
    indices: 3,
    indices_start: 8,
    witnesses: [
        [0x00],
        [0x02, 0x03],
        [0x04, 0x05],
     ],
}
```
2023-07-08 11:28:04 +10:00
Tobin C. Harding 9787ba6c96
Rename Script::empty to Script::new
The `empty` constructor is mis-named for the following reasons:

- Non-uniform with `ScriptBuf::new`
- Non-standard with respect to stdlib which uses `Path::new` and
  `PathBuf::new` (on which we based the `Scritp`/`ScriptBuf`)

Rename the function to `new`, put it at the top of the impl block while
we are at it.
2023-07-08 11:26:30 +10:00
yancy f5591d8dee Use weight instead of checked_weight 2023-06-26 12:44:27 +02:00
yancy 80a4d692c4 Change weight to call predict_weight 2023-06-26 12:35:59 +02:00
Andrew Poelstra 12dc0b013b
Merge rust-bitcoin/rust-bitcoin#1863: Removed only available in 1.46.0 line
6a18997e3c Removed only available in 1.46.0 line (TATHAGATA ROY)

Pull request description:

  Fix: #1850

  Removed "*Important: only available in Rust 1.46+*" on the file  transaction.rs from lines 1288 and 1407 respectively.

ACKs for top commit:
  Kixunil:
    ACK 6a18997e3c
  apoelstra:
    ACK 6a18997e3c
  tcharding:
    ACK 6a18997e3c
  sanket1729:
    ACK 6a18997e3c

Tree-SHA512: 1395384ffe301b628687cc6d154e191b6a4415acd33eb4209065c5bf94115c3210ea1d28f7d7186e41665b39b5bebae849c3fa5394786ce24bdcd57b765cdbd3
2023-06-07 15:22:48 +00:00
junderw d961b9c4ee
Fix minor comments on count_sigops PR 2023-06-04 15:17:36 -07:00
junderw 638445f8a9
Feature: Add opcodes::All::decode_pushnum and Script::count_sigops 2023-06-02 08:46:46 -07:00
Tobin C. Harding 042dcaa4b7
Remove doc(hidden) from error conversion functions
Give people access to the error type conversion docs, its no harm and it
may be useful when the compiler does not give enough information.
2023-05-30 15:54:40 +10:00
Andrew Poelstra 5729b034d8
Merge rust-bitcoin/rust-bitcoin#1874: Add `ValidationError`
0f74eb6876 Remove the unused crate::Error (Tobin C. Harding)
74154c2294 Add block::ValidationError (Tobin C. Harding)
3a9b5526b3 Move BlockHash From impls (Tobin C. Harding)

Pull request description:

  Remove the `crate::Error` and replace its usage with `block::ValidationError`.

ACKs for top commit:
  apoelstra:
    ACK 0f74eb6876
  Kixunil:
    ACK 0f74eb6876

Tree-SHA512: 80b2c98d3d8f7c3f060c8ea2d94e1ebe118c07d0dcf91f6d13aed00df2cb0b15bf5e295ec0976d88d81e029cf7d3e8e4a1fe70120db57e49bbd8dd229291836b
2023-05-28 23:08:32 +00:00
Tobin C. Harding 74154c2294
Add block::ValidationError
Add a `ValidationError` to the `block` module and remove the two
variants out of `crate::Error`.

This error is only used by the `validate_pow` function, a specific error
better serves our purposes.
2023-05-25 13:35:49 +10:00
Tobin C. Harding 3a9b5526b3
Move BlockHash From impls
Move the `From` impls on `BlockHash` to below the `BlockHash` impl
block.

Code move only, no other changes.
2023-05-25 13:32:25 +10:00
Tobin C. Harding 0046bb8ad8
Fix usage of cfg(rust_1_53)
Our custom conditional configuration options enables for various
versions of the compiler are not features.

Fix the incorrect usage.
2023-05-24 22:03:57 +10:00
Tobin C. Harding c3450f3913
Remove stale usage of doc(cfg)
We no longer need to use `doc(cfg())` because we use `doc_cfg_auto`,
remove it.
2023-05-24 22:02:24 +10:00
Andrew Poelstra b5aa482532
Merge rust-bitcoin/rust-bitcoin#1857: tests: Use script hash functions
013dffa65d tests: Use script hash methods (Tobin C. Harding)

Pull request description:

  The `ScriptBuf` type can be serialized using it's `to_bytes` function. Do not use the `psbt::Serialize` trait to do so in test code.

  No logic changes, since the impl of `psbt::Serialize` for `ScriptBuf` just calls `to_bytes`.

ACKs for top commit:
  Kixunil:
    ACK 013dffa65d
  apoelstra:
    ACK 013dffa65d

Tree-SHA512: 08959e065f1528f2ca69c12d5e34bceea3ddd17eefcee45094f071b3fa7357dbf289f6fe54d18fea02eecd1d0a7cd04598bbf014a5f10676387dbe27bb490395
2023-05-22 20:31:42 +00:00
Tobin C. Harding 013dffa65d
tests: Use script hash methods
We have methods to convert a script to a `WScritpHash` and `ScriptHash`,
no need to do this manually, let alone use the `psbt::Serialize` trait
to do so.
2023-05-20 07:31:54 +10:00
yancy b03c24db8c Add a checked version of weight mul fee_rate 2023-05-19 10:17:57 +02:00
TATHAGATA ROY 6a18997e3c Removed only available in 1.46.0 line 2023-05-18 12:14:38 +00:00
Martin Habovstiak 6cec19f6d3 Update documentation of `InputWeightPrediction`
The doc was written before alternative constructors existed, update it.
2023-05-11 20:38:32 +02:00
Martin Habovstiak 6fec1789b9 Fix associated constants of `InputWeightPrediction`
These constants had an error that they had `script_size` set to 0 which
was incorrect because it's not length of the script but serialized size.
Rather than just bumping the value this uses the `from_slice` method
which is less error-prone.

This also deletes a useless test of the constants.

Closes #1834
2023-05-11 20:38:32 +02:00
yancy 75b3f19b96 Move and rename TxOut default trait to a const called NULL 2023-05-08 09:11:06 +02:00
Andrew Poelstra 64540b9b93
Merge rust-bitcoin/rust-bitcoin#1793: Comment predict_weight
dff757d7db Comment predict_weight (yancy)

Pull request description:

  I've been reading over the `predict_weight` function since it is one of the biggest challenges for coin-selection.  IE choosing inputs and constructing an optimal selection strategy requires predicting the weight to get the best selection.  It's great this work has been done but there are some things I don't understand well enough to comment.

  1) why are we looking at the size of VarInt struct here
  > let script_size = script_len + VarInt(script_len as u64).len()

  2) [predict_weight_internal](36500b4451/bitcoin/src/blockdata/transaction.rs (L1245)) has a bunch of magic numbers.  I'd like to be able to comment this as well but I don't fully understand that function.

  Also, `Transaction.rs` is a big file and it seems like all of the prediction stuff could be moved to a separate module or maybe a separate crate?

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

Tree-SHA512: 8ffa16d500075d691528ce1819b9352a148af431889bebbd7cddcf470bd4e3048ec53a5e778bc3659e33d8c25b68422a93dac1d46b9489ff56f41d88d7f05433
2023-05-05 02:55:23 +00:00
yancy d57ec019d5 Use Amount type for TxOut value field 2023-05-04 17:09:08 +02:00
Tobin C. Harding 6cab7beba3
Deprecate min/max_value methods
Our previous MSRV did not support MIN/MAX associated consts so we had
methods min/max_value. Now that our MSRV is Rust 1.48.0 we can use the
consts.

Deprecate min/max_value methods in favor of MIN/MAX associated conts.
2023-05-03 08:26:58 +10:00
Tobin C. Harding 5fbbd483ea
Use MIN/MAX consts instead of min/max_value
We currently use the functions `min_value` and `max_value` because the
consts were not available in Rust 1.41.1, however we recently bumped the
MSRV so we can use the consts now.
2023-05-03 08:22:30 +10:00
Andrew Poelstra 936f2ee3bb
Merge rust-bitcoin/rust-bitcoin#1825: Simplify per-file license comments
1c3bbd4bf2 internals: Remove attribution from all files (Tobin C. Harding)
99673ab5c4 hashes: Introduce SPDX license identifiers (Tobin C. Harding)
984fe69448 bitcoin: Remove attribution from all files (Tobin C. Harding)

Pull request description:

  Please note, whether or not we need a per-file license comment is out of scope for this PR. This PR leaves us with the most simple per-file solution possible and leaves the merit of per-file license comment to be discussed on another day.

  Simplify the per-file license stuff by doing:

  - Remove the attribution line from each file.

      Currently we have a mishmash of attribution lines accompanying the SPDX identifier. These lines are basically meaningless because:

      - The date is often wrong
      - The original author attributed is not the only contributor to a file
      - The term "rust bitcoin developers" is basically just noise

  - Introduce SPDX license identifiers into `hashes` and remove attribution line (ie, make `hashes` uniform with `bitcoin`)

  Required before merge please:

  - [x] ack from apoelstra because as the library original author many of the changes in this PR remove his name
  - [x] ack from Kixunil because he had some concerns in the issue descussion

  Fix: #1816

ACKs for top commit:
  Kixunil:
    ACK 1c3bbd4bf2
  sanket1729:
    ACK 1c3bbd4bf2
  apoelstra:
    ACK 1c3bbd4bf2

Tree-SHA512: c5ac05c5eb23b3b6a760f707c344b22f5871a4dedee4990b1840f57e4cee1d38560ff4507c354bbf29bc8ff05a179d95d7e100fcf19bd93c5362344a352c7b5a
2023-05-01 20:11:53 +00:00
Riccardo Casatta fc7c251502
Move weight constants in the `Weight` type
deprecate constants::MAX_BLOCK_WEIGHT and constants::MIN_TRANSACTION_WEIGHT
to nicely redirect users to the constants in the Weight type
2023-05-01 13:55:14 +02:00
Tobin C. Harding 984fe69448
bitcoin: Remove attribution from all files
Currently we have a mishmash of attribution lines accompanying the SPDX
identifier. These lines are basically meaningless because:

- The date is often wrong
- The original author attributed is not the only contributor to a file
- The term "rust bitcoin developers" is basically just noise

Just remove all the attribution lines and be done with it. While we are
at it add an SPDX line to the few files missing it, whether this license
nonsense is even needed is left as an argument for another day.
2023-05-01 09:22:48 +10:00
Andrew Poelstra ca7c60a09d
Merge rust-bitcoin/rust-bitcoin#1817: Hardcode expected weight in txin_txout_weight_tests
dd4ad9444e Hardcode expected weight in txin_txout_weight_tests (Peter Todd)

Pull request description:

  Rational: the expected weight is fixed so this both ensures we don't accidentally change it somehow, and makes it easier to re-use these test cases in other codebases (eg python-bitcoinlib).

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

Tree-SHA512: 4769a4bb8695f4f4c95e258bb5f06a232090b14c3d9159d6d5de2d09d7fc934a1b920b90cc09677a88fc0cf37ac21ed27794692dff2c73df4252c9551dc10fc2
2023-04-28 15:39:06 +00:00
Peter Todd dd4ad9444e
Hardcode expected weight in txin_txout_weight_tests
Rational: the expected weight is fixed so this both ensures we don't
accidentally change it somehow, and makes it easier to re-use these test
cases in other codebases (eg python-bitcoinlib).
2023-04-26 22:02:03 +00:00
Peter Todd 8b9ec8eb77
Fix policy link 2023-04-26 21:08:26 +00:00
Andrew Poelstra 967b58dc2a
Merge rust-bitcoin/rust-bitcoin#1810: Spelling and typo fixes
20b812dc66 Spelling and typo fixes (yancy)

Pull request description:

  Fix some types in Transaction.rs and encode.rs

ACKs for top commit:
  apoelstra:
    ACK 20b812dc66
  Kixunil:
    ACK 20b812dc66
  tcharding:
    ACK 20b812dc66

Tree-SHA512: d613189932ecb760be385c4095723501222cc8cec8b25525b58f76f9486d33fe46ab241bd2c098ea141f0fd12bb52abc677eba4a0cf3fe5b40b515bd75c7237c
2023-04-26 00:46:31 +00:00
Andrew Poelstra 84a075d03a
Merge rust-bitcoin/rust-bitcoin#1796: transaction: Rename is_coin_base to is_coinbase
a54e1ceab1 Apply rustfmt (The rustfmt Tyranny)
38d11ce3da ci: Make release CI search for NEXT.RELEASE instead (Steven Roose)
dad3abd20f transaction: Rename is_coin_base to is_coinbase (Steven Roose)

Pull request description:

  Alternative to https://github.com/rust-bitcoin/rust-bitcoin/pull/1795.

  Keep the old method as deprecated and add doc alias. Also change internal usage of the method.

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

Tree-SHA512: 52d9729bf83da164556d960f8867cb836ff57a0f619da3dd3620efffb28a974aac23b8085863ab0e072a4bdb2f13ac576efa43ad2eec9a271ad044227f4d00a4
2023-04-24 23:14:39 +00:00
The rustfmt Tyranny a54e1ceab1 Apply rustfmt 2023-04-24 18:20:23 +01:00
yancy 20b812dc66 Spelling and typo fixes 2023-04-24 16:51:10 +02:00
Steven Roose 38d11ce3da
ci: Make release CI search for NEXT.RELEASE instead 2023-04-19 16:07:03 +01:00
Steven Roose dad3abd20f
transaction: Rename is_coin_base to is_coinbase
Keep the old method as deprecated and add doc alias.
Also change internal usage of the method.
2023-04-19 16:02:59 +01:00
yancy 8f6317fbab Add predict_weight test for witness address types 2023-04-14 12:02:37 +02:00
yancy dff757d7db Comment predict_weight 2023-04-13 17:44:41 +02:00
Andrew Poelstra e83a2d3422
Merge rust-bitcoin/rust-bitcoin#1742: Use package in manifest and shorten import
fabcde036f Use package in manifest and shorten import (Tobin C. Harding)

Pull request description:

  We can use `package` to rename `bitcoin_hashes` to `hashes` and `bitcoin_internals` to `internals`. This makes imports more terse with no loss of meaning.

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

Tree-SHA512: bc5bff6f7f6bf3b68ba1e0644a83da014081d8c6c9d578c21cb54fdd56a018f68733dd1135d05b590ba193ed9efd12fa9019182c1fed347e604d8548f6ef9103
2023-04-05 14:20:06 +00:00
Martin Habovstiak 8b4280d5ef Update clippy MSRV configuration
We've upgraded MSRV but didn't update clippy config, so some things that
could be improved aren't caught by clippy. This updates the config and
fixes the new issues.

I also `rg '1\.41\.1'`ed for interesting changes and found one
additional improvement.
2023-04-02 16:28:10 +02:00
Tobin C. Harding a189942c64
Use doc_auto_cfg
If we use `#![cfg_attr(docsrs, feature(doc_auto_cfg))]` instead of
`#![cfg_attr(docsrs, feature(doc_cfg))]` we no longer need to manually
mark types with `#[cfg_attr(docsrs, doc(cfg(feature = "std")))]`.

Sweeeeeet.
2023-03-29 14:50:33 +11:00
Tobin C. Harding fabcde036f
Use package in manifest and shorten import
We can use `package` to rename `bitcoin_hashes` to `hashes` and
`bitcoin_internals` to `internals`. This makes imports more terse with
no loss of meaning.
2023-03-28 12:20:04 +11:00
Martin Habovstiak ed80df5ebc Add `ChainHash::from_genesis_block_hash`
This improves readability of converting `BlockHash` into `ChainHash`.
It's useful in e.g. Electrum protocol which sends `BlockHash`
(serialized backward).

Closes #1751
2023-03-26 10:42:22 +02:00
Andrew Poelstra 531dbc888c
Merge rust-bitcoin/rust-bitcoin#1730: Use shorter imports
122188f7dd Use shorter import statements (Tobin C. Harding)

Pull request description:

  Just patch 2, patch 1 is #1728

  From the commit log of patch 2

      Use shorter import statements

      As per discussion [0] use the shorter form for importing crates that we
      re-export (`hashes` and `secp256k1`).

      [0] https://github.com/rust-bitcoin/rust-bitcoin/discussions/1661

ACKs for top commit:
  apoelstra:
    ACK 122188f7dd
  sanket1729:
    utACK 122188f7dd

Tree-SHA512: 3f540464d38c72ba9d68f8ceda8600540bd0c3eef0ba67531c87fa1e0e4f757af7035cf80a1a5f17aa05604a17fdd9ef59bb6bece6b4145d540dac1e5362fc01
2023-03-23 17:39:03 +00:00
Andrew Poelstra 3417585a53
Merge rust-bitcoin/rust-bitcoin#1729: Bump MSRV to 1.48.0
1dc04fe10f Remove rust_v_1_46 (Tobin C. Harding)
71fa9e81e7 Bump MSRV to 1.48.1 (Tobin C. Harding)

Pull request description:

  Just patch 2, patch 1 is #1728

  From the commit log of patch 2

      Bump MSRV to 1.48.1

      As per discussion [0] bump our MSRV for all crates in `rust-bitcoin`
      repo to 1.48.1 [1].

      [0] https://github.com/rust-bitcoin/rust-bitcoin/discussions/1329
      [1] https://blog.rust-lang.org/2020/11/19/Rust-1.48.html

ACKs for top commit:
  apoelstra:
    ACK 1dc04fe10f
  sanket1729:
    ACK 1dc04fe10f

Tree-SHA512: 533470c55f7aeede88382db8245033dac9317d75b38ced2ad8256d38319632a524335f893044e96300a1e60048f9aaca2d1634735eb324eb8ed9ad3c9ab2f872
2023-03-23 00:50:33 +00:00
Andrew Poelstra 3ca9de82a6
Merge rust-bitcoin/rust-bitcoin#1735: Add kilo weight unit conversion
dbd2ea07b5 Add kilo weight unit conversion (yancy)

Pull request description:

  The FeeRate module defaults to sats per `kwu` so when doing fee calculations, it would be convenient to easily convert weight to the same units.

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

Tree-SHA512: fe26b631cf474f1dca627a4d21e9052c80f8ada9a0eb635c46b7f8f42095671b9b161fb5012b723699008372faf7668507d5e92bde7d1808d389f85dba33529b
2023-03-23 00:11:12 +00:00
Tobin C. Harding 1dc04fe10f
Remove rust_v_1_46
We just bumped the MSRV to 1.48.0 so we know that we have all features
from 1.46, no need for `rust_v_1_46` check anymore.
2023-03-23 08:27:10 +11:00
yancy dbd2ea07b5 Add kilo weight unit conversion 2023-03-22 20:05:07 +01:00
Tobin C. Harding 122188f7dd
Use shorter import statements
As per discussion [0] use the shorter form for importing crates that we
re-export (`hashes` and `secp256k1`).

[0] https://github.com/rust-bitcoin/rust-bitcoin/discussions/1661
2023-03-22 14:09:58 +11:00
Tobin C. Harding 5781896814
Run cargo +nightly rustfmt
We just merged a patch to enable formatting in CI but commit: `05fdead2
Feature: Add difficulty_float method for block::Header.` must have
slipped in.

Run the formatter.
2023-03-22 13:49:17 +11:00
Andrew Poelstra 24af58c5ad
Merge rust-bitcoin/rust-bitcoin#1434: Format the `rust-bitcoin` crate
913575ac91 hashes: Run the formatter (Tobin C. Harding)
52c4579057 Enable formatting for hashes (Tobin C. Harding)
3f16b6bf9f util: Run the formatter (Tobin C. Harding)
d210d2ac83 Enable formatting for util (Tobin C. Harding)
5973dce9db blockdata: Run the formatter (Tobin C. Harding)
0dcbed3c7b Enable formatting for blockdata (Tobin C. Harding)
a52746d01c psbt: Run the formatter (Tobin C. Harding)
ef306db5e2 Enable formatting for psbt (Tobin C. Harding)
296f2ed82c Make test panic instead of using code comment (Tobin C. Harding)
3ec8a12428 crypto: Run the formatter (Tobin C. Harding)
c8a3c58786 Enable formatting for crypto (Tobin C. Harding)
314e6786b4 crypto: Add rustfmt::skip attributes (Tobin C. Harding)
450a84f6e8 consensus: Run the formatter (Tobin C. Harding)
89143205f9 Enable formatting for consensus (Tobin C. Harding)
ce773af20f tests: Remove useless use of super imports (Tobin C. Harding)
ef01f4d0f6 consensus: Introduce local variables (Tobin C. Harding)

Pull request description:

  One final push crew, 16 patches, only a few are big.

  All non-trivial formatting is done in separate patches so the changes can be verified mechanically.

  With this applied the whole `rust-bitcoin` crate will be formatted.

  Big thanks to everyone for putting up with the ongoing formatting PRs, no-one likes doing these but hopefully this an improvement to the project - especially in helping us get more contributors to the project.

ACKs for top commit:
  tcharding:
    > ACK [913575a](913575ac91). Went through the workflow locally.
  sanket1729:
    ACK 913575ac91. Went through the workflow locally.
  apoelstra:
    ACK 913575ac91

Tree-SHA512: b30eaa2893563155de05f8fa97be4a24a7dd8bf43bb426314c5104598477ca2173af279da796da8b18cc53a0ed525908b3d4edd0504836a443465efa0773632d
2023-03-22 01:40:01 +00:00
junderw 05fdead2a4
Feature: Add difficulty_float method for block::Header. 2023-03-21 10:30:17 -07:00
Tobin C. Harding 5973dce9db
blockdata: Run the formatter
Run `cargo +nightly fmt`, no other manual changes.
2023-03-21 08:33:24 +11:00
Tobin C. Harding 0dcbed3c7b
Enable formatting for blockdata
Add `rustfmt::skip` attribute in a couple of places and then remove the
exclude for the `blockdata` module. Do not run the formatter, that will
be done as a separate patch to aid review.
2023-03-21 08:33:23 +11:00
Tobin C. Harding 296f2ed82c
Make test panic instead of using code comment
Currently we have a code comment that is supposed to assist devs in
maintaining the `network::constants::Network` type by failing to build
if a new variant is added. This plays havoc with the formatter because
the comment is hanging at the bottom of a match block and the formatting
thinks its for the proceeding line of code.

Instead of using a code comment add a panic so the unit test fails if a
new variant is added to `network::constants::Network`.
2023-03-21 08:33:23 +11:00
Andrew Poelstra e7521fa225
Merge rust-bitcoin/rust-bitcoin#1710: Support weight prediction in `const` context
00b46d6d9d Indent functions (Martin Habovstiak)
d56d202aeb Support weight prediction in `const` context (Martin Habovstiak)

Pull request description:

  **Notes for reviewers:**
  This is something that I want to use in my code and hopefully reasonably easy to review, so if this can get into 0.30 that'd be really nice. No hard feelings if it doesn't.
  I tried to put extra effort into making review easier by:
  * intentionally "mis-formatting" the first commit so diff is smaller and easy to understand - see individual commits.
  * copying patterns from non-const fn to const fn so it's obviously correct (includes same variable names)
  * not bothering with the array trick in `VarInt::len` and simply accepting the limitation of Rust 1.46+ (I use 1.48 BTW).

  **Description**

  Some smart contracts or simplified wallets statically know the sizes of
  transactions or inputs. The possible approaches to handling them so far
  were re-computing the values (and hoping the optimizer will const fold
  them) or using a simple constant which may be harder to understand and
  get right. It's much nicer to just use a `const` but our code didn't
  support it until now.

  This change adds methods that can compute the prediction in `const`
  context for Rust versions >= 1.46.0 which allow use of loops (and
  conditions but those could be workaround anyway).

  As a side effect of this, the change also adds `const` to `VarInt::len`
  in Rust 1.46+. While this one could be made unconditional using array
  trick it's probably not worth it because of the planned MSRV bump.

ACKs for top commit:
  apoelstra:
    ACK 00b46d6d9d
  tcharding:
    ACK 00b46d6d9d

Tree-SHA512: 5509886a68b4de5227db0e28d92a40be8de64592e0b189c519213db21bcfe98ca03d9a1936b1024729b97db69e8ec0b55fac870a7ce9bab0d0c9a47b2087990f
2023-03-19 13:22:34 +00:00
Martin Habovstiak 00b46d6d9d Indent functions
This fixes indentatiion that was intentionally "messed up" to make code
review easier.
2023-03-17 08:16:21 +01:00
Martin Habovstiak d56d202aeb Support weight prediction in `const` context
Some smart contracts or simplified wallets statically know the sizes of
transactions or inputs. The possible approaches to handling them so far
were re-computing the values (and hoping the optimizer will const fold
them) or using a simple constant which may be harder to understand and
get right. It's much nicer to just use a `const` but our code didn't
support it until now.

This change adds methods that can compute the prediction in `const`
context for Rust versions >= 1.46.0 which allow use of loops (and
conditions but those could be workaround anyway).

As a side effect of this, the change also adds `const` to `VarInt::len`
in Rust 1.46+. While this one could be made unconditional using array
trick it's probably not worth it because of the planned MSRV bump.

Note: this commit is intentionally unformatted to make diff easier to
understand. Formatting will be done in future commit.
2023-03-17 08:16:17 +01:00
Salvatore Ingala 2961c0c589
Exclude usage of atomic types if not supported for the target
The gate is only added for Rust >= v1.60, since earlier versions don't support #[cfg(target_has_atomic = ...)]
2023-03-16 22:42:15 +01:00
yancy f0a3aad5b8 Add tests to Weight type 2023-03-09 17:00:22 +01:00
yancy b311e96603 Fix weight subtract bug 2023-03-08 12:22:50 +01:00
Andrew Poelstra d6134248df
Merge rust-bitcoin/rust-bitcoin#1690: Add tests for the FeeRate type
e3f95ee22b Add tests for the FeeRate type (yancy)

Pull request description:

  Adds some tests for the `FeeRate` type.

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

Tree-SHA512: 74d6597c747d5aa62a6510bf9fa8de971f89ad56f571aadd496f6487e80cc88bb2b5a1c6bcfed825d09d18ca2310b2bfd6fdbe330f2760369d167a653d26bef8
2023-03-08 01:07:48 +00:00
Martin Habovstiak 3eb648df01 Add constants to `InputWeightPrediction`
There are several common spends in Bitcoin that have known input weight
predictions. It can be useful to have these as constants, so this change
adds them. However, this only adds native segwit ones as the others are
slowly fading away and might clutter the API.
2023-03-07 09:50:13 +01:00
yancy e3f95ee22b Add tests for the FeeRate type 2023-03-06 10:30:45 +01:00
Andrew Poelstra 2cf1a4c088
Merge rust-bitcoin/rust-bitcoin#1678: Improve the public API
42b07586ac Improve the public API (Tobin C. Harding)

Pull request description:

  We created the `crypto` crate as a container for cryptography modules with the idea that it may be split out into a separate crate. There is no reason for users of the lib to know about this module. Also, we have two `taproot` modules, one in `crypto` and one at the crate root, this makes for un-ergonomic usage of the lib.

  Improve the public API by doing:

  - Make the `crypto` module private (`pub(crate)`).
  - Re-export `crypto::taproot::Signature` (and `Error`) from `crate::taproot`

  Fix: #1668

ACKs for top commit:
  apoelstra:
    ACK 42b07586ac
  Kixunil:
    ACK 42b07586ac

Tree-SHA512: 5713b98b2a48d2776cdd6ca2c0e798d6e2df604d780e5182bd770fb2df807cf1f65bc24663ee6f7734fc1e0c80494c5a87dc60e44c83acefcffec7f059203a47
2023-03-01 15:42:36 +00:00
Tobin C. Harding 42b07586ac
Improve the public API
We created the `crypto` crate as a container for cryptography modules
with the idea that it may be split out into a separate crate. There is
no reason for users of the lib to know about this module. Also, we have
two `taproot` modules, one in `crypto` and one at the crate root, this
makes for un-ergonomic usage of the lib.

Improve the public API by doing:

- Make the `crypto` module private (`pub(crate)`).
- Re-export `crypto::taproot::Signature` (and `Error`) from
  `crate::taproot`
2023-03-01 09:28:42 +11:00
Andrew Poelstra 5ad2bec626
Merge rust-bitcoin/rust-bitcoin#1679: Add constant for coinbase maturity
7d1645aea0 Add constant for coinbase maturity (benthecarman)

Pull request description:

  Not sure if this is the best place to put this but it is nice to have a constant for this instead of having other libraries make their own (ie https://github.com/lightningdevkit/rust-lightning/pull/1924#pullrequestreview-1222807626)

ACKs for top commit:
  tcharding:
    ACK 7d1645aea0
  apoelstra:
    ACK 7d1645aea0

Tree-SHA512: 5ac2a3359cadd303158c66ba45db8f4bf8cc80b6c19604262999ff361fd0bd98e2a4851c57da1962cb5c74f5789a85c8b3861f1742706a60ce1fbc57c3c200cc
2023-02-28 22:01:34 +00:00
benthecarman 7d1645aea0
Add constant for coinbase maturity 2023-02-28 05:37:10 -06:00
Tobin C. Harding 161273b209
Re-name hash inner/byte methods
Currently we have an associated type on hash types `Inner` with
accompanying methods `into_inner`, `from_inner`, `as_inner`. Also, we
provide a way to create new wrapped hash types. The use of 'inner'
becomes ambiguous with the addition of wrapped types because the inner
could be the inner hash type or the `Inner` byte array of the inner
wrapped hash type.

In an effort to make the API more clear and uniform do the following:

- Rename `Inner` -> `Bytes`
- Rename `*_inner` -> `*_byte_array`
- Rename the inner hash to/from methods to `*_raw_hash`

Correct method prefix `into_` -> `to_` because theses methods convert
owned `Copy` types.

Add the trait Bound `Copy` to the `Bytes` type because we rely on this
trait bound for the conversion methods to be correctly named according
to convention.

Because of the dependency hole created by `secp256k1` this patch changes
the secp dependency to a git tag dependency that includes changes to the
hashes calls required so that we can get green lights on CI in this
repo.
2023-02-27 14:23:58 +11:00
Andrew Poelstra 5a867821aa
Merge rust-bitcoin/rust-bitcoin#1623: Improve string parsing
090dad770f Improve string parsing (Tobin C. Harding)

Pull request description:

  Currently we implement string parsing for height/time from the `absolute` module but not the `relative` module.

  Improve the macros used to implement string parsing and use the new versions to implement string parsing for the height and time types in `relative`.

  Done while reviewing data structures in relation to `serde`.

ACKs for top commit:
  apoelstra:
    ACK 090dad770f
  Kixunil:
    ACK 090dad770f

Tree-SHA512: bfa88efbaf5dc35755eb46df373a08e223f112860e8a65f58db9fdd77e2c01dc9377da735b33ef58940004fe5fe11690ac09be19591fded2c9fd04cd7d2bdf73
2023-02-25 21:16:38 +00:00
Harshil Jani f62885890d Accept borrowed values in InputWeightPrediction::new()
Signed-off-by: Harshil Jani <harshiljani2002@gmail.com>
2023-02-25 17:20:40 +05:30
Andrew Poelstra 3a53e7e12e
Merge rust-bitcoin/rust-bitcoin#1653: Show cache construction in rustdoc
438ee45691 Show cache construction in rustdoc (Tobin C. Harding)

Pull request description:

  To make it more clear what the cache is show the cache construction line in rustdoc.

ACKs for top commit:
  apoelstra:
    ACK 438ee45691
  Kixunil:
    ACK 438ee45691

Tree-SHA512: d6da6bad57fddf9e2f4bcfb7c9b87df38bf4b2bb914e92e52d5ae8afa3405a9793536d7164223021ab6d183ddde732cf6889370834e36f37bae470127b0271fa
2023-02-22 15:28:56 +00:00
Tobin C. Harding 438ee45691
Show cache construction in rustdoc
To make it more clear what the cache is show the cache construction line
in rustdoc.
2023-02-21 08:12:32 +11:00
Martin Habovstiak d83739a980 Clarify the intention of strange condition
It may not be obvious why the condition in `push_bytes` module checks
for negation of 16 and 32 bit architectures rather than 64 bit. This
adds a comment about it being conservative.
2023-02-20 19:40:19 +01:00
Tobin C. Harding 9f39e872bc
Rename SchnorrSighashType to TapSighashType
As we did for `SchnorrSighash`, rename the `SchnorrSighashType` to
`TapSighashType`.
2023-02-20 12:58:09 +11:00
Tobin C. Harding 40c246743b
Split Sighash into LegacySighash and SegwitV0Sighash
Currently we have `TapSighash` that is used for taproot sighashes but
for non-taproot sighashes we use `hash_types::Sighash`. We can improve
the API by creating a `LegacySighash`, and `SegwitV0Sighash`.

Copy the original `Sighash` macro calls to create the two new types in
the `sighash` module.

While we are at it, put the `TapSighash` and `TapSighashTag` into the
`sighash` module also.
2023-02-20 12:58:09 +11:00
Tobin C. Harding e38d843536
Do not use deprecated function in rustdoc example
Currently we are use the deprecated signing method on `Transaction`, we
should use the new method on the sighash cache.
2023-02-20 12:58:08 +11:00
Tobin C. Harding 7e4da3c0ab
Move taproot keys to the keys module
We have a keys module, taproot keys should live in there.
2023-02-20 12:58:05 +11:00
Martin Habovstiak bcd95fa036 Add a newtype for script-pushable slice
The code previously contained undocumented panic when trying to push
slice. This change solves it by adding a newtype that guarantees limited
length.
2023-02-18 16:35:25 +01:00
Martin Habovstiak 26fc4152ec Use `PushDataLenLen` to improve confidence
Script parsing is composed of several functions which implicitly rely on
various properties. Adding a type that restricts the valid values makes
local review easier.
2023-02-18 12:24:36 +01:00
Martin Habovstiak bb2c7ec790 Introduce `hex_lit` crate
So far we deserialized hex into `Vec<u8>` at run time. This was mainly
in tests where it had negligible performance cost. However moving the
computation to compile time has a few benefits: it allows proving the
length of the decoded bytes and identifies potential typos before the
code goes through LLVM and other compilation machinery which makes
feedback faster.

This change uses the `hex_lit` crate to move computation to compile
time. It is implemented as `const` declarative macro which doesn't blow
up compilation time.
2023-02-18 12:03:08 +01:00
Tobin C. Harding c3cc9e52ab
Fix absolute lock time examples and tests
An absolute lock time of 100 is nonsensical because we are well past
block 100. This value was used because it makes sense for _relative_
locktimes but for absolute lock times it makes the examples and tests
slightly confusing.
2023-02-15 13:58:50 +11:00
Tobin C. Harding a819cf99d6
Remove FromStr impl from ScriptBuf
`FromStr` impls should roundtrip with `Display` imlps but currently our
`ScriptBuf` displays using instructions but parses hex.
2023-02-14 10:37:56 +11:00
Andrew Poelstra 5fed454949
Merge rust-bitcoin/rust-bitcoin#1644: Flatten the types directory
272cdbcf7c Flatten the types directory (Tobin C. Harding)

Pull request description:

  We recently created a `types` subdirectory under `script` to keep all the `Script` and `ScriptBuf` impls together. Turns out this additional level of subdirectory is a bit annoying and we can achieve the same grouping by just using `script/mod.rs`.

  Move code from `types/mod.rs` to `script/mod.rs`, move the two submodules up a level, remove the `types` directory.

  Fix: #1640

ACKs for top commit:
  Kixunil:
    ACK 272cdbcf7c
  apoelstra:
    ACK 272cdbcf7c

Tree-SHA512: 91fd78084829fa24f3b6420602d7d5094670647fff43e6e193d6de3126f1657132873ea133540d87db7d0d4dfc4cb9666489e39c861377085ce0254da81fd564
2023-02-13 23:20:09 +00:00
Andrew Poelstra f00b6362a0
Merge rust-bitcoin/rust-bitcoin#1636: Weight prediction
ae2aaaa436 Add `script_pubkey_lens` method (Martin Habovstiak)
cf068d16b0 Implement transaction weight prediction (Martin Habovstiak)

Pull request description:

  When creating a transaction one must know the the fee beforehand to set
  appropriate amounts for outputs and to know the fee, weight is required.
  So far we only had a method on an already-constructed transaction. This
  method clearly wasn't helpful when constructing the transaction except
  for hacks like temporarily adding an all-zeroes signature.

  This change adds a function that can compute the transaction weight
  without knowing individual bytes of the scripts, witnesses and other
  elements. It only needs to know their sizes.

  To make the API less error-prone a special, trivial, type is also added
  for computing the lengths of witnesses.

  Based on #1627

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

Tree-SHA512: 55376601c2c2826bb0909cc25ff5b65816f0b1a2d57fb2cd8831f3db5382de0f4a364d518b312f0528bb5f44c30f3f74f8d254145eed2bfd65e2332b7c4d7c8b
2023-02-13 22:40:42 +00:00
Tobin C. Harding 272cdbcf7c
Flatten the types directory
We recently created a `types` subdirectory under `script` to keep all
the `Script` and `ScriptBuf` impls together. Turns out this additional
level of subdirectory is a bit annoying and we can achieve the same
grouping by just using `script/mod.rs`.

Move code from `types/mod.rs` to `script/mod.rs`, move the two
submodules up a level, remove the `types` directory.
2023-02-14 06:48:10 +11:00
Martin Habovstiak ae2aaaa436 Add `script_pubkey_lens` method
In some cases people construct the transaction with a dummy fee output
value before calculating the weight. A method to create the iterator
over `script_pubkey` lengths is useful in such cases.
2023-02-13 11:16:31 +01:00
Martin Habovstiak 6be89bf94f Add `minimal_non_dust` to `TxOut`
In some scenarios it's useful to create outputs with minimal relayable
value. E.g. outputs designated for fee bumping using CPFP. A method for
this is useful.

This implements a constructor of `TxOut` that computes the minimal
non-dust value from the passed script.

Closes #1459
2023-02-11 17:14:48 +01:00
Martin Habovstiak cf068d16b0 Implement transaction weight prediction
When creating a transaction one must know the the fee beforehand to set
appropriate amounts for outputs and to know the fee, weight is required.
So far we only had a method on an already-constructed transaction. This
method clearly wasn't helpful when constructing the transaction except
for hacks like temporarily adding an all-zeroes signature.

This change adds a function that can compute the transaction weight
without knowing individual bytes of the scripts, witnesses and other
elements. It only needs to know their sizes.

To make the API less error-prone a special, trivial, type is also added
for computing the lengths of witnesses.
2023-02-11 12:57:37 +01:00
Andrew Poelstra 4fdbf076bf
Merge rust-bitcoin/rust-bitcoin#1559: Deprecate `script::read_uint`
a7117bf8f1 Document source of logic fro read_scriptint (Tobin C. Harding)
2eb2420b40 Add comment on rountripping read/write scripint (Tobin C. Harding)
657dd51e8b Use OP_0 to better mimic bitcoin core code (Tobin C. Harding)
31d254a6a8 Fix push operators URL (Tobin C. Harding)
84cd4ca964 Deprecate script::read_uint (Tobin C. Harding)

Pull request description:

  Patch one does the deprecation, the rest of the PR is made up of tiny improvements to the code around reading/writing 'scriptint's (conceptually `CScriptNum`s). I did all this while trying to decipher the discussion on #1547.

  ### Note Please

  There are many more changes in the pipeline for all this read/write "script int" stuff. This PR was done ages ago and I believe it stall adds value.

  I re-did the whole PR manually because of the recent `script` module changes. I hope no one else has to do that - if you do please feel free to holla and I'll "rebase" your PR for you.

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

Tree-SHA512: 5e8ee7fa8d1393a1a50e4241dd947b837cc0ddd15ff1239a49e4839489459fb95d184d6773f73633d55c436310bfab0c73f806d492ed4a4215f924c6c0993936
2023-02-10 20:02:12 +00:00
Tobin C. Harding 090dad770f
Improve string parsing
Currently we implement string parsing for height/time from the
`absolute` module but not the `relative` module.

Improve the macros used to implement string parsing and use the new
versions to implement string parsing for the height and time types in
`relative`.
2023-02-10 12:28:25 +11:00
Tobin C. Harding 1e0e712bb0
Add push_* methods for lock times
Lock times are u32 and can necessitate encoding using 5 bytes. As such
they are "special".

Add methods `push_lock_time` and `push_sequence` for pushing absolute
lock times and sequence numbers. We do not push relative locktimes
because they are only 16 bits from the original sequence number.
2023-02-10 12:23:15 +11:00
Tobin C. Harding a7117bf8f1
Document source of logic fro read_scriptint
Our `script::read_scriptint` function is based on the constructor
code (incl. call to `set_vch`) code from Bitcoin Core. Add rustdoc
comment saying so, emit a link because there are already multiple links
to `script.h` in this file (one just right below the added comment).
2023-02-10 12:02:32 +11:00
Tobin C. Harding 2eb2420b40
Add comment on rountripping read/write scripint
We only support reads of upto 4 bytes where as Bitcoin Core allows
reading a `CScriptNum` with more bytes than that. Add a rustdoc
comment (incl. link to Bitcoin Core) mentioning that.
2023-02-10 12:02:00 +11:00
Tobin C. Harding 657dd51e8b
Use OP_0 to better mimic bitcoin core code
Our `Builder::push_int` method is the same as Bitcoin Core `CScript`
`push_int64` method. We currently use `OP_FALSE` (equivalent to `OP_0`)
but recently we added `OP_0`, lets use it to make our code better mimic
Core (also saves devs checking that `OP_FALSE` is the same as `OP_0`).
2023-02-10 12:01:23 +11:00
Tobin C. Harding 31d254a6a8
Fix push operators URL
The URL is wrong (section `#Push_operators` should be
`#push-operators`), also should use angle brackets not back ticks.
2023-02-10 11:59:37 +11:00
Tobin C. Harding 84cd4ca964
Deprecate script::read_uint
There is no current usage for reading an unsigned script integer, seems
like this is kruft from days gone past.
2023-02-10 11:58:25 +11:00
Andrew Poelstra b6387db47f
Merge rust-bitcoin/rust-bitcoin#1627: Add `Weight` and `FeeRate` newtypes
70cf4515db Add `Weight` and `FeeRate` newtypes (Martin Habovstiak)

Pull request description:

  Use of general-purpose integers is often error-prone and annoying. We're working towards improving it by introducing newtypes.

  This adds newtypes for weight and fee rate to make fee computation easier and more readable. Note however that this dosn't change the type for individual parts of the transaction since computing the total weight is not as simple as summing them up and we want to avoid such confusion.

  Part of #630
  Replaces #1607 (I want to get this in quickly and don't want to be blocked on DanGould's availability.)

ACKs for top commit:
  apoelstra:
    ACK 70cf4515db
  tcharding:
    ACK 70cf4515db

Tree-SHA512: ab9cc9f554a52ab0109ff23565b3e2cb2d3f609b557457b4afd8763e3e1b418aecbb3d22733e33304e858ecf900904a1af6e6fdc16dc21483b9ef84f56f103b2
2023-02-10 00:33:56 +00:00
Martin Habovstiak 70cf4515db Add `Weight` and `FeeRate` newtypes
Use of general-purpose integers is often error-prone and annoying. We're
working towards improving it by introducing newtypes.

This adds newtypes for weight and fee rate to make fee computation
easier and more readable. Note however that this dosn't change the type
for individual parts of the transaction since computing the total weight
is not as simple as summing them up and we want to avoid such confusion.

Part of #630
2023-02-08 23:17:30 +01:00
Tobin C. Harding a9108d3939
Refactor script module
The `script` module is large and unwieldy.

Refactor the `script` module, splitting it up into a tree of modules.
Here are a few of the changes and their stated benefits

- Split the two script types out into separate files: Readers of the
methods can then tell immediately from the file name which type they are
reading.
- Put all the impls for the two script types together: Makes parsing the
API easier because one can more quickly see which traits are implemented
on what i.e., all the `AsRef` imlps are grouped together.
- Put the impls for the two script types in order, first `Script` then
`ScriptBuf`: Makes it easier for us to see if we missed something.
- Put the `Builder` and `Instruction` (and associated) types in their
own modules: Some devs find long files hard to navigate, so far there
hasn't been too much push back against short files.
- Put tests in a separate file: This idea was recently discussed.

This is only moving code and fixing import statements etc. No other
changes to the code.
2023-02-07 16:07:54 +11:00
Andrew Poelstra 12298ddc14
Merge rust-bitcoin/rust-bitcoin#1608: Set `rustv_1_53` in build script
bb612fdafa Set rustv_1_53 in build script (Tobin C. Harding)

Pull request description:

  The rust version is supposed to be set by the build script so that users automagically get features matching the toolchain in use. Currently we have a feature in the manifest for `rustv_1_53` instead setting a compiler conditional configuration option in the build script. This causes `cargo +1.41.1 --all-features check` to fail.

  ## Note

  I don't see `rustv_1_46` used anywhere, do we need that still?

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

Tree-SHA512: f74195d4ee5a5bc5f209e99d30789df3552cef10aee5ea8b61a5a701b753999c34d04be9fe0321ccee7a8ec14fa5a05e0b454b9dc5f8deddd7b5b8d4f3d7e744
2023-02-06 14:11:01 +00:00
Tobin C. Harding aae03999a5
Do not export unusual hash types at crate root
Currently we use a wildcard to export all the hash types in
`hash_types`. We are moving to a world were we only export
normal/standard types from the crate root.

Remove the reexport of the following hash types:

- `FilterHash`
- `FilterHeader`
- `TxMerkleNode`
- `WitnessCommitment`
- `WitnessMerkleNode`
- `XpubIdentifier`
- `Sighash`

Fix: #1541
2023-02-04 13:34:20 +11:00
Tobin C. Harding bb612fdafa
Set rustv_1_53 in build script
The rust version is supposed to be set by the build script so that users
automagically get features matching the toolchain in use. Currently we
have a feature in the manifest for `rustv_1_53` instead setting a
compiler conditional configuration option in the build script. This
causes `cargo +1.41.1 --all-features check` to fail.
2023-02-02 10:12:16 +11:00
Tobin C. Harding a308e1e2ea
Remove FromHex for all types except Vec and array
Remove `FromHex` from hash and script types

- Remove the `FromHex` implementation from hash types and `ScriptBuf`
- Remove the `FromStr` implementation from `ScriptBuf` because it does not
  roundtrip with `Display`.
- Implement a method `from_hex` on `ScriptBuf`.
- Implement `FromStr` on hash types using a fixed size array.

This leaves `FromHex` implementations only on `Vec` and fixed size arrays.
2023-02-01 08:26:46 +11:00
Andrew Poelstra ca902e65f8
Merge rust-bitcoin/rust-bitcoin#1596: Implement fmt traits for ScriptBuf
ed6f6d11dd Implement fmt traits for ScriptBuf (Tobin C. Harding)

Pull request description:

  We can improve ergonomics of the `script` module by implementing the `fmt` traits on `ScriptBuf`, trivial because we can call through to the `Script` implementations.

  Fix: #1585

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

Tree-SHA512: 878a1522af4ed1e10d1d8d60d150e6571008c008b5e5c662c67462f9e09075b4f1fe4e399ed50e98cd7253b6815937c6732cd1ce02b74a5be017d5b8fcdbbd2f
2023-01-31 14:40:50 +00:00
Tobin C. Harding ed6f6d11dd Implement fmt traits for ScriptBuf
We can improve ergonomics of the `script` module by implementing the
`fmt` traits on `ScriptBuf`, trivial because we can call through to the
`Script` implementations.
2023-01-31 17:48:01 +11:00
Tobin C. Harding 7dde3b3b22 Make max/min_value functions const
The `max_value` and `min_value` functions only exist to be
compatible/uniform with Rust 1.41.1 they will never change and they just
return a constant value. They can therefore be made const functions.
2023-01-31 08:35:32 +11:00
Tobin C. Harding 3c0598b399
Add standard constants to lock times
Some of the lock time structs (`Height`, `Time` ect.) are missing
standard constants for min, max ect.

Add standard constants taking into consideration the various locktime
corner cases.

Add `max_value` and `min_value` to be consistent with Rust 1.41.1 (incl.
`Sequence`).

Fix: #1451
2023-01-25 07:43:48 +11:00
Andrew Poelstra ac65c338ab
Merge rust-bitcoin/rust-bitcoin#1575: Add documentation to `Sequence::is_final`
a762a89b48 Add documentation to Sequence::is_final (Tobin C. Harding)
b1490a26ea Move enables_absolute_lock_time method (Tobin C. Harding)

Pull request description:

  The term "final" is an archaic Bitcoin term however it is well used, it exists in Bitcoin Core code as well as in various bips. To help folks new to Bitcoin add documentation to the `is_final` method including historical notes.

  Note, this does _not_ deprecate `is_final` - while writing the notes I found the term "final" in enough official places that I think its fair game to keep the term, some things people just have to learn, we can definitely help with that learning though.

  Fix: #1198

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

Tree-SHA512: 895fbdce90223d90c0a68fb1e3d6b7aada4a3606d1294ea4df1f4194681a79d970b0434e7bb078f6d5cbf413b3550e72560d6d5cf811a5a959adf53f7f778ab2
2023-01-24 15:52:15 +00:00
Andrew Poelstra 96865e3b23
Merge rust-bitcoin/rust-bitcoin#1580: Delegate debug for ScriptBuf to Script
8c0e5213d3 Delegate debug for ScriptBuf to Script (Tobin C. Harding)

Pull request description:

  Currently the derived implementation of `Debug` for `ScriptBuf` prints the inner vector of u8s as integers, this is ugly and hard to read. The `Script` implementation of `Debug` prints the script opcodes and data as hex, we can just delegate to it.

  With this applied we get debug output of form:

      Script(OP_DUP OP_HASH160 OP_PUSHBYTES_20 3bde42dbee7e4dbe6a21b2d50ce2f0167faa8159 OP_EQUALVERIFY OP_CHECKSIG)

  Fix: #1516

ACKs for top commit:
  Kixunil:
    ACK 8c0e5213d3
  apoelstra:
    ACK 8c0e5213d3

Tree-SHA512: ca07d9fb191f4e0379cbd96b2944e6881094a8334d39b97209b6bf452a3c15d4aede53b9c88176b9b7667b7a539d47897940bc561dc9f8cd83ce1990a08047e1
2023-01-24 15:30:41 +00:00
sanket1729 6ebc9de252 Introduce WitnessProgram struct and cleanup Address validity invariants
Addresses with Segwitv0 not having len 20/32 are invalid and cannot be
constructed. Also cleans up a API bug in
ScriptBuf::new_witness_prog(ver, prog) allowing prog of invalid lenghts.
2023-01-23 13:45:46 -08:00
Tobin C. Harding 8c0e5213d3
Delegate debug for ScriptBuf to Script
Currently the derived implementation of `Debug` for `ScriptBuf` prints
the inner vector of u8s as integers, this is ugly and hard to read. The
`Script` implementation of `Debug` prints the script opcodes and data as
hex, we can just delegate to it.

With this applied we get debug output of form:

    Script(OP_DUP OP_HASH160 OP_PUSHBYTES_20 \
    3bde42dbee7e4dbe6a21b2d50ce2f0167faa8159 OP_EQUALVERIFY OP_CHECKSIG)

Fix: #1516
2023-01-23 12:49:11 +11:00
Tobin C. Harding a762a89b48
Add documentation to Sequence::is_final
The term "final" is an archaic Bitcoin term however it is well used, it
exists in Bitcoin Core code as well as in various bips. To help folks
new to Bitcoin add documentation to the `is_final` method including
historical notes.
2023-01-23 09:18:23 +11:00
Tobin C. Harding b1490a26ea
Move enables_absolute_lock_time method
In preparation for deprecating the `is_final` method; move the
`enables_absolute_lock_time` method to be directly above the `is_final`
method.

Refactor only, no logic changes.
2023-01-22 17:04:58 +11:00
Andrew Poelstra 649bf023af
Merge rust-bitcoin/rust-bitcoin#1537: Export the DisplayHex trait from within prelude
70fe07f1ce Export the DisplayHex trait from within prelude (Tobin C. Harding)

Pull request description:

  We use `internals::hex::display::DisplayHex` in many places, we can improve ergonomics of the `internals` crate by re-exporting it from the `prelude` module.

ACKs for top commit:
  Kixunil:
    ACK 70fe07f1ce
  apoelstra:
    ACK 70fe07f1ce

Tree-SHA512: 96a89135cb0b829b7b5926a3b344f78e178b5b48e772a69da5133fab6d2e14e7b7bbaa56b7a417a5c1a64337546a1c7bac32307d3a1f27aa199ed61f590902bf
2023-01-16 15:21:33 +00:00
Tobin C. Harding 70fe07f1ce
Export the DisplayHex trait from within prelude
We use `internals::hex::display::DisplayHex` in many places, we can
improve ergonomics of the `internals` crate by re-exporting it from the
`prelude` module.
2023-01-10 09:56:41 +11:00
sanket1729 5ff2635585 Rename TapBranchHash -> TapNodeHash 2023-01-09 12:42:46 -08:00
Martin Habovstiak 1b0988833a Remove `ToHex`
The `ToHex` trait was replaced by either simple `Display`/`LowerHex`
where appropriate or `DisplayHex` from `bitcoin_internals` which is
faster.

This change replaces the usages and removes the trait.
2023-01-07 19:50:03 +01:00
Andrew Poelstra 5f2beb8ae2
Merge rust-bitcoin/rust-bitcoin#1527: Rename `push_scriptint` and make it private
94b678e73f Rename `push_scriptint` and make it private (Martin Habovstiak)

Pull request description:

  `push_scriptint` is a significant footgun with an unclear name. This renames it and unpublishes to avoid mistakes by downstream crates.

  Closes #1517

ACKs for top commit:
  apoelstra:
    ACK 94b678e73f

Tree-SHA512: 9e1772c6fb326d8b0c78d702ad9926a79a91589feb8650aed7c5e75bfbdbf0164357b4d5b190877c40b8469e0e3be3d3453fe407741b5dae0c5758176f756417
2023-01-06 14:57:00 +00:00
bnabi 77799bf5cb 1514: fix formatting in opcodes, unify definition and display 2023-01-05 14:28:20 +05:30
Martin Habovstiak 94b678e73f Rename `push_scriptint` and make it private
`push_scriptint` is a significant footgun with an unclear name. This
renames it and unpublishes to avoid mistakes by downstream crates.

Closes #1517
2023-01-04 22:58:27 +01:00
sanket1729 98203bc8b3
Merge rust-bitcoin/rust-bitcoin#1438: Replace `Vec::from_hex` with `hex!`
089a1e452d Replace `Vec::from_hex` with `hex!` (Martin Habovstiak)

Pull request description:

  This makes the code less noisy and is a preparation for changing it to `const`-based literal. Because of the preparation, places that used variables to store the hex string were changed to constants.

  There are still some instances of `Vec::from_hex` left - where they won't be changeable to `const` and where `hex!` is unavailable (integration tests). These may be dealt with later.

  See also #1189

  Note that while the change appears big it's nearly entirely mechanical, so should be pretty easy to review. (But I don't feel it's `trivial`.)

ACKs for top commit:
  apoelstra:
    ACK 089a1e452d
  tcharding:
    ACK 089a1e452d

Tree-SHA512: c55357b8cffc86f8e107c0f8390490ede75948260018f63dc926455700cbcaf422f6ae72444f93943065e6f4ffa4335bee9160a64184ea4f8e91721f30a46ace
2023-01-01 02:24:01 -08:00
Martin Habovstiak 089a1e452d Replace `Vec::from_hex` with `hex!`
This makes the code less noisy and is a preparation for changing it to
`const`-based literal. Because of the preparation, places that used
variables to store the hex string were changed to constants.

There are still some instances of `Vec::from_hex` left - where they
won't be changeable to `const` and where `hex!` is unavailable
(integration tests). These may be dealt with later.

See also #1189
2022-12-31 21:10:19 +01:00
Martin Habovstiak 8d58ee2ca3 Change `#[cfg(docsrs)]` to `#[cfg(doc)]` on `use`
The additional `use` items were added to improve the style of
documentation. Because they were only used for doc they were `cfg`ed.
But because this is independent from being built by `docs.rs` the `cfg`
should've been `doc` not `docsrs`.

IOW `docsrs` means roughly `all(doc, nightly)` and the added items are
unrelated to `nightly`.
2022-12-31 20:58:17 +01:00
Andrew Poelstra 3e4a299615
Merge rust-bitcoin/rust-bitcoin#1513: Add mutation testing to the `locktime` module
ca471557a5 locktime: Add mutation testing (Tobin C. Harding)
26c0da41b4 locktime: Add inline to public functions (Tobin C. Harding)

Pull request description:

  Add mutation testing to the `locktime` module and add unit tests to cover all mutants and ensure they are killed.

ACKs for top commit:
  sanket1729:
    ACK ca471557a5. How stable are the mutation tests?
  apoelstra:
    ACK ca471557a5

Tree-SHA512: 46a59c90fc25b0c803e96f7c5b98bd39055f7835e45ba137b2a01ad4221a676c54bc228b9ef7663b7300bb4260a6c2c80a0820c4f1bf0987650e1e2bd699f62d
2022-12-30 22:18:31 +00:00
sanket1729 90bb150422
Merge rust-bitcoin/rust-bitcoin#1512: Make `Witness::tapscript()` return `Script` instead of raw bytes
e0bc50953a Make `Witness::tapscript()` return `Script` instead of raw bytes (Jiri Jakes)

Pull request description:

  Since there is unsized `Script` now, this method can return it.

ACKs for top commit:
  sanket1729:
    utACK e0bc50953a
  tcharding:
    ACK e0bc50953a

Tree-SHA512: 32d4ca14f1b0fc1029f7376b1a43db90332b869a806609c82f660cb2690a4f0e1b91e1799fdac0d43c8a630aed0331f251d4159a662e86e5942c6fb698c42cd2
2022-12-29 23:08:02 -08:00