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

lightningnetwork / lnd / 13980885714

20 Mar 2025 10:53PM UTC coverage: 58.613% (-10.2%) from 68.789%
13980885714

Pull #9623

github

web-flow
Merge 9eaec1f7a into 09b674508
Pull Request #9623: Size msg test msg

0 of 1572 new or added lines in 42 files covered. (0.0%)

27755 existing lines in 442 files now uncovered.

96886 of 165299 relevant lines covered (58.61%)

1.82 hits per line

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

44.44
/lnwire/node_announcement.go
1
package lnwire
2

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

11
        "pgregory.net/rapid"
12
)
13

14
// ErrUnknownAddrType is an error returned if we encounter an unknown address type
15
// when parsing addresses.
16
type ErrUnknownAddrType struct {
17
        addrType addressType
18
}
19

20
// Error returns a human readable string describing the error.
21
//
22
// NOTE: implements the error interface.
23
func (e ErrUnknownAddrType) Error() string {
×
24
        return fmt.Sprintf("unknown address type: %v", e.addrType)
×
25
}
×
26

27
// ErrInvalidNodeAlias is an error returned if a node alias we parse on the
28
// wire is invalid, as in it has non UTF-8 characters.
29
type ErrInvalidNodeAlias struct{}
30

31
// Error returns a human readable string describing the error.
32
//
33
// NOTE: implements the error interface.
34
func (e ErrInvalidNodeAlias) Error() string {
×
35
        return "node alias has non-utf8 characters"
×
36
}
×
37

38
// NodeAlias is a hex encoded UTF-8 string that may be displayed as an
39
// alternative to the node's ID. Notice that aliases are not unique and may be
40
// freely chosen by the node operators.
41
type NodeAlias [32]byte
42

43
// NewNodeAlias creates a new instance of a NodeAlias. Verification is
44
// performed on the passed string to ensure it meets the alias requirements.
45
func NewNodeAlias(s string) (NodeAlias, error) {
3✔
46
        var n NodeAlias
3✔
47

3✔
48
        if len(s) > 32 {
6✔
49
                return n, fmt.Errorf("alias too large: max is %v, got %v", 32,
3✔
50
                        len(s))
3✔
51
        }
3✔
52

53
        if !utf8.ValidString(s) {
3✔
UNCOV
54
                return n, &ErrInvalidNodeAlias{}
×
UNCOV
55
        }
×
56

57
        copy(n[:], []byte(s))
3✔
58
        return n, nil
3✔
59
}
60

61
// String returns a utf8 string representation of the alias bytes.
62
func (n NodeAlias) String() string {
3✔
63
        // Trim trailing zero-bytes for presentation
3✔
64
        return string(bytes.Trim(n[:], "\x00"))
3✔
65
}
3✔
66

67
// NodeAnnouncement message is used to announce the presence of a Lightning
68
// node and also to signal that the node is accepting incoming connections.
69
// Each NodeAnnouncement authenticating the advertised information within the
70
// announcement via a signature using the advertised node pubkey.
71
type NodeAnnouncement struct {
72
        // Signature is used to prove the ownership of node id.
73
        Signature Sig
74

75
        // Features is the list of protocol features this node supports.
76
        Features *RawFeatureVector
77

78
        // Timestamp allows ordering in the case of multiple announcements.
79
        Timestamp uint32
80

81
        // NodeID is a public key which is used as node identification.
82
        NodeID [33]byte
83

84
        // RGBColor is used to customize their node's appearance in maps and
85
        // graphs
86
        RGBColor color.RGBA
87

88
        // Alias is used to customize their node's appearance in maps and
89
        // graphs
90
        Alias NodeAlias
91

92
        // Address includes two specification fields: 'ipv6' and 'port' on
93
        // which the node is accepting incoming connections.
94
        Addresses []net.Addr
95

96
        // ExtraOpaqueData is the set of data that was appended to this
97
        // message, some of which we may not actually know how to iterate or
98
        // parse. By holding onto this data, we ensure that we're able to
99
        // properly validate the set of signatures that cover these new fields,
100
        // and ensure we're able to make upgrades to the network in a forwards
101
        // compatible manner.
102
        ExtraOpaqueData ExtraOpaqueData
103
}
104

105
// A compile time check to ensure NodeAnnouncement implements the
106
// lnwire.Message interface.
107
var _ Message = (*NodeAnnouncement)(nil)
108

109
// A compile time check to ensure NodeAnnouncement implements the
110
// lnwire.SizeableMessage interface.
111
var _ SizeableMessage = (*NodeAnnouncement)(nil)
112

113
// A compile time check to ensure NodeAnnouncement implements the
114
// lnwire.TestMessage interface.
115
var _ TestMessage = (*NodeAnnouncement)(nil)
116

117
// Decode deserializes a serialized NodeAnnouncement stored in the passed
118
// io.Reader observing the specified protocol version.
119
//
120
// This is part of the lnwire.Message interface.
121
func (a *NodeAnnouncement) Decode(r io.Reader, pver uint32) error {
3✔
122
        return ReadElements(r,
3✔
123
                &a.Signature,
3✔
124
                &a.Features,
3✔
125
                &a.Timestamp,
3✔
126
                &a.NodeID,
3✔
127
                &a.RGBColor,
3✔
128
                &a.Alias,
3✔
129
                &a.Addresses,
3✔
130
                &a.ExtraOpaqueData,
3✔
131
        )
3✔
132
}
3✔
133

134
// Encode serializes the target NodeAnnouncement into the passed io.Writer
135
// observing the protocol version specified.
136
//
137
// This is part of the lnwire.Message interface.
138
func (a *NodeAnnouncement) Encode(w *bytes.Buffer, pver uint32) error {
3✔
139
        if err := WriteSig(w, a.Signature); err != nil {
3✔
140
                return err
×
141
        }
×
142

143
        if err := WriteRawFeatureVector(w, a.Features); err != nil {
3✔
144
                return err
×
145
        }
×
146

147
        if err := WriteUint32(w, a.Timestamp); err != nil {
3✔
148
                return err
×
149
        }
×
150

151
        if err := WriteBytes(w, a.NodeID[:]); err != nil {
3✔
152
                return err
×
153
        }
×
154

155
        if err := WriteColorRGBA(w, a.RGBColor); err != nil {
3✔
156
                return err
×
157
        }
×
158

159
        if err := WriteNodeAlias(w, a.Alias); err != nil {
3✔
160
                return err
×
161
        }
×
162

163
        if err := WriteNetAddrs(w, a.Addresses); err != nil {
3✔
164
                return err
×
165
        }
×
166

167
        return WriteBytes(w, a.ExtraOpaqueData)
3✔
168
}
169

170
// MsgType returns the integer uniquely identifying this message type on the
171
// wire.
172
//
173
// This is part of the lnwire.Message interface.
174
func (a *NodeAnnouncement) MsgType() MessageType {
3✔
175
        return MsgNodeAnnouncement
3✔
176
}
3✔
177

178
// DataToSign returns the part of the message that should be signed.
179
func (a *NodeAnnouncement) DataToSign() ([]byte, error) {
3✔
180

3✔
181
        // We should not include the signatures itself.
3✔
182
        buffer := make([]byte, 0, MaxMsgBody)
3✔
183
        buf := bytes.NewBuffer(buffer)
3✔
184

3✔
185
        if err := WriteRawFeatureVector(buf, a.Features); err != nil {
3✔
UNCOV
186
                return nil, err
×
UNCOV
187
        }
×
188

189
        if err := WriteUint32(buf, a.Timestamp); err != nil {
3✔
190
                return nil, err
×
191
        }
×
192

193
        if err := WriteBytes(buf, a.NodeID[:]); err != nil {
3✔
194
                return nil, err
×
195
        }
×
196

197
        if err := WriteColorRGBA(buf, a.RGBColor); err != nil {
3✔
198
                return nil, err
×
199
        }
×
200

201
        if err := WriteNodeAlias(buf, a.Alias); err != nil {
3✔
202
                return nil, err
×
203
        }
×
204

205
        if err := WriteNetAddrs(buf, a.Addresses); err != nil {
3✔
206
                return nil, err
×
207
        }
×
208

209
        if err := WriteBytes(buf, a.ExtraOpaqueData); err != nil {
3✔
210
                return nil, err
×
211
        }
×
212

213
        return buf.Bytes(), nil
3✔
214
}
215

216
// SerializedSize returns the serialized size of the message in bytes.
217
//
218
// This is part of the lnwire.SizeableMessage interface.
NEW
219
func (a *NodeAnnouncement) SerializedSize() (uint32, error) {
×
NEW
220
        return MessageSerializedSize(a)
×
NEW
221
}
×
222

223
// RandTestMessage populates the message with random data suitable for testing.
224
// It uses the rapid testing framework to generate random values.
225
//
226
// This is part of the TestMessage interface.
NEW
227
func (a *NodeAnnouncement) RandTestMessage(t *rapid.T) Message {
×
NEW
228
        // Generate random compressed public key for node ID
×
NEW
229
        pubKey := RandPubKey(t)
×
NEW
230
        var nodeID [33]byte
×
NEW
231
        copy(nodeID[:], pubKey.SerializeCompressed())
×
NEW
232

×
NEW
233
        // Generate random RGB color
×
NEW
234
        rgbColor := color.RGBA{
×
NEW
235
                R: uint8(rapid.IntRange(0, 255).Draw(t, "rgbR")),
×
NEW
236
                G: uint8(rapid.IntRange(0, 255).Draw(t, "rgbG")),
×
NEW
237
                B: uint8(rapid.IntRange(0, 255).Draw(t, "rgbB")),
×
NEW
238
        }
×
NEW
239

×
NEW
240
        return &NodeAnnouncement{
×
NEW
241
                Signature: RandSignature(t),
×
NEW
242
                Features:  RandFeatureVector(t),
×
NEW
243
                Timestamp: uint32(rapid.IntRange(0, 0x7FFFFFFF).Draw(
×
NEW
244
                        t, "timestamp"),
×
NEW
245
                ),
×
NEW
246
                NodeID:          nodeID,
×
NEW
247
                RGBColor:        rgbColor,
×
NEW
248
                Alias:           RandNodeAlias(t),
×
NEW
249
                Addresses:       RandNetAddrs(t),
×
NEW
250
                ExtraOpaqueData: RandExtraOpaqueData(t, nil),
×
NEW
251
        }
×
NEW
252
}
×
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