mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-07-03 20:16:02 +02:00
chanbackup: add functionality to allow external callers to backup channels
In this commit, we introduce a series of interfaces and methods that will allow external callers to backup either all channels, or a specific channel identified by its channel point. In order to abstract away the details w.r.t _how_ we obtain the set of open channels, or their storage mechanisms, we introduce a new LiveChannelSource interfaces. This interfaces allows us to fetch all channels, a channel by its channel point, and also all the known addresses for a node as we'll need this in order to connect out to the node in the case of a recovery attempt.
This commit is contained in:
99
chanbackup/backup.go
Normal file
99
chanbackup/backup.go
Normal file
@ -0,0 +1,99 @@
|
||||
package chanbackup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
)
|
||||
|
||||
// LiveChannelSource is an interface that allows us to query for the set of
|
||||
// live channels. A live channel is one that is open, and has not had a
|
||||
// commitment transaction broadcast.
|
||||
type LiveChannelSource interface {
|
||||
// FetchAllChannels returns all known live channels.
|
||||
FetchAllChannels() ([]*channeldb.OpenChannel, error)
|
||||
|
||||
// FetchChannel attempts to locate a live channel identified by the
|
||||
// passed chanPoint.
|
||||
FetchChannel(chanPoint wire.OutPoint) (*channeldb.OpenChannel, error)
|
||||
|
||||
// AddrsForNode returns all known addresses for the target node public
|
||||
// key.
|
||||
AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr, error)
|
||||
}
|
||||
|
||||
// assembleChanBackup attempts to assemble a static channel backup for the
|
||||
// passed open channel. The backup includes all information required to restore
|
||||
// the channel, as well as addressing information so we can find the peer and
|
||||
// reconnect to them to initiate the protocol.
|
||||
func assembleChanBackup(chanSource LiveChannelSource,
|
||||
openChan *channeldb.OpenChannel) (*Single, error) {
|
||||
|
||||
log.Debugf("Crafting backup for ChannelPoint(%v)",
|
||||
openChan.FundingOutpoint)
|
||||
|
||||
// First, we'll query the channel source to obtain all the addresses
|
||||
// that are are associated with the peer for this channel.
|
||||
nodeAddrs, err := chanSource.AddrsForNode(openChan.IdentityPub)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
single := NewSingle(openChan, nodeAddrs)
|
||||
|
||||
return &single, nil
|
||||
}
|
||||
|
||||
// FetchBackupForChan attempts to create a plaintext static channel backup for
|
||||
// the target channel identified by its channel point. If we're unable to find
|
||||
// the target channel, then an error will be returned.
|
||||
func FetchBackupForChan(chanPoint wire.OutPoint,
|
||||
chanSource LiveChannelSource) (*Single, error) {
|
||||
|
||||
// First, we'll query the channel source to see if the channel is known
|
||||
// and open within the database.
|
||||
targetChan, err := chanSource.FetchChannel(chanPoint)
|
||||
if err != nil {
|
||||
// If we can't find the channel, then we return with an error,
|
||||
// as we have nothing to backup.
|
||||
return nil, fmt.Errorf("unable to find target channel")
|
||||
}
|
||||
|
||||
// Once we have the target channel, we can assemble the backup using
|
||||
// the source to obtain any extra information that we may need.
|
||||
staticChanBackup, err := assembleChanBackup(chanSource, targetChan)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to create chan backup: %v", err)
|
||||
}
|
||||
|
||||
return staticChanBackup, nil
|
||||
}
|
||||
|
||||
// FetchStaticChanBackups will return a plaintext static channel back up for
|
||||
// all known active/open channels within the passed channel source.
|
||||
func FetchStaticChanBackups(chanSource LiveChannelSource) ([]Single, error) {
|
||||
// First, we'll query the backup source for information concerning all
|
||||
// currently open and available channels.
|
||||
openChans, err := chanSource.FetchAllChannels()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Now that we have all the channels, we'll use the chanSource to
|
||||
// obtain any auxiliary information we need to craft a backup for each
|
||||
// channel.
|
||||
staticChanBackups := make([]Single, len(openChans))
|
||||
for i, openChan := range openChans {
|
||||
chanBackup, err := assembleChanBackup(chanSource, openChan)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
staticChanBackups[i] = *chanBackup
|
||||
}
|
||||
|
||||
return staticChanBackups, nil
|
||||
}
|
Reference in New Issue
Block a user