2019-01-24 16:28:25 +03:00
|
|
|
package lnd
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
|
|
import (
|
2016-12-27 08:42:23 +03:00
|
|
|
"bytes"
|
2017-04-17 01:34:23 +03:00
|
|
|
"encoding/binary"
|
2017-06-08 20:48:07 +03:00
|
|
|
"fmt"
|
2016-06-21 21:31:10 +03:00
|
|
|
"sync"
|
2016-12-27 08:42:23 +03:00
|
|
|
"time"
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2018-07-06 04:33:28 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2019-12-03 12:38:29 +03:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2018-07-06 04:33:28 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2018-03-11 06:00:57 +03:00
|
|
|
"github.com/coreos/bbolt"
|
2017-01-25 04:12:51 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2016-11-24 11:49:18 +03:00
|
|
|
"github.com/go-errors/errors"
|
2017-01-24 05:19:54 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2019-08-08 05:18:23 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chanacceptor"
|
2017-01-24 05:19:54 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-04-17 23:26:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/discovery"
|
2017-06-17 01:15:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2019-01-16 17:47:43 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2018-02-18 02:29:01 +03:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2018-07-12 03:37:54 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnpeer"
|
2016-08-31 02:52:53 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2016-06-21 21:31:10 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2019-10-31 05:43:05 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2016-06-21 21:31:10 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2017-11-14 04:20:05 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
2018-10-18 01:59:02 +03:00
|
|
|
"golang.org/x/crypto/salsa20"
|
2016-06-21 21:31:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TODO(roasbeef): tune
|
|
|
|
msgBufferSize = 50
|
2017-07-31 01:53:53 +03:00
|
|
|
|
2018-03-15 00:54:04 +03:00
|
|
|
// minBtcRemoteDelay and maxBtcRemoteDelay is the extremes of the
|
|
|
|
// Bitcoin CSV delay we will require the remote to use for its
|
|
|
|
// commitment transaction. The actual delay we will require will be
|
|
|
|
// somewhere between these values, depending on channel size.
|
|
|
|
minBtcRemoteDelay uint16 = 144
|
|
|
|
maxBtcRemoteDelay uint16 = 2016
|
|
|
|
|
|
|
|
// minLtcRemoteDelay and maxLtcRemoteDelay is the extremes of the
|
|
|
|
// Litecoin CSV delay we will require the remote to use for its
|
|
|
|
// commitment transaction. The actual delay we will require will be
|
|
|
|
// somewhere between these values, depending on channel size.
|
|
|
|
minLtcRemoteDelay uint16 = 576
|
|
|
|
maxLtcRemoteDelay uint16 = 8064
|
2017-12-17 18:08:21 +03:00
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
// maxWaitNumBlocksFundingConf is the maximum number of blocks to wait
|
2019-05-19 01:11:17 +03:00
|
|
|
// for the funding transaction to be confirmed before forgetting
|
|
|
|
// channels that aren't initiated by us. 2016 blocks is ~2 weeks.
|
|
|
|
maxWaitNumBlocksFundingConf = 2016
|
2018-03-19 02:58:39 +03:00
|
|
|
|
|
|
|
// minChanFundingSize is the smallest channel that we'll allow to be
|
|
|
|
// created over the RPC interface.
|
|
|
|
minChanFundingSize = btcutil.Amount(20000)
|
2018-05-23 22:05:04 +03:00
|
|
|
|
2019-05-15 10:02:53 +03:00
|
|
|
// MaxBtcFundingAmount is a soft-limit of the maximum channel size
|
2018-05-23 22:05:04 +03:00
|
|
|
// currently accepted on the Bitcoin chain within the Lightning
|
|
|
|
// Protocol. This limit is defined in BOLT-0002, and serves as an
|
|
|
|
// initial precautionary limit while implementations are battle tested
|
|
|
|
// in the real world.
|
2019-05-15 10:02:53 +03:00
|
|
|
MaxBtcFundingAmount = btcutil.Amount(1<<24) - 1
|
2018-05-23 22:05:04 +03:00
|
|
|
|
|
|
|
// maxLtcFundingAmount is a soft-limit of the maximum channel size
|
|
|
|
// currently accepted on the Litecoin chain within the Lightning
|
|
|
|
// Protocol.
|
2019-05-15 10:02:53 +03:00
|
|
|
maxLtcFundingAmount = MaxBtcFundingAmount * btcToLtcConversionRate
|
2018-05-23 22:05:04 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-05-15 10:02:53 +03:00
|
|
|
// MaxFundingAmount is a soft-limit of the maximum channel size
|
2018-05-23 22:05:04 +03:00
|
|
|
// currently accepted within the Lightning Protocol. This limit is
|
|
|
|
// defined in BOLT-0002, and serves as an initial precautionary limit
|
|
|
|
// while implementations are battle tested in the real world.
|
|
|
|
//
|
|
|
|
// At the moment, this value depends on which chain is active. It is set
|
|
|
|
// to the value under the Bitcoin chain as default.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): add command line param to modify
|
2019-05-15 10:02:53 +03:00
|
|
|
MaxFundingAmount = MaxBtcFundingAmount
|
2018-07-06 04:33:28 +03:00
|
|
|
|
|
|
|
// ErrFundingManagerShuttingDown is an error returned when attempting to
|
|
|
|
// process a funding request/message but the funding manager has already
|
|
|
|
// been signaled to shut down.
|
|
|
|
ErrFundingManagerShuttingDown = errors.New("funding manager shutting " +
|
|
|
|
"down")
|
2018-09-16 11:40:10 +03:00
|
|
|
|
|
|
|
// ErrConfirmationTimeout is an error returned when we as a responder
|
|
|
|
// are waiting for a funding transaction to confirm, but too many
|
|
|
|
// blocks pass without confirmation.
|
|
|
|
ErrConfirmationTimeout = errors.New("timeout waiting for funding " +
|
|
|
|
"confirmation")
|
2019-12-03 12:38:29 +03:00
|
|
|
|
|
|
|
// errUpfrontShutdownScriptNotSupported is returned if an upfront shutdown
|
|
|
|
// script is set for a peer that does not support the feature bit.
|
|
|
|
errUpfrontShutdownScriptNotSupported = errors.New("peer does not support" +
|
|
|
|
"option upfront shutdown script")
|
2016-06-21 21:31:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// reservationWithCtx encapsulates a pending channel reservation. This wrapper
|
|
|
|
// struct is used internally within the funding manager to track and progress
|
2016-10-15 16:18:38 +03:00
|
|
|
// the funding workflow initiated by incoming/outgoing methods from the target
|
2016-06-21 21:31:10 +03:00
|
|
|
// peer. Additionally, this struct houses a response and error channel which is
|
|
|
|
// used to respond to the caller in the case a channel workflow is initiated
|
|
|
|
// via a local signal such as RPC.
|
2017-07-31 01:53:53 +03:00
|
|
|
//
|
2016-06-21 21:31:10 +03:00
|
|
|
// TODO(roasbeef): actually use the context package
|
|
|
|
// * deadlines, etc.
|
|
|
|
type reservationWithCtx struct {
|
|
|
|
reservation *lnwallet.ChannelReservation
|
2018-07-05 23:41:51 +03:00
|
|
|
peer lnpeer.Peer
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2018-04-05 20:48:41 +03:00
|
|
|
chanAmt btcutil.Amount
|
|
|
|
|
|
|
|
// Constraints we require for the remote.
|
2018-04-05 20:14:26 +03:00
|
|
|
remoteCsvDelay uint16
|
2018-04-05 20:48:41 +03:00
|
|
|
remoteMinHtlc lnwire.MilliSatoshi
|
2017-07-31 01:53:53 +03:00
|
|
|
|
2018-03-14 04:12:33 +03:00
|
|
|
updateMtx sync.RWMutex
|
2018-03-13 04:58:51 +03:00
|
|
|
lastUpdated time.Time
|
|
|
|
|
2016-08-31 02:52:53 +03:00
|
|
|
updates chan *lnrpc.OpenStatusUpdate
|
|
|
|
err chan error
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
// isLocked checks the reservation's timestamp to determine whether it is locked.
|
|
|
|
func (r *reservationWithCtx) isLocked() bool {
|
2018-03-14 04:12:33 +03:00
|
|
|
r.updateMtx.RLock()
|
|
|
|
defer r.updateMtx.RUnlock()
|
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
// The time zero value represents a locked reservation.
|
|
|
|
return r.lastUpdated.IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateTimestamp updates the reservation's timestamp with the current time.
|
|
|
|
func (r *reservationWithCtx) updateTimestamp() {
|
2018-03-14 04:12:33 +03:00
|
|
|
r.updateMtx.Lock()
|
|
|
|
defer r.updateMtx.Unlock()
|
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
r.lastUpdated = time.Now()
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// initFundingMsg is sent by an outside subsystem to the funding manager in
|
|
|
|
// order to kick off a funding workflow with a specified target peer. The
|
2016-06-21 21:31:10 +03:00
|
|
|
// original request which defines the parameters of the funding workflow are
|
|
|
|
// embedded within this message giving the funding manager full context w.r.t
|
|
|
|
// the workflow.
|
|
|
|
type initFundingMsg struct {
|
2018-07-05 23:41:51 +03:00
|
|
|
peer lnpeer.Peer
|
2016-06-21 21:31:10 +03:00
|
|
|
*openChanReq
|
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// fundingOpenMsg couples an lnwire.OpenChannel message with the peer who sent
|
|
|
|
// the message. This allows the funding manager to queue a response directly to
|
|
|
|
// the peer, progressing the funding workflow.
|
|
|
|
type fundingOpenMsg struct {
|
2018-07-05 23:41:51 +03:00
|
|
|
msg *lnwire.OpenChannel
|
|
|
|
peer lnpeer.Peer
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// fundingAcceptMsg couples an lnwire.AcceptChannel message with the peer who
|
|
|
|
// sent the message. This allows the funding manager to queue a response
|
|
|
|
// directly to the peer, progressing the funding workflow.
|
|
|
|
type fundingAcceptMsg struct {
|
2018-07-05 23:41:51 +03:00
|
|
|
msg *lnwire.AcceptChannel
|
|
|
|
peer lnpeer.Peer
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// fundingCreatedMsg couples an lnwire.FundingCreated message with the peer who
|
|
|
|
// sent the message. This allows the funding manager to queue a response
|
|
|
|
// directly to the peer, progressing the funding workflow.
|
|
|
|
type fundingCreatedMsg struct {
|
2018-07-05 23:41:51 +03:00
|
|
|
msg *lnwire.FundingCreated
|
|
|
|
peer lnpeer.Peer
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// fundingSignedMsg couples an lnwire.FundingSigned message with the peer who
|
|
|
|
// sent the message. This allows the funding manager to queue a response
|
|
|
|
// directly to the peer, progressing the funding workflow.
|
|
|
|
type fundingSignedMsg struct {
|
2018-07-05 23:41:51 +03:00
|
|
|
msg *lnwire.FundingSigned
|
|
|
|
peer lnpeer.Peer
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
// fundingLockedMsg couples an lnwire.FundingLocked message with the peer who
|
|
|
|
// sent the message. This allows the funding manager to finalize the funding
|
|
|
|
// process and announce the existence of the new channel.
|
|
|
|
type fundingLockedMsg struct {
|
2018-07-05 23:41:51 +03:00
|
|
|
msg *lnwire.FundingLocked
|
|
|
|
peer lnpeer.Peer
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
// fundingErrorMsg couples an lnwire.Error message with the peer who sent the
|
|
|
|
// message. This allows the funding manager to properly process the error.
|
2016-10-15 16:24:56 +03:00
|
|
|
type fundingErrorMsg struct {
|
2018-07-05 23:38:11 +03:00
|
|
|
err *lnwire.Error
|
|
|
|
peerKey *btcec.PublicKey
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// pendingChannels is a map instantiated per-peer which tracks all active
|
2017-07-31 01:53:53 +03:00
|
|
|
// pending single funded channels indexed by their pending channel identifier,
|
|
|
|
// which is a set of 32-bytes generated via a CSPRNG.
|
2017-04-17 01:34:23 +03:00
|
|
|
type pendingChannels map[[32]byte]*reservationWithCtx
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
// serializedPubKey is used within the FundingManager's activeReservations list
|
|
|
|
// to identify the nodes with which the FundingManager is actively working to
|
|
|
|
// initiate new channels.
|
|
|
|
type serializedPubKey [33]byte
|
|
|
|
|
2017-02-22 09:14:22 +03:00
|
|
|
// newSerializedKey creates a new serialized public key from an instance of a
|
|
|
|
// live pubkey object.
|
|
|
|
func newSerializedKey(pubKey *btcec.PublicKey) serializedPubKey {
|
|
|
|
var s serializedPubKey
|
|
|
|
copy(s[:], pubKey.SerializeCompressed())
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// fundingConfig defines the configuration for the FundingManager. All elements
|
2017-01-13 06:40:38 +03:00
|
|
|
// within the configuration MUST be non-nil for the FundingManager to carry out
|
|
|
|
// its duties.
|
2017-02-22 09:14:22 +03:00
|
|
|
type fundingConfig struct {
|
2017-01-24 05:19:54 +03:00
|
|
|
// IDKey is the PublicKey that is used to identify this node within the
|
|
|
|
// Lightning Network.
|
|
|
|
IDKey *btcec.PublicKey
|
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
// Wallet handles the parts of the funding process that involves moving
|
|
|
|
// funds from on-chain transaction outputs into Lightning channels.
|
|
|
|
Wallet *lnwallet.LightningWallet
|
|
|
|
|
2018-03-11 04:28:00 +03:00
|
|
|
// PublishTransaction facilitates the process of broadcasting a
|
|
|
|
// transaction to the network.
|
|
|
|
PublishTransaction func(*wire.MsgTx) error
|
|
|
|
|
2017-05-01 10:27:12 +03:00
|
|
|
// FeeEstimator calculates appropriate fee rates based on historical
|
|
|
|
// transaction information.
|
2019-10-31 05:43:05 +03:00
|
|
|
FeeEstimator chainfee.Estimator
|
2017-05-01 10:27:12 +03:00
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
// Notifier is used by the FundingManager to determine when the
|
|
|
|
// channel's funding transaction has been confirmed on the blockchain
|
|
|
|
// so that the channel creation process can be completed.
|
|
|
|
Notifier chainntnfs.ChainNotifier
|
|
|
|
|
2019-05-10 18:14:19 +03:00
|
|
|
// SignMessage signs an arbitrary message with a given public key. The
|
2017-04-14 21:17:41 +03:00
|
|
|
// actual digest signed is the double sha-256 of the message. In the
|
|
|
|
// case that the private key corresponding to the passed public key
|
|
|
|
// cannot be located, then an error is returned.
|
2017-07-31 01:53:53 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): should instead pass on this responsibility to a
|
|
|
|
// distinct sub-system?
|
2017-04-14 21:17:41 +03:00
|
|
|
SignMessage func(pubKey *btcec.PublicKey, msg []byte) (*btcec.Signature, error)
|
2017-03-28 22:08:14 +03:00
|
|
|
|
2017-08-05 04:32:25 +03:00
|
|
|
// CurrentNodeAnnouncement should return the latest, fully signed node
|
2018-02-07 06:11:11 +03:00
|
|
|
// announcement from the backing Lightning Network node.
|
2017-08-08 15:48:12 +03:00
|
|
|
CurrentNodeAnnouncement func() (lnwire.NodeAnnouncement, error)
|
2017-07-14 22:47:52 +03:00
|
|
|
|
2019-04-17 23:26:11 +03:00
|
|
|
// SendAnnouncement is used by the FundingManager to send announcement
|
|
|
|
// messages to the Gossiper to possibly broadcast to the greater
|
|
|
|
// network. A set of optional message fields can be provided to populate
|
|
|
|
// any information within the graph that is not included in the gossip
|
|
|
|
// message.
|
|
|
|
SendAnnouncement func(msg lnwire.Message,
|
|
|
|
optionalFields ...discovery.OptionalMsgField) chan error
|
2017-01-24 05:19:54 +03:00
|
|
|
|
2017-09-25 01:01:05 +03:00
|
|
|
// NotifyWhenOnline allows the FundingManager to register with a
|
2018-07-05 23:41:51 +03:00
|
|
|
// subsystem that will notify it when the peer comes online. This is
|
|
|
|
// used when sending the fundingLocked message, since it MUST be
|
2017-09-25 01:01:05 +03:00
|
|
|
// delivered after the funding transaction is confirmed.
|
2018-07-05 23:41:51 +03:00
|
|
|
//
|
|
|
|
// NOTE: The peerChan channel must be buffered.
|
2019-05-31 00:37:30 +03:00
|
|
|
NotifyWhenOnline func(peer [33]byte, peerChan chan<- lnpeer.Peer)
|
2017-09-25 01:01:05 +03:00
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
// FindChannel queries the database for the channel with the given
|
2017-04-17 01:34:23 +03:00
|
|
|
// channel ID.
|
2018-09-26 12:12:57 +03:00
|
|
|
FindChannel func(chanID lnwire.ChannelID) (*channeldb.OpenChannel, error)
|
2017-04-17 01:34:23 +03:00
|
|
|
|
|
|
|
// TempChanIDSeed is a cryptographically random string of bytes that's
|
|
|
|
// used as a seed to generate pending channel ID's.
|
|
|
|
TempChanIDSeed [32]byte
|
2017-06-17 01:15:26 +03:00
|
|
|
|
|
|
|
// DefaultRoutingPolicy is the default routing policy used when
|
|
|
|
// initially announcing channels.
|
|
|
|
DefaultRoutingPolicy htlcswitch.ForwardingPolicy
|
2017-07-31 01:53:53 +03:00
|
|
|
|
|
|
|
// NumRequiredConfs is a function closure that helps the funding
|
|
|
|
// manager decide how many confirmations it should require for a
|
|
|
|
// channel extended to it. The function is able to take into account
|
|
|
|
// the amount of the channel, and any funds we'll be pushed in the
|
|
|
|
// process to determine how many confirmations we'll require.
|
2017-08-22 09:25:41 +03:00
|
|
|
NumRequiredConfs func(btcutil.Amount, lnwire.MilliSatoshi) uint16
|
2017-07-31 01:53:53 +03:00
|
|
|
|
|
|
|
// RequiredRemoteDelay is a function that maps the total amount in a
|
|
|
|
// proposed channel to the CSV delay that we'll require for the remote
|
|
|
|
// party. Naturally a larger channel should require a higher CSV delay
|
|
|
|
// in order to give us more time to claim funds in the case of a
|
|
|
|
// contract breach.
|
|
|
|
RequiredRemoteDelay func(btcutil.Amount) uint16
|
2018-01-17 07:05:07 +03:00
|
|
|
|
2017-11-29 15:59:27 +03:00
|
|
|
// RequiredRemoteChanReserve is a function closure that, given the
|
2018-05-14 21:03:58 +03:00
|
|
|
// channel capacity and dust limit, will return an appropriate amount
|
|
|
|
// for the remote peer's required channel reserve that is to be adhered
|
|
|
|
// to at all times.
|
|
|
|
RequiredRemoteChanReserve func(capacity, dustLimit btcutil.Amount) btcutil.Amount
|
2017-11-29 15:59:27 +03:00
|
|
|
|
2018-04-03 02:17:50 +03:00
|
|
|
// RequiredRemoteMaxValue is a function closure that, given the channel
|
|
|
|
// capacity, returns the amount of MilliSatoshis that our remote peer
|
|
|
|
// can have in total outstanding HTLCs with us.
|
2017-11-29 15:59:27 +03:00
|
|
|
RequiredRemoteMaxValue func(btcutil.Amount) lnwire.MilliSatoshi
|
|
|
|
|
2018-04-03 02:17:50 +03:00
|
|
|
// RequiredRemoteMaxHTLCs is a function closure that, given the channel
|
|
|
|
// capacity, returns the number of maximum HTLCs the remote peer can
|
|
|
|
// offer us.
|
2017-11-29 15:59:27 +03:00
|
|
|
RequiredRemoteMaxHTLCs func(btcutil.Amount) uint16
|
|
|
|
|
2018-01-19 01:07:56 +03:00
|
|
|
// WatchNewChannel is to be called once a new channel enters the final
|
2018-01-17 07:05:07 +03:00
|
|
|
// funding stage: waiting for on-chain confirmation. This method sends
|
|
|
|
// the channel to the ChainArbitrator so it can watch for any on-chain
|
2018-07-05 23:28:57 +03:00
|
|
|
// events related to the channel. We also provide the public key of the
|
2018-03-31 04:42:27 +03:00
|
|
|
// node we're establishing a channel with for reconnection purposes.
|
2018-07-05 23:28:57 +03:00
|
|
|
WatchNewChannel func(*channeldb.OpenChannel, *btcec.PublicKey) error
|
2018-02-04 05:14:09 +03:00
|
|
|
|
|
|
|
// ReportShortChanID allows the funding manager to report the newly
|
|
|
|
// discovered short channel ID of a formerly pending channel to outside
|
|
|
|
// sub-systems.
|
2018-05-08 07:19:45 +03:00
|
|
|
ReportShortChanID func(wire.OutPoint) error
|
2018-03-13 04:58:51 +03:00
|
|
|
|
2018-04-03 02:17:50 +03:00
|
|
|
// ZombieSweeperInterval is the periodic time interval in which the
|
|
|
|
// zombie sweeper is run.
|
2018-03-13 04:58:51 +03:00
|
|
|
ZombieSweeperInterval time.Duration
|
|
|
|
|
2018-04-03 02:17:50 +03:00
|
|
|
// ReservationTimeout is the length of idle time that must pass before
|
|
|
|
// a reservation is considered a zombie.
|
2018-03-13 04:58:51 +03:00
|
|
|
ReservationTimeout time.Duration
|
2018-04-03 02:17:50 +03:00
|
|
|
|
|
|
|
// MinChanSize is the smallest channel size that we'll accept as an
|
|
|
|
// inbound channel. We have such a parameter, as otherwise, nodes could
|
|
|
|
// flood us with very small channels that would never really be usable
|
|
|
|
// due to fees.
|
|
|
|
MinChanSize btcutil.Amount
|
2019-01-17 01:35:30 +03:00
|
|
|
|
2019-07-11 14:14:35 +03:00
|
|
|
// MaxPendingChannels is the maximum number of pending channels we
|
|
|
|
// allow for each peer.
|
|
|
|
MaxPendingChannels int
|
|
|
|
|
|
|
|
// RejectPush is set true if the fundingmanager should reject any
|
|
|
|
// incoming channels having a non-zero push amount.
|
|
|
|
RejectPush bool
|
|
|
|
|
2019-01-17 01:35:30 +03:00
|
|
|
// NotifyOpenChannelEvent informs the ChannelNotifier when channels
|
|
|
|
// transition from pending open to open.
|
|
|
|
NotifyOpenChannelEvent func(wire.OutPoint)
|
2019-08-08 05:18:23 +03:00
|
|
|
|
|
|
|
// OpenChannelPredicate is a predicate on the lnwire.OpenChannel message
|
|
|
|
// and on the requesting node's public key that returns a bool which tells
|
|
|
|
// the funding manager whether or not to accept the channel.
|
|
|
|
OpenChannelPredicate chanacceptor.ChannelAcceptor
|
2017-01-13 06:40:38 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// fundingManager acts as an orchestrator/bridge between the wallet's
|
2016-11-16 23:54:27 +03:00
|
|
|
// 'ChannelReservation' workflow, and the wire protocol's funding initiation
|
|
|
|
// messages. Any requests to initiate the funding workflow for a channel,
|
2019-05-10 18:14:19 +03:00
|
|
|
// either kicked-off locally or remotely are handled by the funding manager.
|
2016-11-16 23:54:27 +03:00
|
|
|
// Once a channel's funding workflow has been completed, any local callers, the
|
|
|
|
// local peer, and possibly the remote peer are notified of the completion of
|
|
|
|
// the channel workflow. Additionally, any temporary or permanent access
|
|
|
|
// controls between the wallet and remote peers are enforced via the funding
|
|
|
|
// manager.
|
2016-06-21 21:31:10 +03:00
|
|
|
type fundingManager struct {
|
2019-01-23 20:15:57 +03:00
|
|
|
started sync.Once
|
|
|
|
stopped sync.Once
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2018-04-03 02:17:50 +03:00
|
|
|
// cfg is a copy of the configuration struct that the FundingManager
|
|
|
|
// was initialized with.
|
2017-02-22 09:14:22 +03:00
|
|
|
cfg *fundingConfig
|
2017-01-13 06:40:38 +03:00
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
// chanIDKey is a cryptographically random key that's used to generate
|
|
|
|
// temporary channel ID's.
|
|
|
|
chanIDKey [32]byte
|
|
|
|
|
|
|
|
// chanIDNonce is a nonce that's incremented for each new funding
|
|
|
|
// reservation created.
|
|
|
|
nonceMtx sync.RWMutex
|
|
|
|
chanIDNonce uint64
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// activeReservations is a map which houses the state of all pending
|
2016-06-21 21:31:10 +03:00
|
|
|
// funding workflows.
|
2017-01-13 06:40:38 +03:00
|
|
|
activeReservations map[serializedPubKey]pendingChannels
|
2016-11-29 06:43:57 +03:00
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// signedReservations is a utility map that maps the permanent channel
|
|
|
|
// ID of a funding reservation to its temporary channel ID. This is
|
|
|
|
// required as mid funding flow, we switch to referencing the channel
|
|
|
|
// by its full channel ID once the commitment transactions have been
|
|
|
|
// signed by both parties.
|
|
|
|
signedReservations map[lnwire.ChannelID][32]byte
|
|
|
|
|
|
|
|
// resMtx guards both of the maps above to ensure that all access is
|
2018-03-13 04:58:51 +03:00
|
|
|
// goroutine safe.
|
2017-07-31 01:53:53 +03:00
|
|
|
resMtx sync.RWMutex
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// fundingMsgs is a channel which receives wrapped wire messages
|
|
|
|
// related to funding workflow from outside peers.
|
|
|
|
fundingMsgs chan interface{}
|
|
|
|
|
2016-07-06 04:57:08 +03:00
|
|
|
// queries is a channel which receives requests to query the internal
|
|
|
|
// state of the funding manager.
|
|
|
|
queries chan interface{}
|
|
|
|
|
2016-10-15 16:18:38 +03:00
|
|
|
// fundingRequests is a channel used to receive channel initiation
|
2017-01-13 08:01:50 +03:00
|
|
|
// requests from a local subsystem within the daemon.
|
2016-06-21 21:31:10 +03:00
|
|
|
fundingRequests chan *initFundingMsg
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
// newChanBarriers is a map from a channel ID to a 'barrier' which will
|
|
|
|
// be signalled once the channel is fully open. This barrier acts as a
|
|
|
|
// synchronization point for any incoming/outgoing HTLCs before the
|
|
|
|
// channel has been fully opened.
|
2017-01-24 02:33:46 +03:00
|
|
|
barrierMtx sync.RWMutex
|
2017-04-17 01:34:23 +03:00
|
|
|
newChanBarriers map[lnwire.ChannelID]chan struct{}
|
2017-01-24 02:33:46 +03:00
|
|
|
|
2017-06-17 00:19:39 +03:00
|
|
|
localDiscoveryMtx sync.Mutex
|
|
|
|
localDiscoverySignals map[lnwire.ChannelID]chan struct{}
|
|
|
|
|
2017-09-01 12:42:40 +03:00
|
|
|
handleFundingLockedMtx sync.RWMutex
|
|
|
|
handleFundingLockedBarriers map[lnwire.ChannelID]struct{}
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
// channelOpeningState represents the different states a channel can be in
|
|
|
|
// between the funding transaction has been confirmed and the channel is
|
|
|
|
// announced to the network and ready to be used.
|
|
|
|
type channelOpeningState uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// markedOpen is the opening state of a channel if the funding
|
|
|
|
// transaction is confirmed on-chain, but fundingLocked is not yet
|
|
|
|
// successfully sent to the other peer.
|
|
|
|
markedOpen channelOpeningState = iota
|
|
|
|
|
|
|
|
// fundingLockedSent is the opening state of a channel if the
|
|
|
|
// fundingLocked message has successfully been sent to the other peer,
|
|
|
|
// but we still haven't announced the channel to the network.
|
|
|
|
fundingLockedSent
|
2017-11-14 02:55:50 +03:00
|
|
|
|
|
|
|
// addedToRouterGraph is the opening state of a channel if the
|
|
|
|
// channel has been successfully added to the router graph
|
|
|
|
// immediately after the fundingLocked message has been sent, but
|
|
|
|
// we still haven't announced the channel to the network.
|
|
|
|
addedToRouterGraph
|
2017-06-08 20:48:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// channelOpeningStateBucket is the database bucket used to store the
|
|
|
|
// channelOpeningState for each channel that is currently in the process
|
|
|
|
// of being opened.
|
|
|
|
channelOpeningStateBucket = []byte("channelOpeningState")
|
|
|
|
|
2018-05-23 10:09:19 +03:00
|
|
|
// ErrChannelNotFound is an error returned when a channel is not known
|
|
|
|
// to us. In this case of the fundingManager, this error is returned
|
|
|
|
// when the channel in question is not considered being in an opening
|
|
|
|
// state.
|
|
|
|
ErrChannelNotFound = fmt.Errorf("channel not found")
|
2017-06-08 20:48:07 +03:00
|
|
|
)
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// newFundingManager creates and initializes a new instance of the
|
|
|
|
// fundingManager.
|
2017-02-22 09:14:22 +03:00
|
|
|
func newFundingManager(cfg fundingConfig) (*fundingManager, error) {
|
2016-06-21 21:31:10 +03:00
|
|
|
return &fundingManager{
|
2017-09-01 12:42:40 +03:00
|
|
|
cfg: &cfg,
|
|
|
|
chanIDKey: cfg.TempChanIDSeed,
|
|
|
|
activeReservations: make(map[serializedPubKey]pendingChannels),
|
|
|
|
signedReservations: make(map[lnwire.ChannelID][32]byte),
|
|
|
|
newChanBarriers: make(map[lnwire.ChannelID]chan struct{}),
|
|
|
|
fundingMsgs: make(chan interface{}, msgBufferSize),
|
|
|
|
fundingRequests: make(chan *initFundingMsg, msgBufferSize),
|
|
|
|
localDiscoverySignals: make(map[lnwire.ChannelID]chan struct{}),
|
|
|
|
handleFundingLockedBarriers: make(map[lnwire.ChannelID]struct{}),
|
|
|
|
queries: make(chan interface{}, 1),
|
|
|
|
quit: make(chan struct{}),
|
2017-01-13 06:40:38 +03:00
|
|
|
}, nil
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start launches all helper goroutines required for handling requests sent
|
|
|
|
// to the funding manager.
|
|
|
|
func (f *fundingManager) Start() error {
|
2019-01-23 20:15:57 +03:00
|
|
|
var err error
|
|
|
|
f.started.Do(func() {
|
|
|
|
err = f.start()
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2019-01-23 20:15:57 +03:00
|
|
|
func (f *fundingManager) start() error {
|
2016-11-29 06:43:57 +03:00
|
|
|
fndgLog.Tracef("Funding manager running")
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
// Upon restart, the Funding Manager will check the database to load any
|
|
|
|
// channels that were waiting for their funding transactions to be
|
|
|
|
// confirmed on the blockchain at the time when the daemon last went
|
|
|
|
// down.
|
2017-05-11 03:23:47 +03:00
|
|
|
// TODO(roasbeef): store height that funding finished?
|
|
|
|
// * would then replace call below
|
2018-09-16 11:40:11 +03:00
|
|
|
allChannels, err := f.cfg.Wallet.Cfg.Database.FetchAllChannels()
|
2017-06-08 20:48:07 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:11 +03:00
|
|
|
for _, channel := range allChannels {
|
2017-06-08 20:48:07 +03:00
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&channel.FundingOutpoint)
|
2017-09-01 12:42:40 +03:00
|
|
|
|
2018-09-16 11:40:11 +03:00
|
|
|
// For any channels that were in a pending state when the
|
|
|
|
// daemon was last connected, the Funding Manager will
|
|
|
|
// re-initialize the channel barriers, and republish the
|
|
|
|
// funding transaction if we're the initiator.
|
2018-02-04 05:02:14 +03:00
|
|
|
if channel.IsPending {
|
|
|
|
f.barrierMtx.Lock()
|
|
|
|
fndgLog.Tracef("Loading pending ChannelPoint(%v), "+
|
2018-09-16 11:40:11 +03:00
|
|
|
"creating chan barrier",
|
|
|
|
channel.FundingOutpoint)
|
|
|
|
|
2018-02-04 05:02:14 +03:00
|
|
|
f.newChanBarriers[chanID] = make(chan struct{})
|
|
|
|
f.barrierMtx.Unlock()
|
2017-11-14 02:55:50 +03:00
|
|
|
|
2018-09-16 11:40:11 +03:00
|
|
|
f.localDiscoverySignals[chanID] = make(chan struct{})
|
2017-11-14 02:55:50 +03:00
|
|
|
|
2018-09-16 11:40:11 +03:00
|
|
|
// Rebroadcast the funding transaction for any pending
|
|
|
|
// channel that we initiated. No error will be returned
|
2019-11-01 06:56:50 +03:00
|
|
|
// if the transaction already has been broadcast.
|
|
|
|
chanType := channel.ChanType
|
|
|
|
if chanType.IsSingleFunder() && chanType.HasFundingTx() &&
|
2018-09-16 11:40:11 +03:00
|
|
|
channel.IsInitiator {
|
2017-11-14 02:55:50 +03:00
|
|
|
|
2018-09-16 11:40:11 +03:00
|
|
|
err := f.cfg.PublishTransaction(
|
|
|
|
channel.FundingTxn,
|
|
|
|
)
|
2017-09-17 22:34:59 +03:00
|
|
|
if err != nil {
|
2018-09-16 11:40:11 +03:00
|
|
|
fndgLog.Errorf("Unable to rebroadcast "+
|
|
|
|
"funding tx for "+
|
|
|
|
"ChannelPoint(%v): %v",
|
|
|
|
channel.FundingOutpoint, err)
|
2017-09-17 22:34:59 +03:00
|
|
|
}
|
2018-03-11 04:28:00 +03:00
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
2018-03-11 04:28:00 +03:00
|
|
|
|
2018-09-16 11:40:11 +03:00
|
|
|
// We will restart the funding state machine for all channels,
|
|
|
|
// which will wait for the channel's funding transaction to be
|
2019-09-12 16:05:52 +03:00
|
|
|
// confirmed on the blockchain, and transmit the messages
|
2018-09-16 11:40:11 +03:00
|
|
|
// necessary for the channel to be operational.
|
2018-09-16 11:40:09 +03:00
|
|
|
f.wg.Add(1)
|
2018-09-16 11:40:10 +03:00
|
|
|
go f.advanceFundingState(channel, chanID, nil)
|
2017-01-24 05:19:54 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
f.wg.Add(1) // TODO(roasbeef): tune
|
|
|
|
go f.reservationCoordinator()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// Stop signals all helper goroutines to execute a graceful shutdown. This
|
2016-06-21 21:31:10 +03:00
|
|
|
// method will block until all goroutines have exited.
|
|
|
|
func (f *fundingManager) Stop() error {
|
2019-01-23 20:15:57 +03:00
|
|
|
var err error
|
|
|
|
f.stopped.Do(func() {
|
|
|
|
err = f.stop()
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2019-01-23 20:15:57 +03:00
|
|
|
func (f *fundingManager) stop() error {
|
2016-11-29 06:43:57 +03:00
|
|
|
fndgLog.Infof("Funding manager shutting down")
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
|
|
close(f.quit)
|
|
|
|
f.wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
// nextPendingChanID returns the next free pending channel ID to be used to
|
|
|
|
// identify a particular future channel funding workflow.
|
|
|
|
func (f *fundingManager) nextPendingChanID() [32]byte {
|
|
|
|
// Obtain a fresh nonce. We do this by encoding the current nonce
|
|
|
|
// counter, then incrementing it by one.
|
|
|
|
f.nonceMtx.Lock()
|
|
|
|
var nonce [8]byte
|
|
|
|
binary.LittleEndian.PutUint64(nonce[:], f.chanIDNonce)
|
|
|
|
f.chanIDNonce++
|
|
|
|
f.nonceMtx.Unlock()
|
|
|
|
|
|
|
|
// We'll generate the next pending channelID by "encrypting" 32-bytes
|
|
|
|
// of zeroes which'll extract 32 random bytes from our stream cipher.
|
|
|
|
var (
|
|
|
|
nextChanID [32]byte
|
|
|
|
zeroes [32]byte
|
|
|
|
)
|
|
|
|
salsa20.XORKeyStream(nextChanID[:], zeroes[:], nonce[:], &f.chanIDKey)
|
|
|
|
|
|
|
|
return nextChanID
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:33:52 +03:00
|
|
|
type pendingChannel struct {
|
2016-10-28 05:49:10 +03:00
|
|
|
identityPub *btcec.PublicKey
|
2016-07-08 01:33:52 +03:00
|
|
|
channelPoint *wire.OutPoint
|
|
|
|
capacity btcutil.Amount
|
|
|
|
localBalance btcutil.Amount
|
|
|
|
remoteBalance btcutil.Amount
|
|
|
|
}
|
|
|
|
|
|
|
|
type pendingChansReq struct {
|
|
|
|
resp chan []*pendingChannel
|
2017-01-23 10:31:01 +03:00
|
|
|
err chan error
|
2016-07-08 01:33:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// PendingChannels returns a slice describing all the channels which are
|
|
|
|
// currently pending at the last state of the funding workflow.
|
2017-01-23 10:31:01 +03:00
|
|
|
func (f *fundingManager) PendingChannels() ([]*pendingChannel, error) {
|
|
|
|
respChan := make(chan []*pendingChannel, 1)
|
2018-05-30 13:07:20 +03:00
|
|
|
errChan := make(chan error, 1)
|
2016-07-08 01:33:52 +03:00
|
|
|
|
2017-01-23 10:31:01 +03:00
|
|
|
req := &pendingChansReq{
|
|
|
|
resp: respChan,
|
|
|
|
err: errChan,
|
|
|
|
}
|
2016-07-08 01:33:52 +03:00
|
|
|
|
2018-05-30 13:07:20 +03:00
|
|
|
select {
|
|
|
|
case f.queries <- req:
|
|
|
|
case <-f.quit:
|
2018-07-06 04:33:28 +03:00
|
|
|
return nil, ErrFundingManagerShuttingDown
|
2018-05-30 13:07:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case resp := <-respChan:
|
|
|
|
return resp, nil
|
|
|
|
case err := <-errChan:
|
|
|
|
return nil, err
|
|
|
|
case <-f.quit:
|
2018-07-06 04:33:28 +03:00
|
|
|
return nil, ErrFundingManagerShuttingDown
|
2018-05-30 13:07:20 +03:00
|
|
|
}
|
2016-07-08 01:33:52 +03:00
|
|
|
}
|
|
|
|
|
funding+server: ensure we cancel all reservations when a peer disconnects
In this commit, we fix an existing issue that could at times cause an
inconsistent view between the set of total coins, and the set of segwit
coins in the wallet of the node. This could be caused by initiating a
funding flow, but then the funding negotiation breaking down somewhere
along the lines. In this case, us or the other peer will disconnect.
When we initiate funding flows, we lock coins exclusively, to ensure
that concurrent funding flows don’t end up double spending the same
coin. Before this commit, we wouldn’t ever unlock those coins. As a
result, our view of available coins would be skewed.
The walletbalance call would show all the coins, but when adding the
—witness_only flag, some coins would be missing, or gone all together.
This is because the former call actually scans the txstore and manually
tallies the amount of available coins, while the latter looks at the
sent of available outputs, which is filtered based on which coins are
locked.
To remedy this, we now ensure that when a peer disconnects, we wipe all
existing reservations which will return any locked outputs to the set
of available outputs for funding flows.
2017-11-26 22:25:26 +03:00
|
|
|
// CancelPeerReservations cancels all active reservations associated with the
|
|
|
|
// passed node. This will ensure any outputs which have been pre committed,
|
|
|
|
// (and thus locked from coin selection), are properly freed.
|
|
|
|
func (f *fundingManager) CancelPeerReservations(nodePub [33]byte) {
|
|
|
|
|
|
|
|
fndgLog.Debugf("Cancelling all reservations for peer %x", nodePub[:])
|
|
|
|
|
|
|
|
f.resMtx.Lock()
|
|
|
|
defer f.resMtx.Unlock()
|
|
|
|
|
|
|
|
// We'll attempt to look up this node in the set of active
|
|
|
|
// reservations. If they don't have any, then there's no further work
|
|
|
|
// to be done.
|
|
|
|
nodeReservations, ok := f.activeReservations[nodePub]
|
|
|
|
if !ok {
|
|
|
|
fndgLog.Debugf("No active reservations for node: %x", nodePub[:])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If they do have any active reservations, then we'll cancel all of
|
|
|
|
// them (which releases any locked UTXO's), and also delete it from the
|
|
|
|
// reservation map.
|
|
|
|
for pendingID, resCtx := range nodeReservations {
|
|
|
|
if err := resCtx.reservation.Cancel(); err != nil {
|
|
|
|
fndgLog.Errorf("unable to cancel reservation for "+
|
|
|
|
"node=%x: %v", nodePub[:], err)
|
|
|
|
}
|
|
|
|
|
2018-05-30 13:13:12 +03:00
|
|
|
resCtx.err <- fmt.Errorf("peer disconnected")
|
funding+server: ensure we cancel all reservations when a peer disconnects
In this commit, we fix an existing issue that could at times cause an
inconsistent view between the set of total coins, and the set of segwit
coins in the wallet of the node. This could be caused by initiating a
funding flow, but then the funding negotiation breaking down somewhere
along the lines. In this case, us or the other peer will disconnect.
When we initiate funding flows, we lock coins exclusively, to ensure
that concurrent funding flows don’t end up double spending the same
coin. Before this commit, we wouldn’t ever unlock those coins. As a
result, our view of available coins would be skewed.
The walletbalance call would show all the coins, but when adding the
—witness_only flag, some coins would be missing, or gone all together.
This is because the former call actually scans the txstore and manually
tallies the amount of available coins, while the latter looks at the
sent of available outputs, which is filtered based on which coins are
locked.
To remedy this, we now ensure that when a peer disconnects, we wipe all
existing reservations which will return any locked outputs to the set
of available outputs for funding flows.
2017-11-26 22:25:26 +03:00
|
|
|
delete(nodeReservations, pendingID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we'll delete the node itself from the set of reservations.
|
|
|
|
delete(f.activeReservations, nodePub)
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:30:01 +03:00
|
|
|
// failFundingFlow will fail the active funding flow with the target peer,
|
2018-02-27 20:37:21 +03:00
|
|
|
// identified by its unique temporary channel ID. This method will send an
|
|
|
|
// error to the remote peer, and also remove the reservation from our set of
|
|
|
|
// pending reservations.
|
2017-09-12 18:30:01 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): if peer disconnects, and haven't yet broadcast funding
|
|
|
|
// transaction, then all reservations should be cleared.
|
2018-07-05 23:41:51 +03:00
|
|
|
func (f *fundingManager) failFundingFlow(peer lnpeer.Peer, tempChanID [32]byte,
|
|
|
|
fundingErr error) {
|
2018-02-27 20:37:21 +03:00
|
|
|
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Debugf("Failing funding flow for pending_id=%x: %v",
|
2018-05-30 13:22:51 +03:00
|
|
|
tempChanID, fundingErr)
|
|
|
|
|
2018-07-05 23:41:51 +03:00
|
|
|
ctx, err := f.cancelReservationCtx(peer.IdentityKey(), tempChanID)
|
2018-05-30 13:22:51 +03:00
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("unable to cancel reservation: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case the case where the reservation existed, send the funding
|
|
|
|
// error on the error channel.
|
|
|
|
if ctx != nil {
|
|
|
|
ctx.err <- fundingErr
|
|
|
|
}
|
|
|
|
|
2018-02-27 20:37:21 +03:00
|
|
|
// We only send the exact error if it is part of out whitelisted set of
|
2019-09-20 11:55:21 +03:00
|
|
|
// errors (lnwire.FundingError or lnwallet.ReservationError).
|
2018-02-27 20:37:21 +03:00
|
|
|
var msg lnwire.ErrorData
|
|
|
|
switch e := fundingErr.(type) {
|
|
|
|
|
2019-09-20 11:55:20 +03:00
|
|
|
// Let the actual error message be sent to the remote for the
|
|
|
|
// whitelisted types.
|
2018-02-27 20:37:21 +03:00
|
|
|
case lnwallet.ReservationError:
|
|
|
|
msg = lnwire.ErrorData(e.Error())
|
2019-09-20 11:55:21 +03:00
|
|
|
case lnwire.FundingError:
|
2019-09-20 11:55:20 +03:00
|
|
|
msg = lnwire.ErrorData(e.Error())
|
2018-02-27 20:37:21 +03:00
|
|
|
|
2019-09-20 11:55:20 +03:00
|
|
|
// For all other error types we just send a generic error.
|
2018-02-27 20:37:21 +03:00
|
|
|
default:
|
|
|
|
msg = lnwire.ErrorData("funding failed due to internal error")
|
|
|
|
}
|
2017-09-12 18:30:01 +03:00
|
|
|
|
|
|
|
errMsg := &lnwire.Error{
|
|
|
|
ChanID: tempChanID,
|
2017-09-12 23:38:37 +03:00
|
|
|
Data: msg,
|
2017-09-12 18:30:01 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 13:22:51 +03:00
|
|
|
fndgLog.Debugf("Sending funding error to peer (%x): %v",
|
2018-07-05 23:41:51 +03:00
|
|
|
peer.IdentityKey().SerializeCompressed(), spew.Sdump(errMsg))
|
|
|
|
if err := peer.SendMessage(false, errMsg); err != nil {
|
2017-09-12 18:30:01 +03:00
|
|
|
fndgLog.Errorf("unable to send error message to peer %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// reservationCoordinator is the primary goroutine tasked with progressing the
|
|
|
|
// funding workflow between the wallet, and any outside peers or local callers.
|
|
|
|
//
|
|
|
|
// NOTE: This MUST be run as a goroutine.
|
|
|
|
func (f *fundingManager) reservationCoordinator() {
|
2016-12-27 08:42:23 +03:00
|
|
|
defer f.wg.Done()
|
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
zombieSweepTicker := time.NewTicker(f.cfg.ZombieSweeperInterval)
|
|
|
|
defer zombieSweepTicker.Stop()
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
for {
|
|
|
|
select {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
case msg := <-f.fundingMsgs:
|
|
|
|
switch fmsg := msg.(type) {
|
2017-07-31 01:53:53 +03:00
|
|
|
case *fundingOpenMsg:
|
|
|
|
f.handleFundingOpen(fmsg)
|
|
|
|
case *fundingAcceptMsg:
|
|
|
|
f.handleFundingAccept(fmsg)
|
|
|
|
case *fundingCreatedMsg:
|
|
|
|
f.handleFundingCreated(fmsg)
|
|
|
|
case *fundingSignedMsg:
|
|
|
|
f.handleFundingSigned(fmsg)
|
2017-01-24 05:19:54 +03:00
|
|
|
case *fundingLockedMsg:
|
2017-09-01 12:42:40 +03:00
|
|
|
f.wg.Add(1)
|
2017-06-17 00:19:39 +03:00
|
|
|
go f.handleFundingLocked(fmsg)
|
2016-10-15 16:24:56 +03:00
|
|
|
case *fundingErrorMsg:
|
2017-04-17 01:34:23 +03:00
|
|
|
f.handleErrorMsg(fmsg)
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
case req := <-f.fundingRequests:
|
|
|
|
f.handleInitFundingMsg(req)
|
2017-07-31 01:53:53 +03:00
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
case <-zombieSweepTicker.C:
|
|
|
|
f.pruneZombieReservations()
|
|
|
|
|
2016-07-06 04:57:08 +03:00
|
|
|
case req := <-f.queries:
|
|
|
|
switch msg := req.(type) {
|
2016-07-08 01:33:52 +03:00
|
|
|
case *pendingChansReq:
|
|
|
|
f.handlePendingChannels(msg)
|
2016-07-06 04:57:08 +03:00
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
case <-f.quit:
|
2016-12-27 08:42:23 +03:00
|
|
|
return
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// advanceFundingState will advance the channel through the steps after the
|
|
|
|
// funding transaction is broadcasted, up until the point where the channel is
|
|
|
|
// ready for operation. This includes waiting for the funding transaction to
|
|
|
|
// confirm, sending funding locked to the peer, adding the channel to the
|
|
|
|
// router graph, and announcing the channel. The updateChan can be set non-nil
|
|
|
|
// to get OpenStatusUpdates.
|
2018-09-16 11:40:09 +03:00
|
|
|
//
|
|
|
|
// NOTE: This MUST be run as a goroutine.
|
|
|
|
func (f *fundingManager) advanceFundingState(channel *channeldb.OpenChannel,
|
2018-09-16 11:40:10 +03:00
|
|
|
pendingChanID [32]byte, updateChan chan<- *lnrpc.OpenStatusUpdate) {
|
2018-09-16 11:40:09 +03:00
|
|
|
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// If the channel is still pending we must wait for the funding
|
|
|
|
// transaction to confirm.
|
|
|
|
if channel.IsPending {
|
|
|
|
err := f.advancePendingChannelState(channel, pendingChanID)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to advance pending state of "+
|
|
|
|
"ChannelPoint(%v): %v",
|
|
|
|
channel.FundingOutpoint, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
// We create the state-machine object which wraps the database state.
|
|
|
|
lnChannel, err := lnwallet.NewLightningChannel(
|
|
|
|
nil, channel, nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to create LightningChannel(%v): %v",
|
|
|
|
channel.FundingOutpoint, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
channelState, shortChanID, err := f.getChannelOpeningState(
|
|
|
|
&channel.FundingOutpoint,
|
|
|
|
)
|
|
|
|
if err == ErrChannelNotFound {
|
|
|
|
// Channel not in fundingManager's opening database,
|
|
|
|
// meaning it was successfully announced to the
|
|
|
|
// network.
|
2019-08-07 16:38:44 +03:00
|
|
|
// TODO(halseth): could do graph consistency check
|
|
|
|
// here, and re-add the edge if missing.
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Debugf("ChannelPoint(%v) with chan_id=%x not "+
|
2019-08-07 16:38:44 +03:00
|
|
|
"found in opening database, assuming already "+
|
|
|
|
"announced to the network",
|
|
|
|
channel.FundingOutpoint, pendingChanID)
|
2018-09-16 11:40:09 +03:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to query database for "+
|
|
|
|
"channel opening state(%v): %v",
|
|
|
|
channel.FundingOutpoint, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we did find the channel in the opening state database, we
|
|
|
|
// have seen the funding transaction being confirmed, but there
|
|
|
|
// are still steps left of the setup procedure. We continue the
|
|
|
|
// procedure where we left off.
|
|
|
|
err = f.stateStep(
|
|
|
|
channel, lnChannel, shortChanID, channelState,
|
|
|
|
updateChan,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to advance state(%v): %v",
|
|
|
|
channel.FundingOutpoint, err)
|
|
|
|
return
|
|
|
|
}
|
2018-09-16 11:40:09 +03:00
|
|
|
}
|
2018-09-16 11:40:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// stateStep advances the confirmed channel one step in the funding state
|
|
|
|
// machine. This method is synchronous and the new channel opening state will
|
|
|
|
// have been written to the database when it successfully returns. The
|
|
|
|
// updateChan can be set non-nil to get OpenStatusUpdates.
|
|
|
|
func (f *fundingManager) stateStep(channel *channeldb.OpenChannel,
|
|
|
|
lnChannel *lnwallet.LightningChannel,
|
|
|
|
shortChanID *lnwire.ShortChannelID, channelState channelOpeningState,
|
|
|
|
updateChan chan<- *lnrpc.OpenStatusUpdate) error {
|
2018-09-16 11:40:09 +03:00
|
|
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&channel.FundingOutpoint)
|
2018-09-16 11:40:09 +03:00
|
|
|
fndgLog.Debugf("Channel(%v) with ShortChanID %v has opening state %v",
|
|
|
|
chanID, shortChanID, channelState)
|
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
switch channelState {
|
|
|
|
|
|
|
|
// The funding transaction was confirmed, but we did not successfully
|
|
|
|
// send the fundingLocked message to the peer, so let's do that now.
|
|
|
|
case markedOpen:
|
2019-09-18 12:27:34 +03:00
|
|
|
err := f.sendFundingLocked(channel, lnChannel, shortChanID)
|
2018-09-16 11:40:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed sending fundingLocked: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
// As the fundingLocked message is now sent to the peer, the
|
|
|
|
// channel is moved to the next state of the state machine. It
|
|
|
|
// will be moved to the last state (actually deleted from the
|
|
|
|
// database) after the channel is finally announced.
|
|
|
|
err = f.saveChannelOpeningState(
|
|
|
|
&channel.FundingOutpoint, fundingLockedSent,
|
|
|
|
shortChanID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error setting channel state to"+
|
|
|
|
" fundingLockedSent: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
fndgLog.Debugf("Channel(%v) with ShortChanID %v: successfully "+
|
|
|
|
"sent FundingLocked", chanID, shortChanID)
|
2018-09-16 11:40:09 +03:00
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
return nil
|
2018-09-16 11:40:09 +03:00
|
|
|
|
|
|
|
// fundingLocked was sent to peer, but the channel was not added to the
|
|
|
|
// router graph and the channel announcement was not sent.
|
|
|
|
case fundingLockedSent:
|
2018-09-16 11:40:09 +03:00
|
|
|
err := f.addToRouterGraph(channel, shortChanID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed adding to "+
|
|
|
|
"router graph: %v", err)
|
|
|
|
}
|
2018-09-16 11:40:09 +03:00
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
// As the channel is now added to the ChannelRouter's topology,
|
|
|
|
// the channel is moved to the next state of the state machine.
|
|
|
|
// It will be moved to the last state (actually deleted from
|
|
|
|
// the database) after the channel is finally announced.
|
|
|
|
err = f.saveChannelOpeningState(
|
|
|
|
&channel.FundingOutpoint, addedToRouterGraph,
|
|
|
|
shortChanID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error setting channel state to"+
|
|
|
|
" addedToRouterGraph: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
fndgLog.Debugf("Channel(%v) with ShortChanID %v: successfully "+
|
|
|
|
"added to router graph", chanID, shortChanID)
|
2018-09-16 11:40:09 +03:00
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
// Give the caller a final update notifying them that
|
|
|
|
// the channel is now open.
|
|
|
|
// TODO(roasbeef): only notify after recv of funding locked?
|
|
|
|
fundingPoint := channel.FundingOutpoint
|
|
|
|
cp := &lnrpc.ChannelPoint{
|
|
|
|
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
|
|
|
|
FundingTxidBytes: fundingPoint.Hash[:],
|
|
|
|
},
|
|
|
|
OutputIndex: fundingPoint.Index,
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateChan != nil {
|
|
|
|
upd := &lnrpc.OpenStatusUpdate{
|
|
|
|
Update: &lnrpc.OpenStatusUpdate_ChanOpen{
|
|
|
|
ChanOpen: &lnrpc.ChannelOpenUpdate{
|
|
|
|
ChannelPoint: cp,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case updateChan <- upd:
|
|
|
|
case <-f.quit:
|
|
|
|
return ErrFundingManagerShuttingDown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
return nil
|
2018-09-16 11:40:09 +03:00
|
|
|
|
|
|
|
// The channel was added to the Router's topology, but the channel
|
|
|
|
// announcement was not sent.
|
|
|
|
case addedToRouterGraph:
|
2018-09-16 11:40:09 +03:00
|
|
|
err := f.annAfterSixConfs(channel, shortChanID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error sending channel "+
|
|
|
|
"announcement: %v", err)
|
|
|
|
}
|
2018-09-16 11:40:09 +03:00
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
// We delete the channel opening state from our internal
|
|
|
|
// database as the opening process has succeeded. We can do
|
|
|
|
// this because we assume the AuthenticatedGossiper queues the
|
|
|
|
// announcement messages, and persists them in case of a daemon
|
|
|
|
// shutdown.
|
|
|
|
err = f.deleteChannelOpeningState(&channel.FundingOutpoint)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deleting channel state: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
fndgLog.Debugf("Channel(%v) with ShortChanID %v: successfully "+
|
|
|
|
"announced", chanID, shortChanID)
|
2018-09-16 11:40:09 +03:00
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
return nil
|
2018-09-16 11:40:09 +03:00
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:09 +03:00
|
|
|
return fmt.Errorf("undefined channelState: %v", channelState)
|
2018-09-16 11:40:09 +03:00
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// advancePendingChannelState waits for a pending channel's funding tx to
|
|
|
|
// confirm, and marks it open in the database when that happens.
|
|
|
|
func (f *fundingManager) advancePendingChannelState(
|
|
|
|
channel *channeldb.OpenChannel, pendingChanID [32]byte) error {
|
|
|
|
|
2019-10-01 05:59:10 +03:00
|
|
|
confChannel, err := f.waitForFundingWithTimeout(channel)
|
2018-09-16 11:40:10 +03:00
|
|
|
if err == ErrConfirmationTimeout {
|
|
|
|
// We'll get a timeout if the number of blocks mined
|
|
|
|
// since the channel was initiated reaches
|
|
|
|
// maxWaitNumBlocksFundingConf and we are not the
|
|
|
|
// channel initiator.
|
|
|
|
ch := channel
|
|
|
|
localBalance := ch.LocalCommitment.LocalBalance.ToSatoshis()
|
|
|
|
closeInfo := &channeldb.ChannelCloseSummary{
|
|
|
|
ChainHash: ch.ChainHash,
|
|
|
|
ChanPoint: ch.FundingOutpoint,
|
|
|
|
RemotePub: ch.IdentityPub,
|
|
|
|
Capacity: ch.Capacity,
|
|
|
|
SettledBalance: localBalance,
|
|
|
|
CloseType: channeldb.FundingCanceled,
|
|
|
|
RemoteCurrentRevocation: ch.RemoteCurrentRevocation,
|
|
|
|
RemoteNextRevocation: ch.RemoteNextRevocation,
|
|
|
|
LocalChanConfig: ch.LocalChanCfg,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ch.CloseChannel(closeInfo); err != nil {
|
|
|
|
return fmt.Errorf("failed closing channel "+
|
|
|
|
"%v: %v", ch.FundingOutpoint, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
timeoutErr := fmt.Errorf("timeout waiting for funding tx "+
|
|
|
|
"(%v) to confirm", channel.FundingOutpoint)
|
|
|
|
|
|
|
|
// When the peer comes online, we'll notify it that we
|
|
|
|
// are now considering the channel flow canceled.
|
|
|
|
f.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
|
|
|
peerChan := make(chan lnpeer.Peer, 1)
|
|
|
|
var peerKey [33]byte
|
|
|
|
copy(peerKey[:], ch.IdentityPub.SerializeCompressed())
|
|
|
|
|
|
|
|
f.cfg.NotifyWhenOnline(peerKey, peerChan)
|
|
|
|
|
|
|
|
var peer lnpeer.Peer
|
|
|
|
select {
|
|
|
|
case peer = <-peerChan:
|
|
|
|
case <-f.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO(halseth): should this send be made
|
|
|
|
// reliable?
|
|
|
|
f.failFundingFlow(peer, pendingChanID, timeoutErr)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return timeoutErr
|
|
|
|
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("error waiting for funding "+
|
|
|
|
"confirmation for ChannelPoint(%v): %v",
|
|
|
|
channel.FundingOutpoint, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success, funding transaction was confirmed.
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&channel.FundingOutpoint)
|
|
|
|
fndgLog.Debugf("ChannelID(%v) is now fully confirmed! "+
|
2019-10-01 05:59:10 +03:00
|
|
|
"(shortChanID=%v)", chanID, confChannel.shortChanID)
|
2018-09-16 11:40:10 +03:00
|
|
|
|
2019-10-01 05:59:10 +03:00
|
|
|
err = f.handleFundingConfirmation(channel, confChannel)
|
2018-09-16 11:40:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to handle funding "+
|
|
|
|
"confirmation for ChannelPoint(%v): %v",
|
|
|
|
channel.FundingOutpoint, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:33:52 +03:00
|
|
|
// handlePendingChannels responds to a request for details concerning all
|
|
|
|
// currently pending channels waiting for the final phase of the funding
|
|
|
|
// workflow (funding txn confirmation).
|
|
|
|
func (f *fundingManager) handlePendingChannels(msg *pendingChansReq) {
|
|
|
|
var pendingChannels []*pendingChannel
|
2017-01-23 10:31:01 +03:00
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
dbPendingChannels, err := f.cfg.Wallet.Cfg.Database.FetchPendingChannels()
|
2017-01-23 10:31:01 +03:00
|
|
|
if err != nil {
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dbPendingChan := range dbPendingChannels {
|
|
|
|
pendingChan := &pendingChannel{
|
|
|
|
identityPub: dbPendingChan.IdentityPub,
|
2017-07-31 01:53:53 +03:00
|
|
|
channelPoint: &dbPendingChan.FundingOutpoint,
|
2017-01-23 10:31:01 +03:00
|
|
|
capacity: dbPendingChan.Capacity,
|
2017-11-11 02:21:42 +03:00
|
|
|
localBalance: dbPendingChan.LocalCommitment.LocalBalance.ToSatoshis(),
|
|
|
|
remoteBalance: dbPendingChan.LocalCommitment.RemoteBalance.ToSatoshis(),
|
2017-01-23 10:31:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pendingChannels = append(pendingChannels, pendingChan)
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:33:52 +03:00
|
|
|
msg.resp <- pendingChannels
|
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// processFundingOpen sends a message to the fundingManager allowing it to
|
2016-10-15 16:18:38 +03:00
|
|
|
// initiate the new funding workflow with the source peer.
|
2017-07-31 01:53:53 +03:00
|
|
|
func (f *fundingManager) processFundingOpen(msg *lnwire.OpenChannel,
|
2018-07-05 23:41:51 +03:00
|
|
|
peer lnpeer.Peer) {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
2017-09-28 06:06:57 +03:00
|
|
|
select {
|
2018-07-05 23:41:51 +03:00
|
|
|
case f.fundingMsgs <- &fundingOpenMsg{msg, peer}:
|
2017-09-28 06:06:57 +03:00
|
|
|
case <-f.quit:
|
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// handleFundingOpen creates an initial 'ChannelReservation' within the wallet,
|
|
|
|
// then responds to the source peer with an accept channel message progressing
|
|
|
|
// the funding workflow.
|
|
|
|
//
|
2016-07-08 01:33:52 +03:00
|
|
|
// TODO(roasbeef): add error chan to all, let channelManager handle
|
2016-06-21 21:31:10 +03:00
|
|
|
// error+propagate
|
2017-07-31 01:53:53 +03:00
|
|
|
func (f *fundingManager) handleFundingOpen(fmsg *fundingOpenMsg) {
|
2016-10-15 16:24:56 +03:00
|
|
|
// Check number of pending channels to be smaller than maximum allowed
|
2017-07-31 01:53:53 +03:00
|
|
|
// number and send ErrorGeneric to remote peer if condition is
|
|
|
|
// violated.
|
2018-07-26 20:22:11 +03:00
|
|
|
peerPubKey := fmsg.peer.IdentityKey()
|
|
|
|
peerIDKey := newSerializedKey(peerPubKey)
|
2017-11-14 04:20:05 +03:00
|
|
|
|
2017-08-06 02:52:37 +03:00
|
|
|
msg := fmsg.msg
|
|
|
|
amt := msg.FundingAmount
|
|
|
|
|
2018-07-26 20:22:11 +03:00
|
|
|
// We count the number of pending channels for this peer. This is the
|
|
|
|
// sum of the active reservations and the channels pending open in the
|
|
|
|
// database.
|
|
|
|
f.resMtx.RLock()
|
|
|
|
numPending := len(f.activeReservations[peerIDKey])
|
|
|
|
f.resMtx.RUnlock()
|
|
|
|
|
|
|
|
channels, err := f.cfg.Wallet.Cfg.Database.FetchOpenChannels(peerPubKey)
|
|
|
|
if err != nil {
|
|
|
|
f.failFundingFlow(
|
|
|
|
fmsg.peer, fmsg.msg.PendingChannelID, err,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range channels {
|
|
|
|
if c.IsPending {
|
|
|
|
numPending++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// TODO(roasbeef): modify to only accept a _single_ pending channel per
|
|
|
|
// block unless white listed
|
2019-07-11 14:14:35 +03:00
|
|
|
if numPending >= f.cfg.MaxPendingChannels {
|
2017-09-12 18:31:17 +03:00
|
|
|
f.failFundingFlow(
|
2018-07-05 23:41:51 +03:00
|
|
|
fmsg.peer, fmsg.msg.PendingChannelID,
|
|
|
|
lnwire.ErrMaxPendingChannels,
|
|
|
|
)
|
2017-02-25 03:19:56 +03:00
|
|
|
return
|
2017-01-25 04:12:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll also reject any requests to create channels until we're fully
|
|
|
|
// synced to the network as we won't be able to properly validate the
|
|
|
|
// confirmation of the funding transaction.
|
2017-12-10 10:42:46 +03:00
|
|
|
isSynced, _, err := f.cfg.Wallet.IsSynced()
|
2018-02-19 20:32:04 +03:00
|
|
|
if err != nil || !isSynced {
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("unable to query wallet: %v", err)
|
|
|
|
}
|
2017-09-12 18:31:17 +03:00
|
|
|
f.failFundingFlow(
|
2018-07-05 23:41:51 +03:00
|
|
|
fmsg.peer, fmsg.msg.PendingChannelID,
|
|
|
|
lnwire.ErrSynchronizingChain,
|
|
|
|
)
|
2016-10-15 16:24:56 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-06 02:52:37 +03:00
|
|
|
// We'll reject any request to create a channel that's above the
|
|
|
|
// current soft-limit for channel size.
|
2019-05-15 10:02:53 +03:00
|
|
|
if msg.FundingAmount > MaxFundingAmount {
|
2017-09-12 18:31:17 +03:00
|
|
|
f.failFundingFlow(
|
2018-07-05 23:41:51 +03:00
|
|
|
fmsg.peer, fmsg.msg.PendingChannelID,
|
2018-04-03 02:17:50 +03:00
|
|
|
lnwire.ErrChanTooLarge,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll, also ensure that the remote party isn't attempting to propose
|
|
|
|
// a channel that's below our current min channel size.
|
|
|
|
if amt < f.cfg.MinChanSize {
|
|
|
|
f.failFundingFlow(
|
2018-07-05 23:41:51 +03:00
|
|
|
fmsg.peer, fmsg.msg.PendingChannelID,
|
2018-04-03 02:17:50 +03:00
|
|
|
lnwallet.ErrChanTooSmall(amt, btcutil.Amount(f.cfg.MinChanSize)),
|
|
|
|
)
|
2017-08-06 02:52:37 +03:00
|
|
|
return
|
|
|
|
}
|
2017-07-31 01:53:53 +03:00
|
|
|
|
2018-10-10 11:28:39 +03:00
|
|
|
// If request specifies non-zero push amount and 'rejectpush' is set,
|
|
|
|
// signal an error.
|
2019-07-11 14:14:35 +03:00
|
|
|
if f.cfg.RejectPush && msg.PushAmount > 0 {
|
2018-10-10 11:28:39 +03:00
|
|
|
f.failFundingFlow(
|
|
|
|
fmsg.peer, fmsg.msg.PendingChannelID,
|
2019-08-08 05:18:23 +03:00
|
|
|
lnwallet.ErrNonZeroPushAmount(),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the OpenChannel request to the ChannelAcceptor to determine whether
|
|
|
|
// this node will accept the channel.
|
|
|
|
chanReq := &chanacceptor.ChannelAcceptRequest{
|
|
|
|
Node: fmsg.peer.IdentityKey(),
|
|
|
|
OpenChanMsg: fmsg.msg,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !f.cfg.OpenChannelPredicate.Accept(chanReq) {
|
|
|
|
f.failFundingFlow(
|
|
|
|
fmsg.peer, fmsg.msg.PendingChannelID,
|
|
|
|
fmt.Errorf("open channel request rejected"),
|
|
|
|
)
|
2018-10-10 11:28:39 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:31:17 +03:00
|
|
|
fndgLog.Infof("Recv'd fundingRequest(amt=%v, push=%v, delay=%v, "+
|
|
|
|
"pendingId=%x) from peer(%x)", amt, msg.PushAmount,
|
|
|
|
msg.CsvDelay, msg.PendingChannelID,
|
2018-07-05 23:41:51 +03:00
|
|
|
fmsg.peer.IdentityKey().SerializeCompressed())
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
|
|
// Attempt to initialize a reservation within the wallet. If the wallet
|
2017-03-25 02:12:05 +03:00
|
|
|
// has insufficient resources to create the channel, then the
|
|
|
|
// reservation attempt may be rejected. Note that since we're on the
|
|
|
|
// responding side of a single funder workflow, we don't commit any
|
|
|
|
// funds to the channel ourselves.
|
2019-09-11 15:44:31 +03:00
|
|
|
//
|
|
|
|
// Before we init the channel, we'll also check to see if we've
|
|
|
|
// negotiated the new tweakless commitment format. This is only the
|
|
|
|
// case if *both* us and the remote peer are signaling the proper
|
|
|
|
// feature bit.
|
2019-11-08 16:31:47 +03:00
|
|
|
localTweakless := fmsg.peer.LocalFeatures().HasFeature(
|
2019-09-11 15:44:31 +03:00
|
|
|
lnwire.StaticRemoteKeyOptional,
|
|
|
|
)
|
2019-11-08 16:31:47 +03:00
|
|
|
remoteTweakless := fmsg.peer.RemoteFeatures().HasFeature(
|
2019-09-11 15:44:31 +03:00
|
|
|
lnwire.StaticRemoteKeyOptional,
|
|
|
|
)
|
|
|
|
tweaklessCommitment := localTweakless && remoteTweakless
|
2017-07-31 01:53:53 +03:00
|
|
|
chainHash := chainhash.Hash(msg.ChainHash)
|
2018-08-10 05:16:14 +03:00
|
|
|
req := &lnwallet.InitFundingReserveMsg{
|
2019-07-11 14:14:36 +03:00
|
|
|
ChainHash: &chainHash,
|
2019-11-01 07:45:44 +03:00
|
|
|
PendingChanID: msg.PendingChannelID,
|
2019-07-11 14:14:36 +03:00
|
|
|
NodeID: fmsg.peer.IdentityKey(),
|
|
|
|
NodeAddr: fmsg.peer.Address(),
|
|
|
|
LocalFundingAmt: 0,
|
|
|
|
RemoteFundingAmt: amt,
|
2019-10-31 05:43:05 +03:00
|
|
|
CommitFeePerKw: chainfee.SatPerKWeight(msg.FeePerKiloWeight),
|
2019-07-11 14:14:36 +03:00
|
|
|
FundingFeePerKw: 0,
|
|
|
|
PushMSat: msg.PushAmount,
|
|
|
|
Flags: msg.ChannelFlags,
|
|
|
|
MinConfs: 1,
|
2019-09-11 15:44:31 +03:00
|
|
|
Tweakless: tweaklessCommitment,
|
2018-08-10 05:16:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
reservation, err := f.cfg.Wallet.InitChannelReservation(req)
|
2016-06-21 21:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to initialize reservation: %v", err)
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, msg.PendingChannelID, err)
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-11 00:58:34 +03:00
|
|
|
// 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
|
2019-05-10 18:14:19 +03:00
|
|
|
// use our mapping to derive the proper number of confirmations based on
|
2018-12-11 00:58:34 +03:00
|
|
|
// the amount of the channel, and also if any funds are being pushed to
|
|
|
|
// us.
|
2017-07-31 01:53:53 +03:00
|
|
|
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.
|
2018-12-11 00:58:34 +03:00
|
|
|
channelConstraints := &channeldb.ChannelConstraints{
|
|
|
|
DustLimit: msg.DustLimit,
|
|
|
|
ChanReserve: msg.ChannelReserve,
|
|
|
|
MaxPendingAmount: msg.MaxValueInFlight,
|
|
|
|
MinHTLC: msg.HtlcMinimum,
|
|
|
|
MaxAcceptedHtlcs: msg.MaxAcceptedHTLCs,
|
|
|
|
CsvDelay: msg.CsvDelay,
|
|
|
|
}
|
2019-01-30 04:56:28 +03:00
|
|
|
err = reservation.CommitConstraints(channelConstraints)
|
2017-09-12 19:07:43 +03:00
|
|
|
if err != nil {
|
2018-05-14 21:04:34 +03:00
|
|
|
fndgLog.Errorf("Unacceptable channel constraints: %v", err)
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, fmsg.msg.PendingChannelID, err)
|
2017-09-12 19:07:43 +03:00
|
|
|
return
|
|
|
|
}
|
2017-07-31 01:53:53 +03:00
|
|
|
|
2019-12-03 12:38:29 +03:00
|
|
|
// Check whether the peer supports upfront shutdown, and get a new wallet
|
|
|
|
// address if our node is configured to set shutdown addresses by default.
|
|
|
|
// A nil address is set in place of user input, because this channel open
|
|
|
|
// was not initiated by the user.
|
|
|
|
shutdown, err := getUpfrontShutdownScript(
|
|
|
|
fmsg.peer, nil,
|
|
|
|
func() (lnwire.DeliveryAddress, error) {
|
|
|
|
addr, err := f.cfg.Wallet.NewAddress(lnwallet.WitnessPubKey, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return txscript.PayToAddrScript(addr)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
f.failFundingFlow(
|
|
|
|
fmsg.peer, fmsg.msg.PendingChannelID,
|
|
|
|
fmt.Errorf("getUpfrontShutdownScript error: %v", err),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reservation.SetOurUpfrontShutdown(shutdown)
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
fndgLog.Infof("Requiring %v confirmations for pendingChan(%x): "+
|
2019-12-03 12:38:29 +03:00
|
|
|
"amt=%v, push_amt=%v, tweakless=%v, upfrontShutdown=%x", numConfsReq,
|
2019-09-11 15:44:31 +03:00
|
|
|
fmsg.msg.PendingChannelID, amt, msg.PushAmount,
|
2019-12-03 12:38:29 +03:00
|
|
|
tweaklessCommitment, msg.UpfrontShutdownScript)
|
2016-12-06 17:05:46 +03:00
|
|
|
|
2018-05-14 21:03:58 +03:00
|
|
|
// Generate our required constraints for the remote party.
|
2018-04-05 20:14:26 +03:00
|
|
|
remoteCsvDelay := f.cfg.RequiredRemoteDelay(amt)
|
2018-05-14 21:03:58 +03:00
|
|
|
chanReserve := f.cfg.RequiredRemoteChanReserve(amt, msg.DustLimit)
|
2018-04-05 20:48:41 +03:00
|
|
|
maxValue := f.cfg.RequiredRemoteMaxValue(amt)
|
|
|
|
maxHtlcs := f.cfg.RequiredRemoteMaxHTLCs(amt)
|
|
|
|
minHtlc := f.cfg.DefaultRoutingPolicy.MinHTLC
|
|
|
|
|
2017-02-25 03:21:52 +03:00
|
|
|
// Once the reservation has been created successfully, we add it to
|
2018-03-13 04:58:51 +03:00
|
|
|
// this peer's map of pending reservations to track this particular
|
2017-02-25 03:21:52 +03:00
|
|
|
// reservation until either abort or completion.
|
2016-06-21 21:31:10 +03:00
|
|
|
f.resMtx.Lock()
|
2017-01-13 06:40:38 +03:00
|
|
|
if _, ok := f.activeReservations[peerIDKey]; !ok {
|
|
|
|
f.activeReservations[peerIDKey] = make(pendingChannels)
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
2018-03-13 04:58:51 +03:00
|
|
|
resCtx := &reservationWithCtx{
|
2018-04-05 20:14:26 +03:00
|
|
|
reservation: reservation,
|
|
|
|
chanAmt: amt,
|
|
|
|
remoteCsvDelay: remoteCsvDelay,
|
2018-04-05 20:48:41 +03:00
|
|
|
remoteMinHtlc: minHtlc,
|
2018-04-05 20:14:26 +03:00
|
|
|
err: make(chan error, 1),
|
2018-07-05 23:41:51 +03:00
|
|
|
peer: fmsg.peer,
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
2018-03-13 04:58:51 +03:00
|
|
|
f.activeReservations[peerIDKey][msg.PendingChannelID] = resCtx
|
2016-06-21 21:31:10 +03:00
|
|
|
f.resMtx.Unlock()
|
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
// Update the timestamp once the fundingOpenMsg has been handled.
|
|
|
|
defer resCtx.updateTimestamp()
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// With our parameters set, we'll now process their contribution so we
|
|
|
|
// can move the funding workflow ahead.
|
|
|
|
remoteContribution := &lnwallet.ChannelContribution{
|
|
|
|
FundingAmount: amt,
|
|
|
|
FirstCommitmentPoint: msg.FirstCommitmentPoint,
|
|
|
|
ChannelConfig: &channeldb.ChannelConfig{
|
|
|
|
ChannelConstraints: channeldb.ChannelConstraints{
|
|
|
|
DustLimit: msg.DustLimit,
|
2017-09-12 19:07:43 +03:00
|
|
|
MaxPendingAmount: maxValue,
|
|
|
|
ChanReserve: chanReserve,
|
2018-04-05 20:48:41 +03:00
|
|
|
MinHTLC: minHtlc,
|
2017-09-12 19:07:43 +03:00
|
|
|
MaxAcceptedHtlcs: maxHtlcs,
|
2018-12-11 00:56:41 +03:00
|
|
|
CsvDelay: remoteCsvDelay,
|
2017-07-31 01:53:53 +03:00
|
|
|
},
|
2018-02-18 02:29:01 +03:00
|
|
|
MultiSigKey: keychain.KeyDescriptor{
|
|
|
|
PubKey: copyPubKey(msg.FundingKey),
|
|
|
|
},
|
|
|
|
RevocationBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: copyPubKey(msg.RevocationPoint),
|
|
|
|
},
|
|
|
|
PaymentBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: copyPubKey(msg.PaymentPoint),
|
|
|
|
},
|
|
|
|
DelayBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: copyPubKey(msg.DelayedPaymentPoint),
|
|
|
|
},
|
|
|
|
HtlcBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: copyPubKey(msg.HtlcPoint),
|
|
|
|
},
|
2017-07-31 01:53:53 +03:00
|
|
|
},
|
2019-12-03 12:38:29 +03:00
|
|
|
UpfrontShutdown: msg.UpfrontShutdownScript,
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
2017-07-31 01:53:53 +03:00
|
|
|
err = reservation.ProcessSingleContribution(remoteContribution)
|
|
|
|
if err != nil {
|
2016-06-21 21:31:10 +03:00
|
|
|
fndgLog.Errorf("unable to add contribution reservation: %v", err)
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, msg.PendingChannelID, err)
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Infof("Sending fundingResp for pending_id(%x)",
|
2017-04-17 01:34:23 +03:00
|
|
|
msg.PendingChannelID)
|
2017-09-12 19:07:43 +03:00
|
|
|
fndgLog.Debugf("Remote party accepted commitment constraints: %v",
|
|
|
|
spew.Sdump(remoteContribution.ChannelConfig.ChannelConstraints))
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// With the initiator's contribution recorded, respond with our
|
2016-06-21 21:31:10 +03:00
|
|
|
// contribution in the next message of the workflow.
|
|
|
|
ourContribution := reservation.OurContribution()
|
2017-07-31 01:53:53 +03:00
|
|
|
fundingAccept := lnwire.AcceptChannel{
|
2019-12-03 12:38:29 +03:00
|
|
|
PendingChannelID: msg.PendingChannelID,
|
|
|
|
DustLimit: ourContribution.DustLimit,
|
|
|
|
MaxValueInFlight: maxValue,
|
|
|
|
ChannelReserve: chanReserve,
|
|
|
|
MinAcceptDepth: uint32(numConfsReq),
|
|
|
|
HtlcMinimum: minHtlc,
|
|
|
|
CsvDelay: remoteCsvDelay,
|
|
|
|
MaxAcceptedHTLCs: maxHtlcs,
|
|
|
|
FundingKey: ourContribution.MultiSigKey.PubKey,
|
|
|
|
RevocationPoint: ourContribution.RevocationBasePoint.PubKey,
|
|
|
|
PaymentPoint: ourContribution.PaymentBasePoint.PubKey,
|
|
|
|
DelayedPaymentPoint: ourContribution.DelayBasePoint.PubKey,
|
|
|
|
HtlcPoint: ourContribution.HtlcBasePoint.PubKey,
|
|
|
|
FirstCommitmentPoint: ourContribution.FirstCommitmentPoint,
|
|
|
|
UpfrontShutdownScript: ourContribution.UpfrontShutdown,
|
2017-07-31 01:53:53 +03:00
|
|
|
}
|
2019-10-24 14:08:08 +03:00
|
|
|
if err := fmsg.peer.SendMessage(true, &fundingAccept); err != nil {
|
2017-01-13 06:40:38 +03:00
|
|
|
fndgLog.Errorf("unable to send funding response to peer: %v", err)
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, msg.PendingChannelID, err)
|
2017-01-13 06:40:38 +03:00
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// processFundingAccept sends a message to the fundingManager allowing it to
|
2016-06-21 21:31:10 +03:00
|
|
|
// continue the second phase of a funding workflow with the target peer.
|
2017-08-01 07:04:48 +03:00
|
|
|
func (f *fundingManager) processFundingAccept(msg *lnwire.AcceptChannel,
|
2018-07-05 23:41:51 +03:00
|
|
|
peer lnpeer.Peer) {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
2017-09-28 06:06:57 +03:00
|
|
|
select {
|
2018-07-05 23:41:51 +03:00
|
|
|
case f.fundingMsgs <- &fundingAcceptMsg{msg, peer}:
|
2017-09-28 06:06:57 +03:00
|
|
|
case <-f.quit:
|
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// handleFundingAccept processes a response to the workflow initiation sent by
|
2017-07-31 01:53:53 +03:00
|
|
|
// the remote peer. This message then queues a message with the funding
|
2016-06-21 21:31:10 +03:00
|
|
|
// outpoint, and a commitment signature to the remote peer.
|
2017-07-31 01:53:53 +03:00
|
|
|
func (f *fundingManager) handleFundingAccept(fmsg *fundingAcceptMsg) {
|
2016-06-21 21:31:10 +03:00
|
|
|
msg := fmsg.msg
|
2017-04-17 01:34:23 +03:00
|
|
|
pendingChanID := fmsg.msg.PendingChannelID
|
2018-07-05 23:41:51 +03:00
|
|
|
peerKey := fmsg.peer.IdentityKey()
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
resCtx, err := f.getReservationCtx(peerKey, pendingChanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
if err != nil {
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Warnf("Can't find reservation (peerKey:%v, chan_id:%v)",
|
2017-04-17 01:34:23 +03:00
|
|
|
peerKey, pendingChanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
// Update the timestamp once the fundingAcceptMsg has been handled.
|
|
|
|
defer resCtx.updateTimestamp()
|
|
|
|
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Infof("Recv'd fundingResponse for pending_id(%x)",
|
|
|
|
pendingChanID[:])
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2019-01-12 04:02:11 +03:00
|
|
|
// The required number of confirmations should not be greater than the
|
|
|
|
// maximum number of confirmations required by the ChainNotifier to
|
|
|
|
// properly dispatch confirmations.
|
|
|
|
if msg.MinAcceptDepth > chainntnfs.MaxNumConfs {
|
|
|
|
err := lnwallet.ErrNumConfsTooLarge(
|
|
|
|
msg.MinAcceptDepth, chainntnfs.MaxNumConfs,
|
|
|
|
)
|
|
|
|
fndgLog.Warnf("Unacceptable channel constraints: %v", err)
|
|
|
|
f.failFundingFlow(fmsg.peer, fmsg.msg.PendingChannelID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// We'll also specify the responder's preference for the number of
|
2017-09-12 19:07:43 +03:00
|
|
|
// required confirmations, and also the set of channel constraints
|
|
|
|
// they've specified for commitment states we can create.
|
2017-07-31 01:53:53 +03:00
|
|
|
resCtx.reservation.SetNumConfsRequired(uint16(msg.MinAcceptDepth))
|
2018-12-11 00:58:34 +03:00
|
|
|
channelConstraints := &channeldb.ChannelConstraints{
|
|
|
|
DustLimit: msg.DustLimit,
|
|
|
|
ChanReserve: msg.ChannelReserve,
|
|
|
|
MaxPendingAmount: msg.MaxValueInFlight,
|
|
|
|
MinHTLC: msg.HtlcMinimum,
|
|
|
|
MaxAcceptedHtlcs: msg.MaxAcceptedHTLCs,
|
|
|
|
CsvDelay: msg.CsvDelay,
|
|
|
|
}
|
2019-01-30 04:56:28 +03:00
|
|
|
err = resCtx.reservation.CommitConstraints(channelConstraints)
|
2017-09-12 19:07:43 +03:00
|
|
|
if err != nil {
|
2018-02-27 20:37:21 +03:00
|
|
|
fndgLog.Warnf("Unacceptable channel constraints: %v", err)
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, fmsg.msg.PendingChannelID, err)
|
2017-09-12 19:07:43 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// As they've accepted our channel constraints, we'll regenerate them
|
|
|
|
// here so we can properly commit their accepted constraints to the
|
|
|
|
// reservation.
|
2018-05-14 21:03:58 +03:00
|
|
|
chanReserve := f.cfg.RequiredRemoteChanReserve(resCtx.chanAmt, msg.DustLimit)
|
2017-11-29 15:59:27 +03:00
|
|
|
maxValue := f.cfg.RequiredRemoteMaxValue(resCtx.chanAmt)
|
|
|
|
maxHtlcs := f.cfg.RequiredRemoteMaxHTLCs(resCtx.chanAmt)
|
2016-12-06 17:05:46 +03:00
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// The remote node has responded with their portion of the channel
|
|
|
|
// contribution. At this point, we can process their contribution which
|
|
|
|
// allows us to construct and sign both the commitment transaction, and
|
|
|
|
// the funding transaction.
|
2017-07-31 01:53:53 +03:00
|
|
|
remoteContribution := &lnwallet.ChannelContribution{
|
|
|
|
FirstCommitmentPoint: msg.FirstCommitmentPoint,
|
|
|
|
ChannelConfig: &channeldb.ChannelConfig{
|
|
|
|
ChannelConstraints: channeldb.ChannelConstraints{
|
|
|
|
DustLimit: msg.DustLimit,
|
2017-09-12 19:07:43 +03:00
|
|
|
MaxPendingAmount: maxValue,
|
|
|
|
ChanReserve: chanReserve,
|
2018-04-05 20:48:41 +03:00
|
|
|
MinHTLC: resCtx.remoteMinHtlc,
|
2017-09-12 19:07:43 +03:00
|
|
|
MaxAcceptedHtlcs: maxHtlcs,
|
2018-12-11 00:56:41 +03:00
|
|
|
CsvDelay: resCtx.remoteCsvDelay,
|
2017-07-31 01:53:53 +03:00
|
|
|
},
|
2018-02-18 02:29:01 +03:00
|
|
|
MultiSigKey: keychain.KeyDescriptor{
|
|
|
|
PubKey: copyPubKey(msg.FundingKey),
|
|
|
|
},
|
|
|
|
RevocationBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: copyPubKey(msg.RevocationPoint),
|
|
|
|
},
|
|
|
|
PaymentBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: copyPubKey(msg.PaymentPoint),
|
|
|
|
},
|
|
|
|
DelayBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: copyPubKey(msg.DelayedPaymentPoint),
|
|
|
|
},
|
|
|
|
HtlcBasePoint: keychain.KeyDescriptor{
|
|
|
|
PubKey: copyPubKey(msg.HtlcPoint),
|
|
|
|
},
|
2017-07-31 01:53:53 +03:00
|
|
|
},
|
2019-12-03 12:38:29 +03:00
|
|
|
UpfrontShutdown: msg.UpfrontShutdownScript,
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
2017-07-31 01:53:53 +03:00
|
|
|
err = resCtx.reservation.ProcessContribution(remoteContribution)
|
|
|
|
if err != nil {
|
2016-06-21 21:31:10 +03:00
|
|
|
fndgLog.Errorf("Unable to process contribution from %v: %v",
|
2018-07-05 23:41:51 +03:00
|
|
|
peerKey, err)
|
|
|
|
f.failFundingFlow(fmsg.peer, msg.PendingChannelID, err)
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
fndgLog.Infof("pendingChan(%x): remote party proposes num_confs=%v, "+
|
2017-09-18 20:33:37 +03:00
|
|
|
"csv_delay=%v", pendingChanID[:], msg.MinAcceptDepth, msg.CsvDelay)
|
2017-09-12 19:07:43 +03:00
|
|
|
fndgLog.Debugf("Remote party accepted commitment constraints: %v",
|
|
|
|
spew.Sdump(remoteContribution.ChannelConfig.ChannelConstraints))
|
2017-07-31 01:53:53 +03:00
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// Now that we have their contribution, we can extract, then send over
|
|
|
|
// both the funding out point and our signature for their version of
|
|
|
|
// the commitment transaction to the remote peer.
|
|
|
|
outPoint := resCtx.reservation.FundingOutpoint()
|
|
|
|
_, sig := resCtx.reservation.OurSignatures()
|
|
|
|
|
2017-01-24 02:33:46 +03:00
|
|
|
// A new channel has almost finished the funding process. In order to
|
|
|
|
// properly synchronize with the writeHandler goroutine, we add a new
|
|
|
|
// channel to the barriers map which will be closed once the channel is
|
|
|
|
// fully open.
|
|
|
|
f.barrierMtx.Lock()
|
2017-04-17 01:34:23 +03:00
|
|
|
channelID := lnwire.NewChanIDFromOutPoint(outPoint)
|
|
|
|
fndgLog.Debugf("Creating chan barrier for ChanID(%v)", channelID)
|
|
|
|
f.newChanBarriers[channelID] = make(chan struct{})
|
2017-01-24 02:33:46 +03:00
|
|
|
f.barrierMtx.Unlock()
|
2016-07-13 03:38:09 +03:00
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// The next message that advances the funding flow will reference the
|
|
|
|
// channel via its permanent channel ID, so we'll set up this mapping
|
|
|
|
// so we can retrieve the reservation context once we get the
|
|
|
|
// FundingSigned message.
|
|
|
|
f.resMtx.Lock()
|
|
|
|
f.signedReservations[channelID] = pendingChanID
|
|
|
|
f.resMtx.Unlock()
|
|
|
|
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Infof("Generated ChannelPoint(%v) for pending_id(%x)", outPoint,
|
2017-09-18 20:33:37 +03:00
|
|
|
pendingChanID[:])
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
fundingCreated := &lnwire.FundingCreated{
|
|
|
|
PendingChannelID: pendingChanID,
|
|
|
|
FundingPoint: *outPoint,
|
2018-01-31 07:30:00 +03:00
|
|
|
}
|
|
|
|
fundingCreated.CommitSig, err = lnwire.NewSigFromRawSignature(sig)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to parse signature: %v", err)
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, msg.PendingChannelID, err)
|
2018-01-31 07:30:00 +03:00
|
|
|
return
|
2017-07-31 01:53:53 +03:00
|
|
|
}
|
2019-10-24 14:08:08 +03:00
|
|
|
if err := fmsg.peer.SendMessage(true, fundingCreated); err != nil {
|
2017-01-13 06:40:38 +03:00
|
|
|
fndgLog.Errorf("Unable to send funding complete message: %v", err)
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, msg.PendingChannelID, err)
|
2017-01-13 06:40:38 +03:00
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// processFundingCreated queues a funding complete message coupled with the
|
2016-06-21 21:31:10 +03:00
|
|
|
// source peer to the fundingManager.
|
2017-07-31 01:53:53 +03:00
|
|
|
func (f *fundingManager) processFundingCreated(msg *lnwire.FundingCreated,
|
2018-07-05 23:41:51 +03:00
|
|
|
peer lnpeer.Peer) {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
2017-09-28 06:06:57 +03:00
|
|
|
select {
|
2018-07-05 23:41:51 +03:00
|
|
|
case f.fundingMsgs <- &fundingCreatedMsg{msg, peer}:
|
2017-09-28 06:06:57 +03:00
|
|
|
case <-f.quit:
|
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// handleFundingCreated progresses the funding workflow when the daemon is on
|
2016-06-21 21:31:10 +03:00
|
|
|
// the responding side of a single funder workflow. Once this message has been
|
|
|
|
// processed, a signature is sent to the remote peer allowing it to broadcast
|
|
|
|
// the funding transaction, progressing the workflow into the final stage.
|
2017-07-31 01:53:53 +03:00
|
|
|
func (f *fundingManager) handleFundingCreated(fmsg *fundingCreatedMsg) {
|
2018-07-05 23:41:51 +03:00
|
|
|
peerKey := fmsg.peer.IdentityKey()
|
2017-04-17 01:34:23 +03:00
|
|
|
pendingChanID := fmsg.msg.PendingChannelID
|
2017-01-13 06:40:38 +03:00
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
resCtx, err := f.getReservationCtx(peerKey, pendingChanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
if err != nil {
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Warnf("can't find reservation (peer_id:%v, chan_id:%x)",
|
2017-09-18 20:33:37 +03:00
|
|
|
peerKey, pendingChanID[:])
|
2016-11-24 11:49:18 +03:00
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
|
|
// The channel initiator has responded with the funding outpoint of the
|
|
|
|
// final funding transaction, as well as a signature for our version of
|
|
|
|
// the commitment transaction. So at this point, we can validate the
|
2017-06-17 00:15:45 +03:00
|
|
|
// initiator's commitment transaction, then send our own if it's valid.
|
2016-06-21 21:31:10 +03:00
|
|
|
// TODO(roasbeef): make case (p vs P) consistent throughout
|
2017-07-31 01:53:53 +03:00
|
|
|
fundingOut := fmsg.msg.FundingPoint
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Infof("completing pending_id(%x) with ChannelPoint(%v)",
|
2017-09-18 20:33:37 +03:00
|
|
|
pendingChanID[:], fundingOut)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2016-11-16 23:54:27 +03:00
|
|
|
// With all the necessary data available, attempt to advance the
|
|
|
|
// funding workflow to the next stage. If this succeeds then the
|
|
|
|
// funding transaction will broadcast after our next message.
|
2017-06-08 20:48:07 +03:00
|
|
|
// CompleteReservationSingle will also mark the channel as 'IsPending'
|
|
|
|
// in the database.
|
2018-01-31 07:30:00 +03:00
|
|
|
commitSig := fmsg.msg.CommitSig.ToSignatureBytes()
|
2017-01-24 05:19:54 +03:00
|
|
|
completeChan, err := resCtx.reservation.CompleteReservationSingle(
|
2017-07-31 01:53:53 +03:00
|
|
|
&fundingOut, commitSig)
|
2016-11-16 23:54:27 +03:00
|
|
|
if err != nil {
|
2016-06-21 21:31:10 +03:00
|
|
|
// TODO(roasbeef): better error logging: peerID, channelID, etc.
|
|
|
|
fndgLog.Errorf("unable to complete single reservation: %v", err)
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, pendingChanID, err)
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-26 16:11:53 +03:00
|
|
|
// The channel is marked IsPending in the database, and can be removed
|
|
|
|
// from the set of active reservations.
|
|
|
|
f.deleteReservationCtx(peerKey, fmsg.msg.PendingChannelID)
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
// If something goes wrong before the funding transaction is confirmed,
|
|
|
|
// we use this convenience method to delete the pending OpenChannel
|
|
|
|
// from the database.
|
|
|
|
deleteFromDatabase := func() {
|
2018-08-14 05:19:58 +03:00
|
|
|
localBalance := completeChan.LocalCommitment.LocalBalance.ToSatoshis()
|
2017-06-08 20:48:07 +03:00
|
|
|
closeInfo := &channeldb.ChannelCloseSummary{
|
2018-08-14 05:16:47 +03:00
|
|
|
ChanPoint: completeChan.FundingOutpoint,
|
|
|
|
ChainHash: completeChan.ChainHash,
|
|
|
|
RemotePub: completeChan.IdentityPub,
|
|
|
|
CloseType: channeldb.FundingCanceled,
|
|
|
|
Capacity: completeChan.Capacity,
|
2018-08-14 05:19:58 +03:00
|
|
|
SettledBalance: localBalance,
|
2018-08-14 05:16:47 +03:00
|
|
|
RemoteCurrentRevocation: completeChan.RemoteCurrentRevocation,
|
|
|
|
RemoteNextRevocation: completeChan.RemoteNextRevocation,
|
|
|
|
LocalChanConfig: completeChan.LocalChanCfg,
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := completeChan.CloseChannel(closeInfo); err != nil {
|
|
|
|
fndgLog.Errorf("Failed closing channel %v: %v",
|
|
|
|
completeChan.FundingOutpoint, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 02:33:46 +03:00
|
|
|
// A new channel has almost finished the funding process. In order to
|
|
|
|
// properly synchronize with the writeHandler goroutine, we add a new
|
|
|
|
// channel to the barriers map which will be closed once the channel is
|
|
|
|
// fully open.
|
|
|
|
f.barrierMtx.Lock()
|
2017-04-17 01:34:23 +03:00
|
|
|
channelID := lnwire.NewChanIDFromOutPoint(&fundingOut)
|
|
|
|
fndgLog.Debugf("Creating chan barrier for ChanID(%v)", channelID)
|
|
|
|
f.newChanBarriers[channelID] = make(chan struct{})
|
2017-01-24 02:33:46 +03:00
|
|
|
f.barrierMtx.Unlock()
|
2016-07-13 03:38:09 +03:00
|
|
|
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Infof("sending FundingSigned for pending_id(%x) over "+
|
funding+server: ensure we cancel all reservations when a peer disconnects
In this commit, we fix an existing issue that could at times cause an
inconsistent view between the set of total coins, and the set of segwit
coins in the wallet of the node. This could be caused by initiating a
funding flow, but then the funding negotiation breaking down somewhere
along the lines. In this case, us or the other peer will disconnect.
When we initiate funding flows, we lock coins exclusively, to ensure
that concurrent funding flows don’t end up double spending the same
coin. Before this commit, we wouldn’t ever unlock those coins. As a
result, our view of available coins would be skewed.
The walletbalance call would show all the coins, but when adding the
—witness_only flag, some coins would be missing, or gone all together.
This is because the former call actually scans the txstore and manually
tallies the amount of available coins, while the latter looks at the
sent of available outputs, which is filtered based on which coins are
locked.
To remedy this, we now ensure that when a peer disconnects, we wipe all
existing reservations which will return any locked outputs to the set
of available outputs for funding flows.
2017-11-26 22:25:26 +03:00
|
|
|
"ChannelPoint(%v)", pendingChanID[:], fundingOut)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// With their signature for our version of the commitment transaction
|
|
|
|
// verified, we can now send over our signature to the remote peer.
|
|
|
|
_, sig := resCtx.reservation.OurSignatures()
|
2018-01-31 07:30:00 +03:00
|
|
|
ourCommitSig, err := lnwire.NewSigFromRawSignature(sig)
|
2017-07-31 01:53:53 +03:00
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("unable to parse signature: %v", err)
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, pendingChanID, err)
|
2017-06-08 20:48:07 +03:00
|
|
|
deleteFromDatabase()
|
2017-07-31 01:53:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fundingSigned := &lnwire.FundingSigned{
|
|
|
|
ChanID: channelID,
|
|
|
|
CommitSig: ourCommitSig,
|
|
|
|
}
|
2019-10-24 14:08:08 +03:00
|
|
|
if err := fmsg.peer.SendMessage(true, fundingSigned); err != nil {
|
2017-07-31 01:53:53 +03:00
|
|
|
fndgLog.Errorf("unable to send FundingSigned message: %v", err)
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, pendingChanID, err)
|
2017-06-08 20:48:07 +03:00
|
|
|
deleteFromDatabase()
|
2017-01-13 06:40:38 +03:00
|
|
|
return
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
|
2018-01-17 07:05:07 +03:00
|
|
|
// Now that we've sent over our final signature for this channel, we'll
|
|
|
|
// send it to the ChainArbitrator so it can watch for any on-chain
|
|
|
|
// actions during this final confirmation stage.
|
2018-07-05 23:28:57 +03:00
|
|
|
if err := f.cfg.WatchNewChannel(completeChan, peerKey); err != nil {
|
2018-02-03 04:59:33 +03:00
|
|
|
fndgLog.Errorf("Unable to send new ChannelPoint(%v) for "+
|
|
|
|
"arbitration: %v", fundingOut, err)
|
2018-01-17 07:05:07 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 00:19:39 +03:00
|
|
|
// Create an entry in the local discovery map so we can ensure that we
|
|
|
|
// process the channel confirmation fully before we receive a funding
|
|
|
|
// locked message.
|
|
|
|
f.localDiscoveryMtx.Lock()
|
|
|
|
f.localDiscoverySignals[channelID] = make(chan struct{})
|
|
|
|
f.localDiscoveryMtx.Unlock()
|
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
// At this point we have sent our last funding message to the
|
|
|
|
// initiating peer before the funding transaction will be broadcast.
|
2017-07-31 01:53:53 +03:00
|
|
|
// With this last message, our job as the responder is now complete.
|
|
|
|
// We'll wait for the funding transaction to reach the specified number
|
|
|
|
// of confirmations, then start normal operations.
|
2017-06-08 20:48:07 +03:00
|
|
|
//
|
|
|
|
// When we get to this point we have sent the signComplete message to
|
|
|
|
// the channel funder, and BOLT#2 specifies that we MUST remember the
|
|
|
|
// channel for reconnection. The channel is already marked
|
|
|
|
// as pending in the database, so in case of a disconnect or restart,
|
|
|
|
// we will continue waiting for the confirmation the next time we start
|
|
|
|
// the funding manager. In case the funding transaction never appears
|
|
|
|
// on the blockchain, we must forget this channel. We therefore
|
|
|
|
// completely forget about this channel if we haven't seen the funding
|
|
|
|
// transaction in 288 blocks (~ 48 hrs), by canceling the reservation
|
|
|
|
// and canceling the wait for the funding confirmation.
|
2017-11-14 02:55:50 +03:00
|
|
|
f.wg.Add(1)
|
2018-09-16 11:40:10 +03:00
|
|
|
go f.advanceFundingState(completeChan, pendingChanID, nil)
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// processFundingSigned sends a single funding sign complete message along with
|
|
|
|
// the source peer to the funding manager.
|
|
|
|
func (f *fundingManager) processFundingSigned(msg *lnwire.FundingSigned,
|
2018-07-05 23:41:51 +03:00
|
|
|
peer lnpeer.Peer) {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
2017-09-28 06:06:57 +03:00
|
|
|
select {
|
2018-07-05 23:41:51 +03:00
|
|
|
case f.fundingMsgs <- &fundingSignedMsg{msg, peer}:
|
2017-09-28 06:06:57 +03:00
|
|
|
case <-f.quit:
|
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// handleFundingSigned processes the final message received in a single funder
|
|
|
|
// workflow. Once this message is processed, the funding transaction is
|
2017-01-24 05:19:54 +03:00
|
|
|
// broadcast. Once the funding transaction reaches a sufficient number of
|
|
|
|
// confirmations, a message is sent to the responding peer along with a compact
|
|
|
|
// encoding of the location of the channel within the blockchain.
|
2017-07-31 01:53:53 +03:00
|
|
|
func (f *fundingManager) handleFundingSigned(fmsg *fundingSignedMsg) {
|
2018-02-07 06:13:07 +03:00
|
|
|
// As the funding signed message will reference the reservation by its
|
2017-07-31 01:53:53 +03:00
|
|
|
// permanent channel ID, we'll need to perform an intermediate look up
|
|
|
|
// before we can obtain the reservation.
|
|
|
|
f.resMtx.Lock()
|
|
|
|
pendingChanID, ok := f.signedReservations[fmsg.msg.ChanID]
|
|
|
|
delete(f.signedReservations, fmsg.msg.ChanID)
|
|
|
|
f.resMtx.Unlock()
|
|
|
|
if !ok {
|
2018-02-27 20:37:21 +03:00
|
|
|
err := fmt.Errorf("Unable to find signed reservation for "+
|
2017-09-12 18:31:17 +03:00
|
|
|
"chan_id=%x", fmsg.msg.ChanID)
|
2018-02-27 20:37:21 +03:00
|
|
|
fndgLog.Warnf(err.Error())
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, fmsg.msg.ChanID, err)
|
2017-07-31 01:53:53 +03:00
|
|
|
return
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
|
2018-07-05 23:41:51 +03:00
|
|
|
peerKey := fmsg.peer.IdentityKey()
|
|
|
|
resCtx, err := f.getReservationCtx(peerKey, pendingChanID)
|
2017-01-24 05:19:54 +03:00
|
|
|
if err != nil {
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Warnf("Unable to find reservation (peer_id:%v, "+
|
|
|
|
"chan_id:%x)", peerKey, pendingChanID[:])
|
2018-02-27 20:37:21 +03:00
|
|
|
// TODO: add ErrChanNotFound?
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, pendingChanID, err)
|
2017-01-24 05:19:54 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-17 00:19:39 +03:00
|
|
|
// Create an entry in the local discovery map so we can ensure that we
|
|
|
|
// process the channel confirmation fully before we receive a funding
|
|
|
|
// locked message.
|
|
|
|
fundingPoint := resCtx.reservation.FundingOutpoint()
|
|
|
|
permChanID := lnwire.NewChanIDFromOutPoint(fundingPoint)
|
|
|
|
f.localDiscoveryMtx.Lock()
|
|
|
|
f.localDiscoverySignals[permChanID] = make(chan struct{})
|
|
|
|
f.localDiscoveryMtx.Unlock()
|
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
// The remote peer has responded with a signature for our commitment
|
|
|
|
// transaction. We'll verify the signature for validity, then commit
|
|
|
|
// the state to disk as we can now open the channel.
|
2018-01-31 07:30:00 +03:00
|
|
|
commitSig := fmsg.msg.CommitSig.ToSignatureBytes()
|
2018-07-26 16:16:47 +03:00
|
|
|
completeChan, err := resCtx.reservation.CompleteReservation(
|
|
|
|
nil, commitSig,
|
|
|
|
)
|
2017-01-24 05:19:54 +03:00
|
|
|
if err != nil {
|
2018-07-26 16:16:47 +03:00
|
|
|
fndgLog.Errorf("Unable to complete reservation sign "+
|
|
|
|
"complete: %v", err)
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(fmsg.peer, pendingChanID, err)
|
2017-01-24 05:19:54 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-26 16:11:53 +03:00
|
|
|
// The channel is now marked IsPending in the database, and we can
|
|
|
|
// delete it from our set of active reservations.
|
|
|
|
f.deleteReservationCtx(peerKey, pendingChanID)
|
|
|
|
|
2019-11-01 06:56:50 +03:00
|
|
|
// Broadcast the finalized funding transaction to the network, but only
|
|
|
|
// if we actually have the funding transaction.
|
|
|
|
if completeChan.ChanType.HasFundingTx() {
|
|
|
|
fundingTx := completeChan.FundingTxn
|
2018-07-26 16:16:47 +03:00
|
|
|
|
2019-11-01 06:56:50 +03:00
|
|
|
fndgLog.Infof("Broadcasting funding tx for ChannelPoint(%v): %v",
|
|
|
|
completeChan.FundingOutpoint, spew.Sdump(fundingTx))
|
|
|
|
|
|
|
|
err = f.cfg.PublishTransaction(fundingTx)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to broadcast funding tx for "+
|
|
|
|
"ChannelPoint(%v): %v",
|
|
|
|
completeChan.FundingOutpoint, err)
|
|
|
|
|
|
|
|
// We failed to broadcast the funding transaction, but
|
|
|
|
// watch the channel regardless, in case the
|
|
|
|
// transaction made it to the network. We will retry
|
|
|
|
// broadcast at startup.
|
|
|
|
//
|
|
|
|
// TODO(halseth): retry more often? Handle with CPFP?
|
|
|
|
// Just delete from the DB?
|
|
|
|
}
|
2018-07-26 16:16:47 +03:00
|
|
|
}
|
|
|
|
|
2018-01-17 07:05:07 +03:00
|
|
|
// Now that we have a finalized reservation for this funding flow,
|
|
|
|
// we'll send the to be active channel to the ChainArbitrator so it can
|
2019-05-10 18:14:19 +03:00
|
|
|
// watch for any on-chain actions before the channel has fully
|
2018-01-17 07:05:07 +03:00
|
|
|
// confirmed.
|
2018-07-05 23:28:57 +03:00
|
|
|
if err := f.cfg.WatchNewChannel(completeChan, peerKey); err != nil {
|
2018-01-29 01:47:08 +03:00
|
|
|
fndgLog.Errorf("Unable to send new ChannelPoint(%v) for "+
|
2018-02-03 04:59:33 +03:00
|
|
|
"arbitration: %v", fundingPoint, err)
|
2018-01-17 07:05:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Infof("Finalizing pending_id(%x) over ChannelPoint(%v), "+
|
2018-07-05 23:41:51 +03:00
|
|
|
"waiting for channel open on-chain", pendingChanID[:],
|
|
|
|
fundingPoint)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
|
|
// Send an update to the upstream client that the negotiation process
|
|
|
|
// is over.
|
|
|
|
// TODO(roasbeef): add abstraction over updates to accommodate
|
|
|
|
// long-polling, or SSE, etc.
|
2018-08-22 10:32:42 +03:00
|
|
|
upd := &lnrpc.OpenStatusUpdate{
|
2017-01-24 05:19:54 +03:00
|
|
|
Update: &lnrpc.OpenStatusUpdate_ChanPending{
|
|
|
|
ChanPending: &lnrpc.PendingUpdate{
|
|
|
|
Txid: fundingPoint.Hash[:],
|
|
|
|
OutputIndex: fundingPoint.Index,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-22 10:32:42 +03:00
|
|
|
select {
|
|
|
|
case resCtx.updates <- upd:
|
|
|
|
case <-f.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
// At this point we have broadcast the funding transaction and done all
|
2018-07-26 16:11:53 +03:00
|
|
|
// necessary processing.
|
2017-09-17 22:34:59 +03:00
|
|
|
f.wg.Add(1)
|
2018-09-16 11:40:10 +03:00
|
|
|
go f.advanceFundingState(completeChan, pendingChanID, resCtx.updates)
|
2017-01-24 05:19:54 +03:00
|
|
|
}
|
|
|
|
|
2019-10-01 05:59:10 +03:00
|
|
|
// confirmedChannel wraps a confirmed funding transaction, as well as the short
|
|
|
|
// channel ID which identifies that channel into a single struct. We'll use
|
|
|
|
// this to pass around the final state of a channel after it has been
|
|
|
|
// confirmed.
|
|
|
|
type confirmedChannel struct {
|
|
|
|
// shortChanID expresses where in the block the funding transaction was
|
|
|
|
// located.
|
|
|
|
shortChanID lnwire.ShortChannelID
|
|
|
|
|
|
|
|
// fundingTx is the funding transaction that created the channel.
|
|
|
|
fundingTx *wire.MsgTx
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// waitForFundingWithTimeout is a wrapper around waitForFundingConfirmation and
|
|
|
|
// waitForTimeout that will return ErrConfirmationTimeout if we are not the
|
|
|
|
// channel initiator and the maxWaitNumBlocksFundingConf has passed from the
|
|
|
|
// funding broadcast height. In case of confirmation, the short channel ID of
|
2019-10-01 05:59:10 +03:00
|
|
|
// the channel and the funding transaction will be returned.
|
2018-09-16 11:40:10 +03:00
|
|
|
func (f *fundingManager) waitForFundingWithTimeout(
|
2019-10-01 05:59:10 +03:00
|
|
|
ch *channeldb.OpenChannel) (*confirmedChannel, error) {
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2019-10-01 05:59:10 +03:00
|
|
|
confChan := make(chan *confirmedChannel)
|
2018-09-16 11:40:10 +03:00
|
|
|
timeoutChan := make(chan error, 1)
|
2017-06-08 20:48:07 +03:00
|
|
|
cancelChan := make(chan struct{})
|
|
|
|
|
|
|
|
f.wg.Add(1)
|
2018-09-16 11:40:10 +03:00
|
|
|
go f.waitForFundingConfirmation(ch, cancelChan, confChan)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// If we are not the initiator, we have no money at stake and will
|
|
|
|
// timeout waiting for the funding transaction to confirm after a
|
|
|
|
// while.
|
|
|
|
if !ch.IsInitiator {
|
|
|
|
f.wg.Add(1)
|
|
|
|
go f.waitForTimeout(ch, cancelChan, timeoutChan)
|
|
|
|
}
|
|
|
|
defer close(cancelChan)
|
2018-02-23 03:13:05 +03:00
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
select {
|
|
|
|
case err := <-timeoutChan:
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, ErrConfirmationTimeout
|
2018-02-23 03:13:05 +03:00
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
case <-f.quit:
|
|
|
|
// The fundingManager is shutting down, and will resume wait on
|
|
|
|
// startup.
|
|
|
|
return nil, ErrFundingManagerShuttingDown
|
2017-09-17 22:34:59 +03:00
|
|
|
|
2019-10-01 05:59:10 +03:00
|
|
|
case confirmedChannel, ok := <-confChan:
|
2018-09-16 11:40:10 +03:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("waiting for funding" +
|
|
|
|
"confirmation failed")
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
2019-10-01 05:59:10 +03:00
|
|
|
return confirmedChannel, nil
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 08:16:34 +03:00
|
|
|
// makeFundingScript re-creates the funding script for the funding transaction
|
|
|
|
// of the target channel.
|
|
|
|
func makeFundingScript(channel *channeldb.OpenChannel) ([]byte, error) {
|
|
|
|
localKey := channel.LocalChanCfg.MultiSigKey.PubKey.SerializeCompressed()
|
|
|
|
remoteKey := channel.RemoteChanCfg.MultiSigKey.PubKey.SerializeCompressed()
|
|
|
|
|
2019-01-16 17:47:43 +03:00
|
|
|
multiSigScript, err := input.GenMultiSigScript(localKey, remoteKey)
|
2018-05-31 08:16:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-16 17:47:43 +03:00
|
|
|
return input.WitnessScriptHash(multiSigScript)
|
2018-05-31 08:16:34 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
// waitForFundingConfirmation handles the final stages of the channel funding
|
|
|
|
// process once the funding transaction has been broadcast. The primary
|
|
|
|
// function of waitForFundingConfirmation is to wait for blockchain
|
|
|
|
// confirmation, and then to notify the other systems that must be notified
|
|
|
|
// when a channel has become active for lightning transactions.
|
2017-09-17 22:34:59 +03:00
|
|
|
// The wait can be canceled by closing the cancelChan. In case of success,
|
|
|
|
// a *lnwire.ShortChannelID will be passed to confChan.
|
2018-09-16 11:40:10 +03:00
|
|
|
//
|
|
|
|
// NOTE: This MUST be run as a goroutine.
|
2019-07-11 14:14:38 +03:00
|
|
|
func (f *fundingManager) waitForFundingConfirmation(
|
|
|
|
completeChan *channeldb.OpenChannel, cancelChan <-chan struct{},
|
2019-10-01 05:59:10 +03:00
|
|
|
confChan chan<- *confirmedChannel) {
|
2017-01-24 05:19:54 +03:00
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
defer f.wg.Done()
|
2017-09-17 22:34:59 +03:00
|
|
|
defer close(confChan)
|
2017-02-25 03:24:10 +03:00
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
// Register with the ChainNotifier for a notification once the funding
|
|
|
|
// transaction reaches `numConfs` confirmations.
|
|
|
|
txid := completeChan.FundingOutpoint.Hash
|
2018-05-31 08:16:34 +03:00
|
|
|
fundingScript, err := makeFundingScript(completeChan)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("unable to create funding script for "+
|
2019-07-11 14:14:38 +03:00
|
|
|
"ChannelPoint(%v): %v", completeChan.FundingOutpoint,
|
|
|
|
err)
|
2018-05-31 08:16:34 +03:00
|
|
|
return
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
numConfs := uint32(completeChan.NumConfsRequired)
|
2018-05-31 08:16:34 +03:00
|
|
|
confNtfn, err := f.cfg.Notifier.RegisterConfirmationsNtfn(
|
2019-07-11 14:14:38 +03:00
|
|
|
&txid, fundingScript, numConfs,
|
|
|
|
completeChan.FundingBroadcastHeight,
|
2018-05-31 08:16:34 +03:00
|
|
|
)
|
2017-04-17 01:34:23 +03:00
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to register for confirmation of "+
|
2019-07-11 14:14:38 +03:00
|
|
|
"ChannelPoint(%v): %v", completeChan.FundingOutpoint,
|
|
|
|
err)
|
2017-04-17 01:34:23 +03:00
|
|
|
return
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
|
|
fndgLog.Infof("Waiting for funding tx (%v) to reach %v confirmations",
|
|
|
|
txid, numConfs)
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
var confDetails *chainntnfs.TxConfirmation
|
|
|
|
var ok bool
|
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
// Wait until the specified number of confirmations has been reached,
|
2017-06-08 20:48:07 +03:00
|
|
|
// we get a cancel signal, or the wallet signals a shutdown.
|
|
|
|
select {
|
|
|
|
case confDetails, ok = <-confNtfn.Confirmed:
|
|
|
|
// fallthrough
|
2019-07-11 14:14:38 +03:00
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
case <-cancelChan:
|
|
|
|
fndgLog.Warnf("canceled waiting for funding confirmation, "+
|
|
|
|
"stopping funding flow for ChannelPoint(%v)",
|
|
|
|
completeChan.FundingOutpoint)
|
|
|
|
return
|
2019-07-11 14:14:38 +03:00
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
case <-f.quit:
|
|
|
|
fndgLog.Warnf("fundingManager shutting down, stopping funding "+
|
2019-07-11 14:14:38 +03:00
|
|
|
"flow for ChannelPoint(%v)",
|
|
|
|
completeChan.FundingOutpoint)
|
2017-06-08 20:48:07 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-25 03:26:39 +03:00
|
|
|
if !ok {
|
2017-04-17 01:34:23 +03:00
|
|
|
fndgLog.Warnf("ChainNotifier shutting down, cannot complete "+
|
2017-02-25 03:26:39 +03:00
|
|
|
"funding flow for ChannelPoint(%v)",
|
|
|
|
completeChan.FundingOutpoint)
|
|
|
|
return
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
fundingPoint := completeChan.FundingOutpoint
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Infof("ChannelPoint(%v) is now active: ChannelID(%v)",
|
2019-10-01 05:59:10 +03:00
|
|
|
fundingPoint, lnwire.NewChanIDFromOutPoint(&fundingPoint))
|
2017-04-17 01:34:23 +03:00
|
|
|
|
2017-06-17 00:15:45 +03:00
|
|
|
// With the block height and the transaction index known, we can
|
|
|
|
// construct the compact chanID which is used on the network to unique
|
|
|
|
// identify channels.
|
|
|
|
shortChanID := lnwire.ShortChannelID{
|
|
|
|
BlockHeight: confDetails.BlockHeight,
|
|
|
|
TxIndex: confDetails.TxIndex,
|
|
|
|
TxPosition: uint16(fundingPoint.Index),
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
select {
|
2019-10-01 05:59:10 +03:00
|
|
|
case confChan <- &confirmedChannel{
|
|
|
|
shortChanID: shortChanID,
|
|
|
|
fundingTx: confDetails.Tx,
|
|
|
|
}:
|
2018-09-16 11:40:10 +03:00
|
|
|
case <-f.quit:
|
2017-01-24 05:19:54 +03:00
|
|
|
return
|
|
|
|
}
|
2018-09-16 11:40:10 +03:00
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// waitForTimeout will close the timeout channel if maxWaitNumBlocksFundingConf
|
|
|
|
// has passed from the broadcast height of the given channel. In case of error,
|
|
|
|
// the error is sent on timeoutChan. The wait can be canceled by closing the
|
|
|
|
// cancelChan.
|
|
|
|
//
|
|
|
|
// NOTE: timeoutChan MUST be buffered.
|
|
|
|
// NOTE: This MUST be run as a goroutine.
|
|
|
|
func (f *fundingManager) waitForTimeout(completeChan *channeldb.OpenChannel,
|
|
|
|
cancelChan <-chan struct{}, timeoutChan chan<- error) {
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
|
|
|
epochClient, err := f.cfg.Notifier.RegisterBlockEpochNtfn(nil)
|
|
|
|
if err != nil {
|
|
|
|
timeoutChan <- fmt.Errorf("unable to register for epoch "+
|
|
|
|
"notification: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer epochClient.Cancel()
|
|
|
|
|
|
|
|
// On block maxHeight we will cancel the funding confirmation wait.
|
|
|
|
maxHeight := completeChan.FundingBroadcastHeight + maxWaitNumBlocksFundingConf
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case epoch, ok := <-epochClient.Epochs:
|
|
|
|
if !ok {
|
|
|
|
timeoutChan <- fmt.Errorf("epoch client " +
|
|
|
|
"shutting down")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the timeout channel and exit if the block is
|
|
|
|
// aboce the max height.
|
|
|
|
if uint32(epoch.Height) >= maxHeight {
|
|
|
|
fndgLog.Warnf("Waited for %v blocks without "+
|
|
|
|
"seeing funding transaction confirmed,"+
|
|
|
|
" cancelling.",
|
|
|
|
maxWaitNumBlocksFundingConf)
|
|
|
|
|
|
|
|
// Notify the caller of the timeout.
|
|
|
|
close(timeoutChan)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: If we are the channel initiator implement
|
|
|
|
// a method for recovering the funds from the funding
|
|
|
|
// transaction
|
|
|
|
|
|
|
|
case <-cancelChan:
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-f.quit:
|
|
|
|
// The fundingManager is shutting down, will resume
|
|
|
|
// waiting for the funding transaction on startup.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:10 +03:00
|
|
|
// handleFundingConfirmation marks a channel as open in the database, and set
|
|
|
|
// the channelOpeningState markedOpen. In addition it will report the now
|
|
|
|
// decided short channel ID to the switch, and close the local discovery signal
|
|
|
|
// for this channel.
|
|
|
|
func (f *fundingManager) handleFundingConfirmation(
|
|
|
|
completeChan *channeldb.OpenChannel,
|
2019-10-01 05:59:10 +03:00
|
|
|
confChannel *confirmedChannel) error {
|
2018-09-16 11:40:10 +03:00
|
|
|
|
|
|
|
fundingPoint := completeChan.FundingOutpoint
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&fundingPoint)
|
2019-01-17 01:35:30 +03:00
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// TODO(roasbeef): ideally persistent state update for chan above
|
|
|
|
// should be abstracted
|
|
|
|
|
2019-10-01 05:59:10 +03:00
|
|
|
// Now that that the channel has been fully confirmed, we'll request
|
|
|
|
// that the wallet fully verify this channel to ensure that it can be
|
|
|
|
// used.
|
|
|
|
err := f.cfg.Wallet.ValidateChannel(completeChan, confChannel.fundingTx)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(roasbeef): delete chan state?
|
|
|
|
return fmt.Errorf("unable to validate channel: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
// The funding transaction now being confirmed, we add this channel to
|
2018-02-04 05:14:09 +03:00
|
|
|
// the fundingManager's internal persistent state machine that we use
|
|
|
|
// to track the remaining process of the channel opening. This is
|
2018-09-16 11:40:11 +03:00
|
|
|
// useful to resume the opening process in case of restarts. We set the
|
|
|
|
// opening state before we mark the channel opened in the database,
|
|
|
|
// such that we can receover from one of the db writes failing.
|
2019-10-01 05:59:10 +03:00
|
|
|
err = f.saveChannelOpeningState(
|
|
|
|
&fundingPoint, markedOpen, &confChannel.shortChanID,
|
|
|
|
)
|
2017-06-08 20:48:07 +03:00
|
|
|
if err != nil {
|
2018-09-16 11:40:10 +03:00
|
|
|
return fmt.Errorf("error setting channel state to markedOpen: %v",
|
2017-06-08 20:48:07 +03:00
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2018-09-16 11:40:11 +03:00
|
|
|
// Now that the channel has been fully confirmed and we successfully
|
|
|
|
// saved the opening state, we'll mark it as open within the database.
|
2019-10-01 05:59:10 +03:00
|
|
|
err = completeChan.MarkAsOpen(confChannel.shortChanID)
|
|
|
|
if err != nil {
|
2018-09-16 11:40:11 +03:00
|
|
|
return fmt.Errorf("error setting channel pending flag to false: "+
|
|
|
|
"%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inform the ChannelNotifier that the channel has transitioned from
|
|
|
|
// pending open to open.
|
|
|
|
f.cfg.NotifyOpenChannelEvent(completeChan.FundingOutpoint)
|
|
|
|
|
2018-02-04 05:14:09 +03:00
|
|
|
// As there might already be an active link in the switch with an
|
2018-05-08 07:19:45 +03:00
|
|
|
// outdated short chan ID, we'll instruct the switch to load the updated
|
|
|
|
// short chan id from disk.
|
|
|
|
err = f.cfg.ReportShortChanID(fundingPoint)
|
2018-02-04 05:14:09 +03:00
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("unable to report short chan id: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-14 04:20:05 +03:00
|
|
|
// Close the discoverySignal channel, indicating to a separate
|
|
|
|
// goroutine that the channel now is marked as open in the database
|
|
|
|
// and that it is acceptable to process funding locked messages
|
2017-11-14 02:55:50 +03:00
|
|
|
// from the peer.
|
|
|
|
f.localDiscoveryMtx.Lock()
|
|
|
|
if discoverySignal, ok := f.localDiscoverySignals[chanID]; ok {
|
|
|
|
close(discoverySignal)
|
|
|
|
}
|
|
|
|
f.localDiscoveryMtx.Unlock()
|
2017-09-17 22:34:59 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendFundingLocked creates and sends the fundingLocked message.
|
|
|
|
// This should be called after the funding transaction has been confirmed,
|
|
|
|
// and the channelState is 'markedOpen'.
|
2019-09-18 12:27:34 +03:00
|
|
|
func (f *fundingManager) sendFundingLocked(
|
2018-07-05 23:41:51 +03:00
|
|
|
completeChan *channeldb.OpenChannel, channel *lnwallet.LightningChannel,
|
2017-09-17 22:34:59 +03:00
|
|
|
shortChanID *lnwire.ShortChannelID) error {
|
|
|
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&completeChan.FundingOutpoint)
|
2019-05-31 00:37:30 +03:00
|
|
|
|
|
|
|
var peerKey [33]byte
|
|
|
|
copy(peerKey[:], completeChan.IdentityPub.SerializeCompressed())
|
2017-01-24 05:19:54 +03:00
|
|
|
|
2017-04-17 01:39:14 +03:00
|
|
|
// Next, we'll send over the funding locked message which marks that we
|
|
|
|
// consider the channel open by presenting the remote party with our
|
|
|
|
// next revocation key. Without the revocation key, the remote party
|
|
|
|
// will be unable to propose state transitions.
|
2017-07-31 04:22:38 +03:00
|
|
|
nextRevocation, err := channel.NextRevocationKey()
|
2017-01-24 05:19:54 +03:00
|
|
|
if err != nil {
|
2017-09-17 22:34:59 +03:00
|
|
|
return fmt.Errorf("unable to create next revocation: %v", err)
|
2017-02-25 03:24:10 +03:00
|
|
|
}
|
2017-04-17 01:39:14 +03:00
|
|
|
fundingLockedMsg := lnwire.NewFundingLocked(chanID, nextRevocation)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
2017-09-25 01:01:05 +03:00
|
|
|
// If the peer has disconnected before we reach this point, we will need
|
|
|
|
// to wait for him to come back online before sending the fundingLocked
|
|
|
|
// message. This is special for fundingLocked, since failing to send any
|
|
|
|
// of the previous messages in the funding flow just cancels the flow.
|
|
|
|
// But now the funding transaction is confirmed, the channel is open
|
|
|
|
// and we have to make sure the peer gets the fundingLocked message when
|
|
|
|
// it comes back online. This is also crucial during restart of lnd,
|
|
|
|
// where we might try to resend the fundingLocked message before the
|
|
|
|
// server has had the time to connect to the peer. We keep trying to
|
|
|
|
// send fundingLocked until we succeed, or the fundingManager is shut
|
|
|
|
// down.
|
|
|
|
for {
|
2019-09-18 12:27:34 +03:00
|
|
|
connected := make(chan lnpeer.Peer, 1)
|
|
|
|
f.cfg.NotifyWhenOnline(peerKey, connected)
|
|
|
|
|
|
|
|
var peer lnpeer.Peer
|
|
|
|
select {
|
|
|
|
case peer = <-connected:
|
|
|
|
case <-f.quit:
|
|
|
|
return ErrFundingManagerShuttingDown
|
|
|
|
}
|
|
|
|
|
|
|
|
fndgLog.Infof("Peer(%x) is online, sending FundingLocked "+
|
|
|
|
"for ChannelID(%v)", peerKey, chanID)
|
2018-07-05 23:41:51 +03:00
|
|
|
|
2019-10-24 14:08:08 +03:00
|
|
|
if err := peer.SendMessage(true, fundingLockedMsg); err == nil {
|
2018-07-05 23:41:51 +03:00
|
|
|
// Sending succeeded, we can break out and continue the
|
|
|
|
// funding flow.
|
2017-09-25 01:01:05 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-07-05 23:41:51 +03:00
|
|
|
fndgLog.Warnf("Unable to send fundingLocked to peer %x: %v. "+
|
2019-05-31 00:37:30 +03:00
|
|
|
"Will retry when online", peerKey, err)
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2017-09-17 22:34:59 +03:00
|
|
|
return nil
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2017-11-14 04:20:05 +03:00
|
|
|
// addToRouterGraph sends a ChannelAnnouncement and a ChannelUpdate to the
|
|
|
|
// gossiper so that the channel is added to the Router's internal graph.
|
|
|
|
// These announcement messages are NOT broadcasted to the greater network,
|
|
|
|
// only to the channel counter party. The proofs required to announce the
|
|
|
|
// channel to the greater network will be created and sent in annAfterSixConfs.
|
2017-11-14 02:55:50 +03:00
|
|
|
func (f *fundingManager) addToRouterGraph(completeChan *channeldb.OpenChannel,
|
2017-11-14 04:20:05 +03:00
|
|
|
shortChanID *lnwire.ShortChannelID) error {
|
2017-11-14 02:55:50 +03:00
|
|
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&completeChan.FundingOutpoint)
|
|
|
|
|
2018-08-22 10:32:43 +03:00
|
|
|
// We'll obtain the min HTLC value we can forward in our direction, as
|
|
|
|
// we'll use this value within our ChannelUpdate. This constraint is
|
|
|
|
// originally set by the remote node, as it will be the one that will
|
|
|
|
// need to determine the smallest HTLC it deems economically relevant.
|
|
|
|
fwdMinHTLC := completeChan.LocalChanCfg.MinHTLC
|
2017-11-14 04:20:05 +03:00
|
|
|
|
2019-01-12 20:59:45 +03:00
|
|
|
// We'll obtain the max HTLC value we can forward in our direction, as
|
|
|
|
// we'll use this value within our ChannelUpdate. This value must be <=
|
2019-01-30 11:05:01 +03:00
|
|
|
// channel capacity and <= the maximum in-flight msats set by the peer.
|
2019-01-12 20:59:45 +03:00
|
|
|
fwdMaxHTLC := completeChan.LocalChanCfg.MaxPendingAmount
|
2019-01-30 11:05:01 +03:00
|
|
|
capacityMSat := lnwire.NewMSatFromSatoshis(completeChan.Capacity)
|
|
|
|
if fwdMaxHTLC > capacityMSat {
|
|
|
|
fwdMaxHTLC = capacityMSat
|
|
|
|
}
|
2019-01-12 20:59:45 +03:00
|
|
|
|
2018-02-18 02:29:01 +03:00
|
|
|
ann, err := f.newChanAnnouncement(
|
|
|
|
f.cfg.IDKey, completeChan.IdentityPub,
|
|
|
|
completeChan.LocalChanCfg.MultiSigKey.PubKey,
|
|
|
|
completeChan.RemoteChanCfg.MultiSigKey.PubKey, *shortChanID,
|
2019-01-12 20:59:45 +03:00
|
|
|
chanID, fwdMinHTLC, fwdMaxHTLC,
|
2017-11-14 04:20:05 +03:00
|
|
|
)
|
2017-11-14 02:55:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating channel "+
|
|
|
|
"announcement: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send ChannelAnnouncement and ChannelUpdate to the gossiper to add
|
|
|
|
// to the Router's topology.
|
2019-04-17 23:26:11 +03:00
|
|
|
errChan := f.cfg.SendAnnouncement(
|
|
|
|
ann.chanAnn, discovery.ChannelCapacity(completeChan.Capacity),
|
|
|
|
discovery.ChannelPoint(completeChan.FundingOutpoint),
|
|
|
|
)
|
2018-08-20 15:28:11 +03:00
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
if err != nil {
|
|
|
|
if routing.IsError(err, routing.ErrOutdated,
|
|
|
|
routing.ErrIgnored) {
|
|
|
|
fndgLog.Debugf("Router rejected "+
|
|
|
|
"ChannelAnnouncement: %v", err)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("error sending channel "+
|
|
|
|
"announcement: %v", err)
|
|
|
|
}
|
2017-11-14 04:20:05 +03:00
|
|
|
}
|
2018-08-20 15:28:11 +03:00
|
|
|
case <-f.quit:
|
|
|
|
return ErrFundingManagerShuttingDown
|
2017-11-14 02:55:50 +03:00
|
|
|
}
|
2018-08-20 15:28:11 +03:00
|
|
|
|
|
|
|
errChan = f.cfg.SendAnnouncement(ann.chanUpdateAnn)
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
if err != nil {
|
|
|
|
if routing.IsError(err, routing.ErrOutdated,
|
|
|
|
routing.ErrIgnored) {
|
|
|
|
fndgLog.Debugf("Router rejected "+
|
|
|
|
"ChannelUpdate: %v", err)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("error sending channel "+
|
|
|
|
"update: %v", err)
|
|
|
|
}
|
2017-11-14 04:20:05 +03:00
|
|
|
}
|
2018-08-20 15:28:11 +03:00
|
|
|
case <-f.quit:
|
|
|
|
return ErrFundingManagerShuttingDown
|
2017-11-14 02:55:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// annAfterSixConfs broadcasts the necessary channel announcement messages to
|
|
|
|
// the network after 6 confs. Should be called after the fundingLocked message
|
|
|
|
// is sent and the channel is added to the router graph (channelState is
|
2017-11-14 04:20:05 +03:00
|
|
|
// 'addedToRouterGraph') and the channel is ready to be used. This is the last
|
|
|
|
// step in the channel opening process, and the opening state will be deleted
|
|
|
|
// from the database if successful.
|
2017-11-14 02:55:50 +03:00
|
|
|
func (f *fundingManager) annAfterSixConfs(completeChan *channeldb.OpenChannel,
|
2017-11-23 22:38:52 +03:00
|
|
|
shortChanID *lnwire.ShortChannelID) error {
|
2017-09-17 22:34:59 +03:00
|
|
|
|
2018-10-18 01:59:02 +03:00
|
|
|
// If this channel is not meant to be announced to the greater network,
|
|
|
|
// we'll only send our NodeAnnouncement to our counterparty to ensure we
|
|
|
|
// don't leak any of our information.
|
2017-11-14 04:20:05 +03:00
|
|
|
announceChan := completeChan.ChannelFlags&lnwire.FFAnnounceChannel != 0
|
|
|
|
if !announceChan {
|
|
|
|
fndgLog.Debugf("Will not announce private channel %v.",
|
|
|
|
shortChanID.ToUint64())
|
2018-10-18 01:59:02 +03:00
|
|
|
|
|
|
|
peerChan := make(chan lnpeer.Peer, 1)
|
2019-05-31 00:37:30 +03:00
|
|
|
|
|
|
|
var peerKey [33]byte
|
|
|
|
copy(peerKey[:], completeChan.IdentityPub.SerializeCompressed())
|
|
|
|
|
|
|
|
f.cfg.NotifyWhenOnline(peerKey, peerChan)
|
2018-10-18 01:59:02 +03:00
|
|
|
|
|
|
|
var peer lnpeer.Peer
|
|
|
|
select {
|
|
|
|
case peer = <-peerChan:
|
|
|
|
case <-f.quit:
|
|
|
|
return ErrFundingManagerShuttingDown
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeAnn, err := f.cfg.CurrentNodeAnnouncement()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to retrieve current node "+
|
|
|
|
"announcement: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(
|
|
|
|
&completeChan.FundingOutpoint,
|
|
|
|
)
|
|
|
|
pubKey := peer.PubKey()
|
|
|
|
fndgLog.Debugf("Sending our NodeAnnouncement for "+
|
|
|
|
"ChannelID(%v) to %x", chanID, pubKey)
|
|
|
|
|
2019-09-12 16:05:52 +03:00
|
|
|
// TODO(halseth): make reliable. If the peer is not online this
|
|
|
|
// will fail, and the opening process will stop. Should instead
|
|
|
|
// block here, waiting for the peer to come online.
|
2018-10-18 01:59:02 +03:00
|
|
|
if err := peer.SendMessage(true, &nodeAnn); err != nil {
|
|
|
|
return fmt.Errorf("unable to send node announcement "+
|
|
|
|
"to peer %x: %v", pubKey, err)
|
|
|
|
}
|
2017-11-14 04:20:05 +03:00
|
|
|
} else {
|
2018-10-18 01:59:02 +03:00
|
|
|
// Otherwise, we'll wait until the funding transaction has
|
|
|
|
// reached 6 confirmations before announcing it.
|
2017-11-14 04:20:05 +03:00
|
|
|
numConfs := uint32(completeChan.NumConfsRequired)
|
|
|
|
if numConfs < 6 {
|
|
|
|
numConfs = 6
|
|
|
|
}
|
|
|
|
txid := completeChan.FundingOutpoint.Hash
|
|
|
|
fndgLog.Debugf("Will announce channel %v after ChannelPoint"+
|
|
|
|
"(%v) has gotten %d confirmations",
|
|
|
|
shortChanID.ToUint64(), completeChan.FundingOutpoint,
|
|
|
|
numConfs)
|
|
|
|
|
2018-05-31 08:16:34 +03:00
|
|
|
fundingScript, err := makeFundingScript(completeChan)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create funding script for "+
|
|
|
|
"ChannelPoint(%v): %v",
|
|
|
|
completeChan.FundingOutpoint, err)
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:59:02 +03:00
|
|
|
// Register with the ChainNotifier for a notification once the
|
|
|
|
// funding transaction reaches at least 6 confirmations.
|
2018-05-31 08:16:34 +03:00
|
|
|
confNtfn, err := f.cfg.Notifier.RegisterConfirmationsNtfn(
|
2018-10-18 01:59:02 +03:00
|
|
|
&txid, fundingScript, numConfs,
|
|
|
|
completeChan.FundingBroadcastHeight,
|
2018-05-31 08:16:34 +03:00
|
|
|
)
|
2017-11-14 04:20:05 +03:00
|
|
|
if err != nil {
|
2018-07-06 04:33:28 +03:00
|
|
|
return fmt.Errorf("Unable to register for "+
|
|
|
|
"confirmation of ChannelPoint(%v): %v",
|
|
|
|
completeChan.FundingOutpoint, err)
|
2017-11-14 04:20:05 +03:00
|
|
|
}
|
2017-11-14 02:55:50 +03:00
|
|
|
|
2018-07-06 04:33:28 +03:00
|
|
|
// Wait until 6 confirmations has been reached or the wallet
|
|
|
|
// signals a shutdown.
|
2017-11-14 04:20:05 +03:00
|
|
|
select {
|
|
|
|
case _, ok := <-confNtfn.Confirmed:
|
|
|
|
if !ok {
|
2018-07-06 04:33:28 +03:00
|
|
|
return fmt.Errorf("ChainNotifier shutting "+
|
|
|
|
"down, cannot complete funding flow "+
|
|
|
|
"for ChannelPoint(%v)",
|
2017-11-14 04:20:05 +03:00
|
|
|
completeChan.FundingOutpoint)
|
|
|
|
}
|
|
|
|
// Fallthrough.
|
|
|
|
|
|
|
|
case <-f.quit:
|
2018-07-06 04:33:28 +03:00
|
|
|
return fmt.Errorf("%v, stopping funding flow for "+
|
|
|
|
"ChannelPoint(%v)",
|
|
|
|
ErrFundingManagerShuttingDown,
|
|
|
|
completeChan.FundingOutpoint)
|
2017-11-14 02:55:50 +03:00
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2017-11-14 04:20:05 +03:00
|
|
|
fundingPoint := completeChan.FundingOutpoint
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&fundingPoint)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2017-11-14 04:20:05 +03:00
|
|
|
fndgLog.Infof("Announcing ChannelPoint(%v), short_chan_id=%v",
|
2019-02-06 13:50:38 +03:00
|
|
|
&fundingPoint, shortChanID)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
2018-08-22 10:32:43 +03:00
|
|
|
// We'll obtain the min HTLC value we can forward in our
|
|
|
|
// direction, as we'll use this value within our ChannelUpdate.
|
|
|
|
// This constraint is originally set by the remote node, as it
|
|
|
|
// will be the one that will need to determine the smallest
|
|
|
|
// HTLC it deems economically relevant.
|
|
|
|
fwdMinHTLC := completeChan.LocalChanCfg.MinHTLC
|
2017-12-01 09:52:28 +03:00
|
|
|
|
2019-01-30 11:05:01 +03:00
|
|
|
// We'll obtain the max HTLC value we can forward in our
|
|
|
|
// direction, as we'll use this value within our ChannelUpdate.
|
|
|
|
// This value must be <= channel capacity and <= the maximum
|
|
|
|
// in-flight msats set by the peer.
|
2019-01-12 20:59:45 +03:00
|
|
|
fwdMaxHTLC := completeChan.LocalChanCfg.MaxPendingAmount
|
2019-01-30 11:05:01 +03:00
|
|
|
capacityMSat := lnwire.NewMSatFromSatoshis(completeChan.Capacity)
|
|
|
|
if fwdMaxHTLC > capacityMSat {
|
|
|
|
fwdMaxHTLC = capacityMSat
|
|
|
|
}
|
2019-12-05 02:01:58 +03:00
|
|
|
if fwdMaxHTLC > MaxPaymentMSat {
|
|
|
|
fwdMaxHTLC = MaxPaymentMSat
|
|
|
|
}
|
2019-01-12 20:59:45 +03:00
|
|
|
|
2017-11-14 04:20:05 +03:00
|
|
|
// Create and broadcast the proofs required to make this channel
|
|
|
|
// public and usable for other nodes for routing.
|
2018-02-18 02:29:01 +03:00
|
|
|
err = f.announceChannel(
|
|
|
|
f.cfg.IDKey, completeChan.IdentityPub,
|
|
|
|
completeChan.LocalChanCfg.MultiSigKey.PubKey,
|
|
|
|
completeChan.RemoteChanCfg.MultiSigKey.PubKey,
|
2019-01-12 20:59:45 +03:00
|
|
|
*shortChanID, chanID, fwdMinHTLC, fwdMaxHTLC,
|
2017-11-14 04:20:05 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("channel announcement failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fndgLog.Debugf("Channel with ChannelPoint(%v), short_chan_id=%v "+
|
2019-02-06 13:50:38 +03:00
|
|
|
"announced", &fundingPoint, shortChanID)
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
|
|
|
|
2017-09-17 22:34:59 +03:00
|
|
|
return nil
|
2017-01-24 05:19:54 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// processFundingLocked sends a message to the fundingManager allowing it to
|
|
|
|
// finish the funding workflow.
|
2017-01-24 05:19:54 +03:00
|
|
|
func (f *fundingManager) processFundingLocked(msg *lnwire.FundingLocked,
|
2018-07-05 23:41:51 +03:00
|
|
|
peer lnpeer.Peer) {
|
2017-04-01 15:33:17 +03:00
|
|
|
|
2017-09-28 06:06:57 +03:00
|
|
|
select {
|
2018-07-05 23:41:51 +03:00
|
|
|
case f.fundingMsgs <- &fundingLockedMsg{msg, peer}:
|
2017-09-28 06:06:57 +03:00
|
|
|
case <-f.quit:
|
|
|
|
return
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// handleFundingLocked finalizes the channel funding process and enables the
|
|
|
|
// channel to enter normal operating mode.
|
2017-01-24 05:19:54 +03:00
|
|
|
func (f *fundingManager) handleFundingLocked(fmsg *fundingLockedMsg) {
|
2017-09-01 12:42:40 +03:00
|
|
|
defer f.wg.Done()
|
2017-12-09 01:50:26 +03:00
|
|
|
fndgLog.Debugf("Received FundingLocked for ChannelID(%v) from "+
|
|
|
|
"peer %x", fmsg.msg.ChanID,
|
2018-07-05 23:41:51 +03:00
|
|
|
fmsg.peer.IdentityKey().SerializeCompressed())
|
2017-09-01 12:42:40 +03:00
|
|
|
|
|
|
|
// If we are currently in the process of handling a funding locked
|
|
|
|
// message for this channel, ignore.
|
|
|
|
f.handleFundingLockedMtx.Lock()
|
|
|
|
_, ok := f.handleFundingLockedBarriers[fmsg.msg.ChanID]
|
|
|
|
if ok {
|
|
|
|
fndgLog.Infof("Already handling fundingLocked for "+
|
|
|
|
"ChannelID(%v), ignoring.", fmsg.msg.ChanID)
|
|
|
|
f.handleFundingLockedMtx.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not already handling fundingLocked for this channel, set up
|
|
|
|
// barrier, and move on.
|
|
|
|
f.handleFundingLockedBarriers[fmsg.msg.ChanID] = struct{}{}
|
|
|
|
f.handleFundingLockedMtx.Unlock()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
f.handleFundingLockedMtx.Lock()
|
|
|
|
delete(f.handleFundingLockedBarriers, fmsg.msg.ChanID)
|
|
|
|
f.handleFundingLockedMtx.Unlock()
|
|
|
|
}()
|
|
|
|
|
2017-06-17 00:19:39 +03:00
|
|
|
f.localDiscoveryMtx.Lock()
|
2017-07-15 03:14:11 +03:00
|
|
|
localDiscoverySignal, ok := f.localDiscoverySignals[fmsg.msg.ChanID]
|
2017-06-17 00:19:39 +03:00
|
|
|
f.localDiscoveryMtx.Unlock()
|
|
|
|
|
2017-07-15 03:14:11 +03:00
|
|
|
if ok {
|
|
|
|
// Before we proceed with processing the funding locked
|
2017-09-01 12:42:40 +03:00
|
|
|
// message, we'll wait for the local waitForFundingConfirmation
|
2017-07-15 03:14:11 +03:00
|
|
|
// goroutine to signal that it has the necessary state in
|
|
|
|
// place. Otherwise, we may be missing critical information
|
|
|
|
// required to handle forwarded HTLC's.
|
2017-09-01 12:42:40 +03:00
|
|
|
select {
|
|
|
|
case <-localDiscoverySignal:
|
|
|
|
// Fallthrough
|
|
|
|
case <-f.quit:
|
|
|
|
return
|
|
|
|
}
|
2017-07-15 03:14:11 +03:00
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// With the signal received, we can now safely delete the entry
|
|
|
|
// from the map.
|
2017-07-15 03:14:11 +03:00
|
|
|
f.localDiscoveryMtx.Lock()
|
|
|
|
delete(f.localDiscoverySignals, fmsg.msg.ChanID)
|
|
|
|
f.localDiscoveryMtx.Unlock()
|
|
|
|
}
|
2017-06-17 00:19:39 +03:00
|
|
|
|
2019-05-10 18:14:19 +03:00
|
|
|
// First, we'll attempt to locate the channel whose funding workflow is
|
|
|
|
// being finalized by this message. We go to the database rather than
|
2017-04-17 01:39:14 +03:00
|
|
|
// our reservation map as we may have restarted, mid funding flow.
|
|
|
|
chanID := fmsg.msg.ChanID
|
|
|
|
channel, err := f.cfg.FindChannel(chanID)
|
2017-01-24 05:19:54 +03:00
|
|
|
if err != nil {
|
2017-04-17 01:39:14 +03:00
|
|
|
fndgLog.Errorf("Unable to locate ChannelID(%v), cannot complete "+
|
|
|
|
"funding", chanID)
|
2017-01-24 05:19:54 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-01 12:42:40 +03:00
|
|
|
// If the RemoteNextRevocation is non-nil, it means that we have
|
|
|
|
// already processed fundingLocked for this channel, so ignore.
|
2018-09-26 12:12:57 +03:00
|
|
|
if channel.RemoteNextRevocation != nil {
|
2017-09-01 12:42:40 +03:00
|
|
|
fndgLog.Infof("Received duplicate fundingLocked for "+
|
|
|
|
"ChannelID(%v), ignoring.", chanID)
|
|
|
|
return
|
|
|
|
}
|
2017-08-22 09:25:41 +03:00
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// The funding locked message contains the next commitment point we'll
|
|
|
|
// need to create the next commitment state for the remote party. So
|
|
|
|
// we'll insert that into the channel now before passing it along to
|
|
|
|
// other sub-systems.
|
2018-09-26 12:12:57 +03:00
|
|
|
err = channel.InsertNextRevocation(fmsg.msg.NextPerCommitmentPoint)
|
2017-07-31 01:53:53 +03:00
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("unable to insert next commitment point: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-17 01:39:14 +03:00
|
|
|
// Launch a defer so we _ensure_ that the channel barrier is properly
|
2019-05-10 18:14:19 +03:00
|
|
|
// closed even if the target peer is no longer online at this point.
|
2017-04-17 01:39:14 +03:00
|
|
|
defer func() {
|
2019-09-11 15:44:31 +03:00
|
|
|
// Close the active channel barrier signaling the readHandler
|
2017-04-17 01:39:14 +03:00
|
|
|
// that commitment related modifications to this channel can
|
|
|
|
// now proceed.
|
|
|
|
f.barrierMtx.Lock()
|
2017-09-01 12:42:40 +03:00
|
|
|
chanBarrier, ok := f.newChanBarriers[chanID]
|
|
|
|
if ok {
|
|
|
|
fndgLog.Tracef("Closing chan barrier for ChanID(%v)",
|
|
|
|
chanID)
|
|
|
|
close(chanBarrier)
|
|
|
|
delete(f.newChanBarriers, chanID)
|
|
|
|
}
|
2017-04-17 01:39:14 +03:00
|
|
|
f.barrierMtx.Unlock()
|
|
|
|
}()
|
|
|
|
|
2018-07-05 23:41:51 +03:00
|
|
|
if err := fmsg.peer.AddNewChannel(channel, f.quit); err != nil {
|
|
|
|
fndgLog.Errorf("Unable to add new channel %v with peer %x: %v",
|
|
|
|
fmsg.peer.IdentityKey().SerializeCompressed(),
|
2018-09-26 12:12:57 +03:00
|
|
|
channel.FundingOutpoint, err)
|
2017-04-17 01:39:14 +03:00
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
}
|
|
|
|
|
2016-12-27 08:42:23 +03:00
|
|
|
// chanAnnouncement encapsulates the two authenticated announcements that we
|
|
|
|
// send out to the network after a new channel has been created locally.
|
|
|
|
type chanAnnouncement struct {
|
2017-03-28 22:08:14 +03:00
|
|
|
chanAnn *lnwire.ChannelAnnouncement
|
2017-04-20 02:20:46 +03:00
|
|
|
chanUpdateAnn *lnwire.ChannelUpdate
|
2017-03-28 22:08:14 +03:00
|
|
|
chanProof *lnwire.AnnounceSignatures
|
2016-12-27 08:42:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newChanAnnouncement creates the authenticated channel announcement messages
|
|
|
|
// required to broadcast a newly created channel to the network. The
|
|
|
|
// announcement is two part: the first part authenticates the existence of the
|
|
|
|
// channel and contains four signatures binding the funding pub keys and
|
|
|
|
// identity pub keys of both parties to the channel, and the second segment is
|
2017-01-13 08:01:50 +03:00
|
|
|
// authenticated only by us and contains our directional routing policy for the
|
2016-12-27 08:42:23 +03:00
|
|
|
// channel.
|
2018-08-22 10:32:43 +03:00
|
|
|
func (f *fundingManager) newChanAnnouncement(localPubKey, remotePubKey,
|
2017-04-17 01:34:23 +03:00
|
|
|
localFundingKey, remoteFundingKey *btcec.PublicKey,
|
2017-12-01 09:52:28 +03:00
|
|
|
shortChanID lnwire.ShortChannelID, chanID lnwire.ChannelID,
|
2019-01-12 20:59:45 +03:00
|
|
|
fwdMinHTLC, fwdMaxHTLC lnwire.MilliSatoshi) (*chanAnnouncement, error) {
|
2016-12-27 08:42:23 +03:00
|
|
|
|
2017-08-22 09:33:19 +03:00
|
|
|
chainHash := *f.cfg.Wallet.Cfg.NetParams.GenesisHash
|
|
|
|
|
2017-03-28 22:08:14 +03:00
|
|
|
// The unconditional section of the announcement is the ShortChannelID
|
2016-12-27 08:42:23 +03:00
|
|
|
// itself which compactly encodes the location of the funding output
|
|
|
|
// within the blockchain.
|
|
|
|
chanAnn := &lnwire.ChannelAnnouncement{
|
2017-04-17 01:34:23 +03:00
|
|
|
ShortChannelID: shortChanID,
|
2017-10-11 21:37:54 +03:00
|
|
|
Features: lnwire.NewRawFeatureVector(),
|
2017-08-22 09:33:19 +03:00
|
|
|
ChainHash: chainHash,
|
2016-12-27 08:42:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The chanFlags field indicates which directed edge of the channel is
|
|
|
|
// being updated within the ChannelUpdateAnnouncement announcement
|
|
|
|
// below. A value of zero means it's the edge of the "first" node and 1
|
|
|
|
// being the other node.
|
2019-01-12 20:59:43 +03:00
|
|
|
var chanFlags lnwire.ChanUpdateChanFlags
|
2016-12-27 08:42:23 +03:00
|
|
|
|
|
|
|
// The lexicographical ordering of the two identity public keys of the
|
|
|
|
// nodes indicates which of the nodes is "first". If our serialized
|
|
|
|
// identity key is lower than theirs then we're the "first" node and
|
|
|
|
// second otherwise.
|
2017-03-28 22:08:14 +03:00
|
|
|
selfBytes := localPubKey.SerializeCompressed()
|
|
|
|
remoteBytes := remotePubKey.SerializeCompressed()
|
2016-12-27 08:42:23 +03:00
|
|
|
if bytes.Compare(selfBytes, remoteBytes) == -1 {
|
2018-01-31 07:30:00 +03:00
|
|
|
copy(chanAnn.NodeID1[:], localPubKey.SerializeCompressed())
|
|
|
|
copy(chanAnn.NodeID2[:], remotePubKey.SerializeCompressed())
|
|
|
|
copy(chanAnn.BitcoinKey1[:], localFundingKey.SerializeCompressed())
|
|
|
|
copy(chanAnn.BitcoinKey2[:], remoteFundingKey.SerializeCompressed())
|
2016-12-27 08:42:23 +03:00
|
|
|
|
|
|
|
// If we're the first node then update the chanFlags to
|
|
|
|
// indicate the "direction" of the update.
|
|
|
|
chanFlags = 0
|
|
|
|
} else {
|
2018-01-31 07:30:00 +03:00
|
|
|
copy(chanAnn.NodeID1[:], remotePubKey.SerializeCompressed())
|
|
|
|
copy(chanAnn.NodeID2[:], localPubKey.SerializeCompressed())
|
|
|
|
copy(chanAnn.BitcoinKey1[:], remoteFundingKey.SerializeCompressed())
|
|
|
|
copy(chanAnn.BitcoinKey2[:], localFundingKey.SerializeCompressed())
|
2016-12-27 08:42:23 +03:00
|
|
|
|
|
|
|
// If we're the second node then update the chanFlags to
|
|
|
|
// indicate the "direction" of the update.
|
|
|
|
chanFlags = 1
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:59:45 +03:00
|
|
|
// Our channel update message flags will signal that we support the
|
|
|
|
// max_htlc field.
|
|
|
|
msgFlags := lnwire.ChanUpdateOptionMaxHtlc
|
|
|
|
|
2017-12-17 02:01:38 +03:00
|
|
|
// We announce the channel with the default values. Some of
|
|
|
|
// these values can later be changed by crafting a new ChannelUpdate.
|
2017-04-20 02:20:46 +03:00
|
|
|
chanUpdateAnn := &lnwire.ChannelUpdate{
|
2017-12-01 09:52:28 +03:00
|
|
|
ShortChannelID: shortChanID,
|
|
|
|
ChainHash: chainHash,
|
|
|
|
Timestamp: uint32(time.Now().Unix()),
|
2019-01-12 20:59:45 +03:00
|
|
|
MessageFlags: msgFlags,
|
2019-01-12 20:59:43 +03:00
|
|
|
ChannelFlags: chanFlags,
|
2017-12-01 09:52:28 +03:00
|
|
|
TimeLockDelta: uint16(f.cfg.DefaultRoutingPolicy.TimeLockDelta),
|
|
|
|
|
2018-08-22 10:32:43 +03:00
|
|
|
// We use the HtlcMinimumMsat that the remote party required us
|
|
|
|
// to use, as our ChannelUpdate will be used to carry HTLCs
|
|
|
|
// towards them.
|
|
|
|
HtlcMinimumMsat: fwdMinHTLC,
|
2019-01-12 20:59:45 +03:00
|
|
|
HtlcMaximumMsat: fwdMaxHTLC,
|
2017-12-01 09:52:28 +03:00
|
|
|
|
|
|
|
BaseFee: uint32(f.cfg.DefaultRoutingPolicy.BaseFee),
|
|
|
|
FeeRate: uint32(f.cfg.DefaultRoutingPolicy.FeeRate),
|
2016-12-27 08:42:23 +03:00
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// With the channel update announcement constructed, we'll generate a
|
|
|
|
// signature that signs a double-sha digest of the announcement.
|
2017-04-14 21:17:41 +03:00
|
|
|
// This'll serve to authenticate this announcement and any other future
|
|
|
|
// updates we may send.
|
|
|
|
chanUpdateMsg, err := chanUpdateAnn.DataToSign()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-31 07:30:00 +03:00
|
|
|
sig, err := f.cfg.SignMessage(f.cfg.IDKey, chanUpdateMsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("unable to generate channel "+
|
|
|
|
"update announcement signature: %v", err)
|
|
|
|
}
|
|
|
|
chanUpdateAnn.Signature, err = lnwire.NewSigFromSignature(sig)
|
2017-03-28 22:08:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("unable to generate channel "+
|
|
|
|
"update announcement signature: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// The channel existence proofs itself is currently announced in
|
|
|
|
// distinct message. In order to properly authenticate this message, we
|
|
|
|
// need two signatures: one under the identity public key used which
|
|
|
|
// signs the message itself and another signature of the identity
|
|
|
|
// public key under the funding key itself.
|
2017-08-22 09:25:41 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): use SignAnnouncement here instead?
|
2017-04-14 21:17:41 +03:00
|
|
|
chanAnnMsg, err := chanAnn.DataToSign()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nodeSig, err := f.cfg.SignMessage(f.cfg.IDKey, chanAnnMsg)
|
2017-03-28 22:08:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("unable to generate node "+
|
|
|
|
"signature for channel announcement: %v", err)
|
2016-12-27 08:42:23 +03:00
|
|
|
}
|
2017-08-22 09:33:19 +03:00
|
|
|
bitcoinSig, err := f.cfg.SignMessage(localFundingKey, chanAnnMsg)
|
2017-03-28 22:08:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("unable to generate bitcoin "+
|
|
|
|
"signature for node public key: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
// Finally, we'll generate the announcement proof which we'll use to
|
|
|
|
// provide the other side with the necessary signatures required to
|
|
|
|
// allow them to reconstruct the full channel announcement.
|
2017-03-28 22:08:14 +03:00
|
|
|
proof := &lnwire.AnnounceSignatures{
|
2018-01-31 07:30:00 +03:00
|
|
|
ChannelID: chanID,
|
|
|
|
ShortChannelID: shortChanID,
|
|
|
|
}
|
|
|
|
proof.NodeSignature, err = lnwire.NewSigFromSignature(nodeSig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
proof.BitcoinSignature, err = lnwire.NewSigFromSignature(bitcoinSig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &chanAnnouncement{
|
|
|
|
chanAnn: chanAnn,
|
|
|
|
chanUpdateAnn: chanUpdateAnn,
|
|
|
|
chanProof: proof,
|
|
|
|
}, nil
|
2016-12-27 08:42:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// announceChannel announces a newly created channel to the rest of the network
|
2017-04-01 15:33:17 +03:00
|
|
|
// by crafting the two authenticated announcements required for the peers on
|
|
|
|
// the network to recognize the legitimacy of the channel. The crafted
|
|
|
|
// announcements are then sent to the channel router to handle broadcasting to
|
2016-12-27 08:42:23 +03:00
|
|
|
// the network during its next trickle.
|
2017-06-08 20:48:07 +03:00
|
|
|
// This method is synchronous and will return when all the network requests
|
|
|
|
// finish, either successfully or with an error.
|
2017-04-01 15:33:17 +03:00
|
|
|
func (f *fundingManager) announceChannel(localIDKey, remoteIDKey, localFundingKey,
|
2017-04-17 01:34:23 +03:00
|
|
|
remoteFundingKey *btcec.PublicKey, shortChanID lnwire.ShortChannelID,
|
2019-01-12 20:59:45 +03:00
|
|
|
chanID lnwire.ChannelID, fwdMinHTLC, fwdMaxHTLC lnwire.MilliSatoshi) error {
|
2017-08-05 04:32:25 +03:00
|
|
|
|
|
|
|
// First, we'll create the batch of announcements to be sent upon
|
|
|
|
// initial channel creation. This includes the channel announcement
|
|
|
|
// itself, the channel update announcement, and our half of the channel
|
|
|
|
// proof needed to fully authenticate the channel.
|
|
|
|
ann, err := f.newChanAnnouncement(localIDKey, remoteIDKey,
|
2017-12-01 09:52:28 +03:00
|
|
|
localFundingKey, remoteFundingKey, shortChanID, chanID,
|
2019-01-12 20:59:45 +03:00
|
|
|
fwdMinHTLC, fwdMaxHTLC,
|
2017-12-01 09:52:28 +03:00
|
|
|
)
|
2017-03-28 22:08:14 +03:00
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("can't generate channel announcement: %v", err)
|
2017-06-08 20:48:07 +03:00
|
|
|
return err
|
2017-03-28 22:08:14 +03:00
|
|
|
}
|
2016-12-27 08:42:23 +03:00
|
|
|
|
2017-11-14 02:55:50 +03:00
|
|
|
// We only send the channel proof announcement and the node announcement
|
2019-05-10 18:14:19 +03:00
|
|
|
// because addToRouterGraph previously sent the ChannelAnnouncement and
|
2017-11-14 02:55:50 +03:00
|
|
|
// the ChannelUpdate announcement messages. The channel proof and node
|
|
|
|
// announcements are broadcast to the greater network.
|
2018-08-20 15:28:11 +03:00
|
|
|
errChan := f.cfg.SendAnnouncement(ann.chanProof)
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
if err != nil {
|
|
|
|
if routing.IsError(err, routing.ErrOutdated,
|
|
|
|
routing.ErrIgnored) {
|
|
|
|
fndgLog.Debugf("Router rejected "+
|
|
|
|
"AnnounceSignatures: %v", err)
|
|
|
|
} else {
|
|
|
|
fndgLog.Errorf("Unable to send channel "+
|
|
|
|
"proof: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-11-14 04:20:05 +03:00
|
|
|
}
|
2018-08-20 15:28:11 +03:00
|
|
|
|
|
|
|
case <-f.quit:
|
|
|
|
return ErrFundingManagerShuttingDown
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
2017-07-14 22:47:52 +03:00
|
|
|
|
2017-08-05 04:32:25 +03:00
|
|
|
// Now that the channel is announced to the network, we will also
|
|
|
|
// obtain and send a node announcement. This is done since a node
|
|
|
|
// announcement is only accepted after a channel is known for that
|
|
|
|
// particular node, and this might be our first channel.
|
|
|
|
nodeAnn, err := f.cfg.CurrentNodeAnnouncement()
|
2017-07-14 22:47:52 +03:00
|
|
|
if err != nil {
|
2017-08-05 04:32:25 +03:00
|
|
|
fndgLog.Errorf("can't generate node announcement: %v", err)
|
2017-06-08 20:48:07 +03:00
|
|
|
return err
|
2017-07-14 22:47:52 +03:00
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
2018-08-20 15:28:11 +03:00
|
|
|
errChan = f.cfg.SendAnnouncement(&nodeAnn)
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
if err != nil {
|
|
|
|
if routing.IsError(err, routing.ErrOutdated,
|
|
|
|
routing.ErrIgnored) {
|
|
|
|
fndgLog.Debugf("Router rejected "+
|
|
|
|
"NodeAnnouncement: %v", err)
|
|
|
|
} else {
|
|
|
|
fndgLog.Errorf("Unable to send node "+
|
|
|
|
"announcement: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-11-14 04:20:05 +03:00
|
|
|
}
|
2018-08-20 15:28:11 +03:00
|
|
|
|
|
|
|
case <-f.quit:
|
|
|
|
return ErrFundingManagerShuttingDown
|
2017-06-08 20:48:07 +03:00
|
|
|
}
|
2018-08-20 15:28:11 +03:00
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
return nil
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// initFundingWorkflow sends a message to the funding manager instructing it
|
|
|
|
// to initiate a single funder workflow with the source peer.
|
|
|
|
// TODO(roasbeef): re-visit blocking nature..
|
2018-07-05 23:41:51 +03:00
|
|
|
func (f *fundingManager) initFundingWorkflow(peer lnpeer.Peer, req *openChanReq) {
|
2016-06-21 21:31:10 +03:00
|
|
|
f.fundingRequests <- &initFundingMsg{
|
2018-07-05 23:41:51 +03:00
|
|
|
peer: peer,
|
2016-06-21 21:31:10 +03:00
|
|
|
openChanReq: req,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 12:38:29 +03:00
|
|
|
// getUpfrontShutdownScript takes a user provided script and a getScript
|
|
|
|
// function which can be used to generate an upfront shutdown script. If our
|
|
|
|
// peer does not support the feature, this function will error if a non-zero
|
|
|
|
// script was provided by the user, and return an empty script otherwise. If
|
|
|
|
// our peer does support the feature, we will return the user provided script
|
|
|
|
// if non-zero, or a freshly generated script if our node is configured to set
|
|
|
|
// upfront shutdown scripts automatically.
|
|
|
|
func getUpfrontShutdownScript(peer lnpeer.Peer, script lnwire.DeliveryAddress,
|
|
|
|
getScript func() (lnwire.DeliveryAddress, error)) (lnwire.DeliveryAddress,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
// Check whether the remote peer supports upfront shutdown scripts.
|
|
|
|
remoteUpfrontShutdown := peer.RemoteFeatures().HasFeature(
|
|
|
|
lnwire.UpfrontShutdownScriptOptional,
|
|
|
|
)
|
|
|
|
|
|
|
|
// If the peer does not support upfront shutdown scripts, and one has been
|
|
|
|
// provided, return an error because the feature is not supported.
|
|
|
|
if !remoteUpfrontShutdown && len(script) != 0 {
|
|
|
|
return nil, errUpfrontShutdownScriptNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the peer does not support upfront shutdown, return an empty address.
|
|
|
|
if !remoteUpfrontShutdown {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user has provided an script and the peer supports the feature,
|
|
|
|
// return it. Note that user set scripts override the enable upfront
|
|
|
|
// shutdown flag.
|
|
|
|
if len(script) > 0 {
|
|
|
|
return script, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we do not have setting of upfront shutdown script enabled, return
|
|
|
|
// an empty script.
|
|
|
|
if !cfg.EnableUpfrontShutdown {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return getScript()
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// handleInitFundingMsg creates a channel reservation within the daemon's
|
|
|
|
// wallet, then sends a funding request to the remote peer kicking off the
|
|
|
|
// funding workflow.
|
|
|
|
func (f *fundingManager) handleInitFundingMsg(msg *initFundingMsg) {
|
2016-10-26 02:43:55 +03:00
|
|
|
var (
|
2018-07-05 23:41:51 +03:00
|
|
|
peerKey = msg.peer.IdentityKey()
|
2018-03-14 16:20:57 +03:00
|
|
|
localAmt = msg.localFundingAmt
|
|
|
|
minHtlc = msg.minHtlc
|
|
|
|
remoteCsvDelay = msg.remoteCsvDelay
|
2016-10-26 02:43:55 +03:00
|
|
|
)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2018-07-02 23:10:02 +03:00
|
|
|
// We'll determine our dust limit depending on which chain is active.
|
|
|
|
var ourDustLimit btcutil.Amount
|
|
|
|
switch registeredChains.PrimaryChain() {
|
|
|
|
case bitcoinChain:
|
|
|
|
ourDustLimit = lnwallet.DefaultDustLimit()
|
|
|
|
case litecoinChain:
|
|
|
|
ourDustLimit = defaultLitecoinDustLimit
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:14:38 +03:00
|
|
|
fndgLog.Infof("Initiating fundingRequest(local_amt=%v "+
|
|
|
|
"(subtract_fees=%v), push_amt=%v, chain_hash=%v, peer=%x, "+
|
|
|
|
"dust_limit=%v, min_confs=%v)", localAmt, msg.subtractFees,
|
|
|
|
msg.pushAmt, msg.chainHash, peerKey.SerializeCompressed(),
|
|
|
|
ourDustLimit, msg.minConfs)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-05-17 05:10:55 +03:00
|
|
|
// First, we'll query the fee estimator for a fee that should get the
|
2017-11-23 22:38:08 +03:00
|
|
|
// commitment transaction confirmed by the next few blocks (conf target
|
|
|
|
// of 3). We target the near blocks here to ensure that we'll be able
|
|
|
|
// to execute a timely unilateral channel closure if needed.
|
2018-07-28 04:20:58 +03:00
|
|
|
commitFeePerKw, err := f.cfg.FeeEstimator.EstimateFeePerKW(3)
|
2017-11-23 22:38:08 +03:00
|
|
|
if err != nil {
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
2017-05-17 05:10:55 +03:00
|
|
|
|
2018-06-06 03:50:45 +03:00
|
|
|
// We set the channel flags to indicate whether we want this channel to
|
|
|
|
// be announced to the network.
|
2017-11-14 04:20:05 +03:00
|
|
|
var channelFlags lnwire.FundingFlag
|
|
|
|
if !msg.openChanReq.private {
|
|
|
|
// This channel will be announced.
|
|
|
|
channelFlags = lnwire.FFAnnounceChannel
|
|
|
|
}
|
|
|
|
|
2019-11-01 07:45:44 +03:00
|
|
|
// Obtain a new pending channel ID which is used to track this
|
|
|
|
// reservation throughout its lifetime.
|
|
|
|
chanID := f.nextPendingChanID()
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// Initialize a funding reservation with the local wallet. If the
|
2017-07-31 01:53:53 +03:00
|
|
|
// wallet doesn't have enough funds to commit to this channel, then the
|
|
|
|
// request will fail, and be aborted.
|
2019-09-11 15:44:31 +03:00
|
|
|
//
|
|
|
|
// Before we init the channel, we'll also check to see if we've
|
|
|
|
// negotiated the new tweakless commitment format. This is only the
|
|
|
|
// case if *both* us and the remote peer are signaling the proper
|
|
|
|
// feature bit.
|
2019-11-08 16:31:47 +03:00
|
|
|
localTweakless := msg.peer.LocalFeatures().HasFeature(
|
2019-09-11 15:44:31 +03:00
|
|
|
lnwire.StaticRemoteKeyOptional,
|
|
|
|
)
|
2019-11-08 16:31:47 +03:00
|
|
|
remoteTweakless := msg.peer.RemoteFeatures().HasFeature(
|
2019-09-11 15:44:31 +03:00
|
|
|
lnwire.StaticRemoteKeyOptional,
|
|
|
|
)
|
|
|
|
tweaklessCommitment := localTweakless && remoteTweakless
|
2018-08-10 05:16:14 +03:00
|
|
|
req := &lnwallet.InitFundingReserveMsg{
|
2019-07-11 14:14:36 +03:00
|
|
|
ChainHash: &msg.chainHash,
|
2019-11-01 07:45:44 +03:00
|
|
|
PendingChanID: chanID,
|
2019-07-11 14:14:36 +03:00
|
|
|
NodeID: peerKey,
|
|
|
|
NodeAddr: msg.peer.Address(),
|
2019-07-11 14:14:38 +03:00
|
|
|
SubtractFees: msg.subtractFees,
|
2019-07-11 14:14:36 +03:00
|
|
|
LocalFundingAmt: localAmt,
|
|
|
|
RemoteFundingAmt: 0,
|
|
|
|
CommitFeePerKw: commitFeePerKw,
|
|
|
|
FundingFeePerKw: msg.fundingFeePerKw,
|
|
|
|
PushMSat: msg.pushAmt,
|
|
|
|
Flags: channelFlags,
|
|
|
|
MinConfs: msg.minConfs,
|
2019-09-11 15:44:31 +03:00
|
|
|
Tweakless: tweaklessCommitment,
|
2018-08-10 05:16:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
reservation, err := f.cfg.Wallet.InitChannelReservation(req)
|
2016-06-21 21:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-03 12:38:29 +03:00
|
|
|
// Check whether the peer supports upfront shutdown, and get an address which
|
|
|
|
// should be used (either a user specified address or a new address from the
|
|
|
|
// wallet if our node is configured to set shutdown address by default).
|
|
|
|
shutdown, err := getUpfrontShutdownScript(
|
|
|
|
msg.peer, msg.openChanReq.shutdownScript,
|
|
|
|
func() (lnwire.DeliveryAddress, error) {
|
|
|
|
addr, err := f.cfg.Wallet.NewAddress(lnwallet.WitnessPubKey, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return txscript.PayToAddrScript(addr)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set our upfront shutdown address in the existing reservation.
|
|
|
|
reservation.SetOurUpfrontShutdown(shutdown)
|
|
|
|
|
2019-07-11 14:14:38 +03:00
|
|
|
// Now that we have successfully reserved funds for this channel in the
|
|
|
|
// wallet, we can fetch the final channel capacity. This is done at
|
|
|
|
// this point since the final capacity might change in case of
|
|
|
|
// SubtractFees=true.
|
|
|
|
capacity := reservation.Capacity()
|
|
|
|
|
2019-11-01 07:45:44 +03:00
|
|
|
fndgLog.Infof("Target commit tx sat/kw for pendingID(%x): %v", chanID,
|
2017-11-23 22:38:08 +03:00
|
|
|
int64(commitFeePerKw))
|
2017-05-17 05:10:55 +03:00
|
|
|
|
2018-04-05 20:14:26 +03:00
|
|
|
// If the remote CSV delay was not set in the open channel request,
|
|
|
|
// we'll use the RequiredRemoteDelay closure to compute the delay we
|
|
|
|
// require given the total amount of funds within the channel.
|
|
|
|
if remoteCsvDelay == 0 {
|
|
|
|
remoteCsvDelay = f.cfg.RequiredRemoteDelay(capacity)
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:48:41 +03:00
|
|
|
// If no minimum HTLC value was specified, use the default one.
|
|
|
|
if minHtlc == 0 {
|
|
|
|
minHtlc = f.cfg.DefaultRoutingPolicy.MinHTLC
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// If a pending channel map for this peer isn't already created, then
|
|
|
|
// we create one, ultimately allowing us to track this pending
|
|
|
|
// reservation within the target peer.
|
2017-01-13 06:40:38 +03:00
|
|
|
peerIDKey := newSerializedKey(peerKey)
|
2016-06-21 21:31:10 +03:00
|
|
|
f.resMtx.Lock()
|
2017-01-13 06:40:38 +03:00
|
|
|
if _, ok := f.activeReservations[peerIDKey]; !ok {
|
|
|
|
f.activeReservations[peerIDKey] = make(pendingChannels)
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
2016-10-15 16:18:38 +03:00
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
resCtx := &reservationWithCtx{
|
2018-04-05 20:14:26 +03:00
|
|
|
chanAmt: capacity,
|
|
|
|
remoteCsvDelay: remoteCsvDelay,
|
2018-04-05 20:48:41 +03:00
|
|
|
remoteMinHtlc: minHtlc,
|
2018-04-05 20:14:26 +03:00
|
|
|
reservation: reservation,
|
2018-07-05 23:41:51 +03:00
|
|
|
peer: msg.peer,
|
2018-04-05 20:14:26 +03:00
|
|
|
updates: msg.updates,
|
|
|
|
err: msg.err,
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
2018-03-13 04:58:51 +03:00
|
|
|
f.activeReservations[peerIDKey][chanID] = resCtx
|
2016-06-21 21:31:10 +03:00
|
|
|
f.resMtx.Unlock()
|
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
// Update the timestamp once the initFundingMsg has been handled.
|
|
|
|
defer resCtx.updateTimestamp()
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// Once the reservation has been created, and indexed, queue a funding
|
|
|
|
// request to the remote peer, kicking off the funding workflow.
|
2017-07-31 01:53:53 +03:00
|
|
|
ourContribution := reservation.OurContribution()
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-09-12 19:07:43 +03:00
|
|
|
// Finally, we'll use the current value of the channels and our default
|
|
|
|
// policy to determine of required commitment constraints for the
|
|
|
|
// remote party.
|
2018-05-14 21:03:58 +03:00
|
|
|
chanReserve := f.cfg.RequiredRemoteChanReserve(capacity, ourDustLimit)
|
2017-11-29 15:59:27 +03:00
|
|
|
maxValue := f.cfg.RequiredRemoteMaxValue(capacity)
|
|
|
|
maxHtlcs := f.cfg.RequiredRemoteMaxHTLCs(capacity)
|
2017-09-12 19:07:43 +03:00
|
|
|
|
2019-11-06 02:04:43 +03:00
|
|
|
fndgLog.Infof("Starting funding workflow with %v for pending_id(%x), "+
|
2019-09-11 15:44:31 +03:00
|
|
|
"tweakless=%v", msg.peer.Address(), chanID, tweaklessCommitment)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
fundingOpen := lnwire.OpenChannel{
|
2019-12-03 12:38:29 +03:00
|
|
|
ChainHash: *f.cfg.Wallet.Cfg.NetParams.GenesisHash,
|
|
|
|
PendingChannelID: chanID,
|
|
|
|
FundingAmount: capacity,
|
|
|
|
PushAmount: msg.pushAmt,
|
|
|
|
DustLimit: ourContribution.DustLimit,
|
|
|
|
MaxValueInFlight: maxValue,
|
|
|
|
ChannelReserve: chanReserve,
|
|
|
|
HtlcMinimum: minHtlc,
|
|
|
|
FeePerKiloWeight: uint32(commitFeePerKw),
|
|
|
|
CsvDelay: remoteCsvDelay,
|
|
|
|
MaxAcceptedHTLCs: maxHtlcs,
|
|
|
|
FundingKey: ourContribution.MultiSigKey.PubKey,
|
|
|
|
RevocationPoint: ourContribution.RevocationBasePoint.PubKey,
|
|
|
|
PaymentPoint: ourContribution.PaymentBasePoint.PubKey,
|
|
|
|
HtlcPoint: ourContribution.HtlcBasePoint.PubKey,
|
|
|
|
DelayedPaymentPoint: ourContribution.DelayBasePoint.PubKey,
|
|
|
|
FirstCommitmentPoint: ourContribution.FirstCommitmentPoint,
|
|
|
|
ChannelFlags: channelFlags,
|
|
|
|
UpfrontShutdownScript: shutdown,
|
2017-07-31 01:53:53 +03:00
|
|
|
}
|
2019-10-24 14:08:08 +03:00
|
|
|
if err := msg.peer.SendMessage(true, &fundingOpen); err != nil {
|
2018-02-23 13:11:01 +03:00
|
|
|
e := fmt.Errorf("Unable to send funding request message: %v",
|
|
|
|
err)
|
|
|
|
fndgLog.Errorf(e.Error())
|
|
|
|
|
|
|
|
// Since we were unable to send the initial message to the peer
|
|
|
|
// and start the funding flow, we'll cancel this reservation.
|
|
|
|
if _, err := f.cancelReservationCtx(peerKey, chanID); err != nil {
|
|
|
|
fndgLog.Errorf("unable to cancel reservation: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.err <- e
|
2017-01-13 06:40:38 +03:00
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
2016-10-15 16:24:56 +03:00
|
|
|
|
2017-01-24 02:33:46 +03:00
|
|
|
// waitUntilChannelOpen is designed to prevent other lnd subsystems from
|
|
|
|
// sending new update messages to a channel before the channel is fully
|
|
|
|
// opened.
|
2018-08-25 03:55:53 +03:00
|
|
|
func (f *fundingManager) waitUntilChannelOpen(targetChan lnwire.ChannelID,
|
2018-08-25 05:58:29 +03:00
|
|
|
quit <-chan struct{}) error {
|
2018-08-25 03:55:53 +03:00
|
|
|
|
2017-01-24 02:33:46 +03:00
|
|
|
f.barrierMtx.RLock()
|
|
|
|
barrier, ok := f.newChanBarriers[targetChan]
|
|
|
|
f.barrierMtx.RUnlock()
|
|
|
|
if ok {
|
2017-04-17 01:34:23 +03:00
|
|
|
fndgLog.Tracef("waiting for chan barrier signal for ChanID(%v)",
|
|
|
|
targetChan)
|
|
|
|
|
2017-01-24 02:33:46 +03:00
|
|
|
select {
|
|
|
|
case <-barrier:
|
2018-08-25 03:55:53 +03:00
|
|
|
case <-quit:
|
2018-08-25 05:58:29 +03:00
|
|
|
return ErrFundingManagerShuttingDown
|
2018-08-25 03:55:53 +03:00
|
|
|
case <-f.quit:
|
2018-08-25 05:58:29 +03:00
|
|
|
return ErrFundingManagerShuttingDown
|
2017-01-24 02:33:46 +03:00
|
|
|
}
|
2017-04-17 01:34:23 +03:00
|
|
|
|
|
|
|
fndgLog.Tracef("barrier for ChanID(%v) closed", targetChan)
|
2018-08-25 05:58:29 +03:00
|
|
|
return nil
|
2017-01-24 02:33:46 +03:00
|
|
|
}
|
2018-08-25 05:58:29 +03:00
|
|
|
|
|
|
|
return nil
|
2017-01-24 02:33:46 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
// processFundingError sends a message to the fundingManager allowing it to
|
2016-10-23 23:39:54 +03:00
|
|
|
// process the occurred generic error.
|
2017-04-17 01:34:23 +03:00
|
|
|
func (f *fundingManager) processFundingError(err *lnwire.Error,
|
2018-07-05 23:38:11 +03:00
|
|
|
peerKey *btcec.PublicKey) {
|
2016-10-23 23:39:54 +03:00
|
|
|
|
2017-09-28 06:06:57 +03:00
|
|
|
select {
|
2018-07-05 23:38:11 +03:00
|
|
|
case f.fundingMsgs <- &fundingErrorMsg{err, peerKey}:
|
2017-09-28 06:06:57 +03:00
|
|
|
case <-f.quit:
|
|
|
|
return
|
|
|
|
}
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
// handleErrorMsg processes the error which was received from remote peer,
|
2017-01-13 08:01:50 +03:00
|
|
|
// depending on the type of error we should do different clean up steps and
|
|
|
|
// inform the user about it.
|
2017-04-17 01:34:23 +03:00
|
|
|
func (f *fundingManager) handleErrorMsg(fmsg *fundingErrorMsg) {
|
2017-08-31 06:31:33 +03:00
|
|
|
protocolErr := fmsg.err
|
2016-10-15 16:24:56 +03:00
|
|
|
|
2017-08-31 06:31:33 +03:00
|
|
|
chanID := fmsg.err.ChanID
|
2017-01-25 04:12:51 +03:00
|
|
|
|
2018-05-30 13:16:37 +03:00
|
|
|
// First, we'll attempt to retrieve and cancel the funding workflow
|
|
|
|
// that this error was tied to. If we're unable to do so, then we'll
|
|
|
|
// exit early as this was an unwarranted error.
|
2018-07-05 23:38:11 +03:00
|
|
|
resCtx, err := f.cancelReservationCtx(fmsg.peerKey, chanID)
|
2017-08-31 06:31:33 +03:00
|
|
|
if err != nil {
|
|
|
|
fndgLog.Warnf("Received error for non-existent funding "+
|
2019-09-20 11:55:21 +03:00
|
|
|
"flow: %v (%v)", err, protocolErr.Error())
|
2017-01-25 04:12:51 +03:00
|
|
|
return
|
2017-08-31 06:31:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we did indeed find the funding workflow, then we'll return the
|
|
|
|
// error back to the caller (if any), and cancel the workflow itself.
|
2019-09-20 11:55:20 +03:00
|
|
|
fundingErr := fmt.Errorf("received funding error from %x: %v",
|
2019-09-20 11:55:21 +03:00
|
|
|
fmsg.peerKey.SerializeCompressed(), protocolErr.Error(),
|
2017-08-31 06:31:33 +03:00
|
|
|
)
|
2019-09-20 11:55:20 +03:00
|
|
|
fndgLog.Errorf(fundingErr.Error())
|
2017-12-01 09:53:19 +03:00
|
|
|
|
2019-09-20 11:55:20 +03:00
|
|
|
resCtx.err <- fundingErr
|
2016-11-24 11:49:18 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 04:58:51 +03:00
|
|
|
// pruneZombieReservations loops through all pending reservations and fails the
|
|
|
|
// funding flow for any reservations that have not been updated since the
|
|
|
|
// ReservationTimeout and are not locked waiting for the funding transaction.
|
|
|
|
func (f *fundingManager) pruneZombieReservations() {
|
|
|
|
zombieReservations := make(pendingChannels)
|
|
|
|
|
|
|
|
f.resMtx.RLock()
|
|
|
|
for _, pendingReservations := range f.activeReservations {
|
|
|
|
for pendingChanID, resCtx := range pendingReservations {
|
|
|
|
if resCtx.isLocked() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Since(resCtx.lastUpdated) > f.cfg.ReservationTimeout {
|
|
|
|
zombieReservations[pendingChanID] = resCtx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.resMtx.RUnlock()
|
|
|
|
|
|
|
|
for pendingChanID, resCtx := range zombieReservations {
|
2018-07-05 23:41:51 +03:00
|
|
|
err := fmt.Errorf("reservation timed out waiting for peer "+
|
2019-11-06 02:04:43 +03:00
|
|
|
"(peer_id:%v, chan_id:%x)", resCtx.peer.IdentityKey(),
|
2018-07-05 23:41:51 +03:00
|
|
|
pendingChanID[:])
|
2018-03-13 04:58:51 +03:00
|
|
|
fndgLog.Warnf(err.Error())
|
2018-07-05 23:41:51 +03:00
|
|
|
f.failFundingFlow(resCtx.peer, pendingChanID, err)
|
2018-03-13 04:58:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancelReservationCtx does all needed work in order to securely cancel the
|
2016-11-24 11:49:18 +03:00
|
|
|
// reservation.
|
2017-01-13 06:40:38 +03:00
|
|
|
func (f *fundingManager) cancelReservationCtx(peerKey *btcec.PublicKey,
|
2017-07-31 01:53:53 +03:00
|
|
|
pendingChanID [32]byte) (*reservationWithCtx, error) {
|
2016-11-24 11:49:18 +03:00
|
|
|
|
2017-02-22 09:14:22 +03:00
|
|
|
fndgLog.Infof("Cancelling funding reservation for node_key=%x, "+
|
2017-09-18 20:33:37 +03:00
|
|
|
"chan_id=%x", peerKey.SerializeCompressed(), pendingChanID[:])
|
2017-02-22 09:14:22 +03:00
|
|
|
|
2018-06-01 10:18:55 +03:00
|
|
|
peerIDKey := newSerializedKey(peerKey)
|
|
|
|
f.resMtx.Lock()
|
|
|
|
defer f.resMtx.Unlock()
|
|
|
|
|
|
|
|
nodeReservations, ok := f.activeReservations[peerIDKey]
|
|
|
|
if !ok {
|
|
|
|
// No reservations for this node.
|
|
|
|
return nil, errors.Errorf("no active reservations for peer(%x)",
|
|
|
|
peerIDKey[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, ok := nodeReservations[pendingChanID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("unknown channel (id: %x) for "+
|
|
|
|
"peer(%x)", pendingChanID[:], peerIDKey[:])
|
2016-11-24 11:49:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.reservation.Cancel(); err != nil {
|
2017-07-31 01:53:53 +03:00
|
|
|
return nil, errors.Errorf("unable to cancel reservation: %v",
|
2016-11-24 11:49:18 +03:00
|
|
|
err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
2016-11-24 11:49:18 +03:00
|
|
|
|
2018-06-01 10:18:55 +03:00
|
|
|
delete(nodeReservations, pendingChanID)
|
|
|
|
|
|
|
|
// If this was the last active reservation for this peer, delete the
|
|
|
|
// peer's entry altogether.
|
|
|
|
if len(nodeReservations) == 0 {
|
|
|
|
delete(f.activeReservations, peerIDKey)
|
|
|
|
}
|
2016-11-24 11:49:18 +03:00
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// deleteReservationCtx deletes the reservation uniquely identified by the
|
|
|
|
// target public key of the peer, and the specified pending channel ID.
|
2017-04-17 01:34:23 +03:00
|
|
|
func (f *fundingManager) deleteReservationCtx(peerKey *btcec.PublicKey,
|
2017-07-31 01:53:53 +03:00
|
|
|
pendingChanID [32]byte) {
|
2017-04-17 01:34:23 +03:00
|
|
|
|
2016-11-24 11:49:18 +03:00
|
|
|
// TODO(roasbeef): possibly cancel funding barrier in peer's
|
|
|
|
// channelManager?
|
2017-01-13 06:40:38 +03:00
|
|
|
peerIDKey := newSerializedKey(peerKey)
|
2016-11-24 11:49:18 +03:00
|
|
|
f.resMtx.Lock()
|
2018-05-30 13:08:18 +03:00
|
|
|
defer f.resMtx.Unlock()
|
|
|
|
|
|
|
|
nodeReservations, ok := f.activeReservations[peerIDKey]
|
|
|
|
if !ok {
|
|
|
|
// No reservations for this node.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(nodeReservations, pendingChanID)
|
|
|
|
|
|
|
|
// If this was the last active reservation for this peer, delete the
|
|
|
|
// peer's entry altogether.
|
|
|
|
if len(nodeReservations) == 0 {
|
|
|
|
delete(f.activeReservations, peerIDKey)
|
|
|
|
}
|
2016-11-24 11:49:18 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
// getReservationCtx returns the reservation context for a particular pending
|
|
|
|
// channel ID for a target peer.
|
2017-01-13 06:40:38 +03:00
|
|
|
func (f *fundingManager) getReservationCtx(peerKey *btcec.PublicKey,
|
2017-07-31 01:53:53 +03:00
|
|
|
pendingChanID [32]byte) (*reservationWithCtx, error) {
|
2016-11-24 11:49:18 +03:00
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
peerIDKey := newSerializedKey(peerKey)
|
2016-11-24 11:49:18 +03:00
|
|
|
f.resMtx.RLock()
|
2017-07-31 01:53:53 +03:00
|
|
|
resCtx, ok := f.activeReservations[peerIDKey][pendingChanID]
|
2016-11-24 11:49:18 +03:00
|
|
|
f.resMtx.RUnlock()
|
|
|
|
|
|
|
|
if !ok {
|
2018-06-01 10:18:55 +03:00
|
|
|
return nil, errors.Errorf("unknown channel (id: %x) for "+
|
|
|
|
"peer(%x)", pendingChanID[:], peerIDKey[:])
|
2016-11-24 11:49:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resCtx, nil
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
2017-01-15 04:52:05 +03:00
|
|
|
|
2018-03-01 01:35:03 +03:00
|
|
|
// IsPendingChannel returns a boolean indicating whether the channel identified
|
|
|
|
// by the pendingChanID and given peer is pending, meaning it is in the process
|
|
|
|
// of being funded. After the funding transaction has been confirmed, the
|
|
|
|
// channel will receive a new, permanent channel ID, and will no longer be
|
|
|
|
// considered pending.
|
|
|
|
func (f *fundingManager) IsPendingChannel(pendingChanID [32]byte,
|
2018-07-05 23:38:11 +03:00
|
|
|
peerKey *btcec.PublicKey) bool {
|
2018-03-01 01:35:03 +03:00
|
|
|
|
2018-07-05 23:38:11 +03:00
|
|
|
peerIDKey := newSerializedKey(peerKey)
|
2018-03-01 01:35:03 +03:00
|
|
|
f.resMtx.RLock()
|
|
|
|
_, ok := f.activeReservations[peerIDKey][pendingChanID]
|
|
|
|
f.resMtx.RUnlock()
|
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2017-01-15 04:52:05 +03:00
|
|
|
func copyPubKey(pub *btcec.PublicKey) *btcec.PublicKey {
|
|
|
|
return &btcec.PublicKey{
|
|
|
|
Curve: btcec.S256(),
|
|
|
|
X: pub.X,
|
|
|
|
Y: pub.Y,
|
|
|
|
}
|
|
|
|
}
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
// saveChannelOpeningState saves the channelOpeningState for the provided
|
|
|
|
// chanPoint to the channelOpeningStateBucket.
|
|
|
|
func (f *fundingManager) saveChannelOpeningState(chanPoint *wire.OutPoint,
|
|
|
|
state channelOpeningState, shortChanID *lnwire.ShortChannelID) error {
|
2018-11-30 07:04:21 +03:00
|
|
|
return f.cfg.Wallet.Cfg.Database.Update(func(tx *bbolt.Tx) error {
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
bucket, err := tx.CreateBucketIfNotExists(channelOpeningStateBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var outpointBytes bytes.Buffer
|
|
|
|
if err = writeOutpoint(&outpointBytes, chanPoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save state and the uint64 representation of the shortChanID
|
|
|
|
// for later use.
|
|
|
|
scratch := make([]byte, 10)
|
|
|
|
byteOrder.PutUint16(scratch[:2], uint16(state))
|
|
|
|
byteOrder.PutUint64(scratch[2:], shortChanID.ToUint64())
|
|
|
|
|
2017-11-14 02:55:50 +03:00
|
|
|
return bucket.Put(outpointBytes.Bytes(), scratch)
|
2017-06-08 20:48:07 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// getChannelOpeningState fetches the channelOpeningState for the provided
|
|
|
|
// chanPoint from the database, or returns ErrChannelNotFound if the channel
|
|
|
|
// is not found.
|
|
|
|
func (f *fundingManager) getChannelOpeningState(chanPoint *wire.OutPoint) (
|
|
|
|
channelOpeningState, *lnwire.ShortChannelID, error) {
|
|
|
|
|
|
|
|
var state channelOpeningState
|
|
|
|
var shortChanID lnwire.ShortChannelID
|
2018-11-30 07:04:21 +03:00
|
|
|
err := f.cfg.Wallet.Cfg.Database.View(func(tx *bbolt.Tx) error {
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
bucket := tx.Bucket(channelOpeningStateBucket)
|
|
|
|
if bucket == nil {
|
|
|
|
// If the bucket does not exist, it means we never added
|
|
|
|
// a channel to the db, so return ErrChannelNotFound.
|
|
|
|
return ErrChannelNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
var outpointBytes bytes.Buffer
|
|
|
|
if err := writeOutpoint(&outpointBytes, chanPoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := bucket.Get(outpointBytes.Bytes())
|
|
|
|
if value == nil {
|
|
|
|
return ErrChannelNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
state = channelOpeningState(byteOrder.Uint16(value[:2]))
|
|
|
|
shortChanID = lnwire.NewShortChanIDFromInt(byteOrder.Uint64(value[2:]))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state, &shortChanID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteChannelOpeningState removes any state for chanPoint from the database.
|
|
|
|
func (f *fundingManager) deleteChannelOpeningState(chanPoint *wire.OutPoint) error {
|
2018-11-30 07:04:21 +03:00
|
|
|
return f.cfg.Wallet.Cfg.Database.Update(func(tx *bbolt.Tx) error {
|
2017-06-08 20:48:07 +03:00
|
|
|
bucket := tx.Bucket(channelOpeningStateBucket)
|
|
|
|
if bucket == nil {
|
|
|
|
return fmt.Errorf("Bucket not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
var outpointBytes bytes.Buffer
|
|
|
|
if err := writeOutpoint(&outpointBytes, chanPoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-14 02:55:50 +03:00
|
|
|
return bucket.Delete(outpointBytes.Bytes())
|
2017-06-08 20:48:07 +03:00
|
|
|
})
|
|
|
|
}
|