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.
This commit is contained in:
Olaoluwa Osuntokun 2017-11-23 22:21:46 -06:00
parent 5a1a3c7277
commit c560200ba1
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21
2 changed files with 119 additions and 0 deletions

View File

@ -286,6 +286,27 @@ func (l *channelLink) Stop() {
l.cfg.BlockEpochs.Cancel() 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 // htlcManager is the primary goroutine which drives a channel's commitment
// update state-machine in response to messages received via several channels. // update state-machine in response to messages received via several channels.
// This goroutine reads messages from the upstream (remote) peer, and also from // This goroutine reads messages from the upstream (remote) peer, and also from

View File

@ -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)
}
}
}