From 71410f6a08484f58994a1b4d05ac6d0e9b4ec30d Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Mon, 10 Dec 2018 13:58:34 -0800 Subject: [PATCH] lnwallet+fundingmanager: use ChannelConstraints struct with CommitConstraints --- fundingmanager.go | 38 +++++++++++++++++---------- lnwallet/interface_test.go | 40 ++++++++++++++-------------- lnwallet/reservation.go | 54 +++++++++++++++++++------------------- 3 files changed, 71 insertions(+), 61 deletions(-) diff --git a/fundingmanager.go b/fundingmanager.go index 9dc14d2aa..5c5f9687d 100644 --- a/fundingmanager.go +++ b/fundingmanager.go @@ -1072,20 +1072,25 @@ func (f *fundingManager) handleFundingOpen(fmsg *fundingOpenMsg) { return } - // As we're the responder, we get to specify the number of - // confirmations that we require before both of us consider the channel - // open. We'll use out mapping to derive the proper number of - // confirmations based on the amount of the channel, and also if any - // funds are being pushed to us. + // As we're the responder, we get to specify the number of confirmations + // that we require before both of us consider the channel open. We'll + // use out mapping to derive the proper number of confirmations based on + // the amount of the channel, and also if any funds are being pushed to + // us. numConfsReq := f.cfg.NumRequiredConfs(msg.FundingAmount, msg.PushAmount) reservation.SetNumConfsRequired(numConfsReq) // We'll also validate and apply all the constraints the initiating // party is attempting to dictate for our commitment transaction. - err = reservation.CommitConstraints( - msg.CsvDelay, msg.MaxAcceptedHTLCs, msg.MaxValueInFlight, - msg.HtlcMinimum, msg.ChannelReserve, msg.DustLimit, - ) + channelConstraints := &channeldb.ChannelConstraints{ + DustLimit: msg.DustLimit, + ChanReserve: msg.ChannelReserve, + MaxPendingAmount: msg.MaxValueInFlight, + MinHTLC: msg.HtlcMinimum, + MaxAcceptedHtlcs: msg.MaxAcceptedHTLCs, + CsvDelay: msg.CsvDelay, + } + err = reservation.CommitConstraints(channelConstraints) if err != nil { fndgLog.Errorf("Unacceptable channel constraints: %v", err) f.failFundingFlow(fmsg.peer, fmsg.msg.PendingChannelID, err) @@ -1229,10 +1234,15 @@ func (f *fundingManager) handleFundingAccept(fmsg *fundingAcceptMsg) { // required confirmations, and also the set of channel constraints // they've specified for commitment states we can create. resCtx.reservation.SetNumConfsRequired(uint16(msg.MinAcceptDepth)) - err = resCtx.reservation.CommitConstraints( - msg.CsvDelay, msg.MaxAcceptedHTLCs, msg.MaxValueInFlight, - msg.HtlcMinimum, msg.ChannelReserve, msg.DustLimit, - ) + channelConstraints := &channeldb.ChannelConstraints{ + DustLimit: msg.DustLimit, + ChanReserve: msg.ChannelReserve, + MaxPendingAmount: msg.MaxValueInFlight, + MinHTLC: msg.HtlcMinimum, + MaxAcceptedHtlcs: msg.MaxAcceptedHTLCs, + CsvDelay: msg.CsvDelay, + } + err = resCtx.reservation.CommitConstraints(channelConstraints) if err != nil { fndgLog.Warnf("Unacceptable channel constraints: %v", err) f.failFundingFlow(fmsg.peer, fmsg.msg.PendingChannelID, err) @@ -1860,7 +1870,7 @@ func (f *fundingManager) waitForFundingConfirmation(completeChan *channeldb.Open ) if err != nil { fndgLog.Errorf("Unable to register for confirmation of "+ - "ChannelPoint(%v)", completeChan.FundingOutpoint) + "ChannelPoint(%v): %v", completeChan.FundingOutpoint, err) return } diff --git a/lnwallet/interface_test.go b/lnwallet/interface_test.go index c95b82a00..577c53960 100644 --- a/lnwallet/interface_test.go +++ b/lnwallet/interface_test.go @@ -429,11 +429,15 @@ func testDualFundingReservationWorkflow(miner *rpctest.Harness, t.Fatalf("unable to initialize funding reservation: %v", err) } aliceChanReservation.SetNumConfsRequired(numReqConfs) - err = aliceChanReservation.CommitConstraints( - csvDelay, lnwallet.MaxHTLCNumber/2, - lnwire.NewMSatFromSatoshis(fundingAmount), 1, fundingAmount/100, - lnwallet.DefaultDustLimit(), - ) + channelConstraints := &channeldb.ChannelConstraints{ + DustLimit: lnwallet.DefaultDustLimit(), + ChanReserve: fundingAmount / 100, + MaxPendingAmount: lnwire.NewMSatFromSatoshis(fundingAmount), + MinHTLC: 1, + MaxAcceptedHtlcs: lnwallet.MaxHTLCNumber / 2, + CsvDelay: csvDelay, + } + err = aliceChanReservation.CommitConstraints(channelConstraints) if err != nil { t.Fatalf("unable to verify constraints: %v", err) } @@ -467,11 +471,7 @@ func testDualFundingReservationWorkflow(miner *rpctest.Harness, if err != nil { t.Fatalf("bob unable to init channel reservation: %v", err) } - err = bobChanReservation.CommitConstraints( - csvDelay, lnwallet.MaxHTLCNumber/2, - lnwire.NewMSatFromSatoshis(fundingAmount), 1, fundingAmount/100, - lnwallet.DefaultDustLimit(), - ) + err = bobChanReservation.CommitConstraints(channelConstraints) if err != nil { t.Fatalf("unable to verify constraints: %v", err) } @@ -861,11 +861,15 @@ func testSingleFunderReservationWorkflow(miner *rpctest.Harness, t.Fatalf("unable to init channel reservation: %v", err) } aliceChanReservation.SetNumConfsRequired(numReqConfs) - err = aliceChanReservation.CommitConstraints( - csvDelay, lnwallet.MaxHTLCNumber/2, - lnwire.NewMSatFromSatoshis(fundingAmt), 1, fundingAmt/100, - lnwallet.DefaultDustLimit(), - ) + channelConstraints := &channeldb.ChannelConstraints{ + DustLimit: lnwallet.DefaultDustLimit(), + ChanReserve: fundingAmt / 100, + MaxPendingAmount: lnwire.NewMSatFromSatoshis(fundingAmt), + MinHTLC: 1, + MaxAcceptedHtlcs: lnwallet.MaxHTLCNumber / 2, + CsvDelay: csvDelay, + } + err = aliceChanReservation.CommitConstraints(channelConstraints) if err != nil { t.Fatalf("unable to verify constraints: %v", err) } @@ -899,11 +903,7 @@ func testSingleFunderReservationWorkflow(miner *rpctest.Harness, if err != nil { t.Fatalf("unable to create bob reservation: %v", err) } - err = bobChanReservation.CommitConstraints( - csvDelay, lnwallet.MaxHTLCNumber/2, - lnwire.NewMSatFromSatoshis(fundingAmt), 1, fundingAmt/100, - lnwallet.DefaultDustLimit(), - ) + err = bobChanReservation.CommitConstraints(channelConstraints) if err != nil { t.Fatalf("unable to verify constraints: %v", err) } diff --git a/lnwallet/reservation.go b/lnwallet/reservation.go index 62bd449db..6faa0cd5c 100644 --- a/lnwallet/reservation.go +++ b/lnwallet/reservation.go @@ -282,72 +282,72 @@ func (r *ChannelReservation) SetNumConfsRequired(numConfs uint16) { // of satoshis that can be transferred in a single commitment. This function // will also attempt to verify the constraints for sanity, returning an error // if the parameters are seemed unsound. -func (r *ChannelReservation) CommitConstraints(csvDelay, maxHtlcs uint16, - maxValueInFlight, minHtlc lnwire.MilliSatoshi, - chanReserve, dustLimit btcutil.Amount) error { - +func (r *ChannelReservation) CommitConstraints(c *channeldb.ChannelConstraints) error { r.Lock() defer r.Unlock() // Fail if we consider csvDelay excessively large. // TODO(halseth): find a more scientific choice of value. const maxDelay = 10000 - if csvDelay > maxDelay { - return ErrCsvDelayTooLarge(csvDelay, maxDelay) + if c.CsvDelay > maxDelay { + return ErrCsvDelayTooLarge(c.CsvDelay, maxDelay) } // The dust limit should always be greater or equal to the channel // reserve. The reservation request should be denied if otherwise. - if dustLimit > chanReserve { - return ErrChanReserveTooSmall(chanReserve, dustLimit) + if c.DustLimit > c.ChanReserve { + return ErrChanReserveTooSmall(c.ChanReserve, c.DustLimit) } // Fail if we consider the channel reserve to be too large. We // currently fail if it is greater than 20% of the channel capacity. maxChanReserve := r.partialState.Capacity / 5 - if chanReserve > maxChanReserve { - return ErrChanReserveTooLarge(chanReserve, maxChanReserve) + if c.ChanReserve > maxChanReserve { + return ErrChanReserveTooLarge(c.ChanReserve, maxChanReserve) } // Fail if the minimum HTLC value is too large. If this is too large, // the channel won't be useful for sending small payments. This limit // is currently set to maxValueInFlight, effectively letting the remote // setting this as large as it wants. - if minHtlc > maxValueInFlight { - return ErrMinHtlcTooLarge(minHtlc, maxValueInFlight) + if c.MinHTLC > c.MaxPendingAmount { + return ErrMinHtlcTooLarge(c.MinHTLC, c.MaxPendingAmount) } // Fail if maxHtlcs is above the maximum allowed number of 483. This // number is specified in BOLT-02. - if maxHtlcs > uint16(MaxHTLCNumber/2) { - return ErrMaxHtlcNumTooLarge(maxHtlcs, uint16(MaxHTLCNumber/2)) + if c.MaxAcceptedHtlcs > uint16(MaxHTLCNumber/2) { + return ErrMaxHtlcNumTooLarge( + c.MaxAcceptedHtlcs, uint16(MaxHTLCNumber/2), + ) } // Fail if we consider maxHtlcs too small. If this is too small we // cannot offer many HTLCs to the remote. const minNumHtlc = 5 - if maxHtlcs < minNumHtlc { - return ErrMaxHtlcNumTooSmall(maxHtlcs, minNumHtlc) + if c.MaxAcceptedHtlcs < minNumHtlc { + return ErrMaxHtlcNumTooSmall(c.MaxAcceptedHtlcs, minNumHtlc) } // Fail if we consider maxValueInFlight too small. We currently require // the remote to at least allow minNumHtlc * minHtlc in flight. - if maxValueInFlight < minNumHtlc*minHtlc { - return ErrMaxValueInFlightTooSmall(maxValueInFlight, - minNumHtlc*minHtlc) + if c.MaxPendingAmount < minNumHtlc*c.MinHTLC { + return ErrMaxValueInFlightTooSmall( + c.MaxPendingAmount, minNumHtlc*c.MinHTLC, + ) } - // Our dust limit should always be less than or equal our proposed + // Our dust limit should always be less than or equal to our proposed // channel reserve. - if r.ourContribution.DustLimit > chanReserve { - r.ourContribution.DustLimit = chanReserve + if r.ourContribution.DustLimit > c.ChanReserve { + r.ourContribution.DustLimit = c.ChanReserve } - r.ourContribution.ChannelConfig.CsvDelay = csvDelay - r.ourContribution.ChannelConfig.ChanReserve = chanReserve - r.ourContribution.ChannelConfig.MaxAcceptedHtlcs = maxHtlcs - r.ourContribution.ChannelConfig.MaxPendingAmount = maxValueInFlight - r.ourContribution.ChannelConfig.MinHTLC = minHtlc + r.ourContribution.ChanReserve = c.ChanReserve + r.ourContribution.MaxPendingAmount = c.MaxPendingAmount + r.ourContribution.MinHTLC = c.MinHTLC + r.ourContribution.MaxAcceptedHtlcs = c.MaxAcceptedHtlcs + r.ourContribution.CsvDelay = c.CsvDelay return nil }