@@ -92,13 +92,6 @@ const TIMESTAMP_BITS: usize = 35;
92
92
/// allowing for adding an expiry without overflowing.
93
93
const MAX_TIMESTAMP : u64 = core:: u64:: MAX >> ( 64 - TIMESTAMP_BITS ) ;
94
94
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
-
102
95
/// Default expiry time as defined by [BOLT 11].
103
96
///
104
97
/// [BOLT 11]: https://github.com/lightningnetwork/lightning-rfc/blob/master/11-payment-encoding.md
@@ -388,10 +381,6 @@ pub struct PayeePubKey(pub PublicKey);
388
381
389
382
/// Positive duration that defines when (relatively to the timestamp) in the future the invoice
390
383
/// 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.
395
384
#[ derive( Clone , Debug , Hash , Eq , PartialEq ) ]
396
385
pub struct ExpiryTime ( Duration ) ;
397
386
@@ -499,10 +488,7 @@ impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool> InvoiceBui
499
488
500
489
/// Sets the expiry time
501
490
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) ) ) ;
506
492
self
507
493
}
508
494
@@ -1222,7 +1208,9 @@ impl Invoice {
1222
1208
/// Returns whether the expiry time would pass at the given point in time.
1223
1209
/// `at_time` is the timestamp as a duration since the UNIX epoch.
1224
1210
pub fn would_expire ( & self , at_time : Duration ) -> bool {
1225
- self . duration_since_epoch ( ) + self . expiry_time ( ) < at_time
1211
+ self . duration_since_epoch ( )
1212
+ . checked_add ( self . expiry_time ( ) )
1213
+ . unwrap_or_else ( || Duration :: new ( u64:: max_value ( ) , 1_000_000_000 - 1 ) ) < at_time
1226
1214
}
1227
1215
1228
1216
/// Returns the invoice's `min_final_cltv_expiry` time, if present, otherwise
@@ -1343,26 +1331,14 @@ impl Deref for PayeePubKey {
1343
1331
}
1344
1332
1345
1333
impl 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
- }
1334
+ /// Construct an `ExpiryTime` from seconds.
1335
+ pub fn from_seconds ( seconds : u64 ) -> ExpiryTime {
1336
+ ExpiryTime ( Duration :: from_secs ( seconds) )
1355
1337
}
1356
1338
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
- }
1339
+ /// Construct an `ExpiryTime` from a `Duration`.
1340
+ pub fn from_duration ( duration : Duration ) -> ExpiryTime {
1341
+ ExpiryTime ( duration)
1366
1342
}
1367
1343
1368
1344
/// Returns the expiry time in seconds
@@ -1431,12 +1407,9 @@ pub enum CreationError {
1431
1407
/// The specified route has too many hops and can't be encoded
1432
1408
RouteTooLong ,
1433
1409
1434
- /// The unix timestamp of the supplied date is <0 or can't be represented as `SystemTime`
1410
+ /// The unix timestamp of the supplied date is less than zero or greater than 35-bits
1435
1411
TimestampOutOfBounds ,
1436
1412
1437
- /// The supplied expiry time could cause an overflow if added to a `PositiveTimestamp`
1438
- ExpiryTimeOutOfBounds ,
1439
-
1440
1413
/// The supplied millisatoshi amount was greater than the total bitcoin supply.
1441
1414
InvalidAmount ,
1442
1415
}
@@ -1446,8 +1419,7 @@ impl Display for CreationError {
1446
1419
match self {
1447
1420
CreationError :: DescriptionTooLong => f. write_str ( "The supplied description string was longer than 639 bytes" ) ,
1448
1421
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`" ) ,
1422
+ CreationError :: TimestampOutOfBounds => f. write_str ( "The unix timestamp of the supplied date is less than zero or greater than 35-bits" ) ,
1451
1423
CreationError :: InvalidAmount => f. write_str ( "The supplied millisatoshi amount was greater than the total bitcoin supply" ) ,
1452
1424
}
1453
1425
}
@@ -1543,11 +1515,6 @@ mod test {
1543
1515
:: PositiveTimestamp :: from_unix_timestamp( :: MAX_TIMESTAMP + 1 ) ,
1544
1516
Err ( :: CreationError :: TimestampOutOfBounds )
1545
1517
) ;
1546
-
1547
- assert_eq ! (
1548
- :: ExpiryTime :: from_seconds( :: MAX_EXPIRY_TIME + 1 ) ,
1549
- Err ( :: CreationError :: ExpiryTimeOutOfBounds )
1550
- ) ;
1551
1518
}
1552
1519
1553
1520
#[ test]
0 commit comments