From 5fa8124aa1272742214321699e5841eeb377232e Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Fri, 29 Jun 2018 18:22:08 -0700 Subject: [PATCH 1/3] 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 From 10ecf2307f8bc8b7006995236d6a67745b4997d3 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Sat, 30 Jun 2018 11:42:36 -0700 Subject: [PATCH 2/3] htlcswitch/link_test: use realistic link timeouts --- htlcswitch/link_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index 197d1d45..c42c6e50 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -1497,7 +1497,7 @@ func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) ( // to not trigger commit updates automatically during tests. BatchSize: 10000, MinFeeUpdateTimeout: 30 * time.Minute, - MaxFeeUpdateTimeout: 30 * time.Minute, + MaxFeeUpdateTimeout: 40 * time.Minute, } const startingHeight = 100 @@ -3878,7 +3878,7 @@ func restartLink(aliceChannel *lnwallet.LightningChannel, aliceSwitch *Switch, // to not trigger commit updates automatically during tests. BatchSize: 10000, MinFeeUpdateTimeout: 30 * time.Minute, - MaxFeeUpdateTimeout: 30 * time.Minute, + MaxFeeUpdateTimeout: 40 * time.Minute, // Set any hodl flags requested for the new link. HodlMask: hodl.MaskFromFlags(hodlFlags...), DebugHTLC: len(hodlFlags) > 0, From 12f74f762d48e4dd5529609f26af5d3e68708aea Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Sat, 30 Jun 2018 11:43:07 -0700 Subject: [PATCH 3/3] htlcswitch/test_utils: use realistic link timeouts --- htlcswitch/test_utils.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/htlcswitch/test_utils.go b/htlcswitch/test_utils.go index 56af3f90..6cb2740c 100644 --- a/htlcswitch/test_utils.go +++ b/htlcswitch/test_utils.go @@ -878,9 +878,10 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, } const ( - batchTimeout = 50 * time.Millisecond - fwdPkgTimeout = 5 * time.Second - feeUpdateTimeout = 30 * time.Minute + batchTimeout = 50 * time.Millisecond + fwdPkgTimeout = 5 * time.Second + minFeeUpdateTimeout = 30 * time.Minute + maxFeeUpdateTimeout = 40 * time.Minute ) pCache := &mockPreimageCache{ @@ -919,8 +920,8 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, BatchSize: 10, BatchTicker: &mockTicker{time.NewTicker(batchTimeout).C}, FwdPkgGCTicker: &mockTicker{time.NewTicker(fwdPkgTimeout).C}, - MinFeeUpdateTimeout: feeUpdateTimeout, - MaxFeeUpdateTimeout: feeUpdateTimeout, + MinFeeUpdateTimeout: minFeeUpdateTimeout, + MaxFeeUpdateTimeout: maxFeeUpdateTimeout, OnChannelFailure: func(lnwire.ChannelID, lnwire.ShortChannelID, LinkFailureError) {}, }, aliceChannel, @@ -962,8 +963,8 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, BatchSize: 10, BatchTicker: &mockTicker{time.NewTicker(batchTimeout).C}, FwdPkgGCTicker: &mockTicker{time.NewTicker(fwdPkgTimeout).C}, - MinFeeUpdateTimeout: feeUpdateTimeout, - MaxFeeUpdateTimeout: feeUpdateTimeout, + MinFeeUpdateTimeout: minFeeUpdateTimeout, + MaxFeeUpdateTimeout: maxFeeUpdateTimeout, OnChannelFailure: func(lnwire.ChannelID, lnwire.ShortChannelID, LinkFailureError) {}, }, firstBobChannel, @@ -1005,8 +1006,8 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, BatchSize: 10, BatchTicker: &mockTicker{time.NewTicker(batchTimeout).C}, FwdPkgGCTicker: &mockTicker{time.NewTicker(fwdPkgTimeout).C}, - MinFeeUpdateTimeout: feeUpdateTimeout, - MaxFeeUpdateTimeout: feeUpdateTimeout, + MinFeeUpdateTimeout: minFeeUpdateTimeout, + MaxFeeUpdateTimeout: maxFeeUpdateTimeout, OnChannelFailure: func(lnwire.ChannelID, lnwire.ShortChannelID, LinkFailureError) {}, }, secondBobChannel, @@ -1048,8 +1049,8 @@ func newThreeHopNetwork(t testing.TB, aliceChannel, firstBobChannel, BatchSize: 10, BatchTicker: &mockTicker{time.NewTicker(batchTimeout).C}, FwdPkgGCTicker: &mockTicker{time.NewTicker(fwdPkgTimeout).C}, - MinFeeUpdateTimeout: feeUpdateTimeout, - MaxFeeUpdateTimeout: feeUpdateTimeout, + MinFeeUpdateTimeout: minFeeUpdateTimeout, + MaxFeeUpdateTimeout: maxFeeUpdateTimeout, OnChannelFailure: func(lnwire.ChannelID, lnwire.ShortChannelID, LinkFailureError) {}, }, carolChannel,