@@ -474,7 +474,63 @@ where L::Target: Logger {
474474 decay_params : ProbabilisticScoringDecayParameters ,
475475 network_graph : G ,
476476 logger : L ,
477- channel_liquidities : HashMap < u64 , ChannelLiquidity > ,
477+ channel_liquidities : ChannelLiquidities ,
478+ }
479+ /// ChannelLiquidities contains live and historical liquidity bounds for each channel.
480+ pub struct ChannelLiquidities ( HashMap < u64 , ChannelLiquidity > ) ;
481+
482+ impl ChannelLiquidities {
483+ fn new ( ) -> Self {
484+ Self ( new_hash_map ( ) )
485+ }
486+
487+ fn time_passed ( & mut self , duration_since_epoch : Duration , decay_params : ProbabilisticScoringDecayParameters ) {
488+ self . 0 . retain ( |_scid, liquidity| {
489+ liquidity. min_liquidity_offset_msat =
490+ liquidity. decayed_offset ( liquidity. min_liquidity_offset_msat , duration_since_epoch, decay_params) ;
491+ liquidity. max_liquidity_offset_msat =
492+ liquidity. decayed_offset ( liquidity. max_liquidity_offset_msat , duration_since_epoch, decay_params) ;
493+ liquidity. last_updated = duration_since_epoch;
494+
495+ // TODO: Call decay multiple times.
496+ let elapsed_time =
497+ duration_since_epoch. saturating_sub ( liquidity. offset_history_last_updated ) ;
498+ if elapsed_time > decay_params. historical_no_updates_half_life {
499+ let half_life = decay_params. historical_no_updates_half_life . as_secs_f64 ( ) ;
500+ if half_life != 0.0 {
501+ liquidity. liquidity_history . decay_buckets ( elapsed_time. as_secs_f64 ( ) / half_life) ;
502+ liquidity. offset_history_last_updated = duration_since_epoch;
503+ }
504+ }
505+ liquidity. min_liquidity_offset_msat != 0 || liquidity. max_liquidity_offset_msat != 0 ||
506+ liquidity. liquidity_history . has_datapoints ( )
507+ } ) ;
508+ }
509+ }
510+
511+ impl Deref for ChannelLiquidities {
512+ type Target = HashMap < u64 , ChannelLiquidity > ;
513+
514+ fn deref ( & self ) -> & Self :: Target {
515+ & self . 0
516+ }
517+ }
518+
519+ impl DerefMut for ChannelLiquidities {
520+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
521+ & mut self . 0
522+ }
523+ }
524+
525+ impl Readable for ChannelLiquidities {
526+ #[ inline]
527+ fn read < R : Read > ( r : & mut R ) -> Result < Self , DecodeError > {
528+ let mut channel_liquidities = new_hash_map ( ) ;
529+ read_tlv_fields ! ( r, {
530+ ( 0 , channel_liquidities, required) ,
531+ } ) ;
532+ Ok ( ChannelLiquidities ( channel_liquidities) )
533+ }
478534}
479535
480536/// Parameters for configuring [`ProbabilisticScorer`].
@@ -804,7 +860,7 @@ impl ProbabilisticScoringDecayParameters {
804860/// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
805861/// offset fields gives the opposite direction.
806862#[ repr( C ) ] // Force the fields in memory to be in the order we specify
807- struct ChannelLiquidity {
863+ pub struct ChannelLiquidity {
808864 /// Lower channel liquidity bound in terms of an offset from zero.
809865 min_liquidity_offset_msat : u64 ,
810866
@@ -849,7 +905,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> whe
849905 decay_params,
850906 network_graph,
851907 logger,
852- channel_liquidities : new_hash_map ( ) ,
908+ channel_liquidities : ChannelLiquidities :: new ( ) ,
853909 }
854910 }
855911
@@ -1603,26 +1659,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ScoreUpdate for Probabilistic
16031659 }
16041660
16051661 fn time_passed ( & mut self , duration_since_epoch : Duration ) {
1606- let decay_params = self . decay_params ;
1607- self . channel_liquidities . retain ( |_scid, liquidity| {
1608- liquidity. min_liquidity_offset_msat =
1609- liquidity. decayed_offset ( liquidity. min_liquidity_offset_msat , duration_since_epoch, decay_params) ;
1610- liquidity. max_liquidity_offset_msat =
1611- liquidity. decayed_offset ( liquidity. max_liquidity_offset_msat , duration_since_epoch, decay_params) ;
1612- liquidity. last_updated = duration_since_epoch;
1613-
1614- let elapsed_time =
1615- duration_since_epoch. saturating_sub ( liquidity. offset_history_last_updated ) ;
1616- if elapsed_time > decay_params. historical_no_updates_half_life {
1617- let half_life = decay_params. historical_no_updates_half_life . as_secs_f64 ( ) ;
1618- if half_life != 0.0 {
1619- liquidity. liquidity_history . decay_buckets ( elapsed_time. as_secs_f64 ( ) / half_life) ;
1620- liquidity. offset_history_last_updated = duration_since_epoch;
1621- }
1622- }
1623- liquidity. min_liquidity_offset_msat != 0 || liquidity. max_liquidity_offset_msat != 0 ||
1624- liquidity. liquidity_history . has_datapoints ( )
1625- } ) ;
1662+ self . channel_liquidities . time_passed ( duration_since_epoch, self . decay_params ) ;
16261663 }
16271664}
16281665
@@ -2079,10 +2116,7 @@ ReadableArgs<(ProbabilisticScoringDecayParameters, G, L)> for ProbabilisticScore
20792116 r : & mut R , args : ( ProbabilisticScoringDecayParameters , G , L )
20802117 ) -> Result < Self , DecodeError > {
20812118 let ( decay_params, network_graph, logger) = args;
2082- let mut channel_liquidities = new_hash_map ( ) ;
2083- read_tlv_fields ! ( r, {
2084- ( 0 , channel_liquidities, required) ,
2085- } ) ;
2119+ let channel_liquidities = ChannelLiquidities :: read ( r) ?;
20862120 Ok ( Self {
20872121 decay_params,
20882122 network_graph,
0 commit comments