Commit Graph

26 Commits

Author SHA1 Message Date
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 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 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
VolodymyrBg 6ebdf61e76 Fix grammatical typos
Fix grammar and typos in documentation and README
2025-03-27 20:49:19 +02:00
Fmt Bot a74e08a53d 2025-03-16 automated rustfmt nightly 2025-03-16 01:25:25 +00:00
Tobin C. Harding c707b959b7
Rename timestamp module to time
We just re-named `Timestamp` to `BlockTime`. We have a `units::block`
module but it currently holds abstractions (`BlockHeight` and
`BlockInterval`) that are not onchain abstractions and therefore
somewhat different from the `BlockTime`. Instead of making `block` a
block 'utils' module instead re-name the `timestamp` module to `time`.
2025-03-08 08:30:10 +11: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 289a521426
units: Test for dyn compatibility
Phew! dyn compatibility is a non-trivial concept. There are four public
traits in `units`, only one is dyn compatible.

This patch is done in order to check off C-OBJECT from the Rust API
guidelines checklist.

Add a test to check the public traits in `units` for dyn compatibility.

While we are at it add a code comment on `Integer` stating why its not
dyn-compatible.

ref: https://rust-lang.github.io/api-guidelines/flexibility.html#c-object
2025-01-20 09:39:38 +11:00
Tobin C. Harding 96f427a8b8
units: Refactor Send/Sync api test
The `api` test for types implementing `Send` and `Sync` is part of both
C-SEND-SYNC and also C-GOOD-ERR (for error types).

Refactor the Send/Sync tests in both `units` and `io` and improve
comments.
2025-01-08 14:52:42 +11:00
Tobin C. Harding a8fbd5e302
units: Remove api docs
These docs a way too prone to going stale; we only wrote these a week
ago and they are stale already.

Lets just remove them.
2025-01-06 15:32:10 +11:00
Tobin C. Harding aaa20fc098
units: Move code comment to correct imports
This comment is meant to be on top of the `bitcoin_units` imports.
2025-01-06 15:32:10 +11:00
merge-script 3b2363b2c6
Merge rust-bitcoin/rust-bitcoin#3846: Remove `test_` prefix from unit tests
85e04315d5 Remove test_ prefix from unit tests (Tobin C. Harding)

Pull request description:

  There is a loose convention in Rust to not use `test_` prefix. The reason being that `cargo test` outputs 'test <test name>' using the prefix makes the output stutter.

  This patch smells a bit like code-churn but having the prefix in some places and not others is confusing to new contributors and is leading me to explain this many times now. Lets just fix it.

  Remove the prefix unless doing so breaks the code.

ACKs for top commit:
  shinghim:
    ACK 85e04315d5
  apoelstra:
    ACK 85e04315d5eb90075ce55bf18fab8876a4583def; successfully ran local tests

Tree-SHA512: d90ae5ef75cc5e5a8f43f60819544f1a447f13cbe660ba71e84b8f27bfcc04a11d3afde0ed56e4eea5c73ebc3925024b800a1b995f73142cab892f97a414f14a
2025-01-04 00:23:46 +00:00
Tobin C. Harding 85e04315d5
Remove test_ prefix from unit tests
There is a loose convention in Rust to not use `test_` prefix. The
reason being that `cargo test` outputs 'test <test name>' using the
prefix makes the output stutter.

This patch smells a bit like code-churn but having the prefix in some
places and not others is confusing to new contributors and is leading me
to explain this many times now. Lets just fix it.

Remove the prefix unless doing so breaks the code.
2025-01-02 10:06:50 +11:00
Tobin C. Harding c3c1f6f82d
Add missing license comment to test file 2024-12-28 08:02:52 +11:00
merge-script dacf75594e
Merge rust-bitcoin/rust-bitcoin#3799: Automated nightly rustfmt (2024-12-22)
19acae7163 2024-12-22 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:
  tcharding:
    ACK 19acae7163
  apoelstra:
    ACK 19acae716334ce2b215e4baf1cf405d05b326a9a; successfully ran local tests

Tree-SHA512: 93b1959faab558d8ebf825f70014cbef6ae21b28db7fff18bab4a3f1ae7c6ff331091a94548caf6d63dd5f0bb40e4d0833cb089388fbac4467e6ea80a9230dfc
2024-12-24 21:58:27 +00:00
Tobin C. Harding 760f0715dd
units: Make amount::error module private
The `untis::error` module is just a code organisation thing it should
never have been public. We already re-export all the error types and
this is verified by the `units/tests/api.rs` test file.

Make the module private and remove it from the paths in the `api` test.
2024-12-23 15:50:44 +11:00
Fmt Bot 19acae7163 2024-12-22 automated rustfmt nightly 2024-12-22 01:21:54 +00:00
Shing Him Ng 04dfe8dd45 Add api test to check Arbitrary impls 2024-12-19 21:13:35 -06:00
Tobin C. Harding a4a0f2921c
units: Add regression tests for Default impls
Add a regression test to the `api` tests to check the value returned by
`Default` for all types that implement it.
2024-12-12 15:15:29 +11:00
Tobin C. Harding ffdd63fa8e
units: test for C-SEND-SYNC
Done as part of the Rust API guidelines checklist.

Add a unit test for `Send` and one for `Sync`.
2024-12-09 10:54:07 +11:00
Fmt Bot 0990b30035 2024-12-01 automated rustfmt nightly 2024-12-01 01:41:12 +00:00
merge-script 7d8b9bd883
Merge rust-bitcoin/rust-bitcoin#3673: units: Close the hex parse errors
22769268f3 units: Close the hex parse errors (Tobin C. Harding)

Pull request description:

  As part of the 1.0 effort close the errors in the `units::parse` module.

ACKs for top commit:
  apoelstra:
    ACK 22769268f34b45c0bd86c548eb13cfe1e290f0d7; successfully ran local tests; thanks!
  sanket1729:
    ACK 22769268f3

Tree-SHA512: 598ba236f8c08c3f750112e0d8b8e0aa0568be2700a328e4a2d8999ca59eada8a16a6a1d9e9121e7f42ce9bbe3da3f87221ba67c36056996a687e489f4c6007c
2024-11-29 03:41:48 +00:00
Tobin C. Harding c49f40fd9a
units: Test C-COMMON-TRAITS
Add structs to the `api` integration test file that verify the set of
common traits.
2024-11-28 16:59:00 +11:00
Tobin C. Harding 22769268f3
units: Close the hex parse errors
As part of the 1.0 effort close the errors in the `units::parse` module.
2024-11-28 13:36:09 +11:00
Tobin C. Harding 7b8369f381
units: Add integration test of API surface
In an effort to check off items in the Rust API guidelines
checklist (#3632) add an integration test file that tests:

- The location of re-exports for various typical usage styles.
- Regressions in the API surface (things being accidentally moved).
- All public types implement Debug (C-DEBUG).
- For all non-error types:
    - `Debug` representation is never empty (C-DEBUG-NONEMPTY)
- For all error types:
    - Derive standard traits as defined by `rust-bitcoin` policy.

I used the `cargo check-api` script we have laying around from ages
ago (#2986) to parse `units` and get a list of the public types.
2024-11-25 13:33:15 +11:00