Currently in the `bip158` module we do a bunch of dynamic dispatch using
the reader/writer objects. We can improve runtime performance, at the
cost of compile time, by using generics instead of dynamic dispatch.
The function `match_all` takes an iterator, it does not need to use the
`Seek` trait, we can just pass in the content as a slice, no need to
wrap it in a `Cursor`.
Error variants should not end with the same identifier as the enum,
i.e., they should not stutter.
Found by clippy after setting:
avoid-breaking-exported-api = false
Turns out by default clippy does not lint certain parts of the public
API. Specifically, by setting
avoid-breaking-exported-api = false
we can get clippy to lint the public API for various things including
`wrong_self_convention`.
Clippy emits:
warning: methods with the following characteristics: (`to_*` and `self`
type is `Copy`) usually take `self` by value
As suggested, take `self` by value for methods named `to_*`.
870ad59a5e Rename is_finalized to is_finalizable (sanket1729)
aaadd25ddc Add breaking test that allowed incomplete builders to be created (sanket1729)
0b88051318 Update TaprootBuilder::finalize (sanket1729)
5813ec7ac0 Return EmptyTree instead of OverCompleteTree when there are no scripts to add (sanket1729)
Pull request description:
Found while reviewing https://github.com/rust-bitcoin/rust-miniscript/pull/450/ . There is also a BUG fix in the second commit that would have let users spendinfo from incomplete trees.
The bug was introduced in #936 which I am responsible for ACKing
ACKs for top commit:
apoelstra:
ACK 870ad59a5e
Kixunil:
ACK 870ad59a5e
tcharding:
ACK 870ad59a5e
Tree-SHA512: 61442bd95df6912865cbecdc207f330b241e7fbb88b5e915243b2b48a756bea9eb29cb28d8c8249647a0a2ac514df4737bddab695f63075bd55284be5be228ff
This commit does two things:
1) BUG FIX: We should have checked that the length of the branch is 1
before computing the spend_info on it. This was introduced in #936, but
slipped past my review :(
2) Update the return type to return the consumed `self` value. This
function can only error when there the tree building is not complete.
Returning TaprootBuilderError causes issues in downstream projects that
need to deal with error cases which cannot happen in this function
1003ca08e1 Remove needless allocation from BIP-158 encoding (Martin Habovstiak)
Pull request description:
The BIP-158 finalize code was serializing value to `Vec` only to
serialize it to writer right away. Thus the intermediary `Vec` was not
needed.
Even more, the code used `write` which, while correct in case of `Vec`,
could trigger code analysis tools or reviewers.
ACKs for top commit:
tcharding:
ACK 1003ca08e1
sanket1729:
ACK 1003ca08e1
apoelstra:
ACK 1003ca08e1
Tree-SHA512: 01e198726f45ef1b0e4bbe80154674d9db12350281e682531259d1d6467881bc7503cfed30d3837813437c3081a35ba7893c3ae4490d07daf20f1b75dbc62d52
The BIP-158 finalize code was serializing value to `Vec` only to
serialize it to writer right away. Thus the intermediary `Vec` was not
needed.
Even more, the code used `write` which, while correct in case of `Vec`,
could trigger code analysis tools or reviewers.
When debugging parsing errors it's very useful to know some context:
what the input was and what integer type was parsed. `ParseIntError`
from `core` doesn't contain this information.
In this commit a custom `ParseIntError` type is crated that carries the
one from `core` as well as additional information. Its `Display`
implementation displays this additional information as a well-formed
English sentence to aid users with understanding the problem. A helper
function parses any integer type from common string types returning the
new `ParseIntError` type on error.
To clean up the error code a bit some new macros are added and used.
New modules are added to organize the types, functions and macros.
Closes#1113
Add a `LockTime` type to hold the nLockTime `u32` value. Use it in
`Transaction` for `lock_time` instead of a `u32`. Make it public so this
new type can be used by rust-miniscript and other downstream projects.
Add a `PackedLockTime` type that wraps a raw `u32` and derives `Ord`,
this type is for wrapping a consensus lock time value for nesting in
types that would like to derive `Ord`.
74f3a5aeda Run clippy from the test script (Tobin C. Harding)
aa8109a791 Use struct field short form (Tobin C. Harding)
d1a05401f4 Remove redundant calls to clone (Tobin C. Harding)
196492554d Use assert instead of assert_eq (Tobin C. Harding)
3173ef9dbb Remove unnecessary explicit reference (Tobin C. Harding)
Pull request description:
Currently we run clippy in CI using a github action. The invocation has a couple of shortcomings
1. it does not lint the tests (this requires `--all-targets`)
2. it does not lint the examples
I could not find a way to lint the examples without explicitly linting each example by name.
**This PR does the following:**
- Fix clippy issues (patch 1-4)
- Move the clippy control to `test.sh`
- Add an env var `DO_LINT` to control it
- Remove the separate CI job
- Run the linter during the `Test` job using the stable toolchain.
- Run clippy with ` --all-features` AND `--all-targets` (only recently made possible).
- Run each example explicitly
Thanks to dunxen for noticing all the errors in my psbt example on review and prompting me to work out why clippy was not running :)
ACKs for top commit:
apoelstra:
ACK 74f3a5aeda
Kixunil:
ACK 74f3a5aeda
Tree-SHA512: 56dc6262144f4caa5efa6fdc46aeecf7bddc050ef134a639b31a34d4c5e01abcdeda18a00f4ded443866bbdfc982b4e5b67b0089639e0c253e207f0b54777f57
Clippy emits two warnings of form:
warning: redundant field names in struct initialization
Remove the redundant field names and use struct short initialization
form.
Clippy emits various warnings of type:
warning: this expression creates a reference which is immediately
dereferenced by the compiler
As suggested, remove the unnecessary explicit reference.
ee29910911 BIP152: Test net msg ser/der and diff encoding (0xb10c)
cd1aaaf344 BIP152: Add Compact Block unit test based on Elements (Steven Roose)
d4d92a838e BIP152: Add Compact Blocks network messages (Steven Roose)
f2fcdc86e6 BIP152: Add basic Compact Block structures (Steven Roose)
a9a39c4b08 blockdata: Derive PartialOrd, Ord and Hash for BlockHeader (Steven Roose)
Pull request description:
> Adds the basic structures for BIP152 and a method to create a compact block from a full block.
This is a rebase of #249 by stevenroose (see https://github.com/rust-bitcoin/rust-bitcoin/pull/249#issuecomment-1170562141) with a milestone for 29.0. I've added deserialization and serialization tests for the network messages and fixed an off-by-one bug in the deserialization of the differentially encoded varints in the `getblocktxn` message (see https://github.com/rust-bitcoin/rust-bitcoin/pull/249#discussion_r914989521).
Closes#249.
ACKs for top commit:
tcharding:
ACK ee29910911
apoelstra:
ACK ee29910911
Tree-SHA512: 462a91576281f5a2ffdc2610769ea93970b60dac75a150c827966c48daec7cf93f526f9f202e7ba1dbb1410b49148579880260a3c3df298b98330c0d891a4cca
7f498ee2a2 Add docs re Rust 1.51.1 (Tobin C. Harding)
Pull request description:
After auditing the code base for 'msrv' the only remaining mention (excl. md files) is on `unsigned_abs`.
Add docs to save the next guy looking up what version of Rust introduced `unsigned_abs`. (I also added an item to the [msrv tracking issue](https://github.com/rust-bitcoin/rust-bitcoin/issues/1060).)
ACKs for top commit:
Kixunil:
ACK 7f498ee2a2
apoelstra:
ACK 7f498ee2a2
Tree-SHA512: 9b4bdca09d3f7ac1c0584f4eb5207983a064cfe81ed26952d00396b2e0019ef40eee192b7f09d36c68b8c4e1f8de9cac2d1f3ee0946626bae089b46fe38704ab
517059e148 Use u8::try_from (Tobin C. Harding)
Pull request description:
Currently we use a cast to get the `u8` depth, as suggested by the TODO in the code we can now use `TryFrom` since we bumped the MSRV.
Use `u8::try_from.expect("")` since depth (node count) is guarded by `TAPROOT_CONTROL_MAX_NODE_COUNT`.
ACKs for top commit:
Kixunil:
ACK 517059e148
apoelstra:
ACK 517059e148
Tree-SHA512: 98e58617247a05025ec13ad3d00a464cb2351c98c6d871be43b252d3982e291b7187e25780e7fe367f5a3a64fa20f3b328876a8901af4da09e675f50727a26cf
This adds tests for serialization of BIP152 network messages and
tests specifically for the differential encoding of the transaction
indicies of 'getblocktx'. Previously, this code contained an
off-by-one error.
Currently we use a cast to get the `u8` depth, as suggested by the TODO
in the code we can now use `TryFrom` since we bumped the MSRV.
Use `u8::try_from.expect("")` since, assuming the code comment is
correct, the depth is guaranteed to be less that 127.
Replace all instances of
`secp256k1::Message::from_slice(_).expect(_)` with
`secp256k1::Message::from(_)`.
Also adds an implementation of ThirtyTwoByteHash for
TapSighashHash.
Solves https://github.com/rust-bitcoin/rust-bitcoin/issues/824
For internal macros used only in this crate we do not need to use
`macro_use` and pollute the top level namespace now that we have edition
2018. We can add a `pub(crate) use` statement to each and then path
imports work for the macros like normal types.
Now we have edition 2018 we do not need to use `macro_use` or `extern
crate`; `pub use` works with macros.
Remove the extern crate statement and replace it with a pub use
statement. Requires fixing up various imports statements and also
requires importing `sha256t_hash_newtype` macro directly instead of
using a qualified path to it.
73bc2bb058 Remove leading colons from ::core::cmp::Ordering (Tobin C. Harding)
bffe0e840d Remove _most_ leading double colons (Tobin C. Harding)
Pull request description:
Leading double colons are a relic of edition 2015. Attempt to remove _all_ leading double colons (assuming I didn't miss any).
- Patch 1 is done mechanically so it can be repeated by reviewers, just search-and-replace ' ::' with '::' (note the leading space).
- Patch 2 does a single other instance of leading `::`
ACKs for top commit:
apoelstra:
ACK 73bc2bb058
sanket1729:
utACK 73bc2bb058.
Tree-SHA512: 8f7aafdda1aed5b69dcc83f544e65085dfec6590765839f0a6f259b99872805d1f00602fd9deac05c80a5cac3bc222d454dde0117dff4484e5cd34ea930fdfa1
e34bc538c3 Add new type for sequence (Noah Lanson)
Pull request description:
#1082
Created a new type for txin sequence field with methods to create sequences with relative time locks from block height or time units.
ACKs for top commit:
Kixunil:
ACK e34bc538c3
tcharding:
ACK e34bc538c3
apoelstra:
ACK e34bc538c3
Tree-SHA512: 6605349d0312cc36ef9a4632f954e59265b3ba5cfd437aa88a37672fe479688aa4a3eff474902f8cc55848efe55caf3f09f321b3a62417842bfc3ec365c40688
5ce34011f2 Implement std::error::Error for ParseAmount (Tobin C. Harding)
Pull request description:
The `ParseAmountError` does not implement `std::error::Error`, must have been missed when we did the rest.
Implement `std::error::Error` for `ParseAmount`.
ACKs for top commit:
Kixunil:
ACK 5ce34011f2
apoelstra:
ACK 5ce34011f2
Tree-SHA512: 8dafc472b7c23b54d856344e786e0f22e8e179f30f6c1011fbf5f8f0c6b1b5d74ed8e4f2638e5f8246f04dbb429e60027db6fe584d51a78957a6e904feb9e8a3
1e46eeaa88 Upgrade to bitcoin_hashes v0.11.0 (Tobin C. Harding)
Pull request description:
We just released a new version of `bitcoin_hashes`, upgrade the dependency to v0.11.0
ACKs for top commit:
apoelstra:
ACK 1e46eeaa88
Kixunil:
ACK 1e46eeaa88
Tree-SHA512: 32173349c280c255681ebf15a1aa98a28b7016bb813ca907ac55a9797d9d17e1da344e2d8e74c02c2c80d2e6873a03f522c2d55b289b65d7ac55f402b19d689b
24f0441d54 Add PublicKey::to_sort_key method for use with sorting by key (junderw)
Pull request description:
Replaces #524
See previous PR for reasoning.
This solution is a little more straightforward. The name and documentation should be enough to prevent misuse.
We can also impl a to_sort_key for any CompressedKey added later. (or just impl Ord in a BIP67 compliant way)
TODO:
- [x] Add more sorting test vectors. Ideas of edge cases to test are welcome.
ACKs for top commit:
apoelstra:
ACK 24f0441d54
tcharding:
ACK 24f0441d54
Kixunil:
ACK 24f0441d54
Tree-SHA512: 92d68cccaf32e224dd7328edeb3481dd7dcefb2f9090b7381e135e897c21f79ade922815cc766c5adb7ba751b71b51a773d103af6ba13fc081e1f5bfb846b201
36f29d4357 Upgrade to secp256k1 v0.23.0 (Tobin C. Harding)
Pull request description:
We recently released a new version of `rust-secp256k1`, upgrade to use it.
ACKs for top commit:
apoelstra:
ACK 36f29d4357
Kixunil:
ACK 36f29d4357
Tree-SHA512: 46a909dec8bc59daa78acdb76824d93f4f1da0e9736cf6ca443d3bbadfa43867e720293bb7c4919cb0658e75ec59daeffea080611f0e7eed4df439ddac0305de
19ba7ecc03 Add custom error for unknown address type parsing (Arturo Marquez)
Pull request description:
Adds a custom error `UnknownAddressType(_)` which is returned in cases where an unknown address type tries to be parsed via
`FromStr`. This provides more context for the error, since it contains the `String` that tried to be parsed.
Closes https://github.com/rust-bitcoin/rust-bitcoin/issues/1064
ACKs for top commit:
Kixunil:
ACK 19ba7ecc03
dunxen:
ACK 19ba7ec
tcharding:
ACK 19ba7ecc03
apoelstra:
ACK 19ba7ecc03
Tree-SHA512: 2f94eb2e122c1acafcb8c852f7bfe22cb3725806541ae40f82d3a882011fb911ce8fc430153ebea4066f43c5a51813359a4c9b95d2a9077ce8f6dcaa58eee6fd
91ff2f628c Introduce SPDX license identifiers (Tobin C. Harding)
Pull request description:
When `rust-bitcoin` was started in 2014 the SPDX license list and short identifiers where not a thing. Now that we have short identifiers and they are gaining popularity in other projects we can consider using them.
- Add links to the SPDX website in the readme
- Shorten the author section to a single line
- Remove all the licence information in each file and replace it with an
SPDX ID (see https://spdx.dev/ids/#how)
Of note:
- If the author of a file is explicitly listed, maintain this information
- If the 'author' is listed as the generic 'Rust Bitcoin developers' just remove the attribution, this is implicit. This does loose the date info but that can be seen at any time from the git index using
`git log --follow --format=%ad --date default <FILE> | tail -1`
apoelstra, please confirm that I'm not treading on your toes here, especially, are you ok with the new 'written by' string format?
### Ref
- https://spdx.dev/ids/#how
- https://spdx.org/licenses/CC0-1.0.html
- https://spdx.dev/ids/
ACKs for top commit:
apoelstra:
ACK 91ff2f628c
sanket1729:
ACK 91ff2f628c. I am also in IDGAF camp, but I like more red lines in diff.
Kixunil:
ACK 91ff2f628c
Tree-SHA512: ca8aac00f015c18ec18de83dfeb50dd6f4f840653c7def85daa2436a339021ada5f3c34ad0cdf6b18e3e39c45a6d58a8313742e4001d467785b10eee7fdbc938
Adds a custom error `UnknownAddressType(_)` which is returned in
cases where an unknown address type tries to be parsed via
`FromStr`. This provides more context for the error.
For more info see [1].
[1] - `https://github.com/rust-bitcoin/rust-bitcoin/issues/1064`
Leading double colons are a relic of edition 2015. Remove all leading
double colons that follow a space, done like this so that reviewers can
do the same and verify the diff. Done with
search-and-replace ' ::' '::'
And, for the record:
```bash
function search-and-replace() {
if (($# != 2))
then
echo "Usage: $0 <this> <that>"
return
fi
local this="$1"
local that="$2"
for file in $(git grep -l "$this")
do
perl -pi -e "s/$this/$that/g" "$file"
done
}
```
4d2291930b Use fragment-specifier literal (Tobin C. Harding)
Pull request description:
Currently we are using the fragment-specifier `expr` in a bunch of
macros for captures that are only used for literals. If we use `literal`
instead it allows the compiler to give slightly more specific error
messages.
The benefits of this change are minor. Of note, this patch found one
unusual macro call site (removed unnecessary `&`).
The macros changed are all internal macros, this is not a breaking change.
ACKs for top commit:
Kixunil:
ACK 4d2291930b
apoelstra:
ACK 4d2291930b
Tree-SHA512: 51c109fe3a884191bf623508555c1d5ad337a3f3b48538d18aec13e581f2c5fbbd055be49600ced19f38541412c34090bd8bac61fd05d5aa9702c96ff521364f
1fea098dfb Support unsized `R` and `W` in consensus encode/decode (Dawid Ciężarkiewicz)
a24a3b0194 Forward `consensus_decode` to `consensus_decode_from_finite_reader` (Dawid Ciężarkiewicz)
9c754ca4de Take Writer/Reader by `&mut` in consensus en/decoding (Dawid Ciężarkiewicz)
Pull request description:
Fix#1020 (see more relevant discussion there)
This definitely makes the amount of generics compiler
has to generate by avoding generating the same functions
for `R`, `&mut R`, `&mut &mut R` and so on.
old:
```
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 9947832 Jun 2 22:42 target/release/deps/bitcoin-07a9dabf1f3e0266
> strip target/release/deps/bitcoin-07a9dabf1f3e0266
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 4463024 Jun 2 22:46 target/release/deps/bitcoin-07a9dabf1f3e0266
```
new:
```
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 9866800 Jun 2 22:44 target/release/deps/bitcoin-07a9dabf1f3e0266
> strip target/release/deps/bitcoin-07a9dabf1f3e0266
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 4393392 Jun 2 22:45 target/release/deps/bitcoin-07a9dabf1f3e0266
```
In the unit-test binary itself, it saves ~100KB of data.
I did not expect much performance gains, but turn out I was wrong(*):
old:
```
test blockdata::block::benches::bench_block_deserialize ... bench: 1,072,710 ns/iter (+/- 21,871)
test blockdata::block::benches::bench_block_serialize ... bench: 191,223 ns/iter (+/- 5,833)
test blockdata::block::benches::bench_block_serialize_logic ... bench: 37,543 ns/iter (+/- 732)
test blockdata::block::benches::bench_stream_reader ... bench: 1,872,455 ns/iter (+/- 149,519)
test blockdata::transaction::benches::bench_transaction_deserialize ... bench: 136 ns/iter (+/- 3)
test blockdata::transaction::benches::bench_transaction_serialize ... bench: 51 ns/iter (+/- 8)
test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench: 5 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_size ... bench: 3 ns/iter (+/- 0)
```
new:
```
test blockdata::block::benches::bench_block_deserialize ... bench: 1,028,574 ns/iter (+/- 10,910)
test blockdata::block::benches::bench_block_serialize ... bench: 162,143 ns/iter (+/- 3,363)
test blockdata::block::benches::bench_block_serialize_logic ... bench: 30,725 ns/iter (+/- 695)
test blockdata::block::benches::bench_stream_reader ... bench: 1,437,071 ns/iter (+/- 53,694)
test blockdata::transaction::benches::bench_transaction_deserialize ... bench: 92 ns/iter (+/- 2)
test blockdata::transaction::benches::bench_transaction_serialize ... bench: 17 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench: 5 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_size ... bench: 4 ns/iter (+/- 0)
```
(*) - I'm benchmarking on a noisy laptop. Take this with a grain of salt. But I think
at least it doesn't make anything slower.
While doing all this manual labor that will probably generate conflicts,
I took a liberty of changing generic type names and variable names to
`r` and `R` (reader) and `w` and `W` for writer.
ACKs for top commit:
RCasatta:
ACK 1fea098dfb tested in downstream lib, space saving in compiled code confirmed
apoelstra:
ACK 1fea098dfb
Tree-SHA512: bc11994791dc97cc468dc9d411b9abf52ad475f23adf5c43d563f323bae0da180c8f57f2f17c1bb7b9bdcf523584b0943763742b81362880206779872ad7489f
When `rust-bitcoin` was started in 2014 the SPDX license list and short
identifiers where not a thing. Now that we have short identifiers and
they are gaining popularity in other projects we can consider using
them.
- Add links to the SPDX website in the readme
- Shorten the author section to a single line
- Remove all the licence information in each file and replace it with an
SPDX ID (see https://spdx.dev/ids/#how)
Of note:
- If the author of a file is explicitly listed, maintain this
information
- If the 'author' is listed as the generic 'Rust Bitcoin developers'
just remove the attribution, this is implicit. This does loose the date
info but that can be seen at any time from the git index using
`git log --follow --format=%ad --date default <FILE> | tail -1`