2015-12-20 09:00:50 +03:00
|
|
|
package lnwallet
|
2015-11-13 05:43:32 +03:00
|
|
|
|
|
|
|
import (
|
2016-10-27 00:56:48 +03:00
|
|
|
"net"
|
2015-11-14 22:52:07 +03:00
|
|
|
"sync"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2018-07-26 16:16:47 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-01-16 17:47:43 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2019-10-31 05:43:05 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2019-11-01 07:39:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
|
2018-07-26 16:16:47 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2015-11-13 05:43:32 +03:00
|
|
|
)
|
|
|
|
|
2020-03-06 18:11:48 +03:00
|
|
|
// CommitmentType is an enum indicating the commitment type we should use for
|
|
|
|
// the channel we are opening.
|
|
|
|
type CommitmentType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CommitmentTypeLegacy is the legacy commitment format with a tweaked
|
|
|
|
// to_remote key.
|
|
|
|
CommitmentTypeLegacy = iota
|
|
|
|
|
|
|
|
// CommitmentTypeTweakless is a newer commitment format where the
|
|
|
|
// to_remote key is static.
|
|
|
|
CommitmentTypeTweakless
|
2020-03-06 18:11:49 +03:00
|
|
|
|
|
|
|
// CommitmentTypeAnchors is a commitment type that is tweakless, and
|
|
|
|
// has extra anchor ouputs in order to bump the fee of the commitment
|
|
|
|
// transaction.
|
|
|
|
CommitmentTypeAnchors
|
2020-03-06 18:11:48 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns the name of the CommitmentType.
|
|
|
|
func (c CommitmentType) String() string {
|
|
|
|
switch c {
|
|
|
|
case CommitmentTypeLegacy:
|
|
|
|
return "legacy"
|
|
|
|
case CommitmentTypeTweakless:
|
|
|
|
return "tweakless"
|
2020-03-06 18:11:49 +03:00
|
|
|
case CommitmentTypeAnchors:
|
|
|
|
return "anchors"
|
2020-03-06 18:11:48 +03:00
|
|
|
default:
|
|
|
|
return "invalid"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 04:58:55 +03:00
|
|
|
// ChannelContribution is the primary constituent of the funding workflow
|
|
|
|
// within lnwallet. Each side first exchanges their respective contributions
|
|
|
|
// along with channel specific parameters like the min fee/KB. Once
|
|
|
|
// contributions have been exchanged, each side will then produce signatures
|
|
|
|
// for all their inputs to the funding transactions, and finally a signature
|
|
|
|
// for the other party's version of the commitment transaction.
|
2015-12-23 07:30:11 +03:00
|
|
|
type ChannelContribution struct {
|
2016-06-30 22:02:45 +03:00
|
|
|
// FundingOutpoint is the amount of funds contributed to the funding
|
|
|
|
// transaction.
|
2015-12-23 07:30:11 +03:00
|
|
|
FundingAmount btcutil.Amount
|
2015-12-18 22:29:35 +03:00
|
|
|
|
2015-12-23 07:30:11 +03:00
|
|
|
// Inputs to the funding transaction.
|
|
|
|
Inputs []*wire.TxIn
|
|
|
|
|
2016-06-30 22:02:45 +03:00
|
|
|
// ChangeOutputs are the Outputs to be used in the case that the total
|
2016-10-16 02:02:09 +03:00
|
|
|
// value of the funding inputs is greater than the total potential
|
2016-06-30 22:02:45 +03:00
|
|
|
// channel capacity.
|
2015-12-23 07:30:11 +03:00
|
|
|
ChangeOutputs []*wire.TxOut
|
|
|
|
|
2017-07-30 04:58:55 +03:00
|
|
|
// FirstCommitmentPoint is the first commitment point that will be used
|
|
|
|
// to create the revocation key in the first commitment transaction we
|
|
|
|
// send to the remote party.
|
|
|
|
FirstCommitmentPoint *btcec.PublicKey
|
2015-11-14 22:52:07 +03:00
|
|
|
|
2017-07-30 04:58:55 +03:00
|
|
|
// ChannelConfig is the concrete contribution that this node is
|
|
|
|
// offering to the channel. This includes all the various constraints
|
|
|
|
// such as the min HTLC, and also all the keys which will be used for
|
|
|
|
// the duration of the channel.
|
|
|
|
*channeldb.ChannelConfig
|
2019-12-03 12:38:29 +03:00
|
|
|
|
|
|
|
// UpfrontShutdown is an optional address to which the channel should be
|
|
|
|
// paid out to on cooperative close.
|
|
|
|
UpfrontShutdown lnwire.DeliveryAddress
|
2017-07-30 04:58:55 +03:00
|
|
|
}
|
2017-04-13 05:20:36 +03:00
|
|
|
|
2017-07-30 04:58:55 +03:00
|
|
|
// toChanConfig returns the raw channel configuration generated by a node's
|
|
|
|
// contribution to the channel.
|
|
|
|
func (c *ChannelContribution) toChanConfig() channeldb.ChannelConfig {
|
|
|
|
return *c.ChannelConfig
|
2015-12-23 07:30:11 +03:00
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// ChannelReservation represents an intent to open a lightning payment channel
|
2018-03-10 18:21:10 +03:00
|
|
|
// with a counterparty. The funding processes from reservation to channel opening
|
|
|
|
// is a 3-step process. In order to allow for full concurrency during the
|
2016-10-16 02:02:09 +03:00
|
|
|
// reservation workflow, resources consumed by a contribution are "locked"
|
|
|
|
// themselves. This prevents a number of race conditions such as two funding
|
|
|
|
// transactions double-spending the same input. A reservation can also be
|
2019-10-03 18:22:43 +03:00
|
|
|
// canceled, which removes the resources from limbo, allowing another
|
2016-10-16 02:02:09 +03:00
|
|
|
// reservation to claim them.
|
2016-01-02 23:09:31 +03:00
|
|
|
//
|
|
|
|
// The reservation workflow consists of the following three steps:
|
|
|
|
// 1. lnwallet.InitChannelReservation
|
2016-10-16 02:02:09 +03:00
|
|
|
// * One requests the wallet to allocate the necessary resources for a
|
2018-03-10 18:21:10 +03:00
|
|
|
// channel reservation. These resources are put in limbo for the lifetime
|
|
|
|
// of a reservation.
|
|
|
|
// * Once completed the reservation will have the wallet's contribution
|
|
|
|
// accessible via the .OurContribution() method. This contribution
|
|
|
|
// contains the necessary items to allow the remote party to build both
|
|
|
|
// the funding, and commitment transactions.
|
2016-06-21 08:31:57 +03:00
|
|
|
// 2. ChannelReservation.ProcessContribution/ChannelReservation.ProcessSingleContribution
|
2016-01-02 23:09:31 +03:00
|
|
|
// * The counterparty presents their contribution to the payment channel.
|
|
|
|
// This allows us to build the funding, and commitment transactions
|
|
|
|
// ourselves.
|
|
|
|
// * We're now able to sign our inputs to the funding transactions, and
|
|
|
|
// the counterparty's version of the commitment transaction.
|
|
|
|
// * All signatures crafted by us, are now available via .OurSignatures().
|
2016-06-21 08:31:57 +03:00
|
|
|
// 3. ChannelReservation.CompleteReservation/ChannelReservation.CompleteReservationSingle
|
2016-01-02 23:09:31 +03:00
|
|
|
// * The final step in the workflow. The counterparty presents the
|
2016-10-16 02:02:09 +03:00
|
|
|
// signatures for all their inputs to the funding transaction, as well
|
2016-01-02 23:09:31 +03:00
|
|
|
// as a signature to our version of the commitment transaction.
|
|
|
|
// * We then verify the validity of all signatures before considering the
|
|
|
|
// channel "open".
|
2015-12-23 07:30:11 +03:00
|
|
|
type ChannelReservation struct {
|
|
|
|
// This mutex MUST be held when either reading or modifying any of the
|
|
|
|
// fields below.
|
|
|
|
sync.RWMutex
|
2015-11-13 05:43:32 +03:00
|
|
|
|
2016-06-21 08:31:57 +03:00
|
|
|
// fundingTx is the funding transaction for this pending channel.
|
|
|
|
fundingTx *wire.MsgTx
|
|
|
|
|
2015-11-13 05:43:32 +03:00
|
|
|
// In order of sorted inputs. Sorting is done in accordance
|
|
|
|
// to BIP-69: https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki.
|
2019-01-16 17:47:43 +03:00
|
|
|
ourFundingInputScripts []*input.Script
|
|
|
|
theirFundingInputScripts []*input.Script
|
2015-11-13 05:43:32 +03:00
|
|
|
|
2015-12-23 07:30:11 +03:00
|
|
|
// Our signature for their version of the commitment transaction.
|
2015-12-29 08:38:50 +03:00
|
|
|
ourCommitmentSig []byte
|
|
|
|
theirCommitmentSig []byte
|
2015-11-13 05:43:32 +03:00
|
|
|
|
2015-12-23 07:30:11 +03:00
|
|
|
ourContribution *ChannelContribution
|
|
|
|
theirContribution *ChannelContribution
|
|
|
|
|
2015-12-26 21:35:15 +03:00
|
|
|
partialState *channeldb.OpenChannel
|
2018-02-03 09:24:43 +03:00
|
|
|
nodeAddr net.Addr
|
2015-11-13 05:43:32 +03:00
|
|
|
|
2015-12-23 07:30:11 +03:00
|
|
|
// The ID of this reservation, used to uniquely track the reservation
|
|
|
|
// throughout its lifetime.
|
2015-11-13 05:43:32 +03:00
|
|
|
reservationID uint64
|
2015-12-16 00:22:18 +03:00
|
|
|
|
2019-11-01 07:45:44 +03:00
|
|
|
// pendingChanID is the pending channel ID for this channel as
|
|
|
|
// identified within the wire protocol.
|
|
|
|
pendingChanID [32]byte
|
|
|
|
|
2017-08-22 09:20:57 +03:00
|
|
|
// pushMSat the amount of milli-satoshis that should be pushed to the
|
2017-01-10 04:24:13 +03:00
|
|
|
// responder of a single funding channel as part of the initial
|
|
|
|
// commitment state.
|
2017-08-22 09:20:57 +03:00
|
|
|
pushMSat lnwire.MilliSatoshi
|
2017-01-10 04:24:13 +03:00
|
|
|
|
2019-11-01 07:39:17 +03:00
|
|
|
wallet *LightningWallet
|
|
|
|
chanFunder chanfunding.Assembler
|
|
|
|
|
|
|
|
fundingIntent chanfunding.Intent
|
2015-11-13 05:43:32 +03:00
|
|
|
}
|
|
|
|
|
2016-08-13 01:50:47 +03:00
|
|
|
// NewChannelReservation creates a new channel reservation. This function is
|
2017-01-10 04:24:13 +03:00
|
|
|
// used only internally by lnwallet. In order to concurrent safety, the
|
|
|
|
// creation of all channel reservations should be carried out via the
|
2016-01-02 23:09:31 +03:00
|
|
|
// lnwallet.InitChannelReservation interface.
|
2019-07-11 14:14:36 +03:00
|
|
|
func NewChannelReservation(capacity, localFundingAmt btcutil.Amount,
|
2019-10-31 05:43:05 +03:00
|
|
|
commitFeePerKw chainfee.SatPerKWeight, wallet *LightningWallet,
|
2018-02-13 16:37:47 +03:00
|
|
|
id uint64, pushMSat lnwire.MilliSatoshi, chainHash *chainhash.Hash,
|
2020-03-06 18:11:48 +03:00
|
|
|
flags lnwire.FundingFlag, commitType CommitmentType,
|
2019-11-01 07:45:44 +03:00
|
|
|
fundingAssembler chanfunding.Assembler,
|
2020-03-14 02:57:48 +03:00
|
|
|
pendingChanID [32]byte, thawHeight uint32) (*ChannelReservation, error) {
|
2016-11-16 23:51:18 +03:00
|
|
|
|
|
|
|
var (
|
2017-08-22 09:20:57 +03:00
|
|
|
ourBalance lnwire.MilliSatoshi
|
|
|
|
theirBalance lnwire.MilliSatoshi
|
2017-01-17 07:28:30 +03:00
|
|
|
initiator bool
|
2016-11-16 23:51:18 +03:00
|
|
|
)
|
2016-06-21 08:31:57 +03:00
|
|
|
|
2020-03-06 18:11:49 +03:00
|
|
|
// Based on the channel type, we determine the initial commit weight
|
|
|
|
// and fee.
|
|
|
|
commitWeight := int64(input.CommitWeight)
|
|
|
|
if commitType == CommitmentTypeAnchors {
|
|
|
|
commitWeight = input.AnchorCommitWeight
|
|
|
|
}
|
|
|
|
commitFee := commitFeePerKw.FeeForWeight(commitWeight)
|
|
|
|
|
2019-07-11 14:14:36 +03:00
|
|
|
localFundingMSat := lnwire.NewMSatFromSatoshis(localFundingAmt)
|
2019-08-01 06:16:52 +03:00
|
|
|
// TODO(halseth): make method take remote funding amount directly
|
2019-07-11 14:14:36 +03:00
|
|
|
// instead of inferring it from capacity and local amt.
|
2017-08-22 09:20:57 +03:00
|
|
|
capacityMSat := lnwire.NewMSatFromSatoshis(capacity)
|
2020-03-06 18:11:49 +03:00
|
|
|
|
|
|
|
// The total fee paid by the initiator will be the commitment fee in
|
|
|
|
// addition to the two anchor outputs.
|
2017-08-22 09:20:57 +03:00
|
|
|
feeMSat := lnwire.NewMSatFromSatoshis(commitFee)
|
2020-03-06 18:11:49 +03:00
|
|
|
if commitType == CommitmentTypeAnchors {
|
|
|
|
feeMSat += 2 * lnwire.NewMSatFromSatoshis(anchorSize)
|
|
|
|
}
|
2017-08-22 09:20:57 +03:00
|
|
|
|
2016-10-24 04:42:03 +03:00
|
|
|
// If we're the responder to a single-funder reservation, then we have
|
2017-01-10 04:24:13 +03:00
|
|
|
// no initial balance in the channel unless the remote party is pushing
|
|
|
|
// some funds to us within the first commitment state.
|
2019-07-11 14:14:36 +03:00
|
|
|
if localFundingAmt == 0 {
|
2017-08-22 09:20:57 +03:00
|
|
|
ourBalance = pushMSat
|
|
|
|
theirBalance = capacityMSat - feeMSat - pushMSat
|
2017-01-17 07:28:30 +03:00
|
|
|
initiator = false
|
2018-02-25 06:11:14 +03:00
|
|
|
|
|
|
|
// If the responder doesn't have enough funds to actually pay
|
|
|
|
// the fees, then we'll bail our early.
|
|
|
|
if int64(theirBalance) < 0 {
|
|
|
|
return nil, ErrFunderBalanceDust(
|
|
|
|
int64(commitFee), int64(theirBalance.ToSatoshis()),
|
|
|
|
int64(2*DefaultDustLimit()),
|
|
|
|
)
|
|
|
|
}
|
2016-06-21 08:31:57 +03:00
|
|
|
} else {
|
2016-09-13 05:05:56 +03:00
|
|
|
// TODO(roasbeef): need to rework fee structure in general and
|
|
|
|
// also when we "unlock" dual funder within the daemon
|
2016-10-24 04:42:03 +03:00
|
|
|
|
2019-07-11 14:14:36 +03:00
|
|
|
if capacity == localFundingAmt {
|
2016-11-16 23:51:18 +03:00
|
|
|
// If we're initiating a single funder workflow, then
|
|
|
|
// we pay all the initial fees within the commitment
|
2017-01-10 04:24:13 +03:00
|
|
|
// transaction. We also deduct our balance by the
|
|
|
|
// amount pushed as part of the initial state.
|
2017-08-22 09:20:57 +03:00
|
|
|
ourBalance = capacityMSat - feeMSat - pushMSat
|
|
|
|
theirBalance = pushMSat
|
2016-09-13 05:05:56 +03:00
|
|
|
} else {
|
2016-11-16 23:51:18 +03:00
|
|
|
// Otherwise, this is a dual funder workflow where both
|
|
|
|
// slides split the amount funded and the commitment
|
|
|
|
// fee.
|
2019-07-11 14:14:36 +03:00
|
|
|
ourBalance = localFundingMSat - (feeMSat / 2)
|
|
|
|
theirBalance = capacityMSat - localFundingMSat - (feeMSat / 2) + pushMSat
|
2016-09-13 05:05:56 +03:00
|
|
|
}
|
2016-10-24 04:42:03 +03:00
|
|
|
|
2017-01-17 07:28:30 +03:00
|
|
|
initiator = true
|
2018-02-25 06:11:14 +03:00
|
|
|
|
|
|
|
// If we, the initiator don't have enough funds to actually pay
|
|
|
|
// the fees, then we'll exit with an error.
|
|
|
|
if int64(ourBalance) < 0 {
|
|
|
|
return nil, ErrFunderBalanceDust(
|
|
|
|
int64(commitFee), int64(ourBalance),
|
|
|
|
int64(2*DefaultDustLimit()),
|
|
|
|
)
|
|
|
|
}
|
2016-06-21 08:31:57 +03:00
|
|
|
}
|
|
|
|
|
2017-11-26 22:32:57 +03:00
|
|
|
// If we're the initiator and our starting balance within the channel
|
2018-01-29 02:11:13 +03:00
|
|
|
// after we take account of fees is below 2x the dust limit, then we'll
|
|
|
|
// reject this channel creation request.
|
2017-11-26 22:32:57 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): reject if 30% goes to fees? dust channel
|
2018-01-29 02:11:13 +03:00
|
|
|
if initiator && ourBalance.ToSatoshis() <= 2*DefaultDustLimit() {
|
2018-02-27 20:32:54 +03:00
|
|
|
return nil, ErrFunderBalanceDust(
|
|
|
|
int64(commitFee),
|
|
|
|
int64(ourBalance.ToSatoshis()),
|
|
|
|
int64(2*DefaultDustLimit()),
|
|
|
|
)
|
2017-11-26 22:32:57 +03:00
|
|
|
}
|
|
|
|
|
2020-03-06 18:11:48 +03:00
|
|
|
// Similarly we ensure their balance is reasonable if we are not the
|
|
|
|
// initiator.
|
|
|
|
if !initiator && theirBalance.ToSatoshis() <= 2*DefaultDustLimit() {
|
|
|
|
return nil, ErrFunderBalanceDust(
|
|
|
|
int64(commitFee),
|
|
|
|
int64(theirBalance.ToSatoshis()),
|
|
|
|
int64(2*DefaultDustLimit()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-01-17 07:28:30 +03:00
|
|
|
// Next we'll set the channel type based on what we can ascertain about
|
|
|
|
// the balances/push amount within the channel.
|
|
|
|
var chanType channeldb.ChannelType
|
2016-11-16 23:51:18 +03:00
|
|
|
|
2017-01-17 07:28:30 +03:00
|
|
|
// If either of the balances are zero at this point, or we have a
|
|
|
|
// non-zero push amt (there's no pushing for dual funder), then this is
|
|
|
|
// a single-funder channel.
|
2017-08-22 09:20:57 +03:00
|
|
|
if ourBalance == 0 || theirBalance == 0 || pushMSat != 0 {
|
2020-03-06 18:11:49 +03:00
|
|
|
// Both the tweakless type and the anchor type is tweakless,
|
|
|
|
// hence set the bit.
|
|
|
|
if commitType == CommitmentTypeTweakless ||
|
|
|
|
commitType == CommitmentTypeAnchors {
|
|
|
|
|
2019-10-31 05:24:49 +03:00
|
|
|
chanType |= channeldb.SingleFunderTweaklessBit
|
2019-08-01 06:16:52 +03:00
|
|
|
} else {
|
2019-10-31 05:24:49 +03:00
|
|
|
chanType |= channeldb.SingleFunderBit
|
2019-08-01 06:16:52 +03:00
|
|
|
}
|
2019-11-01 07:39:17 +03:00
|
|
|
|
|
|
|
// If this intent isn't one that's able to provide us with a
|
|
|
|
// funding transaction, then we'll set the chanType bit to
|
|
|
|
// signal that we don't have access to one.
|
|
|
|
if _, ok := fundingAssembler.(chanfunding.FundingTxAssembler); !ok {
|
|
|
|
chanType |= channeldb.NoFundingTxBit
|
|
|
|
}
|
|
|
|
|
2017-01-17 07:28:30 +03:00
|
|
|
} else {
|
|
|
|
// Otherwise, this is a dual funder channel, and no side is
|
|
|
|
// technically the "initiator"
|
2016-11-16 23:51:18 +03:00
|
|
|
initiator = false
|
2019-10-31 05:24:49 +03:00
|
|
|
chanType |= channeldb.DualFunderBit
|
2016-11-16 23:51:18 +03:00
|
|
|
}
|
|
|
|
|
2020-03-06 18:11:49 +03:00
|
|
|
// We are adding anchor outputs to our commitment.
|
|
|
|
if commitType == CommitmentTypeAnchors {
|
|
|
|
chanType |= channeldb.AnchorOutputsBit
|
|
|
|
}
|
|
|
|
|
2020-03-14 02:57:48 +03:00
|
|
|
// If the channel is meant to be frozen, then we'll set the frozen bit
|
|
|
|
// now so once the channel is open, it can be interpreted properly.
|
|
|
|
if thawHeight != 0 {
|
|
|
|
chanType |= channeldb.FrozenBit
|
|
|
|
}
|
|
|
|
|
2015-12-19 06:42:49 +03:00
|
|
|
return &ChannelReservation{
|
2015-12-23 07:30:11 +03:00
|
|
|
ourContribution: &ChannelContribution{
|
2017-08-22 09:20:57 +03:00
|
|
|
FundingAmount: ourBalance.ToSatoshis(),
|
2017-07-30 04:58:55 +03:00
|
|
|
ChannelConfig: &channeldb.ChannelConfig{},
|
2015-12-23 07:30:11 +03:00
|
|
|
},
|
|
|
|
theirContribution: &ChannelContribution{
|
2017-08-22 09:20:57 +03:00
|
|
|
FundingAmount: theirBalance.ToSatoshis(),
|
2017-07-30 04:58:55 +03:00
|
|
|
ChannelConfig: &channeldb.ChannelConfig{},
|
2015-12-23 07:30:11 +03:00
|
|
|
},
|
2015-12-26 21:35:15 +03:00
|
|
|
partialState: &channeldb.OpenChannel{
|
2017-11-14 04:15:27 +03:00
|
|
|
ChanType: chanType,
|
|
|
|
ChainHash: *chainHash,
|
|
|
|
IsPending: true,
|
|
|
|
IsInitiator: initiator,
|
|
|
|
ChannelFlags: flags,
|
|
|
|
Capacity: capacity,
|
2017-11-11 01:18:41 +03:00
|
|
|
LocalCommitment: channeldb.ChannelCommitment{
|
|
|
|
LocalBalance: ourBalance,
|
|
|
|
RemoteBalance: theirBalance,
|
2018-02-13 16:37:47 +03:00
|
|
|
FeePerKw: btcutil.Amount(commitFeePerKw),
|
2017-11-11 01:18:41 +03:00
|
|
|
CommitFee: commitFee,
|
|
|
|
},
|
|
|
|
RemoteCommitment: channeldb.ChannelCommitment{
|
|
|
|
LocalBalance: ourBalance,
|
|
|
|
RemoteBalance: theirBalance,
|
2018-02-13 16:37:47 +03:00
|
|
|
FeePerKw: btcutil.Amount(commitFeePerKw),
|
2017-11-11 01:18:41 +03:00
|
|
|
CommitFee: commitFee,
|
|
|
|
},
|
2020-03-14 02:57:48 +03:00
|
|
|
ThawHeight: thawHeight,
|
|
|
|
Db: wallet.Cfg.Database,
|
2015-12-19 06:42:49 +03:00
|
|
|
},
|
2017-08-22 09:20:57 +03:00
|
|
|
pushMSat: pushMSat,
|
2019-11-01 07:45:44 +03:00
|
|
|
pendingChanID: pendingChanID,
|
2017-07-30 04:58:55 +03:00
|
|
|
reservationID: id,
|
|
|
|
wallet: wallet,
|
2019-11-01 07:39:17 +03:00
|
|
|
chanFunder: fundingAssembler,
|
2017-11-26 22:32:57 +03:00
|
|
|
}, nil
|
2015-12-23 07:30:11 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 04:58:55 +03:00
|
|
|
// SetNumConfsRequired sets the number of confirmations that are required for
|
|
|
|
// the ultimate funding transaction before the channel can be considered open.
|
|
|
|
// This is distinct from the main reservation workflow as it allows
|
|
|
|
// implementations a bit more flexibility w.r.t to if the responder of the
|
|
|
|
// initiator sets decides the number of confirmations needed.
|
|
|
|
func (r *ChannelReservation) SetNumConfsRequired(numConfs uint16) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
r.partialState.NumConfsRequired = numConfs
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:41:40 +03:00
|
|
|
// CommitConstraints takes the constraints that the remote party specifies for
|
|
|
|
// the type of commitments that we can generate for them. These constraints
|
|
|
|
// include several parameters that serve as flow control restricting the amount
|
|
|
|
// 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.
|
2019-01-30 04:56:28 +03:00
|
|
|
func (r *ChannelReservation) CommitConstraints(c *channeldb.ChannelConstraints) error {
|
2017-07-30 04:58:55 +03:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2017-11-29 15:50:44 +03:00
|
|
|
// Fail if we consider csvDelay excessively large.
|
|
|
|
// TODO(halseth): find a more scientific choice of value.
|
2018-02-27 20:32:54 +03:00
|
|
|
const maxDelay = 10000
|
2018-12-11 00:58:34 +03:00
|
|
|
if c.CsvDelay > maxDelay {
|
|
|
|
return ErrCsvDelayTooLarge(c.CsvDelay, maxDelay)
|
2017-11-29 16:16:36 +03:00
|
|
|
}
|
|
|
|
|
2019-07-06 01:40:04 +03:00
|
|
|
// The channel reserve should always be greater or equal to the dust
|
|
|
|
// limit. The reservation request should be denied if otherwise.
|
2018-12-11 00:58:34 +03:00
|
|
|
if c.DustLimit > c.ChanReserve {
|
|
|
|
return ErrChanReserveTooSmall(c.ChanReserve, c.DustLimit)
|
2018-05-14 21:04:34 +03:00
|
|
|
}
|
|
|
|
|
2018-02-25 06:09:49 +03:00
|
|
|
// Fail if we consider the channel reserve to be too large. We
|
|
|
|
// currently fail if it is greater than 20% of the channel capacity.
|
2017-11-29 15:50:44 +03:00
|
|
|
maxChanReserve := r.partialState.Capacity / 5
|
2018-12-11 00:58:34 +03:00
|
|
|
if c.ChanReserve > maxChanReserve {
|
|
|
|
return ErrChanReserveTooLarge(c.ChanReserve, maxChanReserve)
|
2017-11-29 16:16:36 +03:00
|
|
|
}
|
|
|
|
|
2018-02-25 06:09:49 +03:00
|
|
|
// 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.
|
2018-12-11 00:58:34 +03:00
|
|
|
if c.MinHTLC > c.MaxPendingAmount {
|
|
|
|
return ErrMinHtlcTooLarge(c.MinHTLC, c.MaxPendingAmount)
|
2017-11-29 16:16:36 +03:00
|
|
|
}
|
|
|
|
|
2018-02-25 06:09:49 +03:00
|
|
|
// Fail if maxHtlcs is above the maximum allowed number of 483. This
|
|
|
|
// number is specified in BOLT-02.
|
2019-01-16 17:47:43 +03:00
|
|
|
if c.MaxAcceptedHtlcs > uint16(input.MaxHTLCNumber/2) {
|
2018-12-11 00:58:34 +03:00
|
|
|
return ErrMaxHtlcNumTooLarge(
|
2019-01-16 17:47:43 +03:00
|
|
|
c.MaxAcceptedHtlcs, uint16(input.MaxHTLCNumber/2),
|
2018-12-11 00:58:34 +03:00
|
|
|
)
|
2017-11-29 16:16:36 +03:00
|
|
|
}
|
|
|
|
|
2018-02-25 06:09:49 +03:00
|
|
|
// Fail if we consider maxHtlcs too small. If this is too small we
|
|
|
|
// cannot offer many HTLCs to the remote.
|
2017-11-29 15:50:44 +03:00
|
|
|
const minNumHtlc = 5
|
2018-12-11 00:58:34 +03:00
|
|
|
if c.MaxAcceptedHtlcs < minNumHtlc {
|
|
|
|
return ErrMaxHtlcNumTooSmall(c.MaxAcceptedHtlcs, minNumHtlc)
|
2017-11-29 16:16:36 +03:00
|
|
|
}
|
|
|
|
|
2018-02-25 06:09:49 +03:00
|
|
|
// Fail if we consider maxValueInFlight too small. We currently require
|
|
|
|
// the remote to at least allow minNumHtlc * minHtlc in flight.
|
2018-12-11 00:58:34 +03:00
|
|
|
if c.MaxPendingAmount < minNumHtlc*c.MinHTLC {
|
|
|
|
return ErrMaxValueInFlightTooSmall(
|
|
|
|
c.MaxPendingAmount, minNumHtlc*c.MinHTLC,
|
|
|
|
)
|
2017-11-29 16:16:36 +03:00
|
|
|
}
|
|
|
|
|
2019-01-30 04:56:28 +03:00
|
|
|
// Our dust limit should always be less than or equal to our proposed
|
2018-05-14 21:04:34 +03:00
|
|
|
// channel reserve.
|
2018-12-11 00:58:34 +03:00
|
|
|
if r.ourContribution.DustLimit > c.ChanReserve {
|
|
|
|
r.ourContribution.DustLimit = c.ChanReserve
|
2018-05-14 21:04:34 +03:00
|
|
|
}
|
|
|
|
|
2018-12-11 00:58:34 +03:00
|
|
|
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
|
2017-09-12 18:41:40 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// OurContribution returns the wallet's fully populated contribution to the
|
|
|
|
// pending payment channel. See 'ChannelContribution' for further details
|
|
|
|
// regarding the contents of a contribution.
|
2017-07-30 04:58:55 +03:00
|
|
|
//
|
2015-12-23 07:30:11 +03:00
|
|
|
// NOTE: This SHOULD NOT be modified.
|
2016-01-02 23:09:31 +03:00
|
|
|
// TODO(roasbeef): make copy?
|
2015-12-23 07:30:11 +03:00
|
|
|
func (r *ChannelReservation) OurContribution() *ChannelContribution {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
2017-07-30 04:58:55 +03:00
|
|
|
|
2015-12-23 07:30:11 +03:00
|
|
|
return r.ourContribution
|
|
|
|
}
|
|
|
|
|
2016-11-24 11:49:18 +03:00
|
|
|
// ProcessContribution verifies the counterparty's contribution to the pending
|
2016-01-02 23:09:31 +03:00
|
|
|
// payment channel. As a result of this incoming message, lnwallet is able to
|
|
|
|
// build the funding transaction, and both commitment transactions. Once this
|
|
|
|
// message has been processed, all signatures to inputs to the funding
|
|
|
|
// transaction belonging to the wallet are available. Additionally, the wallet
|
|
|
|
// will generate a signature to the counterparty's version of the commitment
|
|
|
|
// transaction.
|
2015-12-23 07:30:11 +03:00
|
|
|
func (r *ChannelReservation) ProcessContribution(theirContribution *ChannelContribution) error {
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
|
|
|
r.wallet.msgChan <- &addContributionMsg{
|
|
|
|
pendingFundingID: r.reservationID,
|
|
|
|
contribution: theirContribution,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
|
|
|
return <-errChan
|
|
|
|
}
|
|
|
|
|
2016-06-21 08:31:57 +03:00
|
|
|
// ProcessSingleContribution verifies, and records the initiator's contribution
|
|
|
|
// to this pending single funder channel. Internally, no further action is
|
|
|
|
// taken other than recording the initiator's contribution to the single funder
|
|
|
|
// channel.
|
|
|
|
func (r *ChannelReservation) ProcessSingleContribution(theirContribution *ChannelContribution) error {
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
|
|
|
r.wallet.msgChan <- &addSingleContributionMsg{
|
|
|
|
pendingFundingID: r.reservationID,
|
|
|
|
contribution: theirContribution,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
|
|
|
return <-errChan
|
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// TheirContribution returns the counterparty's pending contribution to the
|
2017-07-30 04:58:55 +03:00
|
|
|
// payment channel. See 'ChannelContribution' for further details regarding the
|
|
|
|
// contents of a contribution. This attribute will ONLY be available after a
|
2018-09-28 06:59:59 +03:00
|
|
|
// call to .ProcessContribution().
|
2017-07-30 04:58:55 +03:00
|
|
|
//
|
2016-01-02 23:09:31 +03:00
|
|
|
// NOTE: This SHOULD NOT be modified.
|
2015-12-23 07:30:11 +03:00
|
|
|
func (r *ChannelReservation) TheirContribution() *ChannelContribution {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
return r.theirContribution
|
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// OurSignatures retrieves the wallet's signatures to all inputs to the funding
|
|
|
|
// transaction belonging to itself, and also a signature for the counterparty's
|
|
|
|
// version of the commitment transaction. The signatures for the wallet's
|
|
|
|
// inputs to the funding transaction are returned in sorted order according to
|
|
|
|
// BIP-69: https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki.
|
2017-07-30 04:58:55 +03:00
|
|
|
//
|
2016-01-02 23:09:31 +03:00
|
|
|
// NOTE: These signatures will only be populated after a call to
|
2018-09-28 06:59:59 +03:00
|
|
|
// .ProcessContribution()
|
2019-01-16 17:47:43 +03:00
|
|
|
func (r *ChannelReservation) OurSignatures() ([]*input.Script, []byte) {
|
2015-12-23 07:30:11 +03:00
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
2016-05-04 05:46:53 +03:00
|
|
|
return r.ourFundingInputScripts, r.ourCommitmentSig
|
2015-12-23 07:30:11 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 04:58:55 +03:00
|
|
|
// CompleteReservation finalizes the pending channel reservation, transitioning
|
|
|
|
// from a pending payment channel, to an open payment channel. All passed
|
|
|
|
// signatures to the counterparty's inputs to the funding transaction will be
|
|
|
|
// fully verified. Signatures are expected to be passed in sorted order
|
|
|
|
// according to BIP-69:
|
|
|
|
// https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki.
|
|
|
|
// Additionally, verification is performed in order to ensure that the
|
|
|
|
// counterparty supplied a valid signature to our version of the commitment
|
2019-05-10 18:14:19 +03:00
|
|
|
// transaction. Once this method returns, callers should broadcast the
|
2018-07-26 16:16:47 +03:00
|
|
|
// created funding transaction, then call .WaitForChannelOpen() which will
|
|
|
|
// block until the funding transaction obtains the configured number of
|
|
|
|
// confirmations. Once the method unblocks, a LightningChannel instance is
|
|
|
|
// returned, marking the channel available for updates.
|
2019-01-16 17:47:43 +03:00
|
|
|
func (r *ChannelReservation) CompleteReservation(fundingInputScripts []*input.Script,
|
2017-01-24 05:19:54 +03:00
|
|
|
commitmentSig []byte) (*channeldb.OpenChannel, error) {
|
2015-12-29 08:58:06 +03:00
|
|
|
|
2016-06-21 08:31:57 +03:00
|
|
|
// TODO(roasbeef): add flag for watch or not?
|
2015-12-23 07:30:11 +03:00
|
|
|
errChan := make(chan error, 1)
|
2017-01-24 05:19:54 +03:00
|
|
|
completeChan := make(chan *channeldb.OpenChannel, 1)
|
2015-12-23 07:30:11 +03:00
|
|
|
|
|
|
|
r.wallet.msgChan <- &addCounterPartySigsMsg{
|
2016-05-04 05:46:53 +03:00
|
|
|
pendingFundingID: r.reservationID,
|
|
|
|
theirFundingInputScripts: fundingInputScripts,
|
|
|
|
theirCommitmentSig: commitmentSig,
|
2017-01-24 05:19:54 +03:00
|
|
|
completeChan: completeChan,
|
2016-05-04 05:46:53 +03:00
|
|
|
err: errChan,
|
2015-12-19 06:42:49 +03:00
|
|
|
}
|
2015-12-23 07:30:11 +03:00
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
return <-completeChan, <-errChan
|
2015-12-23 07:30:11 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 08:31:57 +03:00
|
|
|
// CompleteReservationSingle finalizes the pending single funder channel
|
2017-07-30 04:58:55 +03:00
|
|
|
// reservation. Using the funding outpoint of the constructed funding
|
|
|
|
// transaction, and the initiator's signature for our version of the commitment
|
|
|
|
// transaction, we are able to verify the correctness of our commitment
|
|
|
|
// transaction as crafted by the initiator. Once this method returns, our
|
|
|
|
// signature for the initiator's version of the commitment transaction is
|
|
|
|
// available via the .OurSignatures() method. As this method should only be
|
|
|
|
// called as a response to a single funder channel, only a commitment signature
|
|
|
|
// will be populated.
|
|
|
|
func (r *ChannelReservation) CompleteReservationSingle(fundingPoint *wire.OutPoint,
|
|
|
|
commitSig []byte) (*channeldb.OpenChannel, error) {
|
2016-11-16 23:54:27 +03:00
|
|
|
|
2016-06-21 08:31:57 +03:00
|
|
|
errChan := make(chan error, 1)
|
2017-01-24 05:19:54 +03:00
|
|
|
completeChan := make(chan *channeldb.OpenChannel, 1)
|
2016-06-21 08:31:57 +03:00
|
|
|
|
|
|
|
r.wallet.msgChan <- &addSingleFunderSigsMsg{
|
|
|
|
pendingFundingID: r.reservationID,
|
|
|
|
fundingOutpoint: fundingPoint,
|
|
|
|
theirCommitmentSig: commitSig,
|
2017-01-24 05:19:54 +03:00
|
|
|
completeChan: completeChan,
|
2016-06-21 08:31:57 +03:00
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
return <-completeChan, <-errChan
|
2016-06-21 08:31:57 +03:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// TheirSignatures returns the counterparty's signatures to all inputs to the
|
2016-01-02 23:09:31 +03:00
|
|
|
// funding transaction belonging to them, as well as their signature for the
|
|
|
|
// wallet's version of the commitment transaction. This methods is provided for
|
|
|
|
// additional verification, such as needed by tests.
|
2017-07-30 04:58:55 +03:00
|
|
|
//
|
2016-01-02 23:09:31 +03:00
|
|
|
// NOTE: These attributes will be unpopulated before a call to
|
|
|
|
// .CompleteReservation().
|
2019-01-16 17:47:43 +03:00
|
|
|
func (r *ChannelReservation) TheirSignatures() ([]*input.Script, []byte) {
|
2015-12-23 07:30:11 +03:00
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
2016-05-04 05:46:53 +03:00
|
|
|
return r.theirFundingInputScripts, r.theirCommitmentSig
|
2015-12-23 07:30:11 +03:00
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// FinalFundingTx returns the finalized, fully signed funding transaction for
|
|
|
|
// this reservation.
|
2016-06-21 08:31:57 +03:00
|
|
|
//
|
|
|
|
// NOTE: If this reservation was created as the non-initiator to a single
|
|
|
|
// funding workflow, then the full funding transaction will not be available.
|
|
|
|
// Instead we will only have the final outpoint of the funding transaction.
|
2015-12-23 07:30:11 +03:00
|
|
|
func (r *ChannelReservation) FinalFundingTx() *wire.MsgTx {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
2016-06-21 08:31:57 +03:00
|
|
|
return r.fundingTx
|
|
|
|
}
|
|
|
|
|
|
|
|
// FundingOutpoint returns the outpoint of the funding transaction.
|
|
|
|
//
|
2017-09-25 21:25:58 +03:00
|
|
|
// NOTE: The pointer returned will only be set once the .ProcessContribution()
|
2016-06-21 08:31:57 +03:00
|
|
|
// method is called in the case of the initiator of a single funder workflow,
|
|
|
|
// and after the .CompleteReservationSingle() method is called in the case of
|
|
|
|
// a responder to a single funder workflow.
|
|
|
|
func (r *ChannelReservation) FundingOutpoint() *wire.OutPoint {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
2017-07-30 04:58:55 +03:00
|
|
|
return &r.partialState.FundingOutpoint
|
2016-11-16 23:54:27 +03:00
|
|
|
}
|
|
|
|
|
2019-12-03 12:38:29 +03:00
|
|
|
// SetOurUpfrontShutdown sets the upfront shutdown address on our contribution.
|
|
|
|
func (r *ChannelReservation) SetOurUpfrontShutdown(shutdown lnwire.DeliveryAddress) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
r.ourContribution.UpfrontShutdown = shutdown
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:14:38 +03:00
|
|
|
// Capacity returns the channel capacity for this reservation.
|
|
|
|
func (r *ChannelReservation) Capacity() btcutil.Amount {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
return r.partialState.Capacity
|
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// Cancel abandons this channel reservation. This method should be called in
|
|
|
|
// the scenario that communications with the counterparty break down. Upon
|
|
|
|
// cancellation, all resources previously reserved for this pending payment
|
|
|
|
// channel are returned to the free pool, allowing subsequent reservations to
|
|
|
|
// utilize the now freed resources.
|
2015-12-23 07:30:11 +03:00
|
|
|
func (r *ChannelReservation) Cancel() error {
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
r.wallet.msgChan <- &fundingReserveCancelMsg{
|
|
|
|
pendingFundingID: r.reservationID,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
|
|
|
return <-errChan
|
|
|
|
}
|
|
|
|
|
2017-01-23 02:06:28 +03:00
|
|
|
// OpenChannelDetails wraps the finalized fully confirmed channel which
|
|
|
|
// resulted from a ChannelReservation instance with details concerning exactly
|
|
|
|
// _where_ in the chain the channel was ultimately opened.
|
|
|
|
type OpenChannelDetails struct {
|
|
|
|
// Channel is the active channel created by an instance of a
|
|
|
|
// ChannelReservation and the required funding workflow.
|
|
|
|
Channel *LightningChannel
|
|
|
|
|
2017-07-30 04:58:55 +03:00
|
|
|
// ConfirmationHeight is the block height within the chain that
|
|
|
|
// included the channel.
|
2017-01-23 02:06:28 +03:00
|
|
|
ConfirmationHeight uint32
|
|
|
|
|
|
|
|
// TransactionIndex is the index within the confirming block that the
|
|
|
|
// transaction resides.
|
|
|
|
TransactionIndex uint32
|
|
|
|
}
|