Commit Graph

548 Commits

Author SHA1 Message Date
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
Martin Habovstiak 0555695757 Fix formatting
This slipped in and is breaking builds.
2023-05-11 13:03:22 +02:00
Riccardo Casatta 2df9b2c8b2
Merge rust-bitcoin/rust-bitcoin#1844: make bip21 schema lowercase
8835d5d2f1 make bip21 schema lowercase (Riccardo Casatta)

Pull request description:

  The spec RFC3986 specifies the scheme is case insensitive and we were uppercasing to optimize QR code representation.
  Unfortunately, common platform such as Android seems to fail to recognize uppercase schema, so for compatibility reason we use lowercase.

  close #1843

ACKs for top commit:
  Kixunil:
    ACK 8835d5d2f1
  apoelstra:
    ACK 8835d5d2f1

Tree-SHA512: 02d228b52fe4df20edb71ba8e2ab8a2bae4b912252e30a3150ee3af74e65a6e91b165c9579273b57e894366c9792a8312ea973723cd8c5d98037aaba80d7cf07
2023-05-11 10:41:35 +02:00
Andrew Poelstra 684e14caee
Merge rust-bitcoin/rust-bitcoin#1768: Allow parsing sub-sat denominations with decimal points
6c6a89b1d1 Add sub-sat fractions parsing regression test (Martin Habovstiak)
f1a3dc6719 Allow parsing sub-sat denoms with decimal points (Martin Habovstiak)
b3d9a267ea Add a few more amount parsing tests (Martin Habovstiak)

Pull request description:

  Numbers with only zeros after decimal points are valid if they are also
  multiples of `10^precision` (e.g. 1000 for msats). These were
  artificially disallowed as "too precise" which was at least misleading.

  This change allows parsing such numbers.

  And yes, I know this is not perfectly efficient (unless the compiler figures out some magic opts) but so isn't the rest of the code. TBH this parsing code drives me crazy and I'd love to rewrite it to be more efficient and readable.

ACKs for top commit:
  apoelstra:
    ACK 6c6a89b1d1

Tree-SHA512: 03cf4b416f2eac25e0aac57ef964ed06fa36c7fe8244bdcf97852cc58e1613b1ec6132379b834da58ad3240fdd61508a384202f63aa9ffa335c18cd7b2b724d3
2023-05-10 20:30:54 +00:00
Riccardo Casatta 8835d5d2f1
make bip21 schema lowercase
The spec RFC3986 specifies the scheme is case insensitive and we were uppercasing
to optimize QR code representation.
Unfortunately, common platform such as Android seems to fail to recognize
uppercase schema, so for compatibility reason we use lowercase.
2023-05-10 20:36:15 +02:00
Andrew Poelstra d93e781148
Merge rust-bitcoin/rust-bitcoin#1838: Move and rename TxOut default trait to a const called NULL
75b3f19b96 Move and rename TxOut default trait to a const called NULL (yancy)

Pull request description:

  Create an associated constant `const TxOut::NULL` for consensus signing code and remove the default trait.  Note I tried to deprecate the `default()` fn instead of just removing it but it doesn't seem to be possible.  Also because `TxOut::NULL` is `const`, `ScriptBuf::new()` needed to be changed to `const fn`.

ACKs for top commit:
  apoelstra:
    ACK 75b3f19b96
  Kixunil:
    ACK 75b3f19b96

Tree-SHA512: ff61a2b1641a1ba32f183c27205af2d868dbc2eb47cf758c3d8315329d2c23e0b8a82ea0ab59d1de9add0d238f927165e2e4df014aab1ef066d74d4feda0700b
2023-05-09 18:31:34 +00:00
yancy 639c548aed docs: Add doc comments for external crates 2023-05-08 10:57:49 +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 dea628276c
Merge rust-bitcoin/rust-bitcoin#1837: feat: generate PrivateKey
995c797e0d feat: generate PrivateKey (kshitjj)

Pull request description:

  added a function to generate a private key

  Resolves: #1823

ACKs for top commit:
  apoelstra:
    ACK 995c797e0d
  tcharding:
    ACK 995c797e0d

Tree-SHA512: 29ba54be8cb777e71a4683835686cbf2978b23736f629d7bbff468074235fece261ca170c23f358d1bd878987566d09e4488c3f1a106c59a5c8bdf52b98abffe
2023-05-08 03:13:51 +00:00
kshitjj 995c797e0d feat: generate PrivateKey 2023-05-07 21:56:39 +05:30
Martin Habovstiak 6c6a89b1d1 Add sub-sat fractions parsing regression test
This test triggers the bug fixed in previous commit.
2023-05-07 08:47:53 +02:00
Martin Habovstiak f1a3dc6719 Allow parsing sub-sat denoms with decimal points
Numbers with only zeros after decimal points are valid if they are also
multiples of `10^precision` (e.g. 1000 for msats). These were
artificially disallowed as "too precise" which was at least misleading.

This change allows parsing such numbers.
2023-05-07 08:45:25 +02:00
Martin Habovstiak b3d9a267ea Add a few more amount parsing tests
These tests try to stress various edge cases that should return
`ParseAmountError::TooPrecise`.
2023-05-07 08:39:24 +02:00
Andrew Poelstra 4abbdc20a0
Merge rust-bitcoin/rust-bitcoin#1820: Expose valid (min, max) difficulty transition thresholds
8e6f953aa7 Expose valid (min, max) difficulty transition thresholds (Wilmer Paulino)

Pull request description:

  Once `U256` was made private, we lost the ability to check whether a valid difficulty transition was made in the chain, since `Target` doesn't expose any operations. We only choose to expose `Shl<u32>` and `Shr<u32>` such that we can compute the min and max target thresholds allowed for a difficulty transition.

  This is something we realized was missing after bumping to `rust-bitcoin v0.30.0` in `rust-lightning`, specifically for our `lightning-block-sync` crate. It may also be worth having a helper in `rust-bitcoin` that checks a header properly builds upon the previous, but that can be left for future work.

ACKs for top commit:
  Kixunil:
    ACK 8e6f953aa7
  sanket1729:
    ACK 8e6f953aa7 . Sorry, was confused by some details.
  apoelstra:
    ACK 8e6f953aa7

Tree-SHA512: 740dd64089426463dc6a19726d5a562276bd0966e0e31af8e1e67b28d18945644ac0e50be3cf0cc7fa604acc3d2c5b912a77a7caa69d8cff85f70fd57e5328c5
2023-05-06 18:53:15 +00: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
Andrew Poelstra 25f569adeb
Merge rust-bitcoin/rust-bitcoin#1811: Use Amount type for TxOut value field
d57ec019d5 Use Amount type for TxOut value field (yancy)

Pull request description:

  Propose using `Amount` type for the `TxOut` `value` field.  I only implemented `Decodable ` and `Encodable` enough to compile but this needs to completed obviously if using `Amount` seems like a good idea.

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

Tree-SHA512: df3fd55294d5f9392ca90bb920be8fbb9d7d285d97669412e07d5a099f70f81fd73e7e259679de9c8ce5c6c855e64f62213700f0fb7db415e0c706c509485377
2023-05-05 00:10:55 +00:00
Andrew Poelstra 1abbed2129
Merge rust-bitcoin/rust-bitcoin#1785: Implement serde::Serialize for Address
ed6421c939 address: Add generic serde::Serialize for Address (Steven Roose)
814b9917da address: Add Sync, Send, Sized and UnPin marker traits on NetworkValidation (Steven Roose)

Pull request description:

  With the new rewrite of Address, `serde::Serialize` is only implemented on `Address<bitcoin::address::NetworkChecked>` and `Address<bitcoin::address::NetworkUnchecked>`. But the compiler has no way of knowing that that are all the possible versions of `Address`, so the generic `Address<impl bitcoin::address::NetworkValidation>` doesn't implement `serde::Serialize`.

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

Tree-SHA512: 65e43dff244c94fe08ccb2d985781a2687a1e2db186960a35d4ae89f3b31c5af66892630a3ebaac9cecdc83638487425afa17374869d278648b348869e0ba091
2023-05-04 21:52:38 +00:00
Wilmer Paulino 8e6f953aa7
Expose valid (min, max) difficulty transition thresholds
Once `U256` was made private, we lost the ability to check whether a
valid difficulty transition was made in the chain, since `Target`
no longer exposes any arithmetic operations.
2023-05-04 12:41:28 -07: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
Tobin C. Harding 3885f4d430
Add MIN/MAX consts to amounts
Add associated consts for minimum and maximum values to the `Amount` and
`SignedAmount` types.
2023-05-03 08:22:30 +10:00
Andrew Poelstra 67ff453a3a
Merge rust-bitcoin/rust-bitcoin#1801: Adds as_core_arg serde module to network
aa6a3530a6 Adds as_core_arg serde module to network (Sergi Delgado Segura)

Pull request description:

  The new module provides serde functions to convert from/to Bitcoin Core network variants.

  Fixes #1799

ACKs for top commit:
  Kixunil:
    ACK aa6a3530a6
  tcharding:
    ACK aa6a3530a6
  sanket1729:
    ACK aa6a3530a6

Tree-SHA512: f536d0e41144928b71dc9b58735d4e0b883ee84a7a84e868f1d74b284c2e2be746ddcb30028dd099515bc933e7efa2cf26717f506edde84c40c3f523a5a8ed27
2023-05-02 01:45:41 +00: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
Sergi Delgado Segura aa6a3530a6 Adds as_core_arg serde module to network
The new module provides serde functions to convert from/to Bitcoin Core
network variants

Includes tests suggested by @tcharding
2023-04-17 12:28:13 +02:00
Andrew Poelstra 0af8d45e21
Merge rust-bitcoin/rust-bitcoin#1797: Add predict_weight test for witness address types
8f6317fbab Add predict_weight test for witness address types (yancy)

Pull request description:

  Add a predict_weight test for address types with witness data

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

Tree-SHA512: 954908f068d6b0e8f7cc6bc6bffd110d490d7165cf2544ff0f936264761cb1f8e4da1e37fd5a6a34fd8b05f8d72817a3b340bbf968b29c9f538f10853347fdf0
2023-04-14 16:08:31 +00: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
Steven Roose 3d524e06e4
Add Inventory::network_hash() method 2023-04-13 16:23:22 +01:00
Steven Roose ed6421c939
address: Add generic serde::Serialize for Address
Otherwise a generic `Address<impl NetworkValidation>` is not
serializable.
2023-04-09 19:02:53 +01:00
Steven Roose 814b9917da
address: Add Sync, Send, Sized and UnPin marker traits on NetworkValidation 2023-04-09 19:02:43 +01: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
Andrew Poelstra 8edfc4597b
Merge rust-bitcoin/rust-bitcoin#1765: unsafe address type conversions
29cb34eed7 Refactor Address struct and its methods (Harshil Jani)

Pull request description:

  Closes #1755

  In this PR the `as_unchecked` is added to the Address struct, which returns a reference to the same address but with the type Address<NetworkUnchecked>.  Similarly, the `assume_checked_ref` is added to Address<NetworkUnchecked>, which returns a reference to the same address but with the type Address.

ACKs for top commit:
  tcharding:
    ACK 29cb34eed7
  apoelstra:
    ACK 29cb34eed7

Tree-SHA512: 75ba40883d9fb31026b0e94e8d3fdcf808ff38a4d10f61f99a1e14ddc48358da86bad44cd78563dc67aa5d39382a5493e73c03891317f361f590e39b6257cb84
2023-04-04 17:40:33 +00:00
Andrew Poelstra 350b413e59
Merge rust-bitcoin/rust-bitcoin#1773: Make `sha256t_hash_newtype!` evocative of the output.
91f45a214f Replace hardcoded values with compile-time hashing (Martin Habovstiak)
095b7958dd Make `sha256t_hash_newtype!` evocative of the output. (Martin Habovstiak)

Pull request description:

  The Rust API guidelines state that macros should be evocative of the
  output, which is a sensible recommendation. We already had this for
  `hash_newtype!` macro but didn't for sha256t version.

  This changes the macro to have this syntax:

  ```rust
  sha256t_hash_newtype! {
      // Order of these structs is fixed.
      /// Optional documentation details here. Summary is auto-generated.
      /*pub*/ struct Tag = raw(MIDSTATE_BYTES, LEN);

      /// Documentation here
      #[hash_newtype(forward)] // optional, default is backward
      /*pub*/ struct HashType(/* attributes allowed here */ _);
  }
  ```

  Closes https://github.com/rust-bitcoin/rust-bitcoin/issues/1427

  Depends on #1769

  How do you like the syntax? Is weird `struct Foo = bar(..);` acceptable?

ACKs for top commit:
  tcharding:
    ACK 91f45a214f
  apoelstra:
    ACK 91f45a214f

Tree-SHA512: f6b555b20311a4c1cb097bc296c92459f6fbe16ba102d8c977eb2383dbcae3cc8ffce32e3cb771e7e22197fda26379971aa4feaadc5e2e70d37fa690ae8b8859
2023-04-03 18:21:58 +00:00
Andrew Poelstra 142b4fc431
Merge rust-bitcoin/rust-bitcoin#1771: Use slice patterns
873501a85f Use slice patterns (Martin Habovstiak)

Pull request description:

  Some code looks better with slice patterns. This changes `bip32` to use them.

ACKs for top commit:
  apoelstra:
    ACK 873501a85f
  tcharding:
    ACK 873501a85f

Tree-SHA512: 2e91b21d0f943c04f592112f241444ba3b1b1dad47ea6bea162f62cfbcaf98e383257cad144072b3807aaa0c122dabab638ac10b1cc1f5179727a5f05aac0659
2023-04-03 15:48:35 +00:00
Harshil Jani 29cb34eed7 Refactor Address struct and its methods
This commit refactors the Address struct and its methods to improve
its functionality and usability.The AddressInner struct now holds
the payload and network, and the PhantomData<V> type is used to track
the network validation state.

Also as_unchecked and assume_checked_red methods are added to allow
conversion between checked and unchecked network validation state.

Signed-off-by: Harshil Jani <harshiljani2002@gmail.com>
2023-04-03 15:24:45 +05:30
Martin Habovstiak 91f45a214f Replace hardcoded values with compile-time hashing
Previous changes enabled passing the string used as a tag into
`sha256t_hash_newtype!` macro rather than hard-coding midstate. This
commit takes advantage of it and replaces the hard-coded values with
compile-time executed (`const`) hashing.
2023-04-02 17:01:10 +02:00
Martin Habovstiak 095b7958dd Make `sha256t_hash_newtype!` evocative of the output.
The Rust API guidelines state that macros should be evocative of the
output, which is a sensible recommendation. We already had this for
`hash_newtype!` macro but didn't for sha256t version.

This changes the macro to have this syntax:

```rust
sha256t_hash_newtype! {
    // Order of these structs is fixed.
    /// Optional documentation details here. Summary is auto-generated.
    /*pub*/ struct Tag = raw(MIDSTATE_BYTES, LEN);

    /// Documentation here
    #[hash_newtype(forward)] // optional, default is backward
    /*pub*/ struct HashType(/* attributes allowed here */ _);
}
```

Closes #1427
2023-04-02 17:00:52 +02: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
Martin Habovstiak 873501a85f Use slice patterns
Some code looks better with slice patterns. This changes `bip32` to use
them.
2023-04-02 01:08:03 +02:00
Andrew Poelstra 895db71156
Merge rust-bitcoin/rust-bitcoin#1763: Use doc_auto_cfg
a189942c64 Use doc_auto_cfg (Tobin C. Harding)

Pull request description:

  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.

  Props to pezcore for the lesson :)

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

Tree-SHA512: 1ced1e09f5d1733b362b83ca650d3f52c89eb57e78e8437f74c496d89776548f8c50feab6750352342e2abe680434681de2c126ce36a81dda21397b9695d4d4e
2023-03-30 16:56:49 +00:00
Harshil Jani 046bda321b comparing NetworkUncheck addresses
Signed-off-by: Harshil Jani <harshiljani2002@gmail.com>

implementation of PartialEq<Address> for Address<NetworkUnchecked>

Signed-off-by: Harshil Jani <harshiljani2002@gmail.com>
2023-03-29 11:33:20 +05:30
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
Andrew Poelstra dc72dfb9f2
Merge rust-bitcoin/rust-bitcoin#1753: Add `ChainHash::from_genesis_block_hash`
ed80df5ebc Add `ChainHash::from_genesis_block_hash` (Martin Habovstiak)

Pull request description:

  This improves readability of converting `BlockHash` into `ChainHash`. It's useful in e.g. Electrum protocol which sends `BlockHash` (serialized backward).

  Closes #1751

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

Tree-SHA512: b03eb9ca0a1eb38196cc8e43751c2def0d00b463b774003c5319bf56599db598ddd4bb0e2ad0bd3803f21a925135992d421848e7b1ba5e7bab3a405f60fc973c
2023-03-28 01:25:18 +00: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
Andrew Poelstra 2e2d5e9e34
Merge rust-bitcoin/rust-bitcoin#1740: create a set of recognized denomination forms
82b6332b91 create a set of recognized denomination forms (yancy)

Pull request description:

  I took a stab at restricting the acceptable forms here.  There was some consensus that "BtC" was confusing that was discussed in a previous pr https://github.com/rust-bitcoin/rust-bitcoin/pull/1715.

  Also, personally I felt that the `PossiblyConfusingDenomination` enum variant was itself confusing.  I think it's probably cleaner to just maintain a list of acceptable forms and treat everything else as unknown.  For now I just created a const of possibly confusing forms.

ACKs for top commit:
  Kixunil:
    ACK 82b6332b91
  apoelstra:
    ACK 82b6332b91

Tree-SHA512: f3c212046e4d8a34ce2d50f34065b1778ae190ca80dd9ad73ef41eb5925705c88c0a764ec149d731eb1735d670c5542415953633a7f3f672d0c1e46b6e004d0b
2023-03-27 19:43:00 +00:00
yancy 82b6332b91 create a set of recognized denomination forms 2023-03-27 11:19:55 +02: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
Tobin C. Harding 7cdc90565f
Mutate mul_u64 with mutagen
Add the `mutate` attribute to mutate `mul_u64`. Add non-doc comments
listing the two false positives. These are identical but we list them
twice so when devs grep for `mutagen false pos` the same number of lines
for each function is displayed as is displayed by the `mutagen` run.
This coding false positives thing is also introduced in PR #1655.
2023-03-23 13:51:21 +11: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
Andrew Poelstra 311b023eef
Merge rust-bitcoin/rust-bitcoin#1721: Mark `Denomination` as `non_exhaustive`
67618d679d Mark `Denomination` as `non_exhaustive` (Martin Habovstiak)

Pull request description:

  It is possible that we will add new variants to `Denomination` in the future so making it `non_exhaustive` is better for forward compatibility.

ACKs for top commit:
  tcharding:
    ACK 67618d679d
  apoelstra:
    ACK 67618d679d
  sanket1729:
    ACK 67618d679d

Tree-SHA512: a28b7c6577f098b3d64c505e948e3b9fd0cc43a911a2b7c35610ada6a2f75514a1cb05ab8c99212340cf4e34426ac8b5f0ecc65d5afa22d67296e82d878b9308
2023-03-21 23:08:28 +00:00
Andrew Poelstra 86142f17fb
Merge rust-bitcoin/rust-bitcoin#1717: Remove extern crate hashbrown
2d23e11569 Remove extern crate hashbrown (Tobin C. Harding)

Pull request description:

  (Merge candidate only after release of 0.30.0)

  We no longer have a "hashbrown" feature, the feature gated `pub extern crate hashbrown` should have been removed when we removed the feature.

ACKs for top commit:
  apoelstra:
    ACK 2d23e11569
  Kixunil:
    ACK 2d23e11569

Tree-SHA512: 36d4c539f3f972d42bbc9fda9e96063a78d35afc6aebbf534b878dcef0440b72c2a990bcbdbb2ad3bf99cab7048cdbd4002899c2b314da21e4a7bacaf8c71f0f
2023-03-21 22:47:58 +00:00
Martin Habovstiak 67618d679d Mark `Denomination` as `non_exhaustive`
It is possible that we will add new variants to `Denomination` in the
future so making it `non_exhaustive` is better for forward
compatibility.
2023-03-21 20:21:37 +01:00
junderw 05fdead2a4
Feature: Add difficulty_float method for block::Header. 2023-03-21 10:30:17 -07:00
Tobin C. Harding 2d23e11569
Remove extern crate hashbrown
We no longer have a "hashbrown" feature, the feature gated `pub extern
crate hashbrown` should have been removed when we removed the feature.
2023-03-21 15:14:29 +11:00
Tobin C. Harding 3f16b6bf9f
util: Run the formatter
Run `cargo +nightly fmt`, no other manual changes.
2023-03-21 08:33:24 +11: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 a52746d01c
psbt: Run the formatter
Run `cargo +nightly fmt`, no other manual changes.
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
Tobin C. Harding 3ec8a12428
crypto: Run the formatter
Run `cargo +nightly fmt`, no other manual changes.
2023-03-21 08:33:23 +11:00
Tobin C. Harding 314e6786b4
crypto: Add rustfmt::skip attributes
In preparation for formatting the `crypto` module add a couple of `skip`
attributes to keep arrays formatted 8 bytes per line.
2023-03-21 08:33:22 +11:00
Tobin C. Harding 450a84f6e8
consensus: Run the formatter
Run `cargo +nightly fmt`, no other manual changes.
2023-03-21 08:33:22 +11:00
Tobin C. Harding ce773af20f
tests: Remove useless use of super imports
We already import `super::*`, these other imports are useless.
2023-03-21 08:33:22 +11:00
Tobin C. Harding ef01f4d0f6
consensus: Introduce local variables
In preparation for running the formatter introduce a couple of local
variables to reduce the line length and inhibit function call from being
split over multiple lines.

Refactor only, no logic changes.
2023-03-21 08:33:22 +11:00
yancy 39e528fdfc Add CentiBitcoin to denominations 2023-03-20 16:39:28 +01: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
junderw 4924148dc6
Swap out `Work::log2` implementation for `U256::to_f64` 2023-03-17 14:10:00 -07: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
Martin Habovstiak a6188a90c0 Improve QR code formatting documentation
It wasn't obvious that displaying address with alternate formatting
upper cases bech32 addresses.

This change adds information about this and also a note about the
compatibility of various wallets.
2023-03-15 06:45:58 +01:00
Andrew Poelstra 37b02199e6
Merge rust-bitcoin/rust-bitcoin#1707: Add a method to `pow::Target` for returning difficulty as an f64.
2158f88f1d Add a method to `pow::Target` for returning difficulty as an f64. (junderw)

Pull request description:

  Closes #1703

  This adds a conversion function to U256 to get an f64. We use the method shown in the following blog post.

  https://blog.m-ou.se/floats/

  Target::MAX was converted to a f64 and set as a const that is verified in a unit test.

  The code is rather confusing, so I took a crack at explaining it in my comments as well. Please let me know if you want it cleaned up some more.

ACKs for top commit:
  apoelstra:
    ACK 2158f88f1d
  tcharding:
    ACK 2158f88f1d

Tree-SHA512: 7c0e82bd1756950c1c6dfb9da91fd71b276e2e7dc8a33e69112f87b87e358240f0f7c4894d24ab228e149d862938833a2e65e345ce2712a78dc86dec197da34f
2023-03-14 18:25:38 +00:00
junderw 2158f88f1d
Add a method to `pow::Target` for returning difficulty as an f64.
This adds a conversion function to U256 to get an f64. We use the method shown in the following blog post. https://blog.m-ou.se/floats/
Target::MAX was converted to a f64 and set as a const that is verified in a unit test.
2023-03-10 07:54:36 -07: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
Andrew Poelstra e9e8be3acc
Merge rust-bitcoin/rust-bitcoin#1692: Add constants to `InputWeightPrediction`
3eb648df01 Add constants to `InputWeightPrediction` (Martin Habovstiak)

Pull request description:

  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.

  If anyone wants other constants, please write them for me, their value is not that great to me so I'm not motivated to figure out the correct numbers. :)

  This would be nice to add to 0.30 since it's small and easy but not critical.

ACKs for top commit:
  apoelstra:
    ACK 3eb648df01
  tcharding:
    ACK 3eb648df01
  sanket1729:
    ACK 3eb648df01

Tree-SHA512: 51d2cd2ecef7e6b79f9d4b52319e34b908fe8b5a337551dc2088994feeafc9c3ca87884c3db8369f8bd002947d6d14b373f08ef1419db282713206ed6f1b309a
2023-03-08 00:37:05 +00:00