@@ -841,8 +841,8 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> whe
841
841
let amt = directed_info. effective_capacity ( ) . as_msat ( ) ;
842
842
let dir_liq = liq. as_directed ( source, target, amt) ;
843
843
844
- let min_buckets = & dir_liq. liquidity_history . min_liquidity_offset_history . buckets ;
845
- let max_buckets = & dir_liq. liquidity_history . max_liquidity_offset_history . buckets ;
844
+ let min_buckets = & dir_liq. liquidity_history . min_liquidity_offset_history_buckets ( ) ;
845
+ let max_buckets = & dir_liq. liquidity_history . max_liquidity_offset_history_buckets ( ) ;
846
846
847
847
log_debug ! ( self . logger, core:: concat!(
848
848
"Liquidity from {} to {} via {} is in the range ({}, {}).\n " ,
@@ -935,8 +935,8 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> whe
935
935
let amt = directed_info. effective_capacity ( ) . as_msat ( ) ;
936
936
let dir_liq = liq. as_directed ( source, target, amt) ;
937
937
938
- let min_buckets = dir_liq. liquidity_history . min_liquidity_offset_history . buckets ;
939
- let mut max_buckets = dir_liq. liquidity_history . max_liquidity_offset_history . buckets ;
938
+ let min_buckets = * dir_liq. liquidity_history . min_liquidity_offset_history_buckets ( ) ;
939
+ let mut max_buckets = * dir_liq. liquidity_history . max_liquidity_offset_history_buckets ( ) ;
940
940
941
941
// Note that the liquidity buckets are an offset from the edge, so we inverse
942
942
// the max order to get the probabilities from zero.
@@ -1281,11 +1281,10 @@ DirectedChannelLiquidity<L, BRT, T> {
1281
1281
/// state"), we allow the caller to set an offset applied to our liquidity bounds which
1282
1282
/// represents the amount of the successful payment we just made.
1283
1283
fn update_history_buckets ( & mut self , bucket_offset_msat : u64 , duration_since_epoch : Duration ) {
1284
- self . liquidity_history . min_liquidity_offset_history . track_datapoint (
1285
- * self . min_liquidity_offset_msat + bucket_offset_msat, self . capacity_msat
1286
- ) ;
1287
- self . liquidity_history . max_liquidity_offset_history . track_datapoint (
1288
- self . max_liquidity_offset_msat . saturating_sub ( bucket_offset_msat) , self . capacity_msat
1284
+ self . liquidity_history . track_datapoint (
1285
+ * self . min_liquidity_offset_msat + bucket_offset_msat,
1286
+ self . max_liquidity_offset_msat . saturating_sub ( bucket_offset_msat) ,
1287
+ self . capacity_msat ,
1289
1288
) ;
1290
1289
* self . offset_history_last_updated = duration_since_epoch;
1291
1290
}
@@ -1451,19 +1450,12 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ScoreUpdate for Probabilistic
1451
1450
if elapsed_time > decay_params. historical_no_updates_half_life {
1452
1451
let half_life = decay_params. historical_no_updates_half_life . as_secs_f64 ( ) ;
1453
1452
if half_life != 0.0 {
1454
- let divisor = powf64 ( 2048.0 , elapsed_time. as_secs_f64 ( ) / half_life) as u64 ;
1455
- for bucket in liquidity. liquidity_history . min_liquidity_offset_history . buckets . iter_mut ( ) {
1456
- * bucket = ( ( * bucket as u64 ) * 1024 / divisor) as u16 ;
1457
- }
1458
- for bucket in liquidity. liquidity_history . max_liquidity_offset_history . buckets . iter_mut ( ) {
1459
- * bucket = ( ( * bucket as u64 ) * 1024 / divisor) as u16 ;
1460
- }
1453
+ liquidity. liquidity_history . decay_buckets ( elapsed_time. as_secs_f64 ( ) / half_life) ;
1461
1454
liquidity. offset_history_last_updated = duration_since_epoch;
1462
1455
}
1463
1456
}
1464
1457
liquidity. min_liquidity_offset_msat != 0 || liquidity. max_liquidity_offset_msat != 0 ||
1465
- liquidity. liquidity_history . min_liquidity_offset_history . buckets != [ 0 ; 32 ] ||
1466
- liquidity. liquidity_history . max_liquidity_offset_history . buckets != [ 0 ; 32 ]
1458
+ liquidity. liquidity_history . has_datapoints ( )
1467
1459
} ) ;
1468
1460
}
1469
1461
}
@@ -1593,7 +1585,7 @@ mod bucketed_history {
1593
1585
/// in each of 32 buckets.
1594
1586
#[ derive( Clone , Copy ) ]
1595
1587
pub ( super ) struct HistoricalBucketRangeTracker {
1596
- pub ( super ) buckets : [ u16 ; 32 ] ,
1588
+ buckets : [ u16 ; 32 ] ,
1597
1589
}
1598
1590
1599
1591
/// Buckets are stored in fixed point numbers with a 5 bit fractional part. Thus, the value
@@ -1602,7 +1594,7 @@ mod bucketed_history {
1602
1594
1603
1595
impl HistoricalBucketRangeTracker {
1604
1596
pub ( super ) fn new ( ) -> Self { Self { buckets : [ 0 ; 32 ] } }
1605
- pub ( super ) fn track_datapoint ( & mut self , liquidity_offset_msat : u64 , capacity_msat : u64 ) {
1597
+ fn track_datapoint ( & mut self , liquidity_offset_msat : u64 , capacity_msat : u64 ) {
1606
1598
// We have 32 leaky buckets for min and max liquidity. Each bucket tracks the amount of time
1607
1599
// we spend in each bucket as a 16-bit fixed-point number with a 5 bit fractional part.
1608
1600
//
@@ -1638,11 +1630,10 @@ mod bucketed_history {
1638
1630
impl_writeable_tlv_based ! ( HistoricalBucketRangeTracker , { ( 0 , buckets, required) } ) ;
1639
1631
impl_writeable_tlv_based ! ( LegacyHistoricalBucketRangeTracker , { ( 0 , buckets, required) } ) ;
1640
1632
1641
-
1642
1633
#[ derive( Clone , Copy ) ]
1643
1634
pub ( super ) struct HistoricalLiquidityTracker {
1644
- pub ( super ) min_liquidity_offset_history : HistoricalBucketRangeTracker ,
1645
- pub ( super ) max_liquidity_offset_history : HistoricalBucketRangeTracker ,
1635
+ min_liquidity_offset_history : HistoricalBucketRangeTracker ,
1636
+ max_liquidity_offset_history : HistoricalBucketRangeTracker ,
1646
1637
}
1647
1638
1648
1639
impl HistoricalLiquidityTracker {
@@ -1663,6 +1654,29 @@ mod bucketed_history {
1663
1654
}
1664
1655
}
1665
1656
1657
+ pub ( super ) fn has_datapoints ( & self ) -> bool {
1658
+ self . min_liquidity_offset_history . buckets != [ 0 ; 32 ] ||
1659
+ self . max_liquidity_offset_history . buckets != [ 0 ; 32 ]
1660
+ }
1661
+
1662
+ pub ( super ) fn decay_buckets ( & mut self , half_lives : f64 ) {
1663
+ let divisor = powf64 ( 2048.0 , half_lives) as u64 ;
1664
+ for bucket in self . min_liquidity_offset_history . buckets . iter_mut ( ) {
1665
+ * bucket = ( ( * bucket as u64 ) * 1024 / divisor) as u16 ;
1666
+ }
1667
+ for bucket in self . max_liquidity_offset_history . buckets . iter_mut ( ) {
1668
+ * bucket = ( ( * bucket as u64 ) * 1024 / divisor) as u16 ;
1669
+ }
1670
+ }
1671
+
1672
+ pub ( super ) fn writeable_min_offset_history ( & self ) -> & HistoricalBucketRangeTracker {
1673
+ & self . min_liquidity_offset_history
1674
+ }
1675
+
1676
+ pub ( super ) fn writeable_max_offset_history ( & self ) -> & HistoricalBucketRangeTracker {
1677
+ & self . max_liquidity_offset_history
1678
+ }
1679
+
1666
1680
pub ( super ) fn as_directed < ' a > ( & ' a self , source_less_than_target : bool )
1667
1681
-> HistoricalMinMaxBuckets < & ' a HistoricalBucketRangeTracker > {
1668
1682
let ( min_liquidity_offset_history, max_liquidity_offset_history) =
@@ -1691,13 +1705,30 @@ mod bucketed_history {
1691
1705
pub ( super ) struct HistoricalMinMaxBuckets < D : Deref < Target = HistoricalBucketRangeTracker > > {
1692
1706
/// Buckets tracking where and how often we've seen the minimum liquidity bound for a
1693
1707
/// channel.
1694
- pub ( super ) min_liquidity_offset_history : D ,
1708
+ min_liquidity_offset_history : D ,
1695
1709
/// Buckets tracking where and how often we've seen the maximum liquidity bound for a
1696
1710
/// channel.
1697
- pub ( super ) max_liquidity_offset_history : D ,
1711
+ max_liquidity_offset_history : D ,
1712
+ }
1713
+
1714
+ impl < D : DerefMut < Target = HistoricalBucketRangeTracker > > HistoricalMinMaxBuckets < D > {
1715
+ pub ( super ) fn track_datapoint (
1716
+ & mut self , min_offset_msat : u64 , max_offset_msat : u64 , capacity_msat : u64 ,
1717
+ ) {
1718
+ self . min_liquidity_offset_history . track_datapoint ( min_offset_msat, capacity_msat) ;
1719
+ self . max_liquidity_offset_history . track_datapoint ( max_offset_msat, capacity_msat) ;
1720
+ }
1698
1721
}
1699
1722
1700
1723
impl < D : Deref < Target = HistoricalBucketRangeTracker > > HistoricalMinMaxBuckets < D > {
1724
+ pub ( super ) fn min_liquidity_offset_history_buckets ( & self ) -> & [ u16 ; 32 ] {
1725
+ & self . min_liquidity_offset_history . buckets
1726
+ }
1727
+
1728
+ pub ( super ) fn max_liquidity_offset_history_buckets ( & self ) -> & [ u16 ; 32 ] {
1729
+ & self . max_liquidity_offset_history . buckets
1730
+ }
1731
+
1701
1732
#[ inline]
1702
1733
pub ( super ) fn calculate_success_probability_times_billion (
1703
1734
& self , params : & ProbabilisticScoringFeeParameters , amount_msat : u64 ,
@@ -1824,8 +1855,8 @@ impl Writeable for ChannelLiquidity {
1824
1855
( 2 , self . max_liquidity_offset_msat, required) ,
1825
1856
// 3 was the max_liquidity_offset_history in octile form
1826
1857
( 4 , self . last_updated, required) ,
1827
- ( 5 , self . liquidity_history. min_liquidity_offset_history , required) ,
1828
- ( 7 , self . liquidity_history. max_liquidity_offset_history , required) ,
1858
+ ( 5 , self . liquidity_history. writeable_min_offset_history ( ) , required) ,
1859
+ ( 7 , self . liquidity_history. writeable_max_offset_history ( ) , required) ,
1829
1860
( 9 , self . offset_history_last_updated, required) ,
1830
1861
} ) ;
1831
1862
Ok ( ( ) )
0 commit comments