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

lightningnetwork / lnd / 15843723163

24 Jun 2025 07:07AM UTC coverage: 56.04%. First build
15843723163

Pull #9985

github

web-flow
Merge 7f2e35788 into 1b95798fc
Pull Request #9985: multi: implement awareness of the final/production taproot channel variant

145 of 393 new or added lines in 18 files covered. (36.9%)

108424 of 193477 relevant lines covered (56.04%)

22573.28 hits per line

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

57.59
/funding/commitment_type_negotiation.go
1
package funding
2

3
import (
4
        "errors"
5

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

10
var (
11
        // errUnsupportedCommitmentType is an error returned when a specific
12
        // channel commitment type is being explicitly negotiated but either
13
        // peer of the channel does not support it.
14
        errUnsupportedChannelType = errors.New("requested channel type " +
15
                "not supported")
16
)
17

18
// negotiateCommitmentType negotiates the commitment type of a newly opened
19
// channel. If a desiredChanType is provided, explicit negotiation for said type
20
// will be attempted if the set of both local and remote features support it.
21
// Otherwise, implicit negotiation will be attempted.
22
//
23
// The returned ChannelType is nil when implicit negotiation is used. An error
24
// is only returned if desiredChanType is not supported.
25
func negotiateCommitmentType(desiredChanType *lnwire.ChannelType, local,
26
        remote *lnwire.FeatureVector) (*lnwire.ChannelType,
27
        lnwallet.CommitmentType, error) {
143✔
28

143✔
29
        // BOLT#2 specifies we MUST use explicit negotiation if both peers
143✔
30
        // signal for it.
143✔
31
        explicitNegotiation := hasFeatures(
143✔
32
                local, remote, lnwire.ExplicitChannelTypeOptional,
143✔
33
        )
143✔
34

143✔
35
        chanTypeRequested := desiredChanType != nil
143✔
36

143✔
37
        switch {
143✔
38
        case explicitNegotiation && chanTypeRequested:
33✔
39
                commitType, err := explicitNegotiateCommitmentType(
33✔
40
                        *desiredChanType, local, remote,
33✔
41
                )
33✔
42

33✔
43
                return desiredChanType, commitType, err
33✔
44

45
        // We don't have a specific channel type requested, so we select a
46
        // default type as if implicit negotiation were used, and then we
47
        // explicitly signal that default type.
48
        case explicitNegotiation && !chanTypeRequested:
9✔
49
                defaultChanType, commitType := implicitNegotiateCommitmentType(
9✔
50
                        local, remote,
9✔
51
                )
9✔
52

9✔
53
                return defaultChanType, commitType, nil
9✔
54

55
        // A specific channel type was requested, but we can't explicitly signal
56
        // it. So if implicit negotiation wouldn't select the desired channel
57
        // type, we must return an error.
58
        case !explicitNegotiation && chanTypeRequested:
2✔
59
                implicitChanType, commitType := implicitNegotiateCommitmentType(
2✔
60
                        local, remote,
2✔
61
                )
2✔
62

2✔
63
                expected := lnwire.RawFeatureVector(*desiredChanType)
2✔
64
                actual := lnwire.RawFeatureVector(*implicitChanType)
2✔
65
                if !expected.Equals(&actual) {
2✔
66
                        return nil, 0, errUnsupportedChannelType
×
67
                }
×
68

69
                return nil, commitType, nil
2✔
70

71
        default: // !explicitNegotiation && !chanTypeRequested
99✔
72
                _, commitType := implicitNegotiateCommitmentType(local, remote)
99✔
73

99✔
74
                return nil, commitType, nil
99✔
75
        }
76
}
77

78
// explicitNegotiateCommitmentType attempts to explicitly negotiate for a
79
// specific channel type. Since the channel type is comprised of a set of even
80
// feature bits, we also make sure each feature is supported by both peers. An
81
// error is returned if either peer does not support said channel type.
82
func explicitNegotiateCommitmentType(channelType lnwire.ChannelType, local,
83
        remote *lnwire.FeatureVector) (lnwallet.CommitmentType, error) {
33✔
84

33✔
85
        channelFeatures := lnwire.RawFeatureVector(channelType)
33✔
86

33✔
87
        switch {
33✔
88
        // Lease script enforcement + anchors zero fee + static remote key +
89
        // zero conf + scid alias features only.
90
        case channelFeatures.OnlyContains(
91
                lnwire.ZeroConfRequired,
92
                lnwire.ScidAliasRequired,
93
                lnwire.ScriptEnforcedLeaseRequired,
94
                lnwire.AnchorsZeroFeeHtlcTxRequired,
95
                lnwire.StaticRemoteKeyRequired,
96
        ):
×
97
                if !hasFeatures(
×
98
                        local, remote,
×
99
                        lnwire.ZeroConfOptional,
×
100
                        lnwire.ScriptEnforcedLeaseOptional,
×
101
                        lnwire.AnchorsZeroFeeHtlcTxOptional,
×
102
                        lnwire.StaticRemoteKeyOptional,
×
103
                ) {
×
104

×
105
                        return 0, errUnsupportedChannelType
×
106
                }
×
107
                return lnwallet.CommitmentTypeScriptEnforcedLease, nil
×
108

109
        // Anchors zero fee + static remote key + zero conf + scid alias
110
        // features only.
111
        case channelFeatures.OnlyContains(
112
                lnwire.ZeroConfRequired,
113
                lnwire.ScidAliasRequired,
114
                lnwire.AnchorsZeroFeeHtlcTxRequired,
115
                lnwire.StaticRemoteKeyRequired,
116
        ):
×
117
                if !hasFeatures(
×
118
                        local, remote,
×
119
                        lnwire.ZeroConfOptional,
×
120
                        lnwire.AnchorsZeroFeeHtlcTxOptional,
×
121
                        lnwire.StaticRemoteKeyOptional,
×
122
                ) {
×
123

×
124
                        return 0, errUnsupportedChannelType
×
125
                }
×
126
                return lnwallet.CommitmentTypeAnchorsZeroFeeHtlcTx, nil
×
127

128
        // Lease script enforcement + anchors zero fee + static remote key +
129
        // zero conf features only.
130
        case channelFeatures.OnlyContains(
131
                lnwire.ZeroConfRequired,
132
                lnwire.ScriptEnforcedLeaseRequired,
133
                lnwire.AnchorsZeroFeeHtlcTxRequired,
134
                lnwire.StaticRemoteKeyRequired,
135
        ):
2✔
136
                if !hasFeatures(
2✔
137
                        local, remote,
2✔
138
                        lnwire.ZeroConfOptional,
2✔
139
                        lnwire.ScriptEnforcedLeaseOptional,
2✔
140
                        lnwire.AnchorsZeroFeeHtlcTxOptional,
2✔
141
                        lnwire.StaticRemoteKeyOptional,
2✔
142
                ) {
2✔
143

×
144
                        return 0, errUnsupportedChannelType
×
145
                }
×
146
                return lnwallet.CommitmentTypeScriptEnforcedLease, nil
2✔
147

148
        // Anchors zero fee + static remote key + zero conf features only.
149
        case channelFeatures.OnlyContains(
150
                lnwire.ZeroConfRequired,
151
                lnwire.AnchorsZeroFeeHtlcTxRequired,
152
                lnwire.StaticRemoteKeyRequired,
153
        ):
4✔
154
                if !hasFeatures(
4✔
155
                        local, remote,
4✔
156
                        lnwire.ZeroConfOptional,
4✔
157
                        lnwire.AnchorsZeroFeeHtlcTxOptional,
4✔
158
                        lnwire.StaticRemoteKeyOptional,
4✔
159
                ) {
4✔
160

×
161
                        return 0, errUnsupportedChannelType
×
162
                }
×
163
                return lnwallet.CommitmentTypeAnchorsZeroFeeHtlcTx, nil
4✔
164

165
        // Lease script enforcement + anchors zero fee + static remote key +
166
        // option-scid-alias features only.
167
        case channelFeatures.OnlyContains(
168
                lnwire.ScidAliasRequired,
169
                lnwire.ScriptEnforcedLeaseRequired,
170
                lnwire.AnchorsZeroFeeHtlcTxRequired,
171
                lnwire.StaticRemoteKeyRequired,
172
        ):
2✔
173
                if !hasFeatures(
2✔
174
                        local, remote,
2✔
175
                        lnwire.ScidAliasOptional,
2✔
176
                        lnwire.ScriptEnforcedLeaseOptional,
2✔
177
                        lnwire.AnchorsZeroFeeHtlcTxOptional,
2✔
178
                        lnwire.StaticRemoteKeyOptional,
2✔
179
                ) {
2✔
180

×
181
                        return 0, errUnsupportedChannelType
×
182
                }
×
183
                return lnwallet.CommitmentTypeScriptEnforcedLease, nil
2✔
184

185
        // Anchors zero fee + static remote key + option-scid-alias features
186
        // only.
187
        case channelFeatures.OnlyContains(
188
                lnwire.ScidAliasRequired,
189
                lnwire.AnchorsZeroFeeHtlcTxRequired,
190
                lnwire.StaticRemoteKeyRequired,
191
        ):
2✔
192
                if !hasFeatures(
2✔
193
                        local, remote,
2✔
194
                        lnwire.ScidAliasOptional,
2✔
195
                        lnwire.AnchorsZeroFeeHtlcTxOptional,
2✔
196
                        lnwire.StaticRemoteKeyOptional,
2✔
197
                ) {
2✔
198

×
199
                        return 0, errUnsupportedChannelType
×
200
                }
×
201
                return lnwallet.CommitmentTypeAnchorsZeroFeeHtlcTx, nil
2✔
202

203
        // Lease script enforcement + anchors zero fee + static remote key
204
        // features only.
205
        case channelFeatures.OnlyContains(
206
                lnwire.ScriptEnforcedLeaseRequired,
207
                lnwire.AnchorsZeroFeeHtlcTxRequired,
208
                lnwire.StaticRemoteKeyRequired,
209
        ):
×
210
                if !hasFeatures(
×
211
                        local, remote,
×
212
                        lnwire.ScriptEnforcedLeaseOptional,
×
213
                        lnwire.AnchorsZeroFeeHtlcTxOptional,
×
214
                        lnwire.StaticRemoteKeyOptional,
×
215
                ) {
×
216

×
217
                        return 0, errUnsupportedChannelType
×
218
                }
×
219
                return lnwallet.CommitmentTypeScriptEnforcedLease, nil
×
220

221
        // Anchors zero fee + static remote key features only.
222
        case channelFeatures.OnlyContains(
223
                lnwire.AnchorsZeroFeeHtlcTxRequired,
224
                lnwire.StaticRemoteKeyRequired,
225
        ):
5✔
226
                if !hasFeatures(
5✔
227
                        local, remote,
5✔
228
                        lnwire.AnchorsZeroFeeHtlcTxOptional,
5✔
229
                        lnwire.StaticRemoteKeyOptional,
5✔
230
                ) {
7✔
231

2✔
232
                        return 0, errUnsupportedChannelType
2✔
233
                }
2✔
234
                return lnwallet.CommitmentTypeAnchorsZeroFeeHtlcTx, nil
3✔
235

236
        // Static remote key feature only.
237
        case channelFeatures.OnlyContains(lnwire.StaticRemoteKeyRequired):
2✔
238
                if !hasFeatures(local, remote, lnwire.StaticRemoteKeyOptional) {
2✔
239
                        return 0, errUnsupportedChannelType
×
240
                }
×
241
                return lnwallet.CommitmentTypeTweakless, nil
2✔
242

243
        // Simple taproot channels only (final feature bits).
244
        case channelFeatures.OnlyContains(
245
                lnwire.SimpleTaprootChannelsRequiredFinal,
246
        ):
4✔
247

4✔
248
                if !hasFeatures(
4✔
249
                        local, remote,
4✔
250
                        lnwire.SimpleTaprootChannelsOptionalFinal,
4✔
251
                ) {
6✔
252

2✔
253
                        return 0, errUnsupportedChannelType
2✔
254
                }
2✔
255

256
                return lnwallet.CommitmentTypeSimpleTaprootFinal, nil
2✔
257

258
        // Simple taproot channels only (staging feature bits).
259
        case channelFeatures.OnlyContains(
260
                lnwire.SimpleTaprootChannelsRequiredStaging,
261
        ):
2✔
262

2✔
263
                if !hasFeatures(
2✔
264
                        local, remote,
2✔
265
                        lnwire.SimpleTaprootChannelsOptionalStaging,
2✔
266
                ) {
2✔
267

×
268
                        return 0, errUnsupportedChannelType
×
269
                }
×
270

271
                return lnwallet.CommitmentTypeSimpleTaproot, nil
2✔
272

273
        // Simple taproot channels with scid only (final feature bits).
274
        case channelFeatures.OnlyContains(
275
                lnwire.SimpleTaprootChannelsRequiredFinal,
276
                lnwire.ScidAliasRequired,
277
        ):
2✔
278

2✔
279
                if !hasFeatures(
2✔
280
                        local, remote,
2✔
281
                        lnwire.SimpleTaprootChannelsOptionalFinal,
2✔
282
                        lnwire.ScidAliasOptional,
2✔
283
                ) {
2✔
NEW
284

×
NEW
285
                        return 0, errUnsupportedChannelType
×
NEW
286
                }
×
287

288
                return lnwallet.CommitmentTypeSimpleTaprootFinal, nil
2✔
289

290
        // Simple taproot channels with scid only (staging feature bits).
291
        case channelFeatures.OnlyContains(
292
                lnwire.SimpleTaprootChannelsRequiredStaging,
293
                lnwire.ScidAliasRequired,
294
        ):
×
295

×
296
                if !hasFeatures(
×
297
                        local, remote,
×
298
                        lnwire.SimpleTaprootChannelsOptionalStaging,
×
299
                        lnwire.ScidAliasOptional,
×
300
                ) {
×
301

×
302
                        return 0, errUnsupportedChannelType
×
303
                }
×
304

305
                return lnwallet.CommitmentTypeSimpleTaproot, nil
×
306

307
        // Simple taproot channels with zero conf only (final feature bits).
308
        case channelFeatures.OnlyContains(
309
                lnwire.SimpleTaprootChannelsRequiredFinal,
310
                lnwire.ZeroConfRequired,
311
        ):
2✔
312

2✔
313
                if !hasFeatures(
2✔
314
                        local, remote,
2✔
315
                        lnwire.SimpleTaprootChannelsOptionalFinal,
2✔
316
                        lnwire.ZeroConfOptional,
2✔
317
                ) {
2✔
NEW
318

×
NEW
319
                        return 0, errUnsupportedChannelType
×
NEW
320
                }
×
321

322
                return lnwallet.CommitmentTypeSimpleTaprootFinal, nil
2✔
323

324
        // Simple taproot channels with zero conf only (staging feature bits).
325
        case channelFeatures.OnlyContains(
326
                lnwire.SimpleTaprootChannelsRequiredStaging,
327
                lnwire.ZeroConfRequired,
328
        ):
2✔
329

2✔
330
                if !hasFeatures(
2✔
331
                        local, remote,
2✔
332
                        lnwire.SimpleTaprootChannelsOptionalStaging,
2✔
333
                        lnwire.ZeroConfOptional,
2✔
334
                ) {
2✔
335

×
336
                        return 0, errUnsupportedChannelType
×
337
                }
×
338

339
                return lnwallet.CommitmentTypeSimpleTaproot, nil
2✔
340

341
        // Simple taproot channels with scid and zero conf (final feature bits).
342
        case channelFeatures.OnlyContains(
343
                lnwire.SimpleTaprootChannelsRequiredFinal,
344
                lnwire.ZeroConfRequired,
345
                lnwire.ScidAliasRequired,
346
        ):
2✔
347

2✔
348
                if !hasFeatures(
2✔
349
                        local, remote,
2✔
350
                        lnwire.SimpleTaprootChannelsOptionalFinal,
2✔
351
                        lnwire.ZeroConfOptional,
2✔
352
                        lnwire.ScidAliasOptional,
2✔
353
                ) {
2✔
NEW
354

×
NEW
355
                        return 0, errUnsupportedChannelType
×
NEW
356
                }
×
357

358
                return lnwallet.CommitmentTypeSimpleTaprootFinal, nil
2✔
359

360
        // Simple taproot channels with scid and zero conf (staging feature bits).
361
        case channelFeatures.OnlyContains(
362
                lnwire.SimpleTaprootChannelsRequiredStaging,
363
                lnwire.ZeroConfRequired,
364
                lnwire.ScidAliasRequired,
365
        ):
×
366

×
367
                if !hasFeatures(
×
368
                        local, remote,
×
369
                        lnwire.SimpleTaprootChannelsOptionalStaging,
×
370
                        lnwire.ZeroConfOptional,
×
NEW
371
                        lnwire.ScidAliasOptional,
×
372
                ) {
×
373

×
374
                        return 0, errUnsupportedChannelType
×
375
                }
×
376

377
                return lnwallet.CommitmentTypeSimpleTaproot, nil
×
378

379
        // Simple taproot channels overlay only.
380
        case channelFeatures.OnlyContains(
381
                lnwire.SimpleTaprootOverlayChansRequired,
382
        ):
×
383

×
384
                if !hasFeatures(
×
385
                        local, remote,
×
386
                        lnwire.SimpleTaprootOverlayChansOptional,
×
387
                ) {
×
388

×
389
                        return 0, errUnsupportedChannelType
×
390
                }
×
391

392
                return lnwallet.CommitmentTypeSimpleTaprootOverlay, nil
×
393

394
        // Simple taproot overlay channels with scid only.
395
        case channelFeatures.OnlyContains(
396
                lnwire.SimpleTaprootOverlayChansRequired,
397
                lnwire.ScidAliasRequired,
398
        ):
×
399

×
400
                if !hasFeatures(
×
401
                        local, remote,
×
402
                        lnwire.SimpleTaprootOverlayChansOptional,
×
403
                        lnwire.ScidAliasOptional,
×
404
                ) {
×
405

×
406
                        return 0, errUnsupportedChannelType
×
407
                }
×
408

409
                return lnwallet.CommitmentTypeSimpleTaprootOverlay, nil
×
410

411
        // Simple taproot overlay channels with zero conf only.
412
        case channelFeatures.OnlyContains(
413
                lnwire.SimpleTaprootOverlayChansRequired,
414
                lnwire.ZeroConfRequired,
415
        ):
×
416

×
417
                if !hasFeatures(
×
418
                        local, remote,
×
419
                        lnwire.SimpleTaprootOverlayChansOptional,
×
420
                        lnwire.ZeroConfOptional,
×
421
                ) {
×
422

×
423
                        return 0, errUnsupportedChannelType
×
424
                }
×
425

426
                return lnwallet.CommitmentTypeSimpleTaprootOverlay, nil
×
427

428
        // Simple taproot overlay channels with scid and zero conf.
429
        case channelFeatures.OnlyContains(
430
                lnwire.SimpleTaprootOverlayChansRequired,
431
                lnwire.ZeroConfRequired,
432
                lnwire.ScidAliasRequired,
433
        ):
×
434

×
435
                if !hasFeatures(
×
436
                        local, remote,
×
437
                        lnwire.SimpleTaprootOverlayChansOptional,
×
438
                        lnwire.ZeroConfOptional,
×
439
                        lnwire.ScidAliasOptional,
×
440
                ) {
×
441

×
442
                        return 0, errUnsupportedChannelType
×
443
                }
×
444

445
                return lnwallet.CommitmentTypeSimpleTaprootOverlay, nil
×
446

447
        // No features, use legacy commitment type.
448
        case channelFeatures.IsEmpty():
2✔
449
                return lnwallet.CommitmentTypeLegacy, nil
2✔
450

451
        default:
×
452
                return 0, errUnsupportedChannelType
×
453
        }
454
}
455

456
// implicitNegotiateCommitmentType negotiates the commitment type of a channel
457
// implicitly by choosing the latest type supported by the local and remote
458
// features.
459
func implicitNegotiateCommitmentType(local,
460
        remote *lnwire.FeatureVector) (*lnwire.ChannelType,
461
        lnwallet.CommitmentType) {
110✔
462

110✔
463
        // If both peers are signalling support for simple taproot channels with
110✔
464
        // final feature bits, prefer that over staging bits.
110✔
465
        if hasFeatures(local, remote, lnwire.SimpleTaprootChannelsOptionalFinal) {
114✔
466
                chanType := lnwire.ChannelType(*lnwire.NewRawFeatureVector(
4✔
467
                        lnwire.SimpleTaprootChannelsRequiredFinal,
4✔
468
                ))
4✔
469

4✔
470
                return &chanType, lnwallet.CommitmentTypeSimpleTaprootFinal
4✔
471
        }
4✔
472

473
        // If both peers are signalling support for simple taproot channels with
474
        // staging feature bits, use those.
475
        if hasFeatures(local, remote, lnwire.SimpleTaprootChannelsOptionalStaging) {
108✔
476
                chanType := lnwire.ChannelType(*lnwire.NewRawFeatureVector(
2✔
477
                        lnwire.SimpleTaprootChannelsRequiredStaging,
2✔
478
                ))
2✔
479

2✔
480
                return &chanType, lnwallet.CommitmentTypeSimpleTaproot
2✔
481
        }
2✔
482

483
        // If both peers are signalling support for anchor commitments with
484
        // zero-fee HTLC transactions, we'll use this type.
485
        if hasFeatures(local, remote, lnwire.AnchorsZeroFeeHtlcTxOptional) {
109✔
486
                chanType := lnwire.ChannelType(*lnwire.NewRawFeatureVector(
5✔
487
                        lnwire.AnchorsZeroFeeHtlcTxRequired,
5✔
488
                        lnwire.StaticRemoteKeyRequired,
5✔
489
                ))
5✔
490

5✔
491
                return &chanType, lnwallet.CommitmentTypeAnchorsZeroFeeHtlcTx
5✔
492
        }
5✔
493

494
        // Since we don't want to support the "legacy" anchor type, we will fall
495
        // back to static remote key if the nodes don't support the zero fee
496
        // HTLC tx anchor type.
497
        //
498
        // If both nodes are signaling the proper feature bit for tweakless
499
        // commitments, we'll use that.
500
        if hasFeatures(local, remote, lnwire.StaticRemoteKeyOptional) {
101✔
501
                chanType := lnwire.ChannelType(*lnwire.NewRawFeatureVector(
2✔
502
                        lnwire.StaticRemoteKeyRequired,
2✔
503
                ))
2✔
504

2✔
505
                return &chanType, lnwallet.CommitmentTypeTweakless
2✔
506
        }
2✔
507

508
        // Otherwise we'll fall back to the legacy type.
509
        chanType := lnwire.ChannelType(*lnwire.NewRawFeatureVector())
97✔
510
        return &chanType, lnwallet.CommitmentTypeLegacy
97✔
511
}
512

513
// hasFeatures determines whether a set of features is supported by both the set
514
// of local and remote features.
515
func hasFeatures(local, remote *lnwire.FeatureVector,
516
        features ...lnwire.FeatureBit) bool {
696✔
517

696✔
518
        for _, feature := range features {
1,429✔
519
                if !local.HasFeature(feature) || !remote.HasFeature(feature) {
1,341✔
520
                        return false
608✔
521
                }
608✔
522
        }
523
        return true
88✔
524
}
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