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

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

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

70.83
/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
// MessageTypeSize is the size in bytes of the message type field in the header
16
// of all messages.
17
const MessageTypeSize = 2
18

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

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

69
        // MsgEnd defines the end of the official message range of the protocol.
70
        // If a new message is added beyond this message, then this should be
71
        // modified.
72
        MsgEnd = 778
73
)
74

75
// IsChannelUpdate is a filter function that discerns channel update messages
76
// from the other messages in the Lightning Network Protocol.
77
func (t MessageType) IsChannelUpdate() bool {
3✔
78
        switch t {
3✔
79
        case MsgUpdateAddHTLC:
3✔
80
                return true
3✔
81
        case MsgUpdateFulfillHTLC:
3✔
82
                return true
3✔
83
        case MsgUpdateFailHTLC:
3✔
84
                return true
3✔
85
        case MsgUpdateFailMalformedHTLC:
3✔
86
                return true
3✔
UNCOV
87
        case MsgUpdateFee:
×
UNCOV
88
                return true
×
89
        default:
3✔
90
                return false
3✔
91
        }
92
}
93

94
// ErrorEncodeMessage is used when failed to encode the message payload.
UNCOV
95
func ErrorEncodeMessage(err error) error {
×
UNCOV
96
        return fmt.Errorf("failed to encode message to buffer, got %w", err)
×
UNCOV
97
}
×
98

99
// ErrorWriteMessageType is used when failed to write the message type.
100
func ErrorWriteMessageType(err error) error {
×
101
        return fmt.Errorf("failed to write message type, got %w", err)
×
102
}
×
103

104
// ErrorPayloadTooLarge is used when the payload size exceeds the
105
// MaxMsgBody.
UNCOV
106
func ErrorPayloadTooLarge(size int) error {
×
UNCOV
107
        return fmt.Errorf(
×
UNCOV
108
                "message payload is too large - encoded %d bytes, "+
×
UNCOV
109
                        "but maximum message payload is %d bytes",
×
UNCOV
110
                size, MaxMsgBody,
×
UNCOV
111
        )
×
UNCOV
112
}
×
113

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

200
// UnknownMessage is an implementation of the error interface that allows the
201
// creation of an error in response to an unknown message.
202
type UnknownMessage struct {
203
        messageType MessageType
204
}
205

206
// Error returns a human readable string describing the error.
207
//
208
// This is part of the error interface.
209
func (u *UnknownMessage) Error() string {
3✔
210
        return fmt.Sprintf("unable to parse message of unknown type: %v",
3✔
211
                u.messageType)
3✔
212
}
3✔
213

214
// Serializable is an interface which defines a lightning wire serializable
215
// object.
216
type Serializable interface {
217
        // Decode reads the bytes stream and converts it to the object.
218
        Decode(io.Reader, uint32) error
219

220
        // Encode converts object to the bytes stream and write it into the
221
        // write buffer.
222
        Encode(*bytes.Buffer, uint32) error
223
}
224

225
// Message is an interface that defines a lightning wire protocol message. The
226
// interface is general in order to allow implementing types full control over
227
// the representation of its data.
228
type Message interface {
229
        Serializable
230
        MsgType() MessageType
231
}
232

233
// LinkUpdater is an interface implemented by most messages in BOLT 2 that are
234
// allowed to update the channel state.
235
type LinkUpdater interface {
236
        // All LinkUpdater messages are messages and so we embed the interface
237
        // so that we can treat it as a message if all we know about it is that
238
        // it is a LinkUpdater message.
239
        Message
240

241
        // TargetChanID returns the channel id of the link for which this
242
        // message is intended.
243
        TargetChanID() ChannelID
244
}
245

246
// SizeableMessage is an interface that extends the base Message interface with
247
// a method to calculate the serialized size of a message.
248
type SizeableMessage interface {
249
        Message
250

251
        // SerializedSize returns the serialized size of the message in bytes.
252
        // The returned size includes the message type header bytes.
253
        SerializedSize() (uint32, error)
254
}
255

256
// MessageSerializedSize calculates the serialized size of a message in bytes.
257
// This is a helper function that can be used by all message types to implement
258
// the SerializedSize method.
259
func MessageSerializedSize(msg Message) (uint32, error) {
3✔
260
        var buf bytes.Buffer
3✔
261

3✔
262
        // Encode the message to the buffer.
3✔
263
        if err := msg.Encode(&buf, 0); err != nil {
3✔
264
                return 0, err
×
265
        }
×
266

267
        // Add the size of the message type.
268
        return uint32(buf.Len()) + MessageTypeSize, nil
3✔
269
}
270

271
// makeEmptyMessage creates a new empty message of the proper concrete type
272
// based on the passed message type.
273
func makeEmptyMessage(msgType MessageType) (Message, error) {
3✔
274
        var msg Message
3✔
275

3✔
276
        switch msgType {
3✔
UNCOV
277
        case MsgWarning:
×
UNCOV
278
                msg = &Warning{}
×
279
        case MsgStfu:
3✔
280
                msg = &Stfu{}
3✔
281
        case MsgInit:
3✔
282
                msg = &Init{}
3✔
283
        case MsgOpenChannel:
3✔
284
                msg = &OpenChannel{}
3✔
285
        case MsgAcceptChannel:
3✔
286
                msg = &AcceptChannel{}
3✔
287
        case MsgFundingCreated:
3✔
288
                msg = &FundingCreated{}
3✔
289
        case MsgFundingSigned:
3✔
290
                msg = &FundingSigned{}
3✔
291
        case MsgChannelReady:
3✔
292
                msg = &ChannelReady{}
3✔
293
        case MsgShutdown:
3✔
294
                msg = &Shutdown{}
3✔
295
        case MsgClosingSigned:
3✔
296
                msg = &ClosingSigned{}
3✔
UNCOV
297
        case MsgDynPropose:
×
UNCOV
298
                msg = &DynPropose{}
×
UNCOV
299
        case MsgDynAck:
×
UNCOV
300
                msg = &DynAck{}
×
UNCOV
301
        case MsgDynReject:
×
UNCOV
302
                msg = &DynReject{}
×
UNCOV
303
        case MsgKickoffSig:
×
UNCOV
304
                msg = &KickoffSig{}
×
305
        case MsgUpdateAddHTLC:
3✔
306
                msg = &UpdateAddHTLC{}
3✔
307
        case MsgUpdateFailHTLC:
3✔
308
                msg = &UpdateFailHTLC{}
3✔
309
        case MsgUpdateFulfillHTLC:
3✔
310
                msg = &UpdateFulfillHTLC{}
3✔
311
        case MsgCommitSig:
3✔
312
                msg = &CommitSig{}
3✔
313
        case MsgRevokeAndAck:
3✔
314
                msg = &RevokeAndAck{}
3✔
UNCOV
315
        case MsgUpdateFee:
×
UNCOV
316
                msg = &UpdateFee{}
×
317
        case MsgUpdateFailMalformedHTLC:
3✔
318
                msg = &UpdateFailMalformedHTLC{}
3✔
319
        case MsgChannelReestablish:
3✔
320
                msg = &ChannelReestablish{}
3✔
321
        case MsgError:
3✔
322
                msg = &Error{}
3✔
323
        case MsgChannelAnnouncement:
3✔
324
                msg = &ChannelAnnouncement1{}
3✔
325
        case MsgChannelUpdate:
3✔
326
                msg = &ChannelUpdate1{}
3✔
327
        case MsgNodeAnnouncement:
3✔
328
                msg = &NodeAnnouncement{}
3✔
UNCOV
329
        case MsgPing:
×
UNCOV
330
                msg = &Ping{}
×
331
        case MsgAnnounceSignatures:
3✔
332
                msg = &AnnounceSignatures1{}
3✔
UNCOV
333
        case MsgPong:
×
UNCOV
334
                msg = &Pong{}
×
335
        case MsgQueryShortChanIDs:
3✔
336
                msg = &QueryShortChanIDs{}
3✔
337
        case MsgReplyShortChanIDsEnd:
3✔
338
                msg = &ReplyShortChanIDsEnd{}
3✔
339
        case MsgQueryChannelRange:
3✔
340
                msg = &QueryChannelRange{}
3✔
341
        case MsgReplyChannelRange:
3✔
342
                msg = &ReplyChannelRange{}
3✔
343
        case MsgGossipTimestampRange:
3✔
344
                msg = &GossipTimestampRange{}
3✔
345
        case MsgClosingComplete:
3✔
346
                msg = &ClosingComplete{}
3✔
347
        case MsgClosingSig:
3✔
348
                msg = &ClosingSig{}
3✔
UNCOV
349
        case MsgAnnounceSignatures2:
×
UNCOV
350
                msg = &AnnounceSignatures2{}
×
UNCOV
351
        case MsgChannelAnnouncement2:
×
UNCOV
352
                msg = &ChannelAnnouncement2{}
×
UNCOV
353
        case MsgChannelUpdate2:
×
UNCOV
354
                msg = &ChannelUpdate2{}
×
355
        default:
3✔
356
                // If the message is not within our custom range and has not
3✔
357
                // specifically been overridden, return an unknown message.
3✔
358
                //
3✔
359
                // Note that we do not allow custom message overrides to replace
3✔
360
                // known message types, only protocol messages that are not yet
3✔
361
                // known to lnd.
3✔
362
                if msgType < CustomTypeStart && !IsCustomOverride(msgType) {
6✔
363
                        return nil, &UnknownMessage{msgType}
3✔
364
                }
3✔
365

366
                msg = &Custom{
3✔
367
                        Type: msgType,
3✔
368
                }
3✔
369
        }
370

371
        return msg, nil
3✔
372
}
373

374
// MakeEmptyMessage creates a new empty message of the proper concrete type
375
// based on the passed message type. This is exported to be used in tests.
UNCOV
376
func MakeEmptyMessage(msgType MessageType) (Message, error) {
×
UNCOV
377
        return makeEmptyMessage(msgType)
×
UNCOV
378
}
×
379

380
// WriteMessage writes a lightning Message to a buffer including the necessary
381
// header information and returns the number of bytes written. If any error is
382
// encountered, the buffer passed will be reset to its original state since we
383
// don't want any broken bytes left. In other words, no bytes will be written
384
// if there's an error. Either all or none of the message bytes will be written
385
// to the buffer.
386
//
387
// NOTE: this method is not concurrent safe.
388
func WriteMessage(buf *bytes.Buffer, msg Message, pver uint32) (int, error) {
3✔
389
        // Record the size of the bytes already written in buffer.
3✔
390
        oldByteSize := buf.Len()
3✔
391

3✔
392
        // cleanBrokenBytes is a helper closure that helps reset the buffer to
3✔
393
        // its original state. It truncates all the bytes written in current
3✔
394
        // scope.
3✔
395
        var cleanBrokenBytes = func(b *bytes.Buffer) int {
3✔
UNCOV
396
                b.Truncate(oldByteSize)
×
UNCOV
397
                return 0
×
UNCOV
398
        }
×
399

400
        // Write the message type.
401
        var mType [2]byte
3✔
402
        binary.BigEndian.PutUint16(mType[:], uint16(msg.MsgType()))
3✔
403
        msgTypeBytes, err := buf.Write(mType[:])
3✔
404
        if err != nil {
3✔
405
                return cleanBrokenBytes(buf), ErrorWriteMessageType(err)
×
406
        }
×
407

408
        // Use the write buffer to encode our message.
409
        if err := msg.Encode(buf, pver); err != nil {
3✔
UNCOV
410
                return cleanBrokenBytes(buf), ErrorEncodeMessage(err)
×
UNCOV
411
        }
×
412

413
        // Enforce maximum overall message payload. The write buffer now has
414
        // the size of len(originalBytes) + len(payload) + len(type). We want
415
        // to enforce the payload here, so we subtract it by the length of the
416
        // type and old bytes.
417
        lenp := buf.Len() - oldByteSize - msgTypeBytes
3✔
418
        if lenp > MaxMsgBody {
3✔
UNCOV
419
                return cleanBrokenBytes(buf), ErrorPayloadTooLarge(lenp)
×
UNCOV
420
        }
×
421

422
        return buf.Len() - oldByteSize, nil
3✔
423
}
424

425
// ReadMessage reads, validates, and parses the next Lightning message from r
426
// for the provided protocol version.
427
func ReadMessage(r io.Reader, pver uint32) (Message, error) {
3✔
428
        // First, we'll read out the first two bytes of the message so we can
3✔
429
        // create the proper empty message.
3✔
430
        var mType [2]byte
3✔
431
        if _, err := io.ReadFull(r, mType[:]); err != nil {
3✔
432
                return nil, err
×
433
        }
×
434

435
        msgType := MessageType(binary.BigEndian.Uint16(mType[:]))
3✔
436

3✔
437
        // Now that we know the target message type, we can create the proper
3✔
438
        // empty message type and decode the message into it.
3✔
439
        msg, err := makeEmptyMessage(msgType)
3✔
440
        if err != nil {
6✔
441
                return nil, err
3✔
442
        }
3✔
443
        if err := msg.Decode(r, pver); err != nil {
3✔
UNCOV
444
                return nil, err
×
UNCOV
445
        }
×
446

447
        return msg, nil
3✔
448
}
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