@@ -1065,7 +1065,9 @@ impl<T: Time> ChannelLiquidity<T> {
1065
1065
}
1066
1066
}
1067
1067
1068
- fn decayed_offset ( & self , offset : u64 , decay_params : ProbabilisticScoringDecayParameters ) -> u64 {
1068
+ fn decayed_offset ( & self , offset : u64 , duration_since_epoch : Duration ,
1069
+ decay_params : ProbabilisticScoringDecayParameters
1070
+ ) -> u64 {
1069
1071
let half_life = decay_params. liquidity_offset_half_life . as_secs ( ) ;
1070
1072
if half_life != 0 {
1071
1073
let elapsed_time = T :: now ( ) . duration_since ( self . last_updated ) . as_secs ( ) as f64 ;
@@ -1267,44 +1269,50 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
1267
1269
1268
1270
impl < L : DerefMut < Target = u64 > , BRT : DerefMut < Target = HistoricalBucketRangeTracker > , T : Time , U : DerefMut < Target = T > > DirectedChannelLiquidity < L , BRT , T , U > {
1269
1271
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
1270
- 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 {
1271
1275
let existing_max_msat = self . max_liquidity_msat ( ) ;
1272
1276
if amount_msat < existing_max_msat {
1273
1277
log_debug ! ( logger, "Setting max liquidity of {} from {} to {}" , chan_descr, existing_max_msat, amount_msat) ;
1274
- self . set_max_liquidity_msat ( amount_msat) ;
1278
+ self . set_max_liquidity_msat ( amount_msat, duration_since_epoch ) ;
1275
1279
} else {
1276
1280
log_trace ! ( logger, "Max liquidity of {} is {} (already less than or equal to {})" ,
1277
1281
chan_descr, existing_max_msat, amount_msat) ;
1278
1282
}
1279
- self . update_history_buckets ( 0 ) ;
1283
+ self . update_history_buckets ( 0 , duration_since_epoch ) ;
1280
1284
}
1281
1285
1282
1286
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
1283
- 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 {
1284
1290
let existing_min_msat = self . min_liquidity_msat ( ) ;
1285
1291
if amount_msat > existing_min_msat {
1286
1292
log_debug ! ( logger, "Setting min liquidity of {} from {} to {}" , existing_min_msat, chan_descr, amount_msat) ;
1287
- self . set_min_liquidity_msat ( amount_msat) ;
1293
+ self . set_min_liquidity_msat ( amount_msat, duration_since_epoch ) ;
1288
1294
} else {
1289
1295
log_trace ! ( logger, "Min liquidity of {} is {} (already greater than or equal to {})" ,
1290
1296
chan_descr, existing_min_msat, amount_msat) ;
1291
1297
}
1292
- self . update_history_buckets ( 0 ) ;
1298
+ self . update_history_buckets ( 0 , duration_since_epoch ) ;
1293
1299
}
1294
1300
1295
1301
/// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
1296
- 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 {
1297
1305
let max_liquidity_msat = self . max_liquidity_msat ( ) . checked_sub ( amount_msat) . unwrap_or ( 0 ) ;
1298
1306
log_debug ! ( logger, "Subtracting {} from max liquidity of {} (setting it to {})" , amount_msat, chan_descr, max_liquidity_msat) ;
1299
- self . set_max_liquidity_msat ( max_liquidity_msat) ;
1300
- 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 ) ;
1301
1309
}
1302
1310
1303
1311
/// Updates the history buckets for this channel. Because the history buckets track what we now
1304
1312
/// know about the channel's state *prior to our payment* (i.e. what we assume is "steady
1305
1313
/// state"), we allow the caller to set an offset applied to our liquidity bounds which
1306
1314
/// represents the amount of the successful payment we just made.
1307
- 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 ) {
1308
1316
let half_lives = self . now . duration_since ( * self . offset_history_last_updated ) . as_secs ( )
1309
1317
. checked_div ( self . decay_params . historical_no_updates_half_life . as_secs ( ) )
1310
1318
. map ( |v| v. try_into ( ) . unwrap_or ( u32:: max_value ( ) ) ) . unwrap_or ( u32:: max_value ( ) ) ;
@@ -1322,7 +1330,7 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
1322
1330
}
1323
1331
1324
1332
/// Adjusts the lower bound of the channel liquidity balance in this direction.
1325
- fn set_min_liquidity_msat ( & mut self , amount_msat : u64 ) {
1333
+ fn set_min_liquidity_msat ( & mut self , amount_msat : u64 , duration_since_epoch : Duration ) {
1326
1334
* self . min_liquidity_offset_msat = amount_msat;
1327
1335
* self . max_liquidity_offset_msat = if amount_msat > self . max_liquidity_msat ( ) {
1328
1336
0
@@ -1334,7 +1342,7 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
1334
1342
}
1335
1343
1336
1344
/// Adjusts the upper bound of the channel liquidity balance in this direction.
1337
- 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 ) {
1338
1346
* self . max_liquidity_offset_msat = self . capacity_msat . checked_sub ( amount_msat) . unwrap_or ( 0 ) ;
1339
1347
* self . min_liquidity_offset_msat = if amount_msat < self . min_liquidity_msat ( ) {
1340
1348
0
@@ -1391,7 +1399,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreLookUp for Prob
1391
1399
}
1392
1400
1393
1401
impl < G : Deref < Target = NetworkGraph < L > > , L : Deref , T : Time > ScoreUpdate for ProbabilisticScorerUsingTime < G , L , T > where L :: Target : Logger {
1394
- fn payment_path_failed ( & mut self , path : & Path , short_channel_id : u64 , _duration_since_epoch : Duration ) {
1402
+ fn payment_path_failed ( & mut self , path : & Path , short_channel_id : u64 , duration_since_epoch : Duration ) {
1395
1403
let amount_msat = path. final_value_msat ( ) ;
1396
1404
log_trace ! ( self . logger, "Scoring path through to SCID {} as having failed at {} msat" , short_channel_id, amount_msat) ;
1397
1405
let network_graph = self . network_graph . read_only ( ) ;
@@ -1414,13 +1422,15 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
1414
1422
. entry ( hop. short_channel_id )
1415
1423
. or_insert_with ( ChannelLiquidity :: new)
1416
1424
. as_directed_mut ( source, & target, capacity_msat, self . decay_params )
1417
- . failed_at_channel ( amount_msat, format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1425
+ . failed_at_channel ( amount_msat, duration_since_epoch,
1426
+ format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1418
1427
} else {
1419
1428
self . channel_liquidities
1420
1429
. entry ( hop. short_channel_id )
1421
1430
. or_insert_with ( ChannelLiquidity :: new)
1422
1431
. as_directed_mut ( source, & target, capacity_msat, self . decay_params )
1423
- . failed_downstream ( amount_msat, format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1432
+ . failed_downstream ( amount_msat, duration_since_epoch,
1433
+ format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1424
1434
}
1425
1435
} else {
1426
1436
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)." ,
@@ -1430,7 +1440,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
1430
1440
}
1431
1441
}
1432
1442
1433
- fn payment_path_successful ( & mut self , path : & Path , _duration_since_epoch : Duration ) {
1443
+ fn payment_path_successful ( & mut self , path : & Path , duration_since_epoch : Duration ) {
1434
1444
let amount_msat = path. final_value_msat ( ) ;
1435
1445
log_trace ! ( self . logger, "Scoring path through SCID {} as having succeeded at {} msat." ,
1436
1446
path. hops. split_last( ) . map( |( hop, _) | hop. short_channel_id) . unwrap_or( 0 ) , amount_msat) ;
@@ -1448,7 +1458,8 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
1448
1458
. entry ( hop. short_channel_id )
1449
1459
. or_insert_with ( ChannelLiquidity :: new)
1450
1460
. as_directed_mut ( source, & target, capacity_msat, self . decay_params )
1451
- . successful ( amount_msat, format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1461
+ . successful ( amount_msat, duration_since_epoch,
1462
+ format_args ! ( "SCID {}, towards {:?}" , hop. short_channel_id, target) , & self . logger ) ;
1452
1463
} else {
1453
1464
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)." ,
1454
1465
hop. short_channel_id) ;
@@ -1464,12 +1475,15 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
1464
1475
self . payment_path_failed ( path, u64:: max_value ( ) , duration_since_epoch)
1465
1476
}
1466
1477
1467
- fn decay_liquidity_certainty ( & mut self , _duration_since_epoch : Duration ) {
1478
+ fn decay_liquidity_certainty ( & mut self , duration_since_epoch : Duration ) {
1468
1479
let decay_params = self . decay_params ;
1469
1480
self . channel_liquidities . retain ( |_scid, liquidity| {
1470
- liquidity. min_liquidity_offset_msat = liquidity. decayed_offset ( liquidity. min_liquidity_offset_msat , decay_params) ;
1471
- liquidity. max_liquidity_offset_msat = liquidity. decayed_offset ( liquidity. max_liquidity_offset_msat , decay_params) ;
1481
+ liquidity. min_liquidity_offset_msat =
1482
+ liquidity. decayed_offset ( liquidity. min_liquidity_offset_msat , duration_since_epoch, decay_params) ;
1483
+ liquidity. max_liquidity_offset_msat =
1484
+ liquidity. decayed_offset ( liquidity. max_liquidity_offset_msat , duration_since_epoch, decay_params) ;
1472
1485
liquidity. last_updated = T :: now ( ) ;
1486
+
1473
1487
let elapsed_time =
1474
1488
T :: now ( ) . duration_since ( liquidity. offset_history_last_updated ) ;
1475
1489
if elapsed_time > decay_params. historical_no_updates_half_life {
@@ -2395,7 +2409,7 @@ mod tests {
2395
2409
2396
2410
scorer. channel_liquidities . get_mut ( & 42 ) . unwrap ( )
2397
2411
. as_directed_mut ( & source, & target, 1_000 , decay_params)
2398
- . set_min_liquidity_msat ( 200 ) ;
2412
+ . set_min_liquidity_msat ( 200 , Duration :: ZERO ) ;
2399
2413
2400
2414
let liquidity = scorer. channel_liquidities . get ( & 42 ) . unwrap ( )
2401
2415
. as_directed ( & source, & target, 1_000 , decay_params) ;
@@ -2421,7 +2435,7 @@ mod tests {
2421
2435
2422
2436
scorer. channel_liquidities . get_mut ( & 43 ) . unwrap ( )
2423
2437
. as_directed_mut ( & target, & recipient, 1_000 , decay_params)
2424
- . set_max_liquidity_msat ( 200 ) ;
2438
+ . set_max_liquidity_msat ( 200 , Duration :: ZERO ) ;
2425
2439
2426
2440
let liquidity = scorer. channel_liquidities . get ( & 43 ) . unwrap ( )
2427
2441
. as_directed ( & target, & recipient, 1_000 , decay_params) ;
@@ -2467,7 +2481,7 @@ mod tests {
2467
2481
// Reset from source to target.
2468
2482
scorer. channel_liquidities . get_mut ( & 42 ) . unwrap ( )
2469
2483
. as_directed_mut ( & source, & target, 1_000 , decay_params)
2470
- . set_min_liquidity_msat ( 900 ) ;
2484
+ . set_min_liquidity_msat ( 900 , Duration :: ZERO ) ;
2471
2485
2472
2486
let liquidity = scorer. channel_liquidities . get ( & 42 ) . unwrap ( )
2473
2487
. as_directed ( & source, & target, 1_000 , decay_params) ;
@@ -2482,7 +2496,7 @@ mod tests {
2482
2496
// Reset from target to source.
2483
2497
scorer. channel_liquidities . get_mut ( & 42 ) . unwrap ( )
2484
2498
. as_directed_mut ( & target, & source, 1_000 , decay_params)
2485
- . set_min_liquidity_msat ( 400 ) ;
2499
+ . set_min_liquidity_msat ( 400 , Duration :: ZERO ) ;
2486
2500
2487
2501
let liquidity = scorer. channel_liquidities . get ( & 42 ) . unwrap ( )
2488
2502
. as_directed ( & source, & target, 1_000 , decay_params) ;
@@ -2528,7 +2542,7 @@ mod tests {
2528
2542
// Reset from source to target.
2529
2543
scorer. channel_liquidities . get_mut ( & 42 ) . unwrap ( )
2530
2544
. as_directed_mut ( & source, & target, 1_000 , decay_params)
2531
- . set_max_liquidity_msat ( 300 ) ;
2545
+ . set_max_liquidity_msat ( 300 , Duration :: ZERO ) ;
2532
2546
2533
2547
let liquidity = scorer. channel_liquidities . get ( & 42 ) . unwrap ( )
2534
2548
. as_directed ( & source, & target, 1_000 , decay_params) ;
@@ -2543,7 +2557,7 @@ mod tests {
2543
2557
// Reset from target to source.
2544
2558
scorer. channel_liquidities . get_mut ( & 42 ) . unwrap ( )
2545
2559
. as_directed_mut ( & target, & source, 1_000 , decay_params)
2546
- . set_max_liquidity_msat ( 600 ) ;
2560
+ . set_max_liquidity_msat ( 600 , Duration :: ZERO ) ;
2547
2561
2548
2562
let liquidity = scorer. channel_liquidities . get ( & 42 ) . unwrap ( )
2549
2563
. as_directed ( & source, & target, 1_000 , decay_params) ;
0 commit comments