@@ -801,10 +801,10 @@ struct ChannelLiquidity {
801801}
802802
803803/// A snapshot of [`ChannelLiquidity`] in one direction assuming a certain channel capacity.
804- struct DirectedChannelLiquidity < L : Deref < Target = u64 > , BRT : Deref < Target = HistoricalBucketRangeTracker > , T : Deref < Target = Duration > > {
804+ struct DirectedChannelLiquidity < L : Deref < Target = u64 > , HT : Deref < Target = HistoricalLiquidityTracker > , T : Deref < Target = Duration > > {
805805 min_liquidity_offset_msat : L ,
806806 max_liquidity_offset_msat : L ,
807- liquidity_history : HistoricalMinMaxBuckets < BRT > ,
807+ liquidity_history : DirectedHistoricalLiquidityTracker < HT > ,
808808 capacity_msat : u64 ,
809809 last_updated : T ,
810810 offset_history_last_updated : T ,
@@ -991,7 +991,7 @@ impl ChannelLiquidity {
991991 /// `capacity_msat`.
992992 fn as_directed (
993993 & self , source : & NodeId , target : & NodeId , capacity_msat : u64 ,
994- ) -> DirectedChannelLiquidity < & u64 , & HistoricalBucketRangeTracker , & Duration > {
994+ ) -> DirectedChannelLiquidity < & u64 , & HistoricalLiquidityTracker , & Duration > {
995995 let source_less_than_target = source < target;
996996 let ( min_liquidity_offset_msat, max_liquidity_offset_msat) =
997997 if source_less_than_target {
@@ -1014,7 +1014,7 @@ impl ChannelLiquidity {
10141014 /// `capacity_msat`.
10151015 fn as_directed_mut (
10161016 & mut self , source : & NodeId , target : & NodeId , capacity_msat : u64 ,
1017- ) -> DirectedChannelLiquidity < & mut u64 , & mut HistoricalBucketRangeTracker , & mut Duration > {
1017+ ) -> DirectedChannelLiquidity < & mut u64 , & mut HistoricalLiquidityTracker , & mut Duration > {
10181018 let source_less_than_target = source < target;
10191019 let ( min_liquidity_offset_msat, max_liquidity_offset_msat) =
10201020 if source_less_than_target {
@@ -1128,8 +1128,8 @@ fn success_probability(
11281128 ( numerator, denominator)
11291129}
11301130
1131- impl < L : Deref < Target = u64 > , BRT : Deref < Target = HistoricalBucketRangeTracker > , T : Deref < Target = Duration > >
1132- DirectedChannelLiquidity < L , BRT , T > {
1131+ impl < L : Deref < Target = u64 > , HT : Deref < Target = HistoricalLiquidityTracker > , T : Deref < Target = Duration > >
1132+ DirectedChannelLiquidity < L , HT , T > {
11331133 /// Returns a liquidity penalty for routing the given HTLC `amount_msat` through the channel in
11341134 /// this direction.
11351135 fn penalty_msat ( & self , amount_msat : u64 , score_params : & ProbabilisticScoringFeeParameters ) -> u64 {
@@ -1234,8 +1234,8 @@ DirectedChannelLiquidity< L, BRT, T> {
12341234 }
12351235}
12361236
1237- impl < L : DerefMut < Target = u64 > , BRT : DerefMut < Target = HistoricalBucketRangeTracker > , T : DerefMut < Target = Duration > >
1238- DirectedChannelLiquidity < L , BRT , T > {
1237+ impl < L : DerefMut < Target = u64 > , HT : DerefMut < Target = HistoricalLiquidityTracker > , T : DerefMut < Target = Duration > >
1238+ DirectedChannelLiquidity < L , HT , T > {
12391239 /// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
12401240 fn failed_at_channel < Log : Deref > (
12411241 & mut self , amount_msat : u64 , duration_since_epoch : Duration , chan_descr : fmt:: Arguments , logger : & Log
@@ -1678,55 +1678,52 @@ mod bucketed_history {
16781678 }
16791679
16801680 pub ( super ) fn as_directed < ' a > ( & ' a self , source_less_than_target : bool )
1681- -> HistoricalMinMaxBuckets < & ' a HistoricalBucketRangeTracker > {
1682- let ( min_liquidity_offset_history, max_liquidity_offset_history) =
1683- if source_less_than_target {
1684- ( & self . min_liquidity_offset_history , & self . max_liquidity_offset_history )
1685- } else {
1686- ( & self . max_liquidity_offset_history , & self . min_liquidity_offset_history )
1687- } ;
1688- HistoricalMinMaxBuckets { min_liquidity_offset_history, max_liquidity_offset_history }
1681+ -> DirectedHistoricalLiquidityTracker < & ' a HistoricalLiquidityTracker > {
1682+ DirectedHistoricalLiquidityTracker { source_less_than_target, tracker : self }
16891683 }
16901684
16911685 pub ( super ) fn as_directed_mut < ' a > ( & ' a mut self , source_less_than_target : bool )
1692- -> HistoricalMinMaxBuckets < & ' a mut HistoricalBucketRangeTracker > {
1693- let ( min_liquidity_offset_history, max_liquidity_offset_history) =
1694- if source_less_than_target {
1695- ( & mut self . min_liquidity_offset_history , & mut self . max_liquidity_offset_history )
1696- } else {
1697- ( & mut self . max_liquidity_offset_history , & mut self . min_liquidity_offset_history )
1698- } ;
1699- HistoricalMinMaxBuckets { min_liquidity_offset_history, max_liquidity_offset_history }
1686+ -> DirectedHistoricalLiquidityTracker < & ' a mut HistoricalLiquidityTracker > {
1687+ DirectedHistoricalLiquidityTracker { source_less_than_target, tracker : self }
17001688 }
17011689 }
17021690
17031691 /// A set of buckets representing the history of where we've seen the minimum- and maximum-
17041692 /// liquidity bounds for a given channel.
1705- pub ( super ) struct HistoricalMinMaxBuckets < D : Deref < Target = HistoricalBucketRangeTracker > > {
1706- /// Buckets tracking where and how often we've seen the minimum liquidity bound for a
1707- /// channel.
1708- min_liquidity_offset_history : D ,
1709- /// Buckets tracking where and how often we've seen the maximum liquidity bound for a
1710- /// channel.
1711- max_liquidity_offset_history : D ,
1693+ pub ( super ) struct DirectedHistoricalLiquidityTracker < D : Deref < Target = HistoricalLiquidityTracker > > {
1694+ source_less_than_target : bool ,
1695+ tracker : D ,
17121696 }
17131697
1714- impl < D : DerefMut < Target = HistoricalBucketRangeTracker > > HistoricalMinMaxBuckets < D > {
1698+ impl < D : DerefMut < Target = HistoricalLiquidityTracker > > DirectedHistoricalLiquidityTracker < D > {
17151699 pub ( super ) fn track_datapoint (
17161700 & mut self , min_offset_msat : u64 , max_offset_msat : u64 , capacity_msat : u64 ,
17171701 ) {
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) ;
1702+ if self . source_less_than_target {
1703+ self . tracker . min_liquidity_offset_history . track_datapoint ( min_offset_msat, capacity_msat) ;
1704+ self . tracker . max_liquidity_offset_history . track_datapoint ( max_offset_msat, capacity_msat) ;
1705+ } else {
1706+ self . tracker . max_liquidity_offset_history . track_datapoint ( min_offset_msat, capacity_msat) ;
1707+ self . tracker . min_liquidity_offset_history . track_datapoint ( max_offset_msat, capacity_msat) ;
1708+ }
17201709 }
17211710 }
17221711
1723- impl < D : Deref < Target = HistoricalBucketRangeTracker > > HistoricalMinMaxBuckets < D > {
1712+ impl < D : Deref < Target = HistoricalLiquidityTracker > > DirectedHistoricalLiquidityTracker < D > {
17241713 pub ( super ) fn min_liquidity_offset_history_buckets ( & self ) -> & [ u16 ; 32 ] {
1725- & self . min_liquidity_offset_history . buckets
1714+ if self . source_less_than_target {
1715+ & self . tracker . min_liquidity_offset_history . buckets
1716+ } else {
1717+ & self . tracker . max_liquidity_offset_history . buckets
1718+ }
17261719 }
17271720
17281721 pub ( super ) fn max_liquidity_offset_history_buckets ( & self ) -> & [ u16 ; 32 ] {
1729- & self . max_liquidity_offset_history . buckets
1722+ if self . source_less_than_target {
1723+ & self . tracker . max_liquidity_offset_history . buckets
1724+ } else {
1725+ & self . tracker . min_liquidity_offset_history . buckets
1726+ }
17301727 }
17311728
17321729 #[ inline]
@@ -1744,9 +1741,14 @@ mod bucketed_history {
17441741 let payment_pos = amount_to_pos ( amount_msat, capacity_msat) ;
17451742 if payment_pos >= POSITION_TICKS { return None ; }
17461743
1744+ let min_liquidity_offset_history_buckets =
1745+ self . min_liquidity_offset_history_buckets ( ) ;
1746+ let max_liquidity_offset_history_buckets =
1747+ self . max_liquidity_offset_history_buckets ( ) ;
1748+
17471749 let mut total_valid_points_tracked = 0 ;
1748- for ( min_idx, min_bucket) in self . min_liquidity_offset_history . buckets . iter ( ) . enumerate ( ) {
1749- for max_bucket in self . max_liquidity_offset_history . buckets . iter ( ) . take ( 32 - min_idx) {
1750+ for ( min_idx, min_bucket) in min_liquidity_offset_history_buckets . iter ( ) . enumerate ( ) {
1751+ for max_bucket in max_liquidity_offset_history_buckets . iter ( ) . take ( 32 - min_idx) {
17501752 total_valid_points_tracked += ( * min_bucket as u64 ) * ( * max_bucket as u64 ) ;
17511753 }
17521754 }
@@ -1766,10 +1768,10 @@ mod bucketed_history {
17661768 // datapoint, many of which may have relatively high maximum-available-liquidity
17671769 // values, which will result in us thinking we have some nontrivial probability of
17681770 // routing up to that amount.
1769- if self . min_liquidity_offset_history . buckets [ 0 ] != 0 {
1771+ if min_liquidity_offset_history_buckets [ 0 ] != 0 {
17701772 let mut highest_max_bucket_with_points = 0 ; // The highest max-bucket with any data
17711773 let mut total_max_points = 0 ; // Total points in max-buckets to consider
1772- for ( max_idx, max_bucket) in self . max_liquidity_offset_history . buckets . iter ( ) . enumerate ( ) {
1774+ for ( max_idx, max_bucket) in max_liquidity_offset_history_buckets . iter ( ) . enumerate ( ) {
17731775 if * max_bucket >= BUCKET_FIXED_POINT_ONE {
17741776 highest_max_bucket_with_points = cmp:: max ( highest_max_bucket_with_points, max_idx) ;
17751777 }
@@ -1780,16 +1782,16 @@ mod bucketed_history {
17801782 let ( numerator, denominator) = success_probability ( payment_pos as u64 , 0 ,
17811783 max_bucket_end_pos as u64 , POSITION_TICKS as u64 - 1 , params, true ) ;
17821784 let bucket_prob_times_billion =
1783- ( self . min_liquidity_offset_history . buckets [ 0 ] as u64 ) * total_max_points
1785+ ( min_liquidity_offset_history_buckets [ 0 ] as u64 ) * total_max_points
17841786 * 1024 * 1024 * 1024 / total_valid_points_tracked;
17851787 cumulative_success_prob_times_billion += bucket_prob_times_billion *
17861788 numerator / denominator;
17871789 }
17881790 }
17891791
1790- for ( min_idx, min_bucket) in self . min_liquidity_offset_history . buckets . iter ( ) . enumerate ( ) . skip ( 1 ) {
1792+ for ( min_idx, min_bucket) in min_liquidity_offset_history_buckets . iter ( ) . enumerate ( ) . skip ( 1 ) {
17911793 let min_bucket_start_pos = BUCKET_START_POS [ min_idx] ;
1792- for ( max_idx, max_bucket) in self . max_liquidity_offset_history . buckets . iter ( ) . enumerate ( ) . take ( 32 - min_idx) {
1794+ for ( max_idx, max_bucket) in max_liquidity_offset_history_buckets . iter ( ) . enumerate ( ) . take ( 32 - min_idx) {
17931795 let max_bucket_end_pos = BUCKET_START_POS [ 32 - max_idx] - 1 ;
17941796 // Note that this multiply can only barely not overflow - two 16 bit ints plus
17951797 // 30 bits is 62 bits.
@@ -1814,7 +1816,7 @@ mod bucketed_history {
18141816 }
18151817 }
18161818}
1817- use bucketed_history:: { LegacyHistoricalBucketRangeTracker , HistoricalBucketRangeTracker , HistoricalMinMaxBuckets , HistoricalLiquidityTracker } ;
1819+ use bucketed_history:: { LegacyHistoricalBucketRangeTracker , HistoricalBucketRangeTracker , DirectedHistoricalLiquidityTracker , HistoricalLiquidityTracker } ;
18181820
18191821impl < G : Deref < Target = NetworkGraph < L > > , L : Deref > Writeable for ProbabilisticScorer < G , L > where L :: Target : Logger {
18201822 #[ inline]
0 commit comments