Skip to content

Add is_expired_no_std to Offer #2689

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

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lightning/src/offers/offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ impl Offer {
self.contents.is_expired()
}

/// Whether the offer has expired given the duration since the Unix epoch.
pub fn is_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add this to Refund? Also, would you mind updating builds_offer_with_absolute_expiry and builds_refund_with_absolute_expiry to call is_expired_no_std as well?

self.contents.is_expired_no_std(duration_since_epoch)
}

/// Returns whether the given quantity is valid for the offer.
pub fn is_valid_quantity(&self, quantity: u64) -> bool {
self.contents.is_valid_quantity(quantity)
Expand Down Expand Up @@ -1192,13 +1197,15 @@ mod tests {
fn builds_offer_with_absolute_expiry() {
let future_expiry = Duration::from_secs(u64::max_value());
let past_expiry = Duration::from_secs(0);
let now = future_expiry - Duration::from_secs(1_000);

let offer = OfferBuilder::new("foo".into(), pubkey(42))
.absolute_expiry(future_expiry)
.build()
.unwrap();
#[cfg(feature = "std")]
assert!(!offer.is_expired());
assert!(!offer.is_expired_no_std(now));
assert_eq!(offer.absolute_expiry(), Some(future_expiry));
assert_eq!(offer.as_tlv_stream().absolute_expiry, Some(future_expiry.as_secs()));

Expand All @@ -1209,6 +1216,7 @@ mod tests {
.unwrap();
#[cfg(feature = "std")]
assert!(offer.is_expired());
assert!(offer.is_expired_no_std(now));
assert_eq!(offer.absolute_expiry(), Some(past_expiry));
assert_eq!(offer.as_tlv_stream().absolute_expiry, Some(past_expiry.as_secs()));
}
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/offers/refund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ impl Refund {
self.contents.is_expired()
}

/// Whether the refund has expired given the duration since the Unix epoch.
pub fn is_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
self.contents.is_expired_no_std(duration_since_epoch)
}

/// The issuer of the refund, possibly beginning with `user@domain` or `domain`. Intended to be
/// displayed to the user but with the caveat that it has not been verified in any way.
pub fn issuer(&self) -> Option<PrintableString> {
Expand Down Expand Up @@ -993,6 +998,7 @@ mod tests {
fn builds_refund_with_absolute_expiry() {
let future_expiry = Duration::from_secs(u64::max_value());
let past_expiry = Duration::from_secs(0);
let now = future_expiry - Duration::from_secs(1_000);

let refund = RefundBuilder::new("foo".into(), vec![1; 32], payer_pubkey(), 1000).unwrap()
.absolute_expiry(future_expiry)
Expand All @@ -1001,6 +1007,7 @@ mod tests {
let (_, tlv_stream, _) = refund.as_tlv_stream();
#[cfg(feature = "std")]
assert!(!refund.is_expired());
assert!(!refund.is_expired_no_std(now));
assert_eq!(refund.absolute_expiry(), Some(future_expiry));
assert_eq!(tlv_stream.absolute_expiry, Some(future_expiry.as_secs()));

Expand All @@ -1012,6 +1019,7 @@ mod tests {
let (_, tlv_stream, _) = refund.as_tlv_stream();
#[cfg(feature = "std")]
assert!(refund.is_expired());
assert!(refund.is_expired_no_std(now));
assert_eq!(refund.absolute_expiry(), Some(past_expiry));
assert_eq!(tlv_stream.absolute_expiry, Some(past_expiry.as_secs()));
}
Expand Down