@@ -650,6 +650,20 @@ struct HistoricalBucketRangeTracker {
650
650
651
651
impl HistoricalBucketRangeTracker {
652
652
fn new ( ) -> Self { Self { buckets : [ 0 ; 8 ] } }
653
+
654
+ /// Returns the average of the buckets between the two trackers.
655
+ pub ( crate ) fn combine ( mut self , other : Self ) -> Self {
656
+ let mut buckets: [ u16 ; 8 ] = [ 0 ; 8 ] ;
657
+ for i in 0 ..8 {
658
+ let current = self . buckets [ i] ;
659
+ let other = other. buckets [ i] ;
660
+ buckets[ i] = ( current + other) / 2 ;
661
+ }
662
+
663
+ self . buckets = buckets;
664
+ self
665
+ }
666
+
653
667
fn track_datapoint ( & mut self , liquidity_offset_msat : u64 , capacity_msat : u64 ) {
654
668
// We have 8 leaky buckets for min and max liquidity. Each bucket tracks the amount of time
655
669
// we spend in each bucket as a 16-bit fixed-point number with a 5 bit fractional part.
@@ -787,6 +801,7 @@ impl HistoricalMinMaxBuckets<'_> {
787
801
/// Direction is defined in terms of [`NodeId`] partial ordering, where the source node is the
788
802
/// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
789
803
/// offset fields gives the opposite direction.
804
+ #[ derive( Clone ) ]
790
805
struct ChannelLiquidity < T : Time > {
791
806
/// Lower channel liquidity bound in terms of an offset from zero.
792
807
min_liquidity_offset_msat : u64 ,
@@ -801,6 +816,21 @@ struct ChannelLiquidity<T: Time> {
801
816
max_liquidity_offset_history : HistoricalBucketRangeTracker ,
802
817
}
803
818
819
+ impl < T : Time > ChannelLiquidity < T > {
820
+ pub ( crate ) fn combine ( mut self , other : Self ) -> Self {
821
+ self . min_liquidity_offset_msat = self . min_liquidity_offset_msat . max ( other. min_liquidity_offset_msat ) ;
822
+ self . max_liquidity_offset_msat = self . max_liquidity_offset_msat . min ( other. max_liquidity_offset_msat ) ;
823
+ self . min_liquidity_offset_history . combine ( other. min_liquidity_offset_history ) ;
824
+ self . max_liquidity_offset_history . combine ( other. max_liquidity_offset_history ) ;
825
+
826
+ if self . last_updated . duration_since ( other. last_updated ) < Duration :: from_secs ( 0 ) {
827
+ self . last_updated = other. last_updated ;
828
+ }
829
+
830
+ self
831
+ }
832
+ }
833
+
804
834
/// A snapshot of [`ChannelLiquidity`] in one direction assuming a certain channel capacity and
805
835
/// decayed with a given half life.
806
836
struct DirectedChannelLiquidity < L : Deref < Target = u64 > , BRT : Deref < Target = HistoricalBucketRangeTracker > , T : Time , U : Deref < Target = T > > {
@@ -942,6 +972,27 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ProbabilisticScorerU
942
972
}
943
973
None
944
974
}
975
+
976
+ /// Combines two `ProbabilisticScorerUsingTime` into one.
977
+ ///
978
+ /// This merges the channel liquidity information of both scorers.
979
+ pub fn combine ( mut self , other : Self ) -> Self {
980
+ let mut channel_liquidities = self . channel_liquidities ;
981
+
982
+ for ( id, item) in other. channel_liquidities {
983
+ match channel_liquidities. get ( & id) {
984
+ None => { channel_liquidities. insert ( id, item) ; } ,
985
+ Some ( current) => {
986
+ let liquidity = current. clone ( ) ;
987
+ channel_liquidities. insert ( id, liquidity. combine ( item) ) ;
988
+ }
989
+ }
990
+ }
991
+
992
+ self . channel_liquidities = channel_liquidities;
993
+
994
+ self
995
+ }
945
996
}
946
997
947
998
impl < T : Time > ChannelLiquidity < T > {
0 commit comments