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

lightningnetwork / lnd / 17949158775

23 Sep 2025 02:18PM UTC coverage: 66.713% (+0.07%) from 66.647%
17949158775

Pull #10232

github

web-flow
Merge fc770ddb4 into 82f77e542
Pull Request #10232: lnwire: add missing Gossip 1.75 fields and message

491 of 568 new or added lines in 19 files covered. (86.44%)

64 existing lines in 19 files now uncovered.

136915 of 205230 relevant lines covered (66.71%)

21387.93 hits per line

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

81.88
/lnwire/node_announcement_2.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "encoding/binary"
6
        "fmt"
7
        "image/color"
8
        "io"
9
        "net"
10

11
        "github.com/lightningnetwork/lnd/tlv"
12
        "github.com/lightningnetwork/lnd/tor"
13
)
14

15
type NodeAnnouncement2 struct {
16
        // Features is the feature vector that encodes the features supported
17
        // by the target node.
18
        Features tlv.RecordT[tlv.TlvType0, RawFeatureVector]
19

20
        // Color is an optional field used to customize a node's appearance in
21
        // maps and graphs.
22
        Color tlv.OptionalRecordT[tlv.TlvType1, Color]
23

24
        // BlockHeight allows ordering in the case of multiple announcements. We
25
        // should ignore the message if block height is not greater than the
26
        // last-received. The block height must always be greater or equal to
27
        // the block height that the channel funding transaction was confirmed
28
        // in.
29
        BlockHeight tlv.RecordT[tlv.TlvType2, uint32]
30

31
        // Alias is used to customize their node's appearance in maps and
32
        // graphs.
33
        Alias tlv.OptionalRecordT[tlv.TlvType3, []byte]
34

35
        // NodeID is the public key of the node creating the announcement.
36
        NodeID tlv.RecordT[tlv.TlvType6, [33]byte]
37

38
        // IPV4Addrs is an optional list of ipv4 addresses that the node is
39
        // reachable at.
40
        IPV4Addrs tlv.OptionalRecordT[tlv.TlvType5, IPV4Addrs]
41

42
        // IPV6Addrs is an optional list of ipv6 addresses that the node is
43
        // reachable at.
44
        IPV6Addrs tlv.OptionalRecordT[tlv.TlvType7, IPV6Addrs]
45

46
        // TorV3Addrs is an optional list of tor v3 addresses that the node is
47
        // reachable at.
48
        TorV3Addrs tlv.OptionalRecordT[tlv.TlvType9, TorV3Addrs]
49

50
        // DNSHostName is an optional DNS hostname that the node is reachable
51
        // at.
52
        DNSHostName tlv.OptionalRecordT[tlv.TlvType11, DNSAddress]
53

54
        // Signature is used to validate the announced data and prove the
55
        // ownership of node id.
56
        Signature tlv.RecordT[tlv.TlvType160, Sig]
57

58
        // Any extra fields in the signed range that we do not yet know about,
59
        // but we need to keep them for signature validation and to produce a
60
        // valid message.
61
        ExtraSignedFields
62
}
63

64
// AllRecords returns all the TLV records for the message. This will include all
65
// the records we know about along with any that we don't know about but that
66
// fall in the signed TLV range.
67
//
68
// NOTE: this is part of the PureTLVMessage interface.
69
func (n *NodeAnnouncement2) AllRecords() []tlv.Record {
101✔
70
        recordProducers := []tlv.RecordProducer{
101✔
71
                &n.Features,
101✔
72
                &n.BlockHeight,
101✔
73
                &n.NodeID,
101✔
74
                &n.Signature,
101✔
75
        }
101✔
76

101✔
77
        n.Color.WhenSome(func(r tlv.RecordT[tlv.TlvType1, Color]) {
157✔
78
                recordProducers = append(recordProducers, &r)
56✔
79
        })
56✔
80

81
        n.Alias.WhenSome(func(a tlv.RecordT[tlv.TlvType3, []byte]) {
150✔
82
                recordProducers = append(recordProducers, &a)
49✔
83
        })
49✔
84

85
        n.IPV4Addrs.WhenSome(func(r tlv.RecordT[tlv.TlvType5, IPV4Addrs]) {
151✔
86
                recordProducers = append(recordProducers, &r)
50✔
87
        })
50✔
88

89
        n.IPV6Addrs.WhenSome(func(r tlv.RecordT[tlv.TlvType7, IPV6Addrs]) {
153✔
90
                recordProducers = append(recordProducers, &r)
52✔
91
        })
52✔
92

93
        n.TorV3Addrs.WhenSome(func(r tlv.RecordT[tlv.TlvType9, TorV3Addrs]) {
162✔
94
                recordProducers = append(recordProducers, &r)
61✔
95
        })
61✔
96

97
        n.DNSHostName.WhenSome(func(r tlv.RecordT[tlv.TlvType11, DNSAddress]) {
156✔
98
                recordProducers = append(recordProducers, &r)
55✔
99
        })
55✔
100

101
        recordProducers = append(recordProducers, RecordsAsProducers(
101✔
102
                tlv.MapToRecords(n.ExtraSignedFields),
101✔
103
        )...)
101✔
104

101✔
105
        return ProduceRecordsSorted(recordProducers...)
101✔
106
}
107

108
// Decode deserializes a serialized ChannelUpdate2 stored in the passed
109
// io.Reader observing the specified protocol version.
110
//
111
// This is part of the lnwire.Message interface.
112
func (n *NodeAnnouncement2) Decode(r io.Reader, _ uint32) error {
101✔
113
        var (
101✔
114
                color = tlv.ZeroRecordT[tlv.TlvType1, Color]()
101✔
115
                alias = tlv.ZeroRecordT[tlv.TlvType3, []byte]()
101✔
116
                ipv4  = tlv.ZeroRecordT[tlv.TlvType5, IPV4Addrs]()
101✔
117
                ipv6  = tlv.ZeroRecordT[tlv.TlvType7, IPV6Addrs]()
101✔
118
                torV3 = tlv.ZeroRecordT[tlv.TlvType9, TorV3Addrs]()
101✔
119
                dns   = tlv.ZeroRecordT[tlv.TlvType11, DNSAddress]()
101✔
120
        )
101✔
121
        stream, err := tlv.NewStream(ProduceRecordsSorted(
101✔
122
                &n.Features,
101✔
123
                &n.BlockHeight,
101✔
124
                &n.NodeID,
101✔
125
                &n.Signature,
101✔
126
                &alias,
101✔
127
                &color,
101✔
128
                &ipv4,
101✔
129
                &ipv6,
101✔
130
                &torV3,
101✔
131
                &dns,
101✔
132
        )...)
101✔
133
        if err != nil {
101✔
NEW
134
                return err
×
NEW
135
        }
×
136
        n.Signature.Val.ForceSchnorr()
101✔
137

101✔
138
        typeMap, err := stream.DecodeWithParsedTypesP2P(r)
101✔
139
        if err != nil {
101✔
NEW
140
                return err
×
NEW
141
        }
×
142

143
        if _, ok := typeMap[n.Alias.TlvType()]; ok {
150✔
144
                n.Alias = tlv.SomeRecordT(alias)
49✔
145
        }
49✔
146

147
        if _, ok := typeMap[n.Color.TlvType()]; ok {
157✔
148
                n.Color = tlv.SomeRecordT(color)
56✔
149
        }
56✔
150

151
        if _, ok := typeMap[n.IPV4Addrs.TlvType()]; ok {
151✔
152
                n.IPV4Addrs = tlv.SomeRecordT(ipv4)
50✔
153
        }
50✔
154

155
        if _, ok := typeMap[n.IPV6Addrs.TlvType()]; ok {
153✔
156
                n.IPV6Addrs = tlv.SomeRecordT(ipv6)
52✔
157
        }
52✔
158

159
        if _, ok := typeMap[n.TorV3Addrs.TlvType()]; ok {
162✔
160
                n.TorV3Addrs = tlv.SomeRecordT(torV3)
61✔
161
        }
61✔
162

163
        if _, ok := typeMap[n.DNSHostName.TlvType()]; ok {
156✔
164
                n.DNSHostName = tlv.SomeRecordT(dns)
55✔
165
        }
55✔
166

167
        n.ExtraSignedFields = ExtraSignedFieldsFromTypeMap(typeMap)
101✔
168

101✔
169
        return nil
101✔
170
}
171

172
// Encode serializes the target ChannelUpdate2 into the passed io.Writer
173
// observing the protocol version specified.
174
//
175
// This is part of the lnwire.Message interface.
176
func (n *NodeAnnouncement2) Encode(w *bytes.Buffer, _ uint32) error {
101✔
177
        return EncodePureTLVMessage(n, w)
101✔
178
}
101✔
179

180
// MsgType returns the integer uniquely identifying this message type on the
181
// wire.
182
//
183
// This is part of the lnwire.Message interface.
184
func (n *NodeAnnouncement2) MsgType() MessageType {
100✔
185
        return MsgNodeAnnouncement2
100✔
186
}
100✔
187

188
// A compile-time check to ensure NodeAnnouncement2 implements the Message
189
// interface.
190
var _ Message = (*NodeAnnouncement2)(nil)
191

192
// A compile-time check to ensure NodeAnnouncement2 implements the
193
// PureTLVMessage interface.
194
var _ PureTLVMessage = (*NodeAnnouncement2)(nil)
195

196
type Color color.RGBA
197

198
// Record returns a TLV record for encoding/decoding Color.
199
func (c *Color) Record() tlv.Record {
157✔
200
        return tlv.MakeStaticRecord(0, c, 3, rgbEncoder, rgbDecoder)
157✔
201
}
157✔
202

203
// rgbEncoder encodes a Color as RGB bytes.
204
func rgbEncoder(w io.Writer, val interface{}, _ *[8]byte) error {
56✔
205
        if v, ok := val.(*Color); ok {
112✔
206
                buf := bytes.NewBuffer(nil)
56✔
207
                err := WriteColorRGBA(buf, color.RGBA(*v))
56✔
208
                if err != nil {
56✔
NEW
209
                        return err
×
NEW
210
                }
×
211
                _, err = w.Write(buf.Bytes())
56✔
212

56✔
213
                return err
56✔
214
        }
215

NEW
216
        return tlv.NewTypeForEncodingErr(val, "Color")
×
217
}
218

219
// rgbDecoder decodes RGB bytes into a Color.
220
func rgbDecoder(r io.Reader, val interface{}, _ *[8]byte, l uint64) error {
56✔
221
        if v, ok := val.(*Color); ok {
112✔
222
                return ReadElements(r, &v.R, &v.G, &v.B)
56✔
223
        }
56✔
224

NEW
225
        return tlv.NewTypeForDecodingErr(val, "Color", l, 3)
×
226
}
227

228
// ipv4AddrEncodedSize is the number of bytes required to encode a single ipv4
229
// address. Four bytes are used to encode the IP address and two bytes for the
230
// port number.
231
const ipv4AddrEncodedSize = 4 + 2
232

233
// IPV4Addrs is a list of ipv4 addresses that can be encoded as a TLV record.
234
type IPV4Addrs []*net.TCPAddr
235

236
// Record returns a Record that can be used to encode/decode a IPV4Addrs
237
// to/from a TLV stream.
238
func (a *IPV4Addrs) Record() tlv.Record {
151✔
239
        return tlv.MakeDynamicRecord(
151✔
240
                0, a, a.EncodedSize, ipv4AddrsEncoder, ipv4AddrsDecoder,
151✔
241
        )
151✔
242
}
151✔
243

244
// EncodedSize returns the number of bytes required to encode an IPV4Addrs
245
// variable.
246
func (a *IPV4Addrs) EncodedSize() uint64 {
50✔
247
        return uint64(len(*a) * ipv4AddrEncodedSize)
50✔
248
}
50✔
249

250
// ipv4AddrsEncoder encodes IPv4 addresses as TLV bytes.
251
func ipv4AddrsEncoder(w io.Writer, val interface{}, _ *[8]byte) error {
50✔
252
        if v, ok := val.(*IPV4Addrs); ok {
100✔
253
                for _, ip := range *v {
101✔
254
                        _, err := w.Write(ip.IP.To4())
51✔
255
                        if err != nil {
51✔
NEW
256
                                return err
×
NEW
257
                        }
×
258
                        var port [2]byte
51✔
259
                        binary.BigEndian.PutUint16(port[:], uint16(ip.Port))
51✔
260
                        _, err = w.Write(port[:])
51✔
261
                        if err != nil {
51✔
NEW
262
                                return err
×
NEW
263
                        }
×
264
                }
265

266
                return nil
50✔
267
        }
268

NEW
269
        return tlv.NewTypeForEncodingErr(val, "lnwire.IPV4Addrs")
×
270
}
271

272
// ipv4AddrsDecoder decodes TLV bytes into IPv4 addresses.
273
func ipv4AddrsDecoder(r io.Reader, val interface{}, _ *[8]byte,
274
        l uint64) error {
50✔
275

50✔
276
        if v, ok := val.(*IPV4Addrs); ok {
100✔
277
                if l%(ipv4AddrEncodedSize) != 0 {
50✔
NEW
278
                        return fmt.Errorf("invalid ipv4 list encoding")
×
NEW
279
                }
×
280
                var (
50✔
281
                        numAddrs = int(l / ipv4AddrEncodedSize)
50✔
282
                        addrs    = make([]*net.TCPAddr, 0, numAddrs)
50✔
283
                        ip       [4]byte
50✔
284
                        port     [2]byte
50✔
285
                )
50✔
286
                for len(addrs) < numAddrs {
101✔
287
                        _, err := r.Read(ip[:])
51✔
288
                        if err != nil {
51✔
NEW
289
                                return err
×
NEW
290
                        }
×
291
                        _, err = r.Read(port[:])
51✔
292
                        if err != nil {
51✔
NEW
293
                                return err
×
NEW
294
                        }
×
295
                        addrs = append(addrs, &net.TCPAddr{
51✔
296
                                IP:   ip[:],
51✔
297
                                Port: int(binary.BigEndian.Uint16(port[:])),
51✔
298
                        })
51✔
299
                }
300
                *v = addrs
50✔
301

50✔
302
                return nil
50✔
303
        }
304

NEW
305
        return tlv.NewTypeForEncodingErr(val, "lnwire.IPV4Addrs")
×
306
}
307

308
// IPV6Addrs is a list of ipv6 addresses that can be encoded as a TLV record.
309
type IPV6Addrs []*net.TCPAddr
310

311
// Record returns a Record that can be used to encode/decode a IPV4Addrs
312
// to/from a TLV stream.
313
func (a *IPV6Addrs) Record() tlv.Record {
153✔
314
        return tlv.MakeDynamicRecord(
153✔
315
                0, a, a.EncodedSize, ipv6AddrsEncoder, ipv6AddrsDecoder,
153✔
316
        )
153✔
317
}
153✔
318

319
// ipv6AddrEncodedSize is the number of bytes required to encode a single ipv6
320
// address. Sixteen bytes are used to encode the IP address and two bytes for
321
// the port number.
322
const ipv6AddrEncodedSize = 16 + 2
323

324
// EncodedSize returns the number of bytes required to encode an IPV6Addrs
325
// variable.
326
func (a *IPV6Addrs) EncodedSize() uint64 {
52✔
327
        return uint64(len(*a) * ipv6AddrEncodedSize)
52✔
328
}
52✔
329

330
// ipv6AddrsEncoder encodes IPv6 addresses as TLV bytes.
331
func ipv6AddrsEncoder(w io.Writer, val interface{}, _ *[8]byte) error {
52✔
332
        if v, ok := val.(*IPV6Addrs); ok {
104✔
333
                for _, ip := range *v {
104✔
334
                        _, err := w.Write(ip.IP.To16())
52✔
335
                        if err != nil {
52✔
NEW
336
                                return err
×
NEW
337
                        }
×
338
                        var port [2]byte
52✔
339
                        binary.BigEndian.PutUint16(port[:], uint16(ip.Port))
52✔
340
                        _, err = w.Write(port[:])
52✔
341
                        if err != nil {
52✔
NEW
342
                                return err
×
NEW
343
                        }
×
344
                }
345

346
                return nil
52✔
347
        }
348

NEW
349
        return tlv.NewTypeForEncodingErr(val, "lnwire.IPV6Addrs")
×
350
}
351

352
// ipv6AddrsDecoder decodes TLV bytes into IPv6 addresses.
353
func ipv6AddrsDecoder(r io.Reader, val interface{}, _ *[8]byte,
354
        l uint64) error {
52✔
355

52✔
356
        if v, ok := val.(*IPV6Addrs); ok {
104✔
357
                if l%(ipv6AddrEncodedSize) != 0 {
52✔
NEW
358
                        return fmt.Errorf("invalid ipv6 list encoding")
×
NEW
359
                }
×
360
                var (
52✔
361
                        numAddrs = int(l / ipv6AddrEncodedSize)
52✔
362
                        addrs    = make([]*net.TCPAddr, 0, numAddrs)
52✔
363
                        ip       [16]byte
52✔
364
                        port     [2]byte
52✔
365
                )
52✔
366
                for len(addrs) < numAddrs {
104✔
367
                        _, err := r.Read(ip[:])
52✔
368
                        if err != nil {
52✔
NEW
369
                                return err
×
NEW
370
                        }
×
371
                        _, err = r.Read(port[:])
52✔
372
                        if err != nil {
52✔
NEW
373
                                return err
×
NEW
374
                        }
×
375
                        addrs = append(addrs, &net.TCPAddr{
52✔
376
                                IP:   ip[:],
52✔
377
                                Port: int(binary.BigEndian.Uint16(port[:])),
52✔
378
                        })
52✔
379
                }
380
                *v = addrs
52✔
381

52✔
382
                return nil
52✔
383
        }
384

NEW
385
        return tlv.NewTypeForEncodingErr(val, "lnwire.IPV6Addrs")
×
386
}
387

388
// TorV3Addrs is a list of tor v3 addresses that can be encoded as a TLV record.
389
type TorV3Addrs []*tor.OnionAddr
390

391
// torV3AddrEncodedSize is the number of bytes required to encode a single tor
392
// v3 address.
393
const torV3AddrEncodedSize = tor.V3DecodedLen + 2
394

395
// EncodedSize returns the number of bytes required to encode an TorV3Addrs
396
// variable.
397
func (a *TorV3Addrs) EncodedSize() uint64 {
61✔
398
        return uint64(len(*a) * torV3AddrEncodedSize)
61✔
399
}
61✔
400

401
// Record returns a Record that can be used to encode/decode a IPV4Addrs
402
// to/from a TLV stream.
403
func (a *TorV3Addrs) Record() tlv.Record {
162✔
404
        return tlv.MakeDynamicRecord(
162✔
405
                0, a, a.EncodedSize, torV3AddrsEncoder, torV3AddrsDecoder,
162✔
406
        )
162✔
407
}
162✔
408

409
// torV3AddrsEncoder encodes Tor v3 addresses as TLV bytes.
410
func torV3AddrsEncoder(w io.Writer, val interface{}, _ *[8]byte) error {
61✔
411
        if v, ok := val.(*TorV3Addrs); ok {
122✔
412
                for _, addr := range *v {
122✔
413
                        encodedHostLen := tor.V3Len - tor.OnionSuffixLen
61✔
414
                        host, err := tor.Base32Encoding.DecodeString(
61✔
415
                                addr.OnionService[:encodedHostLen],
61✔
416
                        )
61✔
417
                        if err != nil {
61✔
NEW
418
                                return err
×
NEW
419
                        }
×
420

421
                        if len(host) != tor.V3DecodedLen {
61✔
NEW
422
                                return fmt.Errorf("expected a tor v3 host "+
×
NEW
423
                                        "length of %d, got: %d",
×
NEW
424
                                        tor.V3DecodedLen, len(host))
×
NEW
425
                        }
×
426

427
                        if _, err = w.Write(host); err != nil {
61✔
NEW
428
                                return err
×
NEW
429
                        }
×
430

431
                        var port [2]byte
61✔
432
                        binary.BigEndian.PutUint16(port[:], uint16(addr.Port))
61✔
433
                        if _, err = w.Write(port[:]); err != nil {
61✔
NEW
434
                                return err
×
NEW
435
                        }
×
436
                }
437

438
                return nil
61✔
439
        }
440

NEW
441
        return tlv.NewTypeForEncodingErr(val, "lnwire.TorV3Addrs")
×
442
}
443

444
// torV3AddrsDecoder decodes TLV bytes into Tor v3 addresses.
445
func torV3AddrsDecoder(r io.Reader, val interface{}, _ *[8]byte,
446
        l uint64) error {
61✔
447

61✔
448
        if v, ok := val.(*TorV3Addrs); ok {
122✔
449
                if l%torV3AddrEncodedSize != 0 {
61✔
NEW
450
                        return fmt.Errorf("invalid tor v3 list encoding")
×
NEW
451
                }
×
452
                var (
61✔
453
                        numAddrs = int(l / torV3AddrEncodedSize)
61✔
454
                        addrs    = make([]*tor.OnionAddr, 0, numAddrs)
61✔
455
                        ip       [tor.V3DecodedLen]byte
61✔
456
                        p        [2]byte
61✔
457
                )
61✔
458
                for len(addrs) < numAddrs {
122✔
459
                        _, err := r.Read(ip[:])
61✔
460
                        if err != nil {
61✔
NEW
461
                                return err
×
NEW
462
                        }
×
463
                        _, err = r.Read(p[:])
61✔
464
                        if err != nil {
61✔
NEW
465
                                return err
×
NEW
466
                        }
×
467
                        onionService := tor.Base32Encoding.EncodeToString(ip[:])
61✔
468
                        onionService += tor.OnionSuffix
61✔
469
                        port := int(binary.BigEndian.Uint16(p[:]))
61✔
470
                        addrs = append(addrs, &tor.OnionAddr{
61✔
471
                                OnionService: onionService,
61✔
472
                                Port:         port,
61✔
473
                        })
61✔
474
                }
475
                *v = addrs
61✔
476

61✔
477
                return nil
61✔
478
        }
479

NEW
480
        return tlv.NewTypeForEncodingErr(val, "lnwire.TorV3Addrs")
×
481
}
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