@@ -1793,7 +1793,32 @@ mod bucketed_history {
17931793			} 
17941794
17951795			let  mut  cumulative_success_prob_times_billion = 0 ; 
1796- 			for  ( min_idx,  min_bucket)  in  self . min_liquidity_offset_history . buckets . iter ( ) . enumerate ( )  { 
1796+ 			// Special-case the 0th min bucket - it generally means we failed a payment, so only 
1797+ 			// consider the highest (i.e. largest-offset-from-max-capacity) max bucket for all 
1798+ 			// points against the 0th min bucket! 
1799+ 			if  self . min_liquidity_offset_history . buckets [ 0 ]  != 0  { 
1800+ 				let  mut  highest_max_bucket_with_points = 0 ; 
1801+ 				let  mut  total_max_points = 0 ; 
1802+ 				for  ( max_idx,  max_bucket)  in  self . max_liquidity_offset_history . buckets . iter ( ) . enumerate ( )  { 
1803+ 					if  * max_bucket >= 32  { 
1804+ 						highest_max_bucket_with_points = cmp:: max ( highest_max_bucket_with_points,  max_idx) ; 
1805+ 					} 
1806+ 					total_max_points += * max_bucket as  u64 ; 
1807+ 				} 
1808+ 				let  max_bucket_end_pos = BUCKET_START_POS [ 32  - highest_max_bucket_with_points]  - 1 ; 
1809+ 				if  payment_pos < max_bucket_end_pos { 
1810+ 					let  bucket_prob_times_billion =
1811+ 						( self . min_liquidity_offset_history . buckets [ 0 ]  as  u64 )  *  total_max_points
1812+ 							*  1024  *  1024  *  1024  / total_valid_points_tracked; 
1813+ 					cumulative_success_prob_times_billion += bucket_prob_times_billion * 
1814+ 						( ( max_bucket_end_pos - payment_pos)  as  u64 )  /
1815+ 						// Add an additional one in the divisor as the payment bucket has been 
1816+ 						// rounded down. 
1817+ 						( max_bucket_end_pos + 1 )  as  u64 ; 
1818+ 				} 
1819+ 			} 
1820+ 
1821+ 			for  ( min_idx,  min_bucket)  in  self . min_liquidity_offset_history . buckets . iter ( ) . enumerate ( ) . skip ( 1 )  { 
17971822				let  min_bucket_start_pos = BUCKET_START_POS [ min_idx] ; 
17981823				for  ( max_idx,  max_bucket)  in  self . max_liquidity_offset_history . buckets . iter ( ) . enumerate ( ) . take ( 32  - min_idx)  { 
17991824					let  max_bucket_end_pos = BUCKET_START_POS [ 32  - max_idx]  - 1 ; 
@@ -3060,7 +3085,7 @@ mod tests {
30603085		// Even after we tell the scorer we definitely have enough available liquidity, it will 
30613086		// still remember that there was some failure in the past, and assign a non-0 penalty. 
30623087		scorer. payment_path_failed ( & payment_path_for_amount ( 1000 ) ,  43 ) ; 
3063- 		assert_eq ! ( scorer. channel_penalty_msat( 42 ,  & source,  & target,  usage,  & params) ,  198 ) ; 
3088+ 		assert_eq ! ( scorer. channel_penalty_msat( 42 ,  & source,  & target,  usage,  & params) ,  32 ) ; 
30643089		// The first points should be decayed just slightly and the last bucket has a new point. 
30653090		assert_eq ! ( scorer. historical_estimated_channel_liquidity_probabilities( 42 ,  & target) , 
30663091			Some ( ( [ 31 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  32 ,  0 ,  0 ,  0 ,  0 ,  0 ] , 
@@ -3070,12 +3095,12 @@ mod tests {
30703095		// simply check bounds here. 
30713096		let  five_hundred_prob =
30723097			scorer. historical_estimated_payment_success_probability ( 42 ,  & target,  500 ) . unwrap ( ) ; 
3073- 		assert ! ( five_hundred_prob > 0.5  ) ; 
3074- 		assert ! ( five_hundred_prob < 0.52  ) ; 
3098+ 		assert ! ( five_hundred_prob > 0.66  ) ; 
3099+ 		assert ! ( five_hundred_prob < 0.68  ) ; 
30753100		let  one_prob =
30763101			scorer. historical_estimated_payment_success_probability ( 42 ,  & target,  1 ) . unwrap ( ) ; 
3077- 		assert ! ( one_prob < 0.95 ) ; 
3078- 		assert ! ( one_prob > 0.90  ) ; 
3102+ 		assert ! ( one_prob < 1.0 ) ; 
3103+ 		assert ! ( one_prob > 0.95  ) ; 
30793104
30803105		// Advance the time forward 16 half-lives (which the docs claim will ensure all data is 
30813106		// gone), and check that we're back to where we started. 
@@ -3095,7 +3120,7 @@ mod tests {
30953120		scorer. payment_path_failed ( & payment_path_for_amount ( 1 ) ,  42 ) ; 
30963121		assert_eq ! ( scorer. channel_penalty_msat( 42 ,  & source,  & target,  usage,  & params) ,  2048 ) ; 
30973122		usage. inflight_htlc_msat  = 0 ; 
3098- 		assert_eq ! ( scorer. channel_penalty_msat( 42 ,  & source,  & target,  usage,  & params) ,  409 ) ; 
3123+ 		assert_eq ! ( scorer. channel_penalty_msat( 42 ,  & source,  & target,  usage,  & params) ,  866 ) ; 
30993124
31003125		let  usage = ChannelUsage  { 
31013126			amount_msat :  1 , 
@@ -3281,9 +3306,7 @@ mod tests {
32813306		assert_eq ! ( scorer. historical_estimated_channel_liquidity_probabilities( 42 ,  & target) , 
32823307			Some ( ( [ 63 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ] , 
32833308				[ 32 ,  31 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ] ) ) ) ; 
3284- 		assert ! ( scorer. historical_estimated_payment_success_probability( 42 ,  & target,  amount_msat) 
3285- 			. unwrap( )  > 0.24 ) ; 
3286- 		assert ! ( scorer. historical_estimated_payment_success_probability( 42 ,  & target,  amount_msat) 
3287- 			. unwrap( )  < 0.25 ) ; 
3309+ 		assert_eq ! ( scorer. historical_estimated_payment_success_probability( 42 ,  & target,  amount_msat) , 
3310+ 			Some ( 0.0 ) ) ; 
32883311	} 
32893312} 
0 commit comments