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

lightningnetwork / lnd / 9915780197

13 Jul 2024 12:30AM UTC coverage: 49.268% (-9.1%) from 58.413%
9915780197

push

github

web-flow
Merge pull request #8653 from ProofOfKeags/fn-prim

DynComms [0/n]: `fn` package additions

92837 of 188433 relevant lines covered (49.27%)

1.55 hits per line

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

80.12
/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
        // CustomFeatures is a set of custom features to advertise in each
67
        // set.
68
        CustomFeatures map[Set][]lnwire.FeatureBit
69
}
70

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

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

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

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

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

114
                        // Write the updated feature vector under its set.
115
                        fsets[set] = fv
3✔
116
                }
117
        }
118

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

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

198
                        if raw.IsSet(custom) {
3✔
199
                                return nil, fmt.Errorf("feature bit: %v "+
×
200
                                        "already set", custom)
×
201
                        }
×
202

203
                        if err := raw.SafeSet(custom); err != nil {
3✔
204
                                return nil, fmt.Errorf("%w: could not set "+
×
205
                                        "feature: %d", err, custom)
×
206
                        }
×
207
                }
208

209
                // Track custom features separately so that we can check that
210
                // they aren't unset in subsequent updates. If there is no
211
                // entry for the set, the vector will just be empty.
212
                configFeatures[set] = lnwire.NewFeatureVector(
3✔
213
                        lnwire.NewRawFeatureVector(cfg.CustomFeatures[set]...),
3✔
214
                        lnwire.Features,
3✔
215
                )
3✔
216

3✔
217
                // Ensure that all of our feature sets properly set any
3✔
218
                // dependent features.
3✔
219
                fv := lnwire.NewFeatureVector(raw, lnwire.Features)
3✔
220
                err := ValidateDeps(fv)
3✔
221
                if err != nil {
3✔
222
                        return nil, fmt.Errorf("invalid feature set %v: %w",
×
223
                                set, err)
×
224
                }
×
225
        }
226

227
        return &Manager{
3✔
228
                fsets:          fsets,
3✔
229
                configFeatures: configFeatures,
3✔
230
        }, nil
3✔
231
}
232

233
// GetRaw returns a raw feature vector for the passed set. If no set is known,
234
// an empty raw feature vector is returned.
235
func (m *Manager) GetRaw(set Set) *lnwire.RawFeatureVector {
3✔
236
        if fv, ok := m.fsets[set]; ok {
6✔
237
                return fv.Clone()
3✔
238
        }
3✔
239

240
        return lnwire.NewRawFeatureVector()
×
241
}
242

243
// setRaw sets a new raw feature vector for the given set.
244
func (m *Manager) setRaw(set Set, raw *lnwire.RawFeatureVector) {
3✔
245
        m.fsets[set] = raw
3✔
246
}
3✔
247

248
// Get returns a feature vector for the passed set. If no set is known, an empty
249
// feature vector is returned.
250
func (m *Manager) Get(set Set) *lnwire.FeatureVector {
3✔
251
        raw := m.GetRaw(set)
3✔
252
        return lnwire.NewFeatureVector(raw, lnwire.Features)
3✔
253
}
3✔
254

255
// ListSets returns a list of the feature sets that our node supports.
256
func (m *Manager) ListSets() []Set {
3✔
257
        var sets []Set
3✔
258

3✔
259
        for set := range m.fsets {
6✔
260
                sets = append(sets, set)
3✔
261
        }
3✔
262

263
        return sets
3✔
264
}
265

266
// UpdateFeatureSets accepts a map of new feature vectors for each of the
267
// manager's known sets, validates that the update can be applied and modifies
268
// the feature manager's internal state. If a set is not included in the update
269
// map, it is left unchanged. The feature vectors provided are expected to
270
// include the current set of features, updated with desired bits added/removed.
271
func (m *Manager) UpdateFeatureSets(
272
        updates map[Set]*lnwire.RawFeatureVector) error {
3✔
273

3✔
274
        for set, newFeatures := range updates {
6✔
275
                if !set.valid() {
3✔
276
                        return fmt.Errorf("%w: set: %d", ErrUnknownSet, set)
×
277
                }
×
278

279
                if err := newFeatures.ValidatePairs(); err != nil {
3✔
280
                        return err
×
281
                }
×
282

283
                if err := m.Get(set).ValidateUpdate(
3✔
284
                        newFeatures, set.Maximum(),
3✔
285
                ); err != nil {
6✔
286
                        return err
3✔
287
                }
3✔
288

289
                // If any features were configured for this set, ensure that
290
                // they are still set in the new feature vector.
291
                if cfgFeat, haveCfgFeat := m.configFeatures[set]; haveCfgFeat {
6✔
292
                        for feature := range cfgFeat.Features() {
3✔
293
                                if !newFeatures.IsSet(feature) {
×
294
                                        return fmt.Errorf("%w: can't unset: "+
×
295
                                                "%d", ErrFeatureConfigured,
×
296
                                                feature)
×
297
                                }
×
298
                        }
299
                }
300

301
                fv := lnwire.NewFeatureVector(newFeatures, lnwire.Features)
3✔
302
                if err := ValidateDeps(fv); err != nil {
3✔
303
                        return err
×
304
                }
×
305
        }
306

307
        // Only update the current feature sets once every proposed set has
308
        // passed validation so that we don't partially update any sets then
309
        // fail out on a later set's validation.
310
        for set, features := range updates {
6✔
311
                m.setRaw(set, features.Clone())
3✔
312
        }
3✔
313

314
        return nil
3✔
315
}
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