From 5fa8124aa1272742214321699e5841eeb377232e Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Fri, 29 Jun 2018 18:22:08 -0700 Subject: [PATCH] htlcswitch/link: correct bias in fee update backoff This commit corrects the distribution used to schedule a link's randomized backoff for fee updates. Currently, our algorithm biases the lowest value in the range, with probability equal to lower/upper, or the ratio of the lower bound to the upper. This distribution is skewed more heavily as lower approaches upper. The solution is to sample a random value in the range upper-lower, then add this to our lower bound. The effect is a uniformly distributed timeout in [lower, upper). --- htlcswitch/link.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index b60b43f1..a0e9f3f9 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -995,12 +995,7 @@ out: func (l *channelLink) randomFeeUpdateTimeout() time.Duration { lower := int64(l.cfg.MinFeeUpdateTimeout) upper := int64(l.cfg.MaxFeeUpdateTimeout) - rand := prand.Int63n(upper) - if rand < lower { - rand = lower - } - - return time.Duration(rand) + return time.Duration(prand.Int63n(upper-lower) + lower) } // handleDownStreamPkt processes an HTLC packet sent from the downstream HTLC