2016-06-21 21:31:10 +03:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
"sync/atomic"
|
2016-12-27 08:42:23 +03:00
|
|
|
|
"time"
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
|
"golang.org/x/crypto/salsa20"
|
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
|
"github.com/boltdb/bolt"
|
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"
|
2017-06-17 01:15:26 +03:00
|
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
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-07-31 01:53:53 +03:00
|
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2016-06-21 21:31:10 +03:00
|
|
|
|
"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
|
2017-07-31 01:53:53 +03:00
|
|
|
|
|
|
|
|
|
defaultCsvDelay = 4
|
2017-08-06 02:52:37 +03:00
|
|
|
|
|
|
|
|
|
// maxFundingAmount is a soft-limit of the maximum channel size
|
|
|
|
|
// accepted within the Lightning Protocol Currently. This limit is
|
|
|
|
|
// currently defined in BOLT-0002, and serves as an initial
|
|
|
|
|
// precaturioary limit while implementations are battle tested in the
|
|
|
|
|
// real world.
|
|
|
|
|
//
|
|
|
|
|
// TODO(roasbeef): add command line param to modify
|
2017-08-08 02:30:29 +03:00
|
|
|
|
maxFundingAmount = btcutil.Amount(1 << 24)
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
|
|
// maxWaitNumBlocksFundingConf is the maximum number of blocks to wait
|
|
|
|
|
// for the funding transaction to be confirmed before forgetting about
|
|
|
|
|
// the channel. 288 blocks is ~48 hrs
|
|
|
|
|
maxWaitNumBlocksFundingConf = 288
|
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
|
2017-01-13 06:40:38 +03:00
|
|
|
|
peerAddress *lnwire.NetAddress
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
chanAmt btcutil.Amount
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
|
|
|
|
msg *lnwire.OpenChannel
|
2017-01-13 06:40:38 +03:00
|
|
|
|
peerAddress *lnwire.NetAddress
|
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 {
|
|
|
|
|
msg *lnwire.AcceptChannel
|
2017-01-13 06:40:38 +03:00
|
|
|
|
peerAddress *lnwire.NetAddress
|
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 {
|
|
|
|
|
msg *lnwire.FundingCreated
|
2017-01-13 06:40:38 +03:00
|
|
|
|
peerAddress *lnwire.NetAddress
|
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 {
|
|
|
|
|
msg *lnwire.FundingSigned
|
2017-01-13 06:40:38 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2017-04-17 01:34:23 +03:00
|
|
|
|
err *lnwire.Error
|
2017-01-13 06:40:38 +03:00
|
|
|
|
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
|
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
|
|
|
|
|
|
2017-05-01 10:27:12 +03:00
|
|
|
|
// FeeEstimator calculates appropriate fee rates based on historical
|
|
|
|
|
// transaction information.
|
|
|
|
|
FeeEstimator lnwallet.FeeEstimator
|
|
|
|
|
|
2017-01-13 06:40:38 +03:00
|
|
|
|
// ArbiterChan allows the FundingManager to notify the BreachArbiter
|
|
|
|
|
// that a new channel has been created that should be observed to
|
2017-07-31 01:53:53 +03:00
|
|
|
|
// ensure that the channel counterparty hasn't broadcast an invalid
|
2017-01-13 06:40:38 +03:00
|
|
|
|
// 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-04-14 21:17:41 +03:00
|
|
|
|
// SignMessage signs an arbitrary method with a given public key. The
|
|
|
|
|
// 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
|
|
|
|
|
// announcement from the backing Lighting Network node.
|
2017-08-08 15:48:12 +03:00
|
|
|
|
CurrentNodeAnnouncement func() (lnwire.NodeAnnouncement, error)
|
2017-07-14 22:47:52 +03:00
|
|
|
|
|
2017-04-01 15:33:17 +03:00
|
|
|
|
// SendAnnouncement is used by the FundingManager to announce newly
|
|
|
|
|
// created channels to the rest of the Lightning Network.
|
|
|
|
|
SendAnnouncement func(msg lnwire.Message) error
|
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
|
2017-04-17 01:34:23 +03:00
|
|
|
|
// channel ID.
|
|
|
|
|
FindChannel func(chanID lnwire.ChannelID) (*lnwallet.LightningChannel, error)
|
|
|
|
|
|
|
|
|
|
// 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
|
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
|
|
|
|
|
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
|
|
|
|
|
// goroutine stafe.
|
|
|
|
|
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{}
|
|
|
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
// ErrChannelNotFound is returned when we are looking for a specific
|
|
|
|
|
// channel opening state in the FundingManager's internal database, but
|
|
|
|
|
// the channel in question is not considered being in an opening state.
|
|
|
|
|
ErrChannelNotFound = fmt.Errorf("channel not found in db")
|
|
|
|
|
)
|
|
|
|
|
|
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-06-17 00:19:39 +03:00
|
|
|
|
cfg: &cfg,
|
|
|
|
|
chanIDKey: cfg.TempChanIDSeed,
|
|
|
|
|
activeReservations: make(map[serializedPubKey]pendingChannels),
|
2017-07-31 01:53:53 +03:00
|
|
|
|
signedReservations: make(map[lnwire.ChannelID][32]byte),
|
2017-06-17 00:19:39 +03:00
|
|
|
|
newChanBarriers: make(map[lnwire.ChannelID]chan struct{}),
|
|
|
|
|
fundingMsgs: make(chan interface{}, msgBufferSize),
|
|
|
|
|
fundingRequests: make(chan *initFundingMsg, msgBufferSize),
|
|
|
|
|
localDiscoverySignals: make(map[lnwire.ChannelID]chan 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 {
|
|
|
|
|
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.
|
2017-05-11 03:23:47 +03:00
|
|
|
|
// TODO(roasbeef): store height that funding finished?
|
|
|
|
|
// * would then replace call below
|
2017-07-31 01:53:53 +03:00
|
|
|
|
pendingChannels, err := f.cfg.Wallet.Cfg.Database.FetchPendingChannels()
|
2017-01-24 05:19:54 +03:00
|
|
|
|
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 "+
|
2017-07-31 01:53:53 +03:00
|
|
|
|
"barrier", channel.FundingOutpoint)
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&channel.FundingOutpoint)
|
2017-04-17 01:34:23 +03:00
|
|
|
|
f.newChanBarriers[chanID] = make(chan struct{})
|
2017-01-24 05:19:54 +03:00
|
|
|
|
f.barrierMtx.Unlock()
|
|
|
|
|
|
2017-06-17 00:19:39 +03:00
|
|
|
|
f.localDiscoverySignals[chanID] = make(chan struct{})
|
|
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
|
doneChan := make(chan struct{})
|
2017-06-08 20:48:07 +03:00
|
|
|
|
timeoutChan := make(chan struct{})
|
|
|
|
|
|
|
|
|
|
go func(ch *channeldb.OpenChannel) {
|
|
|
|
|
go f.waitForFundingWithTimeout(ch, doneChan, timeoutChan)
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-timeoutChan:
|
|
|
|
|
// Timeout waiting for the funding transaction
|
|
|
|
|
// to confirm, so we forget the channel and
|
|
|
|
|
// delete it from the database.
|
|
|
|
|
closeInfo := &channeldb.ChannelCloseSummary{
|
|
|
|
|
ChanPoint: ch.FundingOutpoint,
|
|
|
|
|
RemotePub: ch.IdentityPub,
|
|
|
|
|
CloseType: channeldb.FundingCanceled,
|
|
|
|
|
}
|
|
|
|
|
if err := ch.CloseChannel(closeInfo); err != nil {
|
|
|
|
|
fndgLog.Errorf("Failed closing channel "+
|
|
|
|
|
"%v: %v", ch.FundingOutpoint, err)
|
|
|
|
|
}
|
|
|
|
|
case <-f.quit:
|
|
|
|
|
// The fundingManager is shutting down, and will
|
|
|
|
|
// resume wait on startup.
|
|
|
|
|
case <-doneChan:
|
|
|
|
|
// Success, funding transaction was confirmed.
|
|
|
|
|
}
|
|
|
|
|
}(channel)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch all our open channels, and make sure they all finalized the
|
|
|
|
|
// opening process.
|
|
|
|
|
// TODO(halseth): this check is only done on restart atm, but should
|
|
|
|
|
// also be done if a peer that disappeared during the opening process
|
|
|
|
|
// reconnects.
|
|
|
|
|
openChannels, err := f.cfg.Wallet.Cfg.Database.FetchAllChannels()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, channel := range openChannels {
|
|
|
|
|
channelState, shortChanID, err := f.getChannelOpeningState(
|
|
|
|
|
&channel.FundingOutpoint)
|
|
|
|
|
if err == ErrChannelNotFound {
|
|
|
|
|
// Channel not in fundingManager's opening database,
|
|
|
|
|
// meaning it was successully announced to the network.
|
|
|
|
|
continue
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fndgLog.Debugf("channel with opening state %v found",
|
|
|
|
|
channelState)
|
|
|
|
|
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&channel.FundingOutpoint)
|
|
|
|
|
f.localDiscoverySignals[chanID] = make(chan struct{})
|
|
|
|
|
|
|
|
|
|
// If we did find the channel in the opening state database, we
|
|
|
|
|
// have seen the funding transaction being confirmed, but we
|
|
|
|
|
// did not finish the rest of the setup procedure before we shut
|
|
|
|
|
// down. We handle the remaining steps of this setup by
|
|
|
|
|
// continuing the procedure where we left off.
|
|
|
|
|
switch channelState {
|
|
|
|
|
case markedOpen:
|
|
|
|
|
// The funding transaction was confirmed, but we did not
|
|
|
|
|
// successfully send the fundingLocked message to the
|
|
|
|
|
// peer, so let's do that now.
|
|
|
|
|
f.wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
f.sendFundingLockedAndAnnounceChannel(channel,
|
|
|
|
|
shortChanID)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
case fundingLockedSent:
|
|
|
|
|
// fundingLocked was sent to peer, but the channel
|
|
|
|
|
// announcement was not sent.
|
|
|
|
|
f.wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
|
|
|
|
|
lnChannel, err := lnwallet.NewLightningChannel(
|
|
|
|
|
nil, nil, f.cfg.FeeEstimator, channel)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("error creating "+
|
|
|
|
|
"lightning channel: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer lnChannel.Stop()
|
|
|
|
|
|
|
|
|
|
f.sendChannelAnnouncement(channel, lnChannel,
|
|
|
|
|
shortChanID)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
fndgLog.Errorf("undefined channelState: %v",
|
|
|
|
|
channelState)
|
|
|
|
|
}
|
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 {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
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 {
|
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-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
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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.resp <- 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-08-22 09:25:41 +03:00
|
|
|
|
localBalance: dbPendingChan.LocalBalance.ToSatoshis(),
|
|
|
|
|
remoteBalance: dbPendingChan.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-01-23 10:31:01 +03:00
|
|
|
|
msg.err <- nil
|
2016-07-08 01:33:52 +03:00
|
|
|
|
}
|
|
|
|
|
|
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,
|
2017-01-13 06:40:38 +03:00
|
|
|
|
peerAddress *lnwire.NetAddress) {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
|
|
|
|
|
f.fundingMsgs <- &fundingOpenMsg{msg, peerAddress}
|
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.
|
2017-01-13 06:40:38 +03:00
|
|
|
|
peerIDKey := newSerializedKey(fmsg.peerAddress.IdentityKey)
|
|
|
|
|
|
2017-08-06 02:52:37 +03:00
|
|
|
|
msg := fmsg.msg
|
|
|
|
|
amt := msg.FundingAmount
|
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
// TODO(roasbeef): modify to only accept a _single_ pending channel per
|
|
|
|
|
// block unless white listed
|
2017-01-13 06:40:38 +03:00
|
|
|
|
if len(f.activeReservations[peerIDKey]) >= cfg.MaxPendingChannels {
|
2017-04-17 01:34:23 +03:00
|
|
|
|
errMsg := &lnwire.Error{
|
|
|
|
|
ChanID: fmsg.msg.PendingChannelID,
|
2017-08-22 09:30:01 +03:00
|
|
|
|
Data: lnwire.ErrorData{byte(lnwire.ErrMaxPendingChannels)},
|
2017-01-25 04:12:51 +03:00
|
|
|
|
}
|
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 {
|
2017-04-17 01:34:23 +03:00
|
|
|
|
errMsg := &lnwire.Error{
|
|
|
|
|
ChanID: fmsg.msg.PendingChannelID,
|
2017-08-22 09:30:01 +03:00
|
|
|
|
Data: lnwire.ErrorData{byte(lnwire.ErrSynchronizingChain)},
|
2016-10-15 16:24:56 +03:00
|
|
|
|
}
|
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
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
if msg.FundingAmount > maxFundingAmount {
|
|
|
|
|
errMsg := &lnwire.Error{
|
|
|
|
|
ChanID: fmsg.msg.PendingChannelID,
|
2017-08-22 09:30:01 +03:00
|
|
|
|
Data: lnwire.ErrorData{byte(lnwire.ErrChanTooLarge)},
|
2017-08-06 02:52:37 +03:00
|
|
|
|
}
|
|
|
|
|
err := f.cfg.SendToPeer(fmsg.peerAddress.IdentityKey, errMsg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("unable to send error message to peer %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-07-31 01:53:53 +03:00
|
|
|
|
|
2017-08-06 02:52:37 +03:00
|
|
|
|
// TODO(roasbeef): validate sanity of all params sent
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
2016-07-22 02:21:27 +03:00
|
|
|
|
// TODO(roasbeef): error if funding flow already ongoing
|
2017-05-11 03:23:47 +03:00
|
|
|
|
fndgLog.Infof("Recv'd fundingRequest(amt=%v, push=%v, delay=%v, pendingId=%x) "+
|
2017-07-31 01:53:53 +03:00
|
|
|
|
"from peer(%x)", amt, msg.PushAmount, msg.CsvDelay, msg.PendingChannelID,
|
2017-04-17 01:34:23 +03:00
|
|
|
|
fmsg.peerAddress.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.
|
2017-07-31 01:53:53 +03:00
|
|
|
|
//
|
2016-12-15 05:11:31 +03:00
|
|
|
|
// TODO(roasbeef): assuming this was an inbound connection, replace
|
|
|
|
|
// port with default advertised port
|
2017-07-31 01:53:53 +03:00
|
|
|
|
chainHash := chainhash.Hash(msg.ChainHash)
|
2017-01-13 06:40:38 +03:00
|
|
|
|
reservation, err := f.cfg.Wallet.InitChannelReservation(amt, 0,
|
2017-07-31 01:53:53 +03:00
|
|
|
|
msg.PushAmount, btcutil.Amount(msg.FeePerKiloWeight),
|
2017-01-24 05:19:54 +03:00
|
|
|
|
fmsg.peerAddress.IdentityKey, fmsg.peerAddress.Address,
|
2017-07-31 01:53:53 +03:00
|
|
|
|
&chainHash)
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 01:53:53 +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 use out mapping to derive the proper number of
|
|
|
|
|
// confirmations based on the amount of the channel, and also if any
|
|
|
|
|
// funds are being pushed to us.
|
|
|
|
|
numConfsReq := f.cfg.NumRequiredConfs(msg.FundingAmount, msg.PushAmount)
|
|
|
|
|
reservation.SetNumConfsRequired(numConfsReq)
|
|
|
|
|
|
|
|
|
|
// We'll also validate and apply all the constraints the initiating
|
|
|
|
|
// party is attempting to dictate for our commitment transaction.
|
|
|
|
|
reservation.RequireLocalDelay(msg.CsvDelay)
|
|
|
|
|
|
|
|
|
|
fndgLog.Infof("Requiring %v confirmations for pendingChan(%x): "+
|
|
|
|
|
"amt=%v, push_amt=%v", numConfsReq, fmsg.msg.PendingChannelID,
|
|
|
|
|
amt, msg.PushAmount)
|
2016-12-06 17:05:46 +03:00
|
|
|
|
|
2017-02-25 03:21:52 +03:00
|
|
|
|
// Once the reservation has been created successfully, we add it to
|
|
|
|
|
// this peers map of pending reservations to track this particular
|
|
|
|
|
// 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
|
|
|
|
}
|
2017-04-17 01:34:23 +03:00
|
|
|
|
f.activeReservations[peerIDKey][msg.PendingChannelID] = &reservationWithCtx{
|
2016-06-21 21:31:10 +03:00
|
|
|
|
reservation: reservation,
|
2017-07-31 01:53:53 +03:00
|
|
|
|
chanAmt: amt,
|
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() {
|
2017-04-17 01:34:23 +03:00
|
|
|
|
_, err := f.cancelReservationCtx(fmsg.peerAddress.IdentityKey,
|
|
|
|
|
msg.PendingChannelID)
|
2017-02-22 09:14:22 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("unable to cancel reservation: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
// Using the RequiredRemoteDelay closure, we'll compute the remote CSV
|
|
|
|
|
// delay we require given the total amount of funds within the channel.
|
|
|
|
|
remoteCsvDelay := f.cfg.RequiredRemoteDelay(amt)
|
|
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
MaxPendingAmount: msg.MaxValueInFlight,
|
|
|
|
|
ChanReserve: msg.ChannelReserve,
|
2017-08-22 09:25:41 +03:00
|
|
|
|
MinHTLC: msg.HtlcMinimum,
|
2017-07-31 01:53:53 +03:00
|
|
|
|
MaxAcceptedHtlcs: msg.MaxAcceptedHTLCs,
|
|
|
|
|
},
|
|
|
|
|
CsvDelay: remoteCsvDelay,
|
|
|
|
|
MultiSigKey: copyPubKey(msg.FundingKey),
|
|
|
|
|
RevocationBasePoint: copyPubKey(msg.RevocationPoint),
|
|
|
|
|
PaymentBasePoint: copyPubKey(msg.PaymentPoint),
|
|
|
|
|
DelayBasePoint: copyPubKey(msg.DelayedPaymentPoint),
|
|
|
|
|
},
|
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)
|
2017-02-22 09:14:22 +03:00
|
|
|
|
cancelReservation()
|
2016-06-21 21:31:10 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
|
fndgLog.Infof("Sending fundingResp for pendingID(%x)",
|
|
|
|
|
msg.PendingChannelID)
|
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{
|
|
|
|
|
PendingChannelID: msg.PendingChannelID,
|
|
|
|
|
DustLimit: ourContribution.DustLimit,
|
|
|
|
|
MaxValueInFlight: ourContribution.MaxPendingAmount,
|
|
|
|
|
ChannelReserve: ourContribution.ChanReserve,
|
|
|
|
|
MinAcceptDepth: uint32(numConfsReq),
|
2017-08-22 09:25:41 +03:00
|
|
|
|
HtlcMinimum: ourContribution.MinHTLC,
|
2017-07-31 01:53:53 +03:00
|
|
|
|
CsvDelay: uint16(remoteCsvDelay),
|
|
|
|
|
FundingKey: ourContribution.MultiSigKey,
|
|
|
|
|
RevocationPoint: ourContribution.RevocationBasePoint,
|
|
|
|
|
PaymentPoint: ourContribution.PaymentBasePoint,
|
|
|
|
|
DelayedPaymentPoint: ourContribution.DelayBasePoint,
|
|
|
|
|
FirstCommitmentPoint: ourContribution.FirstCommitmentPoint,
|
|
|
|
|
}
|
|
|
|
|
err = f.cfg.SendToPeer(fmsg.peerAddress.IdentityKey, &fundingAccept)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
if err != nil {
|
2017-01-13 06:40:38 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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,
|
2017-01-13 06:40:38 +03:00
|
|
|
|
peerAddress *lnwire.NetAddress) {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
|
|
|
|
|
f.fundingMsgs <- &fundingAcceptMsg{msg, peerAddress}
|
2016-06-21 21:31:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
// handleFundingAceept processes a response to the workflow initiation sent by
|
|
|
|
|
// 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
|
2017-01-13 06:40:38 +03:00
|
|
|
|
peerKey := fmsg.peerAddress.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 {
|
2017-01-13 06:40:38 +03:00
|
|
|
|
fndgLog.Warnf("Can't find reservation (peerKey:%v, chanID:%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
|
|
|
|
|
2017-02-22 09:14:22 +03:00
|
|
|
|
cancelReservation := func() {
|
2017-04-17 01:34:23 +03:00
|
|
|
|
_, err := f.cancelReservationCtx(peerKey, pendingChanID)
|
|
|
|
|
if err != nil {
|
2017-02-22 09:14:22 +03:00
|
|
|
|
fndgLog.Errorf("unable to cancel reservation: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
// TODO(roasbeeef): perform sanity checks on all the params and
|
|
|
|
|
// constraints
|
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
|
fndgLog.Infof("Recv'd fundingResponse for pendingID(%x)", pendingChanID)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
// We'll also specify the responder's preference for the number of
|
|
|
|
|
// required confirmations, and also the CSV delay that they specify for
|
|
|
|
|
// us within the reservation itself.
|
|
|
|
|
resCtx.reservation.SetNumConfsRequired(uint16(msg.MinAcceptDepth))
|
|
|
|
|
resCtx.reservation.RequireLocalDelay(uint16(msg.CsvDelay))
|
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,
|
|
|
|
|
MaxPendingAmount: msg.MaxValueInFlight,
|
|
|
|
|
ChanReserve: msg.ChannelReserve,
|
2017-08-22 09:25:41 +03:00
|
|
|
|
MinHTLC: msg.HtlcMinimum,
|
2017-07-31 01:53:53 +03:00
|
|
|
|
MaxAcceptedHtlcs: msg.MaxAcceptedHTLCs,
|
|
|
|
|
},
|
|
|
|
|
MultiSigKey: copyPubKey(msg.FundingKey),
|
|
|
|
|
RevocationBasePoint: copyPubKey(msg.RevocationPoint),
|
|
|
|
|
PaymentBasePoint: copyPubKey(msg.PaymentPoint),
|
|
|
|
|
DelayBasePoint: copyPubKey(msg.DelayedPaymentPoint),
|
|
|
|
|
},
|
2016-06-21 21:31:10 +03:00
|
|
|
|
}
|
2017-07-31 01:53:53 +03:00
|
|
|
|
remoteContribution.CsvDelay = f.cfg.RequiredRemoteDelay(resCtx.chanAmt)
|
|
|
|
|
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",
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
fndgLog.Infof("pendingChan(%x): remote party proposes num_confs=%v, "+
|
|
|
|
|
"csv_delay=%v", pendingChanID, msg.MinAcceptDepth, msg.CsvDelay)
|
|
|
|
|
|
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()
|
|
|
|
|
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()
|
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()
|
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
|
fndgLog.Infof("Generated ChannelPoint(%v) for pendingID(%x)", outPoint,
|
|
|
|
|
pendingChanID)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
fundingCreated := &lnwire.FundingCreated{
|
|
|
|
|
PendingChannelID: pendingChanID,
|
|
|
|
|
FundingPoint: *outPoint,
|
|
|
|
|
CommitSig: commitSig,
|
|
|
|
|
}
|
|
|
|
|
err = f.cfg.SendToPeer(fmsg.peerAddress.IdentityKey, fundingCreated)
|
2017-04-17 01:34:23 +03:00
|
|
|
|
if err != nil {
|
2017-01-13 06:40:38 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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,
|
2017-01-13 06:40:38 +03:00
|
|
|
|
peerAddress *lnwire.NetAddress) {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
|
|
|
|
|
f.fundingMsgs <- &fundingCreatedMsg{msg, peerAddress}
|
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) {
|
2017-01-13 06:40:38 +03:00
|
|
|
|
peerKey := fmsg.peerAddress.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 {
|
2017-01-13 08:01:50 +03:00
|
|
|
|
fndgLog.Warnf("can't find reservation (peerID:%v, chanID:%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
|
|
|
|
|
2017-02-22 09:14:22 +03:00
|
|
|
|
cancelReservation := func() {
|
2017-04-17 01:34:23 +03:00
|
|
|
|
_, err := f.cancelReservationCtx(peerKey, pendingChanID)
|
|
|
|
|
if err != nil {
|
2017-02-22 09:14:22 +03:00
|
|
|
|
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
|
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
|
2017-04-17 01:34:23 +03:00
|
|
|
|
fndgLog.Infof("completing pendingID(%x) with ChannelPoint(%v)",
|
|
|
|
|
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.
|
2017-07-31 01:53:53 +03:00
|
|
|
|
commitSig := fmsg.msg.CommitSig.Serialize()
|
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)
|
2017-02-22 09:14:22 +03:00
|
|
|
|
cancelReservation()
|
2016-06-21 21:31:10 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
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() {
|
|
|
|
|
closeInfo := &channeldb.ChannelCloseSummary{
|
|
|
|
|
ChanPoint: completeChan.FundingOutpoint,
|
|
|
|
|
RemotePub: completeChan.IdentityPub,
|
|
|
|
|
CloseType: channeldb.FundingCanceled,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
|
fndgLog.Infof("sending signComplete for pendingID(%x) over 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.
|
|
|
|
|
//
|
|
|
|
|
// 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)
|
|
|
|
|
cancelReservation()
|
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,
|
|
|
|
|
}
|
|
|
|
|
if err := f.cfg.SendToPeer(peerKey, fundingSigned); err != nil {
|
|
|
|
|
fndgLog.Errorf("unable to send FundingSigned message: %v", err)
|
2017-02-22 09:14:22 +03:00
|
|
|
|
cancelReservation()
|
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
|
|
|
|
|
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()
|
|
|
|
|
|
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-01-24 05:19:54 +03:00
|
|
|
|
go func() {
|
|
|
|
|
doneChan := make(chan struct{})
|
2017-06-08 20:48:07 +03:00
|
|
|
|
timeoutChan := make(chan struct{})
|
|
|
|
|
go f.waitForFundingWithTimeout(completeChan, doneChan,
|
|
|
|
|
timeoutChan)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
|
select {
|
|
|
|
|
case <-timeoutChan:
|
|
|
|
|
// We did not see the funding confirmation before
|
|
|
|
|
// timeout, so we forget the channel.
|
|
|
|
|
cancelReservation()
|
|
|
|
|
deleteFromDatabase()
|
|
|
|
|
case <-f.quit:
|
|
|
|
|
// The fundingManager is shutting down, will resume
|
|
|
|
|
// wait for funding transaction on startup.
|
|
|
|
|
case <-doneChan:
|
|
|
|
|
// Success, funding transaction was confirmed.
|
|
|
|
|
f.deleteReservationCtx(peerKey,
|
|
|
|
|
fmsg.msg.PendingChannelID)
|
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
|
}()
|
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,
|
2017-01-13 06:40:38 +03:00
|
|
|
|
peerAddress *lnwire.NetAddress) {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
|
|
|
|
|
f.fundingMsgs <- &fundingSignedMsg{msg, peerAddress}
|
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) {
|
|
|
|
|
// As the funding signed message will reference the reservation by it's
|
|
|
|
|
// 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 {
|
|
|
|
|
fndgLog.Warnf("Unable to find signed reservation for chan_id=%x",
|
|
|
|
|
fmsg.msg.ChanID)
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
peerKey := fmsg.peerAddress.IdentityKey
|
|
|
|
|
resCtx, err := f.getReservationCtx(fmsg.peerAddress.IdentityKey,
|
|
|
|
|
pendingChanID)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
if err != nil {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
fndgLog.Warnf("Unable to find reservation (peerID:%v, chanID:%v)",
|
|
|
|
|
peerKey, pendingChanID)
|
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.
|
2017-07-31 01:53:53 +03:00
|
|
|
|
commitSig := fmsg.msg.CommitSig.Serialize()
|
2017-01-24 05:19:54 +03:00
|
|
|
|
completeChan, err := resCtx.reservation.CompleteReservation(nil, commitSig)
|
|
|
|
|
if err != nil {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
fndgLog.Errorf("Unable to complete reservation sign complete: %v", err)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
resCtx.err <- err
|
2017-02-25 03:21:52 +03:00
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
_, err := f.cancelReservationCtx(peerKey, pendingChanID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("Unable to cancel reservation: %v", err)
|
2017-02-25 03:21:52 +03:00
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
|
fndgLog.Infof("Finalizing pendingID(%x) over ChannelPoint(%v), "+
|
2017-07-31 01:53:53 +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.
|
|
|
|
|
resCtx.updates <- &lnrpc.OpenStatusUpdate{
|
|
|
|
|
Update: &lnrpc.OpenStatusUpdate_ChanPending{
|
|
|
|
|
ChanPending: &lnrpc.PendingUpdate{
|
|
|
|
|
Txid: fundingPoint.Hash[:],
|
|
|
|
|
OutputIndex: fundingPoint.Index,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
doneChan := make(chan struct{})
|
2017-06-08 20:48:07 +03:00
|
|
|
|
cancelChan := make(chan struct{})
|
|
|
|
|
|
|
|
|
|
// In case the fundingManager is stopped at some point during
|
|
|
|
|
// the remaining part of the opening process, we must wait for
|
|
|
|
|
// this process to finish (either successully or with some
|
|
|
|
|
// error), before the fundingManager can be shut down.
|
|
|
|
|
f.wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
f.waitForFundingConfirmation(completeChan, cancelChan,
|
|
|
|
|
doneChan)
|
|
|
|
|
}()
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
2017-02-25 03:24:10 +03:00
|
|
|
|
select {
|
|
|
|
|
case <-f.quit:
|
|
|
|
|
return
|
|
|
|
|
case <-doneChan:
|
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
|
|
|
|
// Finally give the caller a final update notifying them that
|
|
|
|
|
// the channel is now open.
|
2017-04-17 01:34:23 +03:00
|
|
|
|
// TODO(roasbeef): only notify after recv of funding locked?
|
2017-01-24 05:19:54 +03:00
|
|
|
|
resCtx.updates <- &lnrpc.OpenStatusUpdate{
|
|
|
|
|
Update: &lnrpc.OpenStatusUpdate_ChanOpen{
|
|
|
|
|
ChanOpen: &lnrpc.ChannelOpenUpdate{
|
|
|
|
|
ChannelPoint: &lnrpc.ChannelPoint{
|
|
|
|
|
FundingTxid: fundingPoint.Hash[:],
|
|
|
|
|
OutputIndex: fundingPoint.Index,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
f.deleteReservationCtx(peerKey, pendingChanID)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
|
// waitForFundingWithTimeout is a wrapper around waitForFundingConfirmation that
|
|
|
|
|
// will cancel the wait for confirmation if maxWaitNumBlocksFundingConf has
|
|
|
|
|
// passed from bestHeight. In the case of timeout, the timeoutChan will be
|
|
|
|
|
// closed. In case of confirmation or error, doneChan will be closed.
|
|
|
|
|
func (f *fundingManager) waitForFundingWithTimeout(completeChan *channeldb.OpenChannel,
|
|
|
|
|
doneChan chan<- struct{}, timeoutChan chan<- struct{}) {
|
|
|
|
|
|
|
|
|
|
epochClient, err := f.cfg.Notifier.RegisterBlockEpochNtfn()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("unable to register for epoch notification: %v",
|
|
|
|
|
err)
|
|
|
|
|
close(doneChan)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-31 06:26:10 +03:00
|
|
|
|
defer epochClient.Cancel()
|
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
|
waitingDoneChan := make(chan struct{})
|
|
|
|
|
cancelChan := make(chan struct{})
|
|
|
|
|
|
|
|
|
|
// Add this goroutine to wait group so we can be sure that it is
|
|
|
|
|
// properly stopped before the funding manager can be shut down.
|
|
|
|
|
f.wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
f.waitForFundingConfirmation(completeChan, cancelChan,
|
|
|
|
|
waitingDoneChan)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// On block maxHeight we will cancel the funding confirmation wait.
|
|
|
|
|
maxHeight := completeChan.FundingBroadcastHeight + maxWaitNumBlocksFundingConf
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case epoch, ok := <-epochClient.Epochs:
|
|
|
|
|
if !ok {
|
|
|
|
|
fndgLog.Warnf("Epoch client shutting down")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if uint32(epoch.Height) >= maxHeight {
|
|
|
|
|
fndgLog.Warnf("waited for %v blocks without "+
|
|
|
|
|
"seeing funding transaction confirmed,"+
|
|
|
|
|
" cancelling.", maxWaitNumBlocksFundingConf)
|
|
|
|
|
|
|
|
|
|
// Cancel the waitForFundingConfirmation
|
|
|
|
|
// goroutine.
|
|
|
|
|
close(cancelChan)
|
|
|
|
|
|
|
|
|
|
// Notify the caller of the timeout.
|
|
|
|
|
close(timeoutChan)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
case <-f.quit:
|
|
|
|
|
// The fundingManager is shutting down, will resume
|
|
|
|
|
// waiting for the funding transaction on startup.
|
|
|
|
|
return
|
|
|
|
|
case <-waitingDoneChan:
|
|
|
|
|
close(doneChan)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-06-08 20:48:07 +03:00
|
|
|
|
// The wait can be canceled by closing the cancelChan.
|
2017-04-17 01:34:23 +03:00
|
|
|
|
func (f *fundingManager) waitForFundingConfirmation(completeChan *channeldb.OpenChannel,
|
2017-06-08 20:48:07 +03:00
|
|
|
|
cancelChan <-chan struct{}, doneChan chan<- struct{}) {
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
2017-02-25 03:24:10 +03:00
|
|
|
|
defer close(doneChan)
|
|
|
|
|
|
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
|
|
|
|
|
numConfs := uint32(completeChan.NumConfsRequired)
|
2017-05-11 03:23:47 +03:00
|
|
|
|
confNtfn, err := f.cfg.Notifier.RegisterConfirmationsNtfn(&txid,
|
2017-06-06 01:04:26 +03:00
|
|
|
|
numConfs, completeChan.FundingBroadcastHeight)
|
2017-04-17 01:34:23 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("Unable to register for confirmation of "+
|
|
|
|
|
"ChannelPoint(%v)", completeChan.FundingOutpoint)
|
|
|
|
|
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
|
|
|
|
|
case <-cancelChan:
|
|
|
|
|
fndgLog.Warnf("canceled waiting for funding confirmation, "+
|
|
|
|
|
"stopping funding flow for ChannelPoint(%v)",
|
|
|
|
|
completeChan.FundingOutpoint)
|
|
|
|
|
return
|
|
|
|
|
case <-f.quit:
|
|
|
|
|
fndgLog.Warnf("fundingManager shutting down, stopping funding "+
|
|
|
|
|
"flow for ChannelPoint(%v)", completeChan.FundingOutpoint)
|
|
|
|
|
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
|
2017-04-17 01:34:23 +03:00
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&fundingPoint)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
|
fndgLog.Infof("ChannelPoint(%v) is now active: ChannelID(%x)",
|
2017-05-25 03:37:28 +03:00
|
|
|
|
fundingPoint, chanID[:])
|
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),
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
|
// Now that the channel has been fully confirmed, we'll mark it as open
|
|
|
|
|
// within the database.
|
2017-01-24 05:19:54 +03:00
|
|
|
|
completeChan.IsPending = false
|
2017-07-31 01:53:53 +03:00
|
|
|
|
err = f.cfg.Wallet.Cfg.Database.MarkChannelAsOpen(&fundingPoint, shortChanID)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("error setting channel pending flag to false: "+
|
|
|
|
|
"%v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
// TODO(roasbeef): ideally persistent state update for chan above
|
|
|
|
|
// should be abstracted
|
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
|
// The funding transaction now being confirmed, we add this channel to
|
|
|
|
|
// the fundingManager's internal persistant state machine that we use
|
|
|
|
|
// to track the remaining process of the channel opening. This is useful
|
|
|
|
|
// to resume the opening process in case of restarts.
|
|
|
|
|
//
|
|
|
|
|
// TODO(halseth): make the two db transactions (MarkChannelAsOpen and
|
|
|
|
|
// saveChannelOpeningState) atomic by doing them in the same transaction.
|
|
|
|
|
// Needed to be properly fault-tolerant.
|
|
|
|
|
err = f.saveChannelOpeningState(&completeChan.FundingOutpoint, markedOpen,
|
|
|
|
|
&shortChanID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("error setting channel state to markedOpen: %v",
|
|
|
|
|
err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now that the funding transaction has the required number of
|
|
|
|
|
// confirmations, we send the fundingLocked message to the peer.
|
|
|
|
|
f.sendFundingLockedAndAnnounceChannel(completeChan, &shortChanID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sendFundingLockedAndAnnounceChannel creates and sends the fundingLocked
|
|
|
|
|
// message, and then the channel announcement. This should be called after the
|
|
|
|
|
// funding transaction has been confirmed, and the channelState is 'markedOpen'.
|
|
|
|
|
func (f *fundingManager) sendFundingLockedAndAnnounceChannel(
|
|
|
|
|
completeChan *channeldb.OpenChannel, shortChanID *lnwire.ShortChannelID) {
|
|
|
|
|
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&completeChan.FundingOutpoint)
|
|
|
|
|
|
2017-04-17 01:34:23 +03:00
|
|
|
|
// With the channel marked open, we'll create the state-machine object
|
|
|
|
|
// which wraps the database state.
|
2017-05-01 10:27:12 +03:00
|
|
|
|
channel, err := lnwallet.NewLightningChannel(nil, nil,
|
|
|
|
|
f.cfg.FeeEstimator, completeChan)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("error creating new lightning channel: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-08-01 07:06:55 +03:00
|
|
|
|
defer channel.Stop()
|
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-04-17 01:39:14 +03:00
|
|
|
|
fndgLog.Errorf("unable to create next revocation: %v", err)
|
2017-02-25 03:24:10 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
2017-04-17 01:39:14 +03:00
|
|
|
|
fundingLockedMsg := lnwire.NewFundingLocked(chanID, nextRevocation)
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
|
err = f.cfg.SendToPeer(completeChan.IdentityPub, fundingLockedMsg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("unable to send fundingLocked to peer: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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(&completeChan.FundingOutpoint, fundingLockedSent,
|
|
|
|
|
shortChanID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("error setting channel state to "+
|
|
|
|
|
"fundingLockedSent: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-22 09:25:41 +03:00
|
|
|
|
// TODO(roasbeef): wait 6 blocks before announcing
|
|
|
|
|
|
2017-06-08 20:48:07 +03:00
|
|
|
|
f.sendChannelAnnouncement(completeChan, channel, shortChanID)
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-22 09:25:41 +03:00
|
|
|
|
// sendChannelAnnouncement broadcast the necessary channel announcement
|
|
|
|
|
// messages to the network. Should be called after the fundingLocked message
|
|
|
|
|
// is sent (channelState is 'fundingLockedSent') and the channel is ready to
|
|
|
|
|
// be used.
|
2017-06-08 20:48:07 +03:00
|
|
|
|
func (f *fundingManager) sendChannelAnnouncement(completeChan *channeldb.OpenChannel,
|
|
|
|
|
channel *lnwallet.LightningChannel, shortChanID *lnwire.ShortChannelID) {
|
|
|
|
|
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(&completeChan.FundingOutpoint)
|
|
|
|
|
fundingPoint := completeChan.FundingOutpoint
|
|
|
|
|
|
|
|
|
|
fndgLog.Infof("Announcing ChannelPoint(%v), short_chan_id=%v",
|
|
|
|
|
&fundingPoint, spew.Sdump(shortChanID))
|
2017-01-24 05:19:54 +03:00
|
|
|
|
|
2017-04-17 01:39:14 +03:00
|
|
|
|
// Register the new link with the L3 routing manager so this new
|
|
|
|
|
// channel can be utilized during path finding.
|
2017-06-08 20:48:07 +03:00
|
|
|
|
err := f.announceChannel(f.cfg.IDKey, completeChan.IdentityPub,
|
2017-04-17 01:39:14 +03:00
|
|
|
|
channel.LocalFundingKey, channel.RemoteFundingKey,
|
2017-06-08 20:48:07 +03:00
|
|
|
|
*shortChanID, chanID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("channel announcement failed: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-22 09:25:41 +03:00
|
|
|
|
// After the channel is successfully announced from the
|
|
|
|
|
// fundingManager, we delete the channel from our internal database.
|
|
|
|
|
// We can do this
|
2017-06-08 20:48:07 +03:00
|
|
|
|
// because we assume the AuthenticatedGossiper queues the announcement
|
|
|
|
|
// messages, and persists them in case of a daemon shutdown.
|
|
|
|
|
err = f.deleteChannelOpeningState(&completeChan.FundingOutpoint)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("error deleting channel state: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-06-17 00:19:39 +03:00
|
|
|
|
|
|
|
|
|
// Finally, as the local channel discovery has been fully processed,
|
2017-07-15 03:14:11 +03:00
|
|
|
|
// we'll trigger the signal indicating that it's safe for any funding
|
2017-06-17 00:19:39 +03:00
|
|
|
|
// locked messages related to this channel to be processed.
|
|
|
|
|
f.localDiscoveryMtx.Lock()
|
2017-07-15 03:14:11 +03:00
|
|
|
|
if discoverySignal, ok := f.localDiscoverySignals[chanID]; ok {
|
|
|
|
|
close(discoverySignal)
|
|
|
|
|
}
|
2017-06-17 00:19:39 +03:00
|
|
|
|
f.localDiscoveryMtx.Unlock()
|
|
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
peerAddress *lnwire.NetAddress) {
|
2017-04-01 15:33:17 +03:00
|
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
|
f.fundingMsgs <- &fundingLockedMsg{msg, peerAddress}
|
|
|
|
|
}
|
|
|
|
|
|
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-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
|
|
|
|
|
// message, we'll wait for the lcoal waitForFundingConfirmation
|
|
|
|
|
// goroutine to signal that it has the necessary state in
|
|
|
|
|
// place. Otherwise, we may be missing critical information
|
|
|
|
|
// required to handle forwarded HTLC's.
|
|
|
|
|
<-localDiscoverySignal
|
|
|
|
|
|
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
|
|
|
|
|
2017-04-17 01:39:14 +03:00
|
|
|
|
// First, we'll attempt to locate the channel who's funding workflow is
|
|
|
|
|
// being finalized by this message. We got to the database rather than
|
|
|
|
|
// 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-08-22 09:25:41 +03:00
|
|
|
|
// TODO(roasbeef): done nothing if repeat message sent
|
|
|
|
|
|
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.
|
|
|
|
|
err = channel.InitNextRevocation(fmsg.msg.NextPerCommitmentPoint)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Errorf("unable to insert next commitment point: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 01:39:14 +03:00
|
|
|
|
// With the channel retrieved, we'll send the breach arbiter the new
|
|
|
|
|
// channel so it can watch for attempts to breach the channel's
|
2017-07-31 01:53:53 +03:00
|
|
|
|
// contract by the remote party.
|
2017-04-17 01:39:14 +03:00
|
|
|
|
f.cfg.ArbiterChan <- channel
|
|
|
|
|
|
|
|
|
|
// Launch a defer so we _ensure_ that the channel barrier is properly
|
|
|
|
|
// closed even if the target peer is not longer online at this point.
|
|
|
|
|
defer func() {
|
|
|
|
|
// 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 ChanID(%v)", chanID)
|
|
|
|
|
close(f.newChanBarriers[chanID])
|
|
|
|
|
delete(f.newChanBarriers, chanID)
|
|
|
|
|
f.barrierMtx.Unlock()
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Finally, we'll find the peer that sent us this message so we can
|
|
|
|
|
// provide it with the fully initialized channel state.
|
|
|
|
|
peer, err := f.cfg.FindPeer(fmsg.peerAddress.IdentityKey)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
// We pause here to wait for the peer to recognize the new channel
|
|
|
|
|
// before we close the channel barrier corresponding to the channel.
|
|
|
|
|
select {
|
|
|
|
|
case <-f.quit:
|
|
|
|
|
return
|
|
|
|
|
case <-newChanDone: // Fallthrough if we're not quitting.
|
|
|
|
|
}
|
2017-01-24 05:19:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
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 {
|
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.
|
2017-03-28 22:08:14 +03:00
|
|
|
|
func (f *fundingManager) newChanAnnouncement(localPubKey, remotePubKey *btcec.PublicKey,
|
2017-04-17 01:34:23 +03:00
|
|
|
|
localFundingKey, remoteFundingKey *btcec.PublicKey,
|
|
|
|
|
shortChanID lnwire.ShortChannelID,
|
|
|
|
|
chanID lnwire.ChannelID) (*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-08-22 09:33:19 +03:00
|
|
|
|
Features: lnwire.NewFeatureVector([]lnwire.Feature{}),
|
|
|
|
|
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.
|
|
|
|
|
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.
|
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 {
|
2017-03-28 22:08:14 +03:00
|
|
|
|
chanAnn.NodeID1 = localPubKey
|
|
|
|
|
chanAnn.NodeID2 = remotePubKey
|
|
|
|
|
chanAnn.BitcoinKey1 = localFundingKey
|
|
|
|
|
chanAnn.BitcoinKey2 = remoteFundingKey
|
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 {
|
2017-03-28 22:08:14 +03:00
|
|
|
|
chanAnn.NodeID1 = remotePubKey
|
|
|
|
|
chanAnn.NodeID2 = localPubKey
|
|
|
|
|
chanAnn.BitcoinKey1 = remoteFundingKey
|
|
|
|
|
chanAnn.BitcoinKey2 = localFundingKey
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 02:20:46 +03:00
|
|
|
|
chanUpdateAnn := &lnwire.ChannelUpdate{
|
2017-06-17 01:15:26 +03:00
|
|
|
|
ShortChannelID: shortChanID,
|
2017-08-22 09:25:41 +03:00
|
|
|
|
ChainHash: chainHash,
|
2017-06-17 01:15:26 +03:00
|
|
|
|
Timestamp: uint32(time.Now().Unix()),
|
|
|
|
|
Flags: chanFlags,
|
|
|
|
|
TimeLockDelta: uint16(f.cfg.DefaultRoutingPolicy.TimeLockDelta),
|
2017-08-22 09:25:41 +03:00
|
|
|
|
HtlcMinimumMsat: f.cfg.DefaultRoutingPolicy.MinHTLC,
|
2017-06-17 01:15:26 +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
|
|
|
|
|
}
|
|
|
|
|
chanUpdateAnn.Signature, err = f.cfg.SignMessage(f.cfg.IDKey, chanUpdateMsg)
|
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{
|
2017-04-17 01:34:23 +03:00
|
|
|
|
ChannelID: chanID,
|
|
|
|
|
ShortChannelID: shortChanID,
|
2017-03-28 22:08:14 +03:00
|
|
|
|
NodeSignature: nodeSig,
|
|
|
|
|
BitcoinSignature: bitcoinSig,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
2017-06-08 20:48:07 +03:00
|
|
|
|
chanID lnwire.ChannelID) 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,
|
|
|
|
|
localFundingKey, remoteFundingKey, shortChanID, chanID)
|
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-08-05 04:32:25 +03:00
|
|
|
|
// With the announcements crafted, we'll now send the announcements to
|
|
|
|
|
// the rest of the network.
|
|
|
|
|
//
|
2017-07-31 01:53:53 +03:00
|
|
|
|
// TODO(roasbeef): add flag that indicates if should be announced or
|
|
|
|
|
// not
|
2017-06-08 20:48:07 +03:00
|
|
|
|
|
|
|
|
|
// The announcement message consists of three distinct messages:
|
|
|
|
|
// 1. channel announcement 2. channel update 3. channel proof
|
|
|
|
|
// We must wait for them all to be successfully announced to the
|
|
|
|
|
// network, and/ if either fails we consider the announcement
|
|
|
|
|
// unsuccessful.
|
|
|
|
|
if err = f.cfg.SendAnnouncement(ann.chanAnn); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err = f.cfg.SendAnnouncement(ann.chanUpdateAnn); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err = f.cfg.SendAnnouncement(ann.chanProof); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
if err = f.cfg.SendAnnouncement(&nodeAnn); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
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..
|
2017-01-13 06:40:38 +03:00
|
|
|
|
func (f *fundingManager) initFundingWorkflow(peerAddress *lnwire.NetAddress,
|
|
|
|
|
req *openChanReq) {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
|
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
|
|
|
|
|
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, "+
|
2017-07-31 01:53:53 +03:00
|
|
|
|
"capacity=%v, chainhash=%v, addr=%v, dustLimit=%v)", localAmt,
|
|
|
|
|
msg.pushAmt, capacity, msg.chainHash, msg.peerAddress.Address,
|
2017-01-24 05:19:54 +03:00
|
|
|
|
ourDustLimit)
|
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
|
|
|
|
|
// commitment transaction into the next block (conf target of 1). We
|
|
|
|
|
// target the next block here to ensure that we'll be able to execute a
|
|
|
|
|
// timely unilateral channel closure if needed.
|
2017-07-31 01:53:53 +03:00
|
|
|
|
//
|
2017-05-17 05:10:55 +03:00
|
|
|
|
// TODO(roasbeef): shouldn't be targeting next block
|
|
|
|
|
feePerWeight := btcutil.Amount(f.cfg.FeeEstimator.EstimateFeePerWeight(1))
|
|
|
|
|
|
|
|
|
|
// The protocol currently operates on the basis of fee-per-kw, so we'll
|
|
|
|
|
// multiply the computed sat/weight by 1000 to arrive at fee-per-kw.
|
|
|
|
|
feePerKw := feePerWeight * 1000
|
|
|
|
|
|
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.
|
2017-01-24 05:19:54 +03:00
|
|
|
|
reservation, err := f.cfg.Wallet.InitChannelReservation(capacity,
|
2017-07-31 01:53:53 +03:00
|
|
|
|
localAmt, msg.pushAmt, feePerKw, peerKey,
|
|
|
|
|
msg.peerAddress.Address, &msg.chainHash)
|
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-04-17 01:34:23 +03:00
|
|
|
|
chanID := f.nextPendingChanID()
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
2017-05-17 05:10:55 +03:00
|
|
|
|
fndgLog.Infof("Target sat/kw for pendingID(%x): %v", chanID,
|
|
|
|
|
int64(feePerKw))
|
|
|
|
|
|
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{
|
2017-07-31 01:53:53 +03:00
|
|
|
|
chanAmt: capacity,
|
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()
|
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
// Using the RequiredRemoteDelay closure, we'll compute the remote CSV
|
|
|
|
|
// delay we require given the total amount of funds within the channel.
|
|
|
|
|
remoteCsvDelay := f.cfg.RequiredRemoteDelay(capacity)
|
|
|
|
|
|
|
|
|
|
// TODO(roasbeef): require remote delay?
|
|
|
|
|
|
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-04-17 01:34:23 +03:00
|
|
|
|
fndgLog.Infof("Starting funding workflow with %v for pendingID(%x)",
|
2017-04-01 15:33:17 +03:00
|
|
|
|
msg.peerAddress.Address, chanID)
|
2016-06-21 21:31:10 +03:00
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
fundingOpen := lnwire.OpenChannel{
|
|
|
|
|
ChainHash: *f.cfg.Wallet.Cfg.NetParams.GenesisHash,
|
|
|
|
|
PendingChannelID: chanID,
|
|
|
|
|
FundingAmount: capacity,
|
|
|
|
|
PushAmount: msg.pushAmt,
|
|
|
|
|
DustLimit: ourContribution.DustLimit,
|
|
|
|
|
MaxValueInFlight: ourContribution.MaxPendingAmount,
|
|
|
|
|
ChannelReserve: ourContribution.ChanReserve,
|
2017-08-22 09:33:19 +03:00
|
|
|
|
HtlcMinimum: ourContribution.MinHTLC,
|
2017-07-31 01:53:53 +03:00
|
|
|
|
FeePerKiloWeight: uint32(feePerKw),
|
|
|
|
|
CsvDelay: uint16(remoteCsvDelay),
|
|
|
|
|
MaxAcceptedHTLCs: ourContribution.MaxAcceptedHtlcs,
|
|
|
|
|
FundingKey: ourContribution.MultiSigKey,
|
|
|
|
|
RevocationPoint: ourContribution.RevocationBasePoint,
|
|
|
|
|
PaymentPoint: ourContribution.PaymentBasePoint,
|
|
|
|
|
DelayedPaymentPoint: ourContribution.DelayBasePoint,
|
|
|
|
|
FirstCommitmentPoint: ourContribution.FirstCommitmentPoint,
|
|
|
|
|
}
|
|
|
|
|
if err := f.cfg.SendToPeer(peerKey, &fundingOpen); err != nil {
|
2017-01-13 06:40:38 +03:00
|
|
|
|
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.
|
2017-04-17 01:34:23 +03:00
|
|
|
|
func (f *fundingManager) waitUntilChannelOpen(targetChan lnwire.ChannelID) {
|
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:
|
|
|
|
|
case <-f.quit: // TODO(roasbeef): add timer?
|
|
|
|
|
break
|
|
|
|
|
}
|
2017-04-17 01:34:23 +03:00
|
|
|
|
|
|
|
|
|
fndgLog.Tracef("barrier for ChanID(%v) closed", targetChan)
|
2017-01-24 02:33:46 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-23 23:39:54 +03:00
|
|
|
|
// processErrorGeneric sends a message to the fundingManager allowing it to
|
|
|
|
|
// process the occurred generic error.
|
2017-04-17 01:34:23 +03:00
|
|
|
|
func (f *fundingManager) processFundingError(err *lnwire.Error,
|
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.
|
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
|
|
|
|
peerKey := fmsg.peerAddress.IdentityKey
|
|
|
|
|
chanID := fmsg.err.ChanID
|
2017-01-25 04:12:51 +03:00
|
|
|
|
|
2017-08-31 06:31:33 +03:00
|
|
|
|
// First, we'll attempt to retrieve 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.
|
|
|
|
|
resCtx, err := f.getReservationCtx(peerKey, chanID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fndgLog.Warnf("Received error for non-existent funding "+
|
|
|
|
|
"flow: %v", protocolErr)
|
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.
|
|
|
|
|
lnErr := lnwire.ErrorCode(protocolErr.Data[0])
|
|
|
|
|
fndgLog.Errorf("Received funding error from %x: %v",
|
|
|
|
|
peerKey.SerializeCompressed(), lnErr,
|
|
|
|
|
)
|
|
|
|
|
resCtx.err <- grpc.Errorf(lnErr.ToGrpcCode(), lnErr.String())
|
2017-01-25 04:12:51 +03:00
|
|
|
|
|
2017-08-31 06:31:33 +03:00
|
|
|
|
if _, err := f.cancelReservationCtx(peerKey, chanID); err != nil {
|
|
|
|
|
fndgLog.Warnf("unable to delete reservation: %v", err)
|
|
|
|
|
return
|
2016-11-24 11:49:18 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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,
|
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-07-31 01:53:53 +03:00
|
|
|
|
"chan_id=%x", peerKey.SerializeCompressed(), pendingChanID)
|
2017-02-22 09:14:22 +03:00
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
ctx, err := f.getReservationCtx(peerKey, pendingChanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
|
if err != nil {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
return nil, errors.Errorf("unable to find reservation: %v",
|
2016-11-24 11:49:18 +03:00
|
|
|
|
err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-07-31 01:53:53 +03:00
|
|
|
|
f.deleteReservationCtx(peerKey, pendingChanID)
|
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()
|
2017-07-31 01:53:53 +03:00
|
|
|
|
delete(f.activeReservations[peerIDKey], pendingChanID)
|
2016-11-24 11:49:18 +03:00
|
|
|
|
f.resMtx.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2017-07-31 01:53:53 +03:00
|
|
|
|
return nil, errors.Errorf("unknown channel (id: %v)",
|
|
|
|
|
pendingChanID)
|
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
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
return f.cfg.Wallet.Cfg.Database.Update(func(tx *bolt.Tx) error {
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
|
|
|
|
|
if err = bucket.Put(outpointBytes.Bytes(), scratch); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
err := f.cfg.Wallet.Cfg.Database.View(func(tx *bolt.Tx) error {
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
return f.cfg.Wallet.Cfg.Database.Update(func(tx *bolt.Tx) error {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := bucket.Delete(outpointBytes.Bytes()); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|