Commit Graph

135 Commits

Author SHA1 Message Date
Alex Pikme 6ddbcb0283
fix error pow.rs 2025-04-25 10:56:28 +02:00
healthyyyoung 427bfb82d7 chore: spellchecker 2025-03-16 07:23:18 +00:00
Tobin C. Harding e2dee4900f
Re-name Timestamp to BlockTime
We just added a `Timestamp` type without knowing that there was a push
by OpenTimestamps to also create a timestamp and that our new type may
lead to confusion. Our timestamp is explicitly for the `time` field in a
block so we can call it `BlockTime`. This name change makes the module
name stale but we will change that in a following patch to ease review.
2025-03-08 08:28:46 +11:00
Tobin C. Harding b3f122b399
Add Timestamp newtype
Bitcoin block headers have a timestamp. Currently we are using a
`u32`. while this functions correctly it gives the compiler no chance
to enforce type safety.

Add a `Timestamp` newtype that is a thin wrapper around a `u32`.
Document it and test the API surface in `api.rs`.
2025-02-25 10:03:28 +11:00
Tobin C. Harding 7277092af4
Remove mutagen
Back in 2022 we elected to use `mutagen` for mutation testing. Since
then `cargo mutants` has progressed to a point where we would now like
to use it instead.

Remove all the `mutagen` stuff and update the lock files.

Close: #2829
2025-01-21 09:43:12 +11:00
Tobin C. Harding 2c9fda4135
Add parenthesis to explicitly show precedence
Recent clippy nightly update introduces warnings about precedence. While
ours are, IMO, clear the lint docs have some cases that are not so I
don't think we should ignore this lint. Specifically I could easily miss
this one

  1 << 2 + 3 equals 32, while (1 << 2) + 3 equals 7

ref: https://rust-lang.github.io/rust-clippy/master/#/precede

Add parenthesis to explicitly show precedence. Refactor only no logic
changes.
2025-01-04 15:25:00 +11:00
Shing Him Ng 4d0d78a3af Fix typo in from_next_work_required documentation 2024-11-27 11:27:26 -06:00
Shing Him Ng 5e47c4123d Update CompactTarget::from_next_work_required to take timespan as i64
Bitcoin Core's consensus rules allow this timespan interval to be
negative. This commit also updates Params::pow_target_timespan to be a
u32. This field is almost compared to u64 and i64s, so changing this to
a u32 will allow users to use `.into()` for converstions
2024-11-27 11:27:22 -06:00
Fmt Bot 500cd10802 2024-11-17 automated rustfmt nightly 2024-11-17 01:23:55 +00:00
Tobin C. Harding 7819e50055
Move Block to primitives
On the way re-design the API by doing:

- Introduce `Checked` and `Unchecked` tags
- Rename the `txdata` field to `transactions`
- Make the `Block` fields private
- Add getters for `header` and `transactions` fields
- Move the various `compute_` methods to be free standing functions
- Make the `check_` functions private
- Introduce extension traits
2024-11-15 07:16:21 +11:00
Shing Him Ng 33b9d6904c Remove From and Into test impls
The removed impls have been replaced with test-specific functions for
converting between Rust primitives and U256
2024-11-13 15:49:50 -06:00
merge-script 4797a75586
Merge rust-bitcoin/rust-bitcoin#3584: Standardize constructor type function docs
1649b68589 Standardize wording to `constructs a new` (Jamil Lambert, PhD)
27f94d5540 Replace `creates` with `constructs` (Jamil Lambert, PhD)

Pull request description:

  As discussed in issue #3575 there are various ways of saying a new object is created.

  These have all be standardized to the agreed version.

  Close #3575

ACKs for top commit:
  apoelstra:
    ACK 1649b68589834dfe9d5b63812da3e9f0e5930107; successfully ran local tests
  tcharding:
    ACK 1649b68589

Tree-SHA512: 0ed9b56819c95f1fc14da1e0fdbbe03c4af2d97a95ea6b56125f72913e8d832db5d2882d713ae139d00614e651f3834a4d72528bdf776231cceb6772bf2f9963
2024-11-06 16:05:24 +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
Tobin C. Harding 915c9627f6
Put feature gate attribute under test attribute
Either way is fine, just pick one and be uniform.
2024-11-05 11:16:23 +11:00
Tobin C. Harding 3e2c43b19e
Elide more lifetimes
clippy found some more lifetimes to elide.
2024-10-28 15:22:13 +11:00
Tobin C. Harding e7d326f071
Seal extension traits
The extension traits are temporary just while we try to stabalize
`primitives`, they are not intended to be implemented by downstream.

Seal the extension traits so that downstream crates cannot implement
them.

Fix: #3231
2024-10-21 14:51:23 +11:00
merge-script 7af9e33f2b
Merge rust-bitcoin/rust-bitcoin#3468: Move `block::Header` to `primitives`
ff64fa3c46 Move block::Header to primitives (Tobin C. Harding)
65925117a0 Manually implement block::Header::block_hash (Tobin C. Harding)
b02ab25342 Add regression test for block_hash function (Tobin C. Harding)
5614694b69 block: Remove wildcard re-export (Tobin C. Harding)

Pull request description:

  - Patch 1: Trivial preparation.
  - Patch 2 and 3: Reduce the API surface in a disappointing way due to the orphan rule.
  - Patch 4: Do the move.

ACKs for top commit:
  apoelstra:
    ACK ff64fa3c46 successfully ran local tests; nice!

Tree-SHA512: 8d93de4a12c9f71f9dfbdc023fd8defb78f7d3da995490af8f83a927c2ca6338dd236bc08691311dc7345183fb6d0be61eedce3e4424d4060be2de934facd60a
2024-10-17 13:12:06 +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
Tobin C. Harding ff64fa3c46
Move block::Header to primitives
Introduce an extension trait and move the `block::Header` type to
`primitives`.

Nothing surprising here.
2024-10-15 11:04:00 +11:00
Tobin C. Harding 6b9429ac7b
Remove BlockHash::all_zeros
Recently we removed the `all_zeros` function from `OutPoint` in favour
of a more meaningfully named associated const. We can do the same for
`BlockHash`, the all zeros has is used for the previous blockhash of the
genesis block, add a const named as such.

In test code where we use the `all_zeros` function, just use the more
explicit form `from_byte_array([0; 32])`.
2024-09-25 06:58:14 +10: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
merge-script 0d9e8f8c99
Merge rust-bitcoin/rust-bitcoin#3204: Do many cleanups (and bug fix)
dae42bef9d do not enable bitcoin-io by default (Antoni Spaanderman)
a14cdaf859 don't enable std by default when testing (Antoni Spaanderman)
e83830dcfc use slice instead of array to not have to hardcode the length (Antoni Spaanderman)
55749d6f61 use `hash.to_byte_array` to check equality with `test.output` (Antoni Spaanderman)
969864e3b0 use fixed size array if possible, otherwise `&'static [u8]` (Antoni Spaanderman)
28ccf70fa6 remove unnecesarry borrow operator (`&`) (Antoni Spaanderman)
fa3a3afd02 remove unnecessary slicing (Antoni Spaanderman)
22e42ab86c fix test code being unnecessarily feature gated (Antoni Spaanderman)

Pull request description:

  - remove 2 unnecessary cfg attributes from tests left over from  #3167 (it made them not dependent on `alloc` anymore)
  - simplify assertion logic by removing unnecessary conversions before comparing
  - make tests `no_std` compatible by adding imports to alloc or std
  - feature gate tests behind the `alloc` feature if they use anything from the alloc crate (like the `format!` macro)
  - `schemars` feature enables `alloc` because (for example) its trait wants implementations to return `String`
  - fix `bitcoin-io` always enabling when `std` is enabled (only useful if people depend on `hashes` only, `bitcoin` depends on `bitcoin-io` already)

ACKs for top commit:
  tcharding:
    ACK dae42bef9d
  Kixunil:
    ACK dae42bef9d
  apoelstra:
    ACK dae42bef9d successfully ran local tests

Tree-SHA512: 622fd4963ef21530a98af89bcfc71abe8723aac12d363ab88d9bd30dcf2f75392711bec10e2901fab5f1a30e11897d1aae36e22892738aa1e5670166f91fddd4
2024-08-29 21:20:10 +00: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
Antoni Spaanderman e83830dcfc
use slice instead of array to not have to hardcode the length 2024-08-26 17:22:09 +02:00
leichak 2756b7fd7a Removed unneeded usages of vec! macro 2024-08-19 10:12:09 +02:00
Tobin C. Harding 2ec901fd63
Move the CompactTarget type to primitives
Potentially the whole `pow` module will move to `primitives` but this
is not possible easily right now. However, we would like to be able to
move the `BlockHash` and `block::Header` types over to `primitives`
and doing so requires the `CompactTarget` to be there.

Move the `CompactTarget` type to `primitives` and re-export it from the
`primitives` crate root.

Note also, we re-export the type publicly from `bitcoin::pow`.
2024-08-13 05:29:22 +10:00
Tobin C. Harding a00bd7cc4d
Introduce CompactTargetExt trait
In preparation for moving the `CompactTarget` type to `primitives`
introduce an extension trait for code that will be left behind in
`bitcoin`.
2024-08-13 05:26:59 +10:00
Tobin C. Harding 100ce03643
Run cargo +nightly fmt
No manual changes.
2024-08-13 05:23:35 +10:00
Tobin C. Harding 9c4a629659
Wrap CompactTarget impl block in temporary module
`rustfmt` is unable to format macro calls so instead we wrap the impl
blocks in a module to enable formatting in the next commit.
2024-08-13 05:23:31 +10:00
Tobin C. Harding 578143c09e
Separate CompactTarget impl blocks
In preparation for adding an `CompactTargetExt` trait move the
primitives methods to a separate impl block.

Refactor only, no logic changes.
2024-08-13 05:23:26 +10:00
Tobin C. Harding 22d5646f7b
Stop using CompactTarget inner field
In preparation for moving the `CompactTarget` type to `primitives` stop
using the inner field in code that will stay behind in the
`bitcoin::pow` module.
2024-08-13 05:23:10 +10:00
Tobin C. Harding 244d7dbe6c
Remove generic test impl
In preparation for moving the `CompactTarget` to `primitives` remove the
generic `Into` impl and explicitly implement for just the `From` impls
that the `pow` unit tests use.

Test code only.
2024-08-13 05:23:10 +10:00
Martin Habovstiak c72069e921 Bump MSRV to 1.63
The version 1.63 satisfies our requirements for MSRV and provides
significant benefits so this commit bumps it. This commit also starts
using some advantages of the new MSRV, namely namespaced features, weak
dependencies and the ability to use trait bounds in `const` context.

This however does not yet migrade the `rand-std` feature because that
requires a release of `secp256k1` with the same kind of change - bumping
MSRV to 1.63 and removing `rand-std` in favor of weak dependency.
2024-07-27 07:24:32 +02:00
merge-script ff5d437d42
Merge rust-bitcoin/rust-bitcoin#3067: Move `params` to the `network` module
54c30556a2 Move params to network module (Tobin C. Harding)
045a661ebe Create network directory (Tobin C. Harding)

Pull request description:

  Discussed in #2779. Patch one moves `network.rs` to `network/mod.rs`, and patch 2 moves the `params` module over there.

ACKs for top commit:
  apoelstra:
    ACK 54c30556a2 Yeah, this seems like a good place for it
  Kixunil:
    ACK 54c30556a2

Tree-SHA512: 134813419db21323d303d465b12fcbf37fd61dc1baf3305e156d324eafd822379e63dede02877ee99dce41540193a29e6e13acd13f9121f3e2fe11096524aa5e
2024-07-19 14:10:26 +00: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 2169b75bba Use lower case error messages
Error messages should start with a lower case character unless it is a
proper noun.

This has been changed everywhere.
2024-07-15 09:25:08 +01:00
Fmt Bot 19b093080b 2024-06-30 automated rustfmt nightly 2024-06-30 01:10:26 +00:00
merge-script 848256c6a9
Merge rust-bitcoin/rust-bitcoin#2921: Make 'use core::fmt' calls consistent
8ee1744b9b Make 'use core::fmt' calls consistent (Shing Him Ng)

Pull request description:

  I started taking a look at #2869 and looked for everything that was implementing the `Display` trait:

  ```rust
  impl fmt::Display for _
  ```

  but found some places where the imports weren't consistent:
  ```rust
  impl Display for _
  ```

  There were only a few instances of the latter, so I went ahead and cleaned those up before starting #2869

  I started pulling this thread when I saw the same thing was happening for `fmt::Debug` and `fmt::Formatter` so I updated the rest of the `use core::fmt::*` statements with a few exceptions:

  - No updates to `use core::fmt::*` if it was being called from within a function since I felt like the function scope was small enough to not cause confusion
  - No updates to `use core::fmt::{self, Write as _};`

ACKs for top commit:
  Kixunil:
    ACK 8ee1744b9b
  tcharding:
    ACK 8ee1744b9b

Tree-SHA512: 33eb6ea0c4e808ef78bc87de6547144b756bde206c50d80488f740e97cd8d11f1abcb8936c487d7bfd29be5e21c7f40ff88f82acdaaec9aacb4b6362ffc4c680
2024-06-27 15:40:06 +00:00
Shing Him Ng 8ee1744b9b Make 'use core::fmt' calls consistent 2024-06-26 09:41:06 -05:00
Andrew Poelstra 8869f35a69
hashes: drop the `all_zeros` method on arbitrary hashes
Manually implement it for Wtxid, Txid and BlockHash, where the all-zero
"hash" has a consensus meaning. But in general we should not be
implementing this method unless we have a good reason to do so. It can
be emulated or implemeted in terms of from_byte_array.

The use of Wtxid::all_zeros is obscure and specific enough that I am
tempted to drop it. But for txid and blockhash, the 0 hash appears in
actual blockdata and we should keep it.

All other uses of all_zeros were either in test code or in places where
the specific hash was not important and [u8; 32] was a more appropriate
type.
2024-06-22 15:09:28 +00:00
Tobin C. Harding a42bcdc22e
Remove usage of blockdata from paths
the `blockdata` directory is code organisation thing, all the
types/modules are re-exported from other places. In preparation for, and
to make easier, the `primitives` crate smashing work - remove all
explicit usage of `blockdata`.

Note that the few instances remain as they seem required e.g.,

  `pub(in crate::blockdata::script)`

Refactor only, no logic changes.
2024-06-20 12:00:22 +10:00
Andrew Poelstra 72ce271b6b
Merge rust-bitcoin/rust-bitcoin#2852: Add inherent functions to hashes
18b2788a5a api: Run just check-api (Tobin C. Harding)
6b7d02e5ae Add inherent functions to hashes (Tobin C. Harding)

Pull request description:

  Currently we have a trait `Hash` that is required for `Hmac`, `Hkdf`, and other use cases. However, it is unegonomic for users who just want to do a simple hash to have to import the trait.

  Add inherent functions to all hash types including those created with the new wrapper type macros.

  This patch introduces some duplicate code but we are trying to make progress in the hashes API re-write. We can come back and de-dublicate later.

  Includes making `to_byte_array`,`from_byte_array`, `as_byte_array`, and `all_zeros` const where easily possible.

ACKs for top commit:
  apoelstra:
    ACK 18b2788a5a

Tree-SHA512: 6b7a8d8a8501e981416d767040e5bd9fa8d1134be2ca133b5c53aa55f65c8456dccb63b642e30d0d571ca838c6f9eaeff6527d92a9b4212819a49ce619c4e093
2024-06-14 16:30:52 +00:00
Tobin C. Harding 6b7d02e5ae
Add inherent functions to hashes
Currently we have a trait `Hash` that is required for `Hmac`, `Hkdf`,
and other use cases. However, it is unegonomic for users who just want
to do a simple hash to have to import the trait.

Add inherent functions to all hash types including those created with
the new wrapper type macros.

This patch introduces some duplicate code but we are trying to make
progress in the hashes API re-write. We can come back and de-dublicate
later.

Includes making `to_byte_array`,`from_byte_array`, `as_byte_array`, and
`all_zeros` const where easily possible.
2024-06-14 10:17:00 +10:00
Divyansh Gupta 531aba0cf1 make `difficulty_float` general to all network 2024-06-12 09:22:40 +05:30
Tobin C. Harding 3298c0c4b5
pow: Unit test from_hex_internal
Add a unit test that fails if put before the "pow: Fix off-by-one error"
patch. Tests that we can correctly parse a 32 character long hex string
into a `U256`.
2024-06-05 09:44:23 +10:00
Tobin C. Harding 47e4bff0ee
pow: Fix off-by-one error
Length check has an off-by-one error in it, we want the check it include
hex strings of length 32 (eg, 128 bytes).
2024-06-05 08:39:47 +10:00
Tobin C. Harding 98bf213c52
bitcoin: Remove error module
The `error` module is empty except for public re-exports. We are still
in the "break everything and get the API right" stage so this module
adds no value - remove it.
2024-05-24 14:32:47 +10:00
Tobin C. Harding a5b93cb159
Flesh out hex unit parsing API
Add to `units::parse` the complete suit of hex unit parsing functions:

- remove prefix
- assert without prefix
- parse with or without prefix
- parse with prefix
- parse without prefix
- parse prefix unchecked

Refactor `bitcoin` to use the exact function we need, removing code
duplication.

This is a breaking change to `units`, it does however keep the current
re-exports from the public, now empty, `bitcoin::error` module.
2024-05-24 14:32:47 +10:00