Commit Graph

193 Commits

Author SHA1 Message Date
Dr. Maxim Orlovsky 86055d9df5
Merge rust-bitcoin/rust-bitcoin#672: New Witness struct to improve ser/de perfomance
106acdc3ac Add fuzzing for Witness struct (Riccardo Casatta)
2fd0125bfa Introduce Witness struct mainly to improve ser/de performance while keeping most usability. (Riccardo Casatta)

Pull request description:

  At the moment the Witness struct is  `Vec<Vec<u8>>`, the vec inside a vec cause a lot of allocations, specifically:

  - empty witness -> 1 allocation, while an empty vec doesn't allocate, the outer vec is not empty
  - witness with n elements -> n+1 allocations

  The proposed Witness struct contains the serialized format of the witness. This reduces the allocations to:

  - empty witness -> 0 allocations
  - witness with n elements -> 1 allocation for most common cases (you don't know how many bytes is long the entire witness beforehand, thus you need to estimate a good value, not too big to avoid wasting space and not too low to avoid vector reallocation, I used 128 since it covers about 80% of cases on mainnet)

  The inconvenience is having slightly less comfortable access to the witness, but the iterator is efficient (no allocations) and you can always collect the iteration to have a Vec of slices. If you collect the iteration you end up doing allocation anyway, but the rationale is that it is an operation you need to do rarely while ser/de is done much more often.

  I had to add a bigger block to better see the improvement (ae860247e191e2136d7c87382f78c96e0908d700), these are the results of the benches on my machine:

  ```
  RCasatta/master_with_block
  test blockdata::block::benches::bench_block_deserialize                 ... bench:   5,496,821 ns/iter (+/- 298,859)
  test blockdata::block::benches::bench_block_serialize                   ... bench:     437,389 ns/iter (+/- 31,576)
  test blockdata::block::benches::bench_block_serialize_logic             ... bench:     108,759 ns/iter (+/- 5,807)
  test blockdata::transaction::benches::bench_transaction_deserialize     ... bench:         670 ns/iter (+/- 49)
  test blockdata::transaction::benches::bench_transaction_get_size        ... bench:           7 ns/iter (+/- 0)
  test blockdata::transaction::benches::bench_transaction_serialize       ... bench:          51 ns/iter (+/- 5)
  test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench:          13 ns/iter (+/- 0)

  branch witness_with_block (this one)
  test blockdata::block::benches::bench_block_deserialize                 ... bench:   4,302,788 ns/iter (+/- 424,806)
  test blockdata::block::benches::bench_block_serialize                   ... bench:     366,493 ns/iter (+/- 42,216)
  test blockdata::block::benches::bench_block_serialize_logic             ... bench:      84,646 ns/iter (+/- 7,366)
  test blockdata::transaction::benches::bench_transaction_deserialize     ... bench:         648 ns/iter (+/- 77)
  test blockdata::transaction::benches::bench_transaction_get_size        ... bench:           7 ns/iter (+/- 0)
  test blockdata::transaction::benches::bench_transaction_serialize       ... bench:          50 ns/iter (+/- 5)
  test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench:          14 ns/iter (+/- 0)
  ```

  With an increased performance to deserialize a block of about 21% and to serialize a block of about 16% (seems even higher than expected, need to do more tests to confirm, I'll appreciate tests results from reviewers)

ACKs for top commit:
  apoelstra:
    ACK 106acdc3ac
  sanket1729:
    ACK 106acdc3ac
  dr-orlovsky:
    utACK 106acdc3ac

Tree-SHA512: e4f23bdd55075c7ea788bc55846fd9e30f9cb76d5847cb259bddbf72523857715b0d4dbac505be3dfb9d4b1bcae289384ab39885b4887e188f8f1c06caf4049a
2021-12-30 01:55:44 +02:00
sanket1729 7d982fa9a2 Add all tests from BIP 371 2021-12-28 20:40:58 +05:30
sanket1729 d22e0149ad Taproot psbt impl BIP 371 2021-12-28 20:40:58 +05:30
Riccardo Casatta 2fd0125bfa
Introduce Witness struct mainly to improve ser/de performance while keeping most usability.
Witness struct is in place of the Vec<Vec<u8>> we have before this commit.

from_vec() and to_vec() methods are provided to switch between this type and Vec<Vec<u8>>

Moreover, implementation of Default, Iterator and others allows to have similar behaviour but
using a single Vec prevent many allocations during deserialization which in turns results in
better performance, even 20% better perfomance on recent block.

last() and second_to_last() allows to access respective element without going through costly Vec
transformation
2021-12-28 09:56:38 +01:00
Martin Habovstiak 779d4110c6 Fixed a bunch of clippy lints, added clippy.toml
This is the initial step towards using and maybe enforcing clippy.
It does not fix all lints as some are not applicable. They may be
explicitly ignored later.
2021-12-21 22:50:13 +01:00
sanket1729 94cfe79170 Rename existing SigHashType to EcdsaSigHashType 2021-12-15 20:00:52 +05:30
Martin Habovštiak ab97d2db1a
Merge pull request #689 from tcharding/module-rustdocs
Clean up module level rustdocs
2021-11-16 13:21:20 +01:00
sanket1729 abc242dfe1
Merge rust-bitcoin/rust-bitcoin#654: Making globals part of PSBT struct. Closes #652
55c627715f Moving globals into PSBT struct (Dr Maxim Orlovsky)

Pull request description:

  I took the most non-invasive approach to reduce diff size. Many parts of the code can be improved in style or further refactored (like some functions are not necessary and can be just moved to be part of other functions), but I'd prefer to do that as a separate PR once this will be merged.

  My approach with this PR:
  1. Remove `Global` struct by moving its fields right into `PartiallySignedTransaction` - but keep the `util/psbt/map/global.rs` file with all its logic
  2. Keep existing `Map for Global` implementation in the same file, but just change it to `Map for PartiallySignedTransaction`
  3. With serialization, convert `Global` deserialization into crate-private function and use it from `PartiallySignedTransaction` deserialization
  4. Refactor the tests and imports as required to get the thing compile and pass tests

  The refactoring will be followed by PR(s) adding support for Taproot

ACKs for top commit:
  apoelstra:
    ACK 55c627715f
  sanket1729:
    ACK 55c627715f . Reviewed range diff with ac0c908 that I previously ACKed

Tree-SHA512: 79b329b6e4e60af905e4e00507d6abc558261d921bcf8f5d4ee34dd685322d7a529b18015423da50a388ba6732b7b662a92bc95ad078228cc809254ad010d467
2021-11-12 11:50:51 -08:00
Tobin Harding 3f5caa501f Clean up module level rustdocs
Docs can always do with a bit of love.

Clean up the module level (`//!`) rustdocs for all public modules.

I claim uniform is better than any specific method/style. I tried to fit
in with what ever was either most sane of most prevalent, therefore
attaining uniformity without unnecessary code churn (one exception being
the changes to headings described below).

Notes:

* Headings - use heading as a regular sentence for all modules e.g.,

```
//! Bitcoin network messages.
```

as opposed to
```
//! # Bitcoin Network Messages
```

It was not clear which style to use so I picked a 'random' mature
project and copied their style.

* Added 'This module' in _most_ places as the start of the module
description, however I was not religious about this one.

* Fixed line length if necessary since most of our code seems to follow
short (80 char) line lengths for comments anyways.

* Added periods and fixed obvious (and sometimes not so obvious)
grammatically errors.

* Added a trailing `//!` to every block since this was almost universal
already. I don't really like this one but I'm guessing it is Andrew's
preferred style since its on the copyright notices as well.
2021-11-06 10:59:53 +11:00
Dr Maxim Orlovsky 55c627715f
Moving globals into PSBT struct 2021-09-16 12:34:04 +02:00
Martin Habovstiak 95fb4e01f9 Document cargo features
This documents cargo features in two ways: explictly in text and in code
using `#[doc(cfg(...))]` attribute where possible. Notably, this is
impossible for `serde` derives. The attribute is contitional and only
activated for docs.rs or explicit local builds.

This change also adds `package.metadata.docs.rs` field to `Cargo.toml`
which instructs docs.rs to build with relevant features and with
`docsrs` config activated enabling `#[doc(cfg(...))] attributes.

I also took the opportunity to fix a few missing spaces in nearby code.
2021-09-14 12:24:57 +02:00
Dr Maxim Orlovsky 994079b099
Refactoring error variants: removing unused; better names & inner types 2021-08-01 21:08:39 +02:00
Devrandom 4826d0c6cc no_std support
Based on the original work by Justin Moon.

*MSRV unchanged from 1.29.0.*

When `std` is off, `no-std` must be on, and we use the [`alloc`](https://doc.rust-lang.org/alloc/) and core2 crates. The `alloc` crate requires the user define a global allocator.

* Import from `core` and `alloc` instead of `std`
* `alloc` only used if `no-std` is on
* Create `std` feature
* Create `no-std` feature which adds a core2 dependency to polyfill `std::io` features. This is an experimental feature and should be
used with caution.
* CI runs tests `no-std`
* MSRV for `no-std` is 1.51 or so
2021-07-15 09:04:49 +02:00
Sebastian b0ae2a6842
Merge pull request #521 from RCasatta/errors_enum
Errors enum improvements
2021-06-15 14:01:56 +02:00
Riccardo Casatta 0eaf45ca81
Use boxed sliced instead of Vec in InvalidPreimageHashPair variant 2021-06-13 22:33:52 +02:00
Riccardo Casatta 781b0e014a
Box big Transaction type in psbt error 2021-06-13 22:33:48 +02:00
Devrandom 95aa3bf153 std -> core 2021-06-11 17:28:04 +02:00
Dr Maxim Orlovsky c0b7defc86
More non-API breaking error derives depending on upstream PRs 2021-05-04 03:58:56 +02:00
Riccardo Casatta c96f7ec71c
use markdown autolink syntax for urls with no text 2021-05-03 13:46:10 +02:00
Dr. Maxim Orlovsky 68096242d3
Merge pull request #594 from RCasatta/capped
Count bytes read in encoding
2021-05-01 16:28:57 +02:00
Riccardo Casatta f692c4a938
Limit bytes read with Take 2021-04-28 09:33:37 +02:00
Dr Maxim Orlovsky b17d7fc31c
Moving keys under `util::ecdsa`, re-exporting them at `util::key`
This is the first step in introducing Schnorr key support as per #588
2021-04-12 14:17:42 +02:00
Steven Roose 7df0d14f1a
Merge pull request #577 from RCasatta/proprietary_key
Include proprietary key in deserialized PSBT
2021-03-15 12:16:30 +00:00
Riccardo Casatta 7e25c133d8
Include proprietary keys in deserialized fields 2021-03-13 15:41:07 +01:00
Riccardo Casatta fcd7200efe
Proprietary key test failing rtt 2021-03-13 15:19:21 +01:00
Dr Maxim Orlovsky 7b7b73f6d2
Improving PSBT FromStr error type 2021-02-22 12:46:22 +01:00
Dr Maxim Orlovsky 79f2729b20
Improving PSBT error tests 2021-02-22 12:46:22 +01:00
Dr Maxim Orlovsky 94102fa597
PSBT Base64 serialization test cases 2021-02-22 12:46:22 +01:00
Dr Maxim Orlovsky 072e1d1b86
PSBT Display & FromStr using Base64 serialization 2021-02-22 12:46:22 +01:00
Antoine Poinsot e36f3a38e4
transaction: deprecate SigHashType::from_u32 in favor of from_u32_consensus
Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
2021-02-19 11:36:44 +01:00
Steven Roose 61918dfe81
Change the signature of consensus_encode to return io::Error's
This is instead of encode::Errors because the encoders should
not be allowed to return errors that don't originate in the writer
they are writing into.

This is a part of the method definition that has been relied upon for a
while already.
2021-01-12 17:39:41 +00:00
Steven Roose 5fd92d1c09 psbt: Serialize maps with byte values using hex 2020-12-30 16:32:52 +01:00
Steven Roose a05f8feec6 serde_util: Rename the btreemap module to btreemap_as_seq
So that we can later distinguish other modules over maps.
2020-12-30 16:32:52 +01:00
Steven Roose a9c13272a0 psbt: Fix serde for maps with non-string keys and binary values 2020-12-30 16:32:52 +01:00
Steven Roose e5f3bca2b4 Add serde_utils module to fix JSON serialization 2020-12-30 16:32:52 +01:00
Steven Roose cdedb0a9d5 psbt::raw: Use hex::format_hex to format the raw key bytes 2020-12-30 16:32:52 +01:00
Steven Roose 94b7371424 Replace serde_struct_impl with derive-based impls 2020-12-30 16:32:52 +01:00
Dr Maxim Orlovsky 7400bccb60
PSBT: proprietary keys type system according to BIP 174 2020-12-21 12:22:35 +01:00
Dr Maxim Orlovsky c3024c3ebb
PSBT: basic support for proprietary keys 2020-12-21 12:17:07 +01:00
Dr Maxim Orlovsky 7f5c2795d6
PSBT global xpub merging algorithm reworked 2020-12-20 11:41:24 +01:00
Dr Maxim Orlovsky b84faa7f5e
PSBT: Improved global keys version and xpub handling 2020-12-16 16:46:28 +01:00
Dr Maxim Orlovsky 21c11e3315
BSPT: Improving global xpub merging algorithm 2020-12-16 16:39:46 +01:00
Dr Maxim Orlovsky 8b1666295c
Nits in new PSBT global types reviews 2020-12-15 16:04:02 +01:00
Dr Maxim Orlovsky 0235abfac2
Improving PSBT merge with dedicated 1.29 rustc borrow scope 2020-12-15 15:50:18 +01:00
Dr Maxim Orlovsky df8635c5fe
PSBT: Key pair serialization for new global keys
Conflicts:
	src/util/psbt/map/global.rs
2020-12-15 15:50:18 +01:00
Dr Maxim Orlovsky 2f838218a8
PSBT: merging new global keys
Plus necessary changes to BIP 32 implementations and error type
2020-12-15 15:47:37 +01:00
Dr Maxim Orlovsky af61d7e7bc
PSBT: adding global types (version, xpub) 2020-12-15 15:47:37 +01:00
Dr Maxim Orlovsky 881f4d75dd
PSBT: Making hash preimage constant names match BIP 174 2020-12-07 16:35:28 +01:00
Dr Maxim Orlovsky 214e10baef
PSBT types are now constants 2020-12-07 15:37:16 +01:00
Dr Maxim Orlovsky 27037e2c72
PSBT: Making RIPEMD160 hash match standard name 2020-12-05 15:46:30 +01:00
Dr Maxim Orlovsky 9b106ee161
PSBT: making BIP32 field name match spec type names 2020-12-05 15:45:20 +01:00
Dr Maxim Orlovsky b5f0e9e144
Fixing serde impl for PSBT inputs 2020-11-06 17:01:34 +01:00
Andrew Poelstra d3210b39fb
Merge pull request #507 from stevenroose/pubkey-write-result
Change PublicKey::write_into to return Result
2020-11-05 19:21:39 +00:00
Andrew Poelstra 35d729d9f0
Merge pull request #478 from sanket1729/psbt_again
Psbt hash preimages (again)
2020-11-05 17:49:58 +00:00
Steven Roose 9699c622fc
Return Result<(), io::Error> from PublicKey::write_into 2020-10-23 21:31:12 +01:00
Dr Maxim Orlovsky 00c3466363
Serde implementation for PSBT 2020-10-14 16:31:24 +02:00
sanket1729 57b63d4b52 Add tests for psbt hash serde 2020-10-11 21:51:03 -05:00
sanket1729 48151ef204 Added Hash preimages to psbt
Added hash preimages to psbt as per updated bip174
2020-10-11 21:51:02 -05:00
Elichai Turkel 2d70623356
Remove deprecated Error::description impl 2020-10-08 17:11:18 +03:00
Dr Maxim Orlovsky c098dfa7f2 BIP32 KeySource refactored to type aliace 2020-09-14 00:37:29 +02:00
Dr Maxim Orlovsky aa67f10162 Introducing `bip32::KeySource`: wrapper for `(Fingerprint, DerivationPath)` 2020-09-13 22:49:52 +02:00
Andrew Poelstra c94295c3a9
Revert "Added hash Preimages to psbt" 2020-09-11 11:31:10 +00:00
sanket1729 c1eafff9ef Added Sighash calculation for psbt 2020-09-11 01:01:33 -05:00
sanket1729 e97dda0ffe Add tests for psbt serde 2020-09-09 12:53:09 -05:00
sanket1729 c5204c001f Added Hash Preimages to psbt
Added hash preimages to psbt as per updated bip174
2020-08-31 17:22:40 -05:00
Dr Maxim Orlovsky 257ca8e504 Using :: for std namespaces in macro 2020-07-21 22:28:05 +02:00
Dr Maxim Orlovsky f6aa8853a0 Namespace hygiene for macros.rs 2020-07-21 22:28:05 +02:00
Andrew Poelstra 7efde3ae47
Merge pull request #419 from elichai/2020-03-description
Deprecate Error::description
2020-05-23 17:26:38 +00:00
Elichai Turkel 654232a3dc
Deprecate Error::description 2020-04-13 02:15:28 +03:00
Elichai Turkel a473d01b17
Made some idiomatic changes 2020-03-29 17:15:15 +03:00
Elichai Turkel 3f2d428706
Remove needless references 2020-03-29 17:15:14 +03:00
Elichai Turkel 16eb81e1f7
Replaced slow vec initialization, and dual calls to hashmap 2020-03-29 17:10:27 +03:00
Elichai Turkel c19b736566
Remove the hex dependency 2020-01-20 18:50:02 +02:00
Dr Maxim Orlovsky ec92a05682 New HashTypes defined according to #284 (WIP), Txid is completed 2020-01-01 13:41:42 +01:00
Steven Roose 0469453da6
Use psbt::Error in PartiallySignedTransaction::from_unsigned_tx 2019-12-09 14:19:07 +00:00
Matt Corallo f1f7718b6c Drop (non-test/serde) hex dep in favor of bitcoin_hashes' fn's 2019-12-05 10:40:59 -05:00
Steven Roose 48f4c1989f
Rename bitcoin_hashes dependency to hashes 2019-08-16 15:52:27 +01:00
Riccardo Casatta 7d6687451a use BTreeMap instead of HashMap to always serialize the same (#310)
* use BTreeMap instead of HashMap to always serialize the same

* fix rust 1.22 error

* psbt fuzz roundtrip

* psbt fuzz roundtrip on our ser
2019-08-09 17:03:12 +02:00
Andrew Poelstra 3b9a94a178 eliminate type parameter from the `Decodable` trait 2019-07-11 17:23:01 +00:00
Andrew Poelstra 42960b959f eliminate type parameter from `Encodable` trait 2019-07-11 17:21:19 +00:00
Andrew Poelstra b734d6488a make consensus_encode return the encoded length 2019-07-11 17:15:32 +00:00
Andrew Poelstra 7e6ad7c893 rename Encoder to WriteExt and Decoder to ReadExt 2019-07-11 15:01:38 +00:00
Andrew Poelstra 24ebc29005 fix some recent rustc warnings 2019-07-11 15:01:38 +00:00
Steven Roose d4282353c9
Two serde quirks from switching dependencies 2019-05-31 10:18:57 +01:00
Carl Dong e5b59120c5 Add copyright notice to PSBT-related files 2019-02-28 11:11:55 -05:00
Carl Dong bc73b315cb Add test vectors from BIP174 specification
- Add macro for decoding and unwrapping PartiallySignedTransaction from
  hex string
2019-02-28 11:11:55 -05:00
Carl Dong 39fd567b56 Add Partially Signed Transaction type
- Add merging logic for PartiallySignedTransactions
- Add (en)decoding logic for PartiallySignedTransaction
- Add converting constructor logic from Transaction for
  PartiallySignedTransaction
- Add extracting constructor logic from PartiallySignedTransaction for
  Transaction

Squashed in fixes from stevenroose <stevenroose@gmail.com>

- Prevent PSBT::extract_tx from panicking
- Make PartiallySignedTransaction fields public
2019-02-28 11:11:55 -05:00
Carl Dong badb0f2a77 Add PSBT input data key-value map type
- Implement psbt::Map trait for psbt::Input
- Add (en)decoding logic for psbt::Input

- Implement PSBT (de)serialization trait for relevant psbt::Input types
2019-02-28 11:11:55 -05:00
Carl Dong 9c08dbae47 Add PSBT output data key-value map type
- Implement psbt::Map trait for psbt::Output
- Add (en)decoding logic for psbt::Output

- Implement PSBT (de)serialization trait for relevant psbt::Output types

- Add macro for merging fields for PSBT key-value maps
- Add macro for implementing decoding logic for PSBT key-value maps
- Add convenience macro for implementing both encoding and decoding
  logic for PSBT key-value maps
- Add macro for inserting raw PSBT key-value pairs into PSBT key-value
  maps
- Add macro for getting raw PSBT key-value pairs from PSBT key-value
  maps
2019-02-28 10:54:53 -05:00
Carl Dong 115f8c043c Add PSBT global data key-value map type
- Implement psbt::Map trait for psbt::Global
- Add converting constructor logic from Transaction for psbt::Global
- Add (en)decoding logic for psbt::Global
  - Always deserialize unsigned_tx as non-witness

- Add trait for PSBT (de)serialization
- Implement PSBT (de)serialization trait for relevant psbt::Global types

- Add macros for consensus::encode-backed PSBT (de)serialization
  implementations
- Add macro for implementing encoding logic for PSBT key-value maps
2019-02-28 10:54:53 -05:00
Carl Dong 2715a6e777 Add trait for PSBT key-value maps 2019-02-28 10:54:53 -05:00
Carl Dong 528e39334c Add data types for raw PSBT key-value pairs
- Add (en)decoding logic for said data types
2019-02-28 10:54:53 -05:00
Carl Dong 4fa39c4a3e Add PSBT-specific Error data type
- Implement psbt::Error data type
- Implement conversion from psbt::Error to util::Error
- Create util::psbt module
- Create non-public util::psbt::error module
2019-02-28 10:54:53 -05:00