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

lightningnetwork / lnd / 15838907453

24 Jun 2025 01:26AM UTC coverage: 57.079% (-11.1%) from 68.172%
15838907453

Pull #9982

github

web-flow
Merge e42780be2 into 45c15646c
Pull Request #9982: lnwire+lnwallet: add LocalNonces field for splice nonce coordination w/ taproot channels

103 of 167 new or added lines in 5 files covered. (61.68%)

30191 existing lines in 463 files now uncovered.

96331 of 168768 relevant lines covered (57.08%)

0.6 hits per line

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

81.08
/feature/manager.go
1
package feature
2

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

7
        "github.com/lightningnetwork/lnd/lnwire"
8
)
9

10
var (
11
        // ErrUnknownSet is returned if a proposed feature vector contains a
12
        // set that is unknown to LND.
13
        ErrUnknownSet = errors.New("unknown feature bit set")
14

15
        // ErrFeatureConfigured is returned if an attempt is made to unset a
16
        // feature that was configured at startup.
17
        ErrFeatureConfigured = errors.New("can't unset configured feature")
18
)
19

20
// Config houses any runtime modifications to the default set descriptors. For
21
// our purposes, this typically means disabling certain features to test legacy
22
// protocol interoperability or functionality.
23
type Config struct {
24
        // NoTLVOnion unsets any optional or required TLVOnionPaylod bits from
25
        // all feature sets.
26
        NoTLVOnion bool
27

28
        // NoStaticRemoteKey unsets any optional or required StaticRemoteKey
29
        // bits from all feature sets.
30
        NoStaticRemoteKey bool
31

32
        // NoAnchors unsets any bits signaling support for anchor outputs.
33
        NoAnchors bool
34

35
        // NoWumbo unsets any bits signalling support for wumbo channels.
36
        NoWumbo bool
37

38
        // NoTaprootChans unsets any bits signaling support for taproot
39
        // channels.
40
        NoTaprootChans bool
41

42
        // NoScriptEnforcementLease unsets any bits signaling support for script
43
        // enforced leases.
44
        NoScriptEnforcementLease bool
45

46
        // NoKeysend unsets any bits signaling support for accepting keysend
47
        // payments.
48
        NoKeysend bool
49

50
        // NoOptionScidAlias unsets any bits signalling support for
51
        // option_scid_alias. This also implicitly disables zero-conf channels.
52
        NoOptionScidAlias bool
53

54
        // NoZeroConf unsets any bits signalling support for zero-conf
55
        // channels. This should be used instead of NoOptionScidAlias to still
56
        // keep option-scid-alias support.
57
        NoZeroConf bool
58

59
        // NoAnySegwit unsets any bits that signal support for using other
60
        // segwit witness versions for co-op closes.
61
        NoAnySegwit bool
62

63
        // NoRouteBlinding unsets route blinding feature bits.
64
        NoRouteBlinding bool
65

66
        // NoQuiescence unsets quiescence feature bits.
67
        NoQuiescence bool
68

69
        // NoTaprootOverlay unsets the taproot overlay channel feature bits.
70
        NoTaprootOverlay bool
71

72
        // NoExperimentalEndorsement unsets any bits that signal support for
73
        // forwarding experimental endorsement.
74
        NoExperimentalEndorsement bool
75

76
        // NoRbfCoopClose unsets any bits that signal support for using RBF for
77
        // coop close.
78
        NoRbfCoopClose bool
79

80
        // CustomFeatures is a set of custom features to advertise in each
81
        // set.
82
        CustomFeatures map[Set][]lnwire.FeatureBit
83
}
84

85
// Manager is responsible for generating feature vectors for different requested
86
// feature sets.
87
type Manager struct {
88
        // fsets is a static map of feature set to raw feature vectors. Requests
89
        // are fulfilled by cloning these internal feature vectors.
90
        fsets map[Set]*lnwire.RawFeatureVector
91

92
        // configFeatures is a set of custom features that were "hard set" in
93
        // lnd's config that cannot be updated at runtime (as is the case with
94
        // our "standard" features that are defined in LND).
95
        configFeatures map[Set]*lnwire.FeatureVector
96
}
97

98
// NewManager creates a new feature Manager, applying any custom modifications
99
// to its feature sets before returning.
100
func NewManager(cfg Config) (*Manager, error) {
1✔
101
        return newManager(cfg, defaultSetDesc)
1✔
102
}
1✔
103

104
// newManager creates a new feature Manager, applying any custom modifications
105
// to its feature sets before returning. This method accepts the setDesc as its
106
// own parameter so that it can be unit tested.
107
func newManager(cfg Config, desc setDesc) (*Manager, error) {
1✔
108
        // First build the default feature vector for all known sets.
1✔
109
        fsets := make(map[Set]*lnwire.RawFeatureVector)
1✔
110
        for bit, sets := range desc {
2✔
111
                for set := range sets {
2✔
112
                        // Fetch the feature vector for this set, allocating a
1✔
113
                        // new one if it doesn't exist.
1✔
114
                        fv, ok := fsets[set]
1✔
115
                        if !ok {
2✔
116
                                fv = lnwire.NewRawFeatureVector()
1✔
117
                        }
1✔
118

119
                        // Set the configured bit on the feature vector,
120
                        // ensuring that we don't set two feature bits for the
121
                        // same pair.
122
                        err := fv.SafeSet(bit)
1✔
123
                        if err != nil {
1✔
124
                                return nil, fmt.Errorf("unable to set "+
×
125
                                        "%v in %v: %v", bit, set, err)
×
126
                        }
×
127

128
                        // Write the updated feature vector under its set.
129
                        fsets[set] = fv
1✔
130
                }
131
        }
132

133
        // Now, remove any features as directed by the config.
134
        configFeatures := make(map[Set]*lnwire.FeatureVector)
1✔
135
        for set, raw := range fsets {
2✔
136
                if cfg.NoTLVOnion {
2✔
137
                        raw.Unset(lnwire.TLVOnionPayloadOptional)
1✔
138
                        raw.Unset(lnwire.TLVOnionPayloadRequired)
1✔
139
                        raw.Unset(lnwire.PaymentAddrOptional)
1✔
140
                        raw.Unset(lnwire.PaymentAddrRequired)
1✔
141
                        raw.Unset(lnwire.MPPOptional)
1✔
142
                        raw.Unset(lnwire.MPPRequired)
1✔
143
                        raw.Unset(lnwire.RouteBlindingOptional)
1✔
144
                        raw.Unset(lnwire.RouteBlindingRequired)
1✔
145
                        raw.Unset(lnwire.Bolt11BlindedPathsOptional)
1✔
146
                        raw.Unset(lnwire.Bolt11BlindedPathsRequired)
1✔
147
                        raw.Unset(lnwire.AMPOptional)
1✔
148
                        raw.Unset(lnwire.AMPRequired)
1✔
149
                        raw.Unset(lnwire.KeysendOptional)
1✔
150
                        raw.Unset(lnwire.KeysendRequired)
1✔
151
                }
1✔
152
                if cfg.NoStaticRemoteKey {
2✔
153
                        raw.Unset(lnwire.StaticRemoteKeyOptional)
1✔
154
                        raw.Unset(lnwire.StaticRemoteKeyRequired)
1✔
155
                }
1✔
156
                if cfg.NoAnchors {
2✔
157
                        raw.Unset(lnwire.AnchorsZeroFeeHtlcTxOptional)
1✔
158
                        raw.Unset(lnwire.AnchorsZeroFeeHtlcTxRequired)
1✔
159

1✔
160
                        // If anchors are disabled, then we also need to
1✔
161
                        // disable all other features that depend on it as
1✔
162
                        // well, as otherwise we may create an invalid feature
1✔
163
                        // bit set.
1✔
164
                        for bit, depFeatures := range deps {
2✔
165
                                for depFeature := range depFeatures {
2✔
166
                                        switch {
1✔
167
                                        case depFeature == lnwire.AnchorsZeroFeeHtlcTxRequired:
×
168
                                                fallthrough
×
169
                                        case depFeature == lnwire.AnchorsZeroFeeHtlcTxOptional:
1✔
170
                                                raw.Unset(bit)
1✔
171
                                        }
172
                                }
173
                        }
174
                }
175
                if cfg.NoWumbo {
2✔
176
                        raw.Unset(lnwire.WumboChannelsOptional)
1✔
177
                        raw.Unset(lnwire.WumboChannelsRequired)
1✔
178
                }
1✔
179
                if cfg.NoScriptEnforcementLease {
2✔
180
                        raw.Unset(lnwire.ScriptEnforcedLeaseOptional)
1✔
181
                        raw.Unset(lnwire.ScriptEnforcedLeaseRequired)
1✔
182
                }
1✔
183
                if cfg.NoKeysend {
1✔
184
                        raw.Unset(lnwire.KeysendOptional)
×
185
                        raw.Unset(lnwire.KeysendRequired)
×
186
                }
×
187
                if cfg.NoOptionScidAlias {
2✔
188
                        raw.Unset(lnwire.ScidAliasOptional)
1✔
189
                        raw.Unset(lnwire.ScidAliasRequired)
1✔
190
                }
1✔
191
                if cfg.NoZeroConf {
2✔
192
                        raw.Unset(lnwire.ZeroConfOptional)
1✔
193
                        raw.Unset(lnwire.ZeroConfRequired)
1✔
194
                }
1✔
195
                if cfg.NoAnySegwit {
2✔
196
                        raw.Unset(lnwire.ShutdownAnySegwitOptional)
1✔
197
                        raw.Unset(lnwire.ShutdownAnySegwitRequired)
1✔
198
                }
1✔
199
                if cfg.NoTaprootChans {
2✔
200
                        raw.Unset(lnwire.SimpleTaprootChannelsOptionalStaging)
1✔
201
                        raw.Unset(lnwire.SimpleTaprootChannelsRequiredStaging)
1✔
202
                }
1✔
203
                if cfg.NoRouteBlinding {
2✔
204
                        raw.Unset(lnwire.RouteBlindingOptional)
1✔
205
                        raw.Unset(lnwire.RouteBlindingRequired)
1✔
206
                        raw.Unset(lnwire.Bolt11BlindedPathsOptional)
1✔
207
                        raw.Unset(lnwire.Bolt11BlindedPathsRequired)
1✔
208
                }
1✔
209
                if cfg.NoQuiescence {
1✔
210
                        raw.Unset(lnwire.QuiescenceOptional)
×
211
                }
×
212
                if cfg.NoTaprootOverlay {
2✔
213
                        raw.Unset(lnwire.SimpleTaprootOverlayChansOptional)
1✔
214
                        raw.Unset(lnwire.SimpleTaprootOverlayChansRequired)
1✔
215
                }
1✔
216
                if cfg.NoExperimentalEndorsement {
2✔
217
                        raw.Unset(lnwire.ExperimentalEndorsementOptional)
1✔
218
                        raw.Unset(lnwire.ExperimentalEndorsementRequired)
1✔
219
                }
1✔
220
                if cfg.NoRbfCoopClose {
2✔
221
                        raw.Unset(lnwire.RbfCoopCloseOptionalStaging)
1✔
222
                        raw.Unset(lnwire.RbfCoopCloseOptional)
1✔
223
                }
1✔
224

225
                for _, custom := range cfg.CustomFeatures[set] {
2✔
226
                        if custom > set.Maximum() {
1✔
227
                                return nil, fmt.Errorf("feature bit: %v "+
×
228
                                        "exceeds set: %v maximum: %v", custom,
×
229
                                        set, set.Maximum())
×
230
                        }
×
231

232
                        if raw.IsSet(custom) {
1✔
233
                                return nil, fmt.Errorf("feature bit: %v "+
×
234
                                        "already set", custom)
×
235
                        }
×
236

237
                        if err := raw.SafeSet(custom); err != nil {
1✔
238
                                return nil, fmt.Errorf("%w: could not set "+
×
239
                                        "feature: %d", err, custom)
×
240
                        }
×
241
                }
242

243
                // Track custom features separately so that we can check that
244
                // they aren't unset in subsequent updates. If there is no
245
                // entry for the set, the vector will just be empty.
246
                configFeatures[set] = lnwire.NewFeatureVector(
1✔
247
                        lnwire.NewRawFeatureVector(cfg.CustomFeatures[set]...),
1✔
248
                        lnwire.Features,
1✔
249
                )
1✔
250

1✔
251
                // Ensure that all of our feature sets properly set any
1✔
252
                // dependent features.
1✔
253
                fv := lnwire.NewFeatureVector(raw, lnwire.Features)
1✔
254
                err := ValidateDeps(fv)
1✔
255
                if err != nil {
1✔
256
                        return nil, fmt.Errorf("invalid feature set %v: %w",
×
257
                                set, err)
×
258
                }
×
259
        }
260

261
        return &Manager{
1✔
262
                fsets:          fsets,
1✔
263
                configFeatures: configFeatures,
1✔
264
        }, nil
1✔
265
}
266

267
// GetRaw returns a raw feature vector for the passed set. If no set is known,
268
// an empty raw feature vector is returned.
269
func (m *Manager) GetRaw(set Set) *lnwire.RawFeatureVector {
1✔
270
        if fv, ok := m.fsets[set]; ok {
2✔
271
                return fv.Clone()
1✔
272
        }
1✔
273

UNCOV
274
        return lnwire.NewRawFeatureVector()
×
275
}
276

277
// setRaw sets a new raw feature vector for the given set.
278
func (m *Manager) setRaw(set Set, raw *lnwire.RawFeatureVector) {
1✔
279
        m.fsets[set] = raw
1✔
280
}
1✔
281

282
// Get returns a feature vector for the passed set. If no set is known, an empty
283
// feature vector is returned.
284
func (m *Manager) Get(set Set) *lnwire.FeatureVector {
1✔
285
        raw := m.GetRaw(set)
1✔
286
        return lnwire.NewFeatureVector(raw, lnwire.Features)
1✔
287
}
1✔
288

289
// ListSets returns a list of the feature sets that our node supports.
290
func (m *Manager) ListSets() []Set {
1✔
291
        var sets []Set
1✔
292

1✔
293
        for set := range m.fsets {
2✔
294
                sets = append(sets, set)
1✔
295
        }
1✔
296

297
        return sets
1✔
298
}
299

300
// UpdateFeatureSets accepts a map of new feature vectors for each of the
301
// manager's known sets, validates that the update can be applied and modifies
302
// the feature manager's internal state. If a set is not included in the update
303
// map, it is left unchanged. The feature vectors provided are expected to
304
// include the current set of features, updated with desired bits added/removed.
305
func (m *Manager) UpdateFeatureSets(
306
        updates map[Set]*lnwire.RawFeatureVector) error {
1✔
307

1✔
308
        for set, newFeatures := range updates {
2✔
309
                if !set.valid() {
1✔
UNCOV
310
                        return fmt.Errorf("%w: set: %d", ErrUnknownSet, set)
×
UNCOV
311
                }
×
312

313
                if err := newFeatures.ValidatePairs(); err != nil {
1✔
UNCOV
314
                        return err
×
UNCOV
315
                }
×
316

317
                if err := m.Get(set).ValidateUpdate(
1✔
318
                        newFeatures, set.Maximum(),
1✔
319
                ); err != nil {
2✔
320
                        return err
1✔
321
                }
1✔
322

323
                // If any features were configured for this set, ensure that
324
                // they are still set in the new feature vector.
325
                if cfgFeat, haveCfgFeat := m.configFeatures[set]; haveCfgFeat {
2✔
326
                        for feature := range cfgFeat.Features() {
1✔
UNCOV
327
                                if !newFeatures.IsSet(feature) {
×
UNCOV
328
                                        return fmt.Errorf("%w: can't unset: "+
×
UNCOV
329
                                                "%d", ErrFeatureConfigured,
×
UNCOV
330
                                                feature)
×
UNCOV
331
                                }
×
332
                        }
333
                }
334

335
                fv := lnwire.NewFeatureVector(newFeatures, lnwire.Features)
1✔
336
                if err := ValidateDeps(fv); err != nil {
1✔
337
                        return err
×
338
                }
×
339
        }
340

341
        // Only update the current feature sets once every proposed set has
342
        // passed validation so that we don't partially update any sets then
343
        // fail out on a later set's validation.
344
        for set, features := range updates {
2✔
345
                m.setRaw(set, features.Clone())
1✔
346
        }
1✔
347

348
        return nil
1✔
349
}
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