• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 15951470896

29 Jun 2025 04:23AM UTC coverage: 67.594% (-0.01%) from 67.606%
15951470896

Pull #9751

github

web-flow
Merge 599d9b051 into 6290edf14
Pull Request #9751: multi: update Go to 1.23.10 and update some packages

135088 of 199851 relevant lines covered (67.59%)

21909.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.44
/chanbackup/backup.go
1
package chanbackup
2

3
import (
4
        "context"
5
        "fmt"
6

7
        "github.com/btcsuite/btcd/wire"
8
        "github.com/lightningnetwork/lnd/channeldb"
9
        "github.com/lightningnetwork/lnd/fn/v2"
10
)
11

12
// LiveChannelSource is an interface that allows us to query for the set of
13
// live channels. A live channel is one that is open, and has not had a
14
// commitment transaction broadcast.
15
type LiveChannelSource interface {
16
        // FetchAllChannels returns all known live channels.
17
        FetchAllChannels() ([]*channeldb.OpenChannel, error)
18

19
        // FetchChannel attempts to locate a live channel identified by the
20
        // passed chanPoint. Optionally an existing db tx can be supplied.
21
        FetchChannel(chanPoint wire.OutPoint) (*channeldb.OpenChannel, error)
22
}
23

24
// assembleChanBackup attempts to assemble a static channel backup for the
25
// passed open channel. The backup includes all information required to restore
26
// the channel, as well as addressing information so we can find the peer and
27
// reconnect to them to initiate the protocol.
28
func assembleChanBackup(ctx context.Context, addrSource channeldb.AddrSource,
29
        openChan *channeldb.OpenChannel) (*Single, error) {
9✔
30

9✔
31
        log.Debugf("Crafting backup for ChannelPoint(%v)",
9✔
32
                openChan.FundingOutpoint)
9✔
33

9✔
34
        // First, we'll query the channel source to obtain all the addresses
9✔
35
        // that are associated with the peer for this channel.
9✔
36
        known, nodeAddrs, err := addrSource.AddrsForNode(
9✔
37
                ctx, openChan.IdentityPub,
9✔
38
        )
9✔
39
        if err != nil {
9✔
40
                return nil, err
×
41
        }
×
42
        if !known {
11✔
43
                return nil, fmt.Errorf("node unknown by address source")
2✔
44
        }
2✔
45

46
        single := NewSingle(openChan, nodeAddrs)
7✔
47

7✔
48
        return &single, nil
7✔
49
}
50

51
// buildCloseTxInputs generates inputs needed to force close a channel from
52
// an open channel. Anyone having these inputs and the signer, can sign the
53
// force closure transaction. Warning! If the channel state updates, an attempt
54
// to close the channel using this method with outdated CloseTxInputs can result
55
// in loss of funds! This may happen if an outdated channel backup is attempted
56
// to be used to force close the channel.
57
func buildCloseTxInputs(
58
        targetChan *channeldb.OpenChannel) fn.Option[CloseTxInputs] {
69✔
59

69✔
60
        log.Debugf("Crafting CloseTxInputs for ChannelPoint(%v)",
69✔
61
                targetChan.FundingOutpoint)
69✔
62

69✔
63
        localCommit := targetChan.LocalCommitment
69✔
64

69✔
65
        if localCommit.CommitTx == nil {
108✔
66
                log.Infof("CommitTx is nil for ChannelPoint(%v), "+
39✔
67
                        "skipping CloseTxInputs. This is possible when "+
39✔
68
                        "DLP is active.", targetChan.FundingOutpoint)
39✔
69

39✔
70
                return fn.None[CloseTxInputs]()
39✔
71
        }
39✔
72

73
        // We need unsigned force close tx and the counterparty's signature.
74
        inputs := CloseTxInputs{
33✔
75
                CommitTx:  localCommit.CommitTx,
33✔
76
                CommitSig: localCommit.CommitSig,
33✔
77
        }
33✔
78

33✔
79
        // In case of a taproot channel, commit height is needed as well to
33✔
80
        // produce verification nonce for the taproot channel using shachain.
33✔
81
        if targetChan.ChanType.IsTaproot() {
66✔
82
                inputs.CommitHeight = localCommit.CommitHeight
33✔
83
        }
33✔
84

85
        // In case of a custom taproot channel, TapscriptRoot is needed as well.
86
        if targetChan.ChanType.HasTapscriptRoot() {
51✔
87
                inputs.TapscriptRoot = targetChan.TapscriptRoot
18✔
88
        }
18✔
89

90
        return fn.Some(inputs)
33✔
91
}
92

93
// FetchBackupForChan attempts to create a plaintext static channel backup for
94
// the target channel identified by its channel point. If we're unable to find
95
// the target channel, then an error will be returned.
96
func FetchBackupForChan(ctx context.Context, chanPoint wire.OutPoint,
97
        chanSource LiveChannelSource,
98
        addrSource channeldb.AddrSource) (*Single, error) {
6✔
99

6✔
100
        // First, we'll query the channel source to see if the channel is known
6✔
101
        // and open within the database.
6✔
102
        targetChan, err := chanSource.FetchChannel(chanPoint)
6✔
103
        if err != nil {
7✔
104
                // If we can't find the channel, then we return with an error,
1✔
105
                // as we have nothing to  backup.
1✔
106
                return nil, fmt.Errorf("unable to find target channel")
1✔
107
        }
1✔
108

109
        // Once we have the target channel, we can assemble the backup using
110
        // the source to obtain any extra information that we may need.
111
        staticChanBackup, err := assembleChanBackup(ctx, addrSource, targetChan)
5✔
112
        if err != nil {
6✔
113
                return nil, fmt.Errorf("unable to create chan backup: %w", err)
1✔
114
        }
1✔
115

116
        return staticChanBackup, nil
4✔
117
}
118

119
// FetchStaticChanBackups will return a plaintext static channel back up for
120
// all known active/open channels within the passed channel source.
121
func FetchStaticChanBackups(ctx context.Context, chanSource LiveChannelSource,
122
        addrSource channeldb.AddrSource) ([]Single, error) {
6✔
123

6✔
124
        // First, we'll query the backup source for information concerning all
6✔
125
        // currently open and available channels.
6✔
126
        openChans, err := chanSource.FetchAllChannels()
6✔
127
        if err != nil {
7✔
128
                return nil, err
1✔
129
        }
1✔
130

131
        // Now that we have all the channels, we'll use the chanSource to
132
        // obtain any auxiliary information we need to craft a backup for each
133
        // channel.
134
        staticChanBackups := make([]Single, 0, len(openChans))
5✔
135
        for _, openChan := range openChans {
12✔
136
                chanBackup, err := assembleChanBackup(ctx, addrSource, openChan)
7✔
137
                if err != nil {
8✔
138
                        return nil, err
1✔
139
                }
1✔
140

141
                staticChanBackups = append(staticChanBackups, *chanBackup)
6✔
142
        }
143

144
        return staticChanBackups, nil
4✔
145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc