htlcswitch: don't return negative value from Bandwidth()

This commits prevents the Bandwith() method from returning
a negative value if the channel reserve is larger than
the actual available balance.
This commit is contained in:
Johan T. Halseth 2017-11-29 13:57:41 +01:00
parent 50f495fae1
commit e6f7a46d90
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
1 changed files with 12 additions and 3 deletions

View File

@ -1261,12 +1261,21 @@ type getBandwidthCmd struct {
//
// NOTE: Part of the ChannelLink interface.
func (l *channelLink) Bandwidth() lnwire.MilliSatoshi {
// TODO(roasbeef): subtract reserve
channelBandwidth := l.channel.AvailableBalance()
overflowBandwidth := l.overflowQueue.TotalHtlcAmount()
reserve := lnwire.NewMSatFromSatoshis(l.channel.GetReserve())
linkBandwidth := channelBandwidth - overflowBandwidth
reserve := lnwire.NewMSatFromSatoshis(l.channel.LocalChanReserve())
return channelBandwidth - overflowBandwidth - reserve
// If the channel reserve is greater than the total available
// balance of the link, just return 0.
if linkBandwidth < reserve {
return 0
}
// Else the amount that is available to flow through the link at
// this point is the available balance minus the reserve amount
// we are required to keep as collateral.
return linkBandwidth - reserve
}
// policyUpdate is a message sent to a channel link when an outside sub-system