Commit Graph

48 Commits

Author SHA1 Message Date
Tobin C. Harding 1e46eeaa88 Upgrade to bitcoin_hashes v0.11.0 2022-07-13 09:38:05 +10:00
Andrew Poelstra 5d06177644
Merge rust-bitcoin/rust-bitcoin#1076: Introduce SPDX license identifiers
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
2022-07-11 15:11:03 +00:00
Tobin C. Harding 91ff2f628c Introduce SPDX license identifiers
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`
2022-06-29 14:12:02 +10:00
Dawid Ciężarkiewicz 1fea098dfb Support unsized `R` and `W` in consensus encode/decode 2022-06-28 18:49:17 -07:00
Dawid Ciężarkiewicz 9c754ca4de Take Writer/Reader by `&mut` in consensus en/decoding
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.
2022-06-23 15:55:14 -07:00
Tobin C. Harding dca0d67771 Fix in preparation for next edition
Use cargo to upgrade from edition 2015 to edition 2018.

 cargo fix --edition

No manual changes made. The result of the command above is just to fix
all the use statements (add `crate::`) and fix the fully qualified path
formats i.e., `::Foo` -> `crate::Foo`.
2022-05-11 10:16:17 +10:00
Tobin Harding 71cf00a314 Use less vertical lines
In this library we specifically do not use rustfmt and tend to favour
terse statements that do not use extra lines unnecessarily. In order to
help new devs understand the style modify code that seems to use an
unnecessary number of lines.

None of these changes should reduce the readability of the code.
2022-03-14 13:52:13 +11:00
Tobin Harding 702e8bf82d Refactor consensus_encode
The implementations of `consensus_encode` use an unnecessary number of
lines. Favour more terse code with no loss of clarity.
2022-03-14 13:52:13 +11: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
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
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
Devrandom 95aa3bf153 std -> core 2021-06-11 17:28:04 +02: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 767b14f696
Make Inventory and NetworkMessage enums exhaustive
Both by added an `Unknown` variant.
2020-12-21 12:04:26 +00:00
Steven Roose f1b0f263ce
Implement PartialOrd and Ord for Inventory 2020-11-10 19:54:07 +00:00
Jake Rawsthorne c7987d8fcd
Add wtxidrelay message and WTx inv type, document PROTOCOL_VERSION 2020-10-07 20:03:15 +01:00
Jake Rawsthorne 81c061a68c Change WitnessTransaction inv type to use txid 2020-04-14 14:11:44 +01: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 5fc24dea33 Multiple fixes for hash types and their computing
Unit test for wtxid and SegWit transactions
2020-01-01 13:54:23 +01:00
Dr Maxim Orlovsky f5a8087105 New hash types: MerkleRoot/Branch, WitnessCommit, SigHash, FilterHash 2020-01-01 13:50:17 +01:00
Steven Roose 024557fe47
Merge pull request #357 from stevenroose/command-str
Various optimizations of the network code
2019-12-11 15:38:52 +00:00
Steven Roose fe3397399e
Add Copy to InvType enum 2019-12-08 20:58:52 +00:00
Steven Roose b4c4a9658d
Implement std:#️⃣:Hash for Inventory 2019-12-08 17:49:11 +00:00
Steven Roose 48f4c1989f
Rename bitcoin_hashes dependency to hashes 2019-08-16 15:52:27 +01: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
Carl Dong 99f63a8ca4 Convert codebase from util::hash to bitcoin_hashes
Also replace unsafe transmute with call to read_u64_into
2019-01-24 16:27:52 -05:00
Carl Dong 0f42ca69b0 Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
  consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
  instead
- Move network::serialize::Simple{En,De}coder to
  consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
  should never fail
2018-09-25 21:19:35 +08:00
Carl Dong e5b5cbfadb Fix Error type for SimpleDecoder and SimpleEncoder
- Separate serialize::Error and network::Error from util::Error
- Remove unneeded propagate_err and consume_err
- Change fuzzing code to ignore Err type
2018-08-21 01:58:40 -07:00
Jean Pierre Dudey b2594087db Use the `?` (try) instead of the `try!` macro.
Signed-off-by: Jean Pierre Dudey <jeandudey@hotmail.com>
2018-08-12 12:47:31 -04:00
Jean Pierre Dudey 77c185d9ec Fix modules documentation title.
Signed-off-by: Jean Pierre Dudey <jeandudey@hotmail.com>
2018-08-08 17:38:50 -04:00
Savil Srivastava 933dcaeb82 [code hygiene] remove deprecated rustc-serialize
Addresses #96.

Turns out it was being used for hex encoding/decoding, so replaced that with the `hex` crate.

i chose to import the `decode` method as:
```
use hex::decode as hex_decode
```

so that it is clear to the reader what is being decoded when it is called. "decode" is such a generic sounding function name that it would get confusing otherwise.
2018-07-26 09:49:15 -07:00
Tamas Blummer 9f2d737045 add witness inv types 2018-05-28 15:24:35 +02:00
Andrew Poelstra e5a3e84c41 Checkpoint commit (nope, not yet to small commit sizes :))
This is mostly fixing compile errors in `cargo test`. We are down
to 3 in `cargo build` and 14 in `cargo test`, at least for this
round.
2015-04-08 17:23:45 -05:00
Andrew Poelstra 08a20f8764 Checkpoint commit
Work is stalled on some other library work (to give better lifetime
requirements on `eventual::Future` and avoid some unsafety), so
committing here.

There are only three errors left in this round :)

Also all the indenting is done, so there should be no more massive
rewrite commits. Depending how invasive the lifetime-error fixes
are, I may even be able to do sanely sized commits from here on.
2015-04-07 17:52:58 -05:00
Andrew Poelstra 200e0fe8e3 Checkpoint commit
27 files changed, 3944 insertions(+), 3812 deletions(-) :} I've
started doing whitespace changes as well, I want everything to
be 4-space tabs from now on.
2015-04-06 20:51:11 -05:00
Andrew Poelstra 811df8a713 Giant collection of fixes ... we are into lifetime errors now :) 2015-04-05 19:10:37 -05:00
Andrew Poelstra 7b89c15ed5 More changes, incl. dropping DumbHasher in favor of SipHasher
only json stuff left in this round of compiler errors :)
2015-04-05 14:43:44 -05:00
Andrew Poelstra 11dbc717c4 Show -> Debug 2015-03-26 10:35:31 -05:00
Andrew Poelstra df065c143b fix attributes for compiler changes 2015-03-26 10:31:19 -05:00
Andrew Poelstra c3377032f8 Many syntax changes for rustc, incomplete 2015-01-18 17:39:51 -06:00
Andrew Poelstra ef11e8273b Destroy socket listener on error rather than trying to reconnect; add #derivings
Reconnecting an existing socket simply was not working; the Rust socket
did not expose any methods for reconnection, so I simply tried calling
connect() again. As near as I can tell, this was a no-op --- which makes
sense because both the sending and receiving threads had their own copy
of the Socket, and it's not clear what the synchronization behaviour
should have been.

Instead if the connection fails, we relay this information to the main
thread, wait for an acknowledgement, then simply destroy the listening
thread. The caller can then simply call `start()` again.
2014-09-10 07:15:48 -05:00
Andrew Poelstra ecdb750148 Implement script except for crypto opcodes, replace zero_hash by Default
Looks like to implement the crypto opcodes I may need to switch from
rust-crypto to rust-openssl.. or implement RIPEMD-160 for rust-crypto.
In either case I will need to generalize the hash.rs stuff to support
other hashes, so I'm committing here as a checkpoint before doing all
that.
2014-08-05 19:08:06 -07:00
Andrew Poelstra a2ce000b2b Revamp Serializable interface to be similar to Encoder/Encodable
This is a massive simplification, fixes a couple endianness bugs (though
not all of them I don't think), should give a speedup, gets rid of the
`serialize_iter` crap.
2014-08-01 09:01:39 -07:00
Andrew Poelstra c9ad7c0b58 Initial commit, move into Cargo 2014-07-18 06:56:17 -07:00