Fix scorer panic when available capacity is zero
authorJeffrey Czyz <jkczyz@gmail.com>
Thu, 5 Jan 2023 17:50:24 +0000 (11:50 -0600)
committerJeffrey Czyz <jkczyz@gmail.com>
Fri, 3 Mar 2023 15:41:42 +0000 (09:41 -0600)
commit10cfe5c973e61251f6bf2180d1fc8d57d5e56ca5
tree951c6405af1b3601a0875aced74d00b0da8bd154
parent8311581fe110a9ee561a6fda6b55c78a02068d43
Fix scorer panic when available capacity is zero

ProbabilisticScorer takes a ChannelUsage when computing a penalty for a
channel. The formula for calculating the liquidity penalty reduces the
maximum capacity by the amount of in-flight HTLCs (available capacity)
and adds one to prevent division by zero.

However, since the available capacity is passed to
DirectedChannelLiquidity as the capacity, other penalty formulas may use
the available (i.e., reduced) capacity inadvertently. In practice, this
has two ramifications for the historical liquidity penalty computation:

1. The bucket formula doesn't have a consistent denominator for a given
   channel.
2. The bucket formula may divide by zero when the in-flight HTLC amount
   equals or exceeds the effective capacity.

Fixing this involves only using the available capacity when appropriate.
lightning/src/routing/scoring.rs