@@ -92,13 +92,6 @@ const TIMESTAMP_BITS: usize = 35;
9292/// allowing for adding an expiry without overflowing.
9393const MAX_TIMESTAMP : u64 = core:: u64:: MAX >> ( 64 - TIMESTAMP_BITS ) ;
9494
95- /// The maximum expiry allowed, represented as a [`Duration`] since the invoice timestamp.
96- const MAX_EXPIRY_TIME : u64 = core:: u64:: MAX - MAX_TIMESTAMP ;
97-
98- /// Assert that the maximum expiry represented as a [`Duration`] since the UNIX epoch does not
99- /// exceed [`u64::MAX`].
100- const _MAX_EXPIRY_TIMESTAMP: u64 = MAX_TIMESTAMP + MAX_EXPIRY_TIME ;
101-
10295/// Default expiry time as defined by [BOLT 11].
10396///
10497/// [BOLT 11]: https://github.com/lightningnetwork/lightning-rfc/blob/master/11-payment-encoding.md
@@ -388,10 +381,6 @@ pub struct PayeePubKey(pub PublicKey);
388381
389382/// Positive duration that defines when (relatively to the timestamp) in the future the invoice
390383/// expires
391- ///
392- /// # Invariants
393- /// The number of seconds this expiry time represents has to be in the range
394- /// `0...MAX_EXPIRY_TIME` to avoid overflows when adding it to a timestamp.
395384#[ derive( Clone , Debug , Hash , Eq , PartialEq ) ]
396385pub struct ExpiryTime ( Duration ) ;
397386
@@ -499,10 +488,7 @@ impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool> InvoiceBui
499488
500489 /// Sets the expiry time
501490 pub fn expiry_time ( mut self , expiry_time : Duration ) -> Self {
502- match ExpiryTime :: from_duration ( expiry_time) {
503- Ok ( t) => self . tagged_fields . push ( TaggedField :: ExpiryTime ( t) ) ,
504- Err ( e) => self . error = Some ( e) ,
505- } ;
491+ self . tagged_fields . push ( TaggedField :: ExpiryTime ( ExpiryTime :: from_duration ( expiry_time) ) ) ;
506492 self
507493 }
508494
@@ -1222,7 +1208,7 @@ impl Invoice {
12221208 /// Returns whether the expiry time would pass at the given point in time.
12231209 /// `at_time` is the timestamp as a duration since the UNIX epoch.
12241210 pub fn would_expire ( & self , at_time : Duration ) -> bool {
1225- self . duration_since_epoch ( ) + self . expiry_time ( ) < at_time
1211+ self . duration_since_epoch ( ) . saturating_add ( self . expiry_time ( ) ) < at_time
12261212 }
12271213
12281214 /// Returns the invoice's `min_final_cltv_expiry` time, if present, otherwise
@@ -1343,26 +1329,14 @@ impl Deref for PayeePubKey {
13431329}
13441330
13451331impl ExpiryTime {
1346- /// Construct an `ExpiryTime` from seconds. If there exists a `PositiveTimestamp` which would
1347- /// overflow on adding the `ExpiryTime` to it then this function will return a
1348- /// `CreationError::ExpiryTimeOutOfBounds`.
1349- pub fn from_seconds ( seconds : u64 ) -> Result < ExpiryTime , CreationError > {
1350- if seconds <= MAX_EXPIRY_TIME {
1351- Ok ( ExpiryTime ( Duration :: from_secs ( seconds) ) )
1352- } else {
1353- Err ( CreationError :: ExpiryTimeOutOfBounds )
1354- }
1332+ /// Construct an `ExpiryTime` from seconds.
1333+ pub fn from_seconds ( seconds : u64 ) -> ExpiryTime {
1334+ ExpiryTime ( Duration :: from_secs ( seconds) )
13551335 }
13561336
1357- /// Construct an `ExpiryTime` from a `Duration`. If there exists a `PositiveTimestamp` which
1358- /// would overflow on adding the `ExpiryTime` to it then this function will return a
1359- /// `CreationError::ExpiryTimeOutOfBounds`.
1360- pub fn from_duration ( duration : Duration ) -> Result < ExpiryTime , CreationError > {
1361- if duration. as_secs ( ) <= MAX_EXPIRY_TIME {
1362- Ok ( ExpiryTime ( duration) )
1363- } else {
1364- Err ( CreationError :: ExpiryTimeOutOfBounds )
1365- }
1337+ /// Construct an `ExpiryTime` from a `Duration`.
1338+ pub fn from_duration ( duration : Duration ) -> ExpiryTime {
1339+ ExpiryTime ( duration)
13661340 }
13671341
13681342 /// Returns the expiry time in seconds
@@ -1431,12 +1405,9 @@ pub enum CreationError {
14311405 /// The specified route has too many hops and can't be encoded
14321406 RouteTooLong ,
14331407
1434- /// The unix timestamp of the supplied date is <0 or can't be represented as `SystemTime`
1408+ /// The unix timestamp of the supplied date is less than zero or greater than 35-bits
14351409 TimestampOutOfBounds ,
14361410
1437- /// The supplied expiry time could cause an overflow if added to a `PositiveTimestamp`
1438- ExpiryTimeOutOfBounds ,
1439-
14401411 /// The supplied millisatoshi amount was greater than the total bitcoin supply.
14411412 InvalidAmount ,
14421413}
@@ -1446,8 +1417,7 @@ impl Display for CreationError {
14461417 match self {
14471418 CreationError :: DescriptionTooLong => f. write_str ( "The supplied description string was longer than 639 bytes" ) ,
14481419 CreationError :: RouteTooLong => f. write_str ( "The specified route has too many hops and can't be encoded" ) ,
1449- CreationError :: TimestampOutOfBounds => f. write_str ( "The unix timestamp of the supplied date is <0 or can't be represented as `SystemTime`" ) ,
1450- CreationError :: ExpiryTimeOutOfBounds => f. write_str ( "The supplied expiry time could cause an overflow if added to a `PositiveTimestamp`" ) ,
1420+ CreationError :: TimestampOutOfBounds => f. write_str ( "The unix timestamp of the supplied date is less than zero or greater than 35-bits" ) ,
14511421 CreationError :: InvalidAmount => f. write_str ( "The supplied millisatoshi amount was greater than the total bitcoin supply" ) ,
14521422 }
14531423 }
@@ -1543,11 +1513,6 @@ mod test {
15431513 :: PositiveTimestamp :: from_unix_timestamp( :: MAX_TIMESTAMP + 1 ) ,
15441514 Err ( :: CreationError :: TimestampOutOfBounds )
15451515 ) ;
1546-
1547- assert_eq ! (
1548- :: ExpiryTime :: from_seconds( :: MAX_EXPIRY_TIME + 1 ) ,
1549- Err ( :: CreationError :: ExpiryTimeOutOfBounds )
1550- ) ;
15511516 }
15521517
15531518 #[ test]
0 commit comments