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

lightningnetwork / lnd / 13980275562

20 Mar 2025 10:06PM UTC coverage: 58.6% (-10.2%) from 68.789%
13980275562

Pull #9623

github

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

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

26603 existing lines in 443 files now uncovered.

96807 of 165200 relevant lines covered (58.6%)

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 lnwire.SizeableMessage interface.
110
var _ SizeableMessage = (*NodeAnnouncement)(nil)
111

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

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

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

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

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

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

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

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

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

165
        return WriteBytes(w, a.ExtraOpaqueData)
3✔
166
}
167

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

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

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

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

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

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

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

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

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

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

211
        return buf.Bytes(), nil
3✔
212
}
213

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

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

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

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