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

lightningnetwork / lnd / 12986279612

27 Jan 2025 09:51AM UTC coverage: 57.652% (-1.1%) from 58.788%
12986279612

Pull #9447

github

yyforyongyu
sweep: rename methods for clarity

We now rename "third party" to "unknown" as the inputs can be spent via
an older sweeping tx, a third party (anchor), or a remote party (pin).
In fee bumper we don't have the info to distinguish the above cases, and
leave them to be further handled by the sweeper as it has more context.
Pull Request #9447: sweep: start tracking input spending status in the fee bumper

83 of 87 new or added lines in 2 files covered. (95.4%)

19578 existing lines in 256 files now uncovered.

103448 of 179434 relevant lines covered (57.65%)

24884.58 hits per line

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

0.0
/channel_notifier.go
1
package lnd
2

3
import (
4
        "fmt"
5

6
        "github.com/btcsuite/btcd/wire"
7
        "github.com/lightningnetwork/lnd/chanbackup"
8
        "github.com/lightningnetwork/lnd/channeldb"
9
        "github.com/lightningnetwork/lnd/channelnotifier"
10
)
11

12
// channelNotifier is an implementation of the chanbackup.ChannelNotifier
13
// interface using the existing channelnotifier.ChannelNotifier struct. This
14
// implementation allows us to satisfy all the dependencies of the
15
// chanbackup.SubSwapper struct.
16
type channelNotifier struct {
17
        // chanNotifier is the based channel notifier that we'll proxy requests
18
        // from.
19
        chanNotifier *channelnotifier.ChannelNotifier
20

21
        // addrs is an implementation of the addrSource interface that allows
22
        // us to get the latest set of addresses for a given node. We'll need
23
        // this to be able to create an SCB for new channels.
24
        addrs channeldb.AddrSource
25
}
26

27
// SubscribeChans requests a new channel subscription relative to the initial
28
// set of known channels. We use the knownChans as a synchronization point to
29
// ensure that the chanbackup.SubSwapper does not miss any channel open or
30
// close events in the period between when it's created, and when it requests
31
// the channel subscription.
32
//
33
// NOTE: This is part of the chanbackup.ChannelNotifier interface.
34
func (c *channelNotifier) SubscribeChans(startingChans map[wire.OutPoint]struct{}) (
UNCOV
35
        *chanbackup.ChannelSubscription, error) {
×
UNCOV
36

×
UNCOV
37
        ltndLog.Infof("Channel backup proxy channel notifier starting")
×
UNCOV
38

×
UNCOV
39
        // TODO(roasbeef): read existing set of chans and diff
×
UNCOV
40

×
UNCOV
41
        quit := make(chan struct{})
×
UNCOV
42
        chanUpdates := make(chan chanbackup.ChannelEvent, 1)
×
UNCOV
43

×
UNCOV
44
        // sendChanOpenUpdate is a closure that sends a ChannelEvent to the
×
UNCOV
45
        // chanUpdates channel to inform subscribers about new pending or
×
UNCOV
46
        // confirmed channels.
×
UNCOV
47
        sendChanOpenUpdate := func(newOrPendingChan *channeldb.OpenChannel) {
×
UNCOV
48
                _, nodeAddrs, err := c.addrs.AddrsForNode(
×
UNCOV
49
                        newOrPendingChan.IdentityPub,
×
UNCOV
50
                )
×
UNCOV
51
                if err != nil {
×
52
                        pub := newOrPendingChan.IdentityPub
×
53
                        ltndLog.Errorf("unable to fetch addrs for %x: %v",
×
54
                                pub.SerializeCompressed(), err)
×
55
                }
×
56

UNCOV
57
                chanEvent := chanbackup.ChannelEvent{
×
UNCOV
58
                        NewChans: []chanbackup.ChannelWithAddrs{
×
UNCOV
59
                                {
×
UNCOV
60
                                        OpenChannel: newOrPendingChan,
×
UNCOV
61
                                        Addrs:       nodeAddrs,
×
UNCOV
62
                                },
×
UNCOV
63
                        },
×
UNCOV
64
                }
×
UNCOV
65

×
UNCOV
66
                select {
×
UNCOV
67
                case chanUpdates <- chanEvent:
×
68
                case <-quit:
×
69
                        return
×
70
                }
71
        }
72

73
        // In order to adhere to the interface, we'll proxy the events from the
74
        // channel notifier to the sub-swapper in a format it understands.
UNCOV
75
        go func() {
×
UNCOV
76
                // First, we'll subscribe to the primary channel notifier so we can
×
UNCOV
77
                // obtain events for new opened/closed channels.
×
UNCOV
78
                chanSubscription, err := c.chanNotifier.SubscribeChannelEvents()
×
UNCOV
79
                if err != nil {
×
80
                        panic(fmt.Sprintf("unable to subscribe to chans: %v",
×
81
                                err))
×
82
                }
83

UNCOV
84
                defer chanSubscription.Cancel()
×
UNCOV
85

×
UNCOV
86
                for {
×
UNCOV
87
                        select {
×
88

89
                        // A new event has been sent by the chanNotifier, we'll
90
                        // filter out the events we actually care about and
91
                        // send them to the sub-swapper.
UNCOV
92
                        case e := <-chanSubscription.Updates():
×
UNCOV
93
                                // TODO(roasbeef): batch dispatch ntnfs
×
UNCOV
94

×
UNCOV
95
                                switch event := e.(type) {
×
96
                                // A new channel has been opened and is still
97
                                // pending. We can still create a backup, even
98
                                // if the final channel ID is not yet available.
UNCOV
99
                                case channelnotifier.PendingOpenChannelEvent:
×
UNCOV
100
                                        pendingChan := event.PendingChannel
×
UNCOV
101
                                        sendChanOpenUpdate(pendingChan)
×
102

103
                                // A new channel has been confirmed, we'll
104
                                // obtain the node address, then send to the
105
                                // sub-swapper.
UNCOV
106
                                case channelnotifier.OpenChannelEvent:
×
UNCOV
107
                                        sendChanOpenUpdate(event.Channel)
×
108

109
                                // An existing channel has been closed, we'll
110
                                // send only the chanPoint of the closed
111
                                // channel to the sub-swapper.
UNCOV
112
                                case channelnotifier.ClosedChannelEvent:
×
UNCOV
113
                                        chanPoint := event.CloseSummary.ChanPoint
×
UNCOV
114
                                        closeType := event.CloseSummary.CloseType
×
UNCOV
115

×
UNCOV
116
                                        // Because we see the contract as closed
×
UNCOV
117
                                        // once our local force close TX
×
UNCOV
118
                                        // confirms, the channel arbitrator
×
UNCOV
119
                                        // already fires on this event. But
×
UNCOV
120
                                        // because our funds can be in limbo for
×
UNCOV
121
                                        // up to 2 weeks worst case we don't
×
UNCOV
122
                                        // want to remove the crucial info we
×
UNCOV
123
                                        // need for sweeping that time locked
×
UNCOV
124
                                        // output before we've actually done so.
×
UNCOV
125
                                        if closeType == channeldb.LocalForceClose {
×
UNCOV
126
                                                ltndLog.Debugf("Channel %v "+
×
UNCOV
127
                                                        "was force closed by "+
×
UNCOV
128
                                                        "us, not removing "+
×
UNCOV
129
                                                        "from channel backup "+
×
UNCOV
130
                                                        "until fully resolved",
×
UNCOV
131
                                                        chanPoint)
×
UNCOV
132

×
UNCOV
133
                                                continue
×
134
                                        }
135

UNCOV
136
                                        chanEvent := chanbackup.ChannelEvent{
×
UNCOV
137
                                                ClosedChans: []wire.OutPoint{
×
UNCOV
138
                                                        chanPoint,
×
UNCOV
139
                                                },
×
UNCOV
140
                                        }
×
UNCOV
141

×
UNCOV
142
                                        select {
×
UNCOV
143
                                        case chanUpdates <- chanEvent:
×
144
                                        case <-quit:
×
145
                                                return
×
146
                                        }
147

148
                                // A channel was fully resolved on chain. This
149
                                // should only really interest us if it was a
150
                                // locally force closed channel where we didn't
151
                                // remove the channel already when the close
152
                                // event was fired.
UNCOV
153
                                case channelnotifier.FullyResolvedChannelEvent:
×
UNCOV
154
                                        chanEvent := chanbackup.ChannelEvent{
×
UNCOV
155
                                                ClosedChans: []wire.OutPoint{
×
UNCOV
156
                                                        *event.ChannelPoint,
×
UNCOV
157
                                                },
×
UNCOV
158
                                        }
×
UNCOV
159

×
UNCOV
160
                                        select {
×
UNCOV
161
                                        case chanUpdates <- chanEvent:
×
162
                                        case <-quit:
×
163
                                                return
×
164
                                        }
165
                                }
166

167
                        // The cancel method has been called, signalling us to
168
                        // exit
UNCOV
169
                        case <-quit:
×
UNCOV
170
                                return
×
171
                        }
172
                }
173
        }()
174

UNCOV
175
        return &chanbackup.ChannelSubscription{
×
UNCOV
176
                ChanUpdates: chanUpdates,
×
UNCOV
177
                Cancel: func() {
×
UNCOV
178
                        close(quit)
×
UNCOV
179
                },
×
180
        }, nil
181
}
182

183
// A compile-time constraint to ensure channelNotifier implements
184
// chanbackup.ChannelNotifier.
185
var _ chanbackup.ChannelNotifier = (*channelNotifier)(nil)
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