Commit Graph

3065 Commits

Author SHA1 Message Date
Andrew Poelstra 279ae365d2
Merge rust-bitcoin/rust-bitcoin#1333: Move amount module out of util
b84e1d46c0 Move amount module out of util (Tobin C. Harding)

Pull request description:

  Done as part of flattening the `util` module. Simply move the `amount` module out of the `util` module and to the crate root. Justified by the fact that the `Amount` type is more-or-less a "primitive" bitcoin type.

ACKs for top commit:
  apoelstra:
    ACK b84e1d46c0
  Kixunil:
    ACK b84e1d46c0
  sanket1729:
    ACK b84e1d46c0

Tree-SHA512: 9ec707f49b7ab29f573be22b366d2ea9c1a8e4b27e80350d521b9c6607fca4142f079648cb739ba8590edd97c21a00029c3647c4c8cebe47cc2dfee1b10b8b39
2022-11-05 13:23:06 +00:00
Tobin C. Harding 8c4fa5ec3d Remove into_bytes from impl_array_newtype
All the types that we define with `impl_array_newtype` are
`Copy` so the correct conversion method to get the underlying byte array
is `to_bytes`. We currently provide `into_bytes` as well as `to_bytes`,
with one of them calling `clone` - this is unnecessary and against
convention.

- Remove `into_bytes` and for `to_bytes` just return the inner field.
- Add a method that causes build to fail if `Copy` is not implemented.
2022-11-05 06:57:40 +11:00
Andrew Poelstra b27169979b
Merge rust-bitcoin/rust-bitcoin#1334: Rename `hash` module to `merkle_root`
29df410ea3 Document state after call to calculate_root_inline (Tobin C. Harding)
2dbc7fdf21 Rename merkle_root functions (Tobin C. Harding)
22dd904735 Rename util::hash module (Tobin C. Harding)

Pull request description:

  Done as part of flattening `util`.

  The `util::hash` module only provides two functions, both to calculate the merkle root of a list of hashes.

  1. Rename `util::hash` -> `crate::merkle_root`
  2. Change function names to `calculate[_inline]` so usage becomes `merkle_root::calculate`

  Done as two separate patches so we can bikeshed the names, can squash if needed.

ACKs for top commit:
  Kixunil:
    ACK 29df410ea3
  apoelstra:
    ACK 29df410ea3

Tree-SHA512: 17ace90c7700b5d7adf8b95731c9a348b5c92863806cc88bc40730547f457e44160efb19985e025970b59fea86d68f0bf4be0af17717a65ae44f11c8d10ec4c6
2022-11-04 14:42:02 +00:00
sanket1729 932aaaa88a
Merge rust-bitcoin/rust-bitcoin#1367: Do clippy improvements
1050fe9cae Remove unnecessary borrow (Tobin C. Harding)
3966709336 Use is_none() (Tobin C. Harding)
d192052519 Remove unnecessary dereference (Tobin C. Harding)
624cda07b3 Remove unnecessary casts (Tobin C. Harding)

Pull request description:

  Clippy has been updated and new warnings are being triggered in our codebase. This PR does all warnings using nightly since they all looked like reasonable things to fix.

  Needed for CI to pass in other open PRs.

ACKs for top commit:
  Kixunil:
    ACK 1050fe9cae
  sanket1729:
    ACK 1050fe9cae.

Tree-SHA512: 7dcfb6a72a0aae51b49b417bb94cbe1becb1095d1bf0011921b1834a10f792cfcdeee37993ab9b103bd2dfcc9cd3c26cd7f1bb80b06b0d1aa4aaa454bfb0b3f0
2022-11-04 03:17:49 -07:00
sanket1729 2be133c577
Merge rust-bitcoin/rust-bitcoin#1359: Add a test parsing transaction with a huge witness
d6ca7e4b9f Add a test parsing transaction with a huge witness (Martin Habovstiak)

Pull request description:

  This transaction broke past versions of `rust-bitcoin` and LND so this adds a test to avoid reintroducing the problem in the future.

  See also https://github.com/romanz/electrs/issues/783

  I'm publishing this immediately for research purposes. I can clean it up later if required (low on time rn) or we may even not merge it if there is a better test.

ACKs for top commit:
  tcharding:
    ACK d6ca7e4b9f

Tree-SHA512: cfa9f5c82be0885a82bddb0c15f3177e05feedb369a931e58bf48d90b26eab85bc501d84d17927fa72b42ad4f1016e6042ad318662403271c738eaa91cee7748
2022-11-04 01:00:18 -07:00
sanket1729 c14cb06d1e
Merge rust-bitcoin/rust-bitcoin#1361: Add a CHANGELOG entry for 0.28.2
cc0b1f1804 Add a CHANGELOG entry for 0.28.2 (Matt Corallo)

Pull request description:

  I'm not actually sure if we want this - we don't seem to (consistently) add entries for point releases, but figured I'd put it up in case we want it.

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

Tree-SHA512: b41f647cfea925a5acd8eaa8568692e7393dabbfd6c75e9dad9e18f34847353363cadaad9109b41a7b1e5851030fefc0180e6b824a2eaaf8f58b4d62fd4bc68f
2022-11-04 00:58:33 -07:00
Tobin C. Harding 1050fe9cae Remove unnecessary borrow
Clippy emits:

 warning: the borrowed expression implements the required traits

As suggested, remove the unnecessary explicit borrow.
2022-11-04 11:49:14 +11:00
Tobin C. Harding 3966709336 Use is_none()
Clippy emits:

 warning: binary comparison to literal `Option::None`

As suggested, use `find.is_none()` instead of comparison with `None`.
2022-11-04 11:47:34 +11:00
Tobin C. Harding d192052519 Remove unnecessary dereference
Clippy emits:

 warning: deref which would be done by auto-deref

As suggested, remove the unnecessary deref (`*`).
2022-11-04 11:45:57 +11:00
Tobin C. Harding 624cda07b3 Remove unnecessary casts
Clippy emits a bunch of warnings of form:

 warning: casting to the same type is unnecessary ...

As suggested, remove the unnecessary casts.
2022-11-04 11:44:23 +11:00
junderw 3c0d5aed73
Add get_tapscript to Witness
This new method will check the last witness element to see if it starts with 0x50, and
depending on the result it will return the second to last or third to last witness
element according to BIP341.

In its current state, Witness can not know what type of script it is fulfilling,
so it is up to the caller to verify if the previous output is a taproot output or not.
2022-11-03 11:48:19 +09:00
junderw 4226d60205
Add Index<usize> and nth(index) to Witness
Arbitrary indexing into Witness fixes the API of last and second_to_last to be more flexible.
This patch started off as an addition of third_to_last, but ended up evolving
into arbitrary indexing to allow for future use cases.

A list of the indices of the start byte for each witness element is stored as an ordered
contiguous group of u32s represented as 4 bytes each in the Vec<u8> contents.
The bytes are stored using to_ne_bytes for performance reasons. A helper function is added
to the tests to allow for easier contruction of the contents Vec in test vectors. u32 was
chosen because 22 bits are needed to store 4,000,000 which is the maximum weight limit for
a block. This might need to be reworked in the event of consensus limits increasing, but
u32 can hold 1000x the current limit, so it should be fine for the forseeable future.

The push and consensus_deserialize functions utilize rotate_left and rotate_right to move
the indices to the end of the new allocation. Depending on the size of the data, this
might be more of a performance hit than just allocating a new temporary Vec to store the
indices and append them after parsing is completed. However, for a majority of cases
rotating the indices should be faster. Suggestions to use VecDeque instead of Vec for
contents necessitate other considerations, since it is not a public facing change,
those optimizations can be dealt with in future patches.

The Index<usize> trait is implemented using the new nth method with expect.

The Iter struct is reworked to make use of the new data representation. This new data
structure makes it trivial to implement DoubleEndedIterator and other such traits, but
I have decided to leave this as out of scope for this patch.
2022-11-03 11:09:53 +09:00
Matt Corallo cc0b1f1804 Add a CHANGELOG entry for 0.28.2 2022-11-02 00:19:26 +00:00
Martin Habovstiak d6ca7e4b9f Add a test parsing transaction with a huge witness
This transaction broke past versions of `rust-bitcoin` and LND so this
adds a test to avoid reintroducing the problem in the future.

See also https://github.com/romanz/electrs/issues/783
2022-11-01 14:27:38 +01:00
Tobin Harding 962abcc963 Add serde regression tests
In order that we can safely change/maintain de/serialization code we
need to have regression tests with hard coded serializations for each
type that implements serde.

It is enough to test a single serde data format, use JSON for `opcodes`
and bincode for other types.

Do regression testing in a newly added `tests` module.
2022-10-29 10:47:16 +11:00
Tobin C. Harding 4e9ff972ad Improve checksum documentation
Improve the wording describing the base58 checksum.
2022-10-29 10:35:08 +11:00
Tobin C. Harding 0f01cb9f51 Use rustdoc summary
Rustdoc comment is too long, use a summary and a longer section as is
convention.
2022-10-29 10:35:08 +11:00
Tobin C. Harding 6151d4c841 base58: Rename public functions
The `base58` module is for encoding and decoding, it makes sense for the
public functions to be called `encode` and `decode`. We also have some
functions that operate on data with a checksum, for these it makes sense
to tack `check` onto the _end_ of the function name.

With this applied the public API is:

- decode
- decode_check
- encode
- encode_check
- encode_check_to_fmt
2022-10-29 10:35:08 +11:00
Tobin C. Harding a94af5c052 base58: Re-order code
Code is arguably easier to read if the most important stuff comes first.
In the old days, when writing C, we had to put definitions before they
were used but in Rust this is not the case

Re-order the `base58` file so that the public API functions are up the top
then other helper functions are defined _after_ they are called.

Refactor only, no logic changes.
2022-10-29 10:35:08 +11:00
Tobin C. Harding d362e6286a base58: Improve rustdocs
Improve the rustdocs by doing:

- Use full sentences
- Use typical project line length
- Use third person tense for functions
2022-10-29 10:35:08 +11:00
Tobin C. Harding a43234e7ab base58: Make SmallVec methods private
The `SmallVec` type is private, it does not need public methods.
2022-10-29 10:35:08 +11:00
Tobin C. Harding 27f2cba623 base58: Use alternate form to print hex
Currently we are manually adding `0x` in calls to `write!`, this is
unnecessary since the alternate form already adds the `0x`.

Was verified with
```
    #[test]
    fn bad_checksum_error_hex_format() {
        let want = "invalid base58 character 0xab";
        let got = format!("{}", Error::BadByte(0xAB));
        assert_eq!(got, want)
    }
```

Use alternate form to print hex.
2022-10-29 10:35:08 +11:00
Tobin C. Harding f659a7aca3 base58: Remove key related errors
The key related errors are incorrect because they are circular, we have
a base58 error variant in `key::Error` and two key error variants in
`base58::Error`.

Remove the key errors from the `base58::Error` type.
2022-10-29 10:33:14 +11:00
Tobin C. Harding 29df410ea3 Document state after call to calculate_root_inline
The function call `calculate_root_inline` calculates the merkle root
using the input array as a scratch buffer, i.e., we trash the data
during recursive calls to `merkle_root_r`.

Add explicit documentation to the function so its super clear not to use
the hashes again after calling this function.
2022-10-29 07:50:09 +11:00
Tobin C. Harding 2dbc7fdf21 Rename merkle_root functions
Recently we renamed the `hash` module to `merkle_root`, this makes the
public functions provided stutter if used with one layer of path as is
Rust convention:

 `merkle_root::bitcoin_merkle_root`

We can improve on this by renaming the functions to 'calculate', then we
get

- `merkle_root::calculate()`
- `merkle_root::calculate_inline()`
2022-10-29 07:45:48 +11:00
Tobin C. Harding 22dd904735 Rename util::hash module
The `util::hash` module provides two functions for computing a merkle
root from a list/iterator of hashes.

Rename the module to `merkle_root` and move it to the crate root,
deprecate the original functions.

Done as part of flattening the `util` module.
2022-10-29 07:45:30 +11:00
Andrew Poelstra d8e94cf181
Merge rust-bitcoin/rust-bitcoin#1354: Add `impl IntoIterator for &'_ Witness`
d2ed0fe022 Add `impl IntoIterator for &'_ Witness` (Martin Habovstiak)

Pull request description:

  It is considered idiomatic for types that have `iter()` method to also implement `IntoIterator` for their references. `Witness` was missing this so it is added here.

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

Tree-SHA512: fc891109696de4f349324d6ddc160249ef22510622d9ce72a65b18f085d86b0de0f3ecb4f7060e1eaf716a908029865cd21cda5a6598fc4c16d0540152d9a4c9
2022-10-28 20:42:47 +00:00
Andrew Poelstra bbf89dd5a4
Merge rust-bitcoin/rust-bitcoin#1223: Remove the endian module
2674327c93 Remove the endian module (Tobin C. Harding)

Pull request description:

  Now we have MSRV of 1.41.1 we can use the `from_le_bytes` and `to_be_bytes` methods implemented on standard integer types, these became available in Rust 1.32.

  Remove the `endian` module replacing its logic with calls to methods on the respective stdlib integer types.

ACKs for top commit:
  Kixunil:
    ACK 2674327c93
  apoelstra:
    ACK 2674327c93

Tree-SHA512: 7cdaf278c9d162cb0080bb6b9ea80ab55f881bfcd389f8b968f8cfaeebb0d27d3b5b46e9677a376bc6b7d4068cf094f50560ed4ae7bc817c50da688f70a7af25
2022-10-28 19:04:59 +00:00
Martin Habovstiak d2ed0fe022 Add `impl IntoIterator for &'_ Witness`
It is considered idiomatic for types that have `iter()` method to also
implement `IntoIterator` for their references. `Witness` was missing
this so it is added here.
2022-10-28 13:07:08 +02:00
sanket1729 21ae79bed4
Merge rust-bitcoin/rust-bitcoin#935: Add psbt BIP174 test vectors tests
b8bd31d5a8 Promote rust-miniscript finalizer (DanGould)
16bf6f68dd Test PSBT integration (DanGould)
6b6ef528a4 Add OP_0 alias for OP_PUSHBYTES_0 (DanGould)
72935a0f6e Move test_data/* tests/data (Tobin Harding)

Pull request description:

  resolves #892

  *  Initial patch adds OP_0 alias for OP_PUSHBYTES_0 as [approved here](https://github.com/rust-bitcoin/rust-bitcoin/pull/935#discussion_r854539416)
  * Second patch is the bulk of this work. It tests BIP 174 PSBT integration defined at [The BIP's Test Vectors Section](https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#test-vectors) using #957
  * Third patch points users to rust-miniscript for a more useful finalizer

ACKs for top commit:
  tcharding:
    ACK b8bd31d5a8
  sanket1729:
    ACK b8bd31d5a8. Thanks for sticking with this PR. This looks awesome after #957

Tree-SHA512: dc68e524d4349530b082baf5032460fa56593b0ef192125c0b7d7e00954e5593f386b7f1984fc00106b4b9eafbf29cc80ab368dbd26b710eba0962dbd89e0013
2022-10-27 22:28:00 -07:00
Tobin C. Harding 2674327c93 Remove the endian module
Now we have MSRV of 1.41.1 we can use the `from_le_bytes` and
`to_be_bytes` methods, these became available in Rust 1.32.

Remove the `endian` module replacing its logic with calls to methods on
the respective stdlib integer types.
2022-10-28 11:01:57 +11:00
Tobin C. Harding 9a1623c1dc Re-export hashbrown when enabled
`hashbrown` used to be exported until
commit 23ee0930c7 which removed the `pub
extern crate` declaration.
2022-10-28 10:59:30 +11:00
DanGould b8bd31d5a8
Promote rust-miniscript finalizer 2022-10-27 10:40:45 -04:00
DanGould 16bf6f68dd
Test PSBT integration
Implement Test Vectors from BIP 174

Co-authored-by: Tobin Harding <me@tobin.cc>
Co-authored-by: Armin Sabouri <armins88@gmail.com>
2022-10-27 10:40:45 -04:00
DanGould 6b6ef528a4
Add OP_0 alias for OP_PUSHBYTES_0 2022-10-27 10:40:45 -04:00
Tobin Harding 72935a0f6e
Move test_data/* tests/data
In preparation for adding integration tests in the standard Rust
`tests/` directroy; move the contents of `test_data` to `tests/data`.
2022-10-27 10:40:44 -04:00
sanket1729 bd80ecd92d
Merge rust-bitcoin/rust-bitcoin#1345: Fix broken build due to conflict between #1340 and #999
c4084b91fb Fix broken build due to conflict between #1340 and #999 (Andrew Poelstra)

Pull request description:

  I will test merge commits more thoroughly before signing off on them in future, sorry.

ACKs for top commit:
  DanGould:
    tACK c4084b91fb
  tcharding:
    ACK c4084b91fb
  sanket1729:
    ACK c4084b91fb

Tree-SHA512: 51ece3aa43045e81138d21b8402b1ec1559a0b37bdfc4c5246ff46fd085364517449a2e20e625934cbc0c96f18eb2fc6121a6e993fd5b9535ae54c863d032a0b
2022-10-27 02:06:55 -07:00
Andrew Poelstra c4084b91fb
Fix broken build due to conflict between #1340 and #999
I will test merge commits more thoroughly before signing off on them in future, sorry.
2022-10-25 22:04:38 +00:00
Andrew Poelstra 391cfd67bd
Merge rust-bitcoin/rust-bitcoin#1342: Re-export base64 when enabled
dea9b1d1e0 Re-export base64 when enabled (Alekos Filini)

Pull request description:

  `base64` used to be exported until commit 23ee0930c7 which removed the `pub extern crate` declaration.

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

Tree-SHA512: 2f32b6676aab9881bab9eb0ae61910ec0d4b60cb17c8a7bf8155ec4a13e50abce0061b52f4e81b106b938e99cb68329d027291c1702213cfa2a46734ebadb488
2022-10-25 19:33:51 +00:00
Andrew Poelstra 9fb8c21f79
Merge rust-bitcoin/rust-bitcoin#999: examples: Add taproot PSBT example workflow
1a89d5230c examples: Add taproot-psbt workflow example (Duncan Dean)

Pull request description:

  Will address #893.

  Currently includes a BIP86 example (no spendable script path)

  Working on script path and key path spending when both are possible spending paths.

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

Tree-SHA512: 31d23914faedb2632d517f9a827075f1bf387df6d98f151000f70546d1e67ac332e698c6a01bda40a9baf5b25bff0114928edc0879c5e01753ecfc6ad182fe26
2022-10-25 16:47:18 +00:00
Martin Habovstiak b6f9e47dba Fix `no_std` when `bitcoinconsensus` is enabled
`default-features = false` was missing previously but blindly adding it
would lead to subtle risk of breaking when a crate not needing `std`
depends on `bitcoinconsensus` and simultaneously another crate not
needing `bitcoinconsensus` depends on `std` and another crate depends on
them both.

This change fixes it by introducing `bitcoinconsensus-std` feature flag
and provides a fallback if the flag is off. Unfortunately the fallback
has to use a bit of reasonable `unsafe` due to limitations of upcasting.

The only safe alternatives are not do it and provide worse experience
for crates that are affected by the problem above or break the API,
which couldn't be backported and would be more annoying to use.

Closes #1343
2022-10-25 15:01:58 +02:00
Alekos Filini dea9b1d1e0
Re-export base64 when enabled 2022-10-25 11:02:02 +02:00
Andrew Poelstra 03d67dd1da
Merge rust-bitcoin/rust-bitcoin#1340: Move sighash module to crate root
fd7f8daeff Move sighash module to crate root (Tobin C. Harding)

Pull request description:

  Done as part of the effort to flatten the `util` module.

  The `sighash` module can stand alone in the crate root, it provides a discreet set of functionality - the `SighashCache` and associated types.

  Marking as high priority because this is part of flattening `util` which is a required step before we start crate smashing.

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

Tree-SHA512: e812ca903f7dccfa5a06084e23f93f617d016583bdf082d7a36ca8e67e49f1d140a3e138b93939e816861460ff2c04d49d5e37a555dd853dca1c76dbccd910bf
2022-10-25 00:57:43 +00:00
Andrew Poelstra f9146d0391
Merge rust-bitcoin/rust-bitcoin#1341: ci: Remove serde version pinning
b05ba16a05 ci: Remove serde version pinning (Tobin C. Harding)

Pull request description:

  The MSRV break in serde is fixed now, remove the serde version pinning.

  Fix: #1256

ACKs for top commit:
  Kixunil:
    ACK b05ba16a05 if CI passes.
  apoelstra:
    ACK b05ba16a05

Tree-SHA512: 2046b443500a848cb7b22576473f3b09ece4538dd3d30d7a06b09b28d6fe26f5c4d482f70ec0ec1b79469c221dc9c09e8b05b83debb71fd27f7dc30571cbbcfe
2022-10-25 00:20:04 +00:00
Andrew Poelstra 29d3bc0108
Merge rust-bitcoin/rust-bitcoin#1338: Implement PartiallySignedTransaction::fee
c34d5f8f85 Implement PartiallySignedTransaction::fee (hashmap)

Pull request description:

  to calculate fee if previous outputs are available.
  Closes https://github.com/rust-bitcoin/rust-bitcoin/issues/1220

ACKs for top commit:
  Kixunil:
    ACK c34d5f8f85 if CI passes
  tcharding:
    ACK c34d5f8f85
  apoelstra:
    ACK c34d5f8f85

Tree-SHA512: 697b837de2fb21bbd5d489c524c06a56bb35b73c0f32cc5b0500f5508f3c539b21d327cd556a04ee847ccf8d98829da994d90c19e80c457ddba2cd9d3469476e
2022-10-24 23:32:25 +00:00
Tobin C. Harding b05ba16a05 ci: Remove serde version pinning
The MSRV break in serde is fixed now, remove the serde version pinning.
2022-10-25 09:32:01 +11:00
Tobin C. Harding fd7f8daeff Move sighash module to crate root
Done as part of the effort to flatten the `util` module.

The `sighash` module can stand alone in the crate root, it provides a
discreet set of functionality - the `SighashCache` and associated types.
2022-10-25 09:14:01 +11:00
Andrew Poelstra afcdfa4a59
Merge rust-bitcoin/rust-bitcoin#1295: Remove usage of `opcodes::all`
c3e4399519 Remove usage of opcodes::all (Tobin C. Harding)

Pull request description:

  We have all of the opcodes defined in a submodule called `all`, this allows wildcard imports without bringing in the other types in the `opcodes` module.

  Use wildcard import `use crate::blockdata::opcodes::all::*` instead of fully qualifying the path to opcodes.

  ### Original PR description (left here so the thread of discussion below makes sense)

  The `all` module adds no value, we can remove it without loss of meaning or clarity, doing so makes the code less verbose.

  EDIT: After review, now includes importing with wildcard and removing the `opcodes::` path from any type starting with `OP_`.

  Idea stolen from: https://github.com/rust-bitcoin/rust-bitcoin/pull/525 (patch 7)

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

Tree-SHA512: 300511d909a25e82c563b18da1b52bcf65653cd3efd8ff32dd5b9e839dacd57924953c1745dfb5e9301fa4f9fc0cd61a075f3a3fd94f6a5a9730bca5155dfd96
2022-10-24 13:32:14 +00:00
hashmap c34d5f8f85
Implement PartiallySignedTransaction::fee
to calculate fee if previous outputs are available.
2022-10-24 14:39:32 +02:00
Tobin C. Harding c3e4399519 Remove usage of opcodes::all
We have all of the opcodes defined in a submodule called `all`, this
allows wildcard imports without bringing in the other types in the
`opcodes` module.

Use wildcard import `use crate::blockdata::opcodes::all::*` instead of
fully qualifying the path to opcodes.
2022-10-24 13:08:47 +11:00