Commit Graph

34 Commits

Author SHA1 Message Date
Tobin C. Harding fd8d563b87
Remove macro debug_from_display
Rust macros, while at times useful, are a maintenance nightmare. And
we have been bitten by calling macros from other crates multiple times
in the past.

In a push to just use less macros remove the `debug_from_display`
macro and just write the code.

This is an API breaking change to `internals` but an internal change
only to any of the _real_ crates.
2025-01-02 07:31:13 +11:00
Jamil Lambert, PhD bb29490308
Remove double spacing in rustdocs 2024-12-17 14:21:12 +00:00
Jamil Lambert, PhD 1649b68589
Standardize wording to `constructs a new`
There is a range of different wordings used in the docs of constructor
type functions.

Change all to start with `Constructs a new` or `Constructs an empty`.
2024-11-05 13:02:26 +00:00
Jamil Lambert, PhD 27f94d5540
Replace `creates` with `constructs`
In functions that act like constructors there is a mixture of the usage
of `creates` and `constructs`.

Replace all occurrences of `creates` with `constructs` in the first line
of docs of constructor like functions.
2024-11-05 12:47:28 +00:00
Jamil Lambert, PhD 88b53a471e
Unify deprecated note field format
All the deprecated note fields have been changed to be lower case and in
the format "use `a` instead".
2024-10-15 15:16:01 +01:00
BinChengZhao 2424b654d5 feat: rust-bitcoin supports testnet4 2024-10-05 23:47:21 +08:00
BinChengZhao afa91a2030 Introduce TestnetVersion enum with only TestnetV3 2024-10-03 18:40:06 +08:00
Fmt Bot 9a5ba9b6df 2024-09-08 automated rustfmt nightly 2024-09-08 01:17:12 +00:00
Shing Him Ng 30bb93c676 Implement impl_to_hex_from_lower_hex macro for types that implement fmt::LowerHex 2024-08-31 22:41:40 -05:00
Jamil Lambert, PhD 9fce57b738
Change T::from_str(s) to s.parse::<T>() in tests
`s.parse` is more idiomatic and produces more helpful error messages.

This has been changed repo wide in tests.
2024-08-28 16:13:03 +01:00
Tobin C. Harding 54c30556a2
Move params to network module
The `Params` struct is currently defined in the `consensus` module which
has become a collection of orthogonal consensus-ish things. We would
like to put things in more descriptive places.

The `Params` struct defines constants that are network specific so it
makes sense to put it in the `network` module. As soft proof of this
argument note in this patch how often the `Params` type is imported
along with the `Network` type.

API break:

The type is no longer available at `bitcoin::consensus::Params` but
rather is re-exported at `bitcoin::network::Params`.
2024-07-17 07:39:34 +10:00
Jamil Lambert, PhD d099b9c195 Remove wildcard from prelude import
Wildcards have been replaced with what is actually used.

In a couple of cases an additional use statement was added to the test
module to import `DisplayHex` which is only used in test, but
previously imported with the wildcard at the top.
2024-06-28 08:02:43 +01:00
Andrew Poelstra f934132b3b
Merge rust-bitcoin/rust-bitcoin#2846: generate Network <-> Magic From and TryFrom with a macro
76826313a1 generate Network <-> Magic From and TryFrom with a macro (Antoni Spaanderman)

Pull request description:

  Removes possible errors when a network is added to the enum (expressed with the comment `// Note: new network entries must explicitly be matched in 'try_from' below.`)

ACKs for top commit:
  Kixunil:
    ACK 76826313a1
  apoelstra:
    ACK 76826313a1

Tree-SHA512: 37aaf4b9021204c24e3dc405e666f3dea8c4f8e478b26892dd962a49c988904ba02c3dee7cec1ad78d4e4bb9ba9565ec4574d0e169dd215d26b010f1c4dd3d0b
2024-06-10 13:59:52 +00:00
Antoni Spaanderman 76826313a1
generate Network <-> Magic From and TryFrom with a macro 2024-06-09 00:23:28 +02:00
Antoni Spaanderman 0949be931a
fix ServiceFlags::remove 2024-06-09 00:09:00 +02:00
Velnbur b7458256af
Add `const` modifier to `Magic::from_bytes`
Resolves: rust-bitcoin/rust-bitcoin#2814
2024-05-30 12:58:29 +03:00
Fmt Bot 747ca578dd 2024-04-07 automated rustfmt nightly 2024-04-07 01:03:23 +00:00
Tobin C. Harding 3ec5eff56e
Add Magic::from_params
Currently `Magic` has per network consts but no way to dynamically get
the magic bytes for a network. Note also that we are currently trying to
reduce the usage of `Network` in the public API.

Add a public constructor to the `Magic` type that accepts a `Params`
parameter to determine the network to use.
2024-04-03 07:58:33 +11:00
Ava Chow 5818e04328 Add ServiceFlags::P2P_V2 2024-03-08 15:07:34 -05:00
Tobin C. Harding 9187bf3a65
Fix new nightly warnings/errors
The latest nightly toolchain introduced a whole bunch of new warnings
and errors, mostly to do with import statements - fix them all.
2024-02-21 14:13:49 +11:00
Tobin C. Harding 263a8b3603
Require BufRead instead of Read
Our decoding code reads bytes in very small chunks. Which is not
efficient when dealing with the OS where the cost of a context switch is
significant. People could already buffer the data but it's easy to
forget it by accident.

This change requires the new `io::BufRead` trait instead of `io::Read`
in all bounds.

Code such as `Transaction::consensus_decode(&mut File::open(foo))` will
break after this is applied, uncovering the inefficiency.

This was originally Kix's work, done before we had the `io` crate.
Changes to `bitcoin` were originally his, any new mistakes are my own.
Changes to `io` are mine.

Co-developed-by: Martin Habovstiak <martin.habovstiak@gmail.com>
2024-01-16 14:36:00 +11:00
Tobin C. Harding 3ca55fb163
Remove qualifying path from Read and Write
There is no advantage in having `io::Read` as opposed to `Read` and
importing the trait. It is surprising that we do so.

Remove `io::` path from `io::Read` and `io::Write`. Some docs keep the
path, leave them as is. Add import `use io::{Read, Write}`.

Refactor only, no logic changes.
2023-12-12 11:48:29 +11:00
Tobin C. Harding 761de886be
Remove imports of TryFrom and TryInto
Now that MSRV is Rust 1.56.1 we no longer need to explicitly import
`TryFrom` and `TryInto`.

No clue why clippy didn't find these for us.
2023-11-24 03:52:05 +11:00
Tobin C. Harding 7d695f6b41
Improve public re-exports
Improve the public exports in two ways:

1. Inline re-exports into the docs of the module that re-exports them.
2. Separate public and private use statements

Recently we discussed a way to separate the public and private import
statements to make the code more clear and prevent `rustfmt` joining
them all together.

Separate public exports using a code block and `#[rustfmt::skip]`. Has
the nice advantage of reducing the number of `#[doc(inline)]` attributes
also.

1. Modules first, as they are part of the project's structure.
2. Private imports
3. Public re-exports (using `rustfmt::skip` to prevent merge)

Use the format

```rust
mod xyz;
mod abc;

use ...;

pub use {
    ...,
};
```

This patch introduces changes to the rendered HTML docs.
2023-10-31 15:16:47 +11:00
Tobin C. Harding 10374af75c
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:

- Uses the following derives (unless not possible, usually because of `io::Error`)

  `#[derive(Debug, Clone, PartialEq, Eq)]`

- Has `non_exhaustive` unless we really know we can commit to not adding
  anything.

Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:

- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after

Re: non_exhaustive

To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 15:15:52 +11:00
Tobin C. Harding 2512dbafc2
Remove impl_std_error macro
We would like the codebase to be optimized for readability not ease of
development, as such code that is write-once-read-many should not use
macros.

Currently we use the `impl_std_error` macro to implement
`std::error::Error` for struct error types. This makes the code harder
to read at a glance because one has to think what the macro does.

Remove the `impl_std_error` macro and write the code explicitly.
2023-10-04 15:15:34 +11:00
Tobin C. Harding f41416a0ea
Add suffix to UnknownMagic error type
By convention we always include the suffix "Error" on our error types.

Rename the error type `UnknownMagic` to `UnknownMagicError`.
2023-10-04 12:29:05 +11:00
Tobin C. Harding 2fb71dd943
Move p2p error types to bottom of file
Move the p2p error types to the bottom of the file next to the various
impls for these types.

Code move only, no other changes.
2023-10-04 12:28:05 +11:00
Tobin C. Harding d4e8f49fc3
Move p2p::constants::Network to crate root
The `Network` type is not a p2p construct, it is more general, used
throughout the codebase to define _which_ Bitcoin network we are
operating on.
2023-08-01 16:46:59 +10:00
Tobin C. Harding 0f78943ef0
Move p2p::constants::Magic to p2p module
In preparation for removing the `p2p::constants` module; move the
`p2p::constants::Magic` type to the `p2p` module.
2023-08-01 16:42:05 +10:00
Tobin C. Harding d9d5a4ed4f
Move p2p::constants::ServiceFlags to p2p module
The `ServiceFlags` type is used by the p2p layer. It can live in the
`mod.rs` file of the `p2p` module. Done in preparation for removing the
`p2p::constants` module.

This is a straight code move, the `ServiceFlags` replaces the
current re-export.
2023-08-01 16:36:12 +10:00
Tobin C. Harding 99d8ae1173
Improve rustdocs on PROTOCOL_VERSION 2023-08-01 16:36:12 +10:00
Tobin C. Harding 4330722d62
Move p2p::constants::PROTOCOL_VERSION to p2p module
The `PROTOCOL_VERSION` const is a p2p layer constant. It can live in the
`mod.rs` file of the `p2p` module.

This is a straight code move, the `PROTOCOL_VERSION` replaces the
current re-export.
2023-08-01 16:36:12 +10:00
Tobin C. Harding 1bac1fd518
Rename the network module to p2p
The `network` module deals with data types and logic related to
internetworking bitcoind nodes, this is commonly referred to as the p2p
layer.

Rename the `network` module to `p2p` and fix all the paths.
2023-08-01 16:36:12 +10:00
Renamed from bitcoin/src/network/mod.rs (Browse further)