From c560200ba1ab2bed8e4c297fd68efe2f371cee66 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 23 Nov 2017 22:21:46 -0600 Subject: [PATCH] htlcswitch: add new shouldAdjustCommitFee helper function In this commit, we add a new helper function to the link which will be utilized in a later commit. This helper function will help us determine if we should update the commitment fee, in response to a change in the network fee return by our fee estimators. --- htlcswitch/link.go | 21 +++++++++ htlcswitch/link_test.go | 98 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 64d9e934..5a07926b 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -286,6 +286,27 @@ func (l *channelLink) Stop() { l.cfg.BlockEpochs.Cancel() } +// shouldAdjustCommitFee returns true if we should update our commitment fee to +// match that of the network fee. We'll only update our commitment fee if the +// network fee is +/- 10% to our network fee. +func shouldAdjustCommitFee(netFee, chanFee btcutil.Amount) bool { + switch { + // If the network fee is greater than the commitment fee, then we'll + // switch to it if it's at least 10% greater than the commit fee. + case netFee > chanFee && netFee >= (chanFee+(chanFee*10)/100): + return true + + // If the network fee is less than our commitment fee, then we'll + // switch to it if it's at least 10% less than the commitment fee. + case netFee < chanFee && netFee <= (chanFee-(chanFee*10)/100): + return true + + // Otherwise, we won't modify our fee. + default: + return false + } +} + // htlcManager is the primary goroutine which drives a channel's commitment // update state-machine in response to messages received via several channels. // This goroutine reads messages from the upstream (remote) peer, and also from diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index 6d9aa121..be161fa3 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -2021,3 +2021,101 @@ func TestChannelRetransmission(t *testing.T) { } } + +// TestShouldAdjustCommitFee tests the shouldAdjustCommitFee pivot function to +// ensure that ie behaves properly. We should only update the fee if it +// deviates from our current fee by more 10% or more. +func TestShouldAdjustCommitFee(t *testing.T) { + tests := []struct { + netFee btcutil.Amount + chanFee btcutil.Amount + shouldAdjust bool + }{ + + // The network fee is 3x lower than the current commitment + // transaction. As a result, we should adjust our fee to match + // it. + { + netFee: 100, + chanFee: 3000, + shouldAdjust: true, + }, + + // The network fee is lower than the current commitment fee, + // but only slightly so, so we won't update the commitment fee. + { + netFee: 2999, + chanFee: 3000, + shouldAdjust: false, + }, + + // The network fee is lower than the commitment fee, but only + // right before it crosses our current threshold. + { + netFee: 1000, + chanFee: 1099, + shouldAdjust: false, + }, + + // The network fee is lower than the commitment fee, and within + // our range of adjustment, so we should adjust. + { + netFee: 1000, + chanFee: 1100, + shouldAdjust: true, + }, + + // The network fee is 2x higher than our commitment fee, so we + // should adjust upwards. + { + netFee: 2000, + chanFee: 1000, + shouldAdjust: true, + }, + + // The network fee is higher than our commitment fee, but only + // slightly so, so we won't update. + { + netFee: 1001, + chanFee: 1000, + shouldAdjust: false, + }, + + // The network fee is higher than our commitment fee, but + // hasn't yet crossed our activation threshold. + { + netFee: 1100, + chanFee: 1099, + shouldAdjust: false, + }, + + // The network fee is higher than our commitment fee, and + // within our activation threshold, so we should update our + // fee. + { + netFee: 1100, + chanFee: 1000, + shouldAdjust: true, + }, + + // Our fees match exactly, so we shouldn't update it at all. + { + netFee: 1000, + chanFee: 1000, + shouldAdjust: false, + }, + } + + for i, test := range tests { + adjustedFee := shouldAdjustCommitFee( + test.netFee, test.chanFee, + ) + + if adjustedFee && !test.shouldAdjust { + t.Fatalf("test #%v failed: net_fee=%v, "+ + "chan_fee=%v, adjust_expect=%v, adjust_returned=%v", + i, test.netFee, test.chanFee, test.shouldAdjust, + adjustedFee) + } + } +}