X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Foffers%2Frefund.rs;h=ce39841df8c9750d9f4e7b717d1e3fb50e967d1a;hb=ec928d55b480254f2ce3457a5c219ed115fdf9ef;hp=ecafb2bb5c7b3f3cb4973016ba117cef9ed638ae;hpb=7c6e62f42359407443c9baacd4ac12ffb2d772b4;p=rust-lightning diff --git a/lightning/src/offers/refund.rs b/lightning/src/offers/refund.rs index ecafb2bb..ce39841d 100644 --- a/lightning/src/offers/refund.rs +++ b/lightning/src/offers/refund.rs @@ -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 { @@ -540,13 +545,16 @@ impl RefundContents { #[cfg(feature = "std")] pub(super) fn is_expired(&self) -> bool { - match self.absolute_expiry { - Some(seconds_from_epoch) => match SystemTime::UNIX_EPOCH.elapsed() { - Ok(elapsed) => elapsed > seconds_from_epoch, - Err(_) => false, - }, - None => false, - } + SystemTime::UNIX_EPOCH + .elapsed() + .map(|duration_since_epoch| self.is_expired_no_std(duration_since_epoch)) + .unwrap_or(false) + } + + pub(super) fn is_expired_no_std(&self, duration_since_epoch: Duration) -> bool { + self.absolute_expiry + .map(|absolute_expiry| duration_since_epoch > absolute_expiry) + .unwrap_or(false) } pub fn issuer(&self) -> Option { @@ -990,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) @@ -998,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())); @@ -1009,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())); }