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.
This commit is contained in:
Johan T. Halseth 2018-09-26 11:12:57 +02:00
parent addb4aed89
commit b712b861f8
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
2 changed files with 30 additions and 14 deletions

@ -5,7 +5,7 @@ import (
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
) )
@ -19,7 +19,7 @@ type Peer interface {
// AddNewChannel adds a new channel to the peer. The channel should fail // AddNewChannel adds a new channel to the peer. The channel should fail
// to be added if the cancel channel is closed. // 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 // WipeChannel removes the channel uniquely identified by its channel
// point from all indexes associated with the peer. // point from all indexes associated with the peer.

40
peer.go

@ -59,11 +59,11 @@ type outgoingMsg struct {
errChan chan error // MUST be buffered. errChan chan error // MUST be buffered.
} }
// newChannelMsg packages an lnwallet.LightningChannel with a channel that // newChannelMsg packages a channeldb.OpenChannel with a channel that allows
// allows the receiver of the request to report when the funding transaction // the receiver of the request to report when the funding transaction has been
// has been confirmed and the channel creation process completed. // confirmed and the channel creation process completed.
type newChannelMsg struct { type newChannelMsg struct {
channel *lnwallet.LightningChannel channel *channeldb.OpenChannel
err chan error err chan error
} }
@ -1522,9 +1522,9 @@ out:
// funding workflow. We'll initialize the necessary local // funding workflow. We'll initialize the necessary local
// state, and notify the htlc switch of a new link. // state, and notify the htlc switch of a new link.
case newChanReq := <-p.newChannels: case newChanReq := <-p.newChannels:
chanPoint := newChanReq.channel.ChannelPoint()
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
newChan := newChanReq.channel newChan := newChanReq.channel
chanPoint := &newChan.FundingOutpoint
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
// Make sure this channel is not already active. // Make sure this channel is not already active.
p.activeChanMtx.Lock() p.activeChanMtx.Lock()
@ -1534,7 +1534,6 @@ out:
p.activeChanMtx.Unlock() p.activeChanMtx.Unlock()
close(newChanReq.err) close(newChanReq.err)
newChanReq.channel.Stop()
// If we're being sent a new channel, and our // If we're being sent a new channel, and our
// existing channel doesn't have the next // existing channel doesn't have the next
@ -1548,7 +1547,7 @@ out:
"FundingLocked for ChannelPoint(%v)", "FundingLocked for ChannelPoint(%v)",
chanPoint) chanPoint)
nextRevoke := newChan.RemoteNextRevocation() nextRevoke := newChan.RemoteNextRevocation
err := currentChan.InitNextRevocation(nextRevoke) err := currentChan.InitNextRevocation(nextRevoke)
if err != nil { if err != nil {
peerLog.Errorf("unable to init chan "+ peerLog.Errorf("unable to init chan "+
@ -1562,7 +1561,21 @@ out:
// If not already active, we'll add this channel to the // If not already active, we'll add this channel to the
// set of active channels, so we can look it up later // set of active channels, so we can look it up later
// easily according to its channel ID. // 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() p.activeChanMtx.Unlock()
peerLog.Infof("New channel active ChannelPoint(%v) "+ peerLog.Infof("New channel active ChannelPoint(%v) "+
@ -1578,6 +1591,7 @@ out:
"block: %v", err) "block: %v", err)
peerLog.Errorf(err.Error()) peerLog.Errorf(err.Error())
lnChan.Stop()
newChanReq.err <- err newChanReq.err <- err
continue continue
} }
@ -1589,6 +1603,7 @@ out:
"chain events: %v", err) "chain events: %v", err)
peerLog.Errorf(err.Error()) peerLog.Errorf(err.Error())
lnChan.Stop()
newChanReq.err <- err newChanReq.err <- err
continue continue
} }
@ -1598,7 +1613,7 @@ out:
// forwarded. For fees we'll use the default values, as // forwarded. For fees we'll use the default values, as
// they currently are always set to the default values // they currently are always set to the default values
// at initial channel creation. // at initial channel creation.
fwdMinHtlc := newChan.FwdMinHtlc() fwdMinHtlc := lnChan.FwdMinHtlc()
defaultPolicy := p.server.cc.routingPolicy defaultPolicy := p.server.cc.routingPolicy
forwardingPolicy := &htlcswitch.ForwardingPolicy{ forwardingPolicy := &htlcswitch.ForwardingPolicy{
MinHTLC: fwdMinHtlc, MinHTLC: fwdMinHtlc,
@ -1609,7 +1624,7 @@ out:
// Create the link and add it to the switch. // Create the link and add it to the switch.
err = p.addLink( err = p.addLink(
chanPoint, newChan, forwardingPolicy, chanPoint, lnChan, forwardingPolicy,
chainEvents, currentHeight, false, chainEvents, currentHeight, false,
) )
if err != nil { if err != nil {
@ -1618,6 +1633,7 @@ out:
p.PubKey()) p.PubKey())
peerLog.Errorf(err.Error()) peerLog.Errorf(err.Error())
lnChan.Stop()
newChanReq.err <- err newChanReq.err <- err
continue continue
} }
@ -2182,7 +2198,7 @@ func (p *peer) Address() net.Addr {
// added if the cancel channel is closed. // added if the cancel channel is closed.
// //
// NOTE: Part of the lnpeer.Peer interface. // 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 { cancel <-chan struct{}) error {
errChan := make(chan error, 1) errChan := make(chan error, 1)