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

lightningnetwork / lnd / 13999301927

21 Mar 2025 07:18PM UTC coverage: 68.989% (+9.9%) from 59.126%
13999301927

push

github

web-flow
Merge pull request #9623 from Roasbeef/size-msg-test-msg

Size msg test msg

1461 of 1572 new or added lines in 43 files covered. (92.94%)

28 existing lines in 6 files now uncovered.

132898 of 192637 relevant lines covered (68.99%)

22200.59 hits per line

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

61.54
/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

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

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

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

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

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

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

487✔
46
        if len(s) > 32 {
491✔
47
                return n, fmt.Errorf("alias too large: max is %v, got %v", 32,
4✔
48
                        len(s))
4✔
49
        }
4✔
50

51
        if !utf8.ValidString(s) {
497✔
52
                return n, &ErrInvalidNodeAlias{}
11✔
53
        }
11✔
54

55
        copy(n[:], []byte(s))
475✔
56
        return n, nil
475✔
57
}
58

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

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

73
        // Features is the list of protocol features this node supports.
74
        Features *RawFeatureVector
75

76
        // Timestamp allows ordering in the case of multiple announcements.
77
        Timestamp uint32
78

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

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

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

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

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

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

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

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

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

137
        if err := WriteRawFeatureVector(w, a.Features); err != nil {
247✔
138
                return err
×
139
        }
×
140

141
        if err := WriteUint32(w, a.Timestamp); err != nil {
247✔
142
                return err
×
143
        }
×
144

145
        if err := WriteBytes(w, a.NodeID[:]); err != nil {
247✔
146
                return err
×
147
        }
×
148

149
        if err := WriteColorRGBA(w, a.RGBColor); err != nil {
247✔
150
                return err
×
151
        }
×
152

153
        if err := WriteNodeAlias(w, a.Alias); err != nil {
247✔
154
                return err
×
155
        }
×
156

157
        if err := WriteNetAddrs(w, a.Addresses); err != nil {
247✔
158
                return err
×
159
        }
×
160

161
        return WriteBytes(w, a.ExtraOpaqueData)
247✔
162
}
163

164
// MsgType returns the integer uniquely identifying this message type on the
165
// wire.
166
//
167
// This is part of the lnwire.Message interface.
168
func (a *NodeAnnouncement) MsgType() MessageType {
317✔
169
        return MsgNodeAnnouncement
317✔
170
}
317✔
171

172
// DataToSign returns the part of the message that should be signed.
173
func (a *NodeAnnouncement) DataToSign() ([]byte, error) {
64✔
174

64✔
175
        // We should not include the signatures itself.
64✔
176
        buffer := make([]byte, 0, MaxMsgBody)
64✔
177
        buf := bytes.NewBuffer(buffer)
64✔
178

64✔
179
        if err := WriteRawFeatureVector(buf, a.Features); err != nil {
65✔
180
                return nil, err
1✔
181
        }
1✔
182

183
        if err := WriteUint32(buf, a.Timestamp); err != nil {
63✔
184
                return nil, err
×
185
        }
×
186

187
        if err := WriteBytes(buf, a.NodeID[:]); err != nil {
63✔
188
                return nil, err
×
189
        }
×
190

191
        if err := WriteColorRGBA(buf, a.RGBColor); err != nil {
63✔
192
                return nil, err
×
193
        }
×
194

195
        if err := WriteNodeAlias(buf, a.Alias); err != nil {
63✔
196
                return nil, err
×
197
        }
×
198

199
        if err := WriteNetAddrs(buf, a.Addresses); err != nil {
63✔
200
                return nil, err
×
201
        }
×
202

203
        if err := WriteBytes(buf, a.ExtraOpaqueData); err != nil {
63✔
204
                return nil, err
×
205
        }
×
206

207
        return buf.Bytes(), nil
63✔
208
}
209

210
// SerializedSize returns the serialized size of the message in bytes.
211
//
212
// This is part of the lnwire.SizeableMessage interface.
NEW
213
func (a *NodeAnnouncement) SerializedSize() (uint32, error) {
×
NEW
214
        return MessageSerializedSize(a)
×
NEW
215
}
×
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