Skip to content

Commit

Permalink
peer: make AddNewChannel take OpenChannel
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
halseth committed Sep 28, 2018
1 parent addb4ae commit b712b86
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lnpeer/peer.go
Expand Up @@ -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"
)

Expand All @@ -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.
Expand Down
40 changes: 28 additions & 12 deletions peer.go
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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 "+
Expand All @@ -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) "+
Expand All @@ -1578,6 +1591,7 @@ out:
"block: %v", err)
peerLog.Errorf(err.Error())

lnChan.Stop()
newChanReq.err <- err
continue
}
Expand All @@ -1589,6 +1603,7 @@ out:
"chain events: %v", err)
peerLog.Errorf(err.Error())

lnChan.Stop()
newChanReq.err <- err
continue
}
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -1618,6 +1633,7 @@ out:
p.PubKey())
peerLog.Errorf(err.Error())

lnChan.Stop()
newChanReq.err <- err
continue
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b712b86

Please sign in to comment.