Skip to content

Commit

Permalink
Removed pointer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nsa committed Sep 13, 2017
1 parent b373c8d commit a1f8fb5
Showing 1 changed file with 79 additions and 40 deletions.
119 changes: 79 additions & 40 deletions fundingmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,11 @@ func (f *fundingManager) Start() error {

f.localDiscoverySignals[chanID] = make(chan struct{})

doneChan := make(chan struct{})
doneChan := make(chan *lnwire.ShortChannelID)
timeoutChan := make(chan struct{})

go func(ch *channeldb.OpenChannel) {
shortChanID := &lnwire.ShortChannelID{}
go f.waitForFundingWithTimeout(ch, doneChan, timeoutChan, shortChanID)
go f.waitForFundingWithTimeout(ch, doneChan, timeoutChan)

select {
case <-timeoutChan:
Expand All @@ -394,9 +393,21 @@ func (f *fundingManager) Start() error {
case <-f.quit:
// The fundingManager is shutting down, and will
// resume wait on startup.
case <-doneChan:
case shortChanID, ok := <-doneChan:
if !ok {
fndgLog.Errorf("doneChan was closed - cannot continue")
return
}
// Success, funding transaction was confirmed.
lnChannel := &lnwallet.LightningChannel{}

// 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
}
if err = f.sendFundingLocked(ch, lnChannel, shortChanID); err != nil {
fndgLog.Errorf("Failed to send fundingLocked: %v", err)
return
Expand Down Expand Up @@ -449,7 +460,16 @@ func (f *fundingManager) Start() error {
f.wg.Add(1)
go func() {
defer f.wg.Done()
lnChannel := &lnwallet.LightningChannel{}

// 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, channel)
if err != nil {
fndgLog.Errorf("error creating new lightning channel: %v", err)
return
}

if err = f.sendFundingLocked(channel, lnChannel, shortChanID); err != nil {
fndgLog.Errorf("Failed to send fundingLocked: %v", err)
return
Expand Down Expand Up @@ -1078,11 +1098,10 @@ func (f *fundingManager) handleFundingCreated(fmsg *fundingCreatedMsg) {
// transaction in 288 blocks (~ 48 hrs), by canceling the reservation
// and canceling the wait for the funding confirmation.
go func() {
doneChan := make(chan struct{})
doneChan := make(chan *lnwire.ShortChannelID)
timeoutChan := make(chan struct{})
shortChanID := &lnwire.ShortChannelID{}
go f.waitForFundingWithTimeout(completeChan, doneChan,
timeoutChan, shortChanID)
timeoutChan)

select {
case <-timeoutChan:
Expand All @@ -1093,16 +1112,29 @@ func (f *fundingManager) handleFundingCreated(fmsg *fundingCreatedMsg) {
case <-f.quit:
// The fundingManager is shutting down, will resume
// wait for funding transaction on startup.
case <-doneChan:
case shortChanID, ok := <-doneChan:
if !ok {
fndgLog.Errorf("doneChan was closed - cannot continue")
return
}
// Success, funding transaction was confirmed.
channel := &lnwallet.LightningChannel{}
if err = f.sendFundingLocked(completeChan, channel, shortChanID); err != nil {

// 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
}

if err = f.sendFundingLocked(completeChan, lnChannel, shortChanID); err != nil {
fndgLog.Errorf("Failed to send fundingLocked: %v", err)
return
}
f.deleteReservationCtx(peerKey,
fmsg.msg.PendingChannelID)
if err = f.sendChannelAnnouncement(completeChan, channel, shortChanID); err != nil {
if err = f.sendChannelAnnouncement(completeChan, lnChannel, shortChanID); err != nil {
fndgLog.Errorf("Failed to send channel announcement: %v", err)
}
}
Expand Down Expand Up @@ -1187,31 +1219,42 @@ func (f *fundingManager) handleFundingSigned(fmsg *fundingSignedMsg) {
}

go func() {
doneChan := make(chan struct{})
doneChan := make(chan *lnwire.ShortChannelID)
cancelChan := make(chan struct{})

// In case the fundingManager is stopped at some point during
// the remaining part of the opening process, we must wait for
// this process to finish (either successully or with some
// error), before the fundingManager can be shut down.
f.wg.Add(1)
shortChanID := &lnwire.ShortChannelID{}
go func() {
defer f.wg.Done()
f.waitForFundingConfirmation(completeChan, cancelChan,
doneChan, shortChanID)
doneChan)
}()

select {
case <-f.quit:
return
case <-doneChan:
channel := &lnwallet.LightningChannel{}
if err = f.sendFundingLocked(completeChan, channel, shortChanID); err != nil {
case shortChanID, ok := <-doneChan:
if !ok {
fndgLog.Errorf("doneChan was closed - cannot continue")
return
}

// 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
}
if err = f.sendFundingLocked(completeChan, lnChannel, shortChanID); err != nil {
fndgLog.Errorf("Failed to send fundingLocked: %v", err)
return
}
if err = f.sendChannelAnnouncement(completeChan, channel, shortChanID); err != nil {
if err = f.sendChannelAnnouncement(completeChan, lnChannel, shortChanID); err != nil {
fndgLog.Errorf("Failed to send channel announcement: %v", err)
return
}
Expand Down Expand Up @@ -1240,7 +1283,7 @@ func (f *fundingManager) handleFundingSigned(fmsg *fundingSignedMsg) {
// passed from bestHeight. In the case of timeout, the timeoutChan will be
// closed. In case of confirmation or error, doneChan will be closed.
func (f *fundingManager) waitForFundingWithTimeout(completeChan *channeldb.OpenChannel,
doneChan chan<- struct{}, timeoutChan chan<- struct{}, shortChanID *lnwire.ShortChannelID) {
doneChan chan<- *lnwire.ShortChannelID, timeoutChan chan<- struct{}) {

epochClient, err := f.cfg.Notifier.RegisterBlockEpochNtfn()
if err != nil {
Expand All @@ -1252,7 +1295,7 @@ func (f *fundingManager) waitForFundingWithTimeout(completeChan *channeldb.OpenC

defer epochClient.Cancel()

waitingDoneChan := make(chan struct{})
waitingDoneChan := make(chan *lnwire.ShortChannelID)
cancelChan := make(chan struct{})

// Add this goroutine to wait group so we can be sure that it is
Expand All @@ -1261,7 +1304,7 @@ func (f *fundingManager) waitForFundingWithTimeout(completeChan *channeldb.OpenC
go func() {
defer f.wg.Done()
f.waitForFundingConfirmation(completeChan, cancelChan,
waitingDoneChan, shortChanID)
waitingDoneChan)
}()

// On block maxHeight we will cancel the funding confirmation wait.
Expand Down Expand Up @@ -1291,8 +1334,13 @@ func (f *fundingManager) waitForFundingWithTimeout(completeChan *channeldb.OpenC
// The fundingManager is shutting down, will resume
// waiting for the funding transaction on startup.
return
case <-waitingDoneChan:
close(doneChan)
case shortChanID, ok := <-waitingDoneChan:
if !ok {
fndgLog.Errorf("doneChan was closed - cannot continue")
close(doneChan)
return
}
doneChan <- shortChanID
return
}
}
Expand All @@ -1305,7 +1353,7 @@ func (f *fundingManager) waitForFundingWithTimeout(completeChan *channeldb.OpenC
// when a channel has become active for lightning transactions.
// The wait can be canceled by closing the cancelChan.
func (f *fundingManager) waitForFundingConfirmation(completeChan *channeldb.OpenChannel,
cancelChan <-chan struct{}, doneChan chan<- struct{}, shortChanID *lnwire.ShortChannelID) {
cancelChan <-chan struct{}, doneChan chan<- *lnwire.ShortChannelID) {

defer close(doneChan)

Expand Down Expand Up @@ -1359,7 +1407,7 @@ func (f *fundingManager) waitForFundingConfirmation(completeChan *channeldb.Open
// With the block height and the transaction index known, we can
// construct the compact chanID which is used on the network to unique
// identify channels.
*shortChanID = lnwire.ShortChannelID{
shortChanID := lnwire.ShortChannelID{
BlockHeight: confDetails.BlockHeight,
TxIndex: confDetails.TxIndex,
TxPosition: uint16(fundingPoint.Index),
Expand All @@ -1368,7 +1416,7 @@ func (f *fundingManager) waitForFundingConfirmation(completeChan *channeldb.Open
// Now that the channel has been fully confirmed, we'll mark it as open
// within the database.
completeChan.IsPending = false
err = f.cfg.Wallet.Cfg.Database.MarkChannelAsOpen(&fundingPoint, *shortChanID)
err = f.cfg.Wallet.Cfg.Database.MarkChannelAsOpen(&fundingPoint, shortChanID)
if err != nil {
fndgLog.Errorf("error setting channel pending flag to false: "+
"%v", err)
Expand All @@ -1387,12 +1435,14 @@ func (f *fundingManager) waitForFundingConfirmation(completeChan *channeldb.Open
// saveChannelOpeningState) atomic by doing them in the same transaction.
// Needed to be properly fault-tolerant.
err = f.saveChannelOpeningState(&completeChan.FundingOutpoint, markedOpen,
shortChanID)
&shortChanID)
if err != nil {
fndgLog.Errorf("error setting channel state to markedOpen: %v",
err)
return
}

doneChan <- &shortChanID
}

// sendFundingLocked creates and sends the fundingLocked message.
Expand All @@ -1404,17 +1454,6 @@ func (f *fundingManager) sendFundingLocked(completeChan *channeldb.OpenChannel,

chanID := lnwire.NewChanIDFromOutPoint(&completeChan.FundingOutpoint)

// 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 err
}

*channel = *lnChannel

// Note: we don't call `defer channel.Stop()` since we want the channel
// running when sendChannelAnnouncement() is called.

Expand Down

0 comments on commit a1f8fb5

Please sign in to comment.