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

lightningnetwork / lnd / 18197857992

02 Oct 2025 03:32PM UTC coverage: 66.622% (-0.02%) from 66.646%
18197857992

Pull #10267

github

web-flow
Merge 0d9bfccfe into 1c2ff4a7e
Pull Request #10267: [g175] multi: small G175 preparations

24 of 141 new or added lines in 12 files covered. (17.02%)

64 existing lines in 20 files now uncovered.

137216 of 205963 relevant lines covered (66.62%)

21302.01 hits per line

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

65.85
/lnwire/channel_update_2.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "fmt"
6
        "io"
7

8
        "github.com/btcsuite/btcd/chaincfg"
9
        "github.com/btcsuite/btcd/chaincfg/chainhash"
10
        "github.com/lightningnetwork/lnd/tlv"
11
)
12

13
const (
14
        defaultCltvExpiryDelta           = uint16(80)
15
        defaultHtlcMinMsat               = MilliSatoshi(1)
16
        defaultFeeBaseMsat               = uint32(1000)
17
        defaultFeeProportionalMillionths = uint32(1)
18
)
19

20
// ChannelUpdate2 message is used after taproot channel has been initially
21
// announced. Each side independently announces its fees and minimum expiry for
22
// HTLCs and other parameters. This message is also used to redeclare initially
23
// set channel parameters.
24
type ChannelUpdate2 struct {
25
        // ChainHash denotes the target chain that this channel was opened
26
        // within. This value should be the genesis hash of the target chain.
27
        // Along with the short channel ID, this uniquely identifies the
28
        // channel globally in a blockchain.
29
        ChainHash tlv.RecordT[tlv.TlvType0, chainhash.Hash]
30

31
        // ShortChannelID is the unique description of the funding transaction.
32
        ShortChannelID tlv.RecordT[tlv.TlvType2, ShortChannelID]
33

34
        // BlockHeight allows ordering in the case of multiple announcements. We
35
        // should ignore the message if block height is not greater than the
36
        // last-received. The block height must always be greater or equal to
37
        // the block height that the channel funding transaction was confirmed
38
        // in.
39
        BlockHeight tlv.RecordT[tlv.TlvType4, uint32]
40

41
        // DisabledFlags is an optional bitfield that describes various reasons
42
        // that the node is communicating that the channel should be considered
43
        // disabled.
44
        DisabledFlags tlv.RecordT[tlv.TlvType6, ChanUpdateDisableFlags]
45

46
        // SecondPeer is used to indicate which node the channel node has
47
        // created and signed this message. If this field is present, it was
48
        // node 2 otherwise it was node 1.
49
        SecondPeer tlv.OptionalRecordT[tlv.TlvType8, TrueBoolean]
50

51
        // CLTVExpiryDelta is the minimum number of blocks this node requires to
52
        // be added to the expiry of HTLCs. This is a security parameter
53
        // determined by the node operator. This value represents the required
54
        // gap between the time locks of the incoming and outgoing HTLC's set
55
        // to this node.
56
        CLTVExpiryDelta tlv.RecordT[tlv.TlvType10, uint16]
57

58
        // HTLCMinimumMsat is the minimum HTLC value which will be accepted.
59
        HTLCMinimumMsat tlv.RecordT[tlv.TlvType12, MilliSatoshi]
60

61
        // HtlcMaximumMsat is the maximum HTLC value which will be accepted.
62
        HTLCMaximumMsat tlv.RecordT[tlv.TlvType14, MilliSatoshi]
63

64
        // FeeBaseMsat is the base fee that must be used for incoming HTLC's to
65
        // this particular channel. This value will be tacked onto the required
66
        // for a payment independent of the size of the payment.
67
        FeeBaseMsat tlv.RecordT[tlv.TlvType16, uint32]
68

69
        // FeeProportionalMillionths is the fee rate that will be charged per
70
        // millionth of a satoshi.
71
        FeeProportionalMillionths tlv.RecordT[tlv.TlvType18, uint32]
72

73
        // InboundFee is an optional TLV record that contains the fee
74
        // information for incoming HTLCs.
75
        // TODO(elle): assign normal tlv type?
76
        InboundFee tlv.OptionalRecordT[tlv.TlvType55555, Fee]
77

78
        // Signature is used to validate the announced data and prove the
79
        // ownership of node id.
80
        Signature tlv.RecordT[tlv.TlvType160, Sig]
81

82
        // Any extra fields in the signed range that we do not yet know about,
83
        // but we need to keep them for signature validation and to produce a
84
        // valid message.
85
        ExtraSignedFields
86
}
87

88
// GossipVersion returns the gossip version that this message is part of.
89
//
90
// NOTE: this is part of the GossipMessage interface.
NEW
91
func (c *ChannelUpdate2) GossipVersion() GossipVersion {
×
NEW
92
        return GossipVersion2
×
NEW
93
}
×
94

95
// Encode serializes the target ChannelUpdate2 into the passed io.Writer
96
// observing the protocol version specified.
97
//
98
// This is part of the lnwire.Message interface.
99
func (c *ChannelUpdate2) Encode(w *bytes.Buffer, _ uint32) error {
102✔
100
        return EncodePureTLVMessage(c, w)
102✔
101
}
102✔
102

103
// Decode deserializes a serialized ChannelUpdate2 stored in the passed
104
// io.Reader observing the specified protocol version.
105
//
106
// This is part of the lnwire.Message interface.
107
func (c *ChannelUpdate2) Decode(r io.Reader, _ uint32) error {
188✔
108
        // First extract into extra opaque data.
188✔
109
        var tlvRecords ExtraOpaqueData
188✔
110
        if err := ReadElements(r, &tlvRecords); err != nil {
188✔
111
                return err
×
112
        }
×
113

114
        var (
188✔
115
                chainHash  = tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
188✔
116
                secondPeer = tlv.ZeroRecordT[tlv.TlvType8, TrueBoolean]()
188✔
117
                inboundFee = tlv.ZeroRecordT[tlv.TlvType55555, Fee]()
188✔
118
        )
188✔
119
        typeMap, err := tlvRecords.ExtractRecords(
188✔
120
                &chainHash, &c.ShortChannelID, &c.BlockHeight, &c.DisabledFlags,
188✔
121
                &secondPeer, &c.CLTVExpiryDelta, &c.HTLCMinimumMsat,
188✔
122
                &c.HTLCMaximumMsat, &c.FeeBaseMsat,
188✔
123
                &c.FeeProportionalMillionths, &inboundFee,
188✔
124
                &c.Signature,
188✔
125
        )
188✔
126
        if err != nil {
273✔
127
                return err
85✔
128
        }
85✔
129
        c.Signature.Val.ForceSchnorr()
103✔
130

103✔
131
        // By default, the chain-hash is the bitcoin mainnet genesis block hash.
103✔
132
        c.ChainHash.Val = *chaincfg.MainNetParams.GenesisHash
103✔
133
        if _, ok := typeMap[c.ChainHash.TlvType()]; ok {
204✔
134
                c.ChainHash.Val = chainHash.Val
101✔
135
        }
101✔
136

137
        // The presence of the second_peer tlv type indicates "true".
138
        if _, ok := typeMap[c.SecondPeer.TlvType()]; ok {
159✔
139
                c.SecondPeer = tlv.SomeRecordT(secondPeer)
56✔
140
        }
56✔
141

142
        // If the CLTV expiry delta was not encoded, then set it to the default
143
        // value.
144
        if _, ok := typeMap[c.CLTVExpiryDelta.TlvType()]; !ok {
106✔
145
                c.CLTVExpiryDelta.Val = defaultCltvExpiryDelta
3✔
146
        }
3✔
147

148
        // If the HTLC Minimum msat was not encoded, then set it to the default
149
        // value.
150
        if _, ok := typeMap[c.HTLCMinimumMsat.TlvType()]; !ok {
117✔
151
                c.HTLCMinimumMsat.Val = defaultHtlcMinMsat
14✔
152
        }
14✔
153

154
        // If the base fee was not encoded, then set it to the default value.
155
        if _, ok := typeMap[c.FeeBaseMsat.TlvType()]; !ok {
105✔
156
                c.FeeBaseMsat.Val = defaultFeeBaseMsat
2✔
157
        }
2✔
158

159
        // If the proportional fee was not encoded, then set it to the default
160
        // value.
161
        if _, ok := typeMap[c.FeeProportionalMillionths.TlvType()]; !ok {
117✔
162
                c.FeeProportionalMillionths.Val = defaultFeeProportionalMillionths //nolint:ll
14✔
163
        }
14✔
164

165
        // If the inbound fee was encoded, set it.
166
        if _, ok := typeMap[c.InboundFee.TlvType()]; ok {
149✔
167
                c.InboundFee = tlv.SomeRecordT(inboundFee)
46✔
168
        }
46✔
169

170
        c.ExtraSignedFields = ExtraSignedFieldsFromTypeMap(typeMap)
103✔
171

103✔
172
        return nil
103✔
173
}
174

175
// AllRecords returns all the TLV records for the message. This will include all
176
// the records we know about along with any that we don't know about but that
177
// fall in the signed TLV range.
178
//
179
// NOTE: this is part of the PureTLVMessage interface.
180
func (c *ChannelUpdate2) AllRecords() []tlv.Record {
102✔
181
        var recordProducers []tlv.RecordProducer
102✔
182

102✔
183
        // The chain-hash record is only included if it is _not_ equal to the
102✔
184
        // bitcoin mainnet genisis block hash.
102✔
185
        if !c.ChainHash.Val.IsEqual(chaincfg.MainNetParams.GenesisHash) {
203✔
186
                hash := tlv.ZeroRecordT[tlv.TlvType0, [32]byte]()
101✔
187
                hash.Val = c.ChainHash.Val
101✔
188

101✔
189
                recordProducers = append(recordProducers, &hash)
101✔
190
        }
101✔
191

192
        recordProducers = append(recordProducers,
102✔
193
                &c.ShortChannelID, &c.BlockHeight, &c.Signature,
102✔
194
        )
102✔
195

102✔
196
        // Only include the disable flags if any bit is set.
102✔
197
        if !c.DisabledFlags.Val.IsEnabled() {
185✔
198
                recordProducers = append(recordProducers, &c.DisabledFlags)
83✔
199
        }
83✔
200

201
        // We only need to encode the second peer boolean if it is true
202
        c.SecondPeer.WhenSome(func(r tlv.RecordT[tlv.TlvType8, TrueBoolean]) {
158✔
203
                recordProducers = append(recordProducers, &r)
56✔
204
        })
56✔
205

206
        // We only encode the cltv expiry delta if it is not equal to the
207
        // default.
208
        if c.CLTVExpiryDelta.Val != defaultCltvExpiryDelta {
202✔
209
                recordProducers = append(recordProducers, &c.CLTVExpiryDelta)
100✔
210
        }
100✔
211

212
        if c.HTLCMinimumMsat.Val != defaultHtlcMinMsat {
191✔
213
                recordProducers = append(recordProducers, &c.HTLCMinimumMsat)
89✔
214
        }
89✔
215

216
        recordProducers = append(recordProducers, &c.HTLCMaximumMsat)
102✔
217

102✔
218
        if c.FeeBaseMsat.Val != defaultFeeBaseMsat {
203✔
219
                recordProducers = append(recordProducers, &c.FeeBaseMsat)
101✔
220
        }
101✔
221

222
        if c.FeeProportionalMillionths.Val != defaultFeeProportionalMillionths {
191✔
223
                recordProducers = append(
89✔
224
                        recordProducers, &c.FeeProportionalMillionths,
89✔
225
                )
89✔
226
        }
89✔
227

228
        c.InboundFee.WhenSome(func(r tlv.RecordT[tlv.TlvType55555, Fee]) {
148✔
229
                recordProducers = append(recordProducers, &r)
46✔
230
        })
46✔
231

232
        recordProducers = append(recordProducers, RecordsAsProducers(
102✔
233
                tlv.MapToRecords(c.ExtraSignedFields),
102✔
234
        )...)
102✔
235

102✔
236
        return ProduceRecordsSorted(recordProducers...)
102✔
237
}
238

239
// MsgType returns the integer uniquely identifying this message type on the
240
// wire.
241
//
242
// This is part of the lnwire.Message interface.
243
func (c *ChannelUpdate2) MsgType() MessageType {
101✔
244
        return MsgChannelUpdate2
101✔
245
}
101✔
246

247
// SerializedSize returns the serialized size of the message in bytes.
248
//
249
// This is part of the lnwire.SizeableMessage interface.
250
func (c *ChannelUpdate2) SerializedSize() (uint32, error) {
×
251
        return MessageSerializedSize(c)
×
252
}
×
253

254
// A compile time check to ensure ChannelUpdate2 implements the
255
// lnwire.Message interface.
256
var _ Message = (*ChannelUpdate2)(nil)
257

258
// A compile time check to ensure ChannelUpdate2 implements the
259
// lnwire.PureTLVMessage interface.
260
var _ PureTLVMessage = (*ChannelUpdate2)(nil)
261

262
// SCID returns the ShortChannelID of the channel that the update applies to.
263
//
264
// NOTE: this is part of the ChannelUpdate interface.
265
func (c *ChannelUpdate2) SCID() ShortChannelID {
×
266
        return c.ShortChannelID.Val
×
267
}
×
268

269
// IsNode1 is true if the update was produced by node 1 of the channel peers.
270
// Node 1 is the node with the lexicographically smaller public key.
271
//
272
// NOTE: this is part of the ChannelUpdate interface.
273
func (c *ChannelUpdate2) IsNode1() bool {
×
274
        return c.SecondPeer.IsNone()
×
275
}
×
276

277
// IsDisabled is true if the update is announcing that the channel should be
278
// considered disabled.
279
//
280
// NOTE: this is part of the ChannelUpdate interface.
281
func (c *ChannelUpdate2) IsDisabled() bool {
×
282
        return !c.DisabledFlags.Val.IsEnabled()
×
283
}
×
284

285
// GetChainHash returns the hash of the chain that the message is referring to.
286
//
287
// NOTE: this is part of the ChannelUpdate interface.
288
func (c *ChannelUpdate2) GetChainHash() chainhash.Hash {
×
289
        return c.ChainHash.Val
×
290
}
×
291

292
// ForwardingPolicy returns the set of forwarding constraints of the update.
293
//
294
// NOTE: this is part of the ChannelUpdate interface.
295
func (c *ChannelUpdate2) ForwardingPolicy() *ForwardingPolicy {
×
296
        return &ForwardingPolicy{
×
297
                TimeLockDelta: c.CLTVExpiryDelta.Val,
×
298
                BaseFee:       MilliSatoshi(c.FeeBaseMsat.Val),
×
299
                FeeRate:       MilliSatoshi(c.FeeProportionalMillionths.Val),
×
300
                MinHTLC:       c.HTLCMinimumMsat.Val,
×
301
                HasMaxHTLC:    true,
×
302
                MaxHTLC:       c.HTLCMaximumMsat.Val,
×
303
        }
×
304
}
×
305

306
// CmpAge can be used to determine if the update is older or newer than the
307
// passed update. It returns 1 if this update is newer, -1 if it is older, and
308
// 0 if they are the same age.
309
//
310
// NOTE: this is part of the ChannelUpdate interface.
311
func (c *ChannelUpdate2) CmpAge(update ChannelUpdate) (CompareResult, error) {
×
312
        other, ok := update.(*ChannelUpdate2)
×
313
        if !ok {
×
314
                return 0, fmt.Errorf("expected *ChannelUpdate2, got: %T",
×
315
                        update)
×
316
        }
×
317

318
        switch {
×
319
        case c.BlockHeight.Val > other.BlockHeight.Val:
×
320
                return GreaterThan, nil
×
321
        case c.BlockHeight.Val < other.BlockHeight.Val:
×
322
                return LessThan, nil
×
323
        default:
×
324
                return EqualTo, nil
×
325
        }
326
}
327

328
// SetDisabledFlag can be used to adjust the disabled flag of an update.
329
//
330
// NOTE: this is part of the ChannelUpdate interface.
331
func (c *ChannelUpdate2) SetDisabledFlag(disabled bool) {
×
332
        if disabled {
×
333
                c.DisabledFlags.Val |= ChanUpdateDisableIncoming
×
334
                c.DisabledFlags.Val |= ChanUpdateDisableOutgoing
×
335
        } else {
×
336
                c.DisabledFlags.Val &^= ChanUpdateDisableIncoming
×
337
                c.DisabledFlags.Val &^= ChanUpdateDisableOutgoing
×
338
        }
×
339
}
340

341
// SetSCID can be used to overwrite the SCID of the update.
342
//
343
// NOTE: this is part of the ChannelUpdate interface.
344
func (c *ChannelUpdate2) SetSCID(scid ShortChannelID) {
×
345
        c.ShortChannelID.Val = scid
×
346
}
×
347

348
// A compile time check to ensure ChannelUpdate2 implements the
349
// lnwire.ChannelUpdate interface.
350
var _ ChannelUpdate = (*ChannelUpdate2)(nil)
351

352
// ChanUpdateDisableFlags is a bit vector that can be used to indicate various
353
// reasons for the channel being marked as disabled.
354
type ChanUpdateDisableFlags uint8
355

356
const (
357
        // ChanUpdateDisableIncoming is a bit indicates that a channel is
358
        // disabled in the inbound direction meaning that the node broadcasting
359
        // the update is communicating that they cannot receive funds.
360
        ChanUpdateDisableIncoming ChanUpdateDisableFlags = 1 << iota
361

362
        // ChanUpdateDisableOutgoing is a bit indicates that a channel is
363
        // disabled in the outbound direction meaning that the node broadcasting
364
        // the update is communicating that they cannot send or route funds.
365
        ChanUpdateDisableOutgoing = 2
366
)
367

368
// IncomingDisabled returns true if the ChanUpdateDisableIncoming bit is set.
369
func (c ChanUpdateDisableFlags) IncomingDisabled() bool {
×
370
        return c&ChanUpdateDisableIncoming == ChanUpdateDisableIncoming
×
371
}
×
372

373
// OutgoingDisabled returns true if the ChanUpdateDisableOutgoing bit is set.
374
func (c ChanUpdateDisableFlags) OutgoingDisabled() bool {
×
375
        return c&ChanUpdateDisableOutgoing == ChanUpdateDisableOutgoing
×
376
}
×
377

378
// IsEnabled returns true if none of the disable bits are set.
379
func (c ChanUpdateDisableFlags) IsEnabled() bool {
102✔
380
        return c == 0
102✔
381
}
102✔
382

383
// String returns the bitfield flags as a string.
384
func (c ChanUpdateDisableFlags) String() string {
×
385
        return fmt.Sprintf("%08b", c)
×
386
}
×
387

388
// Record returns the tlv record for the disable flags.
389
func (c *ChanUpdateDisableFlags) Record() tlv.Record {
271✔
390
        return tlv.MakeStaticRecord(0, c, 1, encodeDisableFlags,
271✔
391
                decodeDisableFlags)
271✔
392
}
271✔
393

394
func encodeDisableFlags(w io.Writer, val interface{}, buf *[8]byte) error {
83✔
395
        if v, ok := val.(*ChanUpdateDisableFlags); ok {
166✔
396
                flagsInt := uint8(*v)
83✔
397

83✔
398
                return tlv.EUint8(w, &flagsInt, buf)
83✔
399
        }
83✔
400

401
        return tlv.NewTypeForEncodingErr(val, "lnwire.ChanUpdateDisableFlags")
×
402
}
403

404
func decodeDisableFlags(r io.Reader, val interface{}, buf *[8]byte,
405
        l uint64) error {
83✔
406

83✔
407
        if v, ok := val.(*ChanUpdateDisableFlags); ok {
166✔
408
                var flagsInt uint8
83✔
409
                err := tlv.DUint8(r, &flagsInt, buf, l)
83✔
410
                if err != nil {
83✔
411
                        return err
×
412
                }
×
413

414
                *v = ChanUpdateDisableFlags(flagsInt)
83✔
415

83✔
416
                return nil
83✔
417
        }
418

419
        return tlv.NewTypeForDecodingErr(val, "lnwire.ChanUpdateDisableFlags",
×
420
                l, l)
×
421
}
422

423
// TrueBoolean is a record that indicates true or false using the presence of
424
// the record. If the record is absent, it indicates false. If it is present,
425
// it indicates true.
426
type TrueBoolean struct{}
427

428
// Record returns the tlv record for the boolean entry.
429
func (b *TrueBoolean) Record() tlv.Record {
341✔
430
        return tlv.MakeStaticRecord(
341✔
431
                0, b, 0, booleanEncoder, booleanDecoder,
341✔
432
        )
341✔
433
}
341✔
434

435
func booleanEncoder(_ io.Writer, val interface{}, _ *[8]byte) error {
98✔
436
        if _, ok := val.(*TrueBoolean); ok {
196✔
437
                return nil
98✔
438
        }
98✔
439

440
        return tlv.NewTypeForEncodingErr(val, "TrueBoolean")
×
441
}
442

443
func booleanDecoder(_ io.Reader, val interface{}, _ *[8]byte,
444
        l uint64) error {
78✔
445

78✔
446
        if _, ok := val.(*TrueBoolean); ok && (l == 0 || l == 1) {
156✔
447
                return nil
78✔
448
        }
78✔
449

450
        return tlv.NewTypeForEncodingErr(val, "TrueBoolean")
×
451
}
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