multi: thread bandwidth check amount down to MayAddOutgoingHtlc

Pass htlc amount down to the channel so that we don't need to rely
on minHtlc (and pad it when the channel sets a 0 min htlc). Update
test to just check some sane values since we're no longer relying
on minHtlc amount at all.
This commit is contained in:
carla committed 2021-10-19 09:50:51 +02:00
1 parent 45de686d35
commit 990dda4b18
13 files changed
+81 -39

No files matched your search

+20 -10
View File
@@ -5038,19 +5038,29 @@ func (lc *LightningChannel) GetDustSum(remote bool) lnwire.MilliSatoshi {
}
// MayAddOutgoingHtlc validates whether we can add an outgoing htlc to this
// channel. We don't have a value or circuit for this htlc, because we just
// want to test that we have slots for a potential htlc so we use a "mock"
// htlc to validate a potential commitment state with one more outgoing htlc.
func (lc *LightningChannel) MayAddOutgoingHtlc() error {
// channel. We don't have a circuit for this htlc, because we just want to test
// that we have slots for a potential htlc so we use a "mock" htlc to validate
// a potential commitment state with one more outgoing htlc. If a zero htlc
// amount is provided, we'll attempt to add the smallest possible htlc to the
// channel (either the minimum htlc, or 1 sat).
func (lc *LightningChannel) MayAddOutgoingHtlc(amt lnwire.MilliSatoshi) error {
lc.Lock()
defer lc.Unlock()
// As this is a mock HTLC, we'll attempt to add the smallest possible
// HTLC permitted in the channel. However certain implementations may
// set this value to zero, so we'll catch that and increment things so
// we always use a non-zero value.
mockHtlcAmt := lc.channelState.LocalChanCfg.MinHTLC
if mockHtlcAmt == 0 {
var mockHtlcAmt lnwire.MilliSatoshi
switch {
// If the caller specifically set an amount, we use it.
case amt != 0:
mockHtlcAmt = amt
// In absence of a specific amount, we want to use minimum htlc value
// for the channel. However certain implementations may set this value
// to zero, so we only use this value if it is non-zero.
case lc.channelState.LocalChanCfg.MinHTLC != 0:
mockHtlcAmt = lc.channelState.LocalChanCfg.MinHTLC
// As a last resort, we just add a non-zero amount.
default:
mockHtlcAmt++
}
+26 -7
View File
@@ -9841,10 +9841,9 @@ func TestChannelSignedAckRegression(t *testing.T) {
require.NoError(t, err)
}
// TestMayAddOutgoingHtlcZeroValue tests that if the minHTLC value of the
// channel is zero, then the MayAddOutgoingHtlc doesn't exit early due to
// running into a zero valued HTLC.
func TestMayAddOutgoingHtlcZeroValue(t *testing.T) {
// TestMayAddOutgoingHtlc tests MayAddOutgoingHtlc against zero and non-zero
// htlc amounts.
func TestMayAddOutgoingHtlc(t *testing.T) {
t.Parallel()
// The default channel created as a part of the test fixture already
@@ -9857,9 +9856,29 @@ func TestMayAddOutgoingHtlcZeroValue(t *testing.T) {
defer cleanUp()
// The channels start out with a 50/50 balance, so both sides should be
// able to add an outgoing HTLC.
require.NoError(t, aliceChannel.MayAddOutgoingHtlc())
require.NoError(t, bobChannel.MayAddOutgoingHtlc())
// able to add an outgoing HTLC with no specific amount added.
require.NoError(t, aliceChannel.MayAddOutgoingHtlc(0))
require.NoError(t, bobChannel.MayAddOutgoingHtlc(0))
chanBal, err := btcutil.NewAmount(testChannelCapacity)
require.NoError(t, err)
// Each side should be able to add 1/4 of total channel balance since
// we're 50/50 split.
mayAdd := lnwire.MilliSatoshi(chanBal / 4 * 1000)
require.NoError(t, aliceChannel.MayAddOutgoingHtlc(mayAdd))
require.NoError(t, bobChannel.MayAddOutgoingHtlc(mayAdd))
// Both channels should fail if we try to add an amount more than
// their current balance.
mayNotAdd := lnwire.MilliSatoshi(chanBal * 1000)
require.Error(t, aliceChannel.MayAddOutgoingHtlc(mayNotAdd))
require.Error(t, bobChannel.MayAddOutgoingHtlc(mayNotAdd))
// Hard set alice's min htlc to zero and test the case where we just
// fall back to a non-zero value.
aliceChannel.channelState.LocalChanCfg.MinHTLC = 0
require.NoError(t, aliceChannel.MayAddOutgoingHtlc(0))
}
// TestIsChannelClean tests that IsChannelClean returns the expected values
+3 -1
View File
@@ -96,6 +96,8 @@ var (
aliceDustLimit = btcutil.Amount(200)
bobDustLimit = btcutil.Amount(1300)
testChannelCapacity float64 = 10
)
// CreateTestChannels creates to fully populated channels to be used within
@@ -109,7 +111,7 @@ var (
func CreateTestChannels(chanType channeldb.ChannelType) (
*LightningChannel, *LightningChannel, func(), error) {
channelCapacity, err := btcutil.NewAmount(10)
channelCapacity, err := btcutil.NewAmount(testChannelCapacity)
if err != nil {
return nil, nil, nil, err
}