Commit Graph

609 Commits

Author SHA1 Message Date
merge-script e43c574146
Merge rust-bitcoin/rust-bitcoin#4473: Assert num op error
a7d059151e Assert error type (yancy)
2f7e74da45 Add MathOp helper methods (yancy)

Pull request description:

  Follow up from https://github.com/rust-bitcoin/rust-bitcoin/pull/4428 to assert the error type which I agree improves the test.

  Added some helper functions since it can be nice to see what type of overflow happened.

ACKs for top commit:
  tcharding:
    ACK a7d059151e
  apoelstra:
    ACK a7d059151eb47bf4202302604309c95c0d66371d; successfully ran local tests

Tree-SHA512: e1b3eba640de2e4f98e075270fd797582601c541e1eebef2959a4e609bf51129e8ad38baab1253b40474c39f82ee4802658ec545cc5c3590637f2ddb13f873f7
2025-05-09 15:47:38 +00:00
yancy a7d059151e Assert error type
Improve test granularity by asserting the specific type of overflow
returned.
2025-05-08 18:20:24 -05:00
yancy 2f7e74da45 Add MathOp helper methods
It's helpful to be able to assert what type of Math error occurred.
2025-05-08 18:18:25 -05:00
Tobin C. Harding dca4266205
units: Fix rustdoc column width
We've done a bit of renaming which has adversely effected rustdoc column
width, fix it up.
2025-05-08 10:16:57 +10:00
Tobin C. Harding d557caf552
Run the formatter 2025-05-08 10:16:56 +10:00
Tobin C. Harding 7c2115b68f
Rename MtpInterval to NumberOf512Seconds
Name the type exactly what it is. This used to be `Time`, then we tried
`MtpInterval`.

Note that this makes some of the original function names overly verbose
e.g., `NumberOf512seconds::from_512_second_intervals()` but given the
curlyness of locktimes too verbose is better than too terse. Also this
type, along with `NumberOfBlocks` is not going to be in very wide use so
the ergonomic hit is worth the additional clarity.
2025-05-08 10:12:23 +10:00
Tobin C. Harding 3a97ea2259
Rename HeightInterval to NumberOfBlocks
Name this type exactly what it is. Note for the error we just use
'height' even though this is a bit stale but the general concept is ok
in the error type because the name is long already.
2025-05-08 10:12:22 +10:00
Tobin C. Harding c3b7457f6c
Rename Mtp to MedianTimePast
Favour expressiveness over terseness for
`units::locktime::absolute::Mtp` (formerly `Time`) because in general
locktimes are curly AF.
2025-05-08 10:09:09 +10:00
Tobin C. Harding b38d2256fd
Run the formatter 2025-05-08 09:46:52 +10:00
merge-script 4ca6cd6065
Merge rust-bitcoin/rust-bitcoin#4458: locktimes: replace `MtpAndHeight` type with pair of `BlockMtp` and `BlockHeight`
47c77afaac units: delete MtpAndHeight type (Andrew Poelstra)
d82b8c0bcb primitives: stop using MtpAndHeight (Andrew Poelstra)
72d5fbad73 units: stop using MtpAndHeight in locktime::relative is_satisfied_by methods (Andrew Poelstra)
d933c754f5 units: change type of MtpHeight::to_mtp to BlockMtp (Andrew Poelstra)
dcbdb7ca8a units: add checked arithmetic to Block{Height,Mtp}{Interval,} (Andrew Poelstra)
4300271f0c units: add constructor for absolute::Mtp from timestamps (Andrew Poelstra)
4e4601b3d5 units: rename BlockInterval to BlockHeightInterval (Andrew Poelstra)
cb882c5ce1 units: add global `BlockMtpInterval` type (Andrew Poelstra)
4e3af5162f units: add global `BlockMtp` type (Andrew Poelstra)
a3228d4636 units: pull u32 conversions for BlockHeight/BlockInterval into macro (Andrew Poelstra)

Pull request description:

  This is a more involved PR than I'd expected but hopefully the individual commits make sense and are well-motivated. Essentially, my goal was to replace `MtpAndHeight` as used by relative locktimes with a pair of `Mtp` and `Height`.

  However, relative locktimes, when given a MTP/Height for the UTXO creation and the chain tip, are roughly modeled as "take a diff of MTPs to get a `relative::MtpInterval`, a diff of heights to get a `relative::HeightInterval`, and compare to the locktimes". *However*, we have no standalone MTP type to "take a diff of", and also there are failure modes when creating the diffs (e.g. if the diff would exceed the range of `MtpInterval` or `HeightInterval`).

  So I backed up and decided to use the existing `BlockHeight`/`BlockInterval` as the type to "take a diff of". I needed to introduce a `BlockMtp`/`BlockMtpInterval` to work with MTPs. These types have full-u32 range, unlike the similarly-named types in `units::locktimes::absolute`. I then needed to add some conversion methods. Along the way, I cleaned up the APIs and documentation, added checked arithmetic, etc., as needed.

  See the individual commit messages for more detail.

  I believe the resulting API is much more consistent and discoverable, even though it has more surface than the old API.

  I considered splitting this into 2 PRs but I think the first half of the changes aren't well-motivated with out the second half. Let me know.

ACKs for top commit:
  tcharding:
    ACK 47c77afaac

Tree-SHA512: ebe19a5b1684db8c2d913274347c994026aaa0dcdd79349c237920a82fe55560777278efdbbc7f1b1424c9391d9bbd891ae844db885deea75288000437a8a287
2025-05-07 22:32:16 +00:00
merge-script 884dc5e103
Merge rust-bitcoin/rust-bitcoin#4428: Use NumOpResult instead of Option
13cbead947 Use NumOpResult instead of Option (yancy)
002a0382aa Mark function constant (yancy)

Pull request description:

  Prefer the more descriptive NumOpResult return type over Option where return types are fallible.

  Closes https://github.com/rust-bitcoin/rust-bitcoin/issues/4419

ACKs for top commit:
  apoelstra:
    ACK 13cbead94766987f59482b1fbc1d0ebd0799737c; successfully ran local tests
  tcharding:
    ACK 13cbead947
  Kixunil:
    ACK 13cbead947

Tree-SHA512: 1a870962dcafe901a07abd93bd8075e41696341c1a4b3efef615493c73d5e5728bbc2326f8c2c95b9034ab001d0b3c668c9d64793ab03486d3a19f31df907c96
2025-05-07 20:04:16 +00:00
merge-script 10657865c3
Merge rust-bitcoin/rust-bitcoin#4457: rustdoc: Fix unused lint warnings
52940d4e12 Prefix unused variables with _ in rustdocs (Jamil Lambert, PhD)
a852aef4b8 Remove unused imports in rustdocs (Jamil Lambert, PhD)

Pull request description:

  There is a lint warning about unused variables and imports in the rustdoc examples.

  Remove the unused imports and prefix the unused variables with an underscore.

ACKs for top commit:
  apoelstra:
    ACK 52940d4e1216ad5118f7980cb2e6b8b425c61589; successfully ran local tests
  tcharding:
    ACK 52940d4e12

Tree-SHA512: 953862d546dc6e0bcd64172e8b383f0fc2a1a851971a1bcad0c1e30cbaeeaea993a0de7dd8b424c4ac1410053e179c52d0b5c90cd1b6560c27123b6b7fa49732
2025-05-07 18:56:53 +00:00
Andrew Poelstra 47c77afaac
units: delete MtpAndHeight type
This type provides little value beyond just pairing a BlockHeight and a
BlockMtp.
2025-05-06 19:23:31 +00:00
Andrew Poelstra 72d5fbad73
units: stop using MtpAndHeight in locktime::relative is_satisfied_by methods
Keep using it in primitives; will remove it there in the next commit.
2025-05-06 18:51:03 +00:00
Andrew Poelstra d933c754f5
units: change type of MtpHeight::to_mtp to BlockMtp
We are going to delete MtpHeight in a couple commits, but to let us do
the transition with smaller diffs, we will first change it to be easily
convertible to a BlockHeight/BlockMtp pair.
2025-05-06 15:50:55 +00:00
Andrew Poelstra dcbdb7ca8a
units: add checked arithmetic to Block{Height,Mtp}{Interval,} 2025-05-06 15:50:53 +00:00
Andrew Poelstra 4300271f0c
units: add constructor for absolute::Mtp from timestamps
This is a convenience method to allow direct construction of
absolute::Mtp rather than going through units::BlockMtp.
2025-05-06 15:50:46 +00:00
Andrew Poelstra 4e4601b3d5
units: rename BlockInterval to BlockHeightInterval
Now that we have BlockMtpInterval we want to distinguish BlockInterval
from it.
2025-05-06 15:50:43 +00:00
Andrew Poelstra cb882c5ce1
units: add global `BlockMtpInterval` type
See the previous commit message for justification; for sensible
arithmetic on block timestamps we need the ability to do MTP
calculations on arbitrary MTPs and arbitrary intervals between them.
However, the absolute::Mtp and relative::MtpInterval types are severely
limited in both range and precision.

Also adds a bunch of arithmetic ops to match the existing ops for
BlockHeight and BlockInterval. These panic on overflow, just like the
underlying std arithmetic, which I think is reasonable behavior for
types which are documented as being thin wrappers around u32.

We may want to add checked_add, checked_sub and maybe checked_sum
methods, but that's out of scope for this PR.
2025-05-06 15:19:36 +00:00
Andrew Poelstra 4e3af5162f
units: add global `BlockMtp` type
For our relative locktime API, we are going to want to take differences
of arbitrary MTPs in order to check whether they meet some relative
timelock threshold.

However, the `locktime::absolute::Mtp` type can only represent MTPs that
exceed 500 million. In practice this is a non-issue; by consensus MTPs
must be monotonic and every real chain (even test chains) have initial
real MTPs well above 500 million, which as a UNIX timestamp corresponds
to November 5, 1985.

But in theory this is a big problem: if we were to treat relative MTPs
as "differences of absolute-timelock MTPs" then we will be unable to
construct relative timelocks on chains with weird timestamps (and on
legitimate chains, we'd have .unwrap()s everywhere that would be hard to
justify). But we need to treat them as a "difference of MTPs" in *some*
sense, because otherwise they'd be very hard to construct.
2025-05-06 15:19:35 +00:00
Andrew Poelstra a3228d4636
units: pull u32 conversions for BlockHeight/BlockInterval into macro
There is a lot of duplicated code between BlockHeight and BlockInterval.
It obfuscates the differences between them: which timelock types they
can be converted to/from and what their arithmetic properties are.
2025-05-06 15:19:35 +00:00
Jamil Lambert, PhD 52940d4e12
Prefix unused variables with _ in rustdocs
There is a lint warning about unused variables in the rustdoc examples.

Prefix them with an underscore.
2025-05-06 10:28:51 +01:00
Jamil Lambert, PhD a852aef4b8
Remove unused imports in rustdocs
There is a lint warning about unused imports in the rustdoc examples.

Remove them.
2025-05-06 10:26:00 +01:00
Jamil Lambert, PhD 2fbbc825c9
Allow uninlined format args
There is a new lint error on nightly-2025-04-25 "variables can be used
directly in the `format!` string".

Exclude the lint to allow the existing syntax in `format!` strings.
2025-05-06 09:49:02 +01:00
merge-script ec44656933
Merge rust-bitcoin/rust-bitcoin#4438: Automated nightly rustfmt (2025-05-04)
1f19d9b4bd 2025-05-04 automated rustfmt nightly (Fmt Bot)

Pull request description:

  Automated nightly `rustfmt` changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action

ACKs for top commit:
  apoelstra:
    ACK 1f19d9b4bd8c55b9a7dc233c4b969d5d54d3a951; successfully ran local tests
  tcharding:
    ACK 1f19d9b4bd

Tree-SHA512: c13d24482848cc6e103304b52bd51964fbca9f3b09b5c179af7956aef0537b6cc05014a203844c93d0b21da08cd51bf4a84dc5611b61ae5684ae57b0beb2848f
2025-05-05 12:53:41 +00:00
yancy 13cbead947 Use NumOpResult instead of Option
Prefer the more descriptive NumOpResult return type over Option where
return types are fallible.

The returned type NumOpResult is already annotated as must_use per the
lint, so must_use can be removed after changing the method return types
to NumOpResult.
2025-05-05 07:18:15 -05:00
yancy 002a0382aa Mark function constant
Allow this function to be called elsewhere in the codebase in const
context.
2025-05-05 07:17:16 -05:00
Fmt Bot 1f19d9b4bd 2025-05-04 automated rustfmt nightly 2025-05-04 01:41:13 +00:00
Andrew Poelstra 826acb8273
units: rename relative::HeightInterval constructors
Rename `value` to `to_height` to be symmetric with `from_height`;
deprecate `to_consensus_u32` which had no symmetric `from_consensus_u32`
and was only used to implement the corresponding methods in primitives
and bitcoin.
2025-05-03 03:12:08 +00:00
Andrew Poelstra 39b4f7670d
units: rename relative::Height to HeightInterval
This is disruptive, but makes the type name consistent with
`MtpInterval` and also greatly improves clarity, helping to distinguish
between absolute and relative locktimes and reminding the author (and
reviewer) of locktime code that this needs to be a diff.
2025-05-03 03:12:08 +00:00
Andrew Poelstra d3619cc1bc
units: deprecate relative::MtpInterval::to_consensus_u32
This method is weird. It's basically just used internally to implement
the locktime methods in `primitives` and `bitcoin`. It has no symmetric
from_consensus_u32.

Conversely the constructors from 512-second intervals have no symmetric
to_* method -- the inverse of these functions is called `value`, which
is a meaningless and undiscoverable name.
2025-05-03 03:12:07 +00:00
Andrew Poelstra 1a6b8b4c7a
units: rename relative::Time to MtpInterval
The name `Time` is misleading. In fact this represents an interval
between MTPs.
2025-05-03 03:12:07 +00:00
Andrew Poelstra 39b057fade
units: rename absolute::Height consensus functions
As with absolute::Mtp, there is no "consensus encoding" of a block
height, except that obtained by converting it to a locktime. For
symmetry with `Mtp`, rename the methods.
2025-05-03 03:12:05 +00:00
Andrew Poelstra 5a8f33f380
units: rename absolute::Mtp consensus functions
There is no "consensus encoding" for a MTP. The intention for these
methods was that a user could interpret the MTP as a locktime and then
consensus-encode that locktime. However, it was instead interpreted as
the MTP representing a *blocktime* as it is consensus-encoded in a block
header.

Evidence of this misinterpretation is in several doccomments, which
casually refer to the Mtp (which used to be just called Time) as a
"block time", which is simply incorrect.
2025-05-02 17:44:58 +00:00
Andrew Poelstra 8ffcd2cf30
units: rename absolute::Time to absolute::Mtp
This is not a generic UNIX timestamp, but rather a MTP restricted to
have values between 500 million and u32::MAX. Most importantly, it is
*not* a blocktime, which is what is implied by its name and
constructors.
2025-05-02 17:44:58 +00:00
Jamil Lambert, PhD 7bcdd5ff66
Add regression tests for Display impl
The output of `Display` should not change in stable crates for types
that have well defined formatting and ones that implement `FromStr`.
Error types do not need to be tested.

Add missing tests for all implementations in `units`.
2025-04-30 18:21:09 +01:00
Jamil Lambert, PhD 9c90bf49c9
Add lock by time and denomination display regression tests
The existing display regression tests only tested height locktimes.
Denomination display regression and round trip were not tested.

Add tests for time locktimes and denomination.
2025-04-30 17:16:37 +01:00
Jamil Lambert, PhD eb7dee831e
Rename locktime tests
Existing display tests only test lock by block height.  New tests are
needed for lock by block time.

Change existing test names to make it clear which type of locktime is
being tested.
2025-04-30 16:30:56 +01:00
merge-script f9bc0f517d
Merge rust-bitcoin/rust-bitcoin#4344: Locktime MtpAndHeight
8b47068a2e feat(locktime): implement MtpAndHeight structure and validation logic (aagbotemi)

Pull request description:

  This PR fixes #4299

  - Computed MtpAndHeight structure
  - Checked if relative time and height is satisfied by MtpAndHeight
  - Compared the Ordering of MtpAndHeight with time and height
  - Checked MtpAndHeight satisfaction and comparison in Locktime
  - Added unit tests for all the implementation

  I've reviewed and adhered to the contribution guidelines

ACKs for top commit:
  apoelstra:
    ACK 8b47068a2efada30aec21c61ae4be0da4d8e8fc8; successfully ran local tests
  Kixunil:
    ACK 8b47068a2e
  tcharding:
    ACK 8b47068a2e

Tree-SHA512: b00d1384d5deaa038b486ca9d77ad33cfa6cd8c987e08407863f2be8d540014bdcc971cd9d46acb51a2d105341accc04ba151e5cccb276e8352a5d45b33097eb
2025-04-28 13:49:12 +00:00
Fmt Bot 6737c3a0e5 2025-04-27 automated rustfmt nightly 2025-04-27 01:36:56 +00:00
leopardracer d5f8a02b9b
Update result.rs 2025-04-26 12:47:19 +03:00
merge-script 12cef1d16b
Merge rust-bitcoin/rust-bitcoin#4389: Create impl_mul_assign and impl_div_assign macros
a92cc71f65 Create impl_mul_assign and impl_div_assign macros (Shing Him Ng)

Pull request description:

  The macros were called on type-rhs pairs that have an existing implementation of ops::Mul and ops::Div, respectively, that have an `Output` of `Self`

  Not as many types as I would have thought, but most of the operations result in a `NumOpResult`, which can't be then assigned back to the variable.

  Closes #4172

ACKs for top commit:
  apoelstra:
    ACK a92cc71f658771776557ea0a40d1d095d3b6d482; successfully ran local tests

Tree-SHA512: 30cfb077b9ba65af991eb17fa05ffc4a870c3f4ded746355d3a8577a71fe9a569588a882c2a936edcc9c88feede4d8bb1379a998e3f330894084a4e2fc434e6e
2025-04-25 14:08:00 +00:00
aagbotemi 8b47068a2e
feat(locktime): implement MtpAndHeight structure and validation logic
- Add MtpAndHeight for relative locktime checks
- Include unit tests for time/height comparisons
- Fix API design for mtp_as_time() error handling
- Update documentation and dependencies
- Fix BlockTime, CI, remove Ordering, and PR discussion fixed
- Fix UTXO height and timestamps
- Fix: chain_state and utxo_state handled seperately for is_satisfied_by
- Fix: panic on overflow fixed with check_add
- Fix: documentation updated and trailing whitespaces removed
- docs(mtpheight): documentation updated
- used accessors to_height and to_mtp over From impl
2025-04-25 12:24:23 +01:00
Shing Him Ng a92cc71f65 Create impl_mul_assign and impl_div_assign macros
The macros were called on type-rhs pairs that have an existing
implementation of ops::Mul and ops::Div, respectively, that have an
`Output` of `Self`
2025-04-22 22:50:09 -05:00
Jamil Lambert c4d9c1b9f8
Use a consistent rustdoc heading level of H1 `#`
There was and inconsistent usage of `#`, `##` and `###` in rustdoc
headings.  The difference in the rendered rustdocs is a minimal font
size change.

Change all headings to be H1 `#`.

Change all subheadings to be `###` to have a noticeable difference in
font size in the rendered docs.
2025-04-22 13:06:32 +01:00
Jamil Lambert f22e997587
Use parameters instead of arguments in rustdocs
The rustdocs use both `# Parameters` and `# Arguments` to mean the same
thing.

Change all of them to `# Parameters` to be consistent.
2025-04-22 10:43:13 +01:00
merge-script 8ca3a430c7
Merge rust-bitcoin/rust-bitcoin#4336: units: Make minor improvements to `MathOp`
0f62c9a582 units: Make minor improvements to MathOp (Tobin C. Harding)

Pull request description:

  Follow up from #4312, improve the `MathOp` type by:

  - Do not provide public constructor
  - Add cast protection

ACKs for top commit:
  apoelstra:
    ACK 0f62c9a5822a813be5c6f6b19d160458243f174a; successfully ran local tests
  Kixunil:
    ACK 0f62c9a582

Tree-SHA512: dbb06c5afd8df5364a2aec12b7c8632620a1e8f3955b83e91a9b4f5c2e0daaa1ecdb050d8e395e95bf018d718847cddbf3338bd89f70cbb0382bf5e080d5cf21
2025-04-20 16:35:47 +00:00
Tobin C. Harding 6323867c65
Run the formatter 2025-04-17 11:43:05 +10:00
Tobin C. Harding d5c08aef32
units: Update column width
Update column width inside the new `encapsulate` modules.

No content change.
2025-04-17 11:43:05 +10:00
Tobin C. Harding f7f1a0be8c
Add privacy boundary to BlockTime
As per policy in #4090 add a privacy boundary to the `BlockTime` type.
Use the module name `encapsulate` as is done in `amount` - its private
so the name can easily be changed later if needed.

Explicitly do not run the formatter or update rustdoc column width so
that review is easier.
2025-04-17 11:43:05 +10:00