From 8990de4d99e4bbf64d8fe5ccb321859b9d5ba20e Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sat, 14 Jan 2017 18:03:45 -0800 Subject: [PATCH] breacharbiter: ensure failure to retrieve channels triggers start-up error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit moves the fetching of active channels from the contractObserver goroutine up to the Start() method on the breachArbiter. By doing this, we ensure that the user receives an error (under the current set up) if the btcd node that lnd is connected to doesn’t have their txindex enabled. --- breacharbiter.go | 55 ++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/breacharbiter.go b/breacharbiter.go index d76e3974d..bf8ea0e3e 100644 --- a/breacharbiter.go +++ b/breacharbiter.go @@ -87,8 +87,37 @@ func (b *breachArbiter) Start() error { brarLog.Tracef("Starting breach aribter") + // First we need to query that database state for all currently active + // channels, each of these channels will need a goroutine assigned to + // it to watch for channel breaches. + activeChannels, err := b.db.FetchAllChannels() + if err != nil && err != channeldb.ErrNoActiveChannels { + brarLog.Errorf("unable to fetch active channels") + return err + } + + if len(activeChannels) > 0 { + brarLog.Infof("Retrieved %v channels from database, watching "+ + "with vigilance!", len(activeChannels)) + } + + // For each of the channels read from disk, we'll create a channel + // state machine in order to watch for any potential channel closures. + channelsToWatch := make([]*lnwallet.LightningChannel, + len(activeChannels)) + for i, chanState := range activeChannels { + channel, err := lnwallet.NewLightningChannel(nil, nil, + b.notifier, chanState) + if err != nil { + brarLog.Errorf("unable to load channel from disk") + return err + } + + channelsToWatch[i] = channel + } + b.wg.Add(1) - go b.contractObserver() + go b.contractObserver(channelsToWatch) return nil } @@ -117,34 +146,14 @@ func (b *breachArbiter) Stop() error { // channel into the daemon's wallet. // // NOTE: This MUST be run as a goroutine. -func (b *breachArbiter) contractObserver() { +func (b *breachArbiter) contractObserver(activeChannels []*lnwallet.LightningChannel) { defer b.wg.Done() - // First we need to query that database state for all currently active - // channels, each of these channels will need a goroutine assigned to - // it to watch for channel breaches. - activeChannels, err := b.db.FetchAllChannels() - if err != nil { - // TODO(roasbeef): this is a fatal error... - brarLog.Errorf("unable to fetch active channels: %v", err) - } - - if len(activeChannels) > 0 { - brarLog.Infof("Retrieved %v channels from database, watching "+ - "with vigilance!", len(activeChannels)) - } - // For each active channel found within the database, we launch a // detected breachObserver goroutine for that channel and also track // the new goroutine within the breachObservers map so we can cancel it // later if necessary. - for _, chanState := range activeChannels { - channel, err := lnwallet.NewLightningChannel(nil, nil, - b.notifier, chanState) - if err != nil { - brarLog.Errorf("unable to load channel: %v", err) - } - + for _, channel := range activeChannels { settleSignal := make(chan struct{}) chanPoint := channel.ChannelPoint() b.breachObservers[*chanPoint] = settleSignal