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

lightningnetwork / lnd / 13586005509

28 Feb 2025 10:14AM UTC coverage: 68.629% (+9.9%) from 58.77%
13586005509

Pull #9521

github

web-flow
Merge 37d3a70a5 into 8532955b3
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

129950 of 189351 relevant lines covered (68.63%)

23726.46 hits per line

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

97.23
/lnwire/message.go
1
// Copyright (c) 2013-2017 The btcsuite developers
2
// Copyright (c) 2015-2016 The Decred developers
3
// code derived from https://github .com/btcsuite/btcd/blob/master/wire/message.go
4
// Copyright (C) 2015-2022 The Lightning Network Developers
5

6
package lnwire
7

8
import (
9
        "bytes"
10
        "encoding/binary"
11
        "fmt"
12
        "io"
13
)
14

15
// MessageType is the unique 2 byte big-endian integer that indicates the type
16
// of message on the wire. All messages have a very simple header which
17
// consists simply of 2-byte message type. We omit a length field, and checksum
18
// as the Lightning Protocol is intended to be encapsulated within a
19
// confidential+authenticated cryptographic messaging protocol.
20
type MessageType uint16
21

22
// The currently defined message types within this current version of the
23
// Lightning protocol.
24
const (
25
        MsgWarning                 MessageType = 1
26
        MsgStfu                                = 2
27
        MsgInit                                = 16
28
        MsgError                               = 17
29
        MsgPing                                = 18
30
        MsgPong                                = 19
31
        MsgOpenChannel                         = 32
32
        MsgAcceptChannel                       = 33
33
        MsgFundingCreated                      = 34
34
        MsgFundingSigned                       = 35
35
        MsgChannelReady                        = 36
36
        MsgShutdown                            = 38
37
        MsgClosingSigned                       = 39
38
        MsgClosingComplete                     = 40
39
        MsgClosingSig                          = 41
40
        MsgDynPropose                          = 111
41
        MsgDynAck                              = 113
42
        MsgDynReject                           = 115
43
        MsgUpdateAddHTLC                       = 128
44
        MsgUpdateFulfillHTLC                   = 130
45
        MsgUpdateFailHTLC                      = 131
46
        MsgCommitSig                           = 132
47
        MsgRevokeAndAck                        = 133
48
        MsgUpdateFee                           = 134
49
        MsgUpdateFailMalformedHTLC             = 135
50
        MsgChannelReestablish                  = 136
51
        MsgChannelAnnouncement                 = 256
52
        MsgNodeAnnouncement                    = 257
53
        MsgChannelUpdate                       = 258
54
        MsgAnnounceSignatures                  = 259
55
        MsgAnnounceSignatures2                 = 260
56
        MsgQueryShortChanIDs                   = 261
57
        MsgReplyShortChanIDsEnd                = 262
58
        MsgQueryChannelRange                   = 263
59
        MsgReplyChannelRange                   = 264
60
        MsgGossipTimestampRange                = 265
61
        MsgChannelAnnouncement2                = 267
62
        MsgChannelUpdate2                      = 271
63
        MsgKickoffSig                          = 777
64
)
65

66
// IsChannelUpdate is a filter function that discerns channel update messages
67
// from the other messages in the Lightning Network Protocol.
68
func (t MessageType) IsChannelUpdate() bool {
3,781✔
69
        switch t {
3,781✔
70
        case MsgUpdateAddHTLC:
933✔
71
                return true
933✔
72
        case MsgUpdateFulfillHTLC:
253✔
73
                return true
253✔
74
        case MsgUpdateFailHTLC:
141✔
75
                return true
141✔
76
        case MsgUpdateFailMalformedHTLC:
6✔
77
                return true
6✔
78
        case MsgUpdateFee:
3✔
79
                return true
3✔
80
        default:
2,457✔
81
                return false
2,457✔
82
        }
83
}
84

85
// ErrorEncodeMessage is used when failed to encode the message payload.
86
func ErrorEncodeMessage(err error) error {
4✔
87
        return fmt.Errorf("failed to encode message to buffer, got %w", err)
4✔
88
}
4✔
89

90
// ErrorWriteMessageType is used when failed to write the message type.
91
func ErrorWriteMessageType(err error) error {
×
92
        return fmt.Errorf("failed to write message type, got %w", err)
×
93
}
×
94

95
// ErrorPayloadTooLarge is used when the payload size exceeds the
96
// MaxMsgBody.
97
func ErrorPayloadTooLarge(size int) error {
2✔
98
        return fmt.Errorf(
2✔
99
                "message payload is too large - encoded %d bytes, "+
2✔
100
                        "but maximum message payload is %d bytes",
2✔
101
                size, MaxMsgBody,
2✔
102
        )
2✔
103
}
2✔
104

105
// String return the string representation of message type.
106
func (t MessageType) String() string {
81✔
107
        switch t {
81✔
108
        case MsgWarning:
2✔
109
                return "Warning"
2✔
110
        case MsgStfu:
5✔
111
                return "Stfu"
5✔
112
        case MsgInit:
5✔
113
                return "Init"
5✔
114
        case MsgOpenChannel:
5✔
115
                return "MsgOpenChannel"
5✔
116
        case MsgAcceptChannel:
5✔
117
                return "MsgAcceptChannel"
5✔
118
        case MsgFundingCreated:
5✔
119
                return "MsgFundingCreated"
5✔
120
        case MsgFundingSigned:
5✔
121
                return "MsgFundingSigned"
5✔
122
        case MsgChannelReady:
5✔
123
                return "ChannelReady"
5✔
124
        case MsgShutdown:
5✔
125
                return "Shutdown"
5✔
126
        case MsgClosingSigned:
5✔
127
                return "ClosingSigned"
5✔
128
        case MsgDynPropose:
2✔
129
                return "DynPropose"
2✔
130
        case MsgDynAck:
2✔
131
                return "DynAck"
2✔
132
        case MsgDynReject:
2✔
133
                return "DynReject"
2✔
134
        case MsgKickoffSig:
2✔
135
                return "KickoffSig"
2✔
136
        case MsgUpdateAddHTLC:
5✔
137
                return "UpdateAddHTLC"
5✔
138
        case MsgUpdateFailHTLC:
5✔
139
                return "UpdateFailHTLC"
5✔
140
        case MsgUpdateFulfillHTLC:
5✔
141
                return "UpdateFulfillHTLC"
5✔
142
        case MsgCommitSig:
5✔
143
                return "CommitSig"
5✔
144
        case MsgRevokeAndAck:
5✔
145
                return "RevokeAndAck"
5✔
146
        case MsgUpdateFailMalformedHTLC:
5✔
147
                return "UpdateFailMalformedHTLC"
5✔
148
        case MsgChannelReestablish:
5✔
149
                return "ChannelReestablish"
5✔
150
        case MsgError:
5✔
151
                return "Error"
5✔
152
        case MsgChannelAnnouncement:
5✔
153
                return "ChannelAnnouncement"
5✔
154
        case MsgChannelUpdate:
5✔
155
                return "ChannelUpdate"
5✔
156
        case MsgNodeAnnouncement:
5✔
157
                return "NodeAnnouncement"
5✔
158
        case MsgPing:
2✔
159
                return "Ping"
2✔
160
        case MsgAnnounceSignatures:
5✔
161
                return "AnnounceSignatures"
5✔
162
        case MsgPong:
2✔
163
                return "Pong"
2✔
164
        case MsgUpdateFee:
2✔
165
                return "UpdateFee"
2✔
166
        case MsgQueryShortChanIDs:
5✔
167
                return "QueryShortChanIDs"
5✔
168
        case MsgReplyShortChanIDsEnd:
5✔
169
                return "ReplyShortChanIDsEnd"
5✔
170
        case MsgQueryChannelRange:
5✔
171
                return "QueryChannelRange"
5✔
172
        case MsgReplyChannelRange:
5✔
173
                return "ReplyChannelRange"
5✔
174
        case MsgGossipTimestampRange:
5✔
175
                return "GossipTimestampRange"
5✔
176
        case MsgClosingComplete:
2✔
177
                return "ClosingComplete"
2✔
178
        case MsgClosingSig:
2✔
179
                return "ClosingSig"
2✔
180
        case MsgAnnounceSignatures2:
2✔
181
                return "MsgAnnounceSignatures2"
2✔
182
        case MsgChannelAnnouncement2:
2✔
183
                return "ChannelAnnouncement2"
2✔
184
        case MsgChannelUpdate2:
2✔
185
                return "ChannelUpdate2"
2✔
186
        default:
3✔
187
                return "<unknown>"
3✔
188
        }
189
}
190

191
// UnknownMessage is an implementation of the error interface that allows the
192
// creation of an error in response to an unknown message.
193
type UnknownMessage struct {
194
        messageType MessageType
195
}
196

197
// Error returns a human readable string describing the error.
198
//
199
// This is part of the error interface.
200
func (u *UnknownMessage) Error() string {
3✔
201
        return fmt.Sprintf("unable to parse message of unknown type: %v",
3✔
202
                u.messageType)
3✔
203
}
3✔
204

205
// Serializable is an interface which defines a lightning wire serializable
206
// object.
207
type Serializable interface {
208
        // Decode reads the bytes stream and converts it to the object.
209
        Decode(io.Reader, uint32) error
210

211
        // Encode converts object to the bytes stream and write it into the
212
        // write buffer.
213
        Encode(*bytes.Buffer, uint32) error
214
}
215

216
// Message is an interface that defines a lightning wire protocol message. The
217
// interface is general in order to allow implementing types full control over
218
// the representation of its data.
219
type Message interface {
220
        Serializable
221
        MsgType() MessageType
222
}
223

224
// LinkUpdater is an interface implemented by most messages in BOLT 2 that are
225
// allowed to update the channel state.
226
type LinkUpdater interface {
227
        // All LinkUpdater messages are messages and so we embed the interface
228
        // so that we can treat it as a message if all we know about it is that
229
        // it is a LinkUpdater message.
230
        Message
231

232
        // TargetChanID returns the channel id of the link for which this
233
        // message is intended.
234
        TargetChanID() ChannelID
235
}
236

237
// makeEmptyMessage creates a new empty message of the proper concrete type
238
// based on the passed message type.
239
func makeEmptyMessage(msgType MessageType) (Message, error) {
14,414✔
240
        var msg Message
14,414✔
241

14,414✔
242
        switch msgType {
14,414✔
243
        case MsgWarning:
113✔
244
                msg = &Warning{}
113✔
245
        case MsgStfu:
136✔
246
                msg = &Stfu{}
136✔
247
        case MsgInit:
183✔
248
                msg = &Init{}
183✔
249
        case MsgOpenChannel:
270✔
250
                msg = &OpenChannel{}
270✔
251
        case MsgAcceptChannel:
236✔
252
                msg = &AcceptChannel{}
236✔
253
        case MsgFundingCreated:
176✔
254
                msg = &FundingCreated{}
176✔
255
        case MsgFundingSigned:
171✔
256
                msg = &FundingSigned{}
171✔
257
        case MsgChannelReady:
196✔
258
                msg = &ChannelReady{}
196✔
259
        case MsgShutdown:
188✔
260
                msg = &Shutdown{}
188✔
261
        case MsgClosingSigned:
176✔
262
                msg = &ClosingSigned{}
176✔
263
        case MsgDynPropose:
246✔
264
                msg = &DynPropose{}
246✔
265
        case MsgDynAck:
165✔
266
                msg = &DynAck{}
165✔
267
        case MsgDynReject:
159✔
268
                msg = &DynReject{}
159✔
269
        case MsgKickoffSig:
129✔
270
                msg = &KickoffSig{}
129✔
271
        case MsgUpdateAddHTLC:
2,306✔
272
                msg = &UpdateAddHTLC{}
2,306✔
273
        case MsgUpdateFailHTLC:
573✔
274
                msg = &UpdateFailHTLC{}
573✔
275
        case MsgUpdateFulfillHTLC:
1,062✔
276
                msg = &UpdateFulfillHTLC{}
1,062✔
277
        case MsgCommitSig:
2,270✔
278
                msg = &CommitSig{}
2,270✔
279
        case MsgRevokeAndAck:
166✔
280
                msg = &RevokeAndAck{}
166✔
281
        case MsgUpdateFee:
203✔
282
                msg = &UpdateFee{}
203✔
283
        case MsgUpdateFailMalformedHTLC:
138✔
284
                msg = &UpdateFailMalformedHTLC{}
138✔
285
        case MsgChannelReestablish:
179✔
286
                msg = &ChannelReestablish{}
179✔
287
        case MsgError:
116✔
288
                msg = &Error{}
116✔
289
        case MsgChannelAnnouncement:
160✔
290
                msg = &ChannelAnnouncement1{}
160✔
291
        case MsgChannelUpdate:
150✔
292
                msg = &ChannelUpdate1{}
150✔
293
        case MsgNodeAnnouncement:
449✔
294
                msg = &NodeAnnouncement{}
449✔
295
        case MsgPing:
112✔
296
                msg = &Ping{}
112✔
297
        case MsgAnnounceSignatures:
131✔
298
                msg = &AnnounceSignatures1{}
131✔
299
        case MsgPong:
109✔
300
                msg = &Pong{}
109✔
301
        case MsgQueryShortChanIDs:
1,160✔
302
                msg = &QueryShortChanIDs{}
1,160✔
303
        case MsgReplyShortChanIDsEnd:
133✔
304
                msg = &ReplyShortChanIDsEnd{}
133✔
305
        case MsgQueryChannelRange:
202✔
306
                msg = &QueryChannelRange{}
202✔
307
        case MsgReplyChannelRange:
1,219✔
308
                msg = &ReplyChannelRange{}
1,219✔
309
        case MsgGossipTimestampRange:
180✔
310
                msg = &GossipTimestampRange{}
180✔
311
        case MsgClosingComplete:
173✔
312
                msg = &ClosingComplete{}
173✔
313
        case MsgClosingSig:
162✔
314
                msg = &ClosingSig{}
162✔
315
        case MsgAnnounceSignatures2:
128✔
316
                msg = &AnnounceSignatures2{}
128✔
317
        case MsgChannelAnnouncement2:
231✔
318
                msg = &ChannelAnnouncement2{}
231✔
319
        case MsgChannelUpdate2:
223✔
320
                msg = &ChannelUpdate2{}
223✔
321
        default:
13✔
322
                // If the message is not within our custom range and has not
13✔
323
                // specifically been overridden, return an unknown message.
13✔
324
                //
13✔
325
                // Note that we do not allow custom message overrides to replace
13✔
326
                // known message types, only protocol messages that are not yet
13✔
327
                // known to lnd.
13✔
328
                if msgType < CustomTypeStart && !IsCustomOverride(msgType) {
17✔
329
                        return nil, &UnknownMessage{msgType}
4✔
330
                }
4✔
331

332
                msg = &Custom{
12✔
333
                        Type: msgType,
12✔
334
                }
12✔
335
        }
336

337
        return msg, nil
14,413✔
338
}
339

340
// WriteMessage writes a lightning Message to a buffer including the necessary
341
// header information and returns the number of bytes written. If any error is
342
// encountered, the buffer passed will be reset to its original state since we
343
// don't want any broken bytes left. In other words, no bytes will be written
344
// if there's an error. Either all or none of the message bytes will be written
345
// to the buffer.
346
//
347
// NOTE: this method is not concurrent safe.
348
func WriteMessage(buf *bytes.Buffer, msg Message, pver uint32) (int, error) {
13,143✔
349
        // Record the size of the bytes already written in buffer.
13,143✔
350
        oldByteSize := buf.Len()
13,143✔
351

13,143✔
352
        // cleanBrokenBytes is a helper closure that helps reset the buffer to
13,143✔
353
        // its original state. It truncates all the bytes written in current
13,143✔
354
        // scope.
13,143✔
355
        var cleanBrokenBytes = func(b *bytes.Buffer) int {
13,147✔
356
                b.Truncate(oldByteSize)
4✔
357
                return 0
4✔
358
        }
4✔
359

360
        // Write the message type.
361
        var mType [2]byte
13,143✔
362
        binary.BigEndian.PutUint16(mType[:], uint16(msg.MsgType()))
13,143✔
363
        msgTypeBytes, err := buf.Write(mType[:])
13,143✔
364
        if err != nil {
13,143✔
365
                return cleanBrokenBytes(buf), ErrorWriteMessageType(err)
×
366
        }
×
367

368
        // Use the write buffer to encode our message.
369
        if err := msg.Encode(buf, pver); err != nil {
13,146✔
370
                return cleanBrokenBytes(buf), ErrorEncodeMessage(err)
3✔
371
        }
3✔
372

373
        // Enforce maximum overall message payload. The write buffer now has
374
        // the size of len(originalBytes) + len(payload) + len(type). We want
375
        // to enforce the payload here, so we subtract it by the length of the
376
        // type and old bytes.
377
        lenp := buf.Len() - oldByteSize - msgTypeBytes
13,140✔
378
        if lenp > MaxMsgBody {
13,141✔
379
                return cleanBrokenBytes(buf), ErrorPayloadTooLarge(lenp)
1✔
380
        }
1✔
381

382
        return buf.Len() - oldByteSize, nil
13,139✔
383
}
384

385
// ReadMessage reads, validates, and parses the next Lightning message from r
386
// for the provided protocol version.
387
func ReadMessage(r io.Reader, pver uint32) (Message, error) {
14,413✔
388
        // First, we'll read out the first two bytes of the message so we can
14,413✔
389
        // create the proper empty message.
14,413✔
390
        var mType [2]byte
14,413✔
391
        if _, err := io.ReadFull(r, mType[:]); err != nil {
14,413✔
392
                return nil, err
×
393
        }
×
394

395
        msgType := MessageType(binary.BigEndian.Uint16(mType[:]))
14,413✔
396

14,413✔
397
        // Now that we know the target message type, we can create the proper
14,413✔
398
        // empty message type and decode the message into it.
14,413✔
399
        msg, err := makeEmptyMessage(msgType)
14,413✔
400
        if err != nil {
14,416✔
401
                return nil, err
3✔
402
        }
3✔
403
        if err := msg.Decode(r, pver); err != nil {
16,391✔
404
                return nil, err
1,978✔
405
        }
1,978✔
406

407
        return msg, nil
12,435✔
408
}
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