@@ -252,7 +252,9 @@ pub type Scorer = ScorerUsingTime::<ConfiguredTime>;
252
252
#[ cfg( not( feature = "no-std" ) ) ]
253
253
type ConfiguredTime = std:: time:: Instant ;
254
254
#[ cfg( feature = "no-std" ) ]
255
- type ConfiguredTime = time:: Eternity ;
255
+ use util:: time:: Eternity ;
256
+ #[ cfg( feature = "no-std" ) ]
257
+ type ConfiguredTime = Eternity ;
256
258
257
259
// Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
258
260
// doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
@@ -1267,83 +1269,12 @@ impl<T: Time> Readable for ChannelLiquidity<T> {
1267
1269
}
1268
1270
}
1269
1271
1270
- pub ( crate ) mod time {
1271
- use core:: ops:: Sub ;
1272
- use core:: time:: Duration ;
1273
- /// A measurement of time.
1274
- pub trait Time : Copy + Sub < Duration , Output = Self > where Self : Sized {
1275
- /// Returns an instance corresponding to the current moment.
1276
- fn now ( ) -> Self ;
1277
-
1278
- /// Returns the amount of time elapsed since `self` was created.
1279
- fn elapsed ( & self ) -> Duration ;
1280
-
1281
- /// Returns the amount of time passed between `earlier` and `self`.
1282
- fn duration_since ( & self , earlier : Self ) -> Duration ;
1283
-
1284
- /// Returns the amount of time passed since the beginning of [`Time`].
1285
- ///
1286
- /// Used during (de-)serialization.
1287
- fn duration_since_epoch ( ) -> Duration ;
1288
- }
1289
-
1290
- /// A state in which time has no meaning.
1291
- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
1292
- pub struct Eternity ;
1293
-
1294
- #[ cfg( not( feature = "no-std" ) ) ]
1295
- impl Time for std:: time:: Instant {
1296
- fn now ( ) -> Self {
1297
- std:: time:: Instant :: now ( )
1298
- }
1299
-
1300
- fn duration_since ( & self , earlier : Self ) -> Duration {
1301
- self . duration_since ( earlier)
1302
- }
1303
-
1304
- fn duration_since_epoch ( ) -> Duration {
1305
- use std:: time:: SystemTime ;
1306
- SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( )
1307
- }
1308
-
1309
- fn elapsed ( & self ) -> Duration {
1310
- std:: time:: Instant :: elapsed ( self )
1311
- }
1312
- }
1313
-
1314
- impl Time for Eternity {
1315
- fn now ( ) -> Self {
1316
- Self
1317
- }
1318
-
1319
- fn duration_since ( & self , _earlier : Self ) -> Duration {
1320
- Duration :: from_secs ( 0 )
1321
- }
1322
-
1323
- fn duration_since_epoch ( ) -> Duration {
1324
- Duration :: from_secs ( 0 )
1325
- }
1326
-
1327
- fn elapsed ( & self ) -> Duration {
1328
- Duration :: from_secs ( 0 )
1329
- }
1330
- }
1331
-
1332
- impl Sub < Duration > for Eternity {
1333
- type Output = Self ;
1334
-
1335
- fn sub ( self , _other : Duration ) -> Self {
1336
- self
1337
- }
1338
- }
1339
- }
1340
-
1341
- pub ( crate ) use self :: time:: Time ;
1272
+ use util:: time:: Time ;
1342
1273
1343
1274
#[ cfg( test) ]
1344
1275
mod tests {
1345
1276
use super :: { ChannelLiquidity , ProbabilisticScoringParameters , ProbabilisticScorerUsingTime , ScoringParameters , ScorerUsingTime , Time } ;
1346
- use super :: time:: Eternity ;
1277
+ use util :: time:: tests :: SinceEpoch ;
1347
1278
1348
1279
use ln:: features:: { ChannelFeatures , NodeFeatures } ;
1349
1280
use ln:: msgs:: { ChannelAnnouncement , ChannelUpdate , OptionalField , UnsignedChannelAnnouncement , UnsignedChannelUpdate } ;
@@ -1357,80 +1288,9 @@ mod tests {
1357
1288
use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
1358
1289
use bitcoin:: network:: constants:: Network ;
1359
1290
use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
1360
- use core:: cell:: Cell ;
1361
- use core:: ops:: Sub ;
1362
1291
use core:: time:: Duration ;
1363
1292
use io;
1364
1293
1365
- // `Time` tests
1366
-
1367
- /// Time that can be advanced manually in tests.
1368
- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
1369
- struct SinceEpoch ( Duration ) ;
1370
-
1371
- impl SinceEpoch {
1372
- thread_local ! {
1373
- static ELAPSED : Cell <Duration > = core:: cell:: Cell :: new( Duration :: from_secs( 0 ) ) ;
1374
- }
1375
-
1376
- fn advance ( duration : Duration ) {
1377
- Self :: ELAPSED . with ( |elapsed| elapsed. set ( elapsed. get ( ) + duration) )
1378
- }
1379
- }
1380
-
1381
- impl Time for SinceEpoch {
1382
- fn now ( ) -> Self {
1383
- Self ( Self :: duration_since_epoch ( ) )
1384
- }
1385
-
1386
- fn duration_since ( & self , earlier : Self ) -> Duration {
1387
- self . 0 - earlier. 0
1388
- }
1389
-
1390
- fn duration_since_epoch ( ) -> Duration {
1391
- Self :: ELAPSED . with ( |elapsed| elapsed. get ( ) )
1392
- }
1393
-
1394
- fn elapsed ( & self ) -> Duration {
1395
- Self :: duration_since_epoch ( ) - self . 0
1396
- }
1397
- }
1398
-
1399
- impl Sub < Duration > for SinceEpoch {
1400
- type Output = Self ;
1401
-
1402
- fn sub ( self , other : Duration ) -> Self {
1403
- Self ( self . 0 - other)
1404
- }
1405
- }
1406
-
1407
- #[ test]
1408
- fn time_passes_when_advanced ( ) {
1409
- let now = SinceEpoch :: now ( ) ;
1410
- assert_eq ! ( now. elapsed( ) , Duration :: from_secs( 0 ) ) ;
1411
-
1412
- SinceEpoch :: advance ( Duration :: from_secs ( 1 ) ) ;
1413
- SinceEpoch :: advance ( Duration :: from_secs ( 1 ) ) ;
1414
-
1415
- let elapsed = now. elapsed ( ) ;
1416
- let later = SinceEpoch :: now ( ) ;
1417
-
1418
- assert_eq ! ( elapsed, Duration :: from_secs( 2 ) ) ;
1419
- assert_eq ! ( later - elapsed, now) ;
1420
- }
1421
-
1422
- #[ test]
1423
- fn time_never_passes_in_an_eternity ( ) {
1424
- let now = Eternity :: now ( ) ;
1425
- let elapsed = now. elapsed ( ) ;
1426
- let later = Eternity :: now ( ) ;
1427
-
1428
- assert_eq ! ( now. elapsed( ) , Duration :: from_secs( 0 ) ) ;
1429
- assert_eq ! ( later - elapsed, now) ;
1430
- }
1431
-
1432
- // `Scorer` tests
1433
-
1434
1294
/// A scorer for testing with time that can be manually advanced.
1435
1295
type Scorer = ScorerUsingTime :: < SinceEpoch > ;
1436
1296
0 commit comments