2015-12-21 00:16:38 +03:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2016-01-14 08:41:46 +03:00
|
|
|
|
"container/list"
|
2016-07-10 02:41:06 +03:00
|
|
|
|
"fmt"
|
2015-12-21 02:10:09 +03:00
|
|
|
|
"net"
|
2017-07-14 22:05:55 +03:00
|
|
|
|
"strings"
|
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"
|
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2017-04-21 01:45:04 +03:00
|
|
|
|
"github.com/lightningnetwork/lnd/brontide"
|
2017-05-02 23:04:58 +03:00
|
|
|
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
|
|
"github.com/go-errors/errors"
|
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"
|
2017-05-02 23:04:58 +03:00
|
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2016-08-31 02:52:53 +03:00
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2016-01-18 06:14:47 +03:00
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2017-06-29 16:52:55 +03:00
|
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
2016-06-21 22:32:32 +03:00
|
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2017-01-06 00:56:27 +03:00
|
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2017-01-06 00:58:06 +03:00
|
|
|
|
"github.com/roasbeef/btcd/connmgr"
|
2017-03-25 04:26:09 +03:00
|
|
|
|
"github.com/roasbeef/btcd/txscript"
|
2016-05-15 17:17:44 +03:00
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
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
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
// outgoinMsg packages an lnwire.Message to be sent out on the wire, along with
|
|
|
|
|
// 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.
|
2016-01-14 08:41:46 +03:00
|
|
|
|
type outgoinMsg struct {
|
|
|
|
|
msg lnwire.Message
|
2016-06-21 22:32:32 +03:00
|
|
|
|
sentChan chan struct{} // MUST be buffered.
|
2016-01-14 08:41:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-24 05:19:54 +03:00
|
|
|
|
// newChannelMsg packages a lnwallet.LightningChannel 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.
|
|
|
|
|
type newChannelMsg struct {
|
|
|
|
|
channel *lnwallet.LightningChannel
|
|
|
|
|
done chan struct{}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
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.
|
|
|
|
|
// TODO(roasbeef): also use a WMA or EMA?
|
|
|
|
|
pingTime int64
|
|
|
|
|
|
|
|
|
|
// pingLastSend is the Unix time expressed in nanoseconds when we sent
|
|
|
|
|
// our last ping message.
|
|
|
|
|
pingLastSend int64
|
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
// MUST be used atomically.
|
2015-12-21 00:16:38 +03:00
|
|
|
|
started int32
|
2016-01-17 06:03:03 +03:00
|
|
|
|
disconnect int32
|
2015-12-21 00:16:38 +03:00
|
|
|
|
|
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
|
|
|
|
|
2016-06-23 08:22:06 +03:00
|
|
|
|
inbound bool
|
|
|
|
|
id int32
|
2015-12-21 00:16:38 +03:00
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
// This mutex protects all the stats below it.
|
2016-01-17 06:03:03 +03:00
|
|
|
|
sync.RWMutex
|
2016-06-21 22:32:32 +03:00
|
|
|
|
timeConnected time.Time
|
|
|
|
|
lastSend time.Time
|
|
|
|
|
lastRecv time.Time
|
|
|
|
|
|
|
|
|
|
// sendQueue is the channel which is used to queue outgoing to be
|
|
|
|
|
// written onto the wire. Note that this channel is unbuffered.
|
|
|
|
|
sendQueue chan outgoinMsg
|
|
|
|
|
|
|
|
|
|
// outgoingQueue is a buffered channel which allows second/third party
|
|
|
|
|
// objects to queue messages to be sent out on the wire.
|
2016-01-14 08:41:46 +03:00
|
|
|
|
outgoingQueue chan outgoinMsg
|
2015-12-21 00:16:38 +03:00
|
|
|
|
|
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
|
|
|
|
activeChanMtx sync.RWMutex
|
|
|
|
|
activeChannels map[lnwire.ChannelID]*lnwallet.LightningChannel
|
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
|
|
|
|
|
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
|
|
|
|
|
2017-03-25 04:26:09 +03:00
|
|
|
|
// shutdownChanReqs is used to send the Shutdown messages that initiate
|
|
|
|
|
// the cooperative close workflow.
|
|
|
|
|
shutdownChanReqs chan *lnwire.Shutdown
|
|
|
|
|
|
|
|
|
|
// closingSignedChanReqs is used to send signatures for proposed
|
|
|
|
|
// channel close transactions during the cooperative close workflow.
|
|
|
|
|
closingSignedChanReqs chan *lnwire.ClosingSigned
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
|
|
server *server
|
2015-12-21 00:16:38 +03:00
|
|
|
|
|
2017-02-16 15:39:38 +03:00
|
|
|
|
// localSharedFeatures is a product of comparison of our and their
|
|
|
|
|
// local features vectors which consist of features which are present
|
|
|
|
|
// on both sides.
|
2017-02-23 01:49:04 +03:00
|
|
|
|
localSharedFeatures *lnwire.SharedFeatures
|
2017-02-16 15:39:38 +03:00
|
|
|
|
|
|
|
|
|
// globalSharedFeatures is a product of comparison of our and their
|
|
|
|
|
// global features vectors which consist of features which are present
|
|
|
|
|
// on both sides.
|
|
|
|
|
globalSharedFeatures *lnwire.SharedFeatures
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
addr *lnwire.NetAddress, inbound bool) (*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
|
|
|
|
|
|
|
|
|
id: atomic.AddInt32(&numNodes, 1),
|
|
|
|
|
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
|
|
|
|
|
2017-08-09 02:51:19 +03:00
|
|
|
|
sendQueue: make(chan outgoinMsg),
|
|
|
|
|
outgoingQueue: make(chan outgoinMsg),
|
2016-01-17 06:03:03 +03:00
|
|
|
|
|
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-05-02 23:04:58 +03:00
|
|
|
|
localCloseChanReqs: make(chan *htlcswitch.ChanClose),
|
2017-03-25 04:26:09 +03:00
|
|
|
|
shutdownChanReqs: make(chan *lnwire.Shutdown),
|
|
|
|
|
closingSignedChanReqs: make(chan *lnwire.ClosingSigned),
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
2017-02-17 17:28:11 +03:00
|
|
|
|
localSharedFeatures: nil,
|
|
|
|
|
globalSharedFeatures: nil,
|
2017-02-16 15:39:38 +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
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 22:32:32 +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
|
2017-04-14 00:48:38 +03:00
|
|
|
|
// an upper timeout of 15 seconds to respond before we bail out early.
|
|
|
|
|
case <-time.After(time.Second * 15):
|
2017-03-30 04:33:20 +03:00
|
|
|
|
return fmt.Errorf("peer did not complete handshake within 5 " +
|
|
|
|
|
"seconds")
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 "+
|
|
|
|
|
"peerID(%v)", len(activeChans), p.id)
|
|
|
|
|
if err := p.loadActiveChannels(activeChans); err != nil {
|
|
|
|
|
return fmt.Errorf("unable to load channels: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
|
|
|
|
// If the channel isn't yet open, then we don't need to process
|
|
|
|
|
// it any further.
|
|
|
|
|
if dbChan.IsPending {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2016-01-17 06:03:03 +03:00
|
|
|
|
|
2017-05-18 21:55:25 +03:00
|
|
|
|
lnChan, err := lnwallet.NewLightningChannel(p.server.cc.signer,
|
|
|
|
|
p.server.cc.chainNotifier, p.server.cc.feeEstimator, dbChan)
|
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
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
|
2017-04-24 05:23:15 +03:00
|
|
|
|
|
|
|
|
|
p.activeChanMtx.Lock()
|
|
|
|
|
p.activeChannels[chanID] = lnChan
|
|
|
|
|
p.activeChanMtx.Unlock()
|
|
|
|
|
|
2017-08-18 22:16:20 +03:00
|
|
|
|
peerLog.Infof("peerID(%v) loading ChannelPoint(%v)", p.id, chanPoint)
|
2017-04-24 05:23:15 +03:00
|
|
|
|
|
2017-08-18 22:16:20 +03:00
|
|
|
|
select {
|
|
|
|
|
case p.server.breachArbiter.newContracts <- lnChan:
|
|
|
|
|
case <-p.server.quit:
|
|
|
|
|
return fmt.Errorf("server shutting down")
|
|
|
|
|
case <-p.quit:
|
|
|
|
|
return fmt.Errorf("peer shutting down")
|
|
|
|
|
}
|
2017-04-24 05:23:15 +03:00
|
|
|
|
|
2017-08-03 07:15:49 +03:00
|
|
|
|
blockEpoch, err := p.server.cc.chainNotifier.RegisterBlockEpochNtfn()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
_, 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
|
2017-08-31 01:33:49 +03:00
|
|
|
|
if info != nil && info.NodeKey1.IsEqual(p.server.identityPriv.PubKey()) {
|
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,
|
|
|
|
|
BaseFee: selfPolicy.FeeBaseMSat,
|
|
|
|
|
FeeRate: selfPolicy.FeeProportionalMillionths,
|
|
|
|
|
TimeLockDelta: uint32(selfPolicy.TimeLockDelta),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
forwardingPolicy = &p.server.cc.routingPolicy
|
2017-08-22 09:53:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
peerLog.Tracef("Using link policy of: %v", spew.Sdump(forwardingPolicy))
|
|
|
|
|
|
|
|
|
|
// 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.
|
2017-08-03 07:15:49 +03:00
|
|
|
|
linkCfg := htlcswitch.ChannelLinkConfig{
|
|
|
|
|
Peer: p,
|
|
|
|
|
DecodeHopIterator: p.server.sphinx.DecodeHopIterator,
|
|
|
|
|
DecodeOnionObfuscator: p.server.sphinx.DecodeOnionObfuscator,
|
|
|
|
|
GetLastChannelUpdate: createGetLastUpdate(p.server.chanRouter,
|
|
|
|
|
p.PubKey(), lnChan.ShortChanID()),
|
|
|
|
|
SettledContracts: p.server.breachArbiter.settledContracts,
|
|
|
|
|
DebugHTLC: cfg.DebugHTLC,
|
|
|
|
|
Registry: p.server.invoices,
|
|
|
|
|
Switch: p.server.htlcSwitch,
|
2017-08-22 09:53:21 +03:00
|
|
|
|
FwrdingPolicy: *forwardingPolicy,
|
2017-08-03 07:15:49 +03:00
|
|
|
|
BlockEpochs: blockEpoch,
|
|
|
|
|
}
|
|
|
|
|
link := htlcswitch.NewChannelLink(linkCfg, lnChan,
|
|
|
|
|
uint32(currentHeight))
|
2017-05-02 23:04:58 +03:00
|
|
|
|
|
|
|
|
|
if err := p.server.htlcSwitch.AddLink(link); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-04-24 05:23:15 +03:00
|
|
|
|
}
|
2016-01-17 06:03:03 +03:00
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
// irrecoverable protocol error has been encountered.
|
|
|
|
|
func (p *peer) WaitForDisconnect() {
|
|
|
|
|
<-p.quit
|
|
|
|
|
}
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-12 16:44:17 +03:00
|
|
|
|
peerLog.Tracef("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)
|
2017-08-09 02:51:19 +03:00
|
|
|
|
|
|
|
|
|
p.wg.Wait()
|
2016-07-14 02:40:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
// String returns the string representation of this peer.
|
|
|
|
|
func (p *peer) String() string {
|
|
|
|
|
return p.conn.RemoteAddr().String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
rawMsg, err := noiseConn.ReadNextMessage()
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
// TODO(roasbeef): add message summaries
|
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-08-01 07:20:36 +03:00
|
|
|
|
// chanMsgStream implements a goroutine-safe, in-order stream of messages to be
|
|
|
|
|
// delivered to an active channel. These messages MUST be in order due to the
|
|
|
|
|
// nature of the lightning channel commitment state machine. 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.
|
|
|
|
|
type chanMsgStream struct {
|
|
|
|
|
fundingMgr *fundingManager
|
|
|
|
|
htlcSwitch *htlcswitch.Switch
|
|
|
|
|
|
|
|
|
|
cid lnwire.ChannelID
|
|
|
|
|
|
|
|
|
|
peer *peer
|
|
|
|
|
|
|
|
|
|
msgCond *sync.Cond
|
|
|
|
|
msgs []lnwire.Message
|
|
|
|
|
|
|
|
|
|
chanLink htlcswitch.ChannelLink
|
|
|
|
|
|
|
|
|
|
mtx sync.Mutex
|
|
|
|
|
|
2017-08-09 02:51:19 +03:00
|
|
|
|
wg sync.WaitGroup
|
2017-08-01 07:20:36 +03:00
|
|
|
|
quit chan struct{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// newChanMsgStream creates a new instance of a chanMsgStream for a particular
|
|
|
|
|
// channel identified by its channel ID.
|
|
|
|
|
func newChanMsgStream(f *fundingManager, h *htlcswitch.Switch, p *peer,
|
|
|
|
|
c lnwire.ChannelID) *chanMsgStream {
|
|
|
|
|
|
|
|
|
|
stream := &chanMsgStream{
|
|
|
|
|
fundingMgr: f,
|
|
|
|
|
htlcSwitch: h,
|
|
|
|
|
peer: p,
|
|
|
|
|
cid: c,
|
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
|
}
|
|
|
|
|
stream.msgCond = sync.NewCond(&stream.mtx)
|
|
|
|
|
|
|
|
|
|
return stream
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start starts the chanMsgStream.
|
|
|
|
|
func (c *chanMsgStream) Start() {
|
2017-08-09 02:51:19 +03:00
|
|
|
|
c.wg.Add(1)
|
2017-08-01 07:20:36 +03:00
|
|
|
|
go c.msgConsumer()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop stops the chanMsgStream.
|
|
|
|
|
func (c *chanMsgStream) Stop() {
|
|
|
|
|
// TODO(roasbeef): signal too?
|
|
|
|
|
|
|
|
|
|
close(c.quit)
|
2017-08-01 07:31:16 +03:00
|
|
|
|
|
|
|
|
|
// Wake up the msgConsumer is we've been signalled to exit.
|
|
|
|
|
c.msgCond.Signal()
|
2017-08-09 02:51:19 +03:00
|
|
|
|
|
|
|
|
|
c.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.
|
|
|
|
|
func (c *chanMsgStream) msgConsumer() {
|
2017-08-09 02:51:19 +03:00
|
|
|
|
defer c.wg.Done()
|
|
|
|
|
|
2017-08-01 07:20:36 +03:00
|
|
|
|
peerLog.Tracef("Update stream for ChannelID(%x) created", c.cid[:])
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
// First, we'll check our condition. If the queue of messages
|
|
|
|
|
// is empty, then we'll wait until a new item is added.
|
|
|
|
|
c.msgCond.L.Lock()
|
|
|
|
|
for len(c.msgs) == 0 {
|
|
|
|
|
c.msgCond.Wait()
|
2017-08-01 07:31:16 +03:00
|
|
|
|
|
|
|
|
|
// If we were woke up in order to exit, then we'll do
|
|
|
|
|
// so. Otherwise, we'll check the message queue for any
|
|
|
|
|
// new items.
|
|
|
|
|
select {
|
|
|
|
|
case <-c.quit:
|
|
|
|
|
peerLog.Tracef("Update stream for "+
|
|
|
|
|
"ChannelID(%x) exiting", c.cid[:])
|
2017-08-03 23:49:25 +03:00
|
|
|
|
c.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.
|
|
|
|
|
msg := c.msgs[0]
|
|
|
|
|
c.msgs = c.msgs[1:]
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
c.fundingMgr.waitUntilChannelOpen(c.cid)
|
|
|
|
|
|
|
|
|
|
// Dispatch the commitment update message to the proper active
|
|
|
|
|
// goroutine dedicated to this channel.
|
|
|
|
|
if c.chanLink == nil {
|
|
|
|
|
link, err := c.htlcSwitch.GetLink(c.cid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("recv'd update for unknown "+
|
|
|
|
|
"channel %v from %v", c.cid, c.peer)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
c.chanLink = link
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.chanLink.HandleChannelUpdate(msg)
|
|
|
|
|
c.msgCond.L.Unlock()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddMsg adds a new message to the chanMsgStream. This function is safe for
|
|
|
|
|
// concurrent access.
|
|
|
|
|
func (c *chanMsgStream) AddMsg(msg lnwire.Message) {
|
|
|
|
|
// First, we'll lock the condition, and add the message to the end of
|
|
|
|
|
// the message queue.
|
|
|
|
|
c.msgCond.L.Lock()
|
|
|
|
|
c.msgs = append(c.msgs, msg)
|
|
|
|
|
c.msgCond.L.Unlock()
|
|
|
|
|
|
|
|
|
|
// With the message added, we signal to the msgConsumer that there are
|
|
|
|
|
// additional messages to consume.
|
|
|
|
|
c.msgCond.Signal()
|
|
|
|
|
}
|
|
|
|
|
|
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() {
|
2017-08-09 02:51:19 +03:00
|
|
|
|
defer p.wg.Done()
|
|
|
|
|
|
2017-08-01 07:25:46 +03:00
|
|
|
|
chanMsgStreams := make(map[lnwire.ChannelID]*chanMsgStream)
|
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()
|
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:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
|
p.server.fundingMgr.processFundingOpen(msg, p.addr)
|
|
|
|
|
case *lnwire.AcceptChannel:
|
2017-08-01 07:04:48 +03:00
|
|
|
|
p.server.fundingMgr.processFundingAccept(msg, p.addr)
|
2017-07-31 00:13:28 +03:00
|
|
|
|
case *lnwire.FundingCreated:
|
|
|
|
|
p.server.fundingMgr.processFundingCreated(msg, p.addr)
|
|
|
|
|
case *lnwire.FundingSigned:
|
|
|
|
|
p.server.fundingMgr.processFundingSigned(msg, p.addr)
|
2017-01-31 05:45:28 +03:00
|
|
|
|
case *lnwire.FundingLocked:
|
|
|
|
|
p.server.fundingMgr.processFundingLocked(msg, p.addr)
|
2017-03-25 04:26:09 +03:00
|
|
|
|
|
|
|
|
|
case *lnwire.Shutdown:
|
|
|
|
|
p.shutdownChanReqs <- msg
|
2017-03-09 02:32:11 +03:00
|
|
|
|
case *lnwire.ClosingSigned:
|
2017-03-25 04:26:09 +03:00
|
|
|
|
p.closingSignedChanReqs <- msg
|
2016-10-15 16:24:56 +03:00
|
|
|
|
|
2017-04-17 01:41:11 +03:00
|
|
|
|
case *lnwire.Error:
|
|
|
|
|
p.server.fundingMgr.processFundingError(msg, p.addr)
|
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
|
2017-02-21 05:10:05 +03:00
|
|
|
|
case *lnwire.UpdateFufillHTLC:
|
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.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
|
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,
|
|
|
|
|
*lnwire.AnnounceSignatures:
|
2016-12-27 08:42:23 +03:00
|
|
|
|
|
2017-08-22 09:54:10 +03:00
|
|
|
|
p.server.authGossiper.ProcessRemoteAnnouncement(msg,
|
2016-12-27 08:42:23 +03:00
|
|
|
|
p.addr.IdentityKey)
|
2017-03-28 22:08:14 +03:00
|
|
|
|
default:
|
2017-03-27 20:25:44 +03:00
|
|
|
|
peerLog.Errorf("unknown message received from peer "+
|
|
|
|
|
"%v", 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.
|
|
|
|
|
chanStream = newChanMsgStream(p.server.fundingMgr,
|
|
|
|
|
p.server.htlcSwitch, p, targetChan)
|
|
|
|
|
chanMsgStreams[targetChan] = chanStream
|
|
|
|
|
chanStream.Start()
|
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.
|
|
|
|
|
chanStream.AddMsg(nextMsg)
|
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
|
|
|
|
|
2017-08-03 23:52:02 +03:00
|
|
|
|
for cid, chanStream := range chanMsgStreams {
|
2017-08-01 07:25:46 +03:00
|
|
|
|
chanStream.Stop()
|
2017-08-03 23:52:02 +03:00
|
|
|
|
|
|
|
|
|
delete(chanMsgStreams, cid)
|
2017-08-01 07:25:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
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-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) {
|
|
|
|
|
switch m := msg.(type) {
|
2017-02-21 05:10:05 +03:00
|
|
|
|
case *lnwire.RevokeAndAck:
|
2017-01-15 04:52:05 +03:00
|
|
|
|
m.NextRevocationKey.Curve = nil
|
|
|
|
|
case *lnwire.NodeAnnouncement:
|
|
|
|
|
m.NodeID.Curve = nil
|
|
|
|
|
case *lnwire.ChannelAnnouncement:
|
2017-03-27 18:22:37 +03:00
|
|
|
|
m.NodeID1.Curve = nil
|
|
|
|
|
m.NodeID2.Curve = nil
|
|
|
|
|
m.BitcoinKey1.Curve = nil
|
|
|
|
|
m.BitcoinKey2.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
|
|
|
|
|
m.FirstCommitmentPoint.Curve = nil
|
|
|
|
|
case *lnwire.OpenChannel:
|
|
|
|
|
m.FundingKey.Curve = nil
|
|
|
|
|
m.RevocationPoint.Curve = nil
|
|
|
|
|
m.PaymentPoint.Curve = nil
|
|
|
|
|
m.DelayedPaymentPoint.Curve = nil
|
|
|
|
|
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)
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
// writeMessage writes the target lnwire.Message to the remote peer.
|
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 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
// TODO(roasbeef): add message summaries
|
2017-01-15 04:52:05 +03:00
|
|
|
|
p.logWireMessage(msg, false)
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
2017-04-21 01:45:04 +03:00
|
|
|
|
// As the Lightning wire protocol is fully message oriented, we only
|
|
|
|
|
// allows one wire message per outer encapsulated crypto message. So
|
|
|
|
|
// we'll create a temporary buffer to write the message directly to.
|
|
|
|
|
var msgPayload [lnwire.MaxMessagePayload]byte
|
|
|
|
|
b := bytes.NewBuffer(msgPayload[0:0:len(msgPayload)])
|
|
|
|
|
|
|
|
|
|
// With the temp buffer created and sliced properly (length zero, full
|
|
|
|
|
// capacity), we'll now encode the message directly into this buffer.
|
|
|
|
|
n, err := lnwire.WriteMessage(b, msg, 0)
|
2016-06-21 22:32:32 +03:00
|
|
|
|
atomic.AddUint64(&p.bytesSent, uint64(n))
|
2016-01-14 08:41:46 +03:00
|
|
|
|
|
2017-04-21 01:45:04 +03:00
|
|
|
|
// Finally, write the message itself in a single swoop.
|
|
|
|
|
_, err = p.conn.Write(b.Bytes())
|
2016-01-14 08:41:46 +03:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
// queueHandler in order to ensure the incoming message queue is quickly drained.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: This method MUST be run as a goroutine.
|
|
|
|
|
func (p *peer) writeHandler() {
|
2017-02-02 04:01:33 +03:00
|
|
|
|
defer func() {
|
|
|
|
|
p.wg.Done()
|
|
|
|
|
peerLog.Tracef("writeHandler for peer %v done", p)
|
|
|
|
|
}()
|
|
|
|
|
|
2016-01-14 08:41:46 +03:00
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case outMsg := <-p.sendQueue:
|
2017-01-26 05:20:55 +03:00
|
|
|
|
switch outMsg.msg.(type) {
|
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.
|
2017-01-26 05:20:55 +03:00
|
|
|
|
case *lnwire.Ping:
|
|
|
|
|
// TODO(roasbeef): do this before the write?
|
|
|
|
|
// possibly account for processing within func?
|
|
|
|
|
now := time.Now().UnixNano()
|
|
|
|
|
atomic.StoreInt64(&p.pingLastSend, now)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 04:01:33 +03:00
|
|
|
|
// Write out the message to the socket, closing the
|
|
|
|
|
// 'sentChan' if it's non-nil, The 'sentChan' allows
|
|
|
|
|
// callers to optionally synchronize sends with the
|
|
|
|
|
// writeHandler.
|
|
|
|
|
err := p.writeMessage(outMsg.msg)
|
|
|
|
|
if outMsg.sentChan != nil {
|
|
|
|
|
close(outMsg.sentChan)
|
|
|
|
|
}
|
2016-01-14 08:41:46 +03:00
|
|
|
|
|
2017-02-02 04:01:33 +03:00
|
|
|
|
if err != nil {
|
2017-07-12 16:44:17 +03:00
|
|
|
|
p.Disconnect(errors.Errorf("unable to write message: %v",
|
|
|
|
|
err))
|
2017-02-02 04:01:33 +03:00
|
|
|
|
return
|
2016-01-14 08:41:46 +03:00
|
|
|
|
}
|
2017-02-02 04:01:33 +03:00
|
|
|
|
|
|
|
|
|
case <-p.quit:
|
|
|
|
|
return
|
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()
|
|
|
|
|
|
2016-01-14 08:41:46 +03:00
|
|
|
|
pendingMsgs := list.New()
|
|
|
|
|
for {
|
2017-02-02 04:01:33 +03:00
|
|
|
|
// Before add a queue'd message our pending message queue,
|
|
|
|
|
// we'll first try to aggressively empty out our pending list of
|
|
|
|
|
// messaging.
|
|
|
|
|
for {
|
|
|
|
|
// Examine the front of the queue. If this message is
|
|
|
|
|
// nil, then we've emptied out the queue and can accept
|
|
|
|
|
// new messages from outside sub-systems.
|
|
|
|
|
elem := pendingMsgs.Front()
|
|
|
|
|
if elem == nil {
|
|
|
|
|
break
|
2016-01-14 08:41:46 +03:00
|
|
|
|
}
|
2017-02-02 04:01:33 +03:00
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case p.sendQueue <- elem.Value.(outgoinMsg):
|
|
|
|
|
pendingMsgs.Remove(elem)
|
|
|
|
|
case <-p.quit:
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
break
|
2016-01-14 08:41:46 +03:00
|
|
|
|
}
|
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 there weren't any messages to send, or the writehandler
|
|
|
|
|
// is still blocked, then we'll accept a new message into the
|
|
|
|
|
// queue from outside sub-systems.
|
|
|
|
|
select {
|
2016-01-14 08:41:46 +03:00
|
|
|
|
case <-p.quit:
|
2017-02-02 04:01:33 +03:00
|
|
|
|
return
|
|
|
|
|
case msg := <-p.outgoingQueue:
|
|
|
|
|
pendingMsgs.PushBack(msg)
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
// queueMsg queues a new lnwire.Message to be eventually sent out on the
|
|
|
|
|
// wire.
|
|
|
|
|
func (p *peer) queueMsg(msg lnwire.Message, doneChan chan struct{}) {
|
2016-12-20 04:00:18 +03:00
|
|
|
|
select {
|
|
|
|
|
case p.outgoingQueue <- outgoinMsg{msg, doneChan}:
|
|
|
|
|
case <-p.quit:
|
|
|
|
|
return
|
|
|
|
|
}
|
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 {
|
|
|
|
|
snapshot := activeChan.StateSnapshot()
|
|
|
|
|
snapshots = append(snapshots, snapshot)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return snapshots
|
2016-06-23 08:22:06 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 00:21:21 +03:00
|
|
|
|
// closingScripts are the set of clsoign deslivery scripts for each party. This
|
|
|
|
|
// intermediate state is maintained for each active close negotiation, as the
|
|
|
|
|
// final signatures sent must cover the specified delivery scripts for each
|
|
|
|
|
// party.
|
|
|
|
|
type closingScripts struct {
|
|
|
|
|
localScript []byte
|
|
|
|
|
remoteScript []byte
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
|
2017-03-25 04:26:09 +03:00
|
|
|
|
// chanShutdowns is a map of channels for which our node has initiated
|
|
|
|
|
// a cooperative channel close. When an lnwire.Shutdown is received,
|
|
|
|
|
// this allows the node to determine the next step to be taken in the
|
|
|
|
|
// workflow.
|
2017-05-02 23:04:58 +03:00
|
|
|
|
chanShutdowns := make(map[lnwire.ChannelID]*htlcswitch.ChanClose)
|
2017-03-25 04:26:09 +03:00
|
|
|
|
|
2017-07-31 00:21:21 +03:00
|
|
|
|
deliveryAddrs := make(map[lnwire.ChannelID]*closingScripts)
|
|
|
|
|
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// initiator[ShutdownSigs|FeeProposals] holds the
|
|
|
|
|
// [signature|feeProposal] for the last ClosingSigned sent to the peer
|
|
|
|
|
// by the initiator. This enables us to respond to subsequent steps in
|
|
|
|
|
// the workflow without having to recalculate our signature for the
|
|
|
|
|
// channel close transaction, and track the sent fee proposals for fee
|
|
|
|
|
// negotiation purposes.
|
|
|
|
|
initiatorShutdownSigs := make(map[lnwire.ChannelID][]byte)
|
|
|
|
|
initiatorFeeProposals := make(map[lnwire.ChannelID]uint64)
|
|
|
|
|
|
|
|
|
|
// responder[ShutdownSigs|FeeProposals] is similar to the the maps
|
|
|
|
|
// above, just for the responder.
|
|
|
|
|
responderShutdownSigs := make(map[lnwire.ChannelID][]byte)
|
|
|
|
|
responderFeeProposals := make(map[lnwire.ChannelID]uint64)
|
2017-07-31 00:21:21 +03:00
|
|
|
|
|
|
|
|
|
// TODO(roasbeef): move to cfg closure func
|
|
|
|
|
genDeliveryScript := func() ([]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)
|
|
|
|
|
}
|
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-04-17 01:41:11 +03:00
|
|
|
|
chanPoint := newChanReq.channel.ChannelPoint()
|
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
|
2017-07-31 00:21:21 +03:00
|
|
|
|
newChan := newChanReq.channel
|
2016-11-18 05:43:33 +03:00
|
|
|
|
|
2017-07-31 00:21:21 +03:00
|
|
|
|
// First, we'll add this channel to the set of active
|
|
|
|
|
// channels, so we can look it up later easily
|
|
|
|
|
// according to its channel ID.
|
2016-11-18 05:43:33 +03:00
|
|
|
|
p.activeChanMtx.Lock()
|
2017-07-31 00:21:21 +03:00
|
|
|
|
p.activeChannels[chanID] = newChan
|
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) "+
|
|
|
|
|
"with peerId(%v)", chanPoint, p.id)
|
|
|
|
|
|
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?
|
|
|
|
|
blockEpoch, err := p.server.cc.chainNotifier.RegisterBlockEpochNtfn()
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("unable to register for block epoch: %v", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
_, currentHeight, err := p.server.cc.chainIO.GetBestBlock()
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("unable to get best block: %v", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2017-07-31 00:21:21 +03:00
|
|
|
|
linkConfig := htlcswitch.ChannelLinkConfig{
|
|
|
|
|
Peer: p,
|
|
|
|
|
DecodeHopIterator: p.server.sphinx.DecodeHopIterator,
|
|
|
|
|
DecodeOnionObfuscator: p.server.sphinx.DecodeOnionObfuscator,
|
|
|
|
|
GetLastChannelUpdate: createGetLastUpdate(p.server.chanRouter,
|
|
|
|
|
p.PubKey(), newChanReq.channel.ShortChanID()),
|
|
|
|
|
SettledContracts: p.server.breachArbiter.settledContracts,
|
|
|
|
|
DebugHTLC: cfg.DebugHTLC,
|
|
|
|
|
Registry: p.server.invoices,
|
|
|
|
|
Switch: p.server.htlcSwitch,
|
|
|
|
|
FwrdingPolicy: p.server.cc.routingPolicy,
|
2017-08-03 07:15:49 +03:00
|
|
|
|
BlockEpochs: blockEpoch,
|
2017-07-31 00:21:21 +03:00
|
|
|
|
}
|
2017-08-03 07:15:49 +03:00
|
|
|
|
link := htlcswitch.NewChannelLink(linkConfig, newChan,
|
|
|
|
|
uint32(currentHeight))
|
2017-05-02 23:04:58 +03:00
|
|
|
|
|
2017-07-31 00:21:21 +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.
|
2017-08-03 07:15:49 +03:00
|
|
|
|
if err := p.server.htlcSwitch.AddLink(link); err != nil {
|
2017-05-02 23:04:58 +03:00
|
|
|
|
peerLog.Errorf("can't register new channel "+
|
|
|
|
|
"link(%v) with peerId(%v)", chanPoint, p.id)
|
|
|
|
|
}
|
2017-01-24 02:33:46 +03:00
|
|
|
|
|
|
|
|
|
close(newChanReq.done)
|
2016-07-14 02:40:01 +03:00
|
|
|
|
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// We've just received a local quest to close an active
|
|
|
|
|
// channel.
|
2016-06-21 22:32:32 +03:00
|
|
|
|
case req := <-p.localCloseChanReqs:
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// So we'll first transition the channel to a state of
|
|
|
|
|
// pending shutdown.
|
2017-05-02 23:04:58 +03:00
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(req.ChanPoint)
|
2017-03-25 04:26:09 +03:00
|
|
|
|
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// We'll only track this shutdown request if this is a
|
|
|
|
|
// regular close request, and not in response to a
|
|
|
|
|
// channel breach.
|
2017-07-31 00:21:21 +03:00
|
|
|
|
var (
|
|
|
|
|
deliveryScript []byte
|
|
|
|
|
err error
|
|
|
|
|
)
|
2017-05-02 23:04:58 +03:00
|
|
|
|
if req.CloseType == htlcswitch.CloseRegular {
|
2017-05-24 01:26:38 +03:00
|
|
|
|
chanShutdowns[chanID] = req
|
2017-07-31 00:21:21 +03:00
|
|
|
|
|
|
|
|
|
// As we need to close out the channel and
|
|
|
|
|
// claim our funds on-chain, we'll request a
|
|
|
|
|
// new delivery address from the wallet, and
|
|
|
|
|
// turn that into it corresponding output
|
|
|
|
|
// script.
|
|
|
|
|
deliveryScript, err = genDeliveryScript()
|
|
|
|
|
if err != nil {
|
|
|
|
|
cErr := fmt.Errorf("Unable to generate "+
|
|
|
|
|
"delivery address: %v", err)
|
|
|
|
|
|
|
|
|
|
peerLog.Errorf(cErr.Error())
|
|
|
|
|
|
|
|
|
|
req.Err <- cErr
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We'll also track this delivery script, as
|
|
|
|
|
// we'll need it to reconstruct the cooperative
|
|
|
|
|
// closure transaction during our closing fee
|
|
|
|
|
// negotiation ratchet.
|
|
|
|
|
deliveryAddrs[chanID] = &closingScripts{
|
|
|
|
|
localScript: deliveryScript,
|
|
|
|
|
}
|
2017-05-24 01:26:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// With the state marked as shutting down, we can now
|
|
|
|
|
// proceed with the channel close workflow. If this is
|
|
|
|
|
// regular close, we'll send a shutdown. Otherwise,
|
|
|
|
|
// we'll simply be clearing our indexes.
|
2017-07-31 00:21:21 +03:00
|
|
|
|
p.handleLocalClose(req, deliveryScript)
|
2016-07-14 02:40:01 +03:00
|
|
|
|
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// A receipt of a message over this channel indicates that
|
|
|
|
|
// either a shutdown proposal has been initiated, or a prior
|
|
|
|
|
// one has been completed, advancing to the next state of
|
|
|
|
|
// channel closure.
|
2017-03-25 04:26:09 +03:00
|
|
|
|
case req := <-p.shutdownChanReqs:
|
2017-07-31 00:21:21 +03:00
|
|
|
|
// If we don't have a channel that matches this channel
|
|
|
|
|
// ID, then we'll ignore this message.
|
|
|
|
|
chanID := req.ChannelID
|
|
|
|
|
p.activeChanMtx.Lock()
|
|
|
|
|
_, ok := p.activeChannels[chanID]
|
|
|
|
|
p.activeChanMtx.Unlock()
|
|
|
|
|
if !ok {
|
|
|
|
|
peerLog.Warnf("Received unsolicited shutdown msg: %v",
|
|
|
|
|
spew.Sdump(req))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// First, we'll track their delivery script for when we
|
|
|
|
|
// ultimately create the cooperative closure
|
|
|
|
|
// transaction.
|
|
|
|
|
deliveryScripts, ok := deliveryAddrs[chanID]
|
|
|
|
|
if !ok {
|
|
|
|
|
deliveryAddrs[chanID] = &closingScripts{}
|
|
|
|
|
deliveryScripts = deliveryAddrs[chanID]
|
|
|
|
|
}
|
|
|
|
|
deliveryScripts.remoteScript = req.Address
|
|
|
|
|
|
|
|
|
|
// Next, we'll check in the shutdown map to see if
|
|
|
|
|
// we're the initiator or not. If we don't have an
|
|
|
|
|
// entry for this channel, then this means that we're
|
|
|
|
|
// the responder to the workflow.
|
2017-05-24 01:26:38 +03:00
|
|
|
|
if _, ok := chanShutdowns[req.ChannelID]; !ok {
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// Check responderShutdownSigs for an already
|
|
|
|
|
// existing shutdown signature for this channel.
|
|
|
|
|
// If such a signature exists, it means we
|
|
|
|
|
// already have sent a response to a shutdown
|
|
|
|
|
// message for this channel, so ignore this one.
|
|
|
|
|
_, exists := responderShutdownSigs[req.ChannelID]
|
|
|
|
|
if exists {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 00:21:21 +03:00
|
|
|
|
// As we're the responder, we'll need to
|
|
|
|
|
// generate a delivery script of our own.
|
|
|
|
|
deliveryScript, err := genDeliveryScript()
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("Unable to generate "+
|
|
|
|
|
"delivery address: %v", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
deliveryScripts.localScript = deliveryScript
|
|
|
|
|
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// In this case, we'll send a shutdown message,
|
|
|
|
|
// and also prep our closing signature for the
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// case the fees are immediately agreed upon.
|
|
|
|
|
closeSig, proposedFee := p.handleShutdownResponse(
|
|
|
|
|
req, deliveryScript)
|
2017-05-24 01:26:38 +03:00
|
|
|
|
if closeSig != nil {
|
2017-07-14 22:05:55 +03:00
|
|
|
|
responderShutdownSigs[req.ChannelID] = closeSig
|
|
|
|
|
responderFeeProposals[req.ChannelID] = proposedFee
|
2017-05-24 01:26:38 +03:00
|
|
|
|
}
|
2017-03-25 04:26:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// A receipt of a message over this channel indicates that the
|
|
|
|
|
// final stage of a channel shutdown workflow has been
|
|
|
|
|
// completed.
|
2017-03-25 04:26:09 +03:00
|
|
|
|
case req := <-p.closingSignedChanReqs:
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// First we'll check if this has an entry in the local
|
|
|
|
|
// shutdown map.
|
2017-07-31 00:21:21 +03:00
|
|
|
|
chanID := req.ChannelID
|
|
|
|
|
localCloseReq, ok := chanShutdowns[chanID]
|
2017-05-24 01:26:38 +03:00
|
|
|
|
|
|
|
|
|
// If it does, then this means we were the initiator of
|
|
|
|
|
// the channel shutdown procedure.
|
2017-03-25 04:26:09 +03:00
|
|
|
|
if ok {
|
2017-07-14 22:05:55 +03:00
|
|
|
|
shutdownSig := initiatorShutdownSigs[req.ChannelID]
|
|
|
|
|
initiatorSig := append(shutdownSig,
|
|
|
|
|
byte(txscript.SigHashAll))
|
|
|
|
|
|
|
|
|
|
// To finalize this shtudown, we'll now send a
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// matching close signed message to the other
|
|
|
|
|
// party, and broadcast the closing transaction
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// to the network. If the fees are still being
|
|
|
|
|
// negotiated, handleClosingSigned returns the
|
|
|
|
|
// signature and proposed fee we sent to the
|
|
|
|
|
// peer. In the case fee negotiation was
|
|
|
|
|
// complete, and the closing tx was broadcasted,
|
|
|
|
|
// closeSig will be nil, and we can delete the
|
|
|
|
|
// state associated with this channel shutdown.
|
|
|
|
|
closeSig, proposedFee := p.handleClosingSigned(
|
|
|
|
|
localCloseReq, req,
|
|
|
|
|
deliveryAddrs[chanID], initiatorSig,
|
|
|
|
|
initiatorFeeProposals[req.ChannelID])
|
|
|
|
|
if closeSig != nil {
|
|
|
|
|
initiatorShutdownSigs[req.ChannelID] = closeSig
|
|
|
|
|
initiatorFeeProposals[req.ChannelID] = proposedFee
|
|
|
|
|
} else {
|
|
|
|
|
delete(initiatorShutdownSigs, req.ChannelID)
|
|
|
|
|
delete(initiatorFeeProposals, req.ChannelID)
|
|
|
|
|
delete(chanShutdowns, req.ChannelID)
|
|
|
|
|
delete(deliveryAddrs, req.ChannelID)
|
|
|
|
|
}
|
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-07-14 22:05:55 +03:00
|
|
|
|
shutdownSig := responderShutdownSigs[req.ChannelID]
|
|
|
|
|
responderSig := append(shutdownSig,
|
2017-05-24 01:26:38 +03:00
|
|
|
|
byte(txscript.SigHashAll))
|
2017-07-31 00:21:21 +03:00
|
|
|
|
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// Otherwise, we're the responder to the channel
|
|
|
|
|
// shutdown procedure. The procedure will be the same,
|
|
|
|
|
// but we don't have a local request to to notify about
|
|
|
|
|
// updates, so just pass in nil instead.
|
|
|
|
|
closeSig, proposedFee := p.handleClosingSigned(nil, req,
|
|
|
|
|
deliveryAddrs[chanID], responderSig,
|
|
|
|
|
responderFeeProposals[req.ChannelID])
|
|
|
|
|
if closeSig != nil {
|
|
|
|
|
responderShutdownSigs[req.ChannelID] = closeSig
|
|
|
|
|
responderFeeProposals[req.ChannelID] = proposedFee
|
|
|
|
|
} else {
|
|
|
|
|
delete(responderShutdownSigs, req.ChannelID)
|
|
|
|
|
delete(responderFeeProposals, req.ChannelID)
|
|
|
|
|
delete(deliveryAddrs, chanID)
|
|
|
|
|
}
|
2017-05-24 01:26:38 +03:00
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
case <-p.quit:
|
|
|
|
|
break out
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-12 22:42:26 +03:00
|
|
|
|
// handleLocalClose kicks-off the workflow to execute a cooperative or forced
|
2017-01-13 08:01:50 +03:00
|
|
|
|
// unilateral closure of the channel initiated by a local subsystem.
|
2017-05-24 01:26:38 +03:00
|
|
|
|
//
|
2016-12-15 05:11:31 +03:00
|
|
|
|
// TODO(roasbeef): if no more active channels with peer call Remove on connMgr
|
|
|
|
|
// with peerID
|
2017-07-31 00:21:21 +03:00
|
|
|
|
func (p *peer) handleLocalClose(req *htlcswitch.ChanClose, deliveryScript []byte) {
|
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-07-31 00:21:21 +03:00
|
|
|
|
err := p.sendShutdown(channel, deliveryScript)
|
2017-03-25 04:26:09 +03:00
|
|
|
|
if err != nil {
|
2017-05-02 23:04:58 +03:00
|
|
|
|
req.Err <- err
|
2017-03-25 04:26:09 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
2016-11-29 05:44:14 +03:00
|
|
|
|
|
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)
|
|
|
|
|
if err := p.WipeChannel(channel); 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleShutdownResponse is called when a responder in a cooperative channel
|
|
|
|
|
// close workflow receives a Shutdown message. This is the second step in the
|
|
|
|
|
// cooperative close workflow. This function generates a close transaction with
|
|
|
|
|
// a proposed fee amount and sends the signed transaction to the initiator.
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// Returns the signature used to signed the close proposal, and the proposed
|
|
|
|
|
// fee.
|
2017-07-31 00:21:21 +03:00
|
|
|
|
func (p *peer) handleShutdownResponse(msg *lnwire.Shutdown,
|
2017-07-14 22:05:55 +03:00
|
|
|
|
localDeliveryScript []byte) ([]byte, uint64) {
|
2017-03-25 04:26:09 +03:00
|
|
|
|
p.activeChanMtx.RLock()
|
|
|
|
|
channel, ok := p.activeChannels[msg.ChannelID]
|
|
|
|
|
p.activeChanMtx.RUnlock()
|
|
|
|
|
if !ok {
|
|
|
|
|
peerLog.Errorf("unable to close channel, ChannelPoint(%v) is "+
|
|
|
|
|
"unknown", msg.ChannelID)
|
2017-07-14 22:05:55 +03:00
|
|
|
|
return nil, 0
|
2017-03-25 04:26:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 01:22:56 +03:00
|
|
|
|
// As we just received a shutdown message, we'll also send a shutdown
|
|
|
|
|
// message with our desired fee so we can start the negotiation.
|
2017-07-31 00:21:21 +03:00
|
|
|
|
err := p.sendShutdown(channel, localDeliveryScript)
|
|
|
|
|
if err != nil {
|
2017-03-25 04:26:09 +03:00
|
|
|
|
peerLog.Errorf("error while sending shutdown message: %v", err)
|
2017-07-14 22:05:55 +03:00
|
|
|
|
return nil, 0
|
2017-03-25 04:26:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 01:22:56 +03:00
|
|
|
|
// Calculate an initial proposed fee rate for the close transaction.
|
2017-06-06 05:40:43 +03:00
|
|
|
|
feeRate := p.server.cc.feeEstimator.EstimateFeePerWeight(1) * 1000
|
2017-05-24 01:22:56 +03:00
|
|
|
|
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// We propose a fee and send a close proposal to the peer. This will
|
|
|
|
|
// start the fee negotiations. Once both sides agree on a fee, we'll
|
|
|
|
|
// create a signature that closes the channel using the agreed upon fee.
|
|
|
|
|
fee := channel.CalcFee(feeRate)
|
2017-07-31 00:21:21 +03:00
|
|
|
|
closeSig, proposedFee, err := channel.CreateCloseProposal(
|
2017-07-14 22:05:55 +03:00
|
|
|
|
fee, localDeliveryScript, msg.Address,
|
2017-07-31 00:21:21 +03:00
|
|
|
|
)
|
2017-03-25 04:26:09 +03:00
|
|
|
|
if err != nil {
|
2017-05-24 01:22:56 +03:00
|
|
|
|
peerLog.Errorf("unable to create close proposal: %v", err)
|
2017-07-14 22:05:55 +03:00
|
|
|
|
return nil, 0
|
2017-03-25 04:26:09 +03:00
|
|
|
|
}
|
2017-05-24 01:22:56 +03:00
|
|
|
|
parsedSig, err := btcec.ParseSignature(closeSig, btcec.S256())
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("unable to parse signature: %v", err)
|
2017-07-14 22:05:55 +03:00
|
|
|
|
return nil, 0
|
2017-05-24 01:22:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// With the closing signature assembled, we'll send the matching close
|
|
|
|
|
// signed message to the other party so they can broadcast the closing
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// transaction if they agree with the fee, or create a new close
|
|
|
|
|
// proposal if they don't.
|
2017-05-24 01:22:56 +03:00
|
|
|
|
closingSigned := lnwire.NewClosingSigned(msg.ChannelID, proposedFee,
|
|
|
|
|
parsedSig)
|
|
|
|
|
p.queueMsg(closingSigned, nil)
|
|
|
|
|
|
2017-07-14 22:05:55 +03:00
|
|
|
|
return closeSig, proposedFee
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// calculateCompromiseFee performs the current fee negotiation algorithm,
|
|
|
|
|
// taking into consideration our ideal fee based on current fee environment,
|
|
|
|
|
// the fee we last proposed (if any), and the fee proposed by the peer.
|
|
|
|
|
func calculateCompromiseFee(ourIdealFee, lastSentFee, peerFee uint64) uint64 {
|
|
|
|
|
// We will accept a proposed fee in the interval
|
|
|
|
|
// [0.5*ourIdealFee, 2*ourIdealFee]. If the peer's fee doesn't fall in
|
|
|
|
|
// this range, we'll propose the average of the peer's fee and our last
|
|
|
|
|
// sent fee, as long as it is in this range.
|
|
|
|
|
// TODO(halseth): Dynamic fee to determine what we consider min/max for
|
|
|
|
|
// timely confirmation.
|
|
|
|
|
maxFee := 2 * ourIdealFee
|
|
|
|
|
minFee := ourIdealFee / 2
|
|
|
|
|
|
|
|
|
|
// If we didn't propose a fee before, just use our ideal fee value for
|
|
|
|
|
// the average calculation.
|
|
|
|
|
if lastSentFee == 0 {
|
|
|
|
|
lastSentFee = ourIdealFee
|
|
|
|
|
}
|
|
|
|
|
avgFee := (lastSentFee + peerFee) / 2
|
|
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
case peerFee <= maxFee && peerFee >= minFee:
|
|
|
|
|
// Peer fee is in the accepted range.
|
|
|
|
|
return peerFee
|
|
|
|
|
case avgFee <= maxFee && avgFee >= minFee:
|
|
|
|
|
// The peer's fee is not in the accepted range, but the average
|
|
|
|
|
// fee is.
|
|
|
|
|
return avgFee
|
|
|
|
|
case avgFee > maxFee:
|
|
|
|
|
// TODO(halseth): We must ensure fee is not higher than the
|
|
|
|
|
// current fee on the commitment transaction.
|
|
|
|
|
|
|
|
|
|
// We cannot accept the average fee, as it is more than twice
|
|
|
|
|
// our own estimate. Set our proposed to the maximum we can
|
|
|
|
|
// accept.
|
|
|
|
|
return maxFee
|
|
|
|
|
default:
|
|
|
|
|
// Cannot accept the average, as we consider it too low.
|
|
|
|
|
return minFee
|
|
|
|
|
}
|
2017-03-25 04:26:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// handleClosingSigned is called when the a ClosingSigned message is received
|
|
|
|
|
// from the peer. If we are the initiator in the shutdown procedure, localReq
|
|
|
|
|
// should be set to the local close request. If we are the responder, it should
|
|
|
|
|
// be set to nil.
|
|
|
|
|
//
|
|
|
|
|
// This method sends the necessary ClosingSigned message to continue fee
|
|
|
|
|
// negotiation, and in case we agreed on a fee completes the channel close
|
|
|
|
|
// transaction, and then broadcasts it. It also performs channel cleanup (and
|
|
|
|
|
// reports status back to the caller if this was a local shutdown request).
|
|
|
|
|
//
|
|
|
|
|
// It returns the signature and the proposed fee included in the ClosingSigned
|
|
|
|
|
// sent to the peer.
|
2017-03-25 04:26:09 +03:00
|
|
|
|
//
|
|
|
|
|
// Following the broadcast, both the initiator and responder in the channel
|
|
|
|
|
// closure workflow should watch the blockchain for a confirmation of the
|
|
|
|
|
// closing transaction before considering the channel terminated. In the case
|
|
|
|
|
// of an unresponsive remote party, the initiator can either choose to execute
|
|
|
|
|
// a force closure, or backoff for a period of time, and retry the cooperative
|
|
|
|
|
// closure.
|
2017-07-14 22:05:55 +03:00
|
|
|
|
func (p *peer) handleClosingSigned(localReq *htlcswitch.ChanClose,
|
|
|
|
|
msg *lnwire.ClosingSigned, deliveryScripts *closingScripts,
|
|
|
|
|
lastSig []byte, lastFee uint64) ([]byte, uint64) {
|
2017-08-22 09:54:10 +03:00
|
|
|
|
|
2017-07-14 22:05:55 +03:00
|
|
|
|
chanID := msg.ChannelID
|
2017-03-25 04:26:09 +03:00
|
|
|
|
p.activeChanMtx.RLock()
|
2017-05-24 01:21:35 +03:00
|
|
|
|
channel, ok := p.activeChannels[chanID]
|
2017-03-25 04:26:09 +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-07-14 22:05:55 +03:00
|
|
|
|
if localReq != nil {
|
|
|
|
|
localReq.Err <- err
|
|
|
|
|
}
|
|
|
|
|
return nil, 0
|
2017-03-25 04:26:09 +03:00
|
|
|
|
}
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// We now consider the fee proposed by the peer, together with the fee
|
|
|
|
|
// we last proposed (if any). This method will in case more fee
|
|
|
|
|
// negotiation is necessary send a new ClosingSigned message to the peer
|
|
|
|
|
// with our new proposed fee. In case we can agree on a fee, it will
|
|
|
|
|
// assemble the close transaction, and we can go on to broadcasting it.
|
|
|
|
|
closeTx, ourSig, ourFee, err := p.negotiateFeeAndCreateCloseTx(channel,
|
|
|
|
|
msg, deliveryScripts, lastSig, lastFee)
|
2016-09-12 22:42:26 +03:00
|
|
|
|
if err != nil {
|
2017-07-14 22:05:55 +03:00
|
|
|
|
if localReq != nil {
|
|
|
|
|
localReq.Err <- err
|
|
|
|
|
}
|
|
|
|
|
return nil, 0
|
2017-03-25 04:26:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// If closeTx == nil it means that we did not agree on a fee, but we
|
|
|
|
|
// proposed a new fee to the peer. Return the signature used for this
|
|
|
|
|
// new proposal, and the fee we proposed, for use when we get a reponse.
|
|
|
|
|
if closeTx == nil {
|
|
|
|
|
return ourSig, ourFee
|
2017-05-24 01:26:38 +03:00
|
|
|
|
}
|
2017-03-25 04:26:09 +03:00
|
|
|
|
|
2017-08-18 22:16:20 +03:00
|
|
|
|
chanPoint := channel.ChannelPoint()
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case p.server.breachArbiter.settledContracts <- chanPoint:
|
|
|
|
|
case <-p.server.quit:
|
|
|
|
|
return nil, 0
|
|
|
|
|
case <-p.quit:
|
|
|
|
|
return nil, 0
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// We agreed on a fee, and we can broadcast the closure transaction to
|
|
|
|
|
// the network.
|
2017-03-25 04:26:09 +03:00
|
|
|
|
peerLog.Infof("Broadcasting cooperative close tx: %v",
|
|
|
|
|
newLogClosure(func() string {
|
|
|
|
|
return spew.Sdump(closeTx)
|
|
|
|
|
}))
|
2017-07-14 22:05:55 +03:00
|
|
|
|
|
2017-06-06 05:40:43 +03:00
|
|
|
|
if err := p.server.cc.wallet.PublishTransaction(closeTx); err != nil {
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// TODO(halseth): Add relevant error types to the
|
|
|
|
|
// WalletController interface as this is quite fragile.
|
|
|
|
|
if strings.Contains(err.Error(), "already exists") ||
|
|
|
|
|
strings.Contains(err.Error(), "already have") {
|
|
|
|
|
peerLog.Infof("channel close tx from ChannelPoint(%v) "+
|
|
|
|
|
" already exist, probably broadcasted by peer: %v",
|
|
|
|
|
chanPoint, err)
|
|
|
|
|
} else {
|
|
|
|
|
peerLog.Errorf("channel close tx from ChannelPoint(%v) "+
|
|
|
|
|
" rejected: %v", chanPoint, err)
|
|
|
|
|
|
|
|
|
|
// TODO(roasbeef): send ErrorGeneric to other side
|
|
|
|
|
return nil, 0
|
|
|
|
|
}
|
2016-09-12 22:42:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-05 02:03:47 +03:00
|
|
|
|
// Once we've completed the cooperative channel closure, we'll wipe the
|
|
|
|
|
// channel so we reject any incoming forward or payment requests via
|
|
|
|
|
// this channel.
|
2017-08-22 09:54:53 +03:00
|
|
|
|
select {
|
|
|
|
|
case p.server.breachArbiter.settledContracts <- chanPoint:
|
|
|
|
|
case <-p.server.quit:
|
|
|
|
|
return nil, 0
|
|
|
|
|
}
|
2017-05-02 23:04:58 +03:00
|
|
|
|
if err := p.WipeChannel(channel); err != nil {
|
2017-07-14 22:05:55 +03:00
|
|
|
|
if localReq != nil {
|
|
|
|
|
localReq.Err <- err
|
|
|
|
|
}
|
|
|
|
|
return nil, 0
|
2017-05-05 02:03:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-17 01:11:02 +03:00
|
|
|
|
// TODO(roasbeef): also add closure height to summary
|
|
|
|
|
|
2017-05-05 02:03:47 +03:00
|
|
|
|
// Clear out the current channel state, marking the channel as being
|
|
|
|
|
// closed within the database.
|
2017-05-24 01:26:38 +03:00
|
|
|
|
closingTxid := closeTx.TxHash()
|
2017-05-05 02:03:47 +03:00
|
|
|
|
chanInfo := channel.StateSnapshot()
|
|
|
|
|
closeSummary := &channeldb.ChannelCloseSummary{
|
2017-07-14 22:05:55 +03:00
|
|
|
|
ChanPoint: *chanPoint,
|
2017-03-25 04:26:09 +03:00
|
|
|
|
ClosingTXID: closingTxid,
|
2017-05-15 05:23:51 +03:00
|
|
|
|
RemotePub: &chanInfo.RemoteIdentity,
|
|
|
|
|
Capacity: chanInfo.Capacity,
|
2017-08-22 09:25:41 +03:00
|
|
|
|
SettledBalance: chanInfo.LocalBalance.ToSatoshis(),
|
2017-05-15 05:23:51 +03:00
|
|
|
|
CloseType: channeldb.CooperativeClose,
|
|
|
|
|
IsPending: true,
|
2017-05-05 02:03:47 +03:00
|
|
|
|
}
|
|
|
|
|
if err := channel.DeleteState(closeSummary); err != nil {
|
2017-07-14 22:05:55 +03:00
|
|
|
|
if localReq != nil {
|
|
|
|
|
localReq.Err <- err
|
|
|
|
|
}
|
|
|
|
|
return nil, 0
|
2017-05-05 02:03:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-14 22:05:55 +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 localReq != nil {
|
|
|
|
|
localReq.Updates <- &lnrpc.CloseStatusUpdate{
|
|
|
|
|
Update: &lnrpc.CloseStatusUpdate_ClosePending{
|
|
|
|
|
ClosePending: &lnrpc.PendingUpdate{
|
|
|
|
|
Txid: closingTxid[:],
|
|
|
|
|
},
|
2016-08-31 02:52:53 +03:00
|
|
|
|
},
|
2017-07-14 22:05:55 +03:00
|
|
|
|
}
|
2016-08-31 02:52:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-18 21:55:25 +03:00
|
|
|
|
_, bestHeight, err := p.server.cc.chainIO.GetBestBlock()
|
2017-05-11 03:27:05 +03:00
|
|
|
|
if err != nil {
|
2017-07-14 22:05:55 +03:00
|
|
|
|
if localReq != nil {
|
|
|
|
|
localReq.Err <- err
|
|
|
|
|
}
|
|
|
|
|
return nil, 0
|
2017-05-11 03:27:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
// Finally, launch a goroutine which will request to be notified by the
|
|
|
|
|
// ChainNotifier once the closure transaction obtains a single
|
|
|
|
|
// confirmation.
|
2017-05-18 21:55:25 +03:00
|
|
|
|
notifier := p.server.cc.chainNotifier
|
2017-07-14 22:05:55 +03:00
|
|
|
|
|
|
|
|
|
// If any error happens during waitForChanToClose, forard it to
|
|
|
|
|
// localReq. If this channel closure is not locally initiated, localReq
|
|
|
|
|
// will be nil, so just ignore the error.
|
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
if localReq != nil {
|
|
|
|
|
errChan = localReq.Err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go waitForChanToClose(uint32(bestHeight), notifier, errChan,
|
|
|
|
|
chanPoint, &closingTxid, func() {
|
|
|
|
|
|
2017-05-11 03:27:05 +03:00
|
|
|
|
// First, we'll mark the database as being fully closed
|
|
|
|
|
// so we'll no longer watch for its ultimate closure
|
|
|
|
|
// upon startup.
|
2017-07-14 22:05:55 +03:00
|
|
|
|
err := p.server.chanDB.MarkChanFullyClosed(chanPoint)
|
2017-05-11 03:27:05 +03:00
|
|
|
|
if err != nil {
|
2017-07-14 22:05:55 +03:00
|
|
|
|
if localReq != nil {
|
|
|
|
|
localReq.Err <- err
|
|
|
|
|
}
|
2017-05-11 03:27:05 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Respond to the local subsystem which requested the
|
|
|
|
|
// channel closure.
|
2017-07-14 22:05:55 +03:00
|
|
|
|
if localReq != nil {
|
|
|
|
|
localReq.Updates <- &lnrpc.CloseStatusUpdate{
|
|
|
|
|
Update: &lnrpc.CloseStatusUpdate_ChanClose{
|
|
|
|
|
ChanClose: &lnrpc.ChannelCloseUpdate{
|
|
|
|
|
ClosingTxid: closingTxid[:],
|
|
|
|
|
Success: true,
|
|
|
|
|
},
|
2017-05-11 03:27:05 +03:00
|
|
|
|
},
|
2017-07-14 22:05:55 +03:00
|
|
|
|
}
|
2017-05-11 03:27:05 +03:00
|
|
|
|
}
|
|
|
|
|
})
|
2017-07-14 22:05:55 +03:00
|
|
|
|
return nil, 0
|
2016-06-21 22:32:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// negotiateFeeAndCreateCloseTx takes into consideration the closing transaction
|
|
|
|
|
// fee proposed by the remote peer in the ClosingSigned message and our
|
|
|
|
|
// previously proposed fee (set to 0 if no previous), and continues the fee
|
|
|
|
|
// negotiation it process. In case the peer agreed on the same fee as we
|
|
|
|
|
// previously sent, it will assemble the close transaction and broadcast it. In
|
|
|
|
|
// case the peer propose a fee different from our previous proposal, but that
|
|
|
|
|
// can be accepted, a ClosingSigned message with the accepted fee is sent,
|
|
|
|
|
// before the closing transaction is broadcasted. In the case where we cannot
|
|
|
|
|
// accept the peer's proposed fee, a new fee proposal will be sent.
|
|
|
|
|
//
|
|
|
|
|
// TODO(halseth): In the case where we cannot accept the fee, and we cannot
|
|
|
|
|
// make more proposals, this method should return an error, and we should fail
|
|
|
|
|
// the channel.
|
|
|
|
|
func (p *peer) negotiateFeeAndCreateCloseTx(channel *lnwallet.LightningChannel,
|
|
|
|
|
msg *lnwire.ClosingSigned, deliveryScripts *closingScripts, ourSig []byte,
|
|
|
|
|
ourFeeProp uint64) (*wire.MsgTx, []byte, uint64, error) {
|
|
|
|
|
|
|
|
|
|
peerFeeProposal := msg.FeeSatoshis
|
|
|
|
|
|
|
|
|
|
// If the fee proposed by the peer is different from what we proposed
|
|
|
|
|
// before (or we did not propose anything yet), we must check if we can
|
|
|
|
|
// accept the proposal, or if we should negotiate.
|
|
|
|
|
if peerFeeProposal != ourFeeProp {
|
|
|
|
|
// The peer has suggested a different fee from what we proposed.
|
|
|
|
|
// Let's calculate if this one is tolerable.
|
|
|
|
|
ourIdealFeeRate := p.server.cc.feeEstimator.
|
|
|
|
|
EstimateFeePerWeight(1) * 1000
|
|
|
|
|
ourIdealFee := channel.CalcFee(ourIdealFeeRate)
|
|
|
|
|
fee := calculateCompromiseFee(ourIdealFee, ourFeeProp,
|
|
|
|
|
peerFeeProposal)
|
|
|
|
|
|
|
|
|
|
// Our new proposed fee must be strictly between what we
|
|
|
|
|
// proposed before and what the peer proposed.
|
|
|
|
|
isAcceptable := false
|
|
|
|
|
if fee < peerFeeProposal && fee > ourFeeProp {
|
|
|
|
|
isAcceptable = true
|
|
|
|
|
}
|
|
|
|
|
if fee < ourFeeProp && fee > peerFeeProposal {
|
|
|
|
|
isAcceptable = true
|
|
|
|
|
}
|
2017-07-31 00:21:21 +03:00
|
|
|
|
|
2017-07-14 22:05:55 +03:00
|
|
|
|
if !isAcceptable {
|
|
|
|
|
// TODO(halseth): fail channel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Since the compromise fee is different from the fee we last
|
|
|
|
|
// proposed, we must update our proposal.
|
|
|
|
|
|
|
|
|
|
// Create a new close proposal with the compromise fee, and
|
|
|
|
|
// send this to the peer.
|
|
|
|
|
closeSig, proposedFee, err := channel.CreateCloseProposal(fee,
|
|
|
|
|
deliveryScripts.localScript, deliveryScripts.remoteScript)
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("unable to create close proposal: %v",
|
|
|
|
|
err)
|
|
|
|
|
return nil, nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
parsedSig, err := btcec.ParseSignature(closeSig, btcec.S256())
|
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("unable to parse signature: %v", err)
|
|
|
|
|
return nil, nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
closingSigned := lnwire.NewClosingSigned(msg.ChannelID,
|
|
|
|
|
proposedFee, parsedSig)
|
|
|
|
|
p.queueMsg(closingSigned, nil)
|
|
|
|
|
|
|
|
|
|
// If the compromise fee was different from what the peer
|
|
|
|
|
// proposed, then we must return and wait for an answer, if not
|
|
|
|
|
// we can go on to complete the close transaction.
|
|
|
|
|
if fee != peerFeeProposal {
|
|
|
|
|
return nil, closeSig, proposedFee, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We accept the fee proposed by the peer, so prepare our
|
|
|
|
|
// signature to complete the close transaction.
|
|
|
|
|
ourSig = append(closeSig, byte(txscript.SigHashAll))
|
2017-05-24 01:21:35 +03:00
|
|
|
|
}
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
2017-07-14 22:05:55 +03:00
|
|
|
|
// We agreed on a fee, and we have the peer's signature for this fee,
|
|
|
|
|
// so we can assemble the close tx.
|
|
|
|
|
peerSig := append(msg.Signature.Serialize(), byte(txscript.SigHashAll))
|
2017-04-17 01:41:11 +03:00
|
|
|
|
chanPoint := channel.ChannelPoint()
|
2017-07-14 22:05:55 +03:00
|
|
|
|
closeTx, err := channel.CompleteCooperativeClose(ourSig, peerSig,
|
2017-07-31 00:21:21 +03:00
|
|
|
|
deliveryScripts.localScript, deliveryScripts.remoteScript,
|
2017-07-14 22:05:55 +03:00
|
|
|
|
peerFeeProposal)
|
2016-06-21 22:32:32 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
peerLog.Errorf("unable to complete cooperative "+
|
|
|
|
|
"close for ChannelPoint(%v): %v",
|
|
|
|
|
chanPoint, err)
|
|
|
|
|
// TODO(roasbeef): send ErrorGeneric to other side
|
2017-07-14 22:05:55 +03:00
|
|
|
|
return nil, nil, 0, err
|
2017-05-11 03:27:05 +03:00
|
|
|
|
}
|
2017-07-14 22:05:55 +03:00
|
|
|
|
return closeTx, nil, 0, nil
|
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,
|
|
|
|
|
closingTxID *chainhash.Hash, cb func()) {
|
|
|
|
|
|
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
|
2017-05-11 03:27:05 +03:00
|
|
|
|
confNtfn, err := notifier.RegisterConfirmationsNtfn(closingTxID, 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
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// sendShutdown handles the creation and sending of the Shutdown messages sent
|
|
|
|
|
// between peers to initiate the cooperative channel close workflow. In
|
2017-03-25 04:26:09 +03:00
|
|
|
|
// addition, sendShutdown also signals to the HTLC switch to stop accepting
|
|
|
|
|
// HTLCs for the specified channel.
|
2017-07-31 00:21:21 +03:00
|
|
|
|
func (p *peer) sendShutdown(channel *lnwallet.LightningChannel,
|
|
|
|
|
deliveryScript []byte) error {
|
|
|
|
|
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// In order to construct the shutdown message, we'll need to
|
|
|
|
|
// reconstruct the channelID, and the current set delivery script for
|
|
|
|
|
// the channel closure.
|
2017-03-25 04:26:09 +03:00
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(channel.ChannelPoint())
|
|
|
|
|
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// With both items constructed we'll now send the shutdown message for
|
|
|
|
|
// this particular channel, advertising a shutdown request to our
|
|
|
|
|
// desired closing script.
|
2017-07-31 00:21:21 +03:00
|
|
|
|
shutdown := lnwire.NewShutdown(chanID, deliveryScript)
|
2017-03-25 04:26:09 +03:00
|
|
|
|
p.queueMsg(shutdown, nil)
|
|
|
|
|
|
2017-05-24 01:26:38 +03:00
|
|
|
|
// Finally, we'll unregister the link from the switch in order to
|
|
|
|
|
// Prevent the HTLC switch from receiving additional HTLCs for this
|
|
|
|
|
// channel.
|
2017-05-02 23:04:58 +03:00
|
|
|
|
p.server.htlcSwitch.RemoveLink(chanID)
|
2017-03-25 04:26:09 +03:00
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-02 23:04:58 +03:00
|
|
|
|
// WipeChannel removes the passed channel from all indexes associated with the
|
2016-06-23 08:19:24 +03:00
|
|
|
|
// peer, and deletes the channel from the database.
|
2017-05-02 23:04:58 +03:00
|
|
|
|
func (p *peer) WipeChannel(channel *lnwallet.LightningChannel) error {
|
2017-05-15 05:21:17 +03:00
|
|
|
|
channel.Stop()
|
|
|
|
|
|
2017-04-17 01:41:11 +03:00
|
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(channel.ChannelPoint())
|
2016-06-23 08:19:24 +03:00
|
|
|
|
|
2016-11-18 05:43:33 +03:00
|
|
|
|
p.activeChanMtx.Lock()
|
2017-04-17 01:41:11 +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
|
|
|
|
|
2016-07-10 02:41:06 +03:00
|
|
|
|
// Instruct the Htlc Switch to close this link as the channel is no
|
|
|
|
|
// longer active.
|
2017-05-02 23:04:58 +03:00
|
|
|
|
if err := p.server.htlcSwitch.RemoveLink(chanID); err != nil {
|
|
|
|
|
if err == htlcswitch.ErrChannelLinkNotFound {
|
|
|
|
|
peerLog.Warnf("unable remove channel link with "+
|
|
|
|
|
"ChannelPoint(%v): %v", chanID, err)
|
|
|
|
|
return nil
|
2016-06-21 22:32:32 +03:00
|
|
|
|
}
|
2017-05-02 23:04:58 +03:00
|
|
|
|
return err
|
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 {
|
|
|
|
|
localSharedFeatures, err := p.server.localFeatures.Compare(msg.LocalFeatures)
|
|
|
|
|
if err != nil {
|
2017-04-24 05:24:28 +03:00
|
|
|
|
err := errors.Errorf("can't compare remote and local feature "+
|
2017-02-16 15:39:38 +03:00
|
|
|
|
"vectors: %v", err)
|
|
|
|
|
peerLog.Error(err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
p.localSharedFeatures = localSharedFeatures
|
|
|
|
|
|
|
|
|
|
globalSharedFeatures, err := p.server.globalFeatures.Compare(msg.GlobalFeatures)
|
|
|
|
|
if err != nil {
|
2017-04-24 05:24:28 +03:00
|
|
|
|
err := errors.Errorf("can't compare remote and global feature "+
|
2017-02-16 15:39:38 +03:00
|
|
|
|
"vectors: %v", err)
|
|
|
|
|
peerLog.Error(err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
p.globalSharedFeatures = globalSharedFeatures
|
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
|
p.server.globalFeatures,
|
|
|
|
|
p.server.localFeatures,
|
|
|
|
|
)
|
|
|
|
|
|
2017-03-17 05:45:10 +03:00
|
|
|
|
return p.writeMessage(msg)
|
2017-02-16 15:39:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-17 01:11:02 +03:00
|
|
|
|
// SendMessage queues a message for sending to the target peer.
|
2017-05-02 23:04:58 +03:00
|
|
|
|
func (p *peer) SendMessage(msg lnwire.Message) error {
|
|
|
|
|
p.queueMsg(msg, nil)
|
2017-02-21 05:10:05 +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.
|
|
|
|
|
func (p *peer) PubKey() [33]byte {
|
|
|
|
|
return p.pubKeyBytes
|
2016-07-13 03:45:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
|
// TODO(roasbeef): make all start/stop mutexes a CAS
|
2017-06-29 16:52:55 +03:00
|
|
|
|
|
|
|
|
|
// createGetLastUpdate returns the handler which serve as a source of the last
|
|
|
|
|
// update of the channel in a form of lnwire update message.
|
|
|
|
|
func createGetLastUpdate(router *routing.ChannelRouter,
|
|
|
|
|
pubKey [33]byte, chanID lnwire.ShortChannelID) func() (*lnwire.ChannelUpdate,
|
|
|
|
|
error) {
|
|
|
|
|
|
|
|
|
|
return func() (*lnwire.ChannelUpdate, error) {
|
2017-08-22 09:57:52 +03:00
|
|
|
|
info, edge1, edge2, err := router.GetChannelByID(chanID)
|
2017-06-29 16:52:55 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if edge1 == nil || edge2 == nil {
|
|
|
|
|
return nil, errors.Errorf("unable to find "+
|
|
|
|
|
"channel by ShortChannelID(%v)", chanID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var local *channeldb.ChannelEdgePolicy
|
|
|
|
|
if bytes.Compare(edge1.Node.PubKey.SerializeCompressed(),
|
|
|
|
|
pubKey[:]) == 0 {
|
|
|
|
|
local = edge2
|
|
|
|
|
} else {
|
|
|
|
|
local = edge1
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-22 09:57:52 +03:00
|
|
|
|
update := &lnwire.ChannelUpdate{
|
2017-06-29 16:52:55 +03:00
|
|
|
|
Signature: local.Signature,
|
2017-08-22 09:57:52 +03:00
|
|
|
|
ChainHash: info.ChainHash,
|
2017-06-29 16:52:55 +03:00
|
|
|
|
ShortChannelID: lnwire.NewShortChanIDFromInt(local.ChannelID),
|
2017-08-22 09:57:52 +03:00
|
|
|
|
Timestamp: uint32(local.LastUpdate.Unix()),
|
2017-06-29 16:52:55 +03:00
|
|
|
|
Flags: local.Flags,
|
|
|
|
|
TimeLockDelta: local.TimeLockDelta,
|
2017-08-22 09:25:41 +03:00
|
|
|
|
HtlcMinimumMsat: local.MinHTLC,
|
2017-06-29 16:52:55 +03:00
|
|
|
|
BaseFee: uint32(local.FeeBaseMSat),
|
|
|
|
|
FeeRate: uint32(local.FeeProportionalMillionths),
|
2017-08-22 09:57:52 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hswcLog.Debugf("Sending latest channel_update: %v",
|
|
|
|
|
spew.Sdump(update))
|
|
|
|
|
|
|
|
|
|
return update, nil
|
2017-06-29 16:52:55 +03:00
|
|
|
|
}
|
|
|
|
|
}
|