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

Commit f4090c5

Browse files
committed
add check_platform function
1 parent ef4b048 commit f4090c5

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

README.md

Lines changed: 2 additions & 1 deletion
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)

src/lib.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ 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+
pub fn check_platform() {
48+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
49+
50+
// The upper and lower bounds of `SystemTime` are not part of its public contract and are
51+
// platform specific. That's why we have to test if our assumptions regarding these bounds
52+
// hold on the target platform.
53+
//
54+
// If this test fails on your platform, please don't use the library and open an issue
55+
// instead so we can resolve the situation. Currently this library is tested on:
56+
// * Linux (64bit)
57+
let fail_date = UNIX_EPOCH + Duration::from_secs(SYSTEM_TIME_MAX_UNIX_TIMESTAMP);
58+
let year = Duration::from_secs(60 * 60 * 24 * 365);
59+
60+
// Make sure that the library will keep working for another year
61+
assert!(fail_date.duration_since(SystemTime::now()).unwrap() > year);
62+
63+
let max_ts = PositiveTimestamp::from_unix_timestamp(
64+
SYSTEM_TIME_MAX_UNIX_TIMESTAMP - MAX_EXPIRY_TIME
65+
).unwrap();
66+
let max_exp = ::ExpiryTime::from_seconds(MAX_EXPIRY_TIME).unwrap();
67+
68+
assert_eq!(
69+
(*max_ts.as_time() + *max_exp.as_duration()).duration_since(UNIX_EPOCH).unwrap().as_secs(),
70+
SYSTEM_TIME_MAX_UNIX_TIMESTAMP
71+
);
72+
}
73+
74+
4675
/// Builder for `Invoice`s. It's the most convenient and advised way to use this library. It ensures
4776
/// that only a semantically and syntactically correct Invoice can be built using it.
4877
///
@@ -1126,30 +1155,7 @@ mod test {
11261155

11271156
#[test]
11281157
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-
);
1158+
::check_platform();
11531159

11541160
assert_eq!(
11551161
::PositiveTimestamp::from_unix_timestamp(::SYSTEM_TIME_MAX_UNIX_TIMESTAMP + 1),

0 commit comments

Comments
 (0)