Skip to content

Commit

Permalink
Merge 3bcc44b into 4d7a151
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Nov 16, 2019
2 parents 4d7a151 + 3bcc44b commit 7e689a1
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions chanrestore.go
Expand Up @@ -2,9 +2,11 @@ package lnd

import (
"fmt"
"math"
"net"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/lightningnetwork/lnd/chanbackup"
"github.com/lightningnetwork/lnd/channeldb"
Expand All @@ -14,6 +16,18 @@ import (
"github.com/lightningnetwork/lnd/shachain"
)

const (
// mainnetSCBLaunchBlock is the approximate block height of the bitcoin
// mainnet chain of the date when SCBs first were released in lnd
// (v0.6.0-beta). The block date is 4/15/2019, 10:54 PM UTC.
mainnetSCBLaunchBlock = 571800

// testnetSCBLaunchBlock is the approximate block height of the bitcoin
// testnet3 chain of the date when SCBs first were released in lnd
// (v0.6.0-beta). The block date is 4/16/2019, 08:04 AM UTC.
testnetSCBLaunchBlock = 1489300
)

// chanDBRestorer is an implementation of the chanbackup.ChannelRestorer
// interface that is able to properly map a Single backup, into a
// channeldb.ChannelShell which is required to fully restore a channel. We also
Expand Down Expand Up @@ -126,15 +140,54 @@ func (c *chanDBRestorer) openChannelShell(backup chanbackup.Single) (
// NOTE: Part of the chanbackup.ChannelRestorer interface.
func (c *chanDBRestorer) RestoreChansFromSingles(backups ...chanbackup.Single) error {
channelShells := make([]*channeldb.ChannelShell, 0, len(backups))
firstChanHeight := uint32(math.MaxUint32)
for _, backup := range backups {
chanShell, err := c.openChannelShell(backup)
if err != nil {
return err
}

// Find the block height of the earliest channel in this backup.
chanHeight := chanShell.Chan.ShortChanID().BlockHeight
if chanHeight != 0 && chanHeight < firstChanHeight {
firstChanHeight = chanHeight
}

channelShells = append(channelShells, chanShell)
}

// In case there were only unconfirmed channels, we will have to scan
// the chain beginning from the launch date of SCBs.
if firstChanHeight == math.MaxUint32 {
chainHash := channelShells[0].Chan.ChainHash
switch {
case chainHash.IsEqual(chaincfg.MainNetParams.GenesisHash):
firstChanHeight = mainnetSCBLaunchBlock

case chainHash.IsEqual(chaincfg.TestNet3Params.GenesisHash):
firstChanHeight = testnetSCBLaunchBlock

default:
// Worst case: We have no height hint and start at
// block 1. Should only happen for SCBs in regtest,
// simnet and litecoin.
firstChanHeight = 1
}
}

// If there were channels in the backup that were not confirmed at the
// time of the backup creation, they won't have a block height in the
// ShortChanID which would lead to an error in the chain watcher. Since
// we have no other height hint available for these channels, the best
// we can do is give them a FundingBroadcastHeight that corresponds to
// the lowest block height of the confirmed channels, hoping that they
// in fact were opened before those unconfirmed.
for _, chanShell := range channelShells {
if chanShell.Chan.ShortChanID().BlockHeight == 0 {
chanShell.Chan.FundingBroadcastHeight = firstChanHeight
}
}

ltndLog.Infof("Inserting %v SCB channel shells into DB",
len(channelShells))

Expand Down

0 comments on commit 7e689a1

Please sign in to comment.