From b712b861f89de2a6c4ca8c7ea5c68fcf1961fe85 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Wed, 26 Sep 2018 11:12:57 +0200 Subject: [PATCH] peer: make AddNewChannel take OpenChannel This commit makes the AddNewChannel expect a OpenChannel instead of a LightningChannel struct. This moves the responsibility for starting the LightningChannel from the fundingmanager to the peer, and we can defer the channel restoration until we know that the channel is not already active. --- lnpeer/peer.go | 4 ++-- peer.go | 40 ++++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/lnpeer/peer.go b/lnpeer/peer.go index fb0e743c5..34675aa5b 100644 --- a/lnpeer/peer.go +++ b/lnpeer/peer.go @@ -5,7 +5,7 @@ import ( "github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/wire" - "github.com/lightningnetwork/lnd/lnwallet" + "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/lnwire" ) @@ -19,7 +19,7 @@ type Peer interface { // AddNewChannel adds a new channel to the peer. The channel should fail // to be added if the cancel channel is closed. - AddNewChannel(channel *lnwallet.LightningChannel, cancel <-chan struct{}) error + AddNewChannel(channel *channeldb.OpenChannel, cancel <-chan struct{}) error // WipeChannel removes the channel uniquely identified by its channel // point from all indexes associated with the peer. diff --git a/peer.go b/peer.go index 3dfa639f7..563bb7899 100644 --- a/peer.go +++ b/peer.go @@ -59,11 +59,11 @@ type outgoingMsg struct { errChan chan error // MUST be buffered. } -// newChannelMsg packages an 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. +// 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. type newChannelMsg struct { - channel *lnwallet.LightningChannel + channel *channeldb.OpenChannel err chan error } @@ -1522,9 +1522,9 @@ out: // funding workflow. We'll initialize the necessary local // state, and notify the htlc switch of a new link. case newChanReq := <-p.newChannels: - chanPoint := newChanReq.channel.ChannelPoint() - chanID := lnwire.NewChanIDFromOutPoint(chanPoint) newChan := newChanReq.channel + chanPoint := &newChan.FundingOutpoint + chanID := lnwire.NewChanIDFromOutPoint(chanPoint) // Make sure this channel is not already active. p.activeChanMtx.Lock() @@ -1534,7 +1534,6 @@ out: p.activeChanMtx.Unlock() close(newChanReq.err) - newChanReq.channel.Stop() // If we're being sent a new channel, and our // existing channel doesn't have the next @@ -1548,7 +1547,7 @@ out: "FundingLocked for ChannelPoint(%v)", chanPoint) - nextRevoke := newChan.RemoteNextRevocation() + nextRevoke := newChan.RemoteNextRevocation err := currentChan.InitNextRevocation(nextRevoke) if err != nil { peerLog.Errorf("unable to init chan "+ @@ -1562,7 +1561,21 @@ out: // 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. - p.activeChannels[chanID] = newChan + lnChan, err := lnwallet.NewLightningChannel( + p.server.cc.signer, p.server.witnessBeacon, + newChan, + ) + 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 p.activeChanMtx.Unlock() peerLog.Infof("New channel active ChannelPoint(%v) "+ @@ -1578,6 +1591,7 @@ out: "block: %v", err) peerLog.Errorf(err.Error()) + lnChan.Stop() newChanReq.err <- err continue } @@ -1589,6 +1603,7 @@ out: "chain events: %v", err) peerLog.Errorf(err.Error()) + lnChan.Stop() newChanReq.err <- err continue } @@ -1598,7 +1613,7 @@ out: // forwarded. For fees we'll use the default values, as // they currently are always set to the default values // at initial channel creation. - fwdMinHtlc := newChan.FwdMinHtlc() + fwdMinHtlc := lnChan.FwdMinHtlc() defaultPolicy := p.server.cc.routingPolicy forwardingPolicy := &htlcswitch.ForwardingPolicy{ MinHTLC: fwdMinHtlc, @@ -1609,7 +1624,7 @@ out: // Create the link and add it to the switch. err = p.addLink( - chanPoint, newChan, forwardingPolicy, + chanPoint, lnChan, forwardingPolicy, chainEvents, currentHeight, false, ) if err != nil { @@ -1618,6 +1633,7 @@ out: p.PubKey()) peerLog.Errorf(err.Error()) + lnChan.Stop() newChanReq.err <- err continue } @@ -2182,7 +2198,7 @@ func (p *peer) Address() net.Addr { // added if the cancel channel is closed. // // NOTE: Part of the lnpeer.Peer interface. -func (p *peer) AddNewChannel(channel *lnwallet.LightningChannel, +func (p *peer) AddNewChannel(channel *channeldb.OpenChannel, cancel <-chan struct{}) error { errChan := make(chan error, 1)