Commit Graph

257 Commits

Author SHA1 Message Date
Jiri Jakes bef7c6e687 Use marker type to enforce validation of `Address`'s network
Parsing addresses from strings required a subsequent validation of
network of the parsed address. However, this validation was not
enforced by compiler, one had to remember to perform it.

This change adds a marker type to `Address` that will assist the
compiler in enforcing this validation.
2023-01-11 19:27:10 +08:00
sanket1729 ac6340943c
Merge rust-bitcoin/rust-bitcoin#1538: A crash course in rust perf (#988 nits)
1a409ecc8e Use &[u8] instead of Cursor as Read (DanGould)
81ca10701a Use as_ref() instead of costly clone() (DanGould)
debcce6a03 Improve magic bytes push performance (DanGould)

Pull request description:

  A follow up to address [nits in  #988](https://github.com/rust-bitcoin/rust-bitcoin/pull/988#pullrequestreview-1233687052)

ACKs for top commit:
  tcharding:
    ACK 1a409ecc8e
  elichai:
    ACK 1a409ecc8e
  sanket1729:
    utACK 1a409ecc8e

Tree-SHA512: bc6c538d8f92b0efa8063ba33bb47f0c9dc2f99eb5db267b66aa01fc70fd634695a0cd9c27fb0f33e3498628729a8e0f622d9876dc1c83fab4a1e46484d425b2
2023-01-11 02:38:33 -08:00
DanGould 1a409ecc8e
Use &[u8] instead of Cursor as Read 2023-01-09 23:21:20 -05:00
DanGould 81ca10701a
Use as_ref() instead of costly clone() 2023-01-09 23:17:04 -05:00
DanGould debcce6a03
Improve magic bytes push performance 2023-01-09 23:10:07 -05:00
sanket1729 f39cd88f5f Use TapNodeHash in NodeInfo
This cleans up some ugly code where we had to use sha256::Hash for
combining TapLeafHash and TapBranchHash
2023-01-09 13:01:44 -08:00
sanket1729 5ff2635585 Rename TapBranchHash -> TapNodeHash 2023-01-09 12:42:46 -08:00
Andrew Poelstra 1f7affbc95
Merge rust-bitcoin/rust-bitcoin#1531: Remove `ToHex`
1b0988833a Remove `ToHex` (Martin Habovstiak)

Pull request description:

  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.

ACKs for top commit:
  tcharding:
    ACK 1b0988833a
  apoelstra:
    ACK 1b0988833a

Tree-SHA512: a1b508e24ac247a0692c01b7cb1e7fa8f23fbfa3d6c3d5dfe669eec01f940a96c3f88508cb0b2e3529eebd9cb51e7d41435dbd5f4cbaf3bc14b9c7e7d790308b
2023-01-09 12:50:37 +00:00
sanket1729 cc1e6c02c8
Merge rust-bitcoin/rust-bitcoin#1523: Fix LeafVersion serde
a400757676 Add failing tests from serde-json (sanket1729)
b3246bf73f Fix LeafVersion serde (sanket1729)

Pull request description:

  The default implementation maps to visit_u64. The current implementation
  does not roundtrip with many deserializers, including serde_json. See
  the failing test in the second commit

ACKs for top commit:
  Kixunil:
    tACK a400757676
  tcharding:
    ACK a400757676

Tree-SHA512: a6307b799b0bb4af398addc9ddbff0d811b632d1cc6ab4bdd77aaf3f151e48dd1cd10e3f90a71ef8fc3feb1cd988f7a719f92ec745b5fdf2ae0f0ad97ab2fa10
2023-01-09 01:40:43 -08:00
sanket1729 a400757676 Add failing tests from serde-json 2023-01-08 06:47:40 -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
Andrew Poelstra d71c428949
Merge rust-bitcoin/rust-bitcoin#1528: [WIP] 1514: fix formatting in opcodes
77799bf5cb 1514: fix formatting in opcodes, unify definition and display (bnabi)

Pull request description:

  See  #1514.

ACKs for top commit:
  Kixunil:
    ACK 77799bf5cb
  apoelstra:
    ACK 77799bf5cb

Tree-SHA512: beab4e5ff0f2c995cfb120e5aa04fdeda740ce5136e60da390cf0156c372e3807263a8cdf3eea2762a0fb68b222fed56efdc5ad686b9f481fabe3fccf227fed3
2023-01-06 13:53:21 +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
Casey Rodarmor 16c49df688 Accept amounts with denominations with and without spaces 2023-01-04 11:07:03 -08:00
Andrew Poelstra deaf21dd84
Merge rust-bitcoin/rust-bitcoin#988: Replace consensus `Encodable`/`Decodable` Psbt Serialization
c4363e5ba1 Deserialize Psbt fields, don't consensus_encode (DanGould)
c1dd6ad8a2 Serialize Psbt fields, don't consensus_encode them (DanGould)
1b7b08aa5d De/serialize Psbt without consensus traits (DanGould)

Pull request description:

  fix https://github.com/rust-bitcoin/rust-bitcoin/issues/934

  Instead of using consensus {de,en}code, serialize high-level structures (PartiallySignedTransaciton, Output, Input) borrow the signature from `Serialize`, `Deserialize` traits and implement them on Psbt:

  ```rs
  impl Psbt {
      /// Serialize a value as raw data.
      fn serialize(&self) -> Vec<u8>;

      /// Deserialize a value from raw data.
      fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error>;
  }
  ```

ACKs for top commit:
  apoelstra:
    ACK c4363e5ba1
  tcharding:
    ACK c4363e5ba1
  sanket1729:
    ACK c4363e5ba1. One small comment, but can be addressed in follow up if there is nothing else from other reviewers.
  Kixunil:
    ACK c4363e5ba1

Tree-SHA512: d8dd5ef1189b36fde08969b1ec36006a6fc55ae990f62ea744e6669e03a7f9e90e1d5907be7eac48ee1af23bc20a62aa7e60ff1ff78080d0e923bb9ccedcd432
2023-01-04 18:32:59 +00:00
sanket1729 b3246bf73f Fix LeafVersion serde
The default implementation maps to visit_u64. The current implementation
does not roundtrip with many deserializers, including serde_json. See
failing test in the subsequent commit
2023-01-01 11:42:29 -08: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 3b11ff6607
Merge rust-bitcoin/rust-bitcoin#1387: Add rand-std feature
941083ec4e Remove rand-std dev-dependency from secp256k1 (Tobin C. Harding)
f71335f971 Add rand-std feature (Tobin C. Harding)

Pull request description:

  This PR uncovered incorrect feature gating in `secp256k1`, fixed in https://github.com/rust-bitcoin/rust-secp256k1/pull/519

  Currently we enable "secp256k1/rand-std" in the "rand" feature, this is incorrect because it means "rand" implies "std" which it does not.

  Add a "rand-std" feature that turns on "seck256k1/rand-std" and make the "rand" feature turn on "seck256k1/rand".

  Fix: #1384

ACKs for top commit:
  sanket1729:
    ACK 941083ec4e
  apoelstra:
    ACK 941083ec4e

Tree-SHA512: e1d12196766c2b3082b488fe7d0c03a865ebbfc70ffa6f128c57074df14da71e56c31ea92982991d376ac62fa86c6639049ce17aac93613b523ddcc99677c269
2022-12-30 23:53:54 +00:00
Andrew Poelstra 3a867ee8ce
Merge rust-bitcoin/rust-bitcoin#1503: Fix wrong newtype APIs
64f7d2549e Fix wrong newtype APIs (Martin Habovstiak)

Pull request description:

  This fixes several API bugs:

  * Use `TryFrom` instead of `From` for fallible conversions
  * Move byte conversion methods from `impl_array_newtype` to `impl_bytes_newtype`
  * Add missing trait impls like `AsRef`, `Borrow`, their mutable versions and infallible conversions from arrays

  Closes #1336

  Notably this change is much less bad than even I expected and some places actually get cleaner than before.

ACKs for top commit:
  tcharding:
    ACK 64f7d2549e
  sanket1729:
    code review ACK 64f7d2549e
  apoelstra:
    ACK 64f7d2549e

Tree-SHA512: 0c5ab19aa1a01ccf34ad1c2f3f66bd003a3142a840cb7cf57e1449e721f6eed1523555c62f0e3391012708767ff86315ff7edce9afd85017a67c61cfe57dddbc
2022-12-30 23:10:39 +00: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
Andrew Poelstra 22820274c1
Merge rust-bitcoin/rust-bitcoin#1496: Verify and fix the `U256::mul_u64` method
cf9733d678 Verify and fix mul_u64 (Tobin C. Harding)

Pull request description:

  Add kani verification for `U256::mul_u64`, doing so uncovered a bug in the current implementation due to overflow. Re-write the `mul_u64` method.

  Fix: #1497

  ## Note

  This PR now _only_ tests `mul_u64`, I will add testing div as a separate PR.

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

Tree-SHA512: c044fb385073bb068412195c079f26057ba560b2a7eca22693c360d324a7c0119cf10da2e70e544c69e05ca8269a81156418d307f4e11d8a4c1c1a7cafa32d0c
2022-12-30 20:43:33 +00:00
Andrew Poelstra 4d7b8cd3b4
Merge rust-bitcoin/rust-bitcoin#1420: Allow dead_code/unused_imports when fuzzing
5a2a37d4be Allow dead_code/unused_imports when fuzzing (Tobin C. Harding)

Pull request description:

  Littering the codebase with `#[cfg(not(fuzzing))]` is a bit messy just to quieten the linter during fuzzing. Instead just globally allow.

  Done while debugging #1409

ACKs for top commit:
  sanket1729:
    ACK 5a2a37d4be
  apoelstra:
    ACK 5a2a37d4be

Tree-SHA512: fb84215a2b00ad6d3321b2781ba285af513ff8fd413c0997045a41c4f23028d2ef0fdf083839289d0c5108c990aa66bdae4430ad3ef32881eac5324b2e881b3b
2022-12-30 14:56:40 +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
Tobin C. Harding ca471557a5
locktime: Add mutation testing
Add mutation testing to the `locktime` module and add unit tests to
cover all mutants and ensure they are killed.
2022-12-30 11:13:42 +11:00
Tobin C. Harding 26c0da41b4
locktime: Add inline to public functions
Add `#[inline]` to all public functions/methods excluding error types
and `Display` impls. Error paths do not need to be fast and presumably
`Display` is called on code paths that do IO so this also does not need
to be fast.
2022-12-30 10:19:35 +11:00
Tobin C. Harding cf9733d678 Verify and fix mul_u64
Add kani verification for `U256::mul_u64`, doing so uncovered a bug in
the current implementation due to overflow.

Re-write the `mul_u64` method.

Props to Elichai for the algorithm.

Co-authored-by: Elichai Turkel <elichai.turkel@gmail.com>
2022-12-30 09:39:41 +11:00
sanket1729 c92591e645
Merge rust-bitcoin/rust-bitcoin#1502: Fix bug in `ScriptBuf::extend` for short iterators
920599da94 Add test for previous commit (Martin Habovstiak)
a7f3458c27 Fix bug in `ScriptBuf::extend` for short iterators (Martin Habovstiak)

Pull request description:

  `ScriptBuf::extend` contained an optimization for short scripts that was
  supposed to preallocate the buffer and then fill it. By mistake it
  attempted to fill it from already-exhausted iterator instead of the
  temporary array it drained the items into. This obviously produced
  garbage (empty) values.

  This was not caught by tests because the optimization is only active for
  scripts with known maximum length and the test used `Instructions` which
  doesn't know the maximum length.

ACKs for top commit:
  sanket1729:
    ACK 920599da94 . Tested that the bug is correctly fixed and tested in the new test
  tcharding:
    ACK 920599da94

Tree-SHA512: a80f5f262a840d8e77efd42d63c511224380ee3efa6c31855233e81c90332ac15db228e8d552d039d729d7b642e03c3939c8b6a92d3279001377515acb83abea
2022-12-29 07:52:03 -08:00
Jiri Jakes e0bc50953a Make `Witness::tapscript()` return `Script` instead of raw bytes 2022-12-29 15:22:16 +01:00
Andrew Poelstra 615759a8c2
Merge rust-bitcoin/rust-bitcoin#1495: Introduce mutation testing
8ce928b8e7 Add testing section to readme (Tobin C. Harding)
2e79a0bdc4 Introduce mutation testing (Tobin C. Harding)

Pull request description:

  Introduce mutation testing by way of mutagen [0] (see #1484 for context).

  - Conditionally add the dev-dependency `mutagen` (using `do_mutate` flag)

      This flag is not very well named but `mutagen` and `mutate` are already taken?

  - Mutate all methods of the `U256` struct that do not require additional unit tests.

      Uses `cfg(all(test, do_mutate), mutate)` - I cannot workout why we need to check on `test` as well i.e., I don't understand why we cannot use `cfg(do_mutate, mutate)`?

  With this applied test can be run as usual with a stable toolchain. To run mutagen we use `RUSTFLAGS='--cfg=do_mutate' cargo +nightly mutagen` (doing so runs 29 mutants).

  [0] https://github.com/llogiq/mutagen

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

Tree-SHA512: 024ba5d2dc983f7cd0444e09ba13280771157204999d2a44502e07a1d6436f571b75003c7cb543c943f17949b848d4070eda4e194bda774a3e41443ff79af0af
2022-12-24 06:02:48 +00:00
Martin Habovstiak 64f7d2549e Fix wrong newtype APIs
This fixes several API bugs:

* Use `TryFrom` instead of `From` for fallible conversions
* Move byte conversion methods from `impl_array_newtype` to
  `impl_bytes_newtype`
* Add missing trait impls like `AsRef`, `Borrow`, their mutable versions
  and infallible conversions from arrays

Closes #1336
2022-12-23 01:28:17 +01:00
Martin Habovstiak 920599da94 Add test for previous commit
If this test is added before the previous commit it will fail. It passes
now demonstrating the bug got fixed.
2022-12-22 23:51:07 +01:00
Martin Habovstiak a7f3458c27 Fix bug in `ScriptBuf::extend` for short iterators
`ScriptBuf::extend` contained an optimization for short scripts that was
supposed to preallocate the buffer and then fill it. By mistake it
attempted to fill it from already-exhausted iterator instead of the
temporary array it drained the items into. This obviously produced
garbage (empty) values.

This was not caught by tests because the optimization is only active for
scripts with known maximum length and the test used `Instructions` which
doesn't know the maximum lenght.
2022-12-22 23:35:03 +01:00
Tobin C. Harding 941083ec4e Remove rand-std dev-dependency from secp256k1
In order to get better test coverage we should not enable the secp26k1
feature "rand-std" in dev-dependencies but instead feature gate tests
that depend on this feature.
2022-12-23 08:33:21 +11:00
Tobin C. Harding f71335f971 Add rand-std feature
Currently we enable "secp256k1/rand-std" in the "rand" feature, this is
incorrect because it means "rand" implies "std" which it does not.

Add a "rand-std" feature that turns on "seck256k1/rand-std" and make the
"rand" feature turn on "seck256k1/rand".
2022-12-23 08:32:56 +11:00
Tobin C. Harding 2e79a0bdc4 Introduce mutation testing
Introduce mutation testing by way of mutagen [0]

- Conditionally add the dev-dependency `mutagen` (using `do_mutate`
flag)

 This flag is not very well named but `mutagen` and `mutate` are already
 taken?

- Mutate all methods of the `U256` struct that do not require additional
  unit tests.

 Uses `cfg(all(test, do_mutate), mutate)` - I cannot workout why we need
 to check on `test` as well i.e., I don't understand why we cannot use
 `cfg(do_mutate, mutate)`?

With this applied test can be run as usual with a stable toolchain. To
run mutagen we use `RUSTFLAGS='--cfg=do_mutate' cargo +nightly mutagen`.

[0] https://github.com/llogiq/mutagen
2022-12-23 08:23:49 +11:00
Martin Habovstiak e428486002 Add `from_bytes(Vec<u8>)` to `ScriptBuf`
This is useful when one already has bytes allocated in a vec that can be
reused.

The change also documents that the mirror method `into_bytes()` doesn't
allocate.
2022-12-22 17:55:19 +01:00
DanGould c4363e5ba1
Deserialize Psbt fields, don't consensus_encode 2022-12-21 12:24:06 -05:00
DanGould c1dd6ad8a2
Serialize Psbt fields, don't consensus_encode them 2022-12-21 12:19:38 -05:00
DanGould 1b7b08aa5d
De/serialize Psbt without consensus traits 2022-12-21 12:01:20 -05:00
Byron Hambly ed1992ffbf
build(deps): bump secp256k1 to 0.25.0 2022-12-20 12:23:15 +02:00
Andrew Poelstra 77aee43685
Merge rust-bitcoin/rust-bitcoin#1485: Add `tapscript_leaf_hash()` to `Script`
bae264d0c2 Add `tapscript_leaf_hash()` to `Script` (Jiri Jakes)

Pull request description:

  Adds convenience method to `Script` for computing leaf hash of tapscript. Closes #1482.

  The little test case is taken from `bip341_tests.json`.

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

Tree-SHA512: fb7a3a552017208decd56ca7d27eab1987a3a92aae5b8620896b3a02986c2fc13043c200ccfbadf9cfdd2d74af38b0bc25936338f55b7d318c1296acc88bf22a
2022-12-19 21:07:59 +00:00
Andrew Poelstra dc91b87990
Merge rust-bitcoin/rust-bitcoin#1477: Patch hashes and update the code
6acf9ac8b8 Patch hashes and update the code (Martin Habovstiak)

Pull request description:

  This patches `bitcoin_hashes` to use the version in the repository and fixes the code after removal of `Deref`.

ACKs for top commit:
  tcharding:
    ACK 6acf9ac8b8
  apoelstra:
    ACK 6acf9ac8b8

Tree-SHA512: b779fa79309f1d648020146b58e96346b67e9f76e29551cbd50251ea6bb7bcfc4c5d42f49cc7ad5660dcd0a5f6306efe96dfcd9530e4b32c62edf4af7076d830
2022-12-19 20:34:20 +00:00
Andrew Poelstra eaee7c52dd
Merge rust-bitcoin/rust-bitcoin#1486: Fix typos in docs
4a6a12011d Fix typos in docs (Daniela Brozzoni)

Pull request description:

  See #828

ACKs for top commit:
  Kixunil:
    ACK 4a6a12011d
  apoelstra:
    ACK 4a6a12011d

Tree-SHA512: 3d3e2c37479986e51595a506c5310a37e51b9a84f9eb2f17c0217430e8150b7a9a7ee8b9c383df6c4ec581a081ea2a722ed4070ff4ede8d777d3bf2a2c19f15e
2022-12-19 20:24:09 +00:00
Daniela Brozzoni 4a6a12011d
Fix typos in docs
See #828
2022-12-19 09:32:52 +01:00
Jiri Jakes bae264d0c2 Add `tapscript_leaf_hash()` to `Script` 2022-12-19 08:35:35 +01:00
Martin Habovstiak 6acf9ac8b8 Patch hashes and update the code
This patches `bitcoin_hashes` to use the version in the repository and
fixes the code after removal of `Deref`.

This also turns off `AS_DEPENDENCY` check with the intention to refactor
it later.
2022-12-18 14:33:55 +01:00