2019-01-24 16:28:25 +03:00
|
|
|
package lnd
|
2015-12-21 00:16:38 +03:00
|
|
|
|
|
|
|
import (
|
2018-05-11 00:40:29 +03:00
|
|
|
"bytes"
|
2016-01-14 08:41:46 +03:00
|
|
|
"container/list"
|
2018-09-01 00:54:35 +03:00
|
|
|
"errors"
|
2016-07-10 02:41:06 +03:00
|
|
|
"fmt"
|
2015-12-21 02:10:09 +03:00
|
|
|
"net"
|
2015-12-21 00:16:38 +03:00
|
|
|
"sync"
|
2016-01-14 08:41:46 +03:00
|
|
|
"sync/atomic"
|
2015-12-21 00:16:38 +03:00
|
|
|
"time"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/connmgr"
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2016-06-21 22:32:32 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2018-09-20 13:23:01 +03:00
|
|
|
|
2018-05-11 00:40:29 +03:00
|
|
|
"github.com/lightningnetwork/lnd/brontide"
|
2019-02-22 07:11:33 +03:00
|
|
|
"github.com/lightningnetwork/lnd/buffer"
|
2017-05-05 02:03:47 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2016-06-21 22:32:32 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2018-05-11 00:40:29 +03:00
|
|
|
"github.com/lightningnetwork/lnd/contractcourt"
|
2017-05-02 23:04:58 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2018-07-05 23:27:35 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnpeer"
|
2016-01-18 06:14:47 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-02-22 07:10:51 +03:00
|
|
|
"github.com/lightningnetwork/lnd/pool"
|
2018-08-02 00:48:32 +03:00
|
|
|
"github.com/lightningnetwork/lnd/ticker"
|
2016-01-17 06:03:03 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
numNodes int32
|
2015-12-21 00:16:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-06-21 22:32:32 +03:00
|
|
|
// pingInterval is the interval at which ping messages are sent.
|
2017-01-23 01:35:26 +03:00
|
|
|
pingInterval = 1 * time.Minute
|
2016-01-17 06:03:03 +03:00
|
|
|
|
2017-10-16 01:13:27 +03:00
|
|
|
// idleTimeout is the duration of inactivity before we time out a peer.
|
|
|
|
idleTimeout = 5 * time.Minute
|
|
|
|
|
2019-03-05 00:55:09 +03:00
|
|
|
// writeMessageTimeout is the timeout used when writing a message to peer.
|
2019-03-27 02:40:24 +03:00
|
|
|
writeMessageTimeout = 5 * time.Second
|
2018-06-27 03:27:22 +03:00
|
|
|
|
2019-02-22 07:11:33 +03:00
|
|
|
// readMessageTimeout is the timeout used when reading a message from a
|
|
|
|
// peer.
|
|
|
|
readMessageTimeout = 5 * time.Second
|
|
|
|
|
2018-11-09 20:58:14 +03:00
|
|
|
// handshakeTimeout is the timeout used when waiting for peer init message.
|
|
|
|
handshakeTimeout = 15 * time.Second
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// outgoingQueueLen is the buffer size of the channel which houses
|
|
|
|
// messages to be sent across the wire, requested by objects outside
|
|
|
|
// this struct.
|
2016-01-17 06:03:03 +03:00
|
|
|
outgoingQueueLen = 50
|
2015-12-21 00:16:38 +03:00
|
|
|
)
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// outgoingMsg packages an lnwire.Message to be sent out on the wire, along with
|
2016-06-21 22:32:32 +03:00
|
|
|
// a buffered channel which will be sent upon once the write is complete. This
|
|
|
|
// buffered channel acts as a semaphore to be used for synchronization purposes.
|
2018-02-07 06:11:11 +03:00
|
|
|
type outgoingMsg struct {
|
2019-03-06 04:08:05 +03:00
|
|
|
priority bool
|
|
|
|
msg lnwire.Message
|
|
|
|
errChan chan error // MUST be buffered.
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 12:12:57 +03:00
|
|
|
// newChannelMsg packages a channeldb.OpenChannel with a channel that allows
|
|
|
|
// the receiver of the request to report when the funding transaction has been
|
|
|
|
// confirmed and the channel creation process completed.
|
2017-01-24 05:19:54 +03:00
|
|
|
type newChannelMsg struct {
|
2018-09-26 12:12:57 +03:00
|
|
|
channel *channeldb.OpenChannel
|
2018-09-26 12:12:57 +03:00
|
|
|
err chan error
|
2017-01-24 05:19:54 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// closeMsgs is a wrapper struct around any wire messages that deal with the
|
|
|
|
// cooperative channel closure negotiation process. This struct includes the
|
|
|
|
// raw channel ID targeted along with the original message.
|
|
|
|
type closeMsg struct {
|
|
|
|
cid lnwire.ChannelID
|
|
|
|
msg lnwire.Message
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// chanSnapshotReq is a message sent by outside subsystems to a peer in order
|
2016-06-23 08:22:06 +03:00
|
|
|
// to gain a snapshot of the peer's currently active channels.
|
|
|
|
type chanSnapshotReq struct {
|
|
|
|
resp chan []*channeldb.ChannelSnapshot
|
|
|
|
}
|
|
|
|
|
2018-12-20 20:52:27 +03:00
|
|
|
// pendingUpdate describes the pending state of a closing channel.
|
|
|
|
type pendingUpdate struct {
|
|
|
|
Txid []byte
|
|
|
|
OutputIndex uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// channelCloseUpdate contains the outcome of the close channel operation.
|
|
|
|
type channelCloseUpdate struct {
|
|
|
|
ClosingTxid []byte
|
|
|
|
Success bool
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// peer is an active peer on the Lightning Network. This struct is responsible
|
2016-11-11 04:15:25 +03:00
|
|
|
// for managing any channel state related to this peer. To do so, it has
|
|
|
|
// several helper goroutines to handle events such as HTLC timeouts, new
|
|
|
|
// funding workflow, and detecting an uncooperative closure of any active
|
|
|
|
// channels.
|
2016-09-26 20:39:47 +03:00
|
|
|
// TODO(roasbeef): proper reconnection logic
|
2015-12-21 00:16:38 +03:00
|
|
|
type peer struct {
|
2018-06-01 01:41:41 +03:00
|
|
|
// MUST be used atomically.
|
|
|
|
started int32
|
|
|
|
disconnect int32
|
|
|
|
|
2017-01-30 11:53:09 +03:00
|
|
|
// The following fields are only meant to be used *atomically*
|
|
|
|
bytesReceived uint64
|
|
|
|
bytesSent uint64
|
|
|
|
|
|
|
|
// pingTime is a rough estimate of the RTT (round-trip-time) between us
|
|
|
|
// and the connected peer. This time is expressed in micro seconds.
|
2018-06-01 01:41:41 +03:00
|
|
|
// To be used atomically.
|
2017-01-30 11:53:09 +03:00
|
|
|
// TODO(roasbeef): also use a WMA or EMA?
|
|
|
|
pingTime int64
|
|
|
|
|
|
|
|
// pingLastSend is the Unix time expressed in nanoseconds when we sent
|
2018-06-01 01:41:41 +03:00
|
|
|
// our last ping message. To be used atomically.
|
2017-01-30 11:53:09 +03:00
|
|
|
pingLastSend int64
|
|
|
|
|
2016-12-15 05:11:31 +03:00
|
|
|
connReq *connmgr.ConnReq
|
|
|
|
conn net.Conn
|
2015-12-21 00:16:38 +03:00
|
|
|
|
2016-10-28 05:49:10 +03:00
|
|
|
addr *lnwire.NetAddress
|
2017-06-17 01:11:02 +03:00
|
|
|
pubKeyBytes [33]byte
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2018-08-02 10:22:38 +03:00
|
|
|
// startTime is the time this peer connection was successfully
|
2018-09-01 00:54:35 +03:00
|
|
|
// established. It will be zero for peers that did not successfully
|
2018-08-02 10:22:38 +03:00
|
|
|
// Start().
|
|
|
|
startTime time.Time
|
2015-12-21 00:16:38 +03:00
|
|
|
|
2018-08-02 10:22:38 +03:00
|
|
|
inbound bool
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
// sendQueue is the channel which is used to queue outgoing to be
|
|
|
|
// written onto the wire. Note that this channel is unbuffered.
|
2018-02-07 06:11:11 +03:00
|
|
|
sendQueue chan outgoingMsg
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
// outgoingQueue is a buffered channel which allows second/third party
|
|
|
|
// objects to queue messages to be sent out on the wire.
|
2018-02-07 06:11:11 +03:00
|
|
|
outgoingQueue chan outgoingMsg
|
2015-12-21 00:16:38 +03:00
|
|
|
|
2019-02-15 04:13:44 +03:00
|
|
|
// activeChanMtx protects access to the activeChannels and
|
|
|
|
// addeddChannels maps.
|
|
|
|
activeChanMtx sync.RWMutex
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// activeChannels is a map which stores the state machines of all
|
|
|
|
// active channels. Channels are indexed into the map by the txid of
|
|
|
|
// the funding transaction which opened the channel.
|
2017-08-09 02:51:19 +03:00
|
|
|
activeChannels map[lnwire.ChannelID]*lnwallet.LightningChannel
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2019-02-15 04:13:44 +03:00
|
|
|
// addedChannels tracks any new channels opened during this peer's
|
|
|
|
// lifecycle. We use this to filter out these new channels when the time
|
|
|
|
// comes to request a reenable for active channels, since they will have
|
|
|
|
// waited a shorter duration.
|
|
|
|
addedChannels map[lnwire.ChannelID]struct{}
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// newChannels is used by the fundingManager to send fully opened
|
|
|
|
// channels to the source peer which handled the funding workflow.
|
2017-01-24 05:19:54 +03:00
|
|
|
newChannels chan *newChannelMsg
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// activeChanCloses is a map that keep track of all the active
|
|
|
|
// cooperative channel closures that are active. Any channel closing
|
|
|
|
// messages are directed to one of these active state machines. Once
|
|
|
|
// the channel has been closed, the state machine will be delete from
|
|
|
|
// the map.
|
|
|
|
activeChanCloses map[lnwire.ChannelID]*channelCloser
|
|
|
|
|
2016-11-11 04:15:25 +03:00
|
|
|
// localCloseChanReqs is a channel in which any local requests to close
|
|
|
|
// a particular channel are sent over.
|
2017-05-02 23:04:58 +03:00
|
|
|
localCloseChanReqs chan *htlcswitch.ChanClose
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2018-08-31 02:54:53 +03:00
|
|
|
// linkFailures receives all reported channel failures from the switch,
|
|
|
|
// and instructs the channelManager to clean remaining channel state.
|
|
|
|
linkFailures chan linkFailureReport
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// chanCloseMsgs is a channel that any message related to channel
|
|
|
|
// closures are sent over. This includes lnwire.Shutdown message as
|
|
|
|
// well as lnwire.ClosingSigned messages.
|
|
|
|
chanCloseMsgs chan *closeMsg
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2019-02-15 04:13:44 +03:00
|
|
|
// chanActiveTimeout specifies the duration the peer will wait to
|
|
|
|
// request a channel reenable, beginning from the time the peer was
|
|
|
|
// started.
|
|
|
|
chanActiveTimeout time.Duration
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
server *server
|
2015-12-21 00:16:38 +03:00
|
|
|
|
2017-10-19 01:16:03 +03:00
|
|
|
// localFeatures is the set of local features that we advertised to the
|
|
|
|
// remote node.
|
|
|
|
localFeatures *lnwire.RawFeatureVector
|
2017-02-16 15:39:38 +03:00
|
|
|
|
2019-04-03 13:18:19 +03:00
|
|
|
// outgoingCltvRejectDelta defines the number of blocks before expiry of
|
|
|
|
// an htlc where we don't offer an htlc anymore.
|
|
|
|
outgoingCltvRejectDelta uint32
|
2019-03-26 14:05:43 +03:00
|
|
|
|
2017-10-19 01:14:22 +03:00
|
|
|
// remoteLocalFeatures is the local feature vector received from the
|
|
|
|
// peer during the connection handshake.
|
|
|
|
remoteLocalFeatures *lnwire.FeatureVector
|
|
|
|
|
|
|
|
// remoteGlobalFeatures is the global feature vector received from the
|
|
|
|
// peer during the connection handshake.
|
|
|
|
remoteGlobalFeatures *lnwire.FeatureVector
|
2017-02-16 15:39:38 +03:00
|
|
|
|
2018-03-13 13:03:55 +03:00
|
|
|
// failedChannels is a set that tracks channels we consider `failed`.
|
|
|
|
// This is a temporary measure until we have implemented real failure
|
|
|
|
// handling at the link level, to handle the case where we reconnect to
|
|
|
|
// a peer and try to re-sync a failed channel, triggering a disconnect
|
|
|
|
// loop.
|
|
|
|
// TODO(halseth): remove when link failure is properly handled.
|
|
|
|
failedChannels map[lnwire.ChannelID]struct{}
|
|
|
|
|
2019-02-22 07:10:51 +03:00
|
|
|
// writePool is the task pool to that manages reuse of write buffers.
|
|
|
|
// Write tasks are submitted to the pool in order to conserve the total
|
|
|
|
// number of write buffers allocated at any one time, and decouple write
|
|
|
|
// buffer allocation from the peer life cycle.
|
|
|
|
writePool *pool.Write
|
peer: re-use a static writeBuf within writeMessage optimize memory usage
In this commit, we might a very small change to the way writing messages
works in the peer, which should have large implications w.r.t reducing
memory usage amongst chatty nodes.
When profiling the heap on one of my nodes earlier, I noticed this
fragment:
```
Showing top 20 nodes out of 68
flat flat% sum% cum cum%
0 0% 0% 75.53MB 54.61% main.(*peer).writeHandler
75.53MB 54.61% 54.61% 75.53MB 54.61% main.(*peer).writeMessage
```
Which points to an inefficiency with the way we handle allocations when
writing new messages, drilling down further we see:
```
(pprof) list writeMessage
Total: 138.31MB
ROUTINE ======================== main.(*peer).writeMessage in /root/go/src/github.com/lightningnetwork/lnd/peer.go
75.53MB 75.53MB (flat, cum) 54.61% of Total
. . 1104: p.logWireMessage(msg, false)
. . 1105:
. . 1106: // As the Lightning wire protocol is fully message oriented, we only
. . 1107: // allows one wire message per outer encapsulated crypto message. So
. . 1108: // we'll create a temporary buffer to write the message directly to.
75.53MB 75.53MB 1109: var msgPayload [lnwire.MaxMessagePayload]byte
. . 1110: b := bytes.NewBuffer(msgPayload[0:0:len(msgPayload)])
. . 1111:
. . 1112: // With the temp buffer created and sliced properly (length zero, full
. . 1113: // capacity), we'll now encode the message directly into this buffer.
. . 1114: n, err := lnwire.WriteMessage(b, msg, 0)
(pprof) list writeHandler
Total: 138.31MB
ROUTINE ======================== main.(*peer).writeHandler in /root/go/src/github.com/lightningnetwork/lnd/peer.go
0 75.53MB (flat, cum) 54.61% of Total
. . 1148:
. . 1149: // Write out the message to the socket, closing the
. . 1150: // 'sentChan' if it's non-nil, The 'sentChan' allows
. . 1151: // callers to optionally synchronize sends with the
. . 1152: // writeHandler.
. 75.53MB 1153: err := p.writeMessage(outMsg.msg)
. . 1154: if outMsg.errChan != nil {
. . 1155: outMsg.errChan <- err
. . 1156: }
. . 1157:
. . 1158: if err != nil {
```
Ah hah! We create a _new_ buffer each time we want to write a message
out. This is unnecessary and _very_ wasteful (as seen by the profile).
The fix is simple: re-use a buffer unique to each peer when writing out
messages. Since we know what the max message size is, we just allocate
one of these 65KB buffers for each peer, and keep it around until the
peer is removed.
2018-04-06 22:55:11 +03:00
|
|
|
|
2019-02-22 07:11:33 +03:00
|
|
|
readPool *pool.Read
|
|
|
|
|
2015-12-21 00:16:38 +03:00
|
|
|
queueQuit chan struct{}
|
|
|
|
quit chan struct{}
|
2016-01-14 08:41:46 +03:00
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2018-07-05 23:27:35 +03:00
|
|
|
// A compile-time check to ensure that peer satisfies the lnpeer.Peer interface.
|
|
|
|
var _ lnpeer.Peer = (*peer)(nil)
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// newPeer creates a new peer from an establish connection object, and a
|
|
|
|
// pointer to the main server.
|
2017-02-22 12:10:07 +03:00
|
|
|
func newPeer(conn net.Conn, connReq *connmgr.ConnReq, server *server,
|
2017-10-19 01:16:03 +03:00
|
|
|
addr *lnwire.NetAddress, inbound bool,
|
2019-02-15 04:13:44 +03:00
|
|
|
localFeatures *lnwire.RawFeatureVector,
|
2019-04-03 13:18:19 +03:00
|
|
|
chanActiveTimeout time.Duration,
|
2019-04-16 13:11:20 +03:00
|
|
|
outgoingCltvRejectDelta uint32) (
|
2019-03-26 14:05:43 +03:00
|
|
|
*peer, error) {
|
2016-10-28 05:49:10 +03:00
|
|
|
|
|
|
|
nodePub := addr.IdentityKey
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
p := &peer{
|
2017-06-17 01:11:02 +03:00
|
|
|
conn: conn,
|
|
|
|
addr: addr,
|
2016-10-28 05:49:10 +03:00
|
|
|
|
|
|
|
inbound: inbound,
|
2017-02-22 12:10:07 +03:00
|
|
|
connReq: connReq,
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
server: server,
|
2016-01-17 06:03:03 +03:00
|
|
|
|
2019-04-03 13:18:19 +03:00
|
|
|
localFeatures: localFeatures,
|
|
|
|
|
|
|
|
outgoingCltvRejectDelta: outgoingCltvRejectDelta,
|
2017-10-19 01:16:03 +03:00
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
sendQueue: make(chan outgoingMsg),
|
|
|
|
outgoingQueue: make(chan outgoingMsg),
|
2016-01-17 06:03:03 +03:00
|
|
|
|
2019-02-15 04:13:44 +03:00
|
|
|
addedChannels: make(map[lnwire.ChannelID]struct{}),
|
2017-08-09 02:51:19 +03:00
|
|
|
activeChannels: make(map[lnwire.ChannelID]*lnwallet.LightningChannel),
|
|
|
|
newChannels: make(chan *newChannelMsg, 1),
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
activeChanCloses: make(map[lnwire.ChannelID]*channelCloser),
|
|
|
|
localCloseChanReqs: make(chan *htlcswitch.ChanClose),
|
2018-08-31 02:54:53 +03:00
|
|
|
linkFailures: make(chan linkFailureReport),
|
2017-11-23 10:21:07 +03:00
|
|
|
chanCloseMsgs: make(chan *closeMsg),
|
2018-03-13 13:03:55 +03:00
|
|
|
failedChannels: make(map[lnwire.ChannelID]struct{}),
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2019-02-15 04:13:44 +03:00
|
|
|
chanActiveTimeout: chanActiveTimeout,
|
|
|
|
|
2019-02-22 07:10:51 +03:00
|
|
|
writePool: server.writePool,
|
2019-02-22 07:11:33 +03:00
|
|
|
readPool: server.readPool,
|
2019-02-01 06:21:36 +03:00
|
|
|
|
2016-01-17 06:03:03 +03:00
|
|
|
queueQuit: make(chan struct{}),
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
2017-06-17 01:11:02 +03:00
|
|
|
copy(p.pubKeyBytes[:], nodePub.SerializeCompressed())
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2017-04-12 07:59:45 +03:00
|
|
|
// Start starts all helper goroutines the peer needs for normal operations. In
|
|
|
|
// the case this peer has already been started, then this function is a loop.
|
2016-01-17 06:03:03 +03:00
|
|
|
func (p *peer) Start() error {
|
|
|
|
if atomic.AddInt32(&p.started, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-01 00:54:35 +03:00
|
|
|
peerLog.Tracef("Peer %v starting", p)
|
2016-01-17 06:03:03 +03:00
|
|
|
|
2017-03-17 05:45:10 +03:00
|
|
|
// Exchange local and global features, the init message should be very
|
|
|
|
// first between two nodes.
|
2017-02-16 15:39:38 +03:00
|
|
|
if err := p.sendInitMsg(); err != nil {
|
2017-04-24 05:24:28 +03:00
|
|
|
return fmt.Errorf("unable to send init msg: %v", err)
|
2017-02-16 15:39:38 +03:00
|
|
|
}
|
|
|
|
|
2017-03-17 05:45:10 +03:00
|
|
|
// Before we launch any of the helper goroutines off the peer struct,
|
2017-04-24 05:24:28 +03:00
|
|
|
// we'll first ensure proper adherence to the p2p protocol. The init
|
2017-03-17 05:45:10 +03:00
|
|
|
// message MUST be sent before any other message.
|
2017-03-30 04:33:20 +03:00
|
|
|
readErr := make(chan error, 1)
|
|
|
|
msgChan := make(chan lnwire.Message, 1)
|
2017-08-09 02:51:19 +03:00
|
|
|
p.wg.Add(1)
|
2017-03-30 04:33:20 +03:00
|
|
|
go func() {
|
2017-08-09 02:51:19 +03:00
|
|
|
defer p.wg.Done()
|
|
|
|
|
2017-04-20 02:23:17 +03:00
|
|
|
msg, err := p.readNextMessage()
|
2017-03-30 04:33:20 +03:00
|
|
|
if err != nil {
|
|
|
|
readErr <- err
|
|
|
|
msgChan <- nil
|
2017-08-11 04:07:45 +03:00
|
|
|
return
|
2017-03-30 04:33:20 +03:00
|
|
|
}
|
|
|
|
readErr <- nil
|
|
|
|
msgChan <- msg
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2017-04-12 07:59:45 +03:00
|
|
|
// In order to avoid blocking indefinitely, we'll give the other peer
|
2018-11-09 20:58:14 +03:00
|
|
|
// an upper timeout to respond before we bail out early.
|
|
|
|
case <-time.After(handshakeTimeout):
|
|
|
|
return fmt.Errorf("peer did not complete handshake within %v",
|
|
|
|
handshakeTimeout)
|
2017-03-30 04:33:20 +03:00
|
|
|
case err := <-readErr:
|
|
|
|
if err != nil {
|
2017-04-24 05:24:28 +03:00
|
|
|
return fmt.Errorf("unable to read init msg: %v", err)
|
2017-03-30 04:33:20 +03:00
|
|
|
}
|
2017-02-16 15:39:38 +03:00
|
|
|
}
|
|
|
|
|
2017-05-11 03:37:59 +03:00
|
|
|
// Once the init message arrives, we can parse it so we can figure out
|
|
|
|
// the negotiation of features for this session.
|
2017-03-30 04:33:20 +03:00
|
|
|
msg := <-msgChan
|
2017-02-16 15:39:38 +03:00
|
|
|
if msg, ok := msg.(*lnwire.Init); ok {
|
|
|
|
if err := p.handleInitMsg(msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("very first message between nodes " +
|
|
|
|
"must be init message")
|
|
|
|
}
|
|
|
|
|
2017-04-24 05:23:15 +03:00
|
|
|
// Fetch and then load all the active channels we have with this remote
|
|
|
|
// peer from the database.
|
|
|
|
activeChans, err := p.server.chanDB.FetchOpenChannels(p.addr.IdentityKey)
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Errorf("unable to fetch active chans "+
|
|
|
|
"for peer %v: %v", p, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-06 04:22:29 +03:00
|
|
|
if len(activeChans) == 0 {
|
|
|
|
p.server.prunePersistentPeerConnection(p.pubKeyBytes)
|
|
|
|
}
|
|
|
|
|
2017-04-24 05:23:15 +03:00
|
|
|
// Next, load all the active channels we have with this peer,
|
|
|
|
// registering them with the switch and launching the necessary
|
|
|
|
// goroutines required to operate them.
|
|
|
|
peerLog.Debugf("Loaded %v active channels from database with "+
|
2018-02-20 02:01:23 +03:00
|
|
|
"NodeKey(%x)", len(activeChans), p.PubKey())
|
2018-09-06 04:22:29 +03:00
|
|
|
|
2017-04-24 05:23:15 +03:00
|
|
|
if err := p.loadActiveChannels(activeChans); err != nil {
|
|
|
|
return fmt.Errorf("unable to load channels: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-02 10:22:38 +03:00
|
|
|
p.startTime = time.Now()
|
|
|
|
|
2017-05-11 03:37:59 +03:00
|
|
|
p.wg.Add(5)
|
|
|
|
go p.queueHandler()
|
|
|
|
go p.writeHandler()
|
|
|
|
go p.readHandler()
|
|
|
|
go p.channelManager()
|
|
|
|
go p.pingHandler()
|
|
|
|
|
2016-01-17 06:03:03 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-04 07:58:48 +03:00
|
|
|
// initGossipSync initializes either a gossip syncer or an initial routing
|
|
|
|
// dump, depending on the negotiated synchronization method.
|
|
|
|
func (p *peer) initGossipSync() {
|
|
|
|
switch {
|
|
|
|
|
|
|
|
// If the remote peer knows of the new gossip queries feature, then
|
|
|
|
// we'll create a new gossipSyncer in the AuthenticatedGossiper for it.
|
|
|
|
case p.remoteLocalFeatures.HasFeature(lnwire.GossipQueriesOptional):
|
|
|
|
srvrLog.Infof("Negotiated chan series queries with %x",
|
|
|
|
p.pubKeyBytes[:])
|
|
|
|
|
|
|
|
// Register the this peer's for gossip syncer with the gossiper.
|
|
|
|
// This is blocks synchronously to ensure the gossip syncer is
|
|
|
|
// registered with the gossiper before attempting to read
|
|
|
|
// messages from the remote peer.
|
2019-03-23 05:56:33 +03:00
|
|
|
//
|
|
|
|
// TODO(wilmer): Only sync updates from non-channel peers. This
|
|
|
|
// requires an improved version of the current network
|
|
|
|
// bootstrapper to ensure we can find and connect to non-channel
|
|
|
|
// peers.
|
|
|
|
p.server.authGossiper.InitSyncState(p)
|
2018-09-04 07:58:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 03:10:57 +03:00
|
|
|
// QuitSignal is a method that should return a channel which will be sent upon
|
|
|
|
// or closed once the backing peer exits. This allows callers using the
|
|
|
|
// interface to cancel any processing in the event the backing implementation
|
|
|
|
// exits.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the lnpeer.Peer interface.
|
|
|
|
func (p *peer) QuitSignal() <-chan struct{} {
|
|
|
|
return p.quit
|
|
|
|
}
|
|
|
|
|
2017-04-24 05:23:15 +03:00
|
|
|
// loadActiveChannels creates indexes within the peer for tracking all active
|
|
|
|
// channels returned by the database.
|
|
|
|
func (p *peer) loadActiveChannels(chans []*channeldb.OpenChannel) error {
|
|
|
|
for _, dbChan := range chans {
|
2018-01-17 07:25:34 +03:00
|
|
|
lnChan, err := lnwallet.NewLightningChannel(
|
2019-04-15 15:24:43 +03:00
|
|
|
p.server.cc.signer, dbChan, p.server.sigPool,
|
2018-01-17 07:25:34 +03:00
|
|
|
)
|
2017-04-24 05:23:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-17 06:03:03 +03:00
|
|
|
|
2017-07-31 00:13:28 +03:00
|
|
|
chanPoint := &dbChan.FundingOutpoint
|
2017-10-03 02:30:17 +03:00
|
|
|
|
2017-07-31 00:13:28 +03:00
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
|
2017-04-24 05:23:15 +03:00
|
|
|
|
2018-03-13 13:03:55 +03:00
|
|
|
peerLog.Infof("NodeKey(%x) loading ChannelPoint(%v)",
|
|
|
|
p.PubKey(), chanPoint)
|
2017-04-24 05:23:15 +03:00
|
|
|
|
2017-12-18 08:45:35 +03:00
|
|
|
// Skip adding any permanently irreconcilable channels to the
|
|
|
|
// htlcswitch.
|
2018-12-10 07:03:08 +03:00
|
|
|
switch {
|
|
|
|
case dbChan.HasChanStatus(channeldb.ChanStatusBorked):
|
|
|
|
fallthrough
|
|
|
|
case dbChan.HasChanStatus(channeldb.ChanStatusCommitBroadcasted):
|
|
|
|
fallthrough
|
|
|
|
case dbChan.HasChanStatus(channeldb.ChanStatusLocalDataLoss):
|
2018-04-03 22:53:22 +03:00
|
|
|
peerLog.Warnf("ChannelPoint(%v) has status %v, won't "+
|
2018-08-11 12:12:09 +03:00
|
|
|
"start.", chanPoint, dbChan.ChanStatus())
|
2018-03-13 13:03:55 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also skip adding any channel marked as `failed` for this
|
|
|
|
// session.
|
|
|
|
if _, ok := p.failedChannels[chanID]; ok {
|
|
|
|
peerLog.Warnf("ChannelPoint(%v) is failed, won't "+
|
|
|
|
"start.", chanPoint)
|
2017-12-18 08:45:35 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-08-03 07:15:49 +03:00
|
|
|
_, currentHeight, err := p.server.cc.chainIO.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-22 09:53:21 +03:00
|
|
|
|
|
|
|
// Before we register this new link with the HTLC Switch, we'll
|
|
|
|
// need to fetch its current link-layer forwarding policy from
|
|
|
|
// the database.
|
|
|
|
graph := p.server.chanDB.ChannelGraph()
|
2017-08-23 21:32:50 +03:00
|
|
|
info, p1, p2, err := graph.FetchChannelEdgesByOutpoint(chanPoint)
|
2017-08-31 01:33:49 +03:00
|
|
|
if err != nil && err != channeldb.ErrEdgeNotFound {
|
2017-08-22 09:53:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll filter out our policy from the directional channel
|
|
|
|
// edges based whom the edge connects to. If it doesn't connect
|
|
|
|
// to us, then we know that we were the one that advertised the
|
|
|
|
// policy.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): can add helper method to get policy for
|
|
|
|
// particular channel.
|
|
|
|
var selfPolicy *channeldb.ChannelEdgePolicy
|
2018-01-31 07:30:00 +03:00
|
|
|
if info != nil && bytes.Equal(info.NodeKey1Bytes[:],
|
|
|
|
p.server.identityPriv.PubKey().SerializeCompressed()) {
|
|
|
|
|
2017-08-22 09:53:21 +03:00
|
|
|
selfPolicy = p1
|
|
|
|
} else {
|
|
|
|
selfPolicy = p2
|
|
|
|
}
|
|
|
|
|
2017-08-23 21:32:50 +03:00
|
|
|
// If we don't yet have an advertised routing policy, then
|
|
|
|
// we'll use the current default, otherwise we'll translate the
|
|
|
|
// routing policy into a forwarding policy.
|
|
|
|
var forwardingPolicy *htlcswitch.ForwardingPolicy
|
|
|
|
if selfPolicy != nil {
|
|
|
|
forwardingPolicy = &htlcswitch.ForwardingPolicy{
|
|
|
|
MinHTLC: selfPolicy.MinHTLC,
|
2018-12-09 05:18:20 +03:00
|
|
|
MaxHTLC: selfPolicy.MaxHTLC,
|
2017-08-23 21:32:50 +03:00
|
|
|
BaseFee: selfPolicy.FeeBaseMSat,
|
|
|
|
FeeRate: selfPolicy.FeeProportionalMillionths,
|
|
|
|
TimeLockDelta: uint32(selfPolicy.TimeLockDelta),
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-22 10:32:44 +03:00
|
|
|
peerLog.Warnf("Unable to find our forwarding policy "+
|
|
|
|
"for channel %v, using default values",
|
|
|
|
chanPoint)
|
2017-08-23 21:32:50 +03:00
|
|
|
forwardingPolicy = &p.server.cc.routingPolicy
|
2017-08-22 09:53:21 +03:00
|
|
|
}
|
|
|
|
|
2018-08-22 10:32:44 +03:00
|
|
|
peerLog.Tracef("Using link policy of: %v",
|
|
|
|
spew.Sdump(forwardingPolicy))
|
2017-08-22 09:53:21 +03:00
|
|
|
|
|
|
|
// Register this new channel link with the HTLC Switch. This is
|
|
|
|
// necessary to properly route multi-hop payments, and forward
|
|
|
|
// new payments triggered by RPC clients.
|
2018-01-21 07:25:54 +03:00
|
|
|
chainEvents, err := p.server.chainArb.SubscribeChannelEvents(
|
2018-04-18 14:57:02 +03:00
|
|
|
*chanPoint,
|
2018-01-21 07:25:54 +03:00
|
|
|
)
|
2018-01-19 01:03:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-02 23:04:58 +03:00
|
|
|
|
2018-05-23 14:33:41 +03:00
|
|
|
// Create the link and add it to the switch.
|
2018-06-12 09:52:38 +03:00
|
|
|
err = p.addLink(
|
2018-06-12 05:05:00 +03:00
|
|
|
chanPoint, lnChan, forwardingPolicy, chainEvents,
|
|
|
|
currentHeight, true,
|
2018-06-12 09:52:38 +03:00
|
|
|
)
|
2018-05-23 14:33:41 +03:00
|
|
|
if err != nil {
|
2018-08-02 00:48:32 +03:00
|
|
|
return fmt.Errorf("unable to add link %v to switch: %v",
|
|
|
|
chanPoint, err)
|
2017-05-02 23:04:58 +03:00
|
|
|
}
|
2018-06-12 09:52:38 +03:00
|
|
|
|
|
|
|
p.activeChanMtx.Lock()
|
|
|
|
p.activeChannels[chanID] = lnChan
|
|
|
|
p.activeChanMtx.Unlock()
|
2017-04-24 05:23:15 +03:00
|
|
|
}
|
2016-01-17 06:03:03 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 14:33:41 +03:00
|
|
|
// addLink creates and adds a new link from the specified channel.
|
|
|
|
func (p *peer) addLink(chanPoint *wire.OutPoint,
|
|
|
|
lnChan *lnwallet.LightningChannel,
|
|
|
|
forwardingPolicy *htlcswitch.ForwardingPolicy,
|
|
|
|
chainEvents *contractcourt.ChainEventSubscription,
|
|
|
|
currentHeight int32, syncStates bool) error {
|
|
|
|
|
2018-05-23 14:39:04 +03:00
|
|
|
// onChannelFailure will be called by the link in case the channel
|
|
|
|
// fails for some reason.
|
|
|
|
onChannelFailure := func(chanID lnwire.ChannelID,
|
|
|
|
shortChanID lnwire.ShortChannelID,
|
|
|
|
linkErr htlcswitch.LinkFailureError) {
|
|
|
|
|
2018-08-31 02:54:53 +03:00
|
|
|
failure := linkFailureReport{
|
|
|
|
chanPoint: *chanPoint,
|
|
|
|
chanID: chanID,
|
|
|
|
shortChanID: shortChanID,
|
|
|
|
linkErr: linkErr,
|
|
|
|
}
|
|
|
|
|
2018-05-26 05:08:59 +03:00
|
|
|
select {
|
2018-08-31 02:54:53 +03:00
|
|
|
case p.linkFailures <- failure:
|
|
|
|
case <-p.quit:
|
2018-05-26 05:08:59 +03:00
|
|
|
case <-p.server.quit:
|
|
|
|
}
|
2018-05-23 14:39:04 +03:00
|
|
|
}
|
|
|
|
|
2018-05-23 14:33:41 +03:00
|
|
|
linkCfg := htlcswitch.ChannelLinkConfig{
|
2018-08-22 10:32:44 +03:00
|
|
|
Peer: p,
|
|
|
|
DecodeHopIterators: p.server.sphinx.DecodeHopIterators,
|
|
|
|
ExtractErrorEncrypter: p.server.sphinx.ExtractErrorEncrypter,
|
|
|
|
FetchLastChannelUpdate: p.server.fetchLastChanUpdate(),
|
|
|
|
HodlMask: cfg.Hodl.Mask(),
|
|
|
|
Registry: p.server.invoices,
|
|
|
|
Switch: p.server.htlcSwitch,
|
|
|
|
Circuits: p.server.htlcSwitch.CircuitModifier(),
|
|
|
|
ForwardPackets: p.server.htlcSwitch.ForwardPackets,
|
|
|
|
FwrdingPolicy: *forwardingPolicy,
|
|
|
|
FeeEstimator: p.server.cc.feeEstimator,
|
|
|
|
PreimageCache: p.server.witnessBeacon,
|
|
|
|
ChainEvents: chainEvents,
|
2018-05-23 14:33:41 +03:00
|
|
|
UpdateContractSignals: func(signals *contractcourt.ContractSignals) error {
|
|
|
|
return p.server.chainArb.UpdateContractSignals(
|
|
|
|
*chanPoint, signals,
|
|
|
|
)
|
|
|
|
},
|
2019-04-03 13:18:19 +03:00
|
|
|
OnChannelFailure: onChannelFailure,
|
|
|
|
SyncStates: syncStates,
|
|
|
|
BatchTicker: ticker.New(50 * time.Millisecond),
|
|
|
|
FwdPkgGCTicker: ticker.New(time.Minute),
|
|
|
|
BatchSize: 10,
|
|
|
|
UnsafeReplay: cfg.UnsafeReplay,
|
|
|
|
MinFeeUpdateTimeout: htlcswitch.DefaultMinLinkFeeUpdateTimeout,
|
|
|
|
MaxFeeUpdateTimeout: htlcswitch.DefaultMaxLinkFeeUpdateTimeout,
|
|
|
|
OutgoingCltvRejectDelta: p.outgoingCltvRejectDelta,
|
2019-06-14 03:30:04 +03:00
|
|
|
TowerClient: p.server.towerClient,
|
2019-07-27 04:05:58 +03:00
|
|
|
MaxOutgoingCltvExpiry: cfg.MaxOutgoingCltvExpiry,
|
2018-05-23 14:33:41 +03:00
|
|
|
}
|
2018-05-11 00:40:29 +03:00
|
|
|
|
2018-06-01 06:31:40 +03:00
|
|
|
link := htlcswitch.NewChannelLink(linkCfg, lnChan)
|
2018-05-23 14:33:41 +03:00
|
|
|
|
2018-08-02 12:34:23 +03:00
|
|
|
// Before adding our new link, purge the switch of any pending or live
|
|
|
|
// links going by the same channel id. If one is found, we'll shut it
|
|
|
|
// down to ensure that the mailboxes are only ever under the control of
|
|
|
|
// one link.
|
|
|
|
p.server.htlcSwitch.RemoveLink(link.ChanID())
|
|
|
|
|
2018-05-23 14:33:41 +03:00
|
|
|
// With the channel link created, we'll now notify the htlc switch so
|
|
|
|
// this channel can be used to dispatch local payments and also
|
|
|
|
// passively forward payments.
|
|
|
|
return p.server.htlcSwitch.AddLink(link)
|
|
|
|
}
|
|
|
|
|
2017-04-24 05:29:38 +03:00
|
|
|
// WaitForDisconnect waits until the peer has disconnected. A peer may be
|
|
|
|
// disconnected if the local or remote side terminating the connection, or an
|
2018-07-31 06:51:08 +03:00
|
|
|
// irrecoverable protocol error has been encountered. This method will only
|
|
|
|
// begin watching the peer's waitgroup after the ready channel or the peer's
|
|
|
|
// quit channel are signaled. The ready channel should only be signaled if a
|
|
|
|
// call to Start returns no error. Otherwise, if the peer fails to start,
|
|
|
|
// calling Disconnect will signal the quit channel and the method will not
|
|
|
|
// block, since no goroutines were spawned.
|
|
|
|
func (p *peer) WaitForDisconnect(ready chan struct{}) {
|
|
|
|
select {
|
|
|
|
case <-ready:
|
|
|
|
case <-p.quit:
|
|
|
|
}
|
|
|
|
|
2018-05-08 06:31:22 +03:00
|
|
|
p.wg.Wait()
|
2017-04-24 05:29:38 +03:00
|
|
|
}
|
2017-02-21 09:06:16 +03:00
|
|
|
|
2016-07-14 02:40:01 +03:00
|
|
|
// Disconnect terminates the connection with the remote peer. Additionally, a
|
|
|
|
// signal is sent to the server and htlcSwitch indicating the resources
|
|
|
|
// allocated to the peer can now be cleaned up.
|
2017-07-12 16:44:17 +03:00
|
|
|
func (p *peer) Disconnect(reason error) {
|
2016-07-14 02:40:01 +03:00
|
|
|
if !atomic.CompareAndSwapInt32(&p.disconnect, 0, 1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-01 00:54:35 +03:00
|
|
|
peerLog.Infof("Disconnecting %s, reason: %v", p, reason)
|
2017-02-07 02:04:52 +03:00
|
|
|
|
|
|
|
// Ensure that the TCP connection is properly closed before continuing.
|
|
|
|
p.conn.Close()
|
2016-07-14 02:40:01 +03:00
|
|
|
|
|
|
|
close(p.quit)
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// String returns the string representation of this peer.
|
|
|
|
func (p *peer) String() string {
|
2019-03-27 02:41:13 +03:00
|
|
|
return fmt.Sprintf("%x@%s", p.pubKeyBytes, p.conn.RemoteAddr())
|
2016-06-21 22:32:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// readNextMessage reads, and returns the next message on the wire along with
|
|
|
|
// any additional raw payload.
|
2017-04-20 02:23:17 +03:00
|
|
|
func (p *peer) readNextMessage() (lnwire.Message, error) {
|
2017-04-21 01:45:04 +03:00
|
|
|
noiseConn, ok := p.conn.(*brontide.Conn)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("brontide.Conn required to read messages")
|
|
|
|
}
|
|
|
|
|
2019-02-22 07:11:33 +03:00
|
|
|
err := noiseConn.SetReadDeadline(time.Time{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pktLen, err := noiseConn.ReadNextHeader()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-21 01:45:04 +03:00
|
|
|
// First we'll read the next _full_ message. We do this rather than
|
|
|
|
// reading incrementally from the stream as the Lightning wire protocol
|
|
|
|
// is message oriented and allows nodes to pad on additional data to
|
|
|
|
// the message stream.
|
2019-02-22 07:11:33 +03:00
|
|
|
var rawMsg []byte
|
|
|
|
err = p.readPool.Submit(func(buf *buffer.Read) error {
|
|
|
|
// Before reading the body of the message, set the read timeout
|
|
|
|
// accordingly to ensure we don't block other readers using the
|
|
|
|
// pool. We do so only after the task has been scheduled to
|
|
|
|
// ensure the deadline doesn't expire while the message is in
|
|
|
|
// the process of being scheduled.
|
|
|
|
readDeadline := time.Now().Add(readMessageTimeout)
|
|
|
|
readErr := noiseConn.SetReadDeadline(readDeadline)
|
|
|
|
if readErr != nil {
|
|
|
|
return readErr
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMsg, readErr = noiseConn.ReadNextBody(buf[:pktLen])
|
|
|
|
return readErr
|
|
|
|
})
|
|
|
|
|
2017-04-21 01:45:04 +03:00
|
|
|
atomic.AddUint64(&p.bytesReceived, uint64(len(rawMsg)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, create a new io.Reader implementation from the raw message,
|
|
|
|
// and use this to decode the message directly from.
|
|
|
|
msgReader := bytes.NewReader(rawMsg)
|
|
|
|
nextMsg, err := lnwire.ReadMessage(msgReader, 0)
|
2016-01-14 08:41:46 +03:00
|
|
|
if err != nil {
|
2017-04-20 02:23:17 +03:00
|
|
|
return nil, err
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2017-01-15 04:52:05 +03:00
|
|
|
p.logWireMessage(nextMsg, true)
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2017-04-20 02:23:17 +03:00
|
|
|
return nextMsg, nil
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
// msgStream implements a goroutine-safe, in-order stream of messages to be
|
|
|
|
// delivered via closure to a receiver. These messages MUST be in order due to
|
|
|
|
// the nature of the lightning channel commitment and gossiper state machines.
|
|
|
|
// TODO(conner): use stream handler interface to abstract out stream
|
|
|
|
// state/logging
|
|
|
|
type msgStream struct {
|
2018-06-01 01:41:41 +03:00
|
|
|
streamShutdown int32 // To be used atomically.
|
2018-01-09 05:41:15 +03:00
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
peer *peer
|
2017-08-01 07:20:36 +03:00
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
apply func(lnwire.Message)
|
2017-08-01 07:20:36 +03:00
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
startMsg string
|
|
|
|
stopMsg string
|
2017-08-01 07:20:36 +03:00
|
|
|
|
|
|
|
msgCond *sync.Cond
|
|
|
|
msgs []lnwire.Message
|
|
|
|
|
|
|
|
mtx sync.Mutex
|
|
|
|
|
2018-03-13 02:34:03 +03:00
|
|
|
bufSize uint32
|
|
|
|
producerSema chan struct{}
|
|
|
|
|
2017-08-09 02:51:19 +03:00
|
|
|
wg sync.WaitGroup
|
2017-08-01 07:20:36 +03:00
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
// newMsgStream creates a new instance of a chanMsgStream for a particular
|
2018-03-13 02:34:03 +03:00
|
|
|
// channel identified by its channel ID. bufSize is the max number of messages
|
|
|
|
// that should be buffered in the internal queue. Callers should set this to a
|
|
|
|
// sane value that avoids blocking unnecessarily, but doesn't allow an
|
|
|
|
// unbounded amount of memory to be allocated to buffer incoming messages.
|
|
|
|
func newMsgStream(p *peer, startMsg, stopMsg string, bufSize uint32,
|
2017-11-02 01:50:55 +03:00
|
|
|
apply func(lnwire.Message)) *msgStream {
|
|
|
|
|
|
|
|
stream := &msgStream{
|
2018-03-13 02:34:03 +03:00
|
|
|
peer: p,
|
|
|
|
apply: apply,
|
|
|
|
startMsg: startMsg,
|
|
|
|
stopMsg: stopMsg,
|
|
|
|
producerSema: make(chan struct{}, bufSize),
|
|
|
|
quit: make(chan struct{}),
|
2017-08-01 07:20:36 +03:00
|
|
|
}
|
|
|
|
stream.msgCond = sync.NewCond(&stream.mtx)
|
|
|
|
|
2018-03-13 02:34:03 +03:00
|
|
|
// Before we return the active stream, we'll populate the producer's
|
|
|
|
// semaphore channel. We'll use this to ensure that the producer won't
|
|
|
|
// attempt to allocate memory in the queue for an item until it has
|
|
|
|
// sufficient extra space.
|
|
|
|
for i := uint32(0); i < bufSize; i++ {
|
|
|
|
stream.producerSema <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2017-08-01 07:20:36 +03:00
|
|
|
return stream
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the chanMsgStream.
|
2017-11-02 01:50:55 +03:00
|
|
|
func (ms *msgStream) Start() {
|
|
|
|
ms.wg.Add(1)
|
|
|
|
go ms.msgConsumer()
|
2017-08-01 07:20:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the chanMsgStream.
|
2017-11-02 01:50:55 +03:00
|
|
|
func (ms *msgStream) Stop() {
|
2017-08-01 07:20:36 +03:00
|
|
|
// TODO(roasbeef): signal too?
|
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
close(ms.quit)
|
2017-08-01 07:31:16 +03:00
|
|
|
|
2018-01-09 05:41:15 +03:00
|
|
|
// Now that we've closed the channel, we'll repeatedly signal the msg
|
|
|
|
// consumer until we've detected that it has exited.
|
|
|
|
for atomic.LoadInt32(&ms.streamShutdown) == 0 {
|
|
|
|
ms.msgCond.Signal()
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
}
|
2017-08-09 02:51:19 +03:00
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
ms.wg.Wait()
|
2017-08-01 07:20:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// msgConsumer is the main goroutine that streams messages from the peer's
|
|
|
|
// readHandler directly to the target channel.
|
2017-11-02 01:50:55 +03:00
|
|
|
func (ms *msgStream) msgConsumer() {
|
|
|
|
defer ms.wg.Done()
|
|
|
|
defer peerLog.Tracef(ms.stopMsg)
|
2018-08-25 03:54:26 +03:00
|
|
|
defer atomic.StoreInt32(&ms.streamShutdown, 1)
|
2017-08-09 02:51:19 +03:00
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
peerLog.Tracef(ms.startMsg)
|
2017-08-01 07:20:36 +03:00
|
|
|
|
|
|
|
for {
|
|
|
|
// First, we'll check our condition. If the queue of messages
|
|
|
|
// is empty, then we'll wait until a new item is added.
|
2017-11-02 01:50:55 +03:00
|
|
|
ms.msgCond.L.Lock()
|
|
|
|
for len(ms.msgs) == 0 {
|
|
|
|
ms.msgCond.Wait()
|
2017-08-01 07:31:16 +03:00
|
|
|
|
2018-01-09 05:41:15 +03:00
|
|
|
// If we woke up in order to exit, then we'll do so.
|
|
|
|
// Otherwise, we'll check the message queue for any new
|
|
|
|
// items.
|
2017-08-01 07:31:16 +03:00
|
|
|
select {
|
2018-08-25 03:58:24 +03:00
|
|
|
case <-ms.peer.quit:
|
2018-09-04 03:03:00 +03:00
|
|
|
ms.msgCond.L.Unlock()
|
2018-08-25 03:58:24 +03:00
|
|
|
return
|
2017-11-02 01:50:55 +03:00
|
|
|
case <-ms.quit:
|
|
|
|
ms.msgCond.L.Unlock()
|
2017-08-01 07:31:16 +03:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2017-08-01 07:20:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the message off the front of the queue, shifting the
|
|
|
|
// slice's reference down one in order to remove the message
|
|
|
|
// from the queue.
|
2017-11-02 01:50:55 +03:00
|
|
|
msg := ms.msgs[0]
|
|
|
|
ms.msgs[0] = nil // Set to nil to prevent GC leak.
|
|
|
|
ms.msgs = ms.msgs[1:]
|
|
|
|
|
|
|
|
ms.msgCond.L.Unlock()
|
2017-08-01 07:20:36 +03:00
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
ms.apply(msg)
|
2018-03-13 02:34:03 +03:00
|
|
|
|
|
|
|
// We've just successfully processed an item, so we'll signal
|
|
|
|
// to the producer that a new slot in the buffer. We'll use
|
|
|
|
// this to bound the size of the buffer to avoid allowing it to
|
|
|
|
// grow indefinitely.
|
|
|
|
select {
|
|
|
|
case ms.producerSema <- struct{}{}:
|
2018-08-25 03:58:24 +03:00
|
|
|
case <-ms.peer.quit:
|
|
|
|
return
|
2018-03-13 02:34:03 +03:00
|
|
|
case <-ms.quit:
|
|
|
|
return
|
|
|
|
}
|
2017-08-01 07:20:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
// AddMsg adds a new message to the msgStream. This function is safe for
|
2017-08-01 07:20:36 +03:00
|
|
|
// concurrent access.
|
2018-11-02 03:08:24 +03:00
|
|
|
func (ms *msgStream) AddMsg(msg lnwire.Message) {
|
2018-03-13 02:34:03 +03:00
|
|
|
// First, we'll attempt to receive from the producerSema struct. This
|
|
|
|
// acts as a sempahore to prevent us from indefinitely buffering
|
|
|
|
// incoming items from the wire. Either the msg queue isn't full, and
|
|
|
|
// we'll not block, or the queue is full, and we'll block until either
|
|
|
|
// we're signalled to quit, or a slot is freed up.
|
|
|
|
select {
|
|
|
|
case <-ms.producerSema:
|
2018-11-02 03:08:24 +03:00
|
|
|
case <-ms.peer.quit:
|
2018-08-27 05:58:41 +03:00
|
|
|
return
|
2018-03-13 02:34:03 +03:00
|
|
|
case <-ms.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll lock the condition, and add the message to the end of
|
2017-08-01 07:20:36 +03:00
|
|
|
// the message queue.
|
2017-11-02 01:50:55 +03:00
|
|
|
ms.msgCond.L.Lock()
|
|
|
|
ms.msgs = append(ms.msgs, msg)
|
|
|
|
ms.msgCond.L.Unlock()
|
2017-08-01 07:20:36 +03:00
|
|
|
|
|
|
|
// With the message added, we signal to the msgConsumer that there are
|
|
|
|
// additional messages to consume.
|
2017-11-02 01:50:55 +03:00
|
|
|
ms.msgCond.Signal()
|
|
|
|
}
|
|
|
|
|
|
|
|
// newChanMsgStream is used to create a msgStream between the peer and
|
|
|
|
// particular channel link in the htlcswitch. We utilize additional
|
|
|
|
// synchronization with the fundingManager to ensure we don't attempt to
|
|
|
|
// dispatch a message to a channel before it is fully active. A reference to the
|
|
|
|
// channel this stream forwards to his held in scope to prevent unnecessary
|
|
|
|
// lookups.
|
|
|
|
func newChanMsgStream(p *peer, cid lnwire.ChannelID) *msgStream {
|
|
|
|
|
|
|
|
var chanLink htlcswitch.ChannelLink
|
|
|
|
|
|
|
|
return newMsgStream(p,
|
2018-01-09 05:41:15 +03:00
|
|
|
fmt.Sprintf("Update stream for ChannelID(%x) created", cid[:]),
|
|
|
|
fmt.Sprintf("Update stream for ChannelID(%x) exiting", cid[:]),
|
2018-03-13 02:34:03 +03:00
|
|
|
1000,
|
2017-11-02 01:50:55 +03:00
|
|
|
func(msg lnwire.Message) {
|
2018-11-20 17:09:44 +03:00
|
|
|
_, isChanSyncMsg := msg.(*lnwire.ChannelReestablish)
|
2017-12-07 03:30:50 +03:00
|
|
|
|
2017-12-18 05:40:05 +03:00
|
|
|
// If this is the chanSync message, then we'll deliver
|
|
|
|
// it immediately to the active link.
|
2018-11-20 17:09:44 +03:00
|
|
|
if !isChanSyncMsg {
|
2017-12-07 03:30:50 +03:00
|
|
|
// We'll send a message to the funding manager
|
|
|
|
// and wait iff an active funding process for
|
|
|
|
// this channel hasn't yet completed. We do
|
|
|
|
// this in order to account for the following
|
|
|
|
// scenario: we send the funding locked message
|
|
|
|
// to the other side, they immediately send a
|
|
|
|
// channel update message, but we haven't yet
|
|
|
|
// sent the channel to the channelManager.
|
2018-08-25 05:58:29 +03:00
|
|
|
err := p.server.fundingMgr.waitUntilChannelOpen(
|
2018-08-25 03:57:06 +03:00
|
|
|
cid, p.quit,
|
|
|
|
)
|
2018-08-25 05:58:29 +03:00
|
|
|
if err != nil {
|
|
|
|
// If we have a non-nil error, then the
|
|
|
|
// funding manager is shutting down, s
|
|
|
|
// we can exit here without attempting
|
|
|
|
// to deliver the message.
|
|
|
|
return
|
|
|
|
}
|
2017-12-07 03:30:50 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 03:16:22 +03:00
|
|
|
// In order to avoid unnecessarily delivering message
|
|
|
|
// as the peer is exiting, we'll check quickly to see
|
|
|
|
// if we need to exit.
|
|
|
|
select {
|
|
|
|
case <-p.quit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch the commitment update message to the proper
|
|
|
|
// active goroutine dedicated to this channel.
|
2017-11-02 01:50:55 +03:00
|
|
|
if chanLink == nil {
|
|
|
|
link, err := p.server.htlcSwitch.GetLink(cid)
|
2018-11-20 17:09:46 +03:00
|
|
|
switch {
|
|
|
|
|
|
|
|
// If we failed to find the link in question,
|
|
|
|
// and the message received was a channel sync
|
|
|
|
// message, then this might be a peer trying to
|
|
|
|
// resync closed channel. In this case we'll
|
|
|
|
// try to resend our last channel sync message,
|
|
|
|
// such that the peer can recover funds from
|
|
|
|
// the closed channel.
|
|
|
|
case err != nil && isChanSyncMsg:
|
|
|
|
peerLog.Debugf("Unable to find "+
|
|
|
|
"link(%v) to handle channel "+
|
|
|
|
"sync, attempting to resend "+
|
|
|
|
"last ChanSync message", cid)
|
|
|
|
|
|
|
|
err := p.resendChanSyncMsg(cid)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(halseth): send error to
|
|
|
|
// peer?
|
|
|
|
peerLog.Errorf(
|
|
|
|
"resend failed: %v",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
case err != nil:
|
2018-11-20 17:09:44 +03:00
|
|
|
peerLog.Errorf("recv'd update for "+
|
|
|
|
"unknown channel %v from %v: "+
|
|
|
|
"%v", cid, p, err)
|
2017-11-02 01:50:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
chanLink = link
|
|
|
|
}
|
|
|
|
|
2018-08-26 03:16:22 +03:00
|
|
|
// In order to avoid unnecessarily delivering message
|
|
|
|
// as the peer is exiting, we'll check quickly to see
|
|
|
|
// if we need to exit.
|
|
|
|
select {
|
|
|
|
case <-p.quit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
chanLink.HandleChannelUpdate(msg)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newDiscMsgStream is used to setup a msgStream between the peer and the
|
|
|
|
// authenticated gossiper. This stream should be used to forward all remote
|
|
|
|
// channel announcements.
|
|
|
|
func newDiscMsgStream(p *peer) *msgStream {
|
|
|
|
return newMsgStream(p,
|
|
|
|
"Update stream for gossiper created",
|
|
|
|
"Update stream for gossiper exited",
|
2018-03-13 02:34:03 +03:00
|
|
|
1000,
|
2017-11-02 01:50:55 +03:00
|
|
|
func(msg lnwire.Message) {
|
2018-06-08 06:07:30 +03:00
|
|
|
p.server.authGossiper.ProcessRemoteAnnouncement(msg, p)
|
2017-11-02 01:50:55 +03:00
|
|
|
},
|
|
|
|
)
|
2017-08-01 07:20:36 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// readHandler is responsible for reading messages off the wire in series, then
|
2017-01-13 08:01:50 +03:00
|
|
|
// properly dispatching the handling of the message to the proper subsystem.
|
2016-06-21 22:32:32 +03:00
|
|
|
//
|
|
|
|
// NOTE: This method MUST be run as a goroutine.
|
|
|
|
func (p *peer) readHandler() {
|
2018-08-25 03:49:12 +03:00
|
|
|
defer p.wg.Done()
|
2017-08-09 02:51:19 +03:00
|
|
|
|
2017-10-16 01:13:27 +03:00
|
|
|
// We'll stop the timer after a new messages is received, and also
|
|
|
|
// reset it after we process the next message.
|
|
|
|
idleTimer := time.AfterFunc(idleTimeout, func() {
|
|
|
|
err := fmt.Errorf("Peer %s no answer for %s -- disconnecting",
|
|
|
|
p, idleTimeout)
|
|
|
|
p.Disconnect(err)
|
|
|
|
})
|
|
|
|
|
2019-01-19 05:30:55 +03:00
|
|
|
// Initialize our negotiated gossip sync method before reading messages
|
|
|
|
// off the wire. When using gossip queries, this ensures a gossip
|
|
|
|
// syncer is active by the time query messages arrive.
|
2018-09-04 07:58:48 +03:00
|
|
|
//
|
|
|
|
// TODO(conner): have peer store gossip syncer directly and bypass
|
|
|
|
// gossiper?
|
|
|
|
p.initGossipSync()
|
|
|
|
|
2017-11-02 01:50:55 +03:00
|
|
|
discStream := newDiscMsgStream(p)
|
|
|
|
discStream.Start()
|
|
|
|
defer discStream.Stop()
|
|
|
|
|
|
|
|
chanMsgStreams := make(map[lnwire.ChannelID]*msgStream)
|
2016-01-14 08:41:46 +03:00
|
|
|
out:
|
|
|
|
for atomic.LoadInt32(&p.disconnect) == 0 {
|
2017-04-20 02:23:17 +03:00
|
|
|
nextMsg, err := p.readNextMessage()
|
2019-03-27 02:40:41 +03:00
|
|
|
if !idleTimer.Stop() {
|
|
|
|
select {
|
|
|
|
case <-idleTimer.C:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 08:41:46 +03:00
|
|
|
if err != nil {
|
2017-01-24 07:33:18 +03:00
|
|
|
peerLog.Infof("unable to read message from %v: %v",
|
|
|
|
p, err)
|
2017-01-17 05:03:34 +03:00
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
// If this is just a message we don't yet recognize,
|
|
|
|
// we'll continue processing as normal as this allows
|
|
|
|
// us to introduce new messages in a forwards
|
|
|
|
// compatible manner.
|
|
|
|
case *lnwire.UnknownMessage:
|
2017-10-16 01:13:27 +03:00
|
|
|
idleTimer.Reset(idleTimeout)
|
2017-01-17 05:03:34 +03:00
|
|
|
continue
|
|
|
|
|
2018-03-24 01:49:25 +03:00
|
|
|
// If they sent us an address type that we don't yet
|
|
|
|
// know of, then this isn't a dire error, so we'll
|
|
|
|
// simply continue parsing the remainder of their
|
|
|
|
// messages.
|
|
|
|
case *lnwire.ErrUnknownAddrType:
|
|
|
|
idleTimer.Reset(idleTimeout)
|
|
|
|
continue
|
|
|
|
|
2019-01-04 08:11:41 +03:00
|
|
|
// If the NodeAnnouncement has an invalid alias, then
|
|
|
|
// we'll log that error above and continue so we can
|
|
|
|
// continue to read messges from the peer.
|
|
|
|
case *lnwire.ErrInvalidNodeAlias:
|
|
|
|
idleTimer.Reset(idleTimeout)
|
|
|
|
continue
|
|
|
|
|
2017-01-17 05:03:34 +03:00
|
|
|
// If the error we encountered wasn't just a message we
|
|
|
|
// didn't recognize, then we'll stop all processing s
|
|
|
|
// this is a fatal error.
|
|
|
|
default:
|
|
|
|
break out
|
|
|
|
}
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2017-02-21 05:10:05 +03:00
|
|
|
var (
|
|
|
|
isChanUpdate bool
|
2017-04-17 01:41:11 +03:00
|
|
|
targetChan lnwire.ChannelID
|
2017-02-21 05:10:05 +03:00
|
|
|
)
|
2016-07-13 03:45:29 +03:00
|
|
|
|
2016-01-14 08:41:46 +03:00
|
|
|
switch msg := nextMsg.(type) {
|
2017-01-26 05:20:55 +03:00
|
|
|
case *lnwire.Pong:
|
|
|
|
// When we receive a Pong message in response to our
|
|
|
|
// last ping message, we'll use the time in which we
|
|
|
|
// sent the ping message to measure a rough estimate of
|
|
|
|
// round trip time.
|
|
|
|
pingSendTime := atomic.LoadInt64(&p.pingLastSend)
|
|
|
|
delay := (time.Now().UnixNano() - pingSendTime) / 1000
|
|
|
|
atomic.StoreInt64(&p.pingTime, delay)
|
|
|
|
|
2016-11-11 04:15:25 +03:00
|
|
|
case *lnwire.Ping:
|
2017-04-17 04:11:39 +03:00
|
|
|
pongBytes := make([]byte, msg.NumPongBytes)
|
|
|
|
p.queueMsg(lnwire.NewPong(pongBytes), nil)
|
2016-11-11 04:15:25 +03:00
|
|
|
|
2017-07-31 00:13:28 +03:00
|
|
|
case *lnwire.OpenChannel:
|
2018-07-05 23:41:51 +03:00
|
|
|
p.server.fundingMgr.processFundingOpen(msg, p)
|
2017-07-31 00:13:28 +03:00
|
|
|
case *lnwire.AcceptChannel:
|
2018-07-05 23:41:51 +03:00
|
|
|
p.server.fundingMgr.processFundingAccept(msg, p)
|
2017-07-31 00:13:28 +03:00
|
|
|
case *lnwire.FundingCreated:
|
2018-07-05 23:41:51 +03:00
|
|
|
p.server.fundingMgr.processFundingCreated(msg, p)
|
2017-07-31 00:13:28 +03:00
|
|
|
case *lnwire.FundingSigned:
|
2018-07-05 23:41:51 +03:00
|
|
|
p.server.fundingMgr.processFundingSigned(msg, p)
|
2017-01-31 05:45:28 +03:00
|
|
|
case *lnwire.FundingLocked:
|
2018-07-05 23:41:51 +03:00
|
|
|
p.server.fundingMgr.processFundingLocked(msg, p)
|
2017-03-25 04:26:09 +03:00
|
|
|
|
|
|
|
case *lnwire.Shutdown:
|
2017-09-28 06:18:20 +03:00
|
|
|
select {
|
2017-11-23 10:21:07 +03:00
|
|
|
case p.chanCloseMsgs <- &closeMsg{msg.ChannelID, msg}:
|
2017-09-28 06:18:20 +03:00
|
|
|
case <-p.quit:
|
|
|
|
break out
|
|
|
|
}
|
2017-03-09 02:32:11 +03:00
|
|
|
case *lnwire.ClosingSigned:
|
2017-09-28 06:18:20 +03:00
|
|
|
select {
|
2017-11-23 10:21:07 +03:00
|
|
|
case p.chanCloseMsgs <- &closeMsg{msg.ChannelID, msg}:
|
2017-09-28 06:18:20 +03:00
|
|
|
case <-p.quit:
|
|
|
|
break out
|
|
|
|
}
|
2016-10-15 16:24:56 +03:00
|
|
|
|
2017-04-17 01:41:11 +03:00
|
|
|
case *lnwire.Error:
|
2018-07-05 23:38:11 +03:00
|
|
|
key := p.addr.IdentityKey
|
2018-03-01 02:40:32 +03:00
|
|
|
|
2018-07-05 23:38:11 +03:00
|
|
|
switch {
|
2018-03-01 02:40:32 +03:00
|
|
|
// In the case of an all-zero channel ID we want to
|
|
|
|
// forward the error to all channels with this peer.
|
|
|
|
case msg.ChanID == lnwire.ConnectionWideID:
|
2018-03-13 13:03:55 +03:00
|
|
|
for chanID, chanStream := range chanMsgStreams {
|
2018-11-02 03:08:24 +03:00
|
|
|
chanStream.AddMsg(nextMsg)
|
2018-03-13 13:03:55 +03:00
|
|
|
|
|
|
|
// Also marked this channel as failed,
|
|
|
|
// so we won't try to restart it on
|
|
|
|
// reconnect with this peer.
|
|
|
|
p.failedChannels[chanID] = struct{}{}
|
2018-03-01 02:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the channel ID for the error message corresponds
|
|
|
|
// to a pending channel, then the funding manager will
|
|
|
|
// handle the error.
|
2018-07-05 23:38:11 +03:00
|
|
|
case p.server.fundingMgr.IsPendingChannel(msg.ChanID, key):
|
|
|
|
p.server.fundingMgr.processFundingError(msg, key)
|
2018-03-01 02:40:32 +03:00
|
|
|
|
|
|
|
// If not we hand the error to the channel link for
|
|
|
|
// this channel.
|
|
|
|
default:
|
|
|
|
isChanUpdate = true
|
|
|
|
targetChan = msg.ChanID
|
2018-03-13 13:03:55 +03:00
|
|
|
|
|
|
|
// Also marked this channel as failed, so we
|
|
|
|
// won't try to restart it on reconnect with
|
|
|
|
// this peer.
|
|
|
|
p.failedChannels[targetChan] = struct{}{}
|
2018-03-01 02:40:32 +03:00
|
|
|
}
|
2016-12-27 08:42:23 +03:00
|
|
|
|
2017-01-08 07:23:06 +03:00
|
|
|
// TODO(roasbeef): create ChanUpdater interface for the below
|
2017-02-21 05:10:05 +03:00
|
|
|
case *lnwire.UpdateAddHTLC:
|
2016-10-15 16:18:38 +03:00
|
|
|
isChanUpdate = true
|
2017-04-17 01:41:11 +03:00
|
|
|
targetChan = msg.ChanID
|
2018-02-07 06:11:11 +03:00
|
|
|
case *lnwire.UpdateFulfillHTLC:
|
2016-10-15 16:18:38 +03:00
|
|
|
isChanUpdate = true
|
2017-12-12 22:22:44 +03:00
|
|
|
targetChan = msg.ChanID
|
|
|
|
case *lnwire.UpdateFailMalformedHTLC:
|
|
|
|
isChanUpdate = true
|
2017-04-17 01:41:11 +03:00
|
|
|
targetChan = msg.ChanID
|
2017-02-21 05:10:05 +03:00
|
|
|
case *lnwire.UpdateFailHTLC:
|
2017-01-08 07:23:06 +03:00
|
|
|
isChanUpdate = true
|
2017-04-17 01:41:11 +03:00
|
|
|
targetChan = msg.ChanID
|
2017-02-21 05:10:05 +03:00
|
|
|
case *lnwire.RevokeAndAck:
|
2016-10-15 16:18:38 +03:00
|
|
|
isChanUpdate = true
|
2017-04-17 01:41:11 +03:00
|
|
|
targetChan = msg.ChanID
|
2017-02-21 05:10:05 +03:00
|
|
|
case *lnwire.CommitSig:
|
2016-10-15 16:18:38 +03:00
|
|
|
isChanUpdate = true
|
2017-04-17 01:41:11 +03:00
|
|
|
targetChan = msg.ChanID
|
2017-07-14 21:43:20 +03:00
|
|
|
case *lnwire.UpdateFee:
|
|
|
|
isChanUpdate = true
|
|
|
|
targetChan = msg.ChanID
|
2017-07-09 02:30:20 +03:00
|
|
|
case *lnwire.ChannelReestablish:
|
|
|
|
isChanUpdate = true
|
|
|
|
targetChan = msg.ChanID
|
2016-12-27 08:42:23 +03:00
|
|
|
|
2017-04-20 02:23:17 +03:00
|
|
|
case *lnwire.ChannelUpdate,
|
2016-12-27 08:42:23 +03:00
|
|
|
*lnwire.ChannelAnnouncement,
|
2017-03-28 22:08:14 +03:00
|
|
|
*lnwire.NodeAnnouncement,
|
2018-04-17 05:09:57 +03:00
|
|
|
*lnwire.AnnounceSignatures,
|
|
|
|
*lnwire.GossipTimestampRange,
|
|
|
|
*lnwire.QueryShortChanIDs,
|
|
|
|
*lnwire.QueryChannelRange,
|
|
|
|
*lnwire.ReplyChannelRange,
|
|
|
|
*lnwire.ReplyShortChanIDsEnd:
|
2016-12-27 08:42:23 +03:00
|
|
|
|
2018-11-02 03:08:24 +03:00
|
|
|
discStream.AddMsg(msg)
|
2017-11-02 01:50:55 +03:00
|
|
|
|
2017-03-28 22:08:14 +03:00
|
|
|
default:
|
2017-10-17 04:43:14 +03:00
|
|
|
peerLog.Errorf("unknown message %v received from peer "+
|
|
|
|
"%v", uint16(msg.MsgType()), p)
|
2016-07-13 03:45:29 +03:00
|
|
|
}
|
|
|
|
|
2016-10-15 16:18:38 +03:00
|
|
|
if isChanUpdate {
|
2017-08-01 07:25:46 +03:00
|
|
|
// If this is a channel update, then we need to feed it
|
|
|
|
// into the channel's in-order message stream.
|
|
|
|
chanStream, ok := chanMsgStreams[targetChan]
|
|
|
|
if !ok {
|
|
|
|
// If a stream hasn't yet been created, then
|
|
|
|
// we'll do so, add it to the map, and finally
|
|
|
|
// start it.
|
2017-11-02 01:50:55 +03:00
|
|
|
chanStream = newChanMsgStream(p, targetChan)
|
2017-08-01 07:25:46 +03:00
|
|
|
chanMsgStreams[targetChan] = chanStream
|
|
|
|
chanStream.Start()
|
2018-08-25 03:53:07 +03:00
|
|
|
defer chanStream.Stop()
|
2016-07-13 03:45:29 +03:00
|
|
|
}
|
2017-04-17 01:45:18 +03:00
|
|
|
|
2017-08-01 07:25:46 +03:00
|
|
|
// With the stream obtained, add the message to the
|
|
|
|
// stream so we can continue processing message.
|
2018-11-02 03:08:24 +03:00
|
|
|
chanStream.AddMsg(nextMsg)
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
2017-10-16 01:13:27 +03:00
|
|
|
|
|
|
|
idleTimer.Reset(idleTimeout)
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2017-07-12 16:44:17 +03:00
|
|
|
p.Disconnect(errors.New("read handler closed"))
|
2016-07-14 02:40:01 +03:00
|
|
|
|
|
|
|
peerLog.Tracef("readHandler for peer %v done", p)
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2017-10-18 05:13:19 +03:00
|
|
|
// messageSummary returns a human-readable string that summarizes a
|
|
|
|
// incoming/outgoing message. Not all messages will have a summary, only those
|
|
|
|
// which have additional data that can be informative at a glance.
|
|
|
|
func messageSummary(msg lnwire.Message) string {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *lnwire.Init:
|
|
|
|
// No summary.
|
|
|
|
return ""
|
|
|
|
|
|
|
|
case *lnwire.OpenChannel:
|
2017-10-18 05:35:10 +03:00
|
|
|
return fmt.Sprintf("temp_chan_id=%x, chain=%v, csv=%v, amt=%v, "+
|
2017-10-18 05:13:19 +03:00
|
|
|
"push_amt=%v, reserve=%v, flags=%v",
|
2017-10-18 05:35:10 +03:00
|
|
|
msg.PendingChannelID[:], msg.ChainHash,
|
2017-10-18 05:13:19 +03:00
|
|
|
msg.CsvDelay, msg.FundingAmount, msg.PushAmount,
|
|
|
|
msg.ChannelReserve, msg.ChannelFlags)
|
|
|
|
|
|
|
|
case *lnwire.AcceptChannel:
|
2017-11-28 02:32:06 +03:00
|
|
|
return fmt.Sprintf("temp_chan_id=%x, reserve=%v, csv=%v, num_confs=%v",
|
|
|
|
msg.PendingChannelID[:], msg.ChannelReserve, msg.CsvDelay,
|
|
|
|
msg.MinAcceptDepth)
|
2017-10-18 05:13:19 +03:00
|
|
|
|
|
|
|
case *lnwire.FundingCreated:
|
|
|
|
return fmt.Sprintf("temp_chan_id=%x, chan_point=%v",
|
|
|
|
msg.PendingChannelID[:], msg.FundingPoint)
|
|
|
|
|
|
|
|
case *lnwire.FundingSigned:
|
|
|
|
return fmt.Sprintf("chan_id=%v", msg.ChanID)
|
|
|
|
|
|
|
|
case *lnwire.FundingLocked:
|
|
|
|
return fmt.Sprintf("chan_id=%v, next_point=%x",
|
|
|
|
msg.ChanID, msg.NextPerCommitmentPoint.SerializeCompressed())
|
|
|
|
|
|
|
|
case *lnwire.Shutdown:
|
|
|
|
return fmt.Sprintf("chan_id=%v, script=%x", msg.ChannelID,
|
|
|
|
msg.Address[:])
|
|
|
|
|
|
|
|
case *lnwire.ClosingSigned:
|
|
|
|
return fmt.Sprintf("chan_id=%v, fee_sat=%v", msg.ChannelID,
|
|
|
|
msg.FeeSatoshis)
|
|
|
|
|
|
|
|
case *lnwire.UpdateAddHTLC:
|
|
|
|
return fmt.Sprintf("chan_id=%v, id=%v, amt=%v, expiry=%v, hash=%x",
|
|
|
|
msg.ChanID, msg.ID, msg.Amount, msg.Expiry, msg.PaymentHash[:])
|
|
|
|
|
|
|
|
case *lnwire.UpdateFailHTLC:
|
2017-10-21 01:40:50 +03:00
|
|
|
return fmt.Sprintf("chan_id=%v, id=%v, reason=%x", msg.ChanID,
|
2017-10-18 05:13:19 +03:00
|
|
|
msg.ID, msg.Reason)
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
case *lnwire.UpdateFulfillHTLC:
|
2017-10-18 05:13:19 +03:00
|
|
|
return fmt.Sprintf("chan_id=%v, id=%v, pre_image=%x",
|
|
|
|
msg.ChanID, msg.ID, msg.PaymentPreimage[:])
|
|
|
|
|
|
|
|
case *lnwire.CommitSig:
|
|
|
|
return fmt.Sprintf("chan_id=%v, num_htlcs=%v", msg.ChanID,
|
|
|
|
len(msg.HtlcSigs))
|
|
|
|
|
|
|
|
case *lnwire.RevokeAndAck:
|
|
|
|
return fmt.Sprintf("chan_id=%v, rev=%x, next_point=%x",
|
|
|
|
msg.ChanID, msg.Revocation[:],
|
|
|
|
msg.NextRevocationKey.SerializeCompressed())
|
|
|
|
|
|
|
|
case *lnwire.UpdateFailMalformedHTLC:
|
|
|
|
return fmt.Sprintf("chan_id=%v, id=%v, fail_code=%v",
|
|
|
|
msg.ChanID, msg.ID, msg.FailureCode)
|
|
|
|
|
|
|
|
case *lnwire.Error:
|
2017-12-01 09:19:12 +03:00
|
|
|
return fmt.Sprintf("chan_id=%v, err=%v", msg.ChanID, string(msg.Data))
|
2017-10-18 05:13:19 +03:00
|
|
|
|
|
|
|
case *lnwire.AnnounceSignatures:
|
|
|
|
return fmt.Sprintf("chan_id=%v, short_chan_id=%v", msg.ChannelID,
|
|
|
|
msg.ShortChannelID.ToUint64())
|
|
|
|
|
|
|
|
case *lnwire.ChannelAnnouncement:
|
2017-10-18 05:35:10 +03:00
|
|
|
return fmt.Sprintf("chain_hash=%v, short_chan_id=%v",
|
|
|
|
msg.ChainHash, msg.ShortChannelID.ToUint64())
|
2017-10-18 05:13:19 +03:00
|
|
|
|
|
|
|
case *lnwire.ChannelUpdate:
|
2019-01-12 20:59:43 +03:00
|
|
|
return fmt.Sprintf("chain_hash=%v, short_chan_id=%v, "+
|
|
|
|
"mflags=%v, cflags=%v, update_time=%v", msg.ChainHash,
|
|
|
|
msg.ShortChannelID.ToUint64(), msg.MessageFlags,
|
|
|
|
msg.ChannelFlags, time.Unix(int64(msg.Timestamp), 0))
|
2017-10-18 05:13:19 +03:00
|
|
|
|
|
|
|
case *lnwire.NodeAnnouncement:
|
|
|
|
return fmt.Sprintf("node=%x, update_time=%v",
|
2018-01-31 07:30:00 +03:00
|
|
|
msg.NodeID, time.Unix(int64(msg.Timestamp), 0))
|
2017-10-18 05:13:19 +03:00
|
|
|
|
|
|
|
case *lnwire.Ping:
|
|
|
|
// No summary.
|
|
|
|
return ""
|
|
|
|
|
|
|
|
case *lnwire.Pong:
|
|
|
|
// No summary.
|
|
|
|
return ""
|
|
|
|
|
|
|
|
case *lnwire.UpdateFee:
|
|
|
|
return fmt.Sprintf("chan_id=%v, fee_update_sat=%v",
|
|
|
|
msg.ChanID, int64(msg.FeePerKw))
|
2017-11-11 02:40:15 +03:00
|
|
|
|
|
|
|
case *lnwire.ChannelReestablish:
|
|
|
|
return fmt.Sprintf("next_local_height=%v, remote_tail_height=%v",
|
|
|
|
msg.NextLocalCommitHeight, msg.RemoteCommitTailHeight)
|
2018-04-17 05:10:15 +03:00
|
|
|
|
|
|
|
case *lnwire.ReplyShortChanIDsEnd:
|
|
|
|
return fmt.Sprintf("chain_hash=%v, complete=%v", msg.ChainHash,
|
|
|
|
msg.Complete)
|
|
|
|
|
|
|
|
case *lnwire.ReplyChannelRange:
|
|
|
|
return fmt.Sprintf("complete=%v, encoding=%v, num_chans=%v",
|
|
|
|
msg.Complete, msg.EncodingType, len(msg.ShortChanIDs))
|
|
|
|
|
|
|
|
case *lnwire.QueryShortChanIDs:
|
|
|
|
return fmt.Sprintf("chain_hash=%v, encoding=%v, num_chans=%v",
|
|
|
|
msg.ChainHash, msg.EncodingType, len(msg.ShortChanIDs))
|
|
|
|
|
|
|
|
case *lnwire.QueryChannelRange:
|
|
|
|
return fmt.Sprintf("chain_hash=%v, start_height=%v, "+
|
|
|
|
"num_blocks=%v", msg.ChainHash, msg.FirstBlockHeight,
|
|
|
|
msg.NumBlocks)
|
|
|
|
|
|
|
|
case *lnwire.GossipTimestampRange:
|
|
|
|
return fmt.Sprintf("chain_hash=%v, first_stamp=%v, "+
|
|
|
|
"stamp_range=%v", msg.ChainHash,
|
|
|
|
time.Unix(int64(msg.FirstTimestamp), 0),
|
|
|
|
msg.TimestampRange)
|
|
|
|
|
2017-10-18 05:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-01-15 04:52:05 +03:00
|
|
|
// logWireMessage logs the receipt or sending of particular wire message. This
|
|
|
|
// function is used rather than just logging the message in order to produce
|
|
|
|
// less spammy log messages in trace mode by setting the 'Curve" parameter to
|
|
|
|
// nil. Doing this avoids printing out each of the field elements in the curve
|
|
|
|
// parameters for secp256k1.
|
|
|
|
func (p *peer) logWireMessage(msg lnwire.Message, read bool) {
|
2017-10-18 05:13:19 +03:00
|
|
|
summaryPrefix := "Received"
|
|
|
|
if !read {
|
|
|
|
summaryPrefix = "Sending"
|
|
|
|
}
|
|
|
|
|
|
|
|
peerLog.Debugf("%v", newLogClosure(func() string {
|
|
|
|
// Debug summary of message.
|
|
|
|
summary := messageSummary(msg)
|
|
|
|
if len(summary) > 0 {
|
|
|
|
summary = "(" + summary + ")"
|
|
|
|
}
|
|
|
|
|
2017-10-20 05:45:29 +03:00
|
|
|
preposition := "to"
|
|
|
|
if read {
|
|
|
|
preposition = "from"
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v %v%s %v %s", summaryPrefix,
|
|
|
|
msg.MsgType(), summary, preposition, p)
|
2017-10-18 05:13:19 +03:00
|
|
|
}))
|
|
|
|
|
2017-01-15 04:52:05 +03:00
|
|
|
switch m := msg.(type) {
|
2017-12-07 03:27:56 +03:00
|
|
|
case *lnwire.ChannelReestablish:
|
2017-12-08 00:11:23 +03:00
|
|
|
if m.LocalUnrevokedCommitPoint != nil {
|
|
|
|
m.LocalUnrevokedCommitPoint.Curve = nil
|
|
|
|
}
|
2017-02-21 05:10:05 +03:00
|
|
|
case *lnwire.RevokeAndAck:
|
2017-01-15 04:52:05 +03:00
|
|
|
m.NextRevocationKey.Curve = nil
|
2017-07-31 00:13:28 +03:00
|
|
|
case *lnwire.AcceptChannel:
|
|
|
|
m.FundingKey.Curve = nil
|
|
|
|
m.RevocationPoint.Curve = nil
|
|
|
|
m.PaymentPoint.Curve = nil
|
|
|
|
m.DelayedPaymentPoint.Curve = nil
|
2017-12-02 06:27:38 +03:00
|
|
|
m.HtlcPoint.Curve = nil
|
2017-07-31 00:13:28 +03:00
|
|
|
m.FirstCommitmentPoint.Curve = nil
|
|
|
|
case *lnwire.OpenChannel:
|
|
|
|
m.FundingKey.Curve = nil
|
|
|
|
m.RevocationPoint.Curve = nil
|
|
|
|
m.PaymentPoint.Curve = nil
|
|
|
|
m.DelayedPaymentPoint.Curve = nil
|
2017-12-02 06:27:38 +03:00
|
|
|
m.HtlcPoint.Curve = nil
|
2017-07-31 00:13:28 +03:00
|
|
|
m.FirstCommitmentPoint.Curve = nil
|
2017-01-31 05:45:28 +03:00
|
|
|
case *lnwire.FundingLocked:
|
|
|
|
m.NextPerCommitmentPoint.Curve = nil
|
2017-01-15 04:52:05 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 04:43:38 +03:00
|
|
|
prefix := "readMessage from"
|
2017-01-15 04:52:05 +03:00
|
|
|
if !read {
|
2017-01-25 04:43:38 +03:00
|
|
|
prefix = "writeMessage to"
|
2017-01-15 04:52:05 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 04:43:38 +03:00
|
|
|
peerLog.Tracef(prefix+" %v: %v", p, newLogClosure(func() string {
|
2017-01-15 04:52:05 +03:00
|
|
|
return spew.Sdump(msg)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-04-23 02:05:26 +03:00
|
|
|
// writeMessage writes and flushes the target lnwire.Message to the remote peer.
|
|
|
|
// If the passed message is nil, this method will only try to flush an existing
|
|
|
|
// message buffered on the connection. It is safe to recall this method with a
|
|
|
|
// nil message iff a timeout error is returned. This will continue to flush the
|
|
|
|
// pending message to the wire.
|
2016-01-14 08:41:46 +03:00
|
|
|
func (p *peer) writeMessage(msg lnwire.Message) error {
|
|
|
|
// Simply exit if we're shutting down.
|
|
|
|
if atomic.LoadInt32(&p.disconnect) != 0 {
|
2019-04-27 03:31:27 +03:00
|
|
|
return lnpeer.ErrPeerExiting
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2019-04-23 02:05:26 +03:00
|
|
|
// Only log the message on the first attempt.
|
|
|
|
if msg != nil {
|
|
|
|
p.logWireMessage(msg, false)
|
|
|
|
}
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2019-04-23 02:05:26 +03:00
|
|
|
noiseConn, ok := p.conn.(*brontide.Conn)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("brontide.Conn required to write messages")
|
|
|
|
}
|
2016-01-14 08:41:46 +03:00
|
|
|
|
2019-04-23 02:05:26 +03:00
|
|
|
flushMsg := func() error {
|
2019-02-22 07:10:51 +03:00
|
|
|
// Ensure the write deadline is set before we attempt to send
|
|
|
|
// the message.
|
|
|
|
writeDeadline := time.Now().Add(writeMessageTimeout)
|
2019-04-23 02:05:26 +03:00
|
|
|
err := noiseConn.SetWriteDeadline(writeDeadline)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the pending message to the wire. If an error is
|
|
|
|
// encountered, e.g. write timeout, the number of bytes written
|
|
|
|
// so far will be returned.
|
|
|
|
n, err := noiseConn.Flush()
|
|
|
|
|
|
|
|
// Record the number of bytes written on the wire, if any.
|
|
|
|
if n > 0 {
|
|
|
|
atomic.AddUint64(&p.bytesSent, uint64(n))
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current message has already been serialized, encrypted, and
|
|
|
|
// buffered on the underlying connection we will skip straight to
|
|
|
|
// flushing it to the wire.
|
|
|
|
if msg == nil {
|
|
|
|
return flushMsg()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, this is a new message. We'll acquire a write buffer to
|
|
|
|
// serialize the message and buffer the ciphertext on the connection.
|
|
|
|
err := p.writePool.Submit(func(buf *bytes.Buffer) error {
|
|
|
|
// Using a buffer allocated by the write pool, encode the
|
|
|
|
// message directly into the buffer.
|
|
|
|
_, writeErr := lnwire.WriteMessage(buf, msg, 0)
|
2019-02-22 07:10:51 +03:00
|
|
|
if writeErr != nil {
|
|
|
|
return writeErr
|
|
|
|
}
|
2017-10-16 01:19:45 +03:00
|
|
|
|
2019-04-23 02:05:26 +03:00
|
|
|
// Finally, write the message itself in a single swoop. This
|
|
|
|
// will buffer the ciphertext on the underlying connection. We
|
|
|
|
// will defer flushing the message until the write pool has been
|
|
|
|
// released.
|
|
|
|
return noiseConn.WriteMessage(buf.Bytes())
|
2019-02-22 07:10:51 +03:00
|
|
|
})
|
2019-04-23 02:05:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-22 07:10:51 +03:00
|
|
|
}
|
2019-01-11 06:49:44 +03:00
|
|
|
|
2019-04-23 02:05:26 +03:00
|
|
|
return flushMsg()
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// writeHandler is a goroutine dedicated to reading messages off of an incoming
|
|
|
|
// queue, and writing them out to the wire. This goroutine coordinates with the
|
2017-10-16 01:19:45 +03:00
|
|
|
// queueHandler in order to ensure the incoming message queue is quickly
|
|
|
|
// drained.
|
2016-06-21 22:32:32 +03:00
|
|
|
//
|
|
|
|
// NOTE: This method MUST be run as a goroutine.
|
|
|
|
func (p *peer) writeHandler() {
|
2019-03-27 02:40:41 +03:00
|
|
|
// We'll stop the timer after a new messages is sent, and also reset it
|
|
|
|
// after we process the next message.
|
|
|
|
idleTimer := time.AfterFunc(idleTimeout, func() {
|
|
|
|
err := fmt.Errorf("Peer %s no write for %s -- disconnecting",
|
|
|
|
p, idleTimeout)
|
|
|
|
p.Disconnect(err)
|
|
|
|
})
|
|
|
|
|
2017-09-28 06:22:40 +03:00
|
|
|
var exitErr error
|
2018-02-23 01:28:31 +03:00
|
|
|
|
2017-09-28 06:22:40 +03:00
|
|
|
out:
|
2016-01-14 08:41:46 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case outMsg := <-p.sendQueue:
|
2017-02-02 04:01:33 +03:00
|
|
|
// If we're about to send a ping message, then log the
|
|
|
|
// exact time in which we send the message so we can
|
|
|
|
// use the delay as a rough estimate of latency to the
|
|
|
|
// remote peer.
|
2019-03-27 02:40:07 +03:00
|
|
|
if _, ok := outMsg.msg.(*lnwire.Ping); ok {
|
2017-01-26 05:20:55 +03:00
|
|
|
// TODO(roasbeef): do this before the write?
|
|
|
|
// possibly account for processing within func?
|
|
|
|
now := time.Now().UnixNano()
|
|
|
|
atomic.StoreInt64(&p.pingLastSend, now)
|
|
|
|
}
|
|
|
|
|
2019-03-27 02:40:07 +03:00
|
|
|
// Record the time at which we first attempt to send the
|
|
|
|
// message.
|
|
|
|
startTime := time.Now()
|
|
|
|
|
2019-04-23 02:05:41 +03:00
|
|
|
retry:
|
2019-03-27 02:40:07 +03:00
|
|
|
// Write out the message to the socket. If a timeout
|
|
|
|
// error is encountered, we will catch this and retry
|
|
|
|
// after backing off in case the remote peer is just
|
|
|
|
// slow to process messages from the wire.
|
2017-02-02 04:01:33 +03:00
|
|
|
err := p.writeMessage(outMsg.msg)
|
2019-03-27 02:40:07 +03:00
|
|
|
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
|
|
|
|
peerLog.Debugf("Write timeout detected for "+
|
2019-04-23 02:05:41 +03:00
|
|
|
"peer %s, first write for message "+
|
|
|
|
"attempted %v ago", p,
|
2019-03-27 02:40:07 +03:00
|
|
|
time.Since(startTime))
|
|
|
|
|
2019-04-23 02:05:26 +03:00
|
|
|
// If we received a timeout error, this implies
|
|
|
|
// that the message was buffered on the
|
|
|
|
// connection successfully and that a flush was
|
|
|
|
// attempted. We'll set the message to nil so
|
|
|
|
// that on a subsequent pass we only try to
|
|
|
|
// flush the buffered message, and forgo
|
|
|
|
// reserializing or reencrypting it.
|
|
|
|
outMsg.msg = nil
|
|
|
|
|
2019-04-23 02:05:41 +03:00
|
|
|
goto retry
|
2019-03-27 02:40:07 +03:00
|
|
|
}
|
|
|
|
|
2019-03-27 02:40:41 +03:00
|
|
|
// The write succeeded, reset the idle timer to prevent
|
|
|
|
// us from disconnecting the peer.
|
|
|
|
if !idleTimer.Stop() {
|
|
|
|
select {
|
|
|
|
case <-idleTimer.C:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
idleTimer.Reset(idleTimeout)
|
|
|
|
|
2019-03-27 02:40:07 +03:00
|
|
|
// If the peer requested a synchronous write, respond
|
|
|
|
// with the error.
|
2017-11-16 05:23:46 +03:00
|
|
|
if outMsg.errChan != nil {
|
|
|
|
outMsg.errChan <- err
|
2017-02-02 04:01:33 +03:00
|
|
|
}
|
2016-01-14 08:41:46 +03:00
|
|
|
|
2017-02-02 04:01:33 +03:00
|
|
|
if err != nil {
|
2018-09-01 00:54:35 +03:00
|
|
|
exitErr = fmt.Errorf("unable to write "+
|
|
|
|
"message: %v", err)
|
2017-09-28 06:22:40 +03:00
|
|
|
break out
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
2017-02-02 04:01:33 +03:00
|
|
|
|
|
|
|
case <-p.quit:
|
2019-04-27 03:31:27 +03:00
|
|
|
exitErr = lnpeer.ErrPeerExiting
|
2017-09-28 06:22:40 +03:00
|
|
|
break out
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
}
|
2017-09-28 06:22:40 +03:00
|
|
|
|
|
|
|
p.wg.Done()
|
|
|
|
|
|
|
|
p.Disconnect(exitErr)
|
|
|
|
|
|
|
|
peerLog.Tracef("writeHandler for peer %v done", p)
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// queueHandler is responsible for accepting messages from outside subsystems
|
2016-06-21 22:32:32 +03:00
|
|
|
// to be eventually sent out on the wire by the writeHandler.
|
|
|
|
//
|
|
|
|
// NOTE: This method MUST be run as a goroutine.
|
2016-01-14 08:41:46 +03:00
|
|
|
func (p *peer) queueHandler() {
|
2017-02-02 04:01:33 +03:00
|
|
|
defer p.wg.Done()
|
|
|
|
|
2019-03-06 04:08:05 +03:00
|
|
|
// priorityMsgs holds an in order list of messages deemed high-priority
|
|
|
|
// to be added to the sendQueue. This predominately includes messages
|
|
|
|
// from the funding manager and htlcswitch.
|
|
|
|
priorityMsgs := list.New()
|
|
|
|
|
|
|
|
// lazyMsgs holds an in order list of messages deemed low-priority to be
|
|
|
|
// added to the sendQueue only after all high-priority messages have
|
|
|
|
// been queued. This predominately includes messages from the gossiper.
|
|
|
|
lazyMsgs := list.New()
|
2017-02-02 04:01:33 +03:00
|
|
|
|
2017-11-16 04:56:33 +03:00
|
|
|
for {
|
2019-03-06 04:08:05 +03:00
|
|
|
// Examine the front of the priority queue, if it is empty check
|
|
|
|
// the low priority queue.
|
|
|
|
elem := priorityMsgs.Front()
|
|
|
|
if elem == nil {
|
|
|
|
elem = lazyMsgs.Front()
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:56:33 +03:00
|
|
|
if elem != nil {
|
2019-03-06 04:08:05 +03:00
|
|
|
front := elem.Value.(outgoingMsg)
|
|
|
|
|
2017-11-16 04:56:33 +03:00
|
|
|
// There's an element on the queue, try adding
|
|
|
|
// it to the sendQueue. We also watch for
|
|
|
|
// messages on the outgoingQueue, in case the
|
|
|
|
// writeHandler cannot accept messages on the
|
|
|
|
// sendQueue.
|
2017-02-02 04:01:33 +03:00
|
|
|
select {
|
2019-03-06 04:08:05 +03:00
|
|
|
case p.sendQueue <- front:
|
|
|
|
if front.priority {
|
|
|
|
priorityMsgs.Remove(elem)
|
|
|
|
} else {
|
|
|
|
lazyMsgs.Remove(elem)
|
|
|
|
}
|
2017-11-16 04:56:33 +03:00
|
|
|
case msg := <-p.outgoingQueue:
|
2019-03-06 04:08:05 +03:00
|
|
|
if msg.priority {
|
|
|
|
priorityMsgs.PushBack(msg)
|
|
|
|
} else {
|
|
|
|
lazyMsgs.PushBack(msg)
|
|
|
|
}
|
2017-11-16 04:56:33 +03:00
|
|
|
case <-p.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If there weren't any messages to send to the
|
|
|
|
// writeHandler, then we'll accept a new message
|
|
|
|
// into the queue from outside sub-systems.
|
|
|
|
select {
|
|
|
|
case msg := <-p.outgoingQueue:
|
2019-03-06 04:08:05 +03:00
|
|
|
if msg.priority {
|
|
|
|
priorityMsgs.PushBack(msg)
|
|
|
|
} else {
|
|
|
|
lazyMsgs.PushBack(msg)
|
|
|
|
}
|
2017-02-02 04:01:33 +03:00
|
|
|
case <-p.quit:
|
|
|
|
return
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
2017-02-02 04:01:33 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-21 00:16:38 +03:00
|
|
|
}
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2016-11-11 04:15:25 +03:00
|
|
|
// pingHandler is responsible for periodically sending ping messages to the
|
|
|
|
// remote peer in order to keep the connection alive and/or determine if the
|
|
|
|
// connection is still active.
|
|
|
|
//
|
|
|
|
// NOTE: This method MUST be run as a goroutine.
|
|
|
|
func (p *peer) pingHandler() {
|
2017-08-09 02:51:19 +03:00
|
|
|
defer p.wg.Done()
|
|
|
|
|
2016-11-11 04:15:25 +03:00
|
|
|
pingTicker := time.NewTicker(pingInterval)
|
|
|
|
defer pingTicker.Stop()
|
|
|
|
|
2017-04-17 04:11:39 +03:00
|
|
|
// TODO(roasbeef): make dynamic in order to create fake cover traffic
|
|
|
|
const numPingBytes = 16
|
2016-11-11 04:15:25 +03:00
|
|
|
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pingTicker.C:
|
2017-04-17 04:11:39 +03:00
|
|
|
p.queueMsg(lnwire.NewPing(numPingBytes), nil)
|
2016-11-11 04:15:25 +03:00
|
|
|
case <-p.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 05:20:55 +03:00
|
|
|
// PingTime returns the estimated ping time to the peer in microseconds.
|
|
|
|
func (p *peer) PingTime() int64 {
|
|
|
|
return atomic.LoadInt64(&p.pingTime)
|
|
|
|
}
|
|
|
|
|
2019-03-06 04:08:05 +03:00
|
|
|
// queueMsg adds the lnwire.Message to the back of the high priority send queue.
|
|
|
|
// If the errChan is non-nil, an error is sent back if the msg failed to queue
|
|
|
|
// or failed to write, and nil otherwise.
|
2017-11-16 05:23:46 +03:00
|
|
|
func (p *peer) queueMsg(msg lnwire.Message, errChan chan error) {
|
2019-03-06 04:08:05 +03:00
|
|
|
p.queue(true, msg, errChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
// queueMsgLazy adds the lnwire.Message to the back of the low priority send
|
|
|
|
// queue. If the errChan is non-nil, an error is sent back if the msg failed to
|
|
|
|
// queue or failed to write, and nil otherwise.
|
|
|
|
func (p *peer) queueMsgLazy(msg lnwire.Message, errChan chan error) {
|
|
|
|
p.queue(false, msg, errChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
// queue sends a given message to the queueHandler using the passed priority. If
|
|
|
|
// the errChan is non-nil, an error is sent back if the msg failed to queue or
|
|
|
|
// failed to write, and nil otherwise.
|
|
|
|
func (p *peer) queue(priority bool, msg lnwire.Message, errChan chan error) {
|
2016-12-20 04:00:18 +03:00
|
|
|
select {
|
2019-03-06 04:08:05 +03:00
|
|
|
case p.outgoingQueue <- outgoingMsg{priority, msg, errChan}:
|
2016-12-20 04:00:18 +03:00
|
|
|
case <-p.quit:
|
2018-01-29 01:55:24 +03:00
|
|
|
peerLog.Tracef("Peer shutting down, could not enqueue msg.")
|
2017-11-16 05:23:46 +03:00
|
|
|
if errChan != nil {
|
2019-04-27 03:31:27 +03:00
|
|
|
errChan <- lnpeer.ErrPeerExiting
|
2017-11-16 05:23:46 +03:00
|
|
|
}
|
2016-12-20 04:00:18 +03:00
|
|
|
}
|
2016-06-21 22:32:32 +03:00
|
|
|
}
|
|
|
|
|
2016-11-11 04:15:25 +03:00
|
|
|
// ChannelSnapshots returns a slice of channel snapshots detailing all
|
|
|
|
// currently active channels maintained with the remote peer.
|
2016-06-23 08:22:06 +03:00
|
|
|
func (p *peer) ChannelSnapshots() []*channeldb.ChannelSnapshot {
|
2017-08-09 02:51:19 +03:00
|
|
|
p.activeChanMtx.RLock()
|
|
|
|
defer p.activeChanMtx.RUnlock()
|
|
|
|
|
|
|
|
snapshots := make([]*channeldb.ChannelSnapshot, 0, len(p.activeChannels))
|
|
|
|
for _, activeChan := range p.activeChannels {
|
2018-02-09 06:40:48 +03:00
|
|
|
// We'll only return a snapshot for channels that are
|
|
|
|
// *immedately* available for routing payments over.
|
|
|
|
if activeChan.RemoteNextRevocation() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-08-09 02:51:19 +03:00
|
|
|
snapshot := activeChan.StateSnapshot()
|
|
|
|
snapshots = append(snapshots, snapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
return snapshots
|
2016-06-23 08:22:06 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// genDeliveryScript returns a new script to be used to send our funds to in
|
|
|
|
// the case of a cooperative channel close negotiation.
|
|
|
|
func (p *peer) genDeliveryScript() ([]byte, error) {
|
|
|
|
deliveryAddr, err := p.server.cc.wallet.NewAddress(
|
|
|
|
lnwallet.WitnessPubKey, false,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
peerLog.Infof("Delivery addr for channel close: %v",
|
|
|
|
deliveryAddr)
|
|
|
|
|
|
|
|
return txscript.PayToAddrScript(deliveryAddr)
|
2017-07-31 00:21:21 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// channelManager is goroutine dedicated to handling all requests/signals
|
|
|
|
// pertaining to the opening, cooperative closing, and force closing of all
|
|
|
|
// channels maintained with the remote peer.
|
|
|
|
//
|
|
|
|
// NOTE: This method MUST be run as a goroutine.
|
|
|
|
func (p *peer) channelManager() {
|
2017-08-09 02:51:19 +03:00
|
|
|
defer p.wg.Done()
|
|
|
|
|
2019-02-15 04:13:44 +03:00
|
|
|
// reenableTimeout will fire once after the configured channel status
|
|
|
|
// interval has elapsed. This will trigger us to sign new channel
|
|
|
|
// updates and broadcast them with the "disabled" flag unset.
|
|
|
|
reenableTimeout := time.After(p.chanActiveTimeout)
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
2017-07-31 00:21:21 +03:00
|
|
|
// A new channel has arrived which means we've just completed a
|
|
|
|
// funding workflow. We'll initialize the necessary local
|
|
|
|
// state, and notify the htlc switch of a new link.
|
2017-01-24 02:33:46 +03:00
|
|
|
case newChanReq := <-p.newChannels:
|
2017-07-31 00:21:21 +03:00
|
|
|
newChan := newChanReq.channel
|
2018-09-26 12:12:57 +03:00
|
|
|
chanPoint := &newChan.FundingOutpoint
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
|
2016-11-18 05:43:33 +03:00
|
|
|
|
2017-10-02 14:11:26 +03:00
|
|
|
// Make sure this channel is not already active.
|
|
|
|
p.activeChanMtx.Lock()
|
peer: properly process retransmitted FundingLocked message we've never processed
In this commit, we modify the logic within the channelManager to be
able to process any retransmitted FundingLocked messages. Before this
commit, we would simply ignore any new channels sent to us, iff, we
already had an active channel with the same channel point. With the
recent change to the loadActiveChannels method in the peer, this is now
incorrect.
When a peer retransmits the FundingLocked message, it goes through to
the fundingManager. The fundingMgr will then (if we haven’t already
processed it), send the channel to the breach arbiter and also to the
peer’s channelManager. In order to handle this case properly, if we
already have the channel, we’ll check if our current channel *doesn’t*
already have the RemoteNextRevocation field set. If it doesn’t, then
this means that we haven’t yet processed the FundingLcoked message, so
we’ll process it for the first time.
This new logic will properly:
* ensure that the breachArbiter still has the most up to date channel
* allow us to update the state of the link has been added to the
switch at this point
* this link will now be eligible for forwarding after this
sequence
2017-12-06 05:00:33 +03:00
|
|
|
if currentChan, ok := p.activeChannels[chanID]; ok {
|
2017-11-23 10:21:07 +03:00
|
|
|
peerLog.Infof("Already have ChannelPoint(%v), "+
|
|
|
|
"ignoring.", chanPoint)
|
peer: properly process retransmitted FundingLocked message we've never processed
In this commit, we modify the logic within the channelManager to be
able to process any retransmitted FundingLocked messages. Before this
commit, we would simply ignore any new channels sent to us, iff, we
already had an active channel with the same channel point. With the
recent change to the loadActiveChannels method in the peer, this is now
incorrect.
When a peer retransmits the FundingLocked message, it goes through to
the fundingManager. The fundingMgr will then (if we haven’t already
processed it), send the channel to the breach arbiter and also to the
peer’s channelManager. In order to handle this case properly, if we
already have the channel, we’ll check if our current channel *doesn’t*
already have the RemoteNextRevocation field set. If it doesn’t, then
this means that we haven’t yet processed the FundingLcoked message, so
we’ll process it for the first time.
This new logic will properly:
* ensure that the breachArbiter still has the most up to date channel
* allow us to update the state of the link has been added to the
switch at this point
* this link will now be eligible for forwarding after this
sequence
2017-12-06 05:00:33 +03:00
|
|
|
|
2017-10-02 14:11:26 +03:00
|
|
|
p.activeChanMtx.Unlock()
|
2018-09-26 12:12:57 +03:00
|
|
|
close(newChanReq.err)
|
peer: properly process retransmitted FundingLocked message we've never processed
In this commit, we modify the logic within the channelManager to be
able to process any retransmitted FundingLocked messages. Before this
commit, we would simply ignore any new channels sent to us, iff, we
already had an active channel with the same channel point. With the
recent change to the loadActiveChannels method in the peer, this is now
incorrect.
When a peer retransmits the FundingLocked message, it goes through to
the fundingManager. The fundingMgr will then (if we haven’t already
processed it), send the channel to the breach arbiter and also to the
peer’s channelManager. In order to handle this case properly, if we
already have the channel, we’ll check if our current channel *doesn’t*
already have the RemoteNextRevocation field set. If it doesn’t, then
this means that we haven’t yet processed the FundingLcoked message, so
we’ll process it for the first time.
This new logic will properly:
* ensure that the breachArbiter still has the most up to date channel
* allow us to update the state of the link has been added to the
switch at this point
* this link will now be eligible for forwarding after this
sequence
2017-12-06 05:00:33 +03:00
|
|
|
|
|
|
|
// If we're being sent a new channel, and our
|
|
|
|
// existing channel doesn't have the next
|
|
|
|
// revocation, then we need to update the
|
2017-12-18 05:40:05 +03:00
|
|
|
// current existing channel.
|
peer: properly process retransmitted FundingLocked message we've never processed
In this commit, we modify the logic within the channelManager to be
able to process any retransmitted FundingLocked messages. Before this
commit, we would simply ignore any new channels sent to us, iff, we
already had an active channel with the same channel point. With the
recent change to the loadActiveChannels method in the peer, this is now
incorrect.
When a peer retransmits the FundingLocked message, it goes through to
the fundingManager. The fundingMgr will then (if we haven’t already
processed it), send the channel to the breach arbiter and also to the
peer’s channelManager. In order to handle this case properly, if we
already have the channel, we’ll check if our current channel *doesn’t*
already have the RemoteNextRevocation field set. If it doesn’t, then
this means that we haven’t yet processed the FundingLcoked message, so
we’ll process it for the first time.
This new logic will properly:
* ensure that the breachArbiter still has the most up to date channel
* allow us to update the state of the link has been added to the
switch at this point
* this link will now be eligible for forwarding after this
sequence
2017-12-06 05:00:33 +03:00
|
|
|
if currentChan.RemoteNextRevocation() != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
peerLog.Infof("Processing retransmitted "+
|
|
|
|
"FundingLocked for ChannelPoint(%v)",
|
|
|
|
chanPoint)
|
|
|
|
|
2018-09-26 12:12:57 +03:00
|
|
|
nextRevoke := newChan.RemoteNextRevocation
|
peer: properly process retransmitted FundingLocked message we've never processed
In this commit, we modify the logic within the channelManager to be
able to process any retransmitted FundingLocked messages. Before this
commit, we would simply ignore any new channels sent to us, iff, we
already had an active channel with the same channel point. With the
recent change to the loadActiveChannels method in the peer, this is now
incorrect.
When a peer retransmits the FundingLocked message, it goes through to
the fundingManager. The fundingMgr will then (if we haven’t already
processed it), send the channel to the breach arbiter and also to the
peer’s channelManager. In order to handle this case properly, if we
already have the channel, we’ll check if our current channel *doesn’t*
already have the RemoteNextRevocation field set. If it doesn’t, then
this means that we haven’t yet processed the FundingLcoked message, so
we’ll process it for the first time.
This new logic will properly:
* ensure that the breachArbiter still has the most up to date channel
* allow us to update the state of the link has been added to the
switch at this point
* this link will now be eligible for forwarding after this
sequence
2017-12-06 05:00:33 +03:00
|
|
|
err := currentChan.InitNextRevocation(nextRevoke)
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Errorf("unable to init chan "+
|
|
|
|
"revocation: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-10-02 14:11:26 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// If not already active, we'll add this channel to the
|
|
|
|
// set of active channels, so we can look it up later
|
|
|
|
// easily according to its channel ID.
|
2018-09-26 12:12:57 +03:00
|
|
|
lnChan, err := lnwallet.NewLightningChannel(
|
2019-04-15 15:24:43 +03:00
|
|
|
p.server.cc.signer, newChan, p.server.sigPool,
|
2018-09-26 12:12:57 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
p.activeChanMtx.Unlock()
|
|
|
|
err := fmt.Errorf("unable to create "+
|
|
|
|
"LightningChannel: %v", err)
|
|
|
|
peerLog.Errorf(err.Error())
|
|
|
|
|
|
|
|
newChanReq.err <- err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
p.activeChannels[chanID] = lnChan
|
2019-02-15 04:13:44 +03:00
|
|
|
p.addedChannels[chanID] = struct{}{}
|
2016-11-18 05:43:33 +03:00
|
|
|
p.activeChanMtx.Unlock()
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
peerLog.Infof("New channel active ChannelPoint(%v) "+
|
2018-02-20 02:01:23 +03:00
|
|
|
"with NodeKey(%x)", chanPoint, p.PubKey())
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2017-07-31 00:21:21 +03:00
|
|
|
// Next, we'll assemble a ChannelLink along with the
|
|
|
|
// necessary items it needs to function.
|
2017-08-03 07:15:49 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): panic on below?
|
|
|
|
_, currentHeight, err := p.server.cc.chainIO.GetBestBlock()
|
|
|
|
if err != nil {
|
2018-09-26 12:12:57 +03:00
|
|
|
err := fmt.Errorf("unable to get best "+
|
|
|
|
"block: %v", err)
|
|
|
|
peerLog.Errorf(err.Error())
|
|
|
|
|
|
|
|
newChanReq.err <- err
|
2017-08-03 07:15:49 +03:00
|
|
|
continue
|
|
|
|
}
|
2018-01-21 07:25:54 +03:00
|
|
|
chainEvents, err := p.server.chainArb.SubscribeChannelEvents(
|
2018-04-18 14:57:02 +03:00
|
|
|
*chanPoint,
|
2018-01-21 07:25:54 +03:00
|
|
|
)
|
2018-01-19 01:03:13 +03:00
|
|
|
if err != nil {
|
2018-09-26 12:12:57 +03:00
|
|
|
err := fmt.Errorf("unable to subscribe to "+
|
|
|
|
"chain events: %v", err)
|
|
|
|
peerLog.Errorf(err.Error())
|
|
|
|
|
|
|
|
newChanReq.err <- err
|
2018-01-19 01:03:13 +03:00
|
|
|
continue
|
|
|
|
}
|
2017-05-02 23:04:58 +03:00
|
|
|
|
2018-12-08 05:39:34 +03:00
|
|
|
// We'll query the localChanCfg of the new channel to determine the
|
|
|
|
// minimum HTLC value that can be forwarded. For the maximum HTLC
|
|
|
|
// value that can be forwarded and fees we'll use the default
|
|
|
|
// values, as they currently are always set to the default values
|
|
|
|
// at initial channel creation. Note that the maximum HTLC value
|
|
|
|
// defaults to the cap on the total value of outstanding HTLCs.
|
2018-09-26 12:12:57 +03:00
|
|
|
fwdMinHtlc := lnChan.FwdMinHtlc()
|
2018-08-22 10:32:43 +03:00
|
|
|
defaultPolicy := p.server.cc.routingPolicy
|
|
|
|
forwardingPolicy := &htlcswitch.ForwardingPolicy{
|
|
|
|
MinHTLC: fwdMinHtlc,
|
2019-02-25 18:51:07 +03:00
|
|
|
MaxHTLC: newChan.LocalChanCfg.MaxPendingAmount,
|
2018-08-22 10:32:43 +03:00
|
|
|
BaseFee: defaultPolicy.BaseFee,
|
|
|
|
FeeRate: defaultPolicy.FeeRate,
|
|
|
|
TimeLockDelta: defaultPolicy.TimeLockDelta,
|
|
|
|
}
|
|
|
|
|
2018-05-23 14:33:41 +03:00
|
|
|
// Create the link and add it to the switch.
|
2018-06-12 05:05:00 +03:00
|
|
|
err = p.addLink(
|
2018-09-26 12:12:57 +03:00
|
|
|
chanPoint, lnChan, forwardingPolicy,
|
2018-06-12 05:05:00 +03:00
|
|
|
chainEvents, currentHeight, false,
|
|
|
|
)
|
2018-05-23 14:33:41 +03:00
|
|
|
if err != nil {
|
2018-09-26 12:12:57 +03:00
|
|
|
err := fmt.Errorf("can't register new channel "+
|
2018-07-13 01:59:00 +03:00
|
|
|
"link(%v) with NodeKey(%x)", chanPoint,
|
|
|
|
p.PubKey())
|
2018-09-26 12:12:57 +03:00
|
|
|
peerLog.Errorf(err.Error())
|
|
|
|
|
|
|
|
newChanReq.err <- err
|
|
|
|
continue
|
2017-05-02 23:04:58 +03:00
|
|
|
}
|
2017-01-24 02:33:46 +03:00
|
|
|
|
2018-09-26 12:12:57 +03:00
|
|
|
close(newChanReq.err)
|
2016-07-14 02:40:01 +03:00
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// We've just received a local request to close an active
|
|
|
|
// channel. If will either kick of a cooperative channel
|
|
|
|
// closure negotiation, or be a notification of a breached
|
|
|
|
// contract that should be abandoned.
|
2016-06-21 22:32:32 +03:00
|
|
|
case req := <-p.localCloseChanReqs:
|
2017-11-23 10:21:07 +03:00
|
|
|
p.handleLocalCloseReq(req)
|
|
|
|
|
2018-08-31 02:54:53 +03:00
|
|
|
// We've received a link failure from a link that was added to
|
|
|
|
// the switch. This will initiate the teardown of the link, and
|
|
|
|
// initiate any on-chain closures if necessary.
|
|
|
|
case failure := <-p.linkFailures:
|
|
|
|
p.handleLinkFailure(failure)
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// We've received a new cooperative channel closure related
|
|
|
|
// message from the remote peer, we'll use this message to
|
|
|
|
// advance the chan closer state machine.
|
|
|
|
case closeMsg := <-p.chanCloseMsgs:
|
|
|
|
// We'll now fetch the matching closing state machine
|
|
|
|
// in order to continue, or finalize the channel
|
|
|
|
// closure process.
|
|
|
|
chanCloser, err := p.fetchActiveChanCloser(closeMsg.cid)
|
|
|
|
if err != nil {
|
2018-05-23 10:09:19 +03:00
|
|
|
// If the channel is not known to us, we'll
|
|
|
|
// simply ignore this message.
|
|
|
|
if err == ErrChannelNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
peerLog.Errorf("Unable to respond to remote "+
|
2017-11-23 10:21:07 +03:00
|
|
|
"close msg: %v", err)
|
2018-03-30 23:04:59 +03:00
|
|
|
|
|
|
|
errMsg := &lnwire.Error{
|
|
|
|
ChanID: closeMsg.cid,
|
|
|
|
Data: lnwire.ErrorData(err.Error()),
|
|
|
|
}
|
|
|
|
p.queueMsg(errMsg, nil)
|
2017-11-23 10:21:07 +03:00
|
|
|
continue
|
|
|
|
}
|
2017-07-31 00:21:21 +03:00
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// Next, we'll process the next message using the
|
|
|
|
// target state machine. We'll either continue
|
|
|
|
// negotiation, or halt.
|
|
|
|
msgs, closeFin, err := chanCloser.ProcessCloseMsg(
|
|
|
|
closeMsg.msg,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("unable to process close "+
|
|
|
|
"msg: %v", err)
|
|
|
|
peerLog.Error(err)
|
2017-07-31 00:21:21 +03:00
|
|
|
|
2017-11-23 22:49:48 +03:00
|
|
|
// As the negotiations failed, we'll reset the
|
|
|
|
// channel state to ensure we act to on-chain
|
|
|
|
// events as normal.
|
|
|
|
chanCloser.cfg.channel.ResetState()
|
2017-07-31 00:21:21 +03:00
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
if chanCloser.CloseRequest() != nil {
|
|
|
|
chanCloser.CloseRequest().Err <- err
|
2017-07-31 00:21:21 +03:00
|
|
|
}
|
2017-11-23 10:21:07 +03:00
|
|
|
delete(p.activeChanCloses, closeMsg.cid)
|
2017-07-31 00:21:21 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// Queue any messages to the remote peer that need to
|
|
|
|
// be sent as a part of this latest round of
|
|
|
|
// negotiations.
|
|
|
|
for _, msg := range msgs {
|
|
|
|
p.queueMsg(msg, nil)
|
2017-03-25 04:26:09 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// If we haven't finished close negotiations, then
|
|
|
|
// we'll continue as we can't yet finalize the closure.
|
|
|
|
if !closeFin {
|
2017-05-24 01:26:38 +03:00
|
|
|
continue
|
2017-03-25 04:26:09 +03:00
|
|
|
}
|
2016-07-14 02:40:01 +03:00
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// Otherwise, we've agreed on a closing fee! In this
|
|
|
|
// case, we'll wrap up the channel closure by notifying
|
|
|
|
// relevant sub-systems and launching a goroutine to
|
|
|
|
// wait for close tx conf.
|
|
|
|
p.finalizeChanClosure(chanCloser)
|
2019-02-15 04:13:44 +03:00
|
|
|
|
|
|
|
// The channel reannounce delay has elapsed, broadcast the
|
|
|
|
// reenabled channel updates to the network. This should only
|
|
|
|
// fire once, so we set the reenableTimeout channel to nil to
|
|
|
|
// mark it for garbage collection. If the peer is torn down
|
|
|
|
// before firing, reenabling will not be attempted.
|
|
|
|
// TODO(conner): consolidate reenables timers inside chan status
|
|
|
|
// manager
|
|
|
|
case <-reenableTimeout:
|
|
|
|
p.reenableActiveChannels()
|
|
|
|
|
|
|
|
// Since this channel will never fire again during the
|
|
|
|
// lifecycle of the peer, we nil the channel to mark it
|
|
|
|
// eligible for garbage collection, and make this
|
2019-05-05 01:35:37 +03:00
|
|
|
// explicitly ineligible to receive in future calls to
|
2019-02-15 04:13:44 +03:00
|
|
|
// select. This also shaves a few CPU cycles since the
|
|
|
|
// select will ignore this case entirely.
|
|
|
|
reenableTimeout = nil
|
|
|
|
|
2017-11-23 22:49:48 +03:00
|
|
|
case <-p.quit:
|
|
|
|
|
|
|
|
// As, we've been signalled to exit, we'll reset all
|
|
|
|
// our active channel back to their default state.
|
|
|
|
p.activeChanMtx.Lock()
|
|
|
|
for _, channel := range p.activeChannels {
|
|
|
|
channel.ResetState()
|
2017-07-14 22:05:55 +03:00
|
|
|
}
|
2017-11-23 22:49:48 +03:00
|
|
|
p.activeChanMtx.Unlock()
|
2017-05-24 01:26:38 +03:00
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 04:13:44 +03:00
|
|
|
// reenableActiveChannels searches the index of channels maintained with this
|
|
|
|
// peer, and reenables each public, non-pending channel. This is done at the
|
|
|
|
// gossip level by broadcasting a new ChannelUpdate with the disabled bit unset.
|
|
|
|
// No message will be sent if the channel is already enabled.
|
|
|
|
func (p *peer) reenableActiveChannels() {
|
|
|
|
// First, filter all known channels with this peer for ones that are
|
|
|
|
// both public and not pending.
|
|
|
|
var activePublicChans []wire.OutPoint
|
|
|
|
p.activeChanMtx.RLock()
|
|
|
|
for chanID, lnChan := range p.activeChannels {
|
|
|
|
dbChan := lnChan.State()
|
|
|
|
isPublic := dbChan.ChannelFlags&lnwire.FFAnnounceChannel != 0
|
|
|
|
if !isPublic || dbChan.IsPending {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll also skip any channels added during this peer's
|
|
|
|
// lifecycle since they haven't waited out the timeout. Their
|
|
|
|
// first announcement will be enabled, and the chan status
|
|
|
|
// manager will begin monitoring them passively since they exist
|
|
|
|
// in the database.
|
|
|
|
if _, ok := p.addedChannels[chanID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
activePublicChans = append(
|
|
|
|
activePublicChans, dbChan.FundingOutpoint,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
p.activeChanMtx.RUnlock()
|
|
|
|
|
|
|
|
// For each of the public, non-pending channels, set the channel
|
|
|
|
// disabled bit to false and send out a new ChannelUpdate. If this
|
|
|
|
// channel is already active, the update won't be sent.
|
|
|
|
for _, chanPoint := range activePublicChans {
|
|
|
|
err := p.server.chanStatusMgr.RequestEnable(chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
srvrLog.Errorf("Unable to enable channel %v: %v",
|
|
|
|
chanPoint, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// fetchActiveChanCloser attempts to fetch the active chan closer state machine
|
|
|
|
// for the target channel ID. If the channel isn't active an error is returned.
|
|
|
|
// Otherwise, either an existing state machine will be returned, or a new one
|
|
|
|
// will be created.
|
|
|
|
func (p *peer) fetchActiveChanCloser(chanID lnwire.ChannelID) (*channelCloser, error) {
|
|
|
|
// First, we'll ensure that we actually know of the target channel. If
|
|
|
|
// not, we'll ignore this message.
|
|
|
|
p.activeChanMtx.RLock()
|
|
|
|
channel, ok := p.activeChannels[chanID]
|
|
|
|
p.activeChanMtx.RUnlock()
|
|
|
|
if !ok {
|
2018-05-23 10:09:19 +03:00
|
|
|
return nil, ErrChannelNotFound
|
2017-11-23 10:21:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll attempt to look up the matching state machine, if we can't
|
|
|
|
// find one then this means that the remote party is initiating a
|
|
|
|
// cooperative channel closure.
|
|
|
|
chanCloser, ok := p.activeChanCloses[chanID]
|
|
|
|
if !ok {
|
2018-03-30 23:04:59 +03:00
|
|
|
// If we need to create a chan closer for the first time, then
|
|
|
|
// we'll check to ensure that the channel is even in the proper
|
|
|
|
// state to allow a co-op channel closure.
|
|
|
|
if len(channel.ActiveHtlcs()) != 0 {
|
|
|
|
return nil, fmt.Errorf("cannot co-op close " +
|
|
|
|
"channel w/ active htlcs")
|
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// We'll create a valid closing state machine in order to
|
|
|
|
// respond to the initiated cooperative channel closure.
|
|
|
|
deliveryAddr, err := p.genDeliveryScript()
|
|
|
|
if err != nil {
|
2018-03-30 23:04:59 +03:00
|
|
|
peerLog.Errorf("unable to gen delivery script: %v", err)
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("close addr unavailable")
|
2017-11-23 10:21:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// In order to begin fee negotiations, we'll first compute our
|
|
|
|
// target ideal fee-per-kw. We'll set this to a lax value, as
|
|
|
|
// we weren't the ones that initiated the channel closure.
|
2018-07-28 04:20:58 +03:00
|
|
|
feePerKw, err := p.server.cc.feeEstimator.EstimateFeePerKW(6)
|
2017-11-23 10:21:07 +03:00
|
|
|
if err != nil {
|
2018-03-30 23:04:59 +03:00
|
|
|
peerLog.Errorf("unable to query fee estimator: %v", err)
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unable to estimate fee")
|
2017-11-23 10:21:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, startingHeight, err := p.server.cc.chainIO.GetBestBlock()
|
|
|
|
if err != nil {
|
2018-03-30 23:04:59 +03:00
|
|
|
peerLog.Errorf("unable to obtain best block: %v", err)
|
|
|
|
return nil, fmt.Errorf("cannot obtain best block")
|
2017-11-23 10:21:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
chanCloser = newChannelCloser(
|
|
|
|
chanCloseCfg{
|
|
|
|
channel: channel,
|
|
|
|
unregisterChannel: p.server.htlcSwitch.RemoveLink,
|
|
|
|
broadcastTx: p.server.cc.wallet.PublishTransaction,
|
2019-02-15 04:13:44 +03:00
|
|
|
disableChannel: p.server.chanStatusMgr.RequestDisable,
|
|
|
|
quit: p.quit,
|
2017-11-23 10:21:07 +03:00
|
|
|
},
|
|
|
|
deliveryAddr,
|
2018-07-28 04:20:58 +03:00
|
|
|
feePerKw,
|
2017-11-23 10:21:07 +03:00
|
|
|
uint32(startingHeight),
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
p.activeChanCloses[chanID] = chanCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
return chanCloser, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleLocalCloseReq kicks-off the workflow to execute a cooperative or
|
|
|
|
// forced unilateral closure of the channel initiated by a local subsystem.
|
|
|
|
func (p *peer) handleLocalCloseReq(req *htlcswitch.ChanClose) {
|
2017-05-02 23:04:58 +03:00
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(req.ChanPoint)
|
2017-04-17 01:41:11 +03:00
|
|
|
|
2016-11-18 05:43:33 +03:00
|
|
|
p.activeChanMtx.RLock()
|
2017-05-24 01:21:35 +03:00
|
|
|
channel, ok := p.activeChannels[chanID]
|
2016-11-18 05:43:33 +03:00
|
|
|
p.activeChanMtx.RUnlock()
|
2017-05-24 01:21:35 +03:00
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("unable to close channel, ChannelID(%v) is "+
|
|
|
|
"unknown", chanID)
|
|
|
|
peerLog.Errorf(err.Error())
|
2017-05-02 23:04:58 +03:00
|
|
|
req.Err <- err
|
2017-05-24 01:21:35 +03:00
|
|
|
return
|
|
|
|
}
|
2016-09-12 22:42:26 +03:00
|
|
|
|
2016-11-29 05:44:14 +03:00
|
|
|
switch req.CloseType {
|
2017-07-31 00:21:21 +03:00
|
|
|
|
2016-11-29 05:44:14 +03:00
|
|
|
// A type of CloseRegular indicates that the user has opted to close
|
2017-05-05 02:03:47 +03:00
|
|
|
// out this channel on-chain, so we execute the cooperative channel
|
2017-02-03 04:05:25 +03:00
|
|
|
// closure workflow.
|
2017-05-02 23:04:58 +03:00
|
|
|
case htlcswitch.CloseRegular:
|
2017-11-23 10:21:07 +03:00
|
|
|
// First, we'll fetch a fresh delivery address that we'll use
|
|
|
|
// to send the funds to in the case of a successful
|
|
|
|
// negotiation.
|
|
|
|
deliveryAddr, err := p.genDeliveryScript()
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Errorf(err.Error())
|
|
|
|
req.Err <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll create a new channel closer state machine to
|
|
|
|
// handle the close negotiation.
|
2018-01-20 04:23:38 +03:00
|
|
|
_, startingHeight, err := p.server.cc.chainIO.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Errorf(err.Error())
|
|
|
|
req.Err <- err
|
|
|
|
return
|
|
|
|
}
|
2018-06-14 05:43:42 +03:00
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
chanCloser := newChannelCloser(
|
|
|
|
chanCloseCfg{
|
|
|
|
channel: channel,
|
|
|
|
unregisterChannel: p.server.htlcSwitch.RemoveLink,
|
|
|
|
broadcastTx: p.server.cc.wallet.PublishTransaction,
|
2019-02-15 04:13:44 +03:00
|
|
|
disableChannel: p.server.chanStatusMgr.RequestDisable,
|
|
|
|
quit: p.quit,
|
2017-11-23 10:21:07 +03:00
|
|
|
},
|
|
|
|
deliveryAddr,
|
|
|
|
req.TargetFeePerKw,
|
|
|
|
uint32(startingHeight),
|
|
|
|
req,
|
|
|
|
)
|
|
|
|
p.activeChanCloses[chanID] = chanCloser
|
|
|
|
|
|
|
|
// Finally, we'll initiate the channel shutdown within the
|
|
|
|
// chanCloser, and send the shutdown message to the remote
|
|
|
|
// party to kick things off.
|
|
|
|
shutdownMsg, err := chanCloser.ShutdownChan()
|
2017-03-25 04:26:09 +03:00
|
|
|
if err != nil {
|
2017-11-23 10:21:07 +03:00
|
|
|
peerLog.Errorf(err.Error())
|
2017-05-02 23:04:58 +03:00
|
|
|
req.Err <- err
|
2017-11-23 10:21:07 +03:00
|
|
|
delete(p.activeChanCloses, chanID)
|
|
|
|
|
2017-11-23 22:49:48 +03:00
|
|
|
// As we were unable to shutdown the channel, we'll
|
|
|
|
// return it back to its normal state.
|
|
|
|
channel.ResetState()
|
2017-03-25 04:26:09 +03:00
|
|
|
return
|
|
|
|
}
|
2016-11-29 05:44:14 +03:00
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
p.queueMsg(shutdownMsg, nil)
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// A type of CloseBreach indicates that the counterparty has breached
|
2017-02-03 04:05:25 +03:00
|
|
|
// the channel therefore we need to clean up our local state.
|
2017-05-02 23:04:58 +03:00
|
|
|
case htlcswitch.CloseBreach:
|
2017-07-31 00:21:21 +03:00
|
|
|
// TODO(roasbeef): no longer need with newer beach logic?
|
2016-11-29 05:44:14 +03:00
|
|
|
peerLog.Infof("ChannelPoint(%v) has been breached, wiping "+
|
2017-05-02 23:04:58 +03:00
|
|
|
"channel", req.ChanPoint)
|
2017-11-23 10:21:07 +03:00
|
|
|
if err := p.WipeChannel(req.ChanPoint); err != nil {
|
2016-11-29 05:44:14 +03:00
|
|
|
peerLog.Infof("Unable to wipe channel after detected "+
|
|
|
|
"breach: %v", err)
|
2017-05-02 23:04:58 +03:00
|
|
|
req.Err <- err
|
2016-11-29 05:44:14 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
2016-09-12 22:42:26 +03:00
|
|
|
}
|
2017-03-25 04:26:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 02:54:53 +03:00
|
|
|
// linkFailureReport is sent to the channelManager whenever a link that was
|
|
|
|
// added to the switch reports a link failure, and is forced to exit. The report
|
|
|
|
// houses the necessary information to cleanup the channel state, send back the
|
|
|
|
// error message, and force close if necessary.
|
|
|
|
type linkFailureReport struct {
|
|
|
|
chanPoint wire.OutPoint
|
|
|
|
chanID lnwire.ChannelID
|
|
|
|
shortChanID lnwire.ShortChannelID
|
|
|
|
linkErr htlcswitch.LinkFailureError
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleLinkFailure processes a link failure report when a link in the switch
|
|
|
|
// fails. It handles facilitates removal of all channel state within the peer,
|
|
|
|
// force closing the channel depending on severity, and sending the error
|
|
|
|
// message back to the remote party.
|
|
|
|
func (p *peer) handleLinkFailure(failure linkFailureReport) {
|
|
|
|
// We begin by wiping the link, which will remove it from the switch,
|
|
|
|
// such that it won't be attempted used for any more updates.
|
|
|
|
//
|
|
|
|
// TODO(halseth): should introduce a way to atomically stop/pause the
|
|
|
|
// link and cancel back any adds in its mailboxes such that we can
|
|
|
|
// safely force close without the link being added again and updates
|
|
|
|
// being applied.
|
|
|
|
if err := p.WipeChannel(&failure.chanPoint); err != nil {
|
|
|
|
peerLog.Errorf("Unable to wipe link for chanpoint=%v",
|
|
|
|
failure.chanPoint)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the error encountered was severe enough, we'll now force close the
|
|
|
|
// channel to prevent readding it to the switch in the future.
|
|
|
|
if failure.linkErr.ForceClose {
|
|
|
|
peerLog.Warnf("Force closing link(%v)",
|
|
|
|
failure.shortChanID)
|
|
|
|
|
|
|
|
closeTx, err := p.server.chainArb.ForceCloseContract(
|
|
|
|
failure.chanPoint,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Errorf("unable to force close "+
|
|
|
|
"link(%v): %v", failure.shortChanID, err)
|
|
|
|
} else {
|
|
|
|
peerLog.Infof("channel(%v) force "+
|
|
|
|
"closed with txid %v",
|
|
|
|
failure.shortChanID, closeTx.TxHash())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send an error to the peer, why we failed the channel.
|
|
|
|
if failure.linkErr.ShouldSendToPeer() {
|
|
|
|
// If SendData is set, send it to the peer. If not, we'll use
|
|
|
|
// the standard error messages in the payload. We only include
|
|
|
|
// sendData in the cases where the error data does not contain
|
|
|
|
// sensitive information.
|
|
|
|
data := []byte(failure.linkErr.Error())
|
|
|
|
if failure.linkErr.SendData != nil {
|
|
|
|
data = failure.linkErr.SendData
|
|
|
|
}
|
|
|
|
err := p.SendMessage(true, &lnwire.Error{
|
|
|
|
ChanID: failure.chanID,
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
peerLog.Errorf("unable to send msg to "+
|
|
|
|
"remote peer: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// finalizeChanClosure performs the final clean up steps once the cooperative
|
|
|
|
// closure transaction has been fully broadcast. The finalized closing state
|
2017-12-18 05:40:05 +03:00
|
|
|
// machine should be passed in. Once the transaction has been sufficiently
|
|
|
|
// confirmed, the channel will be marked as fully closed within the database,
|
2017-11-23 10:21:07 +03:00
|
|
|
// and any clients will be notified of updates to the closing state.
|
|
|
|
func (p *peer) finalizeChanClosure(chanCloser *channelCloser) {
|
|
|
|
closeReq := chanCloser.CloseRequest()
|
|
|
|
|
|
|
|
// First, we'll clear all indexes related to the channel in question.
|
|
|
|
chanPoint := chanCloser.cfg.channel.ChannelPoint()
|
|
|
|
if err := p.WipeChannel(chanPoint); err != nil {
|
|
|
|
if closeReq != nil {
|
|
|
|
closeReq.Err <- err
|
2017-07-14 22:05:55 +03:00
|
|
|
}
|
2017-08-18 22:16:20 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// Next, we'll launch a goroutine which will request to be notified by
|
2018-01-17 07:25:34 +03:00
|
|
|
// the ChainNotifier once the closure transaction obtains a single
|
|
|
|
// confirmation.
|
2017-11-23 10:21:07 +03:00
|
|
|
notifier := p.server.cc.chainNotifier
|
2017-10-18 05:25:13 +03:00
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// If any error happens during waitForChanToClose, forward it to
|
|
|
|
// closeReq. If this channel closure is not locally initiated, closeReq
|
|
|
|
// will be nil, so just ignore the error.
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
if closeReq != nil {
|
|
|
|
errChan = closeReq.Err
|
2016-09-12 22:42:26 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
closingTx, err := chanCloser.ClosingTx()
|
|
|
|
if err != nil {
|
|
|
|
if closeReq != nil {
|
|
|
|
peerLog.Error(err)
|
|
|
|
closeReq.Err <- err
|
2017-07-14 22:05:55 +03:00
|
|
|
}
|
2017-05-05 02:03:47 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
closingTxid := closingTx.TxHash()
|
2017-05-05 02:03:47 +03:00
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
// If this is a locally requested shutdown, update the caller with a
|
|
|
|
// new event detailing the current pending state of this request.
|
|
|
|
if closeReq != nil {
|
2018-12-20 20:52:27 +03:00
|
|
|
closeReq.Updates <- &pendingUpdate{
|
|
|
|
Txid: closingTxid[:],
|
2017-07-14 22:05:55 +03:00
|
|
|
}
|
2016-08-31 02:52:53 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 10:21:07 +03:00
|
|
|
go waitForChanToClose(chanCloser.negotiationHeight, notifier, errChan,
|
2018-05-31 08:18:44 +03:00
|
|
|
chanPoint, &closingTxid, closingTx.TxOut[0].PkScript, func() {
|
2018-09-06 04:22:29 +03:00
|
|
|
|
2017-05-11 03:27:05 +03:00
|
|
|
// Respond to the local subsystem which requested the
|
|
|
|
// channel closure.
|
2017-11-23 10:21:07 +03:00
|
|
|
if closeReq != nil {
|
2018-12-20 20:52:27 +03:00
|
|
|
closeReq.Updates <- &channelCloseUpdate{
|
|
|
|
ClosingTxid: closingTxid[:],
|
|
|
|
Success: true,
|
2017-07-14 22:05:55 +03:00
|
|
|
}
|
2017-05-11 03:27:05 +03:00
|
|
|
}
|
|
|
|
})
|
2017-05-05 02:03:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// waitForChanToClose uses the passed notifier to wait until the channel has
|
|
|
|
// been detected as closed on chain and then concludes by executing the
|
|
|
|
// following actions: the channel point will be sent over the settleChan, and
|
|
|
|
// finally the callback will be executed. If any error is encountered within
|
|
|
|
// the function, then it will be sent over the errChan.
|
2017-05-11 03:27:05 +03:00
|
|
|
func waitForChanToClose(bestHeight uint32, notifier chainntnfs.ChainNotifier,
|
2017-05-05 02:03:47 +03:00
|
|
|
errChan chan error, chanPoint *wire.OutPoint,
|
2018-05-31 08:18:44 +03:00
|
|
|
closingTxID *chainhash.Hash, closeScript []byte, cb func()) {
|
2017-05-05 02:03:47 +03:00
|
|
|
|
2017-03-25 04:26:09 +03:00
|
|
|
peerLog.Infof("Waiting for confirmation of cooperative close of "+
|
|
|
|
"ChannelPoint(%v) with txid: %v", chanPoint,
|
|
|
|
closingTxID)
|
|
|
|
|
2017-05-05 02:03:47 +03:00
|
|
|
// TODO(roasbeef): add param for num needed confs
|
2018-05-31 08:18:44 +03:00
|
|
|
confNtfn, err := notifier.RegisterConfirmationsNtfn(
|
|
|
|
closingTxID, closeScript, 1, bestHeight,
|
|
|
|
)
|
2017-05-16 03:53:22 +03:00
|
|
|
if err != nil {
|
|
|
|
if errChan != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
2017-05-05 02:03:47 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the case that the ChainNotifier is shutting down, all subscriber
|
|
|
|
// notification channels will be closed, generating a nil receive.
|
|
|
|
height, ok := <-confNtfn.Confirmed
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The channel has been closed, remove it from any active indexes, and
|
|
|
|
// the database state.
|
2017-08-05 04:32:25 +03:00
|
|
|
peerLog.Infof("ChannelPoint(%v) is now closed at "+
|
2017-05-05 02:03:47 +03:00
|
|
|
"height %v", chanPoint, height.BlockHeight)
|
|
|
|
|
|
|
|
// Finally, execute the closure call back to mark the confirmation of
|
|
|
|
// the transaction closing the contract.
|
|
|
|
cb()
|
2016-06-23 08:19:24 +03:00
|
|
|
}
|
|
|
|
|
2018-09-06 04:22:29 +03:00
|
|
|
// WipeChannel removes the passed channel point from all indexes associated with
|
|
|
|
// the peer, and the switch.
|
2017-11-23 10:15:48 +03:00
|
|
|
func (p *peer) WipeChannel(chanPoint *wire.OutPoint) error {
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
|
2016-06-23 08:19:24 +03:00
|
|
|
|
2016-11-18 05:43:33 +03:00
|
|
|
p.activeChanMtx.Lock()
|
multi: replace per channel sigPool with global daemon level sigPool
In this commit, we remove the per channel `sigPool` within the
`lnwallet.LightningChannel` struct. With this change, we ensure that as
the number of channels grows, the number of gouroutines idling in the
sigPool stays constant. It's the case that currently on the daemon, most
channels are likely inactive, with only a hand full actually
consistently carrying out channel updates. As a result, this change
should reduce the amount of idle CPU usage, as we have less active
goroutines in select loops.
In order to make this change, the `SigPool` itself has been publicly
exported such that outside callers can make a `SigPool` and pass it into
newly created channels. Since the sig pool now lives outside the
channel, we were also able to do away with the Stop() method on the
channel all together.
Finally, the server is the sub-system that is currently responsible for
managing the `SigPool` within lnd.
2018-12-15 03:35:07 +03:00
|
|
|
delete(p.activeChannels, chanID)
|
2016-11-18 05:43:33 +03:00
|
|
|
p.activeChanMtx.Unlock()
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2017-11-23 10:15:48 +03:00
|
|
|
// Instruct the HtlcSwitch to close this link as the channel is no
|
2016-07-10 02:41:06 +03:00
|
|
|
// longer active.
|
2018-08-02 12:34:23 +03:00
|
|
|
p.server.htlcSwitch.RemoveLink(chanID)
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2017-05-02 23:04:58 +03:00
|
|
|
return nil
|
2016-06-21 22:32:32 +03:00
|
|
|
}
|
|
|
|
|
2017-02-16 15:39:38 +03:00
|
|
|
// handleInitMsg handles the incoming init message which contains global and
|
|
|
|
// local features vectors. If feature vectors are incompatible then disconnect.
|
|
|
|
func (p *peer) handleInitMsg(msg *lnwire.Init) error {
|
2019-01-19 05:30:55 +03:00
|
|
|
p.remoteLocalFeatures = lnwire.NewFeatureVector(
|
|
|
|
msg.LocalFeatures, lnwire.LocalFeatures,
|
|
|
|
)
|
|
|
|
p.remoteGlobalFeatures = lnwire.NewFeatureVector(
|
|
|
|
msg.GlobalFeatures, lnwire.GlobalFeatures,
|
|
|
|
)
|
2017-10-11 21:36:23 +03:00
|
|
|
|
2019-01-19 05:30:55 +03:00
|
|
|
// Now that we have their features loaded, we'll ensure that they
|
|
|
|
// didn't set any required bits that we don't know of.
|
2017-10-19 01:14:22 +03:00
|
|
|
unknownLocalFeatures := p.remoteLocalFeatures.UnknownRequiredFeatures()
|
2017-10-11 21:36:23 +03:00
|
|
|
if len(unknownLocalFeatures) > 0 {
|
2018-09-01 00:54:35 +03:00
|
|
|
err := fmt.Errorf("Peer set unknown local feature bits: %v",
|
2017-10-11 21:36:23 +03:00
|
|
|
unknownLocalFeatures)
|
2017-02-16 15:39:38 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-10-19 01:14:22 +03:00
|
|
|
unknownGlobalFeatures := p.remoteGlobalFeatures.UnknownRequiredFeatures()
|
2017-10-11 21:36:23 +03:00
|
|
|
if len(unknownGlobalFeatures) > 0 {
|
2018-09-01 00:54:35 +03:00
|
|
|
err := fmt.Errorf("Peer set unknown global feature bits: %v",
|
2017-10-11 21:36:23 +03:00
|
|
|
unknownGlobalFeatures)
|
2017-02-16 15:39:38 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-19 05:30:55 +03:00
|
|
|
// Now that we know we understand their requirements, we'll check to
|
|
|
|
// see if they don't support anything that we deem to be mandatory.
|
|
|
|
switch {
|
|
|
|
case !p.remoteLocalFeatures.HasFeature(lnwire.DataLossProtectRequired):
|
|
|
|
return fmt.Errorf("data loss protection required")
|
|
|
|
}
|
|
|
|
|
2017-02-16 15:39:38 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-17 05:45:10 +03:00
|
|
|
// sendInitMsg sends init message to remote peer which contains our currently
|
|
|
|
// supported local and global features.
|
2017-02-16 15:39:38 +03:00
|
|
|
func (p *peer) sendInitMsg() error {
|
|
|
|
msg := lnwire.NewInitMessage(
|
2017-10-11 21:36:23 +03:00
|
|
|
p.server.globalFeatures.RawFeatureVector,
|
2017-10-19 01:16:03 +03:00
|
|
|
p.localFeatures,
|
2017-02-16 15:39:38 +03:00
|
|
|
)
|
|
|
|
|
2017-03-17 05:45:10 +03:00
|
|
|
return p.writeMessage(msg)
|
2017-02-16 15:39:38 +03:00
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:46 +03:00
|
|
|
// resendChanSyncMsg will attempt to find a channel sync message for the closed
|
|
|
|
// channel and resend it to our peer.
|
|
|
|
func (p *peer) resendChanSyncMsg(cid lnwire.ChannelID) error {
|
|
|
|
// Check if we have any channel sync messages stored for this channel.
|
|
|
|
c, err := p.server.chanDB.FetchClosedChannelForID(cid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to fetch channel sync messages for "+
|
|
|
|
"peer %v: %v", p, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.LastChanSyncMsg == nil {
|
|
|
|
return fmt.Errorf("no chan sync message stored for channel %v",
|
|
|
|
cid)
|
|
|
|
}
|
|
|
|
|
|
|
|
peerLog.Debugf("Re-sending channel sync message for channel %v to "+
|
|
|
|
"peer %v", cid, p)
|
|
|
|
|
|
|
|
if err := p.SendMessage(true, c.LastChanSyncMsg); err != nil {
|
|
|
|
return fmt.Errorf("Failed resending channel sync "+
|
|
|
|
"message to peer %v: %v", p, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
peerLog.Debugf("Re-sent channel sync message for channel %v to peer "+
|
|
|
|
"%v", cid, p)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 04:08:22 +03:00
|
|
|
// SendMessage sends a variadic number of high-priority message to remote peer.
|
|
|
|
// The first argument denotes if the method should block until the messages have
|
|
|
|
// been sent to the remote peer or an error is returned, otherwise it returns
|
|
|
|
// immediately after queuing.
|
2018-08-02 10:22:38 +03:00
|
|
|
//
|
|
|
|
// NOTE: Part of the lnpeer.Peer interface.
|
2018-06-08 06:07:30 +03:00
|
|
|
func (p *peer) SendMessage(sync bool, msgs ...lnwire.Message) error {
|
2019-03-06 04:08:22 +03:00
|
|
|
return p.sendMessage(sync, true, msgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendMessageLazy sends a variadic number of low-priority message to remote
|
|
|
|
// peer. The first argument denotes if the method should block until the
|
|
|
|
// messages have been sent to the remote peer or an error is returned, otherwise
|
|
|
|
// it returns immediately after queueing.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the lnpeer.Peer interface.
|
|
|
|
func (p *peer) SendMessageLazy(sync bool, msgs ...lnwire.Message) error {
|
|
|
|
return p.sendMessage(sync, false, msgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendMessage queues a variadic number of messages using the passed priority
|
|
|
|
// to the remote peer. If sync is true, this method will block until the
|
|
|
|
// messages have been sent to the remote peer or an error is returned, otherwise
|
|
|
|
// it returns immediately after queueing.
|
|
|
|
func (p *peer) sendMessage(sync, priority bool, msgs ...lnwire.Message) error {
|
2018-06-08 06:07:30 +03:00
|
|
|
// Add all incoming messages to the outgoing queue. A list of error
|
|
|
|
// chans is populated for each message if the caller requested a sync
|
|
|
|
// send.
|
|
|
|
var errChans []chan error
|
2019-03-06 04:08:35 +03:00
|
|
|
if sync {
|
|
|
|
errChans = make([]chan error, 0, len(msgs))
|
|
|
|
}
|
2018-06-08 06:07:30 +03:00
|
|
|
for _, msg := range msgs {
|
|
|
|
// If a sync send was requested, create an error chan to listen
|
|
|
|
// for an ack from the writeHandler.
|
|
|
|
var errChan chan error
|
|
|
|
if sync {
|
|
|
|
errChan = make(chan error, 1)
|
|
|
|
errChans = append(errChans, errChan)
|
|
|
|
}
|
2018-04-05 03:43:51 +03:00
|
|
|
|
2019-03-06 04:08:22 +03:00
|
|
|
if priority {
|
|
|
|
p.queueMsg(msg, errChan)
|
|
|
|
} else {
|
|
|
|
p.queueMsgLazy(msg, errChan)
|
|
|
|
}
|
2018-06-08 06:07:30 +03:00
|
|
|
}
|
2018-04-05 03:43:51 +03:00
|
|
|
|
2018-06-08 06:07:30 +03:00
|
|
|
// Wait for all replies from the writeHandler. For async sends, this
|
|
|
|
// will be a NOP as the list of error chans is nil.
|
|
|
|
for _, errChan := range errChans {
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
return err
|
|
|
|
case <-p.quit:
|
2019-04-27 03:31:27 +03:00
|
|
|
return lnpeer.ErrPeerExiting
|
2019-05-07 00:44:54 +03:00
|
|
|
case <-p.server.quit:
|
|
|
|
return lnpeer.ErrPeerExiting
|
2018-06-08 06:07:30 +03:00
|
|
|
}
|
2018-04-05 03:43:51 +03:00
|
|
|
}
|
2018-06-08 06:07:30 +03:00
|
|
|
|
|
|
|
return nil
|
2016-07-22 03:10:30 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:11:02 +03:00
|
|
|
// PubKey returns the pubkey of the peer in compressed serialized format.
|
2018-08-02 10:22:38 +03:00
|
|
|
//
|
|
|
|
// NOTE: Part of the lnpeer.Peer interface.
|
2017-06-17 01:11:02 +03:00
|
|
|
func (p *peer) PubKey() [33]byte {
|
|
|
|
return p.pubKeyBytes
|
2016-07-13 03:45:29 +03:00
|
|
|
}
|
|
|
|
|
2018-06-08 06:07:30 +03:00
|
|
|
// IdentityKey returns the public key of the remote peer.
|
2018-08-02 10:22:38 +03:00
|
|
|
//
|
|
|
|
// NOTE: Part of the lnpeer.Peer interface.
|
2018-06-08 06:07:30 +03:00
|
|
|
func (p *peer) IdentityKey() *btcec.PublicKey {
|
|
|
|
return p.addr.IdentityKey
|
|
|
|
}
|
|
|
|
|
2018-07-05 23:27:35 +03:00
|
|
|
// Address returns the network address of the remote peer.
|
2018-08-02 10:22:38 +03:00
|
|
|
//
|
|
|
|
// NOTE: Part of the lnpeer.Peer interface.
|
2018-07-05 23:27:35 +03:00
|
|
|
func (p *peer) Address() net.Addr {
|
|
|
|
return p.addr.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddNewChannel adds a new channel to the peer. The channel should fail to be
|
|
|
|
// added if the cancel channel is closed.
|
2018-08-02 10:22:38 +03:00
|
|
|
//
|
|
|
|
// NOTE: Part of the lnpeer.Peer interface.
|
2018-09-26 12:12:57 +03:00
|
|
|
func (p *peer) AddNewChannel(channel *channeldb.OpenChannel,
|
2018-07-05 23:27:35 +03:00
|
|
|
cancel <-chan struct{}) error {
|
|
|
|
|
2018-09-26 12:12:57 +03:00
|
|
|
errChan := make(chan error, 1)
|
2018-07-05 23:27:35 +03:00
|
|
|
newChanMsg := &newChannelMsg{
|
|
|
|
channel: channel,
|
2018-09-26 12:12:57 +03:00
|
|
|
err: errChan,
|
2018-07-05 23:27:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case p.newChannels <- newChanMsg:
|
|
|
|
case <-cancel:
|
|
|
|
return errors.New("canceled adding new channel")
|
|
|
|
case <-p.quit:
|
2019-04-27 03:31:27 +03:00
|
|
|
return lnpeer.ErrPeerExiting
|
2018-07-05 23:27:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We pause here to wait for the peer to recognize the new channel
|
|
|
|
// before we close the channel barrier corresponding to the channel.
|
|
|
|
select {
|
2018-09-26 12:12:57 +03:00
|
|
|
case err := <-errChan:
|
|
|
|
return err
|
2018-07-05 23:27:35 +03:00
|
|
|
case <-p.quit:
|
2019-04-27 03:31:27 +03:00
|
|
|
return lnpeer.ErrPeerExiting
|
2018-07-05 23:27:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 10:22:38 +03:00
|
|
|
// StartTime returns the time at which the connection was established if the
|
|
|
|
// peer started successfully, and zero otherwise.
|
|
|
|
func (p *peer) StartTime() time.Time {
|
|
|
|
return p.startTime
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// TODO(roasbeef): make all start/stop mutexes a CAS
|