-
Notifications
You must be signed in to change notification settings - Fork 402
LSPS2: Add TimeProvider trait for expiry checks in no-std and std builds #3746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8c51013
cca6fa0
fc18eb9
0f17d59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ use alloc::string::String; | |
|
||
use core::fmt::{self, Display}; | ||
use core::str::FromStr; | ||
use core::time::Duration; | ||
|
||
use crate::lsps0::msgs::{ | ||
LSPS0ListProtocolsRequest, LSPS0Message, LSPS0Request, LSPS0Response, | ||
|
@@ -29,8 +30,7 @@ use lightning::util::ser::{LengthLimitedRead, LengthReadable, WithoutLength}; | |
|
||
use bitcoin::secp256k1::PublicKey; | ||
|
||
#[cfg(feature = "std")] | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
use crate::sync::Arc; | ||
|
||
use serde::de::{self, MapAccess, Visitor}; | ||
use serde::ser::SerializeStruct; | ||
|
@@ -204,12 +204,8 @@ impl LSPSDateTime { | |
} | ||
|
||
/// Returns if the given time is in the past. | ||
#[cfg(feature = "std")] | ||
pub fn is_past(&self) -> bool { | ||
let now_seconds_since_epoch = SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.expect("system clock to be ahead of the unix epoch") | ||
.as_secs(); | ||
pub fn is_past(&self, time_provider: Arc<dyn TimeProvider>) -> bool { | ||
let now_seconds_since_epoch = time_provider.duration_since_epoch().as_secs(); | ||
let datetime_seconds_since_epoch = | ||
self.0.timestamp().try_into().expect("expiration to be ahead of unix epoch"); | ||
now_seconds_since_epoch > datetime_seconds_since_epoch | ||
|
@@ -784,3 +780,24 @@ pub(crate) mod u32_fee_rate { | |
Ok(FeeRate::from_sat_per_kwu(fee_rate_sat_kwu as u64)) | ||
} | ||
} | ||
|
||
/// Trait defining a time provider | ||
/// | ||
/// This trait is used to provide the current time service operations and to convert between timestamps and durations. | ||
pub trait TimeProvider { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR is extracting some of the code from LSPS5 PR. My idea was to get this PR merged and then rebase LSPS5 on top of it, that way we would lose some commits from the LSPS5 PR and make it a bit smaller (same idea with the solution to this issue #3459 (PR coming soon), where I'm extracting some of the code written in the LSPS5 PR and putting it in a new PR, that way making the LSPS5 PR smaller) |
||
/// Get the current time as a duration since the Unix epoch. | ||
fn duration_since_epoch(&self) -> Duration; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this name is sort of standard in LDK, but to me something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disagree, exactly because you'd expect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to change it now. As you say, keeping it consistent is a good API feature. For me as a developer though, this isn't the most descriptive name. |
||
} | ||
|
||
/// Default time provider using the system clock. | ||
#[derive(Clone, Debug)] | ||
#[cfg(feature = "time")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I re-read the PR description, but was still thinking why isn't this just std gated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for not mentioning this in the PR description (I’ll update it shortly). This change comes from a discussion in this PR, specifically this comment from tnull:
@tnull tagging Elias for review and input. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But if those users run with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, but we still shouldn't expose a 'default' API that would panic if used? That said, given that it requires There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If some platforms have an incomplete implementation of the std feature, is it the responsibility of this library to protect against hitting the missing part via a feature flag? I also wouldn't make it a 'default' API, but just require a time provider to be supplied. Overall it seems to me that a |
||
pub struct DefaultTimeProvider; | ||
|
||
#[cfg(feature = "time")] | ||
impl TimeProvider for DefaultTimeProvider { | ||
fn duration_since_epoch(&self) -> Duration { | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disabling
SystemTIme::now
without disabling all of std: couldn't this be realized by just passing in another time provider than the std one, without requiring this flag?