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

lightningnetwork / lnd / 15736109134

18 Jun 2025 02:46PM UTC coverage: 58.197% (-10.1%) from 68.248%
15736109134

Pull #9752

github

web-flow
Merge d2634a68c into 31c74f20f
Pull Request #9752: routerrpc: reject payment to invoice that don't have payment secret or blinded paths

6 of 13 new or added lines in 2 files covered. (46.15%)

28331 existing lines in 455 files now uncovered.

97860 of 168153 relevant lines covered (58.2%)

1.81 hits per line

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

46.74
/lnwire/channel_update.go
1
package lnwire
2

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

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

12
// ChanUpdateMsgFlags is a bitfield that signals whether optional fields are
13
// present in the ChannelUpdate.
14
type ChanUpdateMsgFlags uint8
15

16
const (
17
        // ChanUpdateRequiredMaxHtlc is a bit that indicates whether the
18
        // required htlc_maximum_msat field is present in this ChannelUpdate.
19
        ChanUpdateRequiredMaxHtlc ChanUpdateMsgFlags = 1 << iota
20
)
21

22
// String returns the bitfield flags as a string.
23
func (c ChanUpdateMsgFlags) String() string {
3✔
24
        return fmt.Sprintf("%08b", c)
3✔
25
}
3✔
26

27
// HasMaxHtlc returns true if the htlc_maximum_msat option bit is set in the
28
// message flags.
29
func (c ChanUpdateMsgFlags) HasMaxHtlc() bool {
3✔
30
        return c&ChanUpdateRequiredMaxHtlc != 0
3✔
31
}
3✔
32

33
// ChanUpdateChanFlags is a bitfield that signals various options concerning a
34
// particular channel edge. Each bit is to be examined in order to determine
35
// how the ChannelUpdate message is to be interpreted.
36
type ChanUpdateChanFlags uint8
37

38
const (
39
        // ChanUpdateDirection indicates the direction of a channel update. If
40
        // this bit is set to 0 if Node1 (the node with the "smaller" Node ID)
41
        // is updating the channel, and to 1 otherwise.
42
        ChanUpdateDirection ChanUpdateChanFlags = 1 << iota
43

44
        // ChanUpdateDisabled is a bit that indicates if the channel edge
45
        // selected by the ChanUpdateDirection bit is to be treated as being
46
        // disabled.
47
        ChanUpdateDisabled
48
)
49

50
// IsDisabled determines whether the channel flags has the disabled bit set.
51
func (c ChanUpdateChanFlags) IsDisabled() bool {
3✔
52
        return c&ChanUpdateDisabled == ChanUpdateDisabled
3✔
53
}
3✔
54

55
// String returns the bitfield flags as a string.
56
func (c ChanUpdateChanFlags) String() string {
3✔
57
        return fmt.Sprintf("%08b", c)
3✔
58
}
3✔
59

60
// ChannelUpdate1 message is used after channel has been initially announced.
61
// Each side independently announces its fees and minimum expiry for HTLCs and
62
// other parameters. Also this message is used to redeclare initially set
63
// channel parameters.
64
type ChannelUpdate1 struct {
65
        // Signature is used to validate the announced data and prove the
66
        // ownership of node id.
67
        Signature Sig
68

69
        // ChainHash denotes the target chain that this channel was opened
70
        // within. This value should be the genesis hash of the target chain.
71
        // Along with the short channel ID, this uniquely identifies the
72
        // channel globally in a blockchain.
73
        ChainHash chainhash.Hash
74

75
        // ShortChannelID is the unique description of the funding transaction.
76
        ShortChannelID ShortChannelID
77

78
        // Timestamp allows ordering in the case of multiple announcements. We
79
        // should ignore the message if timestamp is not greater than
80
        // the last-received.
81
        Timestamp uint32
82

83
        // MessageFlags is a bitfield that describes whether optional fields
84
        // are present in this update. Currently, the least-significant bit
85
        // must be set to 1 if the optional field MaxHtlc is present.
86
        MessageFlags ChanUpdateMsgFlags
87

88
        // ChannelFlags is a bitfield that describes additional meta-data
89
        // concerning how the update is to be interpreted. Currently, the
90
        // least-significant bit must be set to 0 if the creating node
91
        // corresponds to the first node in the previously sent channel
92
        // announcement and 1 otherwise. If the second bit is set, then the
93
        // channel is set to be disabled.
94
        ChannelFlags ChanUpdateChanFlags
95

96
        // TimeLockDelta is the minimum number of blocks this node requires to
97
        // be added to the expiry of HTLCs. This is a security parameter
98
        // determined by the node operator. This value represents the required
99
        // gap between the time locks of the incoming and outgoing HTLC's set
100
        // to this node.
101
        TimeLockDelta uint16
102

103
        // HtlcMinimumMsat is the minimum HTLC value which will be accepted.
104
        HtlcMinimumMsat MilliSatoshi
105

106
        // BaseFee is the base fee that must be used for incoming HTLC's to
107
        // this particular channel. This value will be tacked onto the required
108
        // for a payment independent of the size of the payment.
109
        BaseFee uint32
110

111
        // FeeRate is the fee rate that will be charged per millionth of a
112
        // satoshi.
113
        FeeRate uint32
114

115
        // HtlcMaximumMsat is the maximum HTLC value which will be accepted.
116
        HtlcMaximumMsat MilliSatoshi
117

118
        // InboundFee is an optional TLV record that contains the fee
119
        // information for incoming HTLCs.
120
        InboundFee tlv.OptionalRecordT[tlv.TlvType55555, Fee]
121

122
        // ExtraData is the set of data that was appended to this message to
123
        // fill out the full maximum transport message size. These fields can
124
        // be used to specify optional data such as custom TLV fields.
125
        ExtraOpaqueData ExtraOpaqueData
126
}
127

128
// A compile time check to ensure ChannelUpdate implements the lnwire.Message
129
// interface.
130
var _ Message = (*ChannelUpdate1)(nil)
131

132
// A compile time check to ensure ChannelUpdate1 implements the
133
// lnwire.SizeableMessage interface.
134
var _ SizeableMessage = (*ChannelUpdate1)(nil)
135

136
// Decode deserializes a serialized ChannelUpdate stored in the passed
137
// io.Reader observing the specified protocol version.
138
//
139
// This is part of the lnwire.Message interface.
140
func (a *ChannelUpdate1) Decode(r io.Reader, _ uint32) error {
3✔
141
        err := ReadElements(r,
3✔
142
                &a.Signature,
3✔
143
                a.ChainHash[:],
3✔
144
                &a.ShortChannelID,
3✔
145
                &a.Timestamp,
3✔
146
                &a.MessageFlags,
3✔
147
                &a.ChannelFlags,
3✔
148
                &a.TimeLockDelta,
3✔
149
                &a.HtlcMinimumMsat,
3✔
150
                &a.BaseFee,
3✔
151
                &a.FeeRate,
3✔
152
        )
3✔
153
        if err != nil {
3✔
UNCOV
154
                return err
×
UNCOV
155
        }
×
156

157
        // Now check whether the max HTLC field is present and read it if so.
158
        if a.MessageFlags.HasMaxHtlc() {
6✔
159
                if err := ReadElements(r, &a.HtlcMaximumMsat); err != nil {
3✔
UNCOV
160
                        return err
×
UNCOV
161
                }
×
162
        }
163

164
        var tlvRecords ExtraOpaqueData
3✔
165
        if err := ReadElements(r, &tlvRecords); err != nil {
3✔
166
                return err
×
167
        }
×
168

169
        var inboundFee = a.InboundFee.Zero()
3✔
170
        typeMap, err := tlvRecords.ExtractRecords(&inboundFee)
3✔
171
        if err != nil {
3✔
UNCOV
172
                return err
×
UNCOV
173
        }
×
174

175
        val, ok := typeMap[a.InboundFee.TlvType()]
3✔
176
        if ok && val == nil {
6✔
177
                a.InboundFee = tlv.SomeRecordT(inboundFee)
3✔
178
        }
3✔
179

180
        if len(tlvRecords) != 0 {
6✔
181
                a.ExtraOpaqueData = tlvRecords
3✔
182
        }
3✔
183

184
        return nil
3✔
185
}
186

187
// Encode serializes the target ChannelUpdate into the passed io.Writer
188
// observing the protocol version specified.
189
//
190
// This is part of the lnwire.Message interface.
191
func (a *ChannelUpdate1) Encode(w *bytes.Buffer, pver uint32) error {
3✔
192
        if err := WriteSig(w, a.Signature); err != nil {
3✔
193
                return err
×
194
        }
×
195

196
        if err := WriteBytes(w, a.ChainHash[:]); err != nil {
3✔
197
                return err
×
198
        }
×
199

200
        if err := WriteShortChannelID(w, a.ShortChannelID); err != nil {
3✔
201
                return err
×
202
        }
×
203

204
        if err := WriteUint32(w, a.Timestamp); err != nil {
3✔
205
                return err
×
206
        }
×
207

208
        if err := WriteChanUpdateMsgFlags(w, a.MessageFlags); err != nil {
3✔
209
                return err
×
210
        }
×
211

212
        if err := WriteChanUpdateChanFlags(w, a.ChannelFlags); err != nil {
3✔
213
                return err
×
214
        }
×
215

216
        if err := WriteUint16(w, a.TimeLockDelta); err != nil {
3✔
217
                return err
×
218
        }
×
219

220
        if err := WriteMilliSatoshi(w, a.HtlcMinimumMsat); err != nil {
3✔
221
                return err
×
222
        }
×
223

224
        if err := WriteUint32(w, a.BaseFee); err != nil {
3✔
225
                return err
×
226
        }
×
227

228
        if err := WriteUint32(w, a.FeeRate); err != nil {
3✔
229
                return err
×
230
        }
×
231

232
        // Now append optional fields if they are set. Currently, the only
233
        // optional field is max HTLC.
234
        if a.MessageFlags.HasMaxHtlc() {
6✔
235
                err := WriteMilliSatoshi(w, a.HtlcMaximumMsat)
3✔
236
                if err != nil {
3✔
237
                        return err
×
238
                }
×
239
        }
240

241
        recordProducers := make([]tlv.RecordProducer, 0, 1)
3✔
242
        a.InboundFee.WhenSome(func(fee tlv.RecordT[tlv.TlvType55555, Fee]) {
6✔
243
                recordProducers = append(recordProducers, &fee)
3✔
244
        })
3✔
245

246
        err := EncodeMessageExtraData(&a.ExtraOpaqueData, recordProducers...)
3✔
247
        if err != nil {
3✔
248
                return err
×
249
        }
×
250

251
        // Finally, append any extra opaque data.
252
        return WriteBytes(w, a.ExtraOpaqueData)
3✔
253
}
254

255
// MsgType returns the integer uniquely identifying this message type on the
256
// wire.
257
//
258
// This is part of the lnwire.Message interface.
259
func (a *ChannelUpdate1) MsgType() MessageType {
3✔
260
        return MsgChannelUpdate
3✔
261
}
3✔
262

263
// DataToSign is used to retrieve part of the announcement message which should
264
// be signed.
265
func (a *ChannelUpdate1) DataToSign() ([]byte, error) {
3✔
266
        // We should not include the signatures itself.
3✔
267
        b := make([]byte, 0, MaxMsgBody)
3✔
268
        buf := bytes.NewBuffer(b)
3✔
269
        if err := WriteBytes(buf, a.ChainHash[:]); err != nil {
3✔
270
                return nil, err
×
271
        }
×
272

273
        if err := WriteShortChannelID(buf, a.ShortChannelID); err != nil {
3✔
274
                return nil, err
×
275
        }
×
276

277
        if err := WriteUint32(buf, a.Timestamp); err != nil {
3✔
278
                return nil, err
×
279
        }
×
280

281
        if err := WriteChanUpdateMsgFlags(buf, a.MessageFlags); err != nil {
3✔
282
                return nil, err
×
283
        }
×
284

285
        if err := WriteChanUpdateChanFlags(buf, a.ChannelFlags); err != nil {
3✔
286
                return nil, err
×
287
        }
×
288

289
        if err := WriteUint16(buf, a.TimeLockDelta); err != nil {
3✔
290
                return nil, err
×
291
        }
×
292

293
        if err := WriteMilliSatoshi(buf, a.HtlcMinimumMsat); err != nil {
3✔
294
                return nil, err
×
295
        }
×
296

297
        if err := WriteUint32(buf, a.BaseFee); err != nil {
3✔
298
                return nil, err
×
299
        }
×
300

301
        if err := WriteUint32(buf, a.FeeRate); err != nil {
3✔
302
                return nil, err
×
303
        }
×
304

305
        // Now append optional fields if they are set. Currently, the only
306
        // optional field is max HTLC.
307
        if a.MessageFlags.HasMaxHtlc() {
6✔
308
                err := WriteMilliSatoshi(buf, a.HtlcMaximumMsat)
3✔
309
                if err != nil {
3✔
310
                        return nil, err
×
311
                }
×
312
        }
313

314
        // Finally, append any extra opaque data.
315
        if err := WriteBytes(buf, a.ExtraOpaqueData); err != nil {
3✔
316
                return nil, err
×
317
        }
×
318

319
        return buf.Bytes(), nil
3✔
320
}
321

322
// SCID returns the ShortChannelID of the channel that the update applies to.
323
//
324
// NOTE: this is part of the ChannelUpdate interface.
325
func (a *ChannelUpdate1) SCID() ShortChannelID {
×
326
        return a.ShortChannelID
×
327
}
×
328

329
// IsNode1 is true if the update was produced by node 1 of the channel peers.
330
// Node 1 is the node with the lexicographically smaller public key.
331
//
332
// NOTE: this is part of the ChannelUpdate interface.
333
func (a *ChannelUpdate1) IsNode1() bool {
×
334
        return a.ChannelFlags&ChanUpdateDirection == 0
×
335
}
×
336

337
// IsDisabled is true if the update is announcing that the channel should be
338
// considered disabled.
339
//
340
// NOTE: this is part of the ChannelUpdate interface.
341
func (a *ChannelUpdate1) IsDisabled() bool {
×
342
        return a.ChannelFlags&ChanUpdateDisabled == ChanUpdateDisabled
×
343
}
×
344

345
// GetChainHash returns the hash of the chain that the message is referring to.
346
//
347
// NOTE: this is part of the ChannelUpdate interface.
348
func (a *ChannelUpdate1) GetChainHash() chainhash.Hash {
×
349
        return a.ChainHash
×
350
}
×
351

352
// ForwardingPolicy returns the set of forwarding constraints of the update.
353
//
354
// NOTE: this is part of the ChannelUpdate interface.
355
func (a *ChannelUpdate1) ForwardingPolicy() *ForwardingPolicy {
×
356
        return &ForwardingPolicy{
×
357
                TimeLockDelta: a.TimeLockDelta,
×
358
                BaseFee:       MilliSatoshi(a.BaseFee),
×
359
                FeeRate:       MilliSatoshi(a.FeeRate),
×
360
                MinHTLC:       a.HtlcMinimumMsat,
×
361
                HasMaxHTLC:    a.MessageFlags.HasMaxHtlc(),
×
362
                MaxHTLC:       a.HtlcMaximumMsat,
×
363
        }
×
364
}
×
365

366
// CmpAge can be used to determine if the update is older or newer than the
367
// passed update. It returns 1 if this update is newer, -1 if it is older, and
368
// 0 if they are the same age.
369
//
370
// NOTE: this is part of the ChannelUpdate interface.
371
func (a *ChannelUpdate1) CmpAge(update ChannelUpdate) (CompareResult, error) {
×
372
        other, ok := update.(*ChannelUpdate1)
×
373
        if !ok {
×
374
                return 0, fmt.Errorf("expected *ChannelUpdate1, got: %T",
×
375
                        update)
×
376
        }
×
377

378
        switch {
×
379
        case a.Timestamp > other.Timestamp:
×
380
                return GreaterThan, nil
×
381
        case a.Timestamp < other.Timestamp:
×
382
                return LessThan, nil
×
383
        default:
×
384
                return EqualTo, nil
×
385
        }
386
}
387

388
// SetDisabledFlag can be used to adjust the disabled flag of an update.
389
//
390
// NOTE: this is part of the ChannelUpdate interface.
391
func (a *ChannelUpdate1) SetDisabledFlag(disabled bool) {
×
392
        if disabled {
×
393
                a.ChannelFlags |= ChanUpdateDisabled
×
394
        } else {
×
395
                a.ChannelFlags &= ^ChanUpdateDisabled
×
396
        }
×
397
}
398

399
// SetSCID can be used to overwrite the SCID of the update.
400
//
401
// NOTE: this is part of the ChannelUpdate interface.
402
func (a *ChannelUpdate1) SetSCID(scid ShortChannelID) {
×
403
        a.ShortChannelID = scid
×
404
}
×
405

406
// A compile time assertion to ensure ChannelUpdate1 implements the
407
// ChannelUpdate interface.
408
var _ ChannelUpdate = (*ChannelUpdate1)(nil)
409

410
// SerializedSize returns the serialized size of the message in bytes.
411
//
412
// This is part of the lnwire.SizeableMessage interface.
413
func (a *ChannelUpdate1) SerializedSize() (uint32, error) {
3✔
414
        return MessageSerializedSize(a)
3✔
415
}
3✔
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