Skip to content

Commit

Permalink
Funding: fixed logging race in tests, new handleFundingConfirmation func
Browse files Browse the repository at this point in the history
This commit fixes a race condition that occurs with the logger during
tests. This commit also includes a new wrapper function that creates
a new LightningChannel object, calls sendFundingLocked, and calls
sendChannelAnnouncement.
  • Loading branch information
nsa committed Sep 17, 2017
1 parent 65d4936 commit adda49b
Showing 1 changed file with 68 additions and 108 deletions.
176 changes: 68 additions & 108 deletions fundingmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,28 +401,12 @@ func (f *fundingManager) Start() error {
}
// Success, funding transaction was confirmed.

// With the channel marked open, we'll create the
// state-machine object which wraps the database
// state.
lnChannel, err := lnwallet.NewLightningChannel(
nil, nil, f.cfg.FeeEstimator, ch)
if err != nil {
fndgLog.Errorf("error creating new "+
"lightning channel: %v", err)
return
}
defer lnChannel.Stop()

err = f.sendFundingLocked(ch, lnChannel, shortChanID)
// Call the function that handles funding
// confirmation.
err := f.handleFundingConfirmation(ch, shortChanID)
if err != nil {
fndgLog.Errorf("Failed to send "+
"fundingLocked: %v", err)
return
}
err = f.sendChannelAnnouncement(ch, lnChannel, shortChanID)
if err != nil {
fndgLog.Errorf("Failed to send channel "+
"announcement: %v", err)
fndgLog.Errorf("failed to handle funding"+
"confirmation: %v", err)
return
}
}
Expand Down Expand Up @@ -470,25 +454,12 @@ func (f *fundingManager) Start() error {
go func() {
defer f.wg.Done()

lnChannel, err := lnwallet.NewLightningChannel(
nil, nil, f.cfg.FeeEstimator, channel)
if err != nil {
fndgLog.Errorf("error creating new "+
"lightning channel: %v", err)
return
}
defer lnChannel.Stop()

err = f.sendFundingLocked(channel, lnChannel, shortChanID)
// Call the function that handles funding
// confirmation.
err := f.handleFundingConfirmation(channel, shortChanID)
if err != nil {
fndgLog.Errorf("Failed to send "+
"fundingLocked: %v", err)
return
}
err = f.sendChannelAnnouncement(channel, lnChannel, shortChanID)
if err != nil {
fndgLog.Errorf("Failed to send channel"+
"announcement: %v", err)
fndgLog.Errorf("failed to handle funding"+
"confirmation: %v", err)
return
}
}()
Expand All @@ -505,6 +476,7 @@ func (f *fundingManager) Start() error {
if err != nil {
fndgLog.Errorf("error creating "+
"lightning channel: %v", err)
return
}
defer lnChannel.Stop()

Expand Down Expand Up @@ -1163,27 +1135,12 @@ func (f *fundingManager) handleFundingCreated(fmsg *fundingCreatedMsg) {
}
// Success, funding transaction was confirmed.

// With the channel marked open, we'll create the
// state-machine object which wraps the database state.
lnChannel, err := lnwallet.NewLightningChannel(nil, nil,
f.cfg.FeeEstimator, completeChan)
// Call the function that handles funding
// confirmation.
err := f.handleFundingConfirmation(completeChan, shortChanID)
if err != nil {
fndgLog.Errorf("error creating new lightning channel:"+
" %v", err)
return
}
defer lnChannel.Stop()

err = f.sendFundingLocked(completeChan, lnChannel, shortChanID)
if err != nil {
fndgLog.Errorf("Failed to send fundingLocked: %v", err)
return
}

err = f.sendChannelAnnouncement(completeChan, lnChannel, shortChanID)
if err != nil {
fndgLog.Errorf("Failed to send channel "+
"announcement: %v", err)
fndgLog.Errorf("failed to handle funding"+
"confirmation: %v", err)
return
}

Expand Down Expand Up @@ -1271,7 +1228,9 @@ func (f *fundingManager) handleFundingSigned(fmsg *fundingSignedMsg) {
},
}

f.wg.Add(1)
go func() {
defer f.wg.Done()
confChan := make(chan *lnwire.ShortChannelID)
cancelChan := make(chan struct{})

Expand All @@ -1295,54 +1254,31 @@ func (f *fundingManager) handleFundingSigned(fmsg *fundingSignedMsg) {
return
}

// In case the fundingManager is stopped or restarted at
// some point during the remaining part of the opening
// process, we must wait for this process to finish
// (either successfully or with some error), before the
// fundingManager can be shut down.
f.wg.Add(1)
go func() {
defer f.wg.Done()
// With the channel marked open, we'll create the state-machine object
// which wraps the database state.
lnChannel, err := lnwallet.NewLightningChannel(nil, nil,
f.cfg.FeeEstimator, completeChan)
if err != nil {
fndgLog.Errorf("error creating new lightning "+
"channel: %v", err)
return
}
defer lnChannel.Stop()

err = f.sendFundingLocked(completeChan, lnChannel, shortChanID)
if err != nil {
fndgLog.Errorf("Failed to send fundingLocked: %v", err)
return
}
err = f.sendChannelAnnouncement(completeChan, lnChannel, shortChanID)
if err != nil {
fndgLog.Errorf("Failed to send channel "+
"announcement: %v", err)
return
}
// Call the function that handles funding
// confirmation.
err := f.handleFundingConfirmation(completeChan, shortChanID)
if err != nil {
fndgLog.Errorf("failed to handle funding"+
"confirmation: %v", err)
return
}
}

// Finally give the caller a final update notifying them that
// the channel is now open.
// TODO(roasbeef): only notify after recv of funding locked?
resCtx.updates <- &lnrpc.OpenStatusUpdate{
Update: &lnrpc.OpenStatusUpdate_ChanOpen{
ChanOpen: &lnrpc.ChannelOpenUpdate{
ChannelPoint: &lnrpc.ChannelPoint{
FundingTxid: fundingPoint.Hash[:],
OutputIndex: fundingPoint.Index,
},
},
// Finally give the caller a final update notifying them that
// the channel is now open.
// TODO(roasbeef): only notify after recv of funding locked?
resCtx.updates <- &lnrpc.OpenStatusUpdate{
Update: &lnrpc.OpenStatusUpdate_ChanOpen{
ChanOpen: &lnrpc.ChannelOpenUpdate{
ChannelPoint: &lnrpc.ChannelPoint{
FundingTxid: fundingPoint.Hash[:],
OutputIndex: fundingPoint.Index,
},
}

f.deleteReservationCtx(peerKey, pendingChanID)
}()
},
},
}

f.deleteReservationCtx(peerKey, pendingChanID)
}()
}

Expand Down Expand Up @@ -1405,8 +1341,6 @@ func (f *fundingManager) waitForFundingWithTimeout(completeChan *channeldb.OpenC
return
case shortChanID, ok := <-waitingConfChan:
if !ok {
fndgLog.Errorf("waiting for funding confirmation" +
"failed")
close(confChan)
return
}
Expand Down Expand Up @@ -1516,6 +1450,33 @@ func (f *fundingManager) waitForFundingConfirmation(completeChan *channeldb.Open
confChan <- &shortChanID
}

// handleFundingConfirmation is a wrapper method for creating a new lightning
// channel, calling sendFundingLocked, and for calling sendChannelAnnouncement.
// This is called when the funding transaction is first confirmed and the channel
// is marked open.
func (f *fundingManager) handleFundingConfirmation(completeChan *channeldb.OpenChannel,
shortChanID *lnwire.ShortChannelID) error {

// We create the state-machine object which wraps the database state
lnChannel, err := lnwallet.NewLightningChannel(nil, nil, f.cfg.FeeEstimator,
completeChan)
if err != nil {
return err
}
defer lnChannel.Stop()

err = f.sendFundingLocked(completeChan, lnChannel, shortChanID)
if err != nil {
return err
}
err = f.sendChannelAnnouncement(completeChan, lnChannel, shortChanID)
if err != nil {
return err
}

return nil
}

// sendFundingLocked creates and sends the fundingLocked message.
// This should be called after the funding transaction has been confirmed,
// and the channelState is 'markedOpen'.
Expand Down Expand Up @@ -1578,8 +1539,7 @@ func (f *fundingManager) sendChannelAnnouncement(completeChan *channeldb.OpenCha
channel.LocalFundingKey, channel.RemoteFundingKey,
*shortChanID, chanID)
if err != nil {
fndgLog.Errorf("channel announcement failed: %v", err)
return err
return fmt.Errorf("channel announcement failed: %v", err)
}

// After the channel is successfully announced from the
Expand Down

0 comments on commit adda49b

Please sign in to comment.