2016-06-21 21:31:10 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-12-27 08:42:23 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
2016-06-21 21:31:10 +03:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2016-12-27 08:42:23 +03:00
|
|
|
"time"
|
2016-06-21 21:31:10 +03:00
|
|
|
|
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"
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
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"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2017-01-06 00:56:27 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2016-06-21 21:31:10 +03:00
|
|
|
"github.com/roasbeef/btcd/txscript"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
2016-07-08 01:33:52 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
2016-11-29 06:43:57 +03:00
|
|
|
"google.golang.org/grpc"
|
2016-06-21 21:31:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TODO(roasbeef): tune
|
|
|
|
msgBufferSize = 50
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
// TODO(roasbeef): actually use the context package
|
|
|
|
// * deadlines, etc.
|
|
|
|
type reservationWithCtx struct {
|
|
|
|
reservation *lnwallet.ChannelReservation
|
2017-01-13 06:40:38 +03:00
|
|
|
peerAddress *lnwire.NetAddress
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2016-08-31 02:52:53 +03:00
|
|
|
updates chan *lnrpc.OpenStatusUpdate
|
|
|
|
err chan error
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2017-01-13 06:40:38 +03:00
|
|
|
peerAddress *lnwire.NetAddress
|
2016-06-21 21:31:10 +03:00
|
|
|
*openChanReq
|
|
|
|
}
|
|
|
|
|
|
|
|
// fundingRequestMsg couples an lnwire.SingleFundingRequest 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 fundingRequestMsg struct {
|
2017-01-13 06:40:38 +03:00
|
|
|
msg *lnwire.SingleFundingRequest
|
|
|
|
peerAddress *lnwire.NetAddress
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fundingResponseMsg couples an lnwire.SingleFundingResponse 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 fundingResponseMsg struct {
|
2017-01-13 06:40:38 +03:00
|
|
|
msg *lnwire.SingleFundingResponse
|
|
|
|
peerAddress *lnwire.NetAddress
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fundingCompleteMsg couples an lnwire.SingleFundingComplete 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 fundingCompleteMsg struct {
|
2017-01-13 06:40:38 +03:00
|
|
|
msg *lnwire.SingleFundingComplete
|
|
|
|
peerAddress *lnwire.NetAddress
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fundingSignCompleteMsg couples an lnwire.SingleFundingSignComplete 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 fundingSignCompleteMsg struct {
|
2017-01-13 06:40:38 +03:00
|
|
|
msg *lnwire.SingleFundingSignComplete
|
|
|
|
peerAddress *lnwire.NetAddress
|
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 {
|
|
|
|
msg *lnwire.FundingLocked
|
2017-01-13 06:40:38 +03:00
|
|
|
peerAddress *lnwire.NetAddress
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2016-10-15 16:24:56 +03:00
|
|
|
// fundingErrorMsg couples an lnwire.ErrorGeneric message
|
|
|
|
// with the peer who sent the message. This allows the funding
|
2017-01-13 08:01:50 +03:00
|
|
|
// manager to properly process the error.
|
2016-10-15 16:24:56 +03:00
|
|
|
type fundingErrorMsg struct {
|
2017-01-13 06:40:38 +03:00
|
|
|
err *lnwire.ErrorGeneric
|
|
|
|
peerAddress *lnwire.NetAddress
|
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
|
|
|
|
// pending single funded channels indexed by their pending channel identifier.
|
|
|
|
type pendingChannels map[uint64]*reservationWithCtx
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// ArbiterChan allows the FundingManager to notify the BreachArbiter
|
|
|
|
// that a new channel has been created that should be observed to
|
|
|
|
// ensure that the channel counterparty hasn't broadcasted an invalid
|
|
|
|
// commitment transaction.
|
|
|
|
ArbiterChan chan<- *lnwallet.LightningChannel
|
|
|
|
|
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
|
|
|
|
|
2017-02-25 03:16:13 +03:00
|
|
|
// SendToRouter is used by the FundingManager to announce newly created
|
2017-01-24 05:19:54 +03:00
|
|
|
// channels to the rest of the Lightning Network.
|
2017-02-25 03:16:13 +03:00
|
|
|
SendToRouter func(msg lnwire.Message)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
// SendToPeer allows the FundingManager to send messages to the peer
|
|
|
|
// node during the multiple steps involved in the creation of the
|
|
|
|
// channel's funding transaction and initial commitment transaction.
|
|
|
|
SendToPeer func(target *btcec.PublicKey, msgs ...lnwire.Message) error
|
|
|
|
|
|
|
|
// FindPeer searches the list of peers connected to the node so that
|
|
|
|
// the FundingManager can notify other daemon subsystems as necessary
|
|
|
|
// during the funding process.
|
|
|
|
FindPeer func(peerKey *btcec.PublicKey) (*peer, error)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
|
|
// FindChannel queries the database for the channel with the given
|
|
|
|
// funding transaction outpoint.
|
|
|
|
FindChannel func(chanPoint wire.OutPoint) (*lnwallet.LightningChannel, error)
|
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,
|
2017-01-13 08:01:50 +03:00
|
|
|
// either kicked-off locally or remotely 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 {
|
|
|
|
// MUST be used atomically.
|
|
|
|
started int32
|
|
|
|
stopped int32
|
|
|
|
|
2017-01-13 06:40:38 +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
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// channelReservations is a map which houses the state of all pending
|
|
|
|
// funding workflows.
|
|
|
|
resMtx sync.RWMutex
|
2017-01-13 06:40:38 +03:00
|
|
|
activeReservations map[serializedPubKey]pendingChannels
|
2016-11-29 06:43:57 +03:00
|
|
|
|
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-01-24 02:33:46 +03:00
|
|
|
// newChanBarriers is a map from a channel point 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.
|
|
|
|
barrierMtx sync.RWMutex
|
|
|
|
newChanBarriers map[wire.OutPoint]chan struct{}
|
|
|
|
|
2016-12-27 08:42:23 +03:00
|
|
|
fakeProof *channelProof
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-12-27 08:42:23 +03:00
|
|
|
// TODO(roasbeef): remove once we actually sign the funding_locked
|
|
|
|
// stuffs
|
|
|
|
s := "30450221008ce2bc69281ce27da07e6683571319d18e949ddfa2965fb6caa" +
|
|
|
|
"1bf0314f882d70220299105481d63e0f4bc2a88121167221b6700d72a0e" +
|
|
|
|
"ad154c03be696a292d24ae"
|
|
|
|
fakeSigHex, _ := hex.DecodeString(s)
|
|
|
|
fakeSig, _ := btcec.ParseSignature(fakeSigHex, btcec.S256())
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
return &fundingManager{
|
2017-01-13 06:40:38 +03:00
|
|
|
cfg: &cfg,
|
2016-11-29 06:43:57 +03:00
|
|
|
|
2016-12-27 08:42:23 +03:00
|
|
|
fakeProof: &channelProof{
|
|
|
|
nodeSig: fakeSig,
|
|
|
|
bitcoinSig: fakeSig,
|
|
|
|
},
|
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
activeReservations: make(map[serializedPubKey]pendingChannels),
|
2017-01-24 02:33:46 +03:00
|
|
|
newChanBarriers: make(map[wire.OutPoint]chan struct{}),
|
2016-06-21 21:31:10 +03:00
|
|
|
fundingMsgs: make(chan interface{}, msgBufferSize),
|
|
|
|
fundingRequests: make(chan *initFundingMsg, msgBufferSize),
|
2016-07-06 04:57:08 +03:00
|
|
|
queries: make(chan interface{}, 1),
|
2016-06-21 21:31:10 +03:00
|
|
|
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 {
|
|
|
|
if atomic.AddInt32(&f.started, 1) != 1 { // TODO(roasbeef): CAS instead
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
pendingChannels, err := f.cfg.Wallet.ChannelDB.FetchPendingChannels()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-25 03:16:13 +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 will also launch waitForFundingConfirmation to wait for
|
|
|
|
// the channel's funding transaction to be confirmed on the blockchain.
|
2017-01-24 05:19:54 +03:00
|
|
|
for _, channel := range pendingChannels {
|
|
|
|
f.barrierMtx.Lock()
|
2017-02-25 03:16:13 +03:00
|
|
|
fndgLog.Tracef("Loading pending ChannelPoint(%v), creating chan "+
|
|
|
|
"barrier", *channel.FundingOutpoint)
|
2017-01-24 05:19:54 +03:00
|
|
|
f.newChanBarriers[*channel.FundingOutpoint] = make(chan struct{})
|
|
|
|
f.barrierMtx.Unlock()
|
|
|
|
|
|
|
|
doneChan := make(chan struct{})
|
|
|
|
go f.waitForFundingConfirmation(channel, doneChan)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if atomic.AddInt32(&f.stopped, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-07-06 04:57:08 +03:00
|
|
|
type numPendingReq struct {
|
|
|
|
resp chan uint32
|
2017-01-23 10:31:01 +03:00
|
|
|
err chan error
|
2016-07-06 04:57:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NumPendingChannels returns the number of pending channels currently
|
|
|
|
// progressing through the reservation workflow.
|
2017-01-23 10:31:01 +03:00
|
|
|
func (f *fundingManager) NumPendingChannels() (uint32, error) {
|
|
|
|
respChan := make(chan uint32, 1)
|
|
|
|
errChan := make(chan error)
|
2016-07-06 04:57:08 +03:00
|
|
|
|
2017-01-23 10:31:01 +03:00
|
|
|
req := &numPendingReq{
|
|
|
|
resp: respChan,
|
|
|
|
err: errChan,
|
|
|
|
}
|
2016-07-06 04:57:08 +03:00
|
|
|
f.queries <- req
|
|
|
|
|
2017-01-23 10:31:01 +03:00
|
|
|
return <-respChan, <-errChan
|
2016-07-06 04:57:08 +03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
errChan := make(chan error)
|
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
|
|
|
f.queries <- req
|
|
|
|
|
2017-01-23 10:31:01 +03:00
|
|
|
return <-respChan, <-errChan
|
2016-07-08 01:33:52 +03:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-f.fundingMsgs:
|
|
|
|
switch fmsg := msg.(type) {
|
|
|
|
case *fundingRequestMsg:
|
|
|
|
f.handleFundingRequest(fmsg)
|
|
|
|
case *fundingResponseMsg:
|
|
|
|
f.handleFundingResponse(fmsg)
|
|
|
|
case *fundingCompleteMsg:
|
|
|
|
f.handleFundingComplete(fmsg)
|
|
|
|
case *fundingSignCompleteMsg:
|
|
|
|
f.handleFundingSignComplete(fmsg)
|
2017-01-24 05:19:54 +03:00
|
|
|
case *fundingLockedMsg:
|
|
|
|
f.handleFundingLocked(fmsg)
|
2016-10-15 16:24:56 +03:00
|
|
|
case *fundingErrorMsg:
|
|
|
|
f.handleErrorGenericMsg(fmsg)
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
case req := <-f.fundingRequests:
|
|
|
|
f.handleInitFundingMsg(req)
|
2016-07-06 04:57:08 +03:00
|
|
|
case req := <-f.queries:
|
|
|
|
switch msg := req.(type) {
|
|
|
|
case *numPendingReq:
|
2016-07-08 01:33:52 +03:00
|
|
|
f.handleNumPending(msg)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:33:52 +03:00
|
|
|
// handleNumPending handles a request for the total number of pending channels.
|
|
|
|
func (f *fundingManager) handleNumPending(msg *numPendingReq) {
|
|
|
|
var numPending uint32
|
|
|
|
for _, peerChannels := range f.activeReservations {
|
|
|
|
numPending += uint32(len(peerChannels))
|
|
|
|
}
|
2017-01-23 10:31:01 +03:00
|
|
|
|
|
|
|
dbPendingChannels, err := f.cfg.Wallet.ChannelDB.FetchPendingChannels()
|
|
|
|
if err != nil {
|
|
|
|
close(msg.resp)
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
numPending = numPending + uint32(len(dbPendingChannels))
|
|
|
|
|
2016-07-08 01:33:52 +03:00
|
|
|
msg.resp <- numPending
|
2017-01-23 10:31:01 +03:00
|
|
|
msg.err <- 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
|
|
|
|
|
|
|
dbPendingChannels, err := f.cfg.Wallet.ChannelDB.FetchPendingChannels()
|
|
|
|
if err != nil {
|
|
|
|
msg.resp <- nil
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dbPendingChan := range dbPendingChannels {
|
|
|
|
pendingChan := &pendingChannel{
|
|
|
|
identityPub: dbPendingChan.IdentityPub,
|
|
|
|
channelPoint: dbPendingChan.ChanID,
|
|
|
|
capacity: dbPendingChan.Capacity,
|
|
|
|
localBalance: dbPendingChan.OurBalance,
|
|
|
|
remoteBalance: dbPendingChan.TheirBalance,
|
|
|
|
}
|
|
|
|
|
|
|
|
pendingChannels = append(pendingChannels, pendingChan)
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:33:52 +03:00
|
|
|
msg.resp <- pendingChannels
|
2017-01-23 10:31:01 +03:00
|
|
|
msg.err <- nil
|
2016-07-08 01:33:52 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// processFundingRequest 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-01-13 06:40:38 +03:00
|
|
|
func (f *fundingManager) processFundingRequest(msg *lnwire.SingleFundingRequest,
|
|
|
|
peerAddress *lnwire.NetAddress) {
|
|
|
|
f.fundingMsgs <- &fundingRequestMsg{msg, peerAddress}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2016-11-24 11:49:18 +03:00
|
|
|
// handleFundingRequest creates an initial 'ChannelReservation' within
|
2016-06-21 21:31:10 +03:00
|
|
|
// the wallet, then responds to the source peer with a single funder response
|
|
|
|
// 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
|
|
|
|
func (f *fundingManager) handleFundingRequest(fmsg *fundingRequestMsg) {
|
2016-10-15 16:24:56 +03:00
|
|
|
// Check number of pending channels to be smaller than maximum allowed
|
|
|
|
// number and send ErrorGeneric to remote peer if condition is violated.
|
2017-01-13 06:40:38 +03:00
|
|
|
peerIDKey := newSerializedKey(fmsg.peerAddress.IdentityKey)
|
|
|
|
|
|
|
|
if len(f.activeReservations[peerIDKey]) >= cfg.MaxPendingChannels {
|
2016-10-15 16:24:56 +03:00
|
|
|
errMsg := &lnwire.ErrorGeneric{
|
2017-02-16 15:41:16 +03:00
|
|
|
ChannelPoint: wire.OutPoint{
|
2017-01-06 00:56:27 +03:00
|
|
|
Hash: chainhash.Hash{},
|
2016-10-15 16:24:56 +03:00
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Problem: "Number of pending channels exceed maximum",
|
2017-01-25 04:12:51 +03:00
|
|
|
Code: lnwire.ErrMaxPendingChannels,
|
|
|
|
PendingChannelID: fmsg.msg.ChannelID,
|
|
|
|
}
|
2017-01-13 06:40:38 +03:00
|
|
|
if err := f.cfg.SendToPeer(fmsg.peerAddress.IdentityKey, errMsg); err != nil {
|
2017-02-23 01:49:04 +03:00
|
|
|
fndgLog.Errorf("unable to send max pending channels "+
|
|
|
|
"message to peer: %v", err)
|
2017-01-13 06:40:38 +03:00
|
|
|
return
|
|
|
|
}
|
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-01-13 06:40:38 +03:00
|
|
|
isSynced, err := f.cfg.Wallet.IsSynced()
|
2017-01-25 04:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("unable to query wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !isSynced {
|
|
|
|
errMsg := &lnwire.ErrorGeneric{
|
2017-02-16 15:41:16 +03:00
|
|
|
ChannelPoint: wire.OutPoint{
|
2017-01-25 04:12:51 +03:00
|
|
|
Hash: chainhash.Hash{},
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
Problem: "Synchronizing blockchain",
|
|
|
|
Code: lnwire.ErrSynchronizingChain,
|
2016-10-15 16:24:56 +03:00
|
|
|
PendingChannelID: fmsg.msg.ChannelID,
|
|
|
|
}
|
2017-01-13 06:40:38 +03:00
|
|
|
if err := f.cfg.SendToPeer(fmsg.peerAddress.IdentityKey, errMsg); err != nil {
|
|
|
|
fndgLog.Errorf("unable to send error message to peer %v", err)
|
|
|
|
return
|
|
|
|
}
|
2016-10-15 16:24:56 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
msg := fmsg.msg
|
|
|
|
amt := msg.FundingAmount
|
|
|
|
delay := msg.CsvDelay
|
|
|
|
|
2016-07-22 02:21:27 +03:00
|
|
|
// TODO(roasbeef): error if funding flow already ongoing
|
2017-01-13 06:40:38 +03:00
|
|
|
fndgLog.Infof("Recv'd fundingRequest(amt=%v, delay=%v, pendingId=%v) "+
|
2017-02-23 02:52:02 +03:00
|
|
|
"from peer(%x)", amt, msg.PushSatoshis, delay, msg.ChannelID,
|
2017-01-13 06:40:38 +03:00
|
|
|
fmsg.peerAddress.IdentityKey.SerializeCompressed())
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2016-12-06 17:05:46 +03:00
|
|
|
ourDustLimit := lnwallet.DefaultDustLimit()
|
|
|
|
theirDustlimit := msg.DustLimit
|
2016-12-15 05:11:31 +03:00
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
// Attempt to initialize a reservation within the wallet. If the wallet
|
|
|
|
// 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.
|
|
|
|
// TODO(roasbeef): passing num confs 1 is irrelevant here, make signed?
|
2016-12-15 05:11:31 +03:00
|
|
|
// TODO(roasbeef): assuming this was an inbound connection, replace
|
|
|
|
// port with default advertised port
|
2017-01-13 06:40:38 +03:00
|
|
|
reservation, err := f.cfg.Wallet.InitChannelReservation(amt, 0,
|
2017-01-24 05:19:54 +03:00
|
|
|
fmsg.peerAddress.IdentityKey, fmsg.peerAddress.Address,
|
|
|
|
uint16(fmsg.msg.ConfirmationDepth), delay, ourDustLimit,
|
|
|
|
msg.PushSatoshis)
|
2016-06-21 21:31:10 +03:00
|
|
|
if err != nil {
|
2016-07-14 02:40:01 +03:00
|
|
|
// TODO(roasbeef): push ErrorGeneric message
|
2016-06-21 21:31:10 +03:00
|
|
|
fndgLog.Errorf("Unable to initialize reservation: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-06 17:05:46 +03:00
|
|
|
reservation.SetTheirDustLimit(theirDustlimit)
|
|
|
|
|
2016-10-15 16:18:38 +03:00
|
|
|
// Once the reservation has been created successfully, we add it to this
|
2016-06-21 21:31:10 +03:00
|
|
|
// peers map of pending reservations to track this particular reservation
|
|
|
|
// until either abort or completion.
|
|
|
|
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
|
|
|
}
|
2017-01-13 06:40:38 +03:00
|
|
|
f.activeReservations[peerIDKey][msg.ChannelID] = &reservationWithCtx{
|
2016-06-21 21:31:10 +03:00
|
|
|
reservation: reservation,
|
2017-02-22 09:14:22 +03:00
|
|
|
err: make(chan error, 1),
|
2017-01-13 06:40:38 +03:00
|
|
|
peerAddress: fmsg.peerAddress,
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
f.resMtx.Unlock()
|
|
|
|
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation := func() {
|
|
|
|
_, err := f.cancelReservationCtx(fmsg.peerAddress.IdentityKey, msg.ChannelID)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("unable to cancel reservation: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// With our portion of the reservation initialized, process the
|
2016-06-21 21:31:10 +03:00
|
|
|
// initiators contribution to the channel.
|
2016-07-14 04:37:50 +03:00
|
|
|
_, addrs, _, err := txscript.ExtractPkScriptAddrs(msg.DeliveryPkScript, activeNetParams.Params)
|
2016-06-21 21:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to extract addresses from script: %v", err)
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation()
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
contribution := &lnwallet.ChannelContribution{
|
|
|
|
FundingAmount: amt,
|
2017-01-15 04:52:05 +03:00
|
|
|
MultiSigKey: copyPubKey(msg.ChannelDerivationPoint),
|
|
|
|
CommitKey: copyPubKey(msg.CommitmentKey),
|
2016-06-21 21:31:10 +03:00
|
|
|
DeliveryAddress: addrs[0],
|
|
|
|
CsvDelay: delay,
|
|
|
|
}
|
|
|
|
if err := reservation.ProcessSingleContribution(contribution); err != nil {
|
|
|
|
fndgLog.Errorf("unable to add contribution reservation: %v", err)
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation()
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fndgLog.Infof("Sending fundingResp for pendingID(%v)", msg.ChannelID)
|
|
|
|
|
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()
|
|
|
|
deliveryScript, err := txscript.PayToAddrScript(ourContribution.DeliveryAddress)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("unable to convert address to pkscript: %v", err)
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation()
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fundingResp := lnwire.NewSingleFundingResponse(msg.ChannelID,
|
2016-06-30 22:14:50 +03:00
|
|
|
ourContribution.RevocationKey, ourContribution.CommitKey,
|
2016-06-21 21:31:10 +03:00
|
|
|
ourContribution.MultiSigKey, ourContribution.CsvDelay,
|
2017-01-24 05:19:54 +03:00
|
|
|
deliveryScript, ourDustLimit, msg.ConfirmationDepth)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
if err := f.cfg.SendToPeer(fmsg.peerAddress.IdentityKey, fundingResp); err != nil {
|
|
|
|
fndgLog.Errorf("unable to send funding response to peer: %v", err)
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation()
|
2017-01-13 06:40:38 +03:00
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// processFundingRequest sends a message to the fundingManager allowing it to
|
|
|
|
// continue the second phase of a funding workflow with the target peer.
|
2017-01-13 06:40:38 +03:00
|
|
|
func (f *fundingManager) processFundingResponse(msg *lnwire.SingleFundingResponse,
|
|
|
|
peerAddress *lnwire.NetAddress) {
|
|
|
|
f.fundingMsgs <- &fundingResponseMsg{msg, peerAddress}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleFundingResponse processes a response to the workflow initiation sent
|
|
|
|
// by the remote peer. This message then queues a message with the funding
|
|
|
|
// outpoint, and a commitment signature to the remote peer.
|
|
|
|
func (f *fundingManager) handleFundingResponse(fmsg *fundingResponseMsg) {
|
|
|
|
msg := fmsg.msg
|
2016-11-24 11:49:18 +03:00
|
|
|
chanID := fmsg.msg.ChannelID
|
2017-01-13 06:40:38 +03:00
|
|
|
peerKey := fmsg.peerAddress.IdentityKey
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
resCtx, err := f.getReservationCtx(peerKey, chanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
if err != nil {
|
2017-01-13 06:40:38 +03:00
|
|
|
fndgLog.Warnf("Can't find reservation (peerKey:%v, chanID:%v)",
|
|
|
|
peerKey, chanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation := func() {
|
|
|
|
if _, err := f.cancelReservationCtx(peerKey, chanID); err != nil {
|
|
|
|
fndgLog.Errorf("unable to cancel reservation: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
fndgLog.Infof("Recv'd fundingResponse for pendingID(%v)", msg.ChannelID)
|
|
|
|
|
2016-12-06 17:05:46 +03:00
|
|
|
resCtx.reservation.SetTheirDustLimit(msg.DustLimit)
|
|
|
|
|
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-02-16 15:41:16 +03:00
|
|
|
_, addrs, _, err := txscript.ExtractPkScriptAddrs(msg.DeliveryPkScript,
|
|
|
|
activeNetParams.Params)
|
2016-06-21 21:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to extract addresses from script: %v", err)
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation()
|
2016-08-31 02:52:53 +03:00
|
|
|
resCtx.err <- err
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
contribution := &lnwallet.ChannelContribution{
|
|
|
|
FundingAmount: 0,
|
2017-01-15 04:52:05 +03:00
|
|
|
MultiSigKey: copyPubKey(msg.ChannelDerivationPoint),
|
|
|
|
CommitKey: copyPubKey(msg.CommitmentKey),
|
2016-06-21 21:31:10 +03:00
|
|
|
DeliveryAddress: addrs[0],
|
2017-01-15 04:52:05 +03:00
|
|
|
RevocationKey: copyPubKey(msg.RevocationKey),
|
2016-06-21 21:31:10 +03:00
|
|
|
CsvDelay: msg.CsvDelay,
|
|
|
|
}
|
|
|
|
if err := resCtx.reservation.ProcessContribution(contribution); err != nil {
|
|
|
|
fndgLog.Errorf("Unable to process contribution from %v: %v",
|
2017-01-13 06:40:38 +03:00
|
|
|
fmsg.peerAddress.IdentityKey, err)
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation()
|
2016-08-31 02:52:53 +03:00
|
|
|
resCtx.err <- err
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
commitSig, err := btcec.ParseSignature(sig, btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to parse signature: %v", err)
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation()
|
2016-08-31 02:52:53 +03:00
|
|
|
resCtx.err <- err
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
fndgLog.Debugf("Creating chan barrier for "+
|
|
|
|
"ChannelPoint(%v)", outPoint)
|
|
|
|
f.newChanBarriers[*outPoint] = make(chan struct{})
|
|
|
|
f.barrierMtx.Unlock()
|
2016-07-13 03:38:09 +03:00
|
|
|
|
2016-11-24 11:49:18 +03:00
|
|
|
fndgLog.Infof("Generated ChannelPoint(%v) for pendingID(%v)", outPoint,
|
|
|
|
chanID)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2016-06-30 22:14:50 +03:00
|
|
|
revocationKey := resCtx.reservation.OurContribution().RevocationKey
|
2016-11-16 23:54:27 +03:00
|
|
|
obsfucator := resCtx.reservation.StateNumObfuscator()
|
|
|
|
|
2017-02-16 15:41:16 +03:00
|
|
|
fundingComplete := lnwire.NewSingleFundingComplete(chanID, *outPoint,
|
2016-11-24 11:49:18 +03:00
|
|
|
commitSig, revocationKey, obsfucator)
|
2017-01-13 06:40:38 +03:00
|
|
|
|
|
|
|
if err := f.cfg.SendToPeer(fmsg.peerAddress.IdentityKey, fundingComplete); err != nil {
|
|
|
|
fndgLog.Errorf("Unable to send funding complete message: %v", err)
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation()
|
2017-01-13 06:40:38 +03:00
|
|
|
resCtx.err <- err
|
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// processFundingComplete queues a funding complete message coupled with the
|
|
|
|
// source peer to the fundingManager.
|
2017-01-13 06:40:38 +03:00
|
|
|
func (f *fundingManager) processFundingComplete(msg *lnwire.SingleFundingComplete,
|
|
|
|
peerAddress *lnwire.NetAddress) {
|
|
|
|
f.fundingMsgs <- &fundingCompleteMsg{msg, peerAddress}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleFundingComplete progresses the funding workflow when the daemon is on
|
|
|
|
// 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.
|
|
|
|
func (f *fundingManager) handleFundingComplete(fmsg *fundingCompleteMsg) {
|
2017-01-13 06:40:38 +03:00
|
|
|
peerKey := fmsg.peerAddress.IdentityKey
|
|
|
|
chanID := fmsg.msg.ChannelID
|
|
|
|
|
|
|
|
resCtx, err := f.getReservationCtx(peerKey, chanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
if err != nil {
|
2017-01-13 08:01:50 +03:00
|
|
|
fndgLog.Warnf("can't find reservation (peerID:%v, chanID:%v)",
|
2017-01-13 06:40:38 +03:00
|
|
|
peerKey, chanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
return
|
|
|
|
}
|
2016-06-21 21:31:10 +03:00
|
|
|
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation := func() {
|
|
|
|
if _, err := f.cancelReservationCtx(peerKey, chanID); err != nil {
|
|
|
|
fndgLog.Errorf("unable to cancel reservation: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// inititator's commitment transaction, then send our own if it's valid.
|
|
|
|
// TODO(roasbeef): make case (p vs P) consistent throughout
|
|
|
|
fundingOut := fmsg.msg.FundingOutPoint
|
|
|
|
fndgLog.Infof("completing pendingID(%v) with ChannelPoint(%v)",
|
2016-11-16 23:54:27 +03:00
|
|
|
chanID, fundingOut,
|
2016-06-21 21:31:10 +03:00
|
|
|
)
|
|
|
|
|
2017-01-15 04:52:05 +03:00
|
|
|
revokeKey := copyPubKey(fmsg.msg.RevocationKey)
|
2016-11-16 23:54:27 +03:00
|
|
|
obsfucator := fmsg.msg.StateHintObsfucator
|
|
|
|
commitSig := fmsg.msg.CommitSignature.Serialize()
|
|
|
|
|
|
|
|
// 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-01-24 05:19:54 +03:00
|
|
|
completeChan, err := resCtx.reservation.CompleteReservationSingle(
|
|
|
|
revokeKey, &fundingOut, commitSig, obsfucator)
|
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)
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation()
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// With their signature for our version of the commitment transaction
|
|
|
|
// verified, we can now send over our signature to the remote peer.
|
|
|
|
// TODO(roasbeef): just have raw bytes in wire msg? avoids decoding
|
|
|
|
// then decoding shortly afterwards.
|
|
|
|
_, sig := resCtx.reservation.OurSignatures()
|
|
|
|
ourCommitSig, err := btcec.ParseSignature(sig, btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("unable to parse signature: %v", err)
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation()
|
2016-06-21 21:31:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
fndgLog.Debugf("Creating chan barrier for "+
|
|
|
|
"ChannelPoint(%v)", fundingOut)
|
|
|
|
f.newChanBarriers[fundingOut] = make(chan struct{})
|
|
|
|
f.barrierMtx.Unlock()
|
2016-07-13 03:38:09 +03:00
|
|
|
|
2016-06-21 21:31:10 +03:00
|
|
|
fndgLog.Infof("sending signComplete for pendingID(%v) over ChannelPoint(%v)",
|
2017-01-13 06:40:38 +03:00
|
|
|
chanID, fundingOut)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
|
|
signComplete := lnwire.NewSingleFundingSignComplete(chanID, ourCommitSig)
|
2017-01-13 06:40:38 +03:00
|
|
|
if err := f.cfg.SendToPeer(peerKey, signComplete); err != nil {
|
|
|
|
fndgLog.Errorf("unable to send signComplete message: %v", err)
|
2017-02-22 09:14:22 +03:00
|
|
|
cancelReservation()
|
2017-01-13 06:40:38 +03:00
|
|
|
return
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
doneChan := make(chan struct{})
|
|
|
|
go f.waitForFundingConfirmation(completeChan, doneChan)
|
|
|
|
|
|
|
|
<-doneChan
|
|
|
|
f.deleteReservationCtx(peerKey, fmsg.msg.ChannelID)
|
|
|
|
}()
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// processFundingSignComplete sends a single funding sign complete message
|
|
|
|
// along with the source peer to the funding manager.
|
2017-01-13 06:40:38 +03:00
|
|
|
func (f *fundingManager) processFundingSignComplete(msg *lnwire.SingleFundingSignComplete,
|
|
|
|
peerAddress *lnwire.NetAddress) {
|
|
|
|
f.fundingMsgs <- &fundingSignCompleteMsg{msg, peerAddress}
|
2016-06-21 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
// handleFundingSignComplete processes the final message received in a single
|
|
|
|
// funder workflow. Once this message is processed, the funding transaction is
|
|
|
|
// 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.
|
|
|
|
func (f *fundingManager) handleFundingSignComplete(fmsg *fundingSignCompleteMsg) {
|
|
|
|
chanID := fmsg.msg.ChannelID
|
|
|
|
peerKey := fmsg.peerAddress.IdentityKey
|
|
|
|
|
|
|
|
resCtx, err := f.getReservationCtx(peerKey, chanID)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Warnf("can't find reservation (peerID:%v, chanID:%v)",
|
|
|
|
peerKey, chanID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
commitSig := fmsg.msg.CommitSignature.Serialize()
|
|
|
|
completeChan, err := resCtx.reservation.CompleteReservation(nil, commitSig)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("unable to complete reservation sign complete: %v", err)
|
|
|
|
resCtx.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fundingPoint := resCtx.reservation.FundingOutpoint()
|
|
|
|
fndgLog.Infof("Finalizing pendingID(%v) over ChannelPoint(%v), "+
|
|
|
|
"waiting for channel open on-chain", chanID, fundingPoint)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
resCtx.updates <- &lnrpc.OpenStatusUpdate{
|
|
|
|
Update: &lnrpc.OpenStatusUpdate_ChanPending{
|
|
|
|
ChanPending: &lnrpc.PendingUpdate{
|
|
|
|
Txid: fundingPoint.Hash[:],
|
|
|
|
OutputIndex: fundingPoint.Index,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
doneChan := make(chan struct{})
|
|
|
|
go f.waitForFundingConfirmation(completeChan, doneChan)
|
|
|
|
|
|
|
|
<-doneChan
|
|
|
|
|
|
|
|
// Finally give the caller a final update notifying them that
|
|
|
|
// the channel is now open.
|
|
|
|
// TODO(roasbeef): helper funcs for proto construction
|
|
|
|
resCtx.updates <- &lnrpc.OpenStatusUpdate{
|
|
|
|
Update: &lnrpc.OpenStatusUpdate_ChanOpen{
|
|
|
|
ChanOpen: &lnrpc.ChannelOpenUpdate{
|
|
|
|
ChannelPoint: &lnrpc.ChannelPoint{
|
|
|
|
FundingTxid: fundingPoint.Hash[:],
|
|
|
|
OutputIndex: fundingPoint.Index,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
f.deleteReservationCtx(peerKey, fmsg.msg.ChannelID)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (f *fundingManager) waitForFundingConfirmation(
|
|
|
|
completeChan *channeldb.OpenChannel, doneChan chan struct{}) {
|
|
|
|
|
|
|
|
// Register with the ChainNotifier for a notification once the funding
|
|
|
|
// transaction reaches `numConfs` confirmations.
|
|
|
|
txid := completeChan.FundingOutpoint.Hash
|
|
|
|
numConfs := uint32(completeChan.NumConfsRequired)
|
|
|
|
confNtfn, _ := f.cfg.Notifier.RegisterConfirmationsNtfn(&txid, numConfs)
|
|
|
|
|
|
|
|
fndgLog.Infof("Waiting for funding tx (%v) to reach %v confirmations",
|
|
|
|
txid, numConfs)
|
|
|
|
|
|
|
|
// Wait until the specified number of confirmations has been reached,
|
|
|
|
// or the wallet signals a shutdown.
|
|
|
|
confDetails := <-confNtfn.Confirmed
|
|
|
|
|
|
|
|
fundingPoint := *completeChan.FundingOutpoint
|
|
|
|
fndgLog.Infof("ChannelPoint(%v) is now active",
|
|
|
|
fundingPoint)
|
|
|
|
|
|
|
|
completeChan.IsPending = false
|
|
|
|
err := f.cfg.Wallet.ChannelDB.MarkChannelAsOpen(&fundingPoint)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("error setting channel pending flag to false: "+
|
|
|
|
"%v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, create and officially open the payment channel!
|
|
|
|
// TODO(roasbeef): CreationTime once tx is 'open'
|
|
|
|
channel, err := lnwallet.NewLightningChannel(f.cfg.Wallet.Signer,
|
|
|
|
f.cfg.Notifier, completeChan)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("error creating new lightning channel: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the channel is open, we need to notify a number of
|
|
|
|
// parties of this event.
|
|
|
|
|
|
|
|
// First we send the newly opened channel to the source server
|
|
|
|
peer, err := f.cfg.FindPeer(completeChan.IdentityPub)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to find peer: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newChanDone := make(chan struct{})
|
|
|
|
newChanMsg := &newChannelMsg{
|
|
|
|
channel: channel,
|
|
|
|
done: newChanDone,
|
|
|
|
}
|
|
|
|
peer.newChannels <- newChanMsg
|
|
|
|
|
|
|
|
<-newChanDone
|
|
|
|
|
|
|
|
// Close the active channel barrier signalling the
|
|
|
|
// readHandler that commitment related modifications to
|
|
|
|
// this channel can now proceed.
|
|
|
|
f.barrierMtx.Lock()
|
|
|
|
fndgLog.Tracef("Closing chan barrier for ChannelPoint(%v)", fundingPoint)
|
|
|
|
close(f.newChanBarriers[fundingPoint])
|
|
|
|
delete(f.newChanBarriers, fundingPoint)
|
|
|
|
f.barrierMtx.Unlock()
|
|
|
|
|
|
|
|
// Afterwards we send the breach arbiter the new channel so it
|
|
|
|
// can watch for attempts to breach the channel's contract by
|
|
|
|
// the remote party.
|
|
|
|
f.cfg.ArbiterChan <- channel
|
|
|
|
|
|
|
|
// With the block height and the transaction index known, we
|
|
|
|
// can construct the compact chainID which is used on the
|
|
|
|
// network to unique identify channels.
|
|
|
|
chanID := lnwire.ChannelID{
|
|
|
|
BlockHeight: confDetails.BlockHeight,
|
|
|
|
TxIndex: confDetails.TxIndex,
|
|
|
|
TxPosition: uint16(fundingPoint.Index),
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the funding transaction has been confirmed, the FundingLocked
|
|
|
|
// message is sent to the remote peer so that the existence of the
|
|
|
|
// channel can be announced to the network.
|
|
|
|
fundingLockedMsg := lnwire.NewFundingLocked(fundingPoint, chanID,
|
|
|
|
f.cfg.IDKey)
|
|
|
|
f.cfg.SendToPeer(completeChan.IdentityPub, fundingLockedMsg)
|
|
|
|
|
|
|
|
close(doneChan)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// processFundingLocked sends a message to the fundingManager allowing it to finish
|
|
|
|
// the funding workflow.
|
|
|
|
func (f *fundingManager) processFundingLocked(msg *lnwire.FundingLocked,
|
|
|
|
peerAddress *lnwire.NetAddress) {
|
|
|
|
f.fundingMsgs <- &fundingLockedMsg{msg, peerAddress}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleFundingLocked finalizes the channel funding process and enables the channel
|
|
|
|
// to enter normal operating mode.
|
|
|
|
func (f *fundingManager) handleFundingLocked(fmsg *fundingLockedMsg) {
|
|
|
|
fundingPoint := fmsg.msg.ChannelOutpoint
|
|
|
|
channel, err := f.cfg.FindChannel(fundingPoint)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("error looking up channel for outpoint: %v", fundingPoint)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the new link with the L3 routing manager so this
|
|
|
|
// new channel can be utilized during path
|
|
|
|
// finding.
|
2017-01-31 05:45:28 +03:00
|
|
|
go f.announceChannel(f.cfg.IDKey, fmsg.peerAddress.IdentityKey, channel,
|
2017-01-24 05:19:54 +03:00
|
|
|
fmsg.msg.ChannelID, f.fakeProof, f.fakeProof)
|
|
|
|
}
|
|
|
|
|
2016-12-27 08:42:23 +03:00
|
|
|
// channelProof is one half of the proof necessary to create an authenticated
|
|
|
|
// announcement on the network. The two signatures individually sign a
|
|
|
|
// statement of the existence of a channel.
|
|
|
|
type channelProof struct {
|
|
|
|
nodeSig *btcec.Signature
|
|
|
|
bitcoinSig *btcec.Signature
|
|
|
|
}
|
|
|
|
|
|
|
|
// chanAnnouncement encapsulates the two authenticated announcements that we
|
|
|
|
// send out to the network after a new channel has been created locally.
|
|
|
|
type chanAnnouncement struct {
|
|
|
|
chanAnn *lnwire.ChannelAnnouncement
|
|
|
|
edgeUpdate *lnwire.ChannelUpdateAnnouncement
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-01-24 05:19:54 +03:00
|
|
|
func newChanAnnouncement(localIdentity, remotePub *btcec.PublicKey,
|
2016-12-27 08:42:23 +03:00
|
|
|
channel *lnwallet.LightningChannel, chanID lnwire.ChannelID,
|
|
|
|
localProof, remoteProof *channelProof) *chanAnnouncement {
|
|
|
|
|
|
|
|
// The unconditional section of the announcement is the ChannelID
|
|
|
|
// itself which compactly encodes the location of the funding output
|
|
|
|
// within the blockchain.
|
|
|
|
chanAnn := &lnwire.ChannelAnnouncement{
|
|
|
|
ChannelID: chanID,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
var chanFlags uint16
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
selfBytes := localIdentity.SerializeCompressed()
|
|
|
|
remoteBytes := remotePub.SerializeCompressed()
|
|
|
|
if bytes.Compare(selfBytes, remoteBytes) == -1 {
|
2017-01-24 05:19:54 +03:00
|
|
|
chanAnn.FirstNodeID = localIdentity
|
|
|
|
chanAnn.SecondNodeID = remotePub
|
2016-12-27 08:42:23 +03:00
|
|
|
chanAnn.FirstNodeSig = localProof.nodeSig
|
|
|
|
chanAnn.SecondNodeSig = remoteProof.nodeSig
|
|
|
|
chanAnn.FirstBitcoinSig = localProof.nodeSig
|
|
|
|
chanAnn.SecondBitcoinSig = remoteProof.nodeSig
|
|
|
|
chanAnn.FirstBitcoinKey = channel.LocalFundingKey
|
|
|
|
chanAnn.SecondBitcoinKey = channel.RemoteFundingKey
|
|
|
|
|
|
|
|
// If we're the first node then update the chanFlags to
|
|
|
|
// indicate the "direction" of the update.
|
|
|
|
chanFlags = 0
|
|
|
|
} else {
|
2017-01-24 05:19:54 +03:00
|
|
|
chanAnn.FirstNodeID = remotePub
|
|
|
|
chanAnn.SecondNodeID = localIdentity
|
2016-12-27 08:42:23 +03:00
|
|
|
chanAnn.FirstNodeSig = remoteProof.nodeSig
|
|
|
|
chanAnn.SecondNodeSig = localProof.nodeSig
|
|
|
|
chanAnn.FirstBitcoinSig = remoteProof.nodeSig
|
|
|
|
chanAnn.SecondBitcoinSig = localProof.nodeSig
|
|
|
|
chanAnn.FirstBitcoinKey = channel.RemoteFundingKey
|
|
|
|
chanAnn.SecondBitcoinKey = channel.LocalFundingKey
|
|
|
|
|
|
|
|
// If we're the second node then update the chanFlags to
|
|
|
|
// indicate the "direction" of the update.
|
|
|
|
chanFlags = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): add real sig, populate proper FeeSchema
|
|
|
|
chanUpdateAnn := &lnwire.ChannelUpdateAnnouncement{
|
|
|
|
Signature: localProof.nodeSig,
|
|
|
|
ChannelID: chanID,
|
|
|
|
Timestamp: uint32(time.Now().Unix()),
|
|
|
|
Flags: chanFlags,
|
|
|
|
Expiry: 1,
|
|
|
|
HtlcMinimumMstat: 0,
|
|
|
|
FeeBaseMstat: 0,
|
|
|
|
FeeProportionalMillionths: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &chanAnnouncement{
|
|
|
|
chanAnn: chanAnn,
|
|
|
|
edgeUpdate: chanUpdateAnn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// announceChannel announces a newly created channel to the rest of the network
|
2017-01-13 08:01:50 +03:00
|
|
|
// by crafting the two authenticated announcements required for the peers on the
|
2016-12-27 08:42:23 +03:00
|
|
|
// network to recognize the legitimacy of the channel. The crafted
|
|
|
|
// announcements are then send to the channel router to handle broadcasting to
|
|
|
|
// the network during its next trickle.
|
2017-01-24 05:19:54 +03:00
|
|
|
func (f *fundingManager) announceChannel(idKey, remoteIdKey *btcec.PublicKey,
|
|
|
|
channel *lnwallet.LightningChannel, chanID lnwire.ChannelID, localProof,
|
|
|
|
remoteProof *channelProof) {
|
2016-12-27 08:42:23 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): need a Signer.SignMessage method to finalize
|
|
|
|
// advertisements
|
2017-01-24 05:19:54 +03:00
|
|
|
chanAnnouncement := newChanAnnouncement(idKey, remoteIdKey, channel, chanID,
|
|
|
|
localProof, remoteProof)
|
2016-12-27 08:42:23 +03:00
|
|
|
|
2017-02-25 03:16:13 +03:00
|
|
|
f.cfg.SendToRouter(chanAnnouncement.chanAnn)
|
|
|
|
f.cfg.SendToRouter(chanAnnouncement.edgeUpdate)
|
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..
|
2017-01-13 06:40:38 +03:00
|
|
|
func (f *fundingManager) initFundingWorkflow(peerAddress *lnwire.NetAddress,
|
|
|
|
req *openChanReq) {
|
2016-06-21 21:31:10 +03:00
|
|
|
f.fundingRequests <- &initFundingMsg{
|
2017-01-13 06:40:38 +03:00
|
|
|
peerAddress: peerAddress,
|
2016-06-21 21:31:10 +03:00
|
|
|
openChanReq: req,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (
|
|
|
|
// TODO(roasbeef): add delay
|
2017-01-13 06:40:38 +03:00
|
|
|
peerKey = msg.peerAddress.IdentityKey
|
2016-12-06 17:05:46 +03:00
|
|
|
localAmt = msg.localFundingAmt
|
|
|
|
remoteAmt = msg.remoteFundingAmt
|
|
|
|
capacity = localAmt + remoteAmt
|
|
|
|
numConfs = msg.numConfs
|
|
|
|
ourDustLimit = lnwallet.DefaultDustLimit()
|
2016-10-26 02:43:55 +03:00
|
|
|
)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
|
|
fndgLog.Infof("Initiating fundingRequest(localAmt=%v, remoteAmt=%v, "+
|
2016-12-06 17:05:46 +03:00
|
|
|
"capacity=%v, numConfs=%v, addr=%v, dustLimit=%v)", localAmt,
|
2017-01-24 05:19:54 +03:00
|
|
|
msg.pushAmt, capacity, numConfs, msg.peerAddress.Address,
|
|
|
|
ourDustLimit)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
|
|
// Initialize a funding reservation with the local wallet. If the
|
|
|
|
// wallet doesn't have enough funds to commit to this channel, then
|
|
|
|
// the request will fail, and be aborted.
|
2017-01-24 05:19:54 +03:00
|
|
|
reservation, err := f.cfg.Wallet.InitChannelReservation(capacity,
|
|
|
|
localAmt, peerKey, msg.peerAddress.Address, uint16(numConfs), 4,
|
|
|
|
ourDustLimit, msg.pushAmt)
|
2016-06-21 21:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtain a new pending channel ID which is used to track this
|
|
|
|
// reservation throughout its lifetime.
|
2017-01-13 06:40:38 +03:00
|
|
|
peer, err := f.cfg.FindPeer(peerKey)
|
|
|
|
if err != nil {
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
chanID := peer.fetchNextPendingChanID()
|
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
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
f.activeReservations[peerIDKey][chanID] = &reservationWithCtx{
|
2016-06-21 21:31:10 +03:00
|
|
|
reservation: reservation,
|
2017-01-13 06:40:38 +03:00
|
|
|
peerAddress: msg.peerAddress,
|
2016-08-31 02:52:53 +03:00
|
|
|
updates: msg.updates,
|
2016-06-21 21:31:10 +03:00
|
|
|
err: msg.err,
|
|
|
|
}
|
|
|
|
f.resMtx.Unlock()
|
|
|
|
|
|
|
|
// Once the reservation has been created, and indexed, queue a funding
|
|
|
|
// request to the remote peer, kicking off the funding workflow.
|
|
|
|
contribution := reservation.OurContribution()
|
|
|
|
deliveryScript, err := txscript.PayToAddrScript(contribution.DeliveryAddress)
|
|
|
|
if err != nil {
|
|
|
|
fndgLog.Errorf("Unable to convert address to pkscript: %v", err)
|
|
|
|
msg.err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fndgLog.Infof("Starting funding workflow with for pendingID(%v)", chanID)
|
|
|
|
|
|
|
|
// TODO(roasbeef): add FundingRequestFromContribution func
|
|
|
|
// TODO(roasbeef): need to set fee/kb
|
|
|
|
fundingReq := lnwire.NewSingleFundingRequest(
|
|
|
|
chanID,
|
|
|
|
msg.channelType,
|
|
|
|
msg.coinType,
|
|
|
|
0, // TODO(roasbeef): grab from fee estimation model
|
2016-09-13 05:05:56 +03:00
|
|
|
capacity,
|
2016-06-21 21:31:10 +03:00
|
|
|
contribution.CsvDelay,
|
|
|
|
contribution.CommitKey,
|
|
|
|
contribution.MultiSigKey,
|
|
|
|
deliveryScript,
|
2016-12-06 17:05:46 +03:00
|
|
|
ourDustLimit,
|
2017-01-10 06:05:11 +03:00
|
|
|
msg.pushAmt,
|
2017-01-24 05:19:54 +03:00
|
|
|
numConfs,
|
2016-06-21 21:31:10 +03:00
|
|
|
)
|
2017-01-13 06:40:38 +03:00
|
|
|
if err := f.cfg.SendToPeer(peerKey, fundingReq); err != nil {
|
|
|
|
fndgLog.Errorf("Unable to send funding request message: %v", err)
|
|
|
|
msg.err <- err
|
|
|
|
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.
|
|
|
|
func (f *fundingManager) waitUntilChannelOpen(targetChan wire.OutPoint) {
|
|
|
|
f.barrierMtx.RLock()
|
|
|
|
barrier, ok := f.newChanBarriers[targetChan]
|
|
|
|
f.barrierMtx.RUnlock()
|
|
|
|
if ok {
|
|
|
|
fndgLog.Tracef("waiting for chan barrier signal for "+
|
|
|
|
"ChannelPoint(%v)", targetChan)
|
|
|
|
select {
|
|
|
|
case <-barrier:
|
|
|
|
case <-f.quit: // TODO(roasbeef): add timer?
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fndgLog.Tracef("barrier for ChannelPoint(%v) closed",
|
|
|
|
targetChan)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 23:39:54 +03:00
|
|
|
// processErrorGeneric sends a message to the fundingManager allowing it to
|
|
|
|
// process the occurred generic error.
|
2016-10-15 16:24:56 +03:00
|
|
|
func (f *fundingManager) processErrorGeneric(err *lnwire.ErrorGeneric,
|
2017-01-13 06:40:38 +03:00
|
|
|
peerAddress *lnwire.NetAddress) {
|
2016-10-23 23:39:54 +03:00
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
f.fundingMsgs <- &fundingErrorMsg{err, peerAddress}
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleErrorGenericMsg process 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.
|
2016-10-15 16:24:56 +03:00
|
|
|
func (f *fundingManager) handleErrorGenericMsg(fmsg *fundingErrorMsg) {
|
2016-11-24 11:49:18 +03:00
|
|
|
e := fmsg.err
|
|
|
|
|
|
|
|
switch e.Code {
|
2017-01-25 04:12:51 +03:00
|
|
|
case lnwire.ErrMaxPendingChannels:
|
|
|
|
fallthrough
|
|
|
|
case lnwire.ErrSynchronizingChain:
|
2017-01-13 06:40:38 +03:00
|
|
|
peerKey := fmsg.peerAddress.IdentityKey
|
2016-10-15 16:24:56 +03:00
|
|
|
chanID := fmsg.err.PendingChannelID
|
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
ctx, err := f.cancelReservationCtx(peerKey, chanID)
|
2017-01-25 04:12:51 +03:00
|
|
|
if err != nil {
|
2016-11-24 11:49:18 +03:00
|
|
|
fndgLog.Warnf("unable to delete reservation: %v", err)
|
2017-02-22 09:14:22 +03:00
|
|
|
ctx.err <- err
|
2016-10-24 04:25:48 +03:00
|
|
|
return
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
fndgLog.Errorf("Received funding error from %v: %v",
|
|
|
|
peerKey.SerializeCompressed(), newLogClosure(func() string {
|
2017-01-25 04:12:51 +03:00
|
|
|
return spew.Sdump(e)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
ctx.err <- grpc.Errorf(e.Code.ToGrpcCode(), e.Problem)
|
2017-01-25 04:12:51 +03:00
|
|
|
return
|
|
|
|
|
2016-10-23 23:39:54 +03:00
|
|
|
default:
|
2016-11-24 11:49:18 +03:00
|
|
|
fndgLog.Warnf("unknown funding error (%v:%v)", e.Code, e.Problem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancelReservationCtx do all needed work in order to securely cancel the
|
|
|
|
// reservation.
|
2017-01-13 06:40:38 +03:00
|
|
|
func (f *fundingManager) cancelReservationCtx(peerKey *btcec.PublicKey,
|
2016-11-24 11:49:18 +03:00
|
|
|
chanID uint64) (*reservationWithCtx, error) {
|
|
|
|
|
2017-02-22 09:14:22 +03:00
|
|
|
fndgLog.Infof("Cancelling funding reservation for node_key=%x, "+
|
|
|
|
"chan_id=%v", peerKey.SerializeCompressed(), chanID)
|
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
ctx, err := f.getReservationCtx(peerKey, chanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("can't find reservation: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.reservation.Cancel(); err != nil {
|
|
|
|
return nil, errors.Errorf("can't cancel reservation: %v",
|
|
|
|
err)
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
2016-11-24 11:49:18 +03:00
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
f.deleteReservationCtx(peerKey, chanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteReservationCtx is needed in order to securely delete the reservation.
|
2017-01-13 06:40:38 +03:00
|
|
|
func (f *fundingManager) deleteReservationCtx(peerKey *btcec.PublicKey, chanID uint64) {
|
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()
|
2017-01-13 06:40:38 +03:00
|
|
|
delete(f.activeReservations[peerIDKey], chanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
f.resMtx.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// getReservationCtx returns the reservation context by peer id and channel id.
|
2017-01-13 06:40:38 +03:00
|
|
|
func (f *fundingManager) getReservationCtx(peerKey *btcec.PublicKey,
|
2016-11-24 11:49:18 +03:00
|
|
|
chanID uint64) (*reservationWithCtx, error) {
|
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
peerIDKey := newSerializedKey(peerKey)
|
2016-11-24 11:49:18 +03:00
|
|
|
f.resMtx.RLock()
|
2017-01-13 06:40:38 +03:00
|
|
|
resCtx, ok := f.activeReservations[peerIDKey][chanID]
|
2016-11-24 11:49:18 +03:00
|
|
|
f.resMtx.RUnlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("unknown channel (id: %v)", chanID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resCtx, nil
|
2016-10-15 16:24:56 +03:00
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
}
|