2015-12-20 09:00:50 +03:00
|
|
|
package lnwallet
|
2015-11-13 05:43:32 +03:00
|
|
|
|
|
|
|
import (
|
2015-11-14 22:52:07 +03:00
|
|
|
"sync"
|
|
|
|
|
2016-01-16 21:45:54 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2015-12-26 21:35:15 +03:00
|
|
|
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcutil"
|
2015-11-13 05:43:32 +03:00
|
|
|
)
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// ChannelContribution is the primary constituent of the funding workflow within
|
|
|
|
// lnwallet. Each side first exchanges their respective contributions along with
|
|
|
|
// channel specific paramters 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 {
|
|
|
|
// Amount of funds contributed to the funding transaction.
|
|
|
|
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
|
|
|
|
|
|
|
|
// Outputs to be used in the case that the total value of the fund
|
|
|
|
// ing inputs is greather than the total potential channel capacity.
|
|
|
|
ChangeOutputs []*wire.TxOut
|
|
|
|
|
|
|
|
// The key to be used for the funding transaction's P2SH multi-sig
|
|
|
|
// 2-of-2 output.
|
|
|
|
MultiSigKey *btcec.PublicKey
|
2015-11-14 22:52:07 +03:00
|
|
|
|
2015-12-23 07:30:11 +03:00
|
|
|
// The key to be used for this party's version of the commitment
|
|
|
|
// transaction.
|
|
|
|
CommitKey *btcec.PublicKey
|
2015-12-18 22:29:35 +03:00
|
|
|
|
2015-12-23 07:30:11 +03:00
|
|
|
// Address to be used for delivery of cleared channel funds in the scenario
|
|
|
|
// of a cooperative channel closure.
|
|
|
|
DeliveryAddress btcutil.Address
|
|
|
|
|
|
|
|
// Hash to be used as the revocation for the initial version of this
|
|
|
|
// party's commitment transaction.
|
2015-12-31 09:30:52 +03:00
|
|
|
RevocationHash [20]byte
|
2015-12-23 07:30:11 +03:00
|
|
|
|
|
|
|
// The delay (in blocks) to be used for the pay-to-self output in this
|
|
|
|
// party's version of the commitment transaction.
|
2015-12-26 09:07:30 +03:00
|
|
|
CsvDelay uint32
|
2015-12-23 07:30:11 +03:00
|
|
|
}
|
|
|
|
|
2016-05-04 05:46:53 +03:00
|
|
|
// InputScripts represents any script inputs required to redeem a previous
|
|
|
|
// output. This struct is used rather than just a witness, or scripSig in
|
|
|
|
// order to accomdate nested p2sh which utilizes both types of input scripts.
|
|
|
|
type InputScript struct {
|
|
|
|
Witness [][]byte
|
|
|
|
ScriptSig []byte
|
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// ChannelReservation represents an intent to open a lightning payment channel
|
|
|
|
// a counterpaty. The funding proceses from reservation to channel opening is a
|
|
|
|
// 3-step process. In order to allow for full concurrency during the 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 cancelled, which
|
|
|
|
// removes the resources from limbo, allowing another reservation to claim them.
|
|
|
|
//
|
|
|
|
// The reservation workflow consists of the following three steps:
|
|
|
|
// 1. lnwallet.InitChannelReservation
|
|
|
|
// * One requests the wallet to allocate the neccessary resources for a
|
|
|
|
// channel reservation. These resources a 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 neccessary items to allow the remote party to build both
|
|
|
|
// the funding, and commitment transactions.
|
|
|
|
// 2. ChannelReservation.ProcessContribution
|
|
|
|
// * 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().
|
|
|
|
// 3. ChannelReservation.CompleteReservation
|
|
|
|
// * The final step in the workflow. The counterparty presents the
|
|
|
|
// signatures for all their inputs to the funding transation, as well
|
|
|
|
// 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
|
|
|
|
2015-12-23 07:30:11 +03:00
|
|
|
// For CLTV it is nLockTime, for CSV it's nSequence, for segwit it's
|
|
|
|
// not needed
|
2015-12-26 09:07:30 +03:00
|
|
|
fundingLockTime uint32
|
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.
|
2016-05-04 05:46:53 +03:00
|
|
|
ourFundingInputScripts []*InputScript
|
|
|
|
theirFundingInputScripts []*InputScript
|
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
|
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
|
|
|
|
2015-12-23 07:30:11 +03:00
|
|
|
// A channel which will be sent on once the channel is considered
|
|
|
|
// 'open'. A channel is open once the funding transaction has reached
|
|
|
|
// a sufficient number of confirmations.
|
2015-12-16 00:22:18 +03:00
|
|
|
chanOpen chan *LightningChannel
|
2015-12-23 07:30:11 +03:00
|
|
|
|
|
|
|
wallet *LightningWallet
|
2015-11-13 05:43:32 +03:00
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// newChannelReservation creates a new channel reservation. This function is
|
|
|
|
// used only internally by lnwallet. In order to concurrent safety, the creation
|
|
|
|
// of all channel reservations should be carried out via the
|
|
|
|
// lnwallet.InitChannelReservation interface.
|
2016-05-04 05:44:02 +03:00
|
|
|
func newChannelReservation(fundingAmt btcutil.Amount, minFeeRate btcutil.Amount,
|
|
|
|
wallet *LightningWallet, id uint64) *ChannelReservation {
|
2015-12-23 07:30:11 +03:00
|
|
|
// TODO(roasbeef): CSV here, or on delay?
|
2015-12-19 06:42:49 +03:00
|
|
|
return &ChannelReservation{
|
2015-12-23 07:30:11 +03:00
|
|
|
ourContribution: &ChannelContribution{
|
|
|
|
FundingAmount: fundingAmt,
|
|
|
|
},
|
|
|
|
theirContribution: &ChannelContribution{
|
|
|
|
FundingAmount: fundingAmt,
|
|
|
|
},
|
2015-12-26 21:35:15 +03:00
|
|
|
partialState: &channeldb.OpenChannel{
|
2015-12-23 07:30:11 +03:00
|
|
|
// TODO(roasbeef): assumes balanced symmetric channels.
|
2015-12-26 09:07:30 +03:00
|
|
|
Capacity: fundingAmt * 2,
|
|
|
|
OurBalance: fundingAmt,
|
|
|
|
TheirBalance: fundingAmt,
|
|
|
|
MinFeePerKb: minFeeRate,
|
2016-03-24 10:01:17 +03:00
|
|
|
Db: wallet.channelDB,
|
2015-12-19 06:42:49 +03:00
|
|
|
},
|
|
|
|
reservationID: id,
|
2015-12-23 07:30:11 +03:00
|
|
|
wallet: wallet,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
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()
|
|
|
|
return r.ourContribution
|
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// ProcesContribution verifies the counterparty's contribution to the pending
|
|
|
|
// 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-01-02 23:09:31 +03:00
|
|
|
// TheirContribution returns the counterparty's pending contribution to the
|
|
|
|
// payment channel. See 'ChannelContribution' for further details regarding
|
|
|
|
// the contents of a contribution. This attribute will ONLY be available
|
|
|
|
// after a call to .ProcesContribution().
|
|
|
|
// 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.
|
|
|
|
// NOTE: These signatures will only be populated after a call to
|
|
|
|
// .ProcesContribution()
|
2016-05-04 05:46:53 +03:00
|
|
|
func (r *ChannelReservation) OurSignatures() ([]*InputScript, []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
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// CompleteFundingReservation 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 transaction.
|
|
|
|
// Once this method returns, caller's should 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.
|
2016-05-04 05:46:53 +03:00
|
|
|
func (r *ChannelReservation) CompleteReservation(fundingInputScripts []*InputScript,
|
2015-12-29 08:58:06 +03:00
|
|
|
commitmentSig []byte) error {
|
|
|
|
|
2015-12-23 07:30:11 +03:00
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
|
|
|
r.wallet.msgChan <- &addCounterPartySigsMsg{
|
2016-05-04 05:46:53 +03:00
|
|
|
pendingFundingID: r.reservationID,
|
|
|
|
theirFundingInputScripts: fundingInputScripts,
|
|
|
|
theirCommitmentSig: commitmentSig,
|
|
|
|
err: errChan,
|
2015-12-19 06:42:49 +03:00
|
|
|
}
|
2015-12-23 07:30:11 +03:00
|
|
|
|
|
|
|
return <-errChan
|
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// OurSignatures returns the counterparty's signatures to all inputs to the
|
|
|
|
// 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.
|
|
|
|
// NOTE: These attributes will be unpopulated before a call to
|
|
|
|
// .CompleteReservation().
|
2016-05-04 05:46:53 +03:00
|
|
|
func (r *ChannelReservation) TheirSignatures() ([]*InputScript, []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.
|
2015-12-23 07:30:11 +03:00
|
|
|
func (r *ChannelReservation) FinalFundingTx() *wire.MsgTx {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
2015-12-24 21:41:15 +03:00
|
|
|
return r.partialState.FundingTx
|
2015-12-23 07:30:11 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-02 23:09:31 +03:00
|
|
|
// WaitForChannelOpen blocks until the funding transaction for this pending
|
|
|
|
// payment channel obtains the configured number of confirmations. Once
|
|
|
|
// confirmations have been obtained, a fully initialized LightningChannel
|
|
|
|
// instance is returned, allowing for channel updates.
|
|
|
|
// NOTE: If this method is called before .CompleteReservation(), it will block
|
|
|
|
// indefinitely.
|
2015-12-23 07:30:11 +03:00
|
|
|
func (r *ChannelReservation) WaitForChannelOpen() *LightningChannel {
|
2016-01-07 03:17:18 +03:00
|
|
|
return <-r.chanOpen
|
2015-12-19 06:42:49 +03:00
|
|
|
}
|
|
|
|
|
2015-12-16 00:22:33 +03:00
|
|
|
// * finish reset of tests
|
|
|
|
// * start on commitment side
|
|
|
|
// * channel should have active namespace to it's bucket, query at that point fo past commits etc
|