lnwallet: send ReservationError for unacceptable constraints

This commit is contained in:
Johan T. Halseth 2018-02-27 18:32:54 +01:00
parent 2cd7ec0833
commit bb63ad7da6
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -1,7 +1,6 @@
package lnwallet package lnwallet
import ( import (
"fmt"
"net" "net"
"sync" "sync"
@ -186,9 +185,11 @@ func NewChannelReservation(capacity, fundingAmt btcutil.Amount,
// //
// TODO(roasbeef): reject if 30% goes to fees? dust channel // TODO(roasbeef): reject if 30% goes to fees? dust channel
if initiator && ourBalance.ToSatoshis() <= 2*DefaultDustLimit() { if initiator && ourBalance.ToSatoshis() <= 2*DefaultDustLimit() {
return nil, fmt.Errorf("unable to init reservation, with "+ return nil, ErrFunderBalanceDust(
"fee=%v sat/kw, local output is too small: %v sat", int64(commitFee),
int64(commitFee), int64(ourBalance.ToSatoshis())) int64(ourBalance.ToSatoshis()),
int64(2*DefaultDustLimit()),
)
} }
// Next we'll set the channel type based on what we can ascertain about // Next we'll set the channel type based on what we can ascertain about
@ -282,8 +283,9 @@ func (r *ChannelReservation) CommitConstraints(csvDelay, maxHtlcs uint16,
// Fail if we consider csvDelay excessively large. // Fail if we consider csvDelay excessively large.
// TODO(halseth): find a more scientific choice of value. // TODO(halseth): find a more scientific choice of value.
if csvDelay > 10000 { const maxDelay = 10000
return fmt.Errorf("csvDelay is too large: %d", csvDelay) if csvDelay > maxDelay {
return ErrCsvDelayTooLarge(csvDelay, maxDelay)
} }
// Fail if we consider the channel reserve to be too large. // Fail if we consider the channel reserve to be too large.
@ -291,8 +293,7 @@ func (r *ChannelReservation) CommitConstraints(csvDelay, maxHtlcs uint16,
// channel capacity. // channel capacity.
maxChanReserve := r.partialState.Capacity / 5 maxChanReserve := r.partialState.Capacity / 5
if chanReserve > maxChanReserve { if chanReserve > maxChanReserve {
return fmt.Errorf("chanReserve is too large: %g", return ErrChanReserveTooLarge(chanReserve, maxChanReserve)
chanReserve.ToBTC())
} }
// Fail if the minimum HTLC value is too large. If this is // Fail if the minimum HTLC value is too large. If this is
@ -302,29 +303,28 @@ func (r *ChannelReservation) CommitConstraints(csvDelay, maxHtlcs uint16,
// it wants. // it wants.
// TODO(halseth): set a reasonable/dynamic value. // TODO(halseth): set a reasonable/dynamic value.
if minHtlc > maxValueInFlight { if minHtlc > maxValueInFlight {
return fmt.Errorf("minimum HTLC value is too large: %g", return ErrMinHtlcTooLarge(minHtlc, maxValueInFlight)
r.ourContribution.MinHTLC.ToBTC())
} }
// Fail if maxHtlcs is above the maximum allowed number of 483. // Fail if maxHtlcs is above the maximum allowed number of 483.
// This number is specified in BOLT-02. // This number is specified in BOLT-02.
if maxHtlcs > uint16(MaxHTLCNumber/2) { if maxHtlcs > uint16(MaxHTLCNumber/2) {
return fmt.Errorf("maxHtlcs is too large: %d", maxHtlcs) return ErrMaxHtlcNumTooLarge(maxHtlcs, uint16(MaxHTLCNumber/2))
} }
// Fail if we consider maxHtlcs too small. If this is too small // Fail if we consider maxHtlcs too small. If this is too small
// we cannot offer many HTLCs to the remote. // we cannot offer many HTLCs to the remote.
const minNumHtlc = 5 const minNumHtlc = 5
if maxHtlcs < minNumHtlc { if maxHtlcs < minNumHtlc {
return fmt.Errorf("maxHtlcs is too small: %d", maxHtlcs) return ErrMaxHtlcNumTooSmall(maxHtlcs, minNumHtlc)
} }
// Fail if we consider maxValueInFlight too small. We currently // Fail if we consider maxValueInFlight too small. We currently
// require the remote to at least allow minNumHtlc * minHtlc // require the remote to at least allow minNumHtlc * minHtlc
// in flight. // in flight.
if maxValueInFlight < minNumHtlc*minHtlc { if maxValueInFlight < minNumHtlc*minHtlc {
return fmt.Errorf("maxValueInFlight is too small: %g", return ErrMaxValueInFlightTooSmall(maxValueInFlight,
maxValueInFlight.ToBTC()) minNumHtlc*minHtlc)
} }
r.ourContribution.ChannelConfig.CsvDelay = csvDelay r.ourContribution.ChannelConfig.CsvDelay = csvDelay