@@ -59,6 +59,7 @@ use routing::network_graph::{NetworkGraph, NodeId};
59
59
use routing:: router:: RouteHop ;
60
60
use util:: ser:: { Readable , ReadableArgs , Writeable , Writer } ;
61
61
use util:: logger:: Logger ;
62
+ use util:: time:: Time ;
62
63
63
64
use prelude:: * ;
64
65
use core:: fmt;
@@ -262,7 +263,9 @@ pub type Scorer = ScorerUsingTime::<ConfiguredTime>;
262
263
#[ cfg( not( feature = "no-std" ) ) ]
263
264
type ConfiguredTime = std:: time:: Instant ;
264
265
#[ cfg( feature = "no-std" ) ]
265
- type ConfiguredTime = time:: Eternity ;
266
+ use util:: time:: Eternity ;
267
+ #[ cfg( feature = "no-std" ) ]
268
+ type ConfiguredTime = Eternity ;
266
269
267
270
// Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
268
271
// doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
@@ -1327,83 +1330,11 @@ impl<T: Time> Readable for ChannelLiquidity<T> {
1327
1330
}
1328
1331
}
1329
1332
1330
- pub ( crate ) mod time {
1331
- use core:: ops:: Sub ;
1332
- use core:: time:: Duration ;
1333
- /// A measurement of time.
1334
- pub trait Time : Copy + Sub < Duration , Output = Self > where Self : Sized {
1335
- /// Returns an instance corresponding to the current moment.
1336
- fn now ( ) -> Self ;
1337
-
1338
- /// Returns the amount of time elapsed since `self` was created.
1339
- fn elapsed ( & self ) -> Duration ;
1340
-
1341
- /// Returns the amount of time passed between `earlier` and `self`.
1342
- fn duration_since ( & self , earlier : Self ) -> Duration ;
1343
-
1344
- /// Returns the amount of time passed since the beginning of [`Time`].
1345
- ///
1346
- /// Used during (de-)serialization.
1347
- fn duration_since_epoch ( ) -> Duration ;
1348
- }
1349
-
1350
- /// A state in which time has no meaning.
1351
- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
1352
- pub struct Eternity ;
1353
-
1354
- #[ cfg( not( feature = "no-std" ) ) ]
1355
- impl Time for std:: time:: Instant {
1356
- fn now ( ) -> Self {
1357
- std:: time:: Instant :: now ( )
1358
- }
1359
-
1360
- fn duration_since ( & self , earlier : Self ) -> Duration {
1361
- self . duration_since ( earlier)
1362
- }
1363
-
1364
- fn duration_since_epoch ( ) -> Duration {
1365
- use std:: time:: SystemTime ;
1366
- SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( )
1367
- }
1368
-
1369
- fn elapsed ( & self ) -> Duration {
1370
- std:: time:: Instant :: elapsed ( self )
1371
- }
1372
- }
1373
-
1374
- impl Time for Eternity {
1375
- fn now ( ) -> Self {
1376
- Self
1377
- }
1378
-
1379
- fn duration_since ( & self , _earlier : Self ) -> Duration {
1380
- Duration :: from_secs ( 0 )
1381
- }
1382
-
1383
- fn duration_since_epoch ( ) -> Duration {
1384
- Duration :: from_secs ( 0 )
1385
- }
1386
-
1387
- fn elapsed ( & self ) -> Duration {
1388
- Duration :: from_secs ( 0 )
1389
- }
1390
- }
1391
-
1392
- impl Sub < Duration > for Eternity {
1393
- type Output = Self ;
1394
-
1395
- fn sub ( self , _other : Duration ) -> Self {
1396
- self
1397
- }
1398
- }
1399
- }
1400
-
1401
- pub ( crate ) use self :: time:: Time ;
1402
-
1403
1333
#[ cfg( test) ]
1404
1334
mod tests {
1405
- use super :: { ChannelLiquidity , ProbabilisticScoringParameters , ProbabilisticScorerUsingTime , ScoringParameters , ScorerUsingTime , Time } ;
1406
- use super :: time:: Eternity ;
1335
+ use super :: { ChannelLiquidity , ProbabilisticScoringParameters , ProbabilisticScorerUsingTime , ScoringParameters , ScorerUsingTime } ;
1336
+ use util:: time:: Time ;
1337
+ use util:: time:: tests:: SinceEpoch ;
1407
1338
1408
1339
use ln:: features:: { ChannelFeatures , NodeFeatures } ;
1409
1340
use ln:: msgs:: { ChannelAnnouncement , ChannelUpdate , OptionalField , UnsignedChannelAnnouncement , UnsignedChannelUpdate } ;
@@ -1418,80 +1349,9 @@ mod tests {
1418
1349
use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
1419
1350
use bitcoin:: network:: constants:: Network ;
1420
1351
use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
1421
- use core:: cell:: Cell ;
1422
- use core:: ops:: Sub ;
1423
1352
use core:: time:: Duration ;
1424
1353
use io;
1425
1354
1426
- // `Time` tests
1427
-
1428
- /// Time that can be advanced manually in tests.
1429
- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
1430
- struct SinceEpoch ( Duration ) ;
1431
-
1432
- impl SinceEpoch {
1433
- thread_local ! {
1434
- static ELAPSED : Cell <Duration > = core:: cell:: Cell :: new( Duration :: from_secs( 0 ) ) ;
1435
- }
1436
-
1437
- fn advance ( duration : Duration ) {
1438
- Self :: ELAPSED . with ( |elapsed| elapsed. set ( elapsed. get ( ) + duration) )
1439
- }
1440
- }
1441
-
1442
- impl Time for SinceEpoch {
1443
- fn now ( ) -> Self {
1444
- Self ( Self :: duration_since_epoch ( ) )
1445
- }
1446
-
1447
- fn duration_since ( & self , earlier : Self ) -> Duration {
1448
- self . 0 - earlier. 0
1449
- }
1450
-
1451
- fn duration_since_epoch ( ) -> Duration {
1452
- Self :: ELAPSED . with ( |elapsed| elapsed. get ( ) )
1453
- }
1454
-
1455
- fn elapsed ( & self ) -> Duration {
1456
- Self :: duration_since_epoch ( ) - self . 0
1457
- }
1458
- }
1459
-
1460
- impl Sub < Duration > for SinceEpoch {
1461
- type Output = Self ;
1462
-
1463
- fn sub ( self , other : Duration ) -> Self {
1464
- Self ( self . 0 - other)
1465
- }
1466
- }
1467
-
1468
- #[ test]
1469
- fn time_passes_when_advanced ( ) {
1470
- let now = SinceEpoch :: now ( ) ;
1471
- assert_eq ! ( now. elapsed( ) , Duration :: from_secs( 0 ) ) ;
1472
-
1473
- SinceEpoch :: advance ( Duration :: from_secs ( 1 ) ) ;
1474
- SinceEpoch :: advance ( Duration :: from_secs ( 1 ) ) ;
1475
-
1476
- let elapsed = now. elapsed ( ) ;
1477
- let later = SinceEpoch :: now ( ) ;
1478
-
1479
- assert_eq ! ( elapsed, Duration :: from_secs( 2 ) ) ;
1480
- assert_eq ! ( later - elapsed, now) ;
1481
- }
1482
-
1483
- #[ test]
1484
- fn time_never_passes_in_an_eternity ( ) {
1485
- let now = Eternity :: now ( ) ;
1486
- let elapsed = now. elapsed ( ) ;
1487
- let later = Eternity :: now ( ) ;
1488
-
1489
- assert_eq ! ( now. elapsed( ) , Duration :: from_secs( 0 ) ) ;
1490
- assert_eq ! ( later - elapsed, now) ;
1491
- }
1492
-
1493
- // `Scorer` tests
1494
-
1495
1355
/// A scorer for testing with time that can be manually advanced.
1496
1356
type Scorer = ScorerUsingTime :: < SinceEpoch > ;
1497
1357
0 commit comments