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

lightningnetwork / lnd / 11170835610

03 Oct 2024 10:41PM UTC coverage: 49.188% (-9.6%) from 58.738%
11170835610

push

github

web-flow
Merge pull request #9154 from ziggie1984/master

multi: bump btcd version.

3 of 6 new or added lines in 6 files covered. (50.0%)

26110 existing lines in 428 files now uncovered.

97359 of 197934 relevant lines covered (49.19%)

1.04 hits per line

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

79.31
/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
        // NoTaprootOverlay unsets the taproot overlay channel feature bits.
67
        NoTaprootOverlay bool
68

69
        // CustomFeatures is a set of custom features to advertise in each
70
        // set.
71
        CustomFeatures map[Set][]lnwire.FeatureBit
72
}
73

74
// Manager is responsible for generating feature vectors for different requested
75
// feature sets.
76
type Manager struct {
77
        // fsets is a static map of feature set to raw feature vectors. Requests
78
        // are fulfilled by cloning these internal feature vectors.
79
        fsets map[Set]*lnwire.RawFeatureVector
80

81
        // configFeatures is a set of custom features that were "hard set" in
82
        // lnd's config that cannot be updated at runtime (as is the case with
83
        // our "standard" features that are defined in LND).
84
        configFeatures map[Set]*lnwire.FeatureVector
85
}
86

87
// NewManager creates a new feature Manager, applying any custom modifications
88
// to its feature sets before returning.
89
func NewManager(cfg Config) (*Manager, error) {
2✔
90
        return newManager(cfg, defaultSetDesc)
2✔
91
}
2✔
92

93
// newManager creates a new feature Manager, applying any custom modifications
94
// to its feature sets before returning. This method accepts the setDesc as its
95
// own parameter so that it can be unit tested.
96
func newManager(cfg Config, desc setDesc) (*Manager, error) {
2✔
97
        // First build the default feature vector for all known sets.
2✔
98
        fsets := make(map[Set]*lnwire.RawFeatureVector)
2✔
99
        for bit, sets := range desc {
4✔
100
                for set := range sets {
4✔
101
                        // Fetch the feature vector for this set, allocating a
2✔
102
                        // new one if it doesn't exist.
2✔
103
                        fv, ok := fsets[set]
2✔
104
                        if !ok {
4✔
105
                                fv = lnwire.NewRawFeatureVector()
2✔
106
                        }
2✔
107

108
                        // Set the configured bit on the feature vector,
109
                        // ensuring that we don't set two feature bits for the
110
                        // same pair.
111
                        err := fv.SafeSet(bit)
2✔
112
                        if err != nil {
2✔
113
                                return nil, fmt.Errorf("unable to set "+
×
114
                                        "%v in %v: %v", bit, set, err)
×
115
                        }
×
116

117
                        // Write the updated feature vector under its set.
118
                        fsets[set] = fv
2✔
119
                }
120
        }
121

122
        // Now, remove any features as directed by the config.
123
        configFeatures := make(map[Set]*lnwire.FeatureVector)
2✔
124
        for set, raw := range fsets {
4✔
125
                if cfg.NoTLVOnion {
4✔
126
                        raw.Unset(lnwire.TLVOnionPayloadOptional)
2✔
127
                        raw.Unset(lnwire.TLVOnionPayloadRequired)
2✔
128
                        raw.Unset(lnwire.PaymentAddrOptional)
2✔
129
                        raw.Unset(lnwire.PaymentAddrRequired)
2✔
130
                        raw.Unset(lnwire.MPPOptional)
2✔
131
                        raw.Unset(lnwire.MPPRequired)
2✔
132
                        raw.Unset(lnwire.RouteBlindingOptional)
2✔
133
                        raw.Unset(lnwire.RouteBlindingRequired)
2✔
134
                        raw.Unset(lnwire.Bolt11BlindedPathsOptional)
2✔
135
                        raw.Unset(lnwire.Bolt11BlindedPathsRequired)
2✔
136
                        raw.Unset(lnwire.AMPOptional)
2✔
137
                        raw.Unset(lnwire.AMPRequired)
2✔
138
                        raw.Unset(lnwire.KeysendOptional)
2✔
139
                        raw.Unset(lnwire.KeysendRequired)
2✔
140
                }
2✔
141
                if cfg.NoStaticRemoteKey {
4✔
142
                        raw.Unset(lnwire.StaticRemoteKeyOptional)
2✔
143
                        raw.Unset(lnwire.StaticRemoteKeyRequired)
2✔
144
                }
2✔
145
                if cfg.NoAnchors {
4✔
146
                        raw.Unset(lnwire.AnchorsZeroFeeHtlcTxOptional)
2✔
147
                        raw.Unset(lnwire.AnchorsZeroFeeHtlcTxRequired)
2✔
148

2✔
149
                        // If anchors are disabled, then we also need to
2✔
150
                        // disable all other features that depend on it as
2✔
151
                        // well, as otherwise we may create an invalid feature
2✔
152
                        // bit set.
2✔
153
                        for bit, depFeatures := range deps {
4✔
154
                                for depFeature := range depFeatures {
4✔
155
                                        switch {
2✔
156
                                        case depFeature == lnwire.AnchorsZeroFeeHtlcTxRequired:
×
157
                                                fallthrough
×
158
                                        case depFeature == lnwire.AnchorsZeroFeeHtlcTxOptional:
2✔
159
                                                raw.Unset(bit)
2✔
160
                                        }
161
                                }
162
                        }
163
                }
164
                if cfg.NoWumbo {
4✔
165
                        raw.Unset(lnwire.WumboChannelsOptional)
2✔
166
                        raw.Unset(lnwire.WumboChannelsRequired)
2✔
167
                }
2✔
168
                if cfg.NoScriptEnforcementLease {
4✔
169
                        raw.Unset(lnwire.ScriptEnforcedLeaseOptional)
2✔
170
                        raw.Unset(lnwire.ScriptEnforcedLeaseRequired)
2✔
171
                }
2✔
172
                if cfg.NoKeysend {
2✔
173
                        raw.Unset(lnwire.KeysendOptional)
×
174
                        raw.Unset(lnwire.KeysendRequired)
×
175
                }
×
176
                if cfg.NoOptionScidAlias {
4✔
177
                        raw.Unset(lnwire.ScidAliasOptional)
2✔
178
                        raw.Unset(lnwire.ScidAliasRequired)
2✔
179
                }
2✔
180
                if cfg.NoZeroConf {
4✔
181
                        raw.Unset(lnwire.ZeroConfOptional)
2✔
182
                        raw.Unset(lnwire.ZeroConfRequired)
2✔
183
                }
2✔
184
                if cfg.NoAnySegwit {
4✔
185
                        raw.Unset(lnwire.ShutdownAnySegwitOptional)
2✔
186
                        raw.Unset(lnwire.ShutdownAnySegwitRequired)
2✔
187
                }
2✔
188
                if cfg.NoTaprootChans {
4✔
189
                        raw.Unset(lnwire.SimpleTaprootChannelsOptionalStaging)
2✔
190
                        raw.Unset(lnwire.SimpleTaprootChannelsRequiredStaging)
2✔
191
                }
2✔
192
                if cfg.NoRouteBlinding {
4✔
193
                        raw.Unset(lnwire.RouteBlindingOptional)
2✔
194
                        raw.Unset(lnwire.RouteBlindingRequired)
2✔
195
                        raw.Unset(lnwire.Bolt11BlindedPathsOptional)
2✔
196
                        raw.Unset(lnwire.Bolt11BlindedPathsRequired)
2✔
197
                }
2✔
198
                if cfg.NoTaprootOverlay {
4✔
199
                        raw.Unset(lnwire.SimpleTaprootOverlayChansOptional)
2✔
200
                        raw.Unset(lnwire.SimpleTaprootOverlayChansRequired)
2✔
201
                }
2✔
202
                for _, custom := range cfg.CustomFeatures[set] {
2✔
UNCOV
203
                        if custom > set.Maximum() {
×
204
                                return nil, fmt.Errorf("feature bit: %v "+
×
205
                                        "exceeds set: %v maximum: %v", custom,
×
206
                                        set, set.Maximum())
×
207
                        }
×
208

UNCOV
209
                        if raw.IsSet(custom) {
×
210
                                return nil, fmt.Errorf("feature bit: %v "+
×
211
                                        "already set", custom)
×
212
                        }
×
213

UNCOV
214
                        if err := raw.SafeSet(custom); err != nil {
×
215
                                return nil, fmt.Errorf("%w: could not set "+
×
216
                                        "feature: %d", err, custom)
×
217
                        }
×
218
                }
219

220
                // Track custom features separately so that we can check that
221
                // they aren't unset in subsequent updates. If there is no
222
                // entry for the set, the vector will just be empty.
223
                configFeatures[set] = lnwire.NewFeatureVector(
2✔
224
                        lnwire.NewRawFeatureVector(cfg.CustomFeatures[set]...),
2✔
225
                        lnwire.Features,
2✔
226
                )
2✔
227

2✔
228
                // Ensure that all of our feature sets properly set any
2✔
229
                // dependent features.
2✔
230
                fv := lnwire.NewFeatureVector(raw, lnwire.Features)
2✔
231
                err := ValidateDeps(fv)
2✔
232
                if err != nil {
2✔
233
                        return nil, fmt.Errorf("invalid feature set %v: %w",
×
234
                                set, err)
×
235
                }
×
236
        }
237

238
        return &Manager{
2✔
239
                fsets:          fsets,
2✔
240
                configFeatures: configFeatures,
2✔
241
        }, nil
2✔
242
}
243

244
// GetRaw returns a raw feature vector for the passed set. If no set is known,
245
// an empty raw feature vector is returned.
246
func (m *Manager) GetRaw(set Set) *lnwire.RawFeatureVector {
2✔
247
        if fv, ok := m.fsets[set]; ok {
4✔
248
                return fv.Clone()
2✔
249
        }
2✔
250

UNCOV
251
        return lnwire.NewRawFeatureVector()
×
252
}
253

254
// setRaw sets a new raw feature vector for the given set.
255
func (m *Manager) setRaw(set Set, raw *lnwire.RawFeatureVector) {
2✔
256
        m.fsets[set] = raw
2✔
257
}
2✔
258

259
// Get returns a feature vector for the passed set. If no set is known, an empty
260
// feature vector is returned.
261
func (m *Manager) Get(set Set) *lnwire.FeatureVector {
2✔
262
        raw := m.GetRaw(set)
2✔
263
        return lnwire.NewFeatureVector(raw, lnwire.Features)
2✔
264
}
2✔
265

266
// ListSets returns a list of the feature sets that our node supports.
267
func (m *Manager) ListSets() []Set {
2✔
268
        var sets []Set
2✔
269

2✔
270
        for set := range m.fsets {
4✔
271
                sets = append(sets, set)
2✔
272
        }
2✔
273

274
        return sets
2✔
275
}
276

277
// UpdateFeatureSets accepts a map of new feature vectors for each of the
278
// manager's known sets, validates that the update can be applied and modifies
279
// the feature manager's internal state. If a set is not included in the update
280
// map, it is left unchanged. The feature vectors provided are expected to
281
// include the current set of features, updated with desired bits added/removed.
282
func (m *Manager) UpdateFeatureSets(
283
        updates map[Set]*lnwire.RawFeatureVector) error {
2✔
284

2✔
285
        for set, newFeatures := range updates {
4✔
286
                if !set.valid() {
2✔
UNCOV
287
                        return fmt.Errorf("%w: set: %d", ErrUnknownSet, set)
×
UNCOV
288
                }
×
289

290
                if err := newFeatures.ValidatePairs(); err != nil {
2✔
UNCOV
291
                        return err
×
UNCOV
292
                }
×
293

294
                if err := m.Get(set).ValidateUpdate(
2✔
295
                        newFeatures, set.Maximum(),
2✔
296
                ); err != nil {
4✔
297
                        return err
2✔
298
                }
2✔
299

300
                // If any features were configured for this set, ensure that
301
                // they are still set in the new feature vector.
302
                if cfgFeat, haveCfgFeat := m.configFeatures[set]; haveCfgFeat {
4✔
303
                        for feature := range cfgFeat.Features() {
2✔
UNCOV
304
                                if !newFeatures.IsSet(feature) {
×
UNCOV
305
                                        return fmt.Errorf("%w: can't unset: "+
×
UNCOV
306
                                                "%d", ErrFeatureConfigured,
×
UNCOV
307
                                                feature)
×
UNCOV
308
                                }
×
309
                        }
310
                }
311

312
                fv := lnwire.NewFeatureVector(newFeatures, lnwire.Features)
2✔
313
                if err := ValidateDeps(fv); err != nil {
2✔
314
                        return err
×
315
                }
×
316
        }
317

318
        // Only update the current feature sets once every proposed set has
319
        // passed validation so that we don't partially update any sets then
320
        // fail out on a later set's validation.
321
        for set, features := range updates {
4✔
322
                m.setRaw(set, features.Clone())
2✔
323
        }
2✔
324

325
        return nil
2✔
326
}
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