Commit Graph

315 Commits

Author SHA1 Message Date
Tobin C. Harding 77085a1fa1
units: Remove serde from amounts
The `Amount` and `SignedAmount` were not supposed to implement `serde`
traits by design because doing so implicitly uses sats. We provide two
modules `as_sat` and `as_btc` to allow users to explicitly serialize in
their preferred format.

In commit: `d57ec019d5 Use Amount type for TxOut value field` derives
were added for `serde` and we did not notice it during review.
2024-11-28 12:55:43 +11:00
merge-script d54c04be00
Merge rust-bitcoin/rust-bitcoin#3647: units: Comment alloc feature
17ca5018eb units: Comment alloc feature (Tobin C. Harding)

Pull request description:

  Add a comment to the `serde` module about why we have all the `alloc` feature gating.

  Done as part of #3556 - auditing the whole crate for use of `alloc` feature.

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

Tree-SHA512: 6d13129903a9c530db811e9f64ae4fa653dbcc952a42502c53c6c622a80c1e58b3037edd0638de78867823ca370b3a4595a08a9ec07b84c71afb6200454f5d6d
2024-11-25 13:32:18 +00:00
merge-script e0ba1b661c
Merge rust-bitcoin/rust-bitcoin#3636: Mark `checked_` functions as `const`
a8379bf005 Mark `checked_` functions const in bitcoin. (Jamil Lambert, PhD)
fe10ff2eb7 Mark functions `const` in `units` (Jamil Lambert, PhD)

Pull request description:

  Following on #3608, #3627 and #1174 the rest of the `checked_` functions in all `rust-bitcoin` have been marked as const.  Except for tests and traits.

ACKs for top commit:
  apoelstra:
    ACK a8379bf0053e66cf5984ce449d19e54e529b6b70; successfully ran local tests; thanks! I find this much easier to read
  tcharding:
    ACK a8379bf005

Tree-SHA512: b8d97b7a3d9fc33b57349f418ccc5aac2f3e8c4145a73bf0e24e85547217d41214696e9a80b1fb5a1bae7e766aad1c2b5df6044564772fe76cec9b3628bcd8e5
2024-11-23 11:46:29 +00:00
Tobin C. Harding 17ca5018eb
units: Comment alloc feature
Add a comment to the `serde` module about why we have all the `alloc`
feature gating.

Done as part of #3556 - auditing the whole crate for use of `alloc`
feature.
2024-11-22 10:42:17 +11:00
Jamil Lambert, PhD fe10ff2eb7
Mark functions `const` in `units`
Mark `checked_` functions const. Replace `map()` and `?` operators,
which are not allowed in const context, with match statements.
Use descriptive variable names in ceiling division to make
it easier to follow.
2024-11-18 14:54:01 +00:00
Fmt Bot 500cd10802 2024-11-17 automated rustfmt nightly 2024-11-17 01:23:55 +00:00
Jamil Lambert, PhD 68aac98753
Mark functions `const`
Mark `checked_` functions const.
Replace `map()` and `?` operators, which are not allowed in const
context, with match statements.
2024-11-15 15:58:52 +00:00
merge-script 3ba3079edf
Merge rust-bitcoin/rust-bitcoin#3614: Add test case
4223655829 Add test case (yancy)

Pull request description:

  Since there are test cases for other functions here, add test case as follow up for https://github.com/rust-bitcoin/rust-bitcoin/pull/3604.  I forgot to request this during review.

ACKs for top commit:
  apoelstra:
    ACK 422365582964cf405529de3a1cf63d766b0d0e2e; successfully ran local tests
  tcharding:
    ACK 4223655829

Tree-SHA512: 8629cfff3e3eb800aa4d4625627e0a777c389566f4125bc63e67fe4c5a597ca63b9fb92da42f6c9dab12e6c63839eef35a13957f98606a37ed687eb96d896c2e
2024-11-15 04:24:13 +00:00
merge-script 7f1237572f
Merge rust-bitcoin/rust-bitcoin#3621: Remove Amount::fmt_value_in
0348801ef3 Remove Amount::fmt_value_in (Shing Him Ng)

Pull request description:

  Saw this mentioned in #3556

  Fixes #2952

ACKs for top commit:
  apoelstra:
    ACK 0348801ef30d1cdfdc40cd2316671532d4c06141; successfully ran local tests
  tcharding:
    ACK 0348801ef3

Tree-SHA512: 29a1d230672a22d059d2ef0870e68dec0fe6d6ae9d3fed51275d9b6779d097578a38a514544df11bdf23908c2d8e3c5fd5c6de2e460aab9792770d6b9e396fc6
2024-11-14 21:44:32 +00:00
yancy 4223655829 Add test case 2024-11-14 13:22:22 -06:00
merge-script 72760bbe96
Merge rust-bitcoin/rust-bitcoin#3606: Deprecate function
eacb1e7831 Deprecate function (yancy)

Pull request description:

  closes https://github.com/rust-bitcoin/rust-bitcoin/issues/3602

ACKs for top commit:
  tcharding:
    ACK eacb1e7831
  apoelstra:
    ACK eacb1e7831bdf2058a674a106b91447c1fe9470e; successfully ran local tests; lol

Tree-SHA512: 9da7881ee62e1808d83c92e232cef42055cce6e3ba03dc1001472948380c16a1968da1d4ce4f7ad13d78d1d70e143851f2635ba167c4df34be61ecf3f2fdee4e
2024-11-14 18:21:20 +00:00
yancy eacb1e7831 Deprecate function 2024-11-13 22:31:24 -06:00
Shing Him Ng 0348801ef3 Remove Amount::fmt_value_in 2024-11-13 19:49:26 -06:00
yancy 971167d0fd Remove test suffix from test names 2024-11-13 17:04:14 -06:00
merge-script 7315ca9a4e
Merge rust-bitcoin/rust-bitcoin#3608: Mark funtions const
4c94695942 Mark funtions const (yancy)

Pull request description:

  May as well mark these const as well.  Follow up to https://github.com/rust-bitcoin/rust-bitcoin/pull/3605

ACKs for top commit:
  apoelstra:
    ACK 4c94695942de41e5eca4ac120db2e069c0bb037d; successfully ran local tests; yeah, I think this are all worth consting
  tcharding:
    ACK 4c94695942
  sanket1729:
    ACK 4c94695942

Tree-SHA512: bf41e416cb8a2eb90e26d7bd640df5c2a5a868e3b721bde7cc8a3b97c687bab4d1cb4ab1c4b04ed625e8657e25c59e386720cb0f9d735ffc492e98f5b8b2ca3c
2024-11-13 14:47:40 +00:00
merge-script a9bc1c7db5
Merge rust-bitcoin/rust-bitcoin#3605: Mark function as const
1db4b723dc Mark function as const (yancy)

Pull request description:

  We discussed marking from_vb as const here: https://github.com/rust-bitcoin/rust-bitcoin/issues/3602

  However, from what I can tell, map() isn't const and I don't see a tracking issue for changing it.  Also, the question mark operator isn't available in const context either, although there is a tracking issue for that: https://github.com/rust-lang/rust/issues/74935.  It will be a long while before that makes it into this projects MSRV if/when it lands.

  There are some other functions in this module that could use the same re-write to make them const as well it looks like.

ACKs for top commit:
  tcharding:
    ACK 1db4b723dc
  apoelstra:
    ACK 1db4b723dc59568c14c76598e7c63c58651ed1cd; successfully ran local tests

Tree-SHA512: 62b612791dd3ce2f6ebf27f586a1262633a46566b9fd3583984171f62209714ad979439345fe86d8ef87d2f78a2cee21d619e2eb3621689faf59d81640e9f77c
2024-11-13 02:01:09 +00:00
yancy 4c94695942 Mark funtions const 2024-11-12 18:16:12 -06:00
yancy 1db4b723dc Mark function as const 2024-11-12 16:10:38 -06:00
Rob N b96501981f
feat(units): add kvb constructor to `FeeRate`
Namely, the feefilter expresses the fee as sat per 1000 vbyte
2024-11-12 08:13:53 -10:00
Tobin C. Harding 84ede349b0
Run the formatter
Run `just fmt`, no manual changes.
2024-11-11 14:19:17 +11:00
yancy 8b928a1515 Split checked_div_by_weight into floor and ceiling version 2024-11-07 16:28:44 -06: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
merge-script 3b0cb0e87d
Merge rust-bitcoin/rust-bitcoin#3539: Improve the `amount` module
81d8699b55 units: Put no_std up top (Tobin C. Harding)
3e332c3839 amount: Fix docs on FromStr (Tobin C. Harding)
5bec76aa51 amount: Fix rustdocs (Tobin C. Harding)
41c80cc476 amount: Improve docs on div by weight (Tobin C. Harding)
2b4a61739a Add type to debug output for Amount (Tobin C. Harding)
42e5043b33 Add from_int_btc group of functions (Tobin C. Harding)

Pull request description:

  Improve the `amount` module by doing:

  - Patch 1: Add/update `from_int_btc` and `from_int_btc_const` functions
  - Patch 2:  Add type to debug output for `Amount`
  - Patch 3: Fix incorrect docs
  - Patch 4: Make docs use correct style and be uniform across the two amount types
  - Patch 5: Fix docs on `FromStr`

ACKs for top commit:
  apoelstra:
    ACK 81d8699b55dad570247cb14d2b3eea64270a83cf; successfully ran local tests
  jamillambert:
    ACK 81d8699b55

Tree-SHA512: ddd6e02b4cd9a9aa8cbdd2d2f7ae289a6bcca9eb002ef0c6d83a6eca950b2cd08f5918286bb33e814f321e3bfe83fdf75ae051cf8f91ee45d3e4abe76c1c2a4c
2024-11-04 23:32:41 +00:00
Tobin C. Harding 81d8699b55
units: Put no_std up top
The `no_std` attribute has significant ramifications, as opposed to the
clippy attributes etc.

Put the `no_std` attribute up top so it catches the eye. This is also
uniform with other crates in this repo.
2024-11-05 07:49:35 +11:00
Tobin C. Harding 3e332c3839
amount: Fix docs on FromStr
The docs on `FromStr` say "string slice is zero" but actually mean to
document the behaviour when the value represented is zero.

Also the fact that `FromStr` returns an error if parsing fails is self
evident.
2024-11-05 07:49:35 +11:00
Tobin C. Harding 5bec76aa51
amount: Fix rustdocs
Fix the rustdocs on `Amount` and `SignedAmount` by doing:

- Make them uniform
- Use correct style (looks like `SignedAmount` got left behind when we
  improved `Amount`)
2024-11-05 07:49:35 +11:00
Tobin C. Harding 41c80cc476
amount: Improve docs on div by weight
Make an effort to improve the docs on `Amount::checked_div_by_weight`
and `ops::Div<weight> for Amount`.
2024-11-05 07:49:28 +11:00
Fmt Bot 5ecf7f2d67 2024-11-03 automated rustfmt nightly 2024-11-03 01:21:14 +00:00
Tobin C. Harding 2b4a61739a
Add type to debug output for Amount
Debug output usually includes the type (and we do so for
`SignedAmount`). Add the type to the `Debug` output of `Amount`.
2024-11-02 10:19:03 +11:00
Tobin C. Harding 42e5043b33
Add from_int_btc group of functions
Add/update the from_int group of functions to provide one that errors
and one that is const and panics (errors in const context are not useful
because one cannot call `unwrap` in const context).
2024-11-02 10:19:03 +11:00
Tobin C. Harding 38b8c6f5ea
units: Remove a bunch of feature gateing
Remove `alloc` feature gating from all modules in `units`!
2024-11-02 08:24:28 +11:00
Tobin C. Harding e347b50614
Use InputString in hex prefix error types
In an effort to reduce requirement for `alloc`; remove the `String` and
use `InputString` in the hex prefix related error types.

Tested with:

(Note in test code we have to use `"cafe".to_owned()` before this patch
is applied.)

rust```
    #[test]
    fn hex_prefix_errors() {
        let err = hex_remove_prefix("cafe").unwrap_err();
        std::println!("{}", err);
        std::println!("{:?}", err);

        let err = hex_check_unprefixed("0xcafe").unwrap_err();
        std::println!("{}", err);
        std::println!("{:?}", err);
    }
```

Before:

hex string is missing a prefix (e.g. 0x): cafe
MissingPrefixError { hex: "cafe" }
hex string contains a prefix: 0xcafe
ContainsPrefixError { hex: "0xcafe" }

After:

failed to parse 'cafe' as hex because it is missing the '0x' prefix
MissingPrefixError { hex: InputString("cafe") }
failed to parse '0xcafe' as hex because it contains the '0x' prefix
ContainsPrefixError { hex: InputString("0xcafe") }
2024-11-02 08:24:27 +11:00
Tobin C. Harding ff20249bdc
Remove unneeded feature gate
We can use the `parse::hex_remove_optional_prefix` now without the
feature gate.
2024-11-02 07:42:57 +11:00
Tobin C. Harding 88f6621e30
Split parse macros
Done in preparation for enabling no-alloc builds.

Split the macro calls to handle `str` separately from `alloc` types.
2024-11-02 07:42:57 +11:00
merge-script 515c0f584a
Merge rust-bitcoin/rust-bitcoin#3559: Replace `String` with `InputString`
3f2e760d1f Replace String with InputString in ParseIntError (Tobin C. Harding)
aa5c78430c Replace invalidInteger with ParseIntError (Tobin C. Harding)
9b7a706bfd Remove From<ParseIntError> (Tobin C. Harding)
c90f4b6033 Fix bug in error output (Tobin C. Harding)
c986b2f620 internals: Move error.rs to error/mod.rs (Tobin C. Harding)

Pull request description:

  This PR hopefully clears the way for removing many of the `alloc` feature gates in `units` and `primitives`

  The three final patches were tested by adding the following test to `units::locktime::absolute`:
  ```rust
      #[test]
      pub fn debug_absolute_error_conversion_height() {
          let invalid_height = LOCK_TIME_THRESHOLD + 1;
          let err = Height::from_consensus(invalid_height).unwrap_err();
          std::println!("{:?}", err);
          std::println!("{}", err);

          let invalid_time =  LOCK_TIME_THRESHOLD - 1;
          let err = Time::from_consensus(invalid_time).unwrap_err();
          std::println!("{:?}", err);
          std::println!("{}", err);

          let invalid_height = std::format!("{:x}", LOCK_TIME_THRESHOLD + 1);
          let err = Height::from_hex(&invalid_height).unwrap_err();
          std::println!("{:?}", err);
          std::println!("{}", err);

          let invalid_time = std::format!("{:x}", LOCK_TIME_THRESHOLD - 1);
          let err = Time::from_hex(&invalid_time).unwrap_err();
          std::println!("{:?}", err);
          std::println!("{}", err);

          let err = Height::from_hex("somerandomshit").unwrap_err();
          std::println!("{:?}", err);
          std::println!("{}", err);

          let err = Time::from_hex("somerandomshit").unwrap_err();
          std::println!("{:?}", err);
          std::println!("{}", err);
      }
  ```

  Gives the following output (the last four lines is the bit that changes, the rest just proves we don't break other variants)

  On commit: `d47ff1c25 Remove From<ParseIntError>`

  ConversionError { unit: Blocks, input: 500000001 }
  invalid lock time value 500000001, expected lock-by-blockheight (must be < 500000000)
  ConversionError { unit: Seconds, input: 499999999 }
  invalid lock time value 499999999, expected lock-by-blocktime (must be >= 500000000)
  ParseHeightError(Conversion(500000001))
  block height 500000001 is above limit 499999999
  ParseTimeError(Conversion(499999999))
  block height 499999999 is below limit 500000000
  ParseHeightError(InvalidInteger { source: ParseIntError { kind: InvalidDigit }, input: "somerandomshit" })
  failed to parse somerandomshit as block height
  ParseTimeError(InvalidInteger { source: ParseIntError { kind: InvalidDigit }, input: "somerandomshit" })
  failed to parse somerandomshit as block time

  On commit: `0155a0d9a Replace invalidInteger with ParseIntError`

  ConversionError { unit: Blocks, input: 500000001 }
  invalid lock time value 500000001, expected lock-by-blockheight (must be < 500000000)
  ConversionError { unit: Seconds, input: 499999999 }
  invalid lock time value 499999999, expected lock-by-blocktime (must be >= 500000000)
  ParseHeightError(Conversion(500000001))
  block height 500000001 is above limit 499999999
  ParseTimeError(Conversion(499999999))
  block height 499999999 is below limit 500000000
  ParseHeightError(ParseInt(ParseIntError { input: "somerandomshit", bits: 32, is_signed: true, source: ParseIntError { kind: InvalidDigit } }))
  failed to parse somerandomshit as block height
  ParseTimeError(ParseInt(ParseIntError { input: "somerandomshit", bits: 32, is_signed: true, source: ParseIntError { kind: InvalidDigit } }))
  failed to parse somerandomshit as block time

  On Commit: `3f2e760d1 Replace String with InputString in ParseIntError`

  ConversionError { unit: Blocks, input: 500000001 }
  invalid lock time value 500000001, expected lock-by-blockheight (must be < 500000000)
  ConversionError { unit: Seconds, input: 499999999 }
  invalid lock time value 499999999, expected lock-by-blocktime (must be >= 500000000)
  ParseHeightError(Conversion(500000001))
  block height 500000001 is above limit 499999999
  ParseTimeError(Conversion(499999999))
  block time 499999999 is below limit 500000000
  ParseHeightError(ParseInt(ParseIntError { input: InputString("somerandomshit"), bits: 32, is_signed: true, source: ParseIntError { kind: InvalidDigit } }))
  failed to parse 'somerandomshit' as absolute Height/Time (block height)
  ParseTimeError(ParseInt(ParseIntError { input: InputString("somerandomshit"), bits: 32, is_signed: true, source: ParseIntError { kind: InvalidDigit } }))
  failed to parse 'somerandomshit' as absolute Height/Time (block time)

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

Tree-SHA512: f7fd55acfb83082419db22c24a6b375c20e2631263401e500410c5b5659463f06dc4bdb145621e475dc15d75e764668cdcbf8f88006a487248a05fdb237ad136
2024-11-01 16:27:07 +00:00
Tobin C. Harding 3f2e760d1f
Replace String with InputString in ParseIntError
Currently the `ParseIntError` contains an owned copy of the input
string, this is causing us to have to use `alloc` everywhere.

We already have a alloc-friendly string replacement type, the
`InputString` - use it.
2024-11-01 17:31:22 +11:00
Tobin C. Harding aa5c78430c
Replace invalidInteger with ParseIntError
We have a special type for wrapping integer parsing errors, use it.

To test this I added the following tests:

    #[test]
    pub fn debug_absolute_error_conversion_height() {
        let invalid_height = LOCK_TIME_THRESHOLD + 1;
        let _ = Height::from_consensus(invalid_height).unwrap();
    }

    #[test]
    pub fn debug_absolute_error_conversion_time() {
        let invalid_time =  LOCK_TIME_THRESHOLD - 1;
        let _ = Time::from_consensus(invalid_time).unwrap();
    }

    #[test]
    #[cfg(feature = "std")]
    pub fn debug_absolute_error_conversion_height_string() {
        let invalid_height = std::format!("{:x}", LOCK_TIME_THRESHOLD + 1);
        let _ = Height::from_hex(&invalid_height).unwrap();
    }

    #[test]
    #[cfg(feature = "std")]
    pub fn debug_absolute_error_conversion_time_string() {
        let invalid_time = std::format!("{:x}", LOCK_TIME_THRESHOLD - 1);
        let _ = Time::from_hex(&invalid_time).unwrap();
    }

    #[test]
    #[cfg(feature = "std")]
    pub fn debug_absolute_error_height_invalid_hex_string() {
        let _ = Height::from_hex("somerandomshit").unwrap();
    }

    #[test]
    #[cfg(feature = "std")]
    pub fn debug_absolute_error_time_invalid_hex_string() {
        let _ = Time::from_hex("somerandomshit").unwrap();
    }

Which resulted in the following output

Before:

---- locktime::absolute::tests::debug_absolute_error_conversion_height stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_conversion_height' panicked at units/src/locktime/absolute.rs:431:56:
called `Result::unwrap()` on an `Err` value: ConversionError { unit: Blocks, input: 500000001 }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- locktime::absolute::tests::debug_absolute_error_conversion_height_string stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_conversion_height_string' panicked at units/src/locktime/absolute.rs:444:51:
called `Result::unwrap()` on an `Err` value: ParseHeightError(Conversion(500000001))

---- locktime::absolute::tests::debug_absolute_error_conversion_time stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_conversion_time' panicked at units/src/locktime/absolute.rs:437:52:
called `Result::unwrap()` on an `Err` value: ConversionError { unit: Seconds, input: 499999999 }

---- locktime::absolute::tests::debug_absolute_error_height_invalid_hex_string stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_height_invalid_hex_string' panicked at units/src/locktime/absolute.rs:457:52:
called `Result::unwrap()` on an `Err` value: ParseHeightError(InvalidInteger { source: ParseIntError { kind: InvalidDigit }, input: "somerandomshit" })

---- locktime::absolute::tests::debug_absolute_error_conversion_time_string stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_conversion_time_string' panicked at units/src/locktime/absolute.rs:451:47:
called `Result::unwrap()` on an `Err` value: ParseTimeError(Conversion(499999999))

---- locktime::absolute::tests::debug_absolute_error_time_invalid_hex_string stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_time_invalid_hex_string' panicked at units/src/locktime/absolute.rs:464:50:
called `Result::unwrap()` on an `Err` value: ParseTimeError(InvalidInteger { source: ParseIntError { kind: InvalidDigit }, input: "somerandomshit" })

After:

---- locktime::absolute::tests::debug_absolute_error_conversion_height stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_conversion_height' panicked at units/src/locktime/absolute.rs:432:56:
called `Result::unwrap()` on an `Err` value: ConversionError { unit: Blocks, input: 500000001 }

---- locktime::absolute::tests::debug_absolute_error_conversion_height_string stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_conversion_height_string' panicked at units/src/locktime/absolute.rs:445:51:
called `Result::unwrap()` on an `Err` value: ParseHeightError(Conversion(500000001))

---- locktime::absolute::tests::debug_absolute_error_conversion_time stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_conversion_time' panicked at units/src/locktime/absolute.rs:438:52:
called `Result::unwrap()` on an `Err` value: ConversionError { unit: Seconds, input: 499999999 }

---- locktime::absolute::tests::debug_absolute_error_conversion_time_string stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_conversion_time_string' panicked at units/src/locktime/absolute.rs:452:47:
called `Result::unwrap()` on an `Err` value: ParseTimeError(Conversion(499999999))

---- locktime::absolute::tests::debug_absolute_error_height_invalid_hex_string stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_height_invalid_hex_string' panicked at units/src/locktime/absolute.rs:458:52:
called `Result::unwrap()` on an `Err` value: ParseHeightError(ParseInt(ParseIntError { input: "somerandomshit", bits: 32, is_signed: false, source: ParseIntError { kind: InvalidDigit } }))

---- locktime::absolute::tests::debug_absolute_error_time_invalid_hex_string stdout ----
thread 'locktime::absolute::tests::debug_absolute_error_time_invalid_hex_string' panicked at units/src/locktime/absolute.rs:465:50:
called `Result::unwrap()` on an `Err` value: ParseTimeError(ParseInt(ParseIntError { input: "somerandomshit", bits: 32, is_signed: false, source: ParseIntError { kind: InvalidDigit } }))
2024-11-01 17:31:22 +11:00
Tobin C. Harding 9b7a706bfd
Remove From<ParseIntError>
The errors in `units::locktime::absolute` are complex, I'd like to make
them more simple so they are more understandable.

I have no clue why this is implemented - remove it.
2024-11-01 17:31:22 +11:00
Tobin C. Harding c90f4b6033
Fix bug in error output
`ParseTimeError` should say "block time" not "block height". This looks
like a cut'n pasta error because the `ParseHeightError` uses the same
string.
2024-11-01 17:31:21 +11:00
Tobin C. Harding 6aa8c2b023
Remove needless_borrows_for_generic_args
This has been fixed and we use nightly to lint so we have access to the
merged fix.

Removing the attribute uncovers a bunch of real lint warnings, fix
them while we are at it.
2024-11-01 14:30:45 +11:00
Tobin C. Harding 10ff979fbd
amount: Move arbitrary impl
In an effort to make the `unsigned` and `signed` files be diff'able move
the `arbitrary` code to be in the same place.

Code move only.
2024-10-31 12:57:08 +11:00
Tobin C. Harding d4d9311603
amount: Move SignedAmount to private signed module
In an effort to make the `amount` module more readable move the
`SignedAmount` type to a private submodule.

Re-export everything so this is not a breaking change.

Code move and re-exports only.
2024-10-31 12:57:08 +11:00
Tobin C. Harding 0fc0e8760b
docs: Remove link from self
When we move `SignedAmount` to a submodule linking to `self` introduces
a clippy warning, I'm not exactly sure why but lets remove the link in
preparation for the move.
2024-10-31 12:57:03 +11:00
Tobin C. Harding 13f9fd1b77
amount: Move Amount to private unsigned module
In an effort to make the `amount` module more readable move the `Amount`
type to a private submodule.

Re-export everything so this is not a breaking change.

Code move and re-exports only.
2024-10-31 12:56:40 +11:00
Tobin C. Harding 2d4c0fa6c1
amount: Format serde file
Done as a separate patch so that the diff of the verification code move
was less noisy.
2024-10-31 12:29:21 +11:00
Tobin C. Harding df96267342
amount: Move serde code to submodule
No changes other than moving the module code.
2024-10-31 12:29:21 +11:00
Tobin C. Harding 87c9a3fd11
amount: Format tests file
Done as a separate patch so that the diff of the verification code move
was less noisy.
2024-10-31 12:29:21 +11:00
Tobin C. Harding e0bc68042d
amount: Move test code to submodule
In preparation for splitting the two amounts into separate files; move
the `amount` module code to a submodule.

Internal change only.
2024-10-31 12:29:20 +11:00
Tobin C. Harding 7e1269704d
tests: Use from_sat
Stop using private constructor in unit tests, use `from_sat` instead.
2024-10-31 12:29:20 +11:00
Tobin C. Harding cd5d1aba2f
amount: Format verification file
Done as a separate patch so that the diff of the verification code move
was less noisy.
2024-10-31 12:29:20 +11:00
Tobin C. Harding 01f907b7a6
amount: Move verification code to submodule
Code move only, no other changes.
2024-10-31 12:29:20 +11:00
Tobin C. Harding 5ce827c5e0
amount: Move error code to submodule
There is _a lot_ of error types in the `amount` module. Move them to a
separate `error` module.

Add a bunch of `pub(super)` to keep things private to the `amount`
module.

Eventually we will want to close all these errors.
2024-10-31 12:29:16 +11:00
Tobin C. Harding abc54d0343
Make amount module a directory
In preparation for splitting the error types out of `amount.rs` into
their own file move the `amount.rs` file to `amount/mod.rs`.

File move only, no other changes.
2024-10-31 09:52:32 +11:00
Casey Rodarmor 1d2cfb036c Make `Amount::to_sat` and `SignedAmount::to_sat` const 2024-10-19 11:49:14 -07:00
merge-script 17899d1b8c
Merge rust-bitcoin/rust-bitcoin#3475: Unify deprecated note field format
88b53a471e Unify deprecated note field format (Jamil Lambert, PhD)

Pull request description:

  Following the suggestion in Issue #3306 all the deprecated note fields have been changed to be lower case and in the format "use `abc` instead".

ACKs for top commit:
  tcharding:
    ACK 88b53a471e
  apoelstra:
    ACK 88b53a471e successfully ran local tests

Tree-SHA512: 5c20eda7140f37ce78eb58dfdf03ecc11a67fcb10f294d860e81eaaee696e3a4209516017a9885cef9bfff1aa69b845534d139578b674933770fa24d59e4275f
2024-10-16 02:10:58 +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
Jamil Lambert, PhD fbc7aa7fd5
Remove unnecessary lifetimes
New lint warnings from recent nightly toolchain show some explicit
lifetimes that can be omitted.

The unnecessary lifetimes have been removed.
2024-10-15 14:02:56 +01:00
Tobin C. Harding e68da281ff
Warn on future deprecations
We use `TBD` in our `deprecated` string and it was discovered that there
is an exception on this string so as not to warn because it is used
internally by the Rust language. However there is a special lint to
enable warnings, lets use it.

Add `#![warn(deprecated_in_future)]` to the coding conventions section
of all crates except `fuzz`.
2024-10-15 07:56:06 +11:00
Fmt Bot f1733309e0 2024-10-13 automated rustfmt nightly 2024-10-13 01:20:28 +00:00
merge-script 27f6f17974
Merge rust-bitcoin/rust-bitcoin#3430: Add checked div by weight to amount
a0c58a4a8b Add checked weight division to Amount (yancy)
8def40a991 Add assertions to checked_weight_mul test (yancy)
16ce70d3a6 Add div_by_weight test to fee_rate (yancy)

Pull request description:

  Adds the checked variant of `amount / weight`.  I also added a test to the non-checked version for comparison so the reviewer knows they compute the same way (integer division rounded down).

  Also added assertion to `checked_weight_mul test` showing the results are rounded up.

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

Tree-SHA512: cf14123ed261d100e3261a720c26f8c10368f05225e32eaa246f25ab766d20515db5feb98335d4e3e08a8146a70db65ff64670da3f75e7764e8f86ef534d2663
2024-10-10 18:19:16 +00:00
yancy a0c58a4a8b Add checked weight division to Amount 2024-10-09 09:30:09 -05:00
yancy 8def40a991 Add assertions to checked_weight_mul test 2024-09-30 19:05:18 -05:00
yancy 16ce70d3a6 Add div_by_weight test to fee_rate 2024-09-30 19:05:18 -05:00
merge-script 95be55177e
Merge rust-bitcoin/rust-bitcoin#3421: Const locktime constructors
de319670ae feat: Create relative lock times at compile time (Christian Lewe)
53e1fb6b0c feat: Create absolute lock time at compile time (Christian Lewe)

Pull request description:

  I noticed that I cannot create lock times as compile-time constants. This PR tries to remedy this issue by marking lock time constructors as `const`.

  Because the `internals` crate depends on the `units` crate in a way that I don't fully understand yet, this PR updates the `units` crate only.

  If `from_consensus` is being kept non-`const` by design, to keep the API flexible to future changes, then please close this PR. In this case, I overlooked existing discussions.

ACKs for top commit:
  apoelstra:
    ACK de319670ae successfully ran local tests; good start; should backport
  tcharding:
    ACK de319670ae

Tree-SHA512: 69f9147707bcf8f91f755dd6d1be5ed08945e775ee46918e33d77a9d07ce474047a80ed1226134a3914ead51d1ddbbc657552ca934dc3c079b92ad3d50b13152
2024-09-30 16:29:18 +00:00
Fmt Bot a65d2a0ee4 2024-09-29 automated rustfmt nightly 2024-09-29 01:21:14 +00:00
Christian Lewe de319670ae feat: Create relative lock times at compile time
Also mark these methods as const. Because <u32 as From<u16>>::from
is not available in const contexts, I had to cast u16 as u32. I try to
avoid casts as much as I can, but in this case a cast seems unavoidable.
Casting u16 as u32 should be safe on all architectures.
2024-09-28 18:29:15 +02:00
Christian Lewe 53e1fb6b0c feat: Create absolute lock time at compile time
Mark the from_consensus and to_consensus methods of the absolute
lock time structs as const. In theory. these methods do some sanity
checking and wrap a u32 value in a newtype. This should be possible
to do in const. Marking the methods as const should not break existing
call sites.
2024-09-28 18:14:35 +02:00
yancy cb2146d5fa Implement FeeRate checked_sub 2024-09-23 21:33:31 -05:00
yancy 212a751929 Implement FeeRate checked_add 2024-09-23 21:33:31 -05:00
yancy c967eabd43 Implement FeeRate SubAssign 2024-09-23 21:33:31 -05:00
yancy c3a8bfa98d Implement FeeRate AddAssign 2024-09-23 21:33:31 -05:00
yancy 0e70870056 Implement FeeRate subtraction 2024-09-23 21:33:31 -05:00
yancy 86359fe364 Implement FeeRate addition 2024-09-23 21:33:31 -05:00
merge-script 855c4bb754
Merge rust-bitcoin/rust-bitcoin#3364: Remove unused inports, and fix unused variables and methods in docs
f6abdcc001 Allow unused in `macros.rs` docs (Jamil Lambert, PhD)
fd89ddf401 Remove or fix unused variables and methods in docs (Jamil Lambert, PhD)
ff6b1d4f19 Remove unused variables and methods from docs (Jamil Lambert, PhD)
e58cda6f92 Remove `unused_imports` in docs (Jamil Lambert, PhD)

Pull request description:

  As mentioned in #3362 examples in documentation are not linted in the same way as other code, but should still contain correctly written code.

  #![doc(test(attr(warn(unused))))] has been added to all lib.rs files

  In the docs throughout all crates:

  - Unused imports have been removed.

  - Unused variables, structs and enums have been used e.g. with an `assert_eq!` or prefixed with `_`

  - Unused methods have been called in the example code.

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

Tree-SHA512: c3de1775ecde6971056e9fed2c9fa1621785787a6a6ccbf3a6dbd11e18d42d4956949f3f8adfc75d94fd25db998b04adb1c346cc2c2ba47f4dc37402e1388277
2024-09-20 02:10:51 +00:00
merge-script 4fb2fccd16
Merge rust-bitcoin/rust-bitcoin#3367: Comment from_str methods
f5cae1cddd Comment from_str methods (yancy)

Pull request description:

  Follow up from https://github.com/rust-bitcoin/rust-bitcoin/pull/3346

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

Tree-SHA512: 2b95381e5281754e2b3a49aa8dfaac5742c244970fb54f68024dc23b61a74955ae95b9a0e7ae848095ac0686df5faf93faf7de3371b2f341b108cc10e5d4a9cd
2024-09-18 18:26:38 +00:00
merge-script 9e90f7dfd8
Merge rust-bitcoin/rust-bitcoin#3371: Add doc comment detailing fee calculation
8c29fe08f8 Revise doc comment (yancy)

Pull request description:

  Update doc comment to make clear that the ceiling is computed instead of the default behavior for integer division.

ACKs for top commit:
  tcharding:
    ACK 8c29fe08f8
  apoelstra:
    ACK 8c29fe08f8 successfully ran local tests

Tree-SHA512: 3793dccab5b5a3e59b3949ecb54475c76263e1debcc18df42f3b0251189435ba87e537c4b5d80c91f4b916449618a75e6efac32b4ba2fc29c42563e1b0fb4a89
2024-09-18 16:58:00 +00:00
Jamil Lambert, PhD fd89ddf401
Remove or fix unused variables and methods in docs
Examples in documentation are not linted in the same way as other code,
but should still contain correctly written code.

Throughout all of the crates except internals (another commit) unused
variables have been prefixed with `_`, unused imports have been removed,
and a warn attribute added to all of the `lib.rs` files.
2024-09-18 16:37:47 +01:00
yancy f5cae1cddd Comment from_str methods 2024-09-17 17:19:51 -05:00
yancy 8c29fe08f8 Revise doc comment
Comment that the ceiling is computed instead of the floor.
2024-09-17 17:12:48 -05:00
Fmt Bot 7990724ff4 2024-09-15 automated rustfmt nightly 2024-09-15 01:19:55 +00:00
merge-script 7360c3ce9a
Merge rust-bitcoin/rust-bitcoin#3346: Add a condition for parsing zero from string when not denominated.
894f82e7cc Add a condition for parsing zero from string when not denominated. (yancy)

Pull request description:

  closes https://github.com/rust-bitcoin/rust-bitcoin/issues/3307

ACKs for top commit:
  Kixunil:
    ACK 894f82e7cc
  tcharding:
    ACK 894f82e7cc
  apoelstra:
    ACK 894f82e7cc9eb459a297d43e82734621e0824610; successfully ran local tests.

Tree-SHA512: 6d32847c74ccedc3355d615838e2dd1e08add29cc47ff69d9ef22683eda93049d41131dd0c992c8d7be3a018f5438856c415a3d2f3cb9de9c986534b268ee386
2024-09-13 17:41:38 +00:00
yancy 894f82e7cc Add a condition for parsing zero from string when not denominated. 2024-09-12 09:34:18 -05:00
Shing Him Ng eda87517c0 Update documentation to indicate that the Display implementation in Amount is unstable 2024-09-07 15:05:14 -05:00
Tobin C. Harding 8f5bde9f17
units: Fix typo in measured
Its "time is measured" not "time is measure".
2024-09-04 14:45:05 +10:00
merge-script 3bbe821ce5
Merge rust-bitcoin/rust-bitcoin#3268: Fix clippy rustdocs warnings
b6371b5801 Fix clippy rustdocs warnings (Tobin C. Harding)

Pull request description:

  A new nightly version (`nightly-2024-08-28`) introduces a few warnings because of our rustdocs. These are valid warnings and should be fixed, thanks `clippy` team.

  (The `bip152` change is a bit sloppy, open to suggestions.)

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

Tree-SHA512: 503fb9d48772b74a5acdb26c0f77a85c52323c03360f983204fccee0f28bedeff142237b067caa1ce6ea04ea9842cc493e0d06dc141ca00a98151fa002b62392
2024-09-02 14:20:44 +00:00
Fmt Bot fa71b0e044 2024-09-01 automated rustfmt nightly 2024-09-01 01:22:04 +00:00
merge-script c2e3e86106
Merge rust-bitcoin/rust-bitcoin#3274: Add Arbitrary to SignedAmount type
3f244db19d Add Arbitrary to SignedAmount type (yancy)

Pull request description:

  While proptesting, I've found Arbitrary for SingedAmount to be useful.  Would appreciate adding this as well.

ACKs for top commit:
  tcharding:
    ACK 3f244db19d
  Kixunil:
    ACK 3f244db19d

Tree-SHA512: f293215505db85998135c75e147f4cb0031a08c7db619b706e6c7d632c6f23783af56992531396aecc6e3da55b107a7f2c7ec4d68a3e5737ebcf4225f306ed29
2024-08-30 20:08:17 +00:00
yancy 3f244db19d Add Arbitrary to SignedAmount type 2024-08-29 20:19:48 -05:00
Tobin C. Harding b6371b5801
Fix clippy rustdocs warnings
A new nightly version (`nightly-2024-08-28`) introduces a few warnings
because of our rustdocs. These are valid warnings and should be fixed,
thanks `clippy` team.

(The `bip152` change is a bit sloppy, open to suggestions.)
2024-08-30 05:47:31 +10:00
merge-script cfb53c7866
Merge rust-bitcoin/rust-bitcoin#3257: Add Arbitrary to Weight
f8edbd1f45 Add Arbitrary to Weight (yancy)

Pull request description:

  Adds Arbitrary to the Weight type.  I had meant to add this with the initial Arbitrary PR but I overlooked it.

ACKs for top commit:
  tcharding:
    ACK f8edbd1f45
  Kixunil:
    ACK f8edbd1f45

Tree-SHA512: b886f78285b5cd06b2531a1e53272cad623a8e43e76e8108abe90668b45d00674b6a867c5ff980ee7f0a34919e1d6bb534624bd0bc67a53c8c9a41ea2ed504e8
2024-08-28 23:02:13 +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
Jamil Lambert, PhD c835bb9eab
Change T::from_str(s) to s.parse::<T>() in docs
`s.parse` is more idiomatic and produces more helpful error messages.

This has been changed repo wide in docs.
2024-08-28 13:50:42 +01:00
yancy f8edbd1f45 Add Arbitrary to Weight 2024-08-27 18:34:47 -05:00
Jamil Lambert, PhD a76d76eca1
Change `T::from_str(s)` to `s.parse::<T>()`
`s.parse` is more idiomatic and produces more helpful error messages.

This has been changed repo wide in the main codebase, not including
examples, rustdocs, and in the test module.

`use std::str::FromStr;` has been removed where this change makes
it unnecessary.
2024-08-27 17:31:00 +01:00
merge-script 51af258eaa
Merge rust-bitcoin/rust-bitcoin#3015: Add Arbitrary
3e034d5ede Add Arbitrary dependency (yancy)

Pull request description:

  Adds an example draft showing what is needed to use Arbitrary for coin selection.

  Shot out to how nice Arbitrary is for fuzzing a target by taking unstructured randomness and creating structured rust-bitcoin types for fuzzing.  Is there a way we could add this to rust-bitcoin for structuring the fuzz data needed?

  This is then the example to fuzz test a SRD algo (after applying this PR to rust-bitcoin) using rust-bitcoin types :)

  ```
  #![no_main]

  use arbitrary::Arbitrary;
  use bitcoin::{Amount, FeeRate};
  use bitcoin_coin_selection::{select_coins_srd, WeightedUtxo};
  use libfuzzer_sys::fuzz_target;
  use rand::thread_rng;

  #[derive(Arbitrary, Debug)]
  pub struct Params {
      target: Amount,
      fee_rate: FeeRate,
      weighted_utxos: Vec<WeightedUtxo>,
  }

  fuzz_target!(|params: Params| {
      let Params { target: t, fee_rate: f, weighted_utxos: wu } = params;
      select_coins_srd(t, f, &wu, &mut thread_rng());
  });
  ```

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

Tree-SHA512: accd565815de3b37730d2ff12a24fcfc84e52ad357e5c940b1500a1e0bb17f4ff5fd6e52d31e8e96bb5290ee4fa050cfd2a9bbd6bbae13fc378f43093b64177f
2024-08-25 13:16:49 +00:00
merge-script 837fc9c9c2
Merge rust-bitcoin/rust-bitcoin#2972: Use index size rather than pointer size to enforce convertibility of `u32` to `usize`
c427d8b213 bitcoin: Compile time assert on index size (Tobin C. Harding)
49a6acc1a0 internals: Remove double parenthesis in const_assert (Tobin C. Harding)
2300b285ef units: Remove compile time pointer width check (Tobin C. Harding)

Pull request description:

  3 patches in preparation for other size related work, this PR does not touch the `ToU64` issue which will be handled separately.

  - Patch 1: Don't check pointer width in `units` because its not consensus code
  - Patch 2: Modify internal macro `const_assert`
  - Patch 3: Use index size to enforce not building on a 16 bit machine

ACKs for top commit:
  Kixunil:
    ACK c427d8b213 though I think the last commit was kinda a waste of time and it should have been adding the trait instead or leave it for later.
  apoelstra:
    ACK c427d8b213 successfully ran local tests; unsure if we want to merg this or wait for #3215

Tree-SHA512: 823df5b6a5af3265bce2422c00d287f45816faeb5f965685650ac974a1bd441cf548e25ac2962591732ff221bee91a55703da936382eb166c014ca5d4129edf8
2024-08-24 14:16:31 +00:00
yancy 3e034d5ede Add Arbitrary dependency
Implement Arbitrary for a select subset of types.
2024-08-23 15:39:20 -05:00
Tobin C. Harding 60b3cabb41
Panic in const context
Now that our MSRV is past 1.57 we can panic in const contexts.

Fix: #2427
2024-08-22 17:37:17 +10:00
Tobin C. Harding 2300b285ef
units: Remove compile time pointer width check
The `units` crate does not contain consensus logic and since our
requirement to only support 32-bit and 64-bit machines is due to
consensus logic we do not need to enforce the `target_pointer_width` in
the `units` crate.

Remove the compile time check on pointer width from the `units` crate.
2024-08-02 11:33:30 -05:00
Tobin C. Harding e3d9376a9b
units: Remove serde re-export
We re-export to help users keep their dependencies in sync but `serde`
is at `v1.0` so this is not really a problem.

Remove the public re-export of `serde` (and with current MSRV we don't
need the `extern crate` at all).
2024-07-23 01:41:41 -05:00
merge-script e1478b1802
Merge rust-bitcoin/rust-bitcoin#3069: Remove re-export of `ParseIntError`
ab581a90f8 Remove re-export of ParseIntError (Tobin C. Harding)

Pull request description:

  In d242125 I claimed that `ParseIntError` was somehow special, I no longer thing this is the case. As we pin down the re-export policy (for errors and other types) it is hard if we have one non-typical re-export.

  We have https://github.com/rust-bitcoin/rust-bitcoin/issues/3068 to discuss the policy, for now just remove the unusual re-export.

ACKs for top commit:
  Kixunil:
    ACK ab581a90f8
  shinghim:
    ACK ab581a90f8
  apoelstra:
    ACK ab581a90f8

Tree-SHA512: 5ac4123aeb27c8cee78e5760f21e70be8035d526ba7e14e72759cba27f98b51cc2cba9b2bf0eeb99e0f6b7210ec4a750986bb6c5dc0725ed892730fdec8a7e06
2024-07-19 15:36:21 +00:00
merge-script a28f11c9fb
Merge rust-bitcoin/rust-bitcoin#3062: Add more unit test coverage for relative LockTime
a76c13f675 Add more unit test coverage for relative LockTime (Shing Him Ng)

Pull request description:

  Adding some test cases for `from_seconds_floor` and one more for `from_seconds_ceil`

ACKs for top commit:
  tcharding:
    ACK a76c13f675
  Kixunil:
    ACK a76c13f675

Tree-SHA512: 5d773a30fa56842af21109d232ec3eae7fde7ce38aca93dadccc7bfbf14d5207ea329ef737f847e45ba755fd22e1862c3e78e7e64a8a0babebcb0e27a2bb7a90
2024-07-18 14:21:46 +00:00
Tobin C. Harding ab581a90f8
Remove re-export of ParseIntError
In d242125 I claimed that `ParseIntError` was somehow special, I no
longer thing this is the case. As we pin down the re-export policy (for
errors and other types) it is hard if we have one non-typical re-export.

We have https://github.com/rust-bitcoin/rust-bitcoin/issues/3068 to
discuss the policy, for now just remove the unusual re-export.
2024-07-17 08:01:32 +10:00
Shing Him Ng a76c13f675 Add more unit test coverage for relative LockTime 2024-07-16 12:19:13 -05:00
merge-script 8eb5d8fad0
Merge rust-bitcoin/rust-bitcoin#3019: Fix error messages
2169b75bba Use lower case error messages (Jamil Lambert, PhD)

Pull request description:

  Error messages should be lower case, except for proper nouns and variable names.  These have all been changed.

  ~~They should also state what went wrong.  Some expect error messages were positive, giving the correct behaviour or correct input.  These have been changed so that they are now negative, i.e. saying what went wrong.~~

  EDIT: After further discussion it was decided not to change the expect messages.

ACKs for top commit:
  Kixunil:
    ACK 2169b75bba
  tcharding:
    ACK 2169b75bba

Tree-SHA512: 92442c869e0141532425f6fca5195fd319b65026f68c4230a65ad70253565d98931b2b44ee202975c307280525c505147e272297dc81207312e40c43d007021c
2024-07-16 15:30:26 +00:00
merge-script f765deb160
Merge rust-bitcoin/rust-bitcoin#3052: Throw error instead of panic when from_second_ceil input is too large
7874db8fc3 Throw error instead of panic when from_second_ceil input is too large (Shing Him Ng)

Pull request description:

  Update `from_second_ceil` per [comment](https://github.com/rust-bitcoin/rust-bitcoin/issues/3029#issuecomment-2227459248) and add unit tests

  Resolves #3029

ACKs for top commit:
  Kixunil:
    ACK 7874db8fc3
  tcharding:
    ACK 7874db8fc3

Tree-SHA512: 0d60397f867d4536ff00b81441c27ea3dc4bf9e5ecc2fb5afbe50cb3153101df54c2bb5056da1916d83c3e84367f987f5bac07a381b7433a5701a0ea8a587f95
2024-07-16 13:36:23 +00:00
merge-script 12f47c72c5
Merge rust-bitcoin/rust-bitcoin#3030: Rename variable assignment
84dd04cf60 Rename variable assignment (yancy)

Pull request description:

  The type created after assignment is a Weight type.  Using a var name vb which is short for virtual byte is incorrect.

  Pulled this out of stale PR 2215

ACKs for top commit:
  shinghim:
    ACK 84dd04cf60
  apoelstra:
    ACK 84dd04cf60
  Kixunil:
    ACK 84dd04cf60

Tree-SHA512: 1bfc875f53037b2c1e8da25fe44e9ca3303981bdce4e48661a8fb2061833e3cd90318d854f7119c805e861cd8a591697378f829f32eb74ac99a71dc4c947abde
2024-07-15 22:40:37 +00:00
yancy 84dd04cf60 Rename variable assignment
The type created after assighnment is a Weight type.  Using a var name
vb which is short for virtual byte is incorrect.
2024-07-15 10:05:47 -05:00
Shing Him Ng 7874db8fc3 Throw error instead of panic when from_second_ceil input is too large 2024-07-15 07:55:41 -05: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
yancy 301fe9fad9 Move impl above tests
It's common in other modules for the tests to be at the end of the file.
Moving the tests to the bottom helps the code base uniformity.
2024-07-13 14:57:31 -05:00
Tobin C. Harding 40bee24de7
Run the formatter
I don't want to wait for the bot, just run the formatter now.

No manual changes.
2024-07-11 11:21:53 +10:00
merge-script 0511f44d99
Merge rust-bitcoin/rust-bitcoin#2994: Remove private prelude module from `units` crate
1cce1b5aa6 Remove private prelude module from units crate (Jamil Lambert, PhD)

Pull request description:

  The private prelude module has been removed from the units crate and instead imports are stated in full when needed.  As discussed in #2926.

ACKs for top commit:
  Kixunil:
    ACK 1cce1b5aa6
  apoelstra:
    ACK 1cce1b5aa6

Tree-SHA512: 58b93ff66f74399938bc1a7f59fe8d2a21d0437c7e90e0c190d3d6a8de30f9c9268c8e4288d1db287b4d190624968937b1ad6c6e54d29025370e47e71be925c1
2024-07-10 21:56:03 +00:00
Jamil Lambert, PhD 1cce1b5aa6 Remove private prelude module from units crate
The private prelude module has been removed from the units crate and
instead imports are stated in full when needed.
2024-07-10 09:14:33 +01:00
merge-script 13e27acb16
Merge rust-bitcoin/rust-bitcoin#2957: Fix rustdocs in `units` crate
6dd5af9678 Add missing links (Jamil Lambert, PhD)
47e367f011 Standardize error headings (Jamil Lambert, PhD)
75f317a689 Fix rustdoc grammar (Jamil Lambert, PhD)
573f8ce724 Add backticks to rustdoc links (Jamil Lambert, PhD)

Pull request description:

  The rustdocs in the `units` crate has been reviewed and improved.

  Some of the links were missing backticks, these have been added.

  Some grammatical changes have been made to improve the documentation.

  The use of links was inconsistent and has been changed to have links everywhere that seemed appropriate.

  A couple of error headings were added and a description as to why a capital M is not accepted in the denomination for MegaSatoshi or MegaBitcoin.

ACKs for top commit:
  apoelstra:
    ACK 6dd5af9678
  tcharding:
    ACK 6dd5af9678

Tree-SHA512: f5481a7c3aa99d7882cda9ccda9df9e27f092ff91ef584f07dc887f38bfe12e5ea801cfa7f42bb4022301860489ee6be55557752a8cbe70932f258ea753495dc
2024-07-09 14:13:32 +00:00
merge-script b392510ec1
Merge rust-bitcoin/rust-bitcoin#2889: Move `serde_round_trip` macro to internals
7fa53440dc Move serde_round_trip macro to internals (Tobin C. Harding)

Pull request description:

  We currently duplicate the serde_round_trip macro in `units` and `bitcoin`, this is unnecessary since it is a private test macro we can just throw it in `internals`.

  While we are at it lets improve the macro by testing a binary encoding also, elect to use the `bincode` crate because we already have it in our dependency graph.

  Add `test-serde` feature to `internals` to feature gate the macro and its usage (preventing the transient dependency on `bincode` and `serde_json`).

ACKs for top commit:
  Kixunil:
    ACK 7fa53440dc
  apoelstra:
    ACK 7fa53440dc

Tree-SHA512: f40c78bf2539940b7836ed413d5fe96ce4e9ce59bad7b3f86d831971320d1c2effcd23d0da5c785d6c372a2c6962bf720080ec4351248fbbdc0f2cfb4ffd602c
2024-07-08 20:50:23 +00:00
Jamil Lambert, PhD 6dd5af9678 Add missing links
The use of links in the rustdocs was inconsistent.

Links have been added when missing.

[`locktime::absolute::Height`] and [`locktime::relative::Height`] did
not work and `(crate::locktime)` was appended to fix it.
2024-07-08 08:53:13 +01:00
Jamil Lambert, PhD 47e367f011 Standardize error headings
Created headings for a couple of function error descriptions to be
consistent with the rest of the crate.

Added a description explaining why Mega is not allowed in a
denomination.
2024-07-08 08:52:15 +01:00
Jamil Lambert, PhD 75f317a689 Fix rustdoc grammar
In the rustdocs, made all function descriptions third person. Corrected
some grammar and improved some wording.
2024-07-08 08:52:15 +01:00
Jamil Lambert, PhD 573f8ce724 Add backticks to rustdoc links
Links in rustdocs should be formatted with a backtick.  This has been
changed throughout Units.
2024-07-08 08:52:15 +01:00
Fmt Bot 91382977fb 2024-07-07 automated rustfmt nightly 2024-07-07 01:10:59 +00:00
Tobin C. Harding 7fa53440dc
Move serde_round_trip macro to internals
We currently duplicate the serde_round_trip macro in `units` and
`bitcoin`, this is unnecessary since it is a private test macro we can
just throw it in `internals`.

While we are at it lets improve the macro by testing a binary encoding
also, elect to use the `bincode` crate because we already have it in
our dependency graph.

Add `test-serde` feature to `internals` to feature gate the macro and
its usage (preventing the transient dependency on `bincode` and
`serde_json`).
2024-07-06 14:51:30 +10:00
yancy dfce405007 Remove nonsense method
Weight type implicitly includes witness scale factor
2024-07-04 12:11:46 -05:00
merge-script 014c4931be
Merge rust-bitcoin/rust-bitcoin#2951: Fix `Amount` decimals handling
3196c271ac Deprecate `Amount::fmt_value_in` (Martin Habovstiak)
467546fc0c Round `Amount` when requested precision is too low (Martin Habovstiak)
7c95a777c1 Fix `Amount` decimals handling (Martin Habovstiak)

Pull request description:

  Displaying with minimal number of zeros is the default in Rust and we want to follow it. This was originally implemented in #716 but #2604 reversed it claiming this is common, however it broke people who rely on minimal display (e.g. BIP21) without fixing the root cause of #2136.

  This reverts commit d887423efc adds a test to prevent this change and also fixes the problem with `{:0.8}` not working.

  Closes #2136
  Closes #2948

  Can we backport this one too?

ACKs for top commit:
  tcharding:
    ACK 3196c271ac
  apoelstra:
    ACK 3196c271ac I also really like how this reduces the complexity of the module, basically passing everything through to `display_in`

Tree-SHA512: 3221f83086ac55af3d4caad03fe2b619be303533bba12096040419d119600c8597938809e171662f11b515d469156b083b2072b901d445e4fdfc7b1062cf7b6a
2024-07-04 13:19:32 +00:00
Martin Habovstiak 3196c271ac Deprecate `Amount::fmt_value_in`
`fmt_value_in` was added when `display_in` wasn't available. However
common usage patterns seem to favor `display_in`. It can be used within
format strings and supports formatting options.

Removing it will simplify the codebase, so this deprecates it.
2024-07-03 07:38:58 +02:00
Martin Habovstiak 467546fc0c Round `Amount` when requested precision is too low
Low precision was previously not considered because it was believed it
could lead to misleading data being displayed. However it's quite
possible that people using low precision know what they are doing and
it's sometimes useful to show rounded numbers.

To enable low precision we just compute what the rounded number would be
and display that one instead.

This actually fully closes #2136 since this issue was mentioned there
along with previously-fixed displaying of higher precisions.
2024-07-02 18:39:56 +02:00
Martin Habovstiak 7c95a777c1 Fix `Amount` decimals handling
Displaying with minimal number of zeros is the default in Rust and we
want to follow it. This was originally implemented in #716 but #2604
reversed it claiming this is common, however it broke people who rely on
minimal display (e.g. BIP21) without fixing the root cause of #2136.

This reverts commit d887423efc adds a test
to prevent this change and also fixes the problem with `{:0.8}` not
working.

Closes #2136
Closes #2948
2024-07-02 18:38:54 +02:00
merge-script 96dffb2152
Merge rust-bitcoin/rust-bitcoin#2939: Automated nightly rustfmt (2024-06-30)
19b093080b 2024-06-30 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 19b093080b

Tree-SHA512: 50d905ab4823e5e746751c0aacd145fc5640c9cb250d99bcd44800c72a890562c0014a9eee21602198388dee7bd9354ff764bab5616aac82bf7660669f8c2ee1
2024-06-30 12:09:40 +00:00
Fmt Bot 19b093080b 2024-06-30 automated rustfmt nightly 2024-06-30 01:10:26 +00: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
Jamil Lambert, PhD 9ea8c58ad6 Fix case sensitivity of denomination
The comment in `FromStr` says that any combination of upper and lower case is considered valid, but the code only allowed a set list of variations. It also had a non exhaustive list of `CONFUSING_FORMS`.

The comment and code has been changed to only consider all upper case or all lower case denominations as valid or where BTC/btc is prefixed with a lower case c, m or u.  `CONFUSING_FORMS` has been changed to contain all combinations of an upper case prefix on an otherwise valid denomination.
2024-06-18 10:46:17 +01:00
Jamil Lambert, PhD 67569ca632 Remove denominations < 1 satoshi
Removed all millisatoshi, nano and pico bitcoin denominations and tests from `Amount`
2024-06-18 10:26:20 +01:00
Jamil Lambert, PhD 4a9f74b55c fix missing fullstops in bitcoin rustdoc
Added missing fullstops to the rustdoc titles for everything on the main page of the bitcoin crate
2024-06-06 16:37:12 +01:00
Tobin C. Harding 1173f3fbff
units: Add BlockHeight and BlockInterval types
Add two simple integer wrapper types for abstracting over block
height (from genesis block) and block interval.

This does not include hex because block height is typically written in
decimal.

These types are very thin wrappers, their usecase is to assist in code
readability instead of enforcing any logic.
2024-05-30 16:49:30 +10:00
Andrew Poelstra 6d06a32d9c
Merge rust-bitcoin/rust-bitcoin#2755: Fix units public re-exports
d2a597c90d unit: Group re-exports (Tobin C. Harding)
d242125ae4 units: Fix error re-exports (Tobin C. Harding)

Pull request description:

  First patch is the meat and potatoes, second one is just a trivial refactor to the same code, done separately so as to make the changes in the first patch more clear.

  From patch 1
  ```
      units: Fix error re-exports

      Currently we re-export two error types at the crate root, this is
      surprising because:

      - Why not none or all the rest?
      - Why these two?

      Observe that the `ParseIntError` is special in that it is used by
      other modules so its good to have at the crate root (other errors are
      expected to be used with a module prefix eg, `amount::ParseError`).

      There is no obvious reason why `ParseAmountError` is re-exported.

      Comment and doc inline the `ParesIntError`, remove the re-export of
      the `ParseAmountError`.
  ```

ACKs for top commit:
  apoelstra:
    ACK d2a597c90d

Tree-SHA512: 38d3f590357e66d07cbd7fedff134c39e0920e076ea99cb34ba276749a14695d428345d7b0f9ec8222f7899cb57e7c97068d3b6e7b2a9be25a0278e0a1abf762
2024-05-28 15:46:53 +00:00
Fmt Bot 4499c4c046 2024-05-26 automated rustfmt nightly 2024-05-26 01:07:01 +00:00
Andrew Poelstra e96961f333
Merge rust-bitcoin/rust-bitcoin#2798: Standardize rustdoc subheadings
11bb1ff6ff Standardize function doc Safety, Returns and Parameters (jamil.lambert)
df83016c98 Standardize function doc Errors (jamil.lambert)
d219ceb68e Standardize function doc Examples (jamil.lambert)
233a9133d8 Standardize function doc Panics (jamil.lambert)

Pull request description:

  The subheadings in the rustdocs have been standardized according to [./CONTRIBUTING.md](https://github.com/rust-bitcoin/rust-bitcoin/blob/master/CONTRIBUTING.md):
  ```rust
  impl FooBar {
      /// Constructs a `FooBar` from a [`Baz`].
      ///
      /// # Errors
      ///
      /// Returns an error if `Baz` is not ...
      ///
      /// # Panics
      ///
      /// If the `Baz`, converted to a `usize`, is out of bounds.
      pub fn from_baz(baz: Baz) -> Result<Self, Error> {
          ...
      }
  }
  ```

ACKs for top commit:
  apoelstra:
    ACK 11bb1ff6ff
  tcharding:
    ACK 11bb1ff6ff

Tree-SHA512: 163af3cd1cfb47cea3e55eddeaeb6843ff7ec89c57354e3247d6bae85e756b183e8045c2555cfcf87e8c23c1388ff9d7592cfb6a951a37a9ec41d27263e5a2e4
2024-05-25 14:55:16 +00:00
jamil.lambert d219ceb68e Standardize function doc Examples
Changed the function docs to have the same format of
///
/// # Examples
///
/// description
2024-05-24 09:59:42 +01:00
jamil.lambert 233a9133d8 Standardize function doc Panics
Changed the function docs to have the same format of
///
/// # Panics
///
/// description
2024-05-24 09:59:29 +01: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
jamil.lambert 101378c4d0 Removed //! spare line at end of headers
Some of the headers had a //! at the end but most didn't. They have all been removed in units/src/ to make the files consistent
2024-05-22 12:29:41 +01:00
Tobin C. Harding d2a597c90d
unit: Group re-exports
As is customary group the public re-exports differently to other use
statements and tell rustfmt to skip them.

Refactor only, no logic changes.
2024-05-08 11:53:53 +10:00
Tobin C. Harding d242125ae4
units: Fix error re-exports
Currently we re-export two error types at the crate root, this is
surprising because:

- Why not none or all the rest?
- Why these two?

Observe that the `ParseIntError` is special in that it is used by
other modules so its good to have at the crate root (other errors are
expected to be used with a module prefix eg, `amount::ParseError`).

There is no obvious reason why `ParseAmountError` is re-exported.

Comment and doc inline the `ParesIntError`, remove the re-export of
the `ParseAmountError`.
2024-05-08 11:51:58 +10:00
Tobin C. Harding e47e57c265
Fix kani test
In a kani test we are checking if the result of `to_unsigned()` is an
error but setting `is_signed` to `true`, this field represents the
signed-ness of the return type of `to_unsigned` (`Amount`) so should be
`false`.

Fix kani daily job.
2024-04-19 06:15:49 +10:00
Tobin C. Harding 5981b15902
kani: Rename tests
The tests currently include the word "add" but they test addition as
well as subtraction. Elect to keep the multiple assertions per test and
just make the names more general.
2024-04-16 10:05:07 +10:00
Tobin C. Harding 17bacc6fb6
kani: Remove redundant import
`cargo` is reporting a redundant import warning:

 warning: the item `TryInto` is imported redundantly

Remove the import statement from the `verification` module.
2024-04-16 06:18:06 +10:00
Andrew Poelstra 163bf64fcc
Merge rust-bitcoin/rust-bitcoin#2668: Automated nightly rustfmt (2024-04-07)
747ca578dd 2024-04-07 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 747ca578dd
  tcharding:
    ACK 747ca578dd

Tree-SHA512: c4387388f2cfcb9b9aacf7023d024379cbf073166f04fcba4b4c0d66972ee109f4c9f66e0d8ed1d57c419266680a9e6953a5e38ecb0aa937e6c368d59aaba976
2024-04-08 22:28:08 +00:00
Andrew Poelstra 5c56b69eed
Merge rust-bitcoin/rust-bitcoin#2667: Remove deprecated legacy numeric methods
051c358bcb Remove deprecated legacy numeric methods (Divyansh Gupta)

Pull request description:

  As `rustc 1.79.0-nightly (9d79cd5f7 2024-04-05)` is released which solves the issue mentioned , but the release has deperacted legacy numeric methods.
  Thus replaced `u16::max_value()` etc with `u32::MAX` & `core::u16` to directly `u16`.

  fix #2639

ACKs for top commit:
  tcharding:
    ACK 051c358bcb
  apoelstra:
    ACK 051c358bcb thanks! I will remove an equivalent commit from my #2669

Tree-SHA512: c08c856f7f3b281417c29283351eac5e0f75cc1c8d23d9aae58d969219a327b2337fe57932053e53773ebb9dbec04254f90149266b6639a66c5c09f2ad1675ef
2024-04-07 15:15:47 +00:00
Fmt Bot 747ca578dd 2024-04-07 automated rustfmt nightly 2024-04-07 01:03:23 +00:00