Skip to content
This repository was archived by the owner on Apr 13, 2021. It is now read-only.

Commit f3194e5

Browse files
committed
add check_platform function
1 parent ef4b048 commit f3194e5

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ This repo provides datastructures for BOLT 11 lightning invoices.
66
It provides functions to parse and serialize invoices from and to bech32.
77

88
**Please be sure to run the test suite since we need to check assumptions
9-
regarding `SystemTime`'s bounds on your platform.**
9+
regarding `SystemTime`'s bounds on your platform. You can also call `check_platform`
10+
on startup or in your test suite to do so.**
1011

1112
## Contributing
1213
* same coding style standard as [rust-bitcoin/rust-lightning](https://github.com/rust-bitcoin/rust-lightning)

Diff for: src/lib.rs

+42-24
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,47 @@ fn __system_time_size_check() {
4343
}
4444

4545

46+
/// **Call this function on startup to ensure that all assumptions about the platform are valid.**
47+
///
48+
/// Unfortunately we have to make assumptions about the upper bounds of the `SystemTime` type on
49+
/// your platform which we can't fully verify at compile time and which isn't part of it's contract.
50+
/// To our best knowledge our assumptions hold for all platforms officially supported by rust, but
51+
/// since this check is fast we recommend to do it anyway.
52+
///
53+
/// If this function fails this is considered a bug. Please open an issue describing your
54+
/// platform and stating your current system time.
55+
///
56+
/// # Panics
57+
/// If the check fails this function panics. By calling this function on startup you ensure that
58+
/// this wont happen at an arbitrary later point in time.
59+
pub fn check_platform() {
60+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
61+
62+
// The upper and lower bounds of `SystemTime` are not part of its public contract and are
63+
// platform specific. That's why we have to test if our assumptions regarding these bounds
64+
// hold on the target platform.
65+
//
66+
// If this test fails on your platform, please don't use the library and open an issue
67+
// instead so we can resolve the situation. Currently this library is tested on:
68+
// * Linux (64bit)
69+
let fail_date = UNIX_EPOCH + Duration::from_secs(SYSTEM_TIME_MAX_UNIX_TIMESTAMP);
70+
let year = Duration::from_secs(60 * 60 * 24 * 365);
71+
72+
// Make sure that the library will keep working for another year
73+
assert!(fail_date.duration_since(SystemTime::now()).unwrap() > year);
74+
75+
let max_ts = PositiveTimestamp::from_unix_timestamp(
76+
SYSTEM_TIME_MAX_UNIX_TIMESTAMP - MAX_EXPIRY_TIME
77+
).unwrap();
78+
let max_exp = ::ExpiryTime::from_seconds(MAX_EXPIRY_TIME).unwrap();
79+
80+
assert_eq!(
81+
(*max_ts.as_time() + *max_exp.as_duration()).duration_since(UNIX_EPOCH).unwrap().as_secs(),
82+
SYSTEM_TIME_MAX_UNIX_TIMESTAMP
83+
);
84+
}
85+
86+
4687
/// Builder for `Invoice`s. It's the most convenient and advised way to use this library. It ensures
4788
/// that only a semantically and syntactically correct Invoice can be built using it.
4889
///
@@ -1126,30 +1167,7 @@ mod test {
11261167

11271168
#[test]
11281169
fn test_system_time_bounds_assumptions() {
1129-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
1130-
1131-
// The upper and lower bounds of `SystemTime` are not part of its public contract and are
1132-
// platform specific. That's why we have to test if our assumptions regarding these bounds
1133-
// hold on the target platform.
1134-
//
1135-
// If this test fails on your platform, please don't use the library and open an issue
1136-
// instead so we can resolve the situation. Currently this library is tested on:
1137-
// * Linux (64bit)
1138-
let fail_date = UNIX_EPOCH + Duration::from_secs(::SYSTEM_TIME_MAX_UNIX_TIMESTAMP);
1139-
let year = Duration::from_secs(60 * 60 * 24 * 365);
1140-
1141-
// Make sure that the library will keep working for another year
1142-
assert!(fail_date.duration_since(SystemTime::now()).unwrap() > year);
1143-
1144-
let max_ts = ::PositiveTimestamp::from_unix_timestamp(
1145-
::SYSTEM_TIME_MAX_UNIX_TIMESTAMP - ::MAX_EXPIRY_TIME
1146-
).unwrap();
1147-
let max_exp = ::ExpiryTime::from_seconds(::MAX_EXPIRY_TIME).unwrap();
1148-
1149-
assert_eq!(
1150-
(*max_ts.as_time() + *max_exp.as_duration()).duration_since(UNIX_EPOCH).unwrap().as_secs(),
1151-
::SYSTEM_TIME_MAX_UNIX_TIMESTAMP
1152-
);
1170+
::check_platform();
11531171

11541172
assert_eq!(
11551173
::PositiveTimestamp::from_unix_timestamp(::SYSTEM_TIME_MAX_UNIX_TIMESTAMP + 1),

0 commit comments

Comments
 (0)