@@ -1064,7 +1064,10 @@ impl<T: Time> ChannelLiquidity<T> {
1064
1064
}
1065
1065
}
1066
1066
1067
- fn decayed_offset ( & self , offset : u64 , decay_params : ProbabilisticScoringDecayParameters ) -> u64 {
1067
+ fn decayed_offset (
1068
+ & self , offset : u64 , duration_since_epoch : Duration ,
1069
+ decay_params : ProbabilisticScoringDecayParameters ,
1070
+ ) -> u64 {
1068
1071
let half_life = decay_params. liquidity_offset_half_life . as_secs_f64 ( ) ;
1069
1072
if half_life != 0.0 {
1070
1073
let elapsed_time = T :: now ( ) . duration_since ( self . last_updated ) . as_secs_f64 ( ) ;
@@ -1266,44 +1269,50 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
1266
1269
1267
1270
impl < L : DerefMut < Target = u64 > , BRT : DerefMut < Target = HistoricalBucketRangeTracker > , T : Time , U : DerefMut < Target = T > > DirectedChannelLiquidity < L , BRT , T , U > {
1268
1271
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
1269
- fn failed_at_channel < Log : Deref > ( & mut self , amount_msat : u64 , chan_descr : fmt:: Arguments , logger : & Log ) where Log :: Target : Logger {
1272
+ fn failed_at_channel < Log : Deref > (
1273
+ & mut self , amount_msat : u64 , duration_since_epoch : Duration , chan_descr : fmt:: Arguments , logger : & Log
1274
+ ) where Log :: Target : Logger {
1270
1275
let existing_max_msat = self . max_liquidity_msat ( ) ;
1271
1276
if amount_msat < existing_max_msat {
1272
1277
log_debug ! ( logger, "Setting max liquidity of {} from {} to {}" , chan_descr, existing_max_msat, amount_msat) ;
1273
- self . set_max_liquidity_msat ( amount_msat) ;
1278
+ self . set_max_liquidity_msat ( amount_msat, duration_since_epoch ) ;
1274
1279
} else {
1275
1280
log_trace ! ( logger, "Max liquidity of {} is {} (already less than or equal to {})" ,
1276
1281
chan_descr, existing_max_msat, amount_msat) ;
1277
1282
}
1278
- self . update_history_buckets ( 0 ) ;
1283
+ self . update_history_buckets ( 0 , duration_since_epoch ) ;
1279
1284
}
1280
1285
1281
1286
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
1282
- fn failed_downstream < Log : Deref > ( & mut self , amount_msat : u64 , chan_descr : fmt:: Arguments , logger : & Log ) where Log :: Target : Logger {
1287
+ fn failed_downstream < Log : Deref > (
1288
+ & mut self , amount_msat : u64 , duration_since_epoch : Duration , chan_descr : fmt:: Arguments , logger : & Log
1289
+ ) where Log :: Target : Logger {
1283
1290
let existing_min_msat = self . min_liquidity_msat ( ) ;
1284
1291
if amount_msat > existing_min_msat {
1285
1292
log_debug ! ( logger, "Setting min liquidity of {} from {} to {}" , existing_min_msat, chan_descr, amount_msat) ;
1286
- self . set_min_liquidity_msat ( amount_msat) ;
1293
+ self . set_min_liquidity_msat ( amount_msat, duration_since_epoch ) ;
1287
1294
} else {
1288
1295
log_trace ! ( logger, "Min liquidity of {} is {} (already greater than or equal to {})" ,
1289
1296
chan_descr, existing_min_msat, amount_msat) ;
1290
1297
}
1291
- self . update_history_buckets ( 0 ) ;
1298
+ self . update_history_buckets ( 0 , duration_since_epoch ) ;
1292
1299
}
1293
1300
1294
1301
/// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
1295
- fn successful < Log : Deref > ( & mut self , amount_msat : u64 , chan_descr : fmt:: Arguments , logger : & Log ) where Log :: Target : Logger {
1302
+ fn successful < Log : Deref > ( & mut self ,
1303
+ amount_msat : u64 , duration_since_epoch : Duration , chan_descr : fmt:: Arguments , logger : & Log
1304
+ ) where Log :: Target : Logger {
1296
1305
let max_liquidity_msat = self . max_liquidity_msat ( ) . checked_sub ( amount_msat) . unwrap_or ( 0 ) ;
1297
1306
log_debug ! ( logger, "Subtracting {} from max liquidity of {} (setting it to {})" , amount_msat, chan_descr, max_liquidity_msat) ;
1298
- self . set_max_liquidity_msat ( max_liquidity_msat) ;
1299
- self . update_history_buckets ( amount_msat) ;
1307
+ self . set_max_liquidity_msat ( max_liquidity_msat, duration_since_epoch ) ;
1308
+ self . update_history_buckets ( amount_msat, duration_since_epoch ) ;
1300
1309
}
1301
1310
1302
1311
/// Updates the history buckets for this channel. Because the history buckets track what we now
1303
1312
/// know about the channel's state *prior to our payment* (i.e. what we assume is "steady
1304
1313
/// state"), we allow the caller to set an offset applied to our liquidity bounds which
1305
1314
/// represents the amount of the successful payment we just made.
1306
- fn update_history_buckets ( & mut self , bucket_offset_msat : u64 ) {
1315
+ fn update_history_buckets ( & mut self , bucket_offset_msat : u64 , duration_since_epoch : Duration ) {
1307
1316
let half_lives = self . now . duration_since ( * self . offset_history_last_updated ) . as_secs ( )
1308
1317
. checked_div ( self . decay_params . historical_no_updates_half_life . as_secs ( ) )
1309
1318
. map ( |v| v. try_into ( ) . unwrap_or ( u32:: max_value ( ) ) ) . unwrap_or ( u32:: max_value ( ) ) ;
@@ -1322,7 +1331,7 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
1322
1331
}
1323
1332
1324
1333
/// Adjusts the lower bound of the channel liquidity balance in this direction.
1325
- fn set_min_liquidity_msat ( & mut self , amount_msat : u64 ) {
1334
+ fn set_min_liquidity_msat ( & mut self , amount_msat : u64 , duration_since_epoch : Duration ) {
1326
1335
* self . min_liquidity_offset_msat = amount_msat;
1327
1336
* self . max_liquidity_offset_msat = if amount_msat > self . max_liquidity_msat ( ) {
1328
1337
0
@@ -1333,7 +1342,7 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
1333
1342
}
1334
1343
1335
1344
/// Adjusts the upper bound of the channel liquidity balance in this direction.
1336
- fn set_max_liquidity_msat ( & mut self , amount_msat : u64 ) {
1345
+ fn set_max_liquidity_msat ( & mut self , amount_msat : u64 , duration_since_epoch : Duration ) {
1337
1346
* self . max_liquidity_offset_msat = self . capacity_msat . checked_sub ( amount_msat) . unwrap_or ( 0 ) ;
1338
1347
* self . min_liquidity_offset_msat = if amount_msat < self . min_liquidity_msat ( ) {
1339
1348
0
@@ -1396,7 +1405,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreLookUp for Prob
1396
1405
}
1397
1406
1398
1407
impl < G : Deref < Target = NetworkGraph < L > > , L : Deref , T : Time > ScoreUpdate for ProbabilisticScorerUsingTime < G , L , T > where L :: Target : Logger {
1399
- fn payment_path_failed ( & mut self , path : & Path , short_channel_id : u64 , _duration_since_epoch : Duration ) {
1408
+ fn payment_path_failed ( & mut self , path : & Path , short_channel_id : u64 , duration_since_epoch : Duration ) {
1400
1409
let amount_msat = path. final_value_msat ( ) ;
1401
1410
log_trace ! ( self . logger, "Scoring path through to SCID {} as having failed at {} msat" , short_channel_id, amount_msat) ;
1402
1411
let network_graph = self . network_graph . read_only ( ) ;
@@ -1419,13 +1428,15 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
1419
1428
. entry ( hop. short_channel_id )
1420
1429
. or_insert_with ( ChannelLiquidity :: new)
1421
1430
. as_directed_mut ( source, & target, capacity_msat, self . decay_params )
1422
- . failed_at_channel ( amount_msat, format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1431
+ . failed_at_channel ( amount_msat, duration_since_epoch,
1432
+ format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1423
1433
} else {
1424
1434
self . channel_liquidities
1425
1435
. entry ( hop. short_channel_id )
1426
1436
. or_insert_with ( ChannelLiquidity :: new)
1427
1437
. as_directed_mut ( source, & target, capacity_msat, self . decay_params )
1428
- . failed_downstream ( amount_msat, format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1438
+ . failed_downstream ( amount_msat, duration_since_epoch,
1439
+ format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1429
1440
}
1430
1441
} else {
1431
1442
log_debug ! ( self . logger, "Not able to penalize channel with SCID {} as we do not have graph info for it (likely a route-hint last-hop)." ,
@@ -1435,7 +1446,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
1435
1446
}
1436
1447
}
1437
1448
1438
- fn payment_path_successful ( & mut self , path : & Path , _duration_since_epoch : Duration ) {
1449
+ fn payment_path_successful ( & mut self , path : & Path , duration_since_epoch : Duration ) {
1439
1450
let amount_msat = path. final_value_msat ( ) ;
1440
1451
log_trace ! ( self . logger, "Scoring path through SCID {} as having succeeded at {} msat." ,
1441
1452
path. hops. split_last( ) . map( |( hop, _) | hop. short_channel_id) . unwrap_or( 0 ) , amount_msat) ;
@@ -1453,7 +1464,8 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
1453
1464
. entry ( hop. short_channel_id )
1454
1465
. or_insert_with ( ChannelLiquidity :: new)
1455
1466
. as_directed_mut ( source, & target, capacity_msat, self . decay_params )
1456
- . successful ( amount_msat, format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1467
+ . successful ( amount_msat, duration_since_epoch,
1468
+ format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1457
1469
} else {
1458
1470
log_debug ! ( self . logger, "Not able to learn for channel with SCID {} as we do not have graph info for it (likely a route-hint last-hop)." ,
1459
1471
hop. short_channel_id) ;
@@ -1469,12 +1481,15 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
1469
1481
self . payment_path_failed ( path, u64:: max_value ( ) , duration_since_epoch)
1470
1482
}
1471
1483
1472
- fn time_passed ( & mut self , _duration_since_epoch : Duration ) {
1484
+ fn time_passed ( & mut self , duration_since_epoch : Duration ) {
1473
1485
let decay_params = self . decay_params ;
1474
1486
self . channel_liquidities . retain ( |_scid, liquidity| {
1475
- liquidity. min_liquidity_offset_msat = liquidity. decayed_offset ( liquidity. min_liquidity_offset_msat , decay_params) ;
1476
- liquidity. max_liquidity_offset_msat = liquidity. decayed_offset ( liquidity. max_liquidity_offset_msat , decay_params) ;
1487
+ liquidity. min_liquidity_offset_msat =
1488
+ liquidity. decayed_offset ( liquidity. min_liquidity_offset_msat , duration_since_epoch, decay_params) ;
1489
+ liquidity. max_liquidity_offset_msat =
1490
+ liquidity. decayed_offset ( liquidity. max_liquidity_offset_msat , duration_since_epoch, decay_params) ;
1477
1491
liquidity. last_updated = T :: now ( ) ;
1492
+
1478
1493
let elapsed_time =
1479
1494
T :: now ( ) . duration_since ( liquidity. offset_history_last_updated ) ;
1480
1495
if elapsed_time > decay_params. historical_no_updates_half_life {
@@ -2408,7 +2423,7 @@ mod tests {
2408
2423
2409
2424
scorer. channel_liquidities . get_mut ( & 42 ) . unwrap ( )
2410
2425
. as_directed_mut ( & source, & target, 1_000 , decay_params)
2411
- . set_min_liquidity_msat ( 200 ) ;
2426
+ . set_min_liquidity_msat ( 200 , Duration :: ZERO ) ;
2412
2427
2413
2428
let liquidity = scorer. channel_liquidities . get ( & 42 ) . unwrap ( )
2414
2429
. as_directed ( & source, & target, 1_000 , decay_params) ;
@@ -2434,7 +2449,7 @@ mod tests {
2434
2449
2435
2450
scorer. channel_liquidities . get_mut ( & 43 ) . unwrap ( )
2436
2451
. as_directed_mut ( & target, & recipient, 1_000 , decay_params)
2437
- . set_max_liquidity_msat ( 200 ) ;
2452
+ . set_max_liquidity_msat ( 200 , Duration :: ZERO ) ;
2438
2453
2439
2454
let liquidity = scorer. channel_liquidities . get ( & 43 ) . unwrap ( )
2440
2455
. as_directed ( & target, & recipient, 1_000 , decay_params) ;
@@ -2480,7 +2495,7 @@ mod tests {
2480
2495
// Reset from source to target.
2481
2496
scorer. channel_liquidities . get_mut ( & 42 ) . unwrap ( )
2482
2497
. as_directed_mut ( & source, & target, 1_000 , decay_params)
2483
- . set_min_liquidity_msat ( 900 ) ;
2498
+ . set_min_liquidity_msat ( 900 , Duration :: ZERO ) ;
2484
2499
2485
2500
let liquidity = scorer. channel_liquidities . get ( & 42 ) . unwrap ( )
2486
2501
. as_directed ( & source, & target, 1_000 , decay_params) ;
@@ -2495,7 +2510,7 @@ mod tests {
2495
2510
// Reset from target to source.
2496
2511
scorer. channel_liquidities . get_mut ( & 42 ) . unwrap ( )
2497
2512
. as_directed_mut ( & target, & source, 1_000 , decay_params)
2498
- . set_min_liquidity_msat ( 400 ) ;
2513
+ . set_min_liquidity_msat ( 400 , Duration :: ZERO ) ;
2499
2514
2500
2515
let liquidity = scorer. channel_liquidities . get ( & 42 ) . unwrap ( )
2501
2516
. as_directed ( & source, & target, 1_000 , decay_params) ;
@@ -2541,7 +2556,7 @@ mod tests {
2541
2556
// Reset from source to target.
2542
2557
scorer. channel_liquidities . get_mut ( & 42 ) . unwrap ( )
2543
2558
. as_directed_mut ( & source, & target, 1_000 , decay_params)
2544
- . set_max_liquidity_msat ( 300 ) ;
2559
+ . set_max_liquidity_msat ( 300 , Duration :: ZERO ) ;
2545
2560
2546
2561
let liquidity = scorer. channel_liquidities . get ( & 42 ) . unwrap ( )
2547
2562
. as_directed ( & source, & target, 1_000 , decay_params) ;
@@ -2556,7 +2571,7 @@ mod tests {
2556
2571
// Reset from target to source.
2557
2572
scorer. channel_liquidities . get_mut ( & 42 ) . unwrap ( )
2558
2573
. as_directed_mut ( & target, & source, 1_000 , decay_params)
2559
- . set_max_liquidity_msat ( 600 ) ;
2574
+ . set_max_liquidity_msat ( 600 , Duration :: ZERO ) ;
2560
2575
2561
2576
let liquidity = scorer. channel_liquidities . get ( & 42 ) . unwrap ( )
2562
2577
. as_directed ( & source, & target, 1_000 , decay_params) ;
0 commit comments