@@ -474,7 +474,84 @@ where L::Target: Logger {
474
474
decay_params : ProbabilisticScoringDecayParameters ,
475
475
network_graph : G ,
476
476
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
+ fn get ( & self , short_channel_id : & u64 ) -> Option < & ChannelLiquidity > {
511
+ self . 0 . get ( short_channel_id)
512
+ }
513
+
514
+ fn get_mut ( & mut self , short_channel_id : & u64 ) -> Option < & mut ChannelLiquidity > {
515
+ self . 0 . get_mut ( short_channel_id)
516
+ }
517
+
518
+ fn insert ( & mut self , short_channel_id : u64 , liquidity : ChannelLiquidity ) -> Option < ChannelLiquidity > {
519
+ self . 0 . insert ( short_channel_id, liquidity)
520
+ }
521
+
522
+ fn iter ( & self ) -> impl Iterator < Item = ( & u64 , & ChannelLiquidity ) > {
523
+ self . 0 . iter ( )
524
+ }
525
+
526
+ fn entry ( & mut self , short_channel_id : u64 ) -> Entry < u64 , ChannelLiquidity , RandomState > {
527
+ self . 0 . entry ( short_channel_id)
528
+ }
529
+
530
+ fn serialized_length ( & self ) -> usize {
531
+ self . 0 . serialized_length ( )
532
+ }
533
+ }
534
+
535
+
536
+ impl Readable for ChannelLiquidities {
537
+ #[ inline]
538
+ fn read < R : Read > ( r : & mut R ) -> Result < Self , DecodeError > {
539
+ let mut channel_liquidities = new_hash_map ( ) ;
540
+ read_tlv_fields ! ( r, {
541
+ ( 0 , channel_liquidities, required) ,
542
+ } ) ;
543
+ Ok ( ChannelLiquidities ( channel_liquidities) )
544
+ }
545
+ }
546
+
547
+ impl Writeable for ChannelLiquidities {
548
+ #[ inline]
549
+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
550
+ write_tlv_fields ! ( w, {
551
+ ( 0 , self . 0 , required) ,
552
+ } ) ;
553
+ Ok ( ( ) )
554
+ }
478
555
}
479
556
480
557
/// Parameters for configuring [`ProbabilisticScorer`].
@@ -804,7 +881,7 @@ impl ProbabilisticScoringDecayParameters {
804
881
/// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
805
882
/// offset fields gives the opposite direction.
806
883
#[ repr( C ) ] // Force the fields in memory to be in the order we specify
807
- struct ChannelLiquidity {
884
+ pub struct ChannelLiquidity {
808
885
/// Lower channel liquidity bound in terms of an offset from zero.
809
886
min_liquidity_offset_msat : u64 ,
810
887
@@ -849,7 +926,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> whe
849
926
decay_params,
850
927
network_graph,
851
928
logger,
852
- channel_liquidities : new_hash_map ( ) ,
929
+ channel_liquidities : ChannelLiquidities :: new ( ) ,
853
930
}
854
931
}
855
932
@@ -1603,26 +1680,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ScoreUpdate for Probabilistic
1603
1680
}
1604
1681
1605
1682
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
- } ) ;
1683
+ self . channel_liquidities . time_passed ( duration_since_epoch, self . decay_params ) ;
1626
1684
}
1627
1685
}
1628
1686
@@ -2061,14 +2119,12 @@ mod bucketed_history {
2061
2119
}
2062
2120
}
2063
2121
use bucketed_history:: { LegacyHistoricalBucketRangeTracker , HistoricalBucketRangeTracker , DirectedHistoricalLiquidityTracker , HistoricalLiquidityTracker } ;
2122
+ use hashbrown:: hash_map:: Entry ;
2064
2123
2065
2124
impl < G : Deref < Target = NetworkGraph < L > > , L : Deref > Writeable for ProbabilisticScorer < G , L > where L :: Target : Logger {
2066
2125
#[ inline]
2067
2126
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
2068
- write_tlv_fields ! ( w, {
2069
- ( 0 , self . channel_liquidities, required) ,
2070
- } ) ;
2071
- Ok ( ( ) )
2127
+ self . channel_liquidities . write ( w)
2072
2128
}
2073
2129
}
2074
2130
@@ -2079,10 +2135,7 @@ ReadableArgs<(ProbabilisticScoringDecayParameters, G, L)> for ProbabilisticScore
2079
2135
r : & mut R , args : ( ProbabilisticScoringDecayParameters , G , L )
2080
2136
) -> Result < Self , DecodeError > {
2081
2137
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
- } ) ;
2138
+ let channel_liquidities = ChannelLiquidities :: read ( r) ?;
2086
2139
Ok ( Self {
2087
2140
decay_params,
2088
2141
network_graph,
0 commit comments