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

lightningnetwork / lnd / 12026968820

26 Nov 2024 08:48AM UTC coverage: 49.896% (-9.1%) from 58.999%
12026968820

Pull #9303

github

yyforyongyu
lnwallet: add debug logs
Pull Request #9303: htlcswitch+routing: handle nil pointer dereference properly

20 of 23 new or added lines in 4 files covered. (86.96%)

25375 existing lines in 428 files now uncovered.

99993 of 200404 relevant lines covered (49.9%)

2.07 hits per line

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

69.04
/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
// ErrorEncodeMessage is used when failed to encode the message payload.
UNCOV
67
func ErrorEncodeMessage(err error) error {
×
UNCOV
68
        return fmt.Errorf("failed to encode message to buffer, got %w", err)
×
UNCOV
69
}
×
70

71
// ErrorWriteMessageType is used when failed to write the message type.
72
func ErrorWriteMessageType(err error) error {
×
73
        return fmt.Errorf("failed to write message type, got %w", err)
×
74
}
×
75

76
// ErrorPayloadTooLarge is used when the payload size exceeds the
77
// MaxMsgBody.
UNCOV
78
func ErrorPayloadTooLarge(size int) error {
×
UNCOV
79
        return fmt.Errorf(
×
UNCOV
80
                "message payload is too large - encoded %d bytes, "+
×
UNCOV
81
                        "but maximum message payload is %d bytes",
×
UNCOV
82
                size, MaxMsgBody,
×
UNCOV
83
        )
×
UNCOV
84
}
×
85

86
// String return the string representation of message type.
87
func (t MessageType) String() string {
4✔
88
        switch t {
4✔
UNCOV
89
        case MsgWarning:
×
UNCOV
90
                return "Warning"
×
UNCOV
91
        case MsgStfu:
×
UNCOV
92
                return "Stfu"
×
93
        case MsgInit:
4✔
94
                return "Init"
4✔
95
        case MsgOpenChannel:
4✔
96
                return "MsgOpenChannel"
4✔
97
        case MsgAcceptChannel:
4✔
98
                return "MsgAcceptChannel"
4✔
99
        case MsgFundingCreated:
4✔
100
                return "MsgFundingCreated"
4✔
101
        case MsgFundingSigned:
4✔
102
                return "MsgFundingSigned"
4✔
103
        case MsgChannelReady:
4✔
104
                return "ChannelReady"
4✔
105
        case MsgShutdown:
4✔
106
                return "Shutdown"
4✔
107
        case MsgClosingSigned:
4✔
108
                return "ClosingSigned"
4✔
UNCOV
109
        case MsgDynPropose:
×
UNCOV
110
                return "DynPropose"
×
UNCOV
111
        case MsgDynAck:
×
UNCOV
112
                return "DynAck"
×
UNCOV
113
        case MsgDynReject:
×
UNCOV
114
                return "DynReject"
×
UNCOV
115
        case MsgKickoffSig:
×
UNCOV
116
                return "KickoffSig"
×
117
        case MsgUpdateAddHTLC:
4✔
118
                return "UpdateAddHTLC"
4✔
119
        case MsgUpdateFailHTLC:
4✔
120
                return "UpdateFailHTLC"
4✔
121
        case MsgUpdateFulfillHTLC:
4✔
122
                return "UpdateFulfillHTLC"
4✔
123
        case MsgCommitSig:
4✔
124
                return "CommitSig"
4✔
125
        case MsgRevokeAndAck:
4✔
126
                return "RevokeAndAck"
4✔
127
        case MsgUpdateFailMalformedHTLC:
4✔
128
                return "UpdateFailMalformedHTLC"
4✔
129
        case MsgChannelReestablish:
4✔
130
                return "ChannelReestablish"
4✔
131
        case MsgError:
4✔
132
                return "Error"
4✔
133
        case MsgChannelAnnouncement:
4✔
134
                return "ChannelAnnouncement"
4✔
135
        case MsgChannelUpdate:
4✔
136
                return "ChannelUpdate"
4✔
137
        case MsgNodeAnnouncement:
4✔
138
                return "NodeAnnouncement"
4✔
139
        case MsgPing:
1✔
140
                return "Ping"
1✔
141
        case MsgAnnounceSignatures:
4✔
142
                return "AnnounceSignatures"
4✔
143
        case MsgPong:
1✔
144
                return "Pong"
1✔
UNCOV
145
        case MsgUpdateFee:
×
UNCOV
146
                return "UpdateFee"
×
147
        case MsgQueryShortChanIDs:
4✔
148
                return "QueryShortChanIDs"
4✔
149
        case MsgReplyShortChanIDsEnd:
4✔
150
                return "ReplyShortChanIDsEnd"
4✔
151
        case MsgQueryChannelRange:
4✔
152
                return "QueryChannelRange"
4✔
153
        case MsgReplyChannelRange:
4✔
154
                return "ReplyChannelRange"
4✔
155
        case MsgGossipTimestampRange:
4✔
156
                return "GossipTimestampRange"
4✔
UNCOV
157
        case MsgClosingComplete:
×
UNCOV
158
                return "ClosingComplete"
×
UNCOV
159
        case MsgClosingSig:
×
UNCOV
160
                return "ClosingSig"
×
UNCOV
161
        case MsgAnnounceSignatures2:
×
UNCOV
162
                return "MsgAnnounceSignatures2"
×
UNCOV
163
        case MsgChannelAnnouncement2:
×
UNCOV
164
                return "ChannelAnnouncement2"
×
UNCOV
165
        case MsgChannelUpdate2:
×
UNCOV
166
                return "ChannelUpdate2"
×
167
        default:
4✔
168
                return "<unknown>"
4✔
169
        }
170
}
171

172
// UnknownMessage is an implementation of the error interface that allows the
173
// creation of an error in response to an unknown message.
174
type UnknownMessage struct {
175
        messageType MessageType
176
}
177

178
// Error returns a human readable string describing the error.
179
//
180
// This is part of the error interface.
181
func (u *UnknownMessage) Error() string {
4✔
182
        return fmt.Sprintf("unable to parse message of unknown type: %v",
4✔
183
                u.messageType)
4✔
184
}
4✔
185

186
// Serializable is an interface which defines a lightning wire serializable
187
// object.
188
type Serializable interface {
189
        // Decode reads the bytes stream and converts it to the object.
190
        Decode(io.Reader, uint32) error
191

192
        // Encode converts object to the bytes stream and write it into the
193
        // write buffer.
194
        Encode(*bytes.Buffer, uint32) error
195
}
196

197
// Message is an interface that defines a lightning wire protocol message. The
198
// interface is general in order to allow implementing types full control over
199
// the representation of its data.
200
type Message interface {
201
        Serializable
202
        MsgType() MessageType
203
}
204

205
// LinkUpdater is an interface implemented by most messages in BOLT 2 that are
206
// allowed to update the channel state.
207
type LinkUpdater interface {
208
        // All LinkUpdater messages are messages and so we embed the interface
209
        // so that we can treat it as a message if all we know about it is that
210
        // it is a LinkUpdater message.
211
        Message
212

213
        // TargetChanID returns the channel id of the link for which this
214
        // message is intended.
215
        TargetChanID() ChannelID
216
}
217

218
// makeEmptyMessage creates a new empty message of the proper concrete type
219
// based on the passed message type.
220
func makeEmptyMessage(msgType MessageType) (Message, error) {
4✔
221
        var msg Message
4✔
222

4✔
223
        switch msgType {
4✔
UNCOV
224
        case MsgWarning:
×
UNCOV
225
                msg = &Warning{}
×
UNCOV
226
        case MsgStfu:
×
UNCOV
227
                msg = &Stfu{}
×
228
        case MsgInit:
4✔
229
                msg = &Init{}
4✔
230
        case MsgOpenChannel:
4✔
231
                msg = &OpenChannel{}
4✔
232
        case MsgAcceptChannel:
4✔
233
                msg = &AcceptChannel{}
4✔
234
        case MsgFundingCreated:
4✔
235
                msg = &FundingCreated{}
4✔
236
        case MsgFundingSigned:
4✔
237
                msg = &FundingSigned{}
4✔
238
        case MsgChannelReady:
4✔
239
                msg = &ChannelReady{}
4✔
240
        case MsgShutdown:
4✔
241
                msg = &Shutdown{}
4✔
242
        case MsgClosingSigned:
4✔
243
                msg = &ClosingSigned{}
4✔
UNCOV
244
        case MsgDynPropose:
×
UNCOV
245
                msg = &DynPropose{}
×
UNCOV
246
        case MsgDynAck:
×
UNCOV
247
                msg = &DynAck{}
×
UNCOV
248
        case MsgDynReject:
×
UNCOV
249
                msg = &DynReject{}
×
UNCOV
250
        case MsgKickoffSig:
×
UNCOV
251
                msg = &KickoffSig{}
×
252
        case MsgUpdateAddHTLC:
4✔
253
                msg = &UpdateAddHTLC{}
4✔
254
        case MsgUpdateFailHTLC:
4✔
255
                msg = &UpdateFailHTLC{}
4✔
256
        case MsgUpdateFulfillHTLC:
4✔
257
                msg = &UpdateFulfillHTLC{}
4✔
258
        case MsgCommitSig:
4✔
259
                msg = &CommitSig{}
4✔
260
        case MsgRevokeAndAck:
4✔
261
                msg = &RevokeAndAck{}
4✔
UNCOV
262
        case MsgUpdateFee:
×
UNCOV
263
                msg = &UpdateFee{}
×
264
        case MsgUpdateFailMalformedHTLC:
4✔
265
                msg = &UpdateFailMalformedHTLC{}
4✔
266
        case MsgChannelReestablish:
4✔
267
                msg = &ChannelReestablish{}
4✔
268
        case MsgError:
4✔
269
                msg = &Error{}
4✔
270
        case MsgChannelAnnouncement:
4✔
271
                msg = &ChannelAnnouncement1{}
4✔
272
        case MsgChannelUpdate:
4✔
273
                msg = &ChannelUpdate1{}
4✔
274
        case MsgNodeAnnouncement:
4✔
275
                msg = &NodeAnnouncement{}
4✔
276
        case MsgPing:
1✔
277
                msg = &Ping{}
1✔
278
        case MsgAnnounceSignatures:
4✔
279
                msg = &AnnounceSignatures1{}
4✔
280
        case MsgPong:
1✔
281
                msg = &Pong{}
1✔
282
        case MsgQueryShortChanIDs:
4✔
283
                msg = &QueryShortChanIDs{}
4✔
284
        case MsgReplyShortChanIDsEnd:
4✔
285
                msg = &ReplyShortChanIDsEnd{}
4✔
286
        case MsgQueryChannelRange:
4✔
287
                msg = &QueryChannelRange{}
4✔
288
        case MsgReplyChannelRange:
4✔
289
                msg = &ReplyChannelRange{}
4✔
290
        case MsgGossipTimestampRange:
4✔
291
                msg = &GossipTimestampRange{}
4✔
UNCOV
292
        case MsgClosingComplete:
×
UNCOV
293
                msg = &ClosingComplete{}
×
UNCOV
294
        case MsgClosingSig:
×
UNCOV
295
                msg = &ClosingSig{}
×
UNCOV
296
        case MsgAnnounceSignatures2:
×
UNCOV
297
                msg = &AnnounceSignatures2{}
×
UNCOV
298
        case MsgChannelAnnouncement2:
×
UNCOV
299
                msg = &ChannelAnnouncement2{}
×
UNCOV
300
        case MsgChannelUpdate2:
×
UNCOV
301
                msg = &ChannelUpdate2{}
×
302
        default:
4✔
303
                // If the message is not within our custom range and has not
4✔
304
                // specifically been overridden, return an unknown message.
4✔
305
                //
4✔
306
                // Note that we do not allow custom message overrides to replace
4✔
307
                // known message types, only protocol messages that are not yet
4✔
308
                // known to lnd.
4✔
309
                if msgType < CustomTypeStart && !IsCustomOverride(msgType) {
8✔
310
                        return nil, &UnknownMessage{msgType}
4✔
311
                }
4✔
312

313
                msg = &Custom{
4✔
314
                        Type: msgType,
4✔
315
                }
4✔
316
        }
317

318
        return msg, nil
4✔
319
}
320

321
// WriteMessage writes a lightning Message to a buffer including the necessary
322
// header information and returns the number of bytes written. If any error is
323
// encountered, the buffer passed will be reset to its original state since we
324
// don't want any broken bytes left. In other words, no bytes will be written
325
// if there's an error. Either all or none of the message bytes will be written
326
// to the buffer.
327
//
328
// NOTE: this method is not concurrent safe.
329
func WriteMessage(buf *bytes.Buffer, msg Message, pver uint32) (int, error) {
4✔
330
        // Record the size of the bytes already written in buffer.
4✔
331
        oldByteSize := buf.Len()
4✔
332

4✔
333
        // cleanBrokenBytes is a helper closure that helps reset the buffer to
4✔
334
        // its original state. It truncates all the bytes written in current
4✔
335
        // scope.
4✔
336
        var cleanBrokenBytes = func(b *bytes.Buffer) int {
4✔
UNCOV
337
                b.Truncate(oldByteSize)
×
UNCOV
338
                return 0
×
UNCOV
339
        }
×
340

341
        // Write the message type.
342
        var mType [2]byte
4✔
343
        binary.BigEndian.PutUint16(mType[:], uint16(msg.MsgType()))
4✔
344
        msgTypeBytes, err := buf.Write(mType[:])
4✔
345
        if err != nil {
4✔
346
                return cleanBrokenBytes(buf), ErrorWriteMessageType(err)
×
347
        }
×
348

349
        // Use the write buffer to encode our message.
350
        if err := msg.Encode(buf, pver); err != nil {
4✔
UNCOV
351
                return cleanBrokenBytes(buf), ErrorEncodeMessage(err)
×
UNCOV
352
        }
×
353

354
        // Enforce maximum overall message payload. The write buffer now has
355
        // the size of len(originalBytes) + len(payload) + len(type). We want
356
        // to enforce the payload here, so we subtract it by the length of the
357
        // type and old bytes.
358
        lenp := buf.Len() - oldByteSize - msgTypeBytes
4✔
359
        if lenp > MaxMsgBody {
4✔
UNCOV
360
                return cleanBrokenBytes(buf), ErrorPayloadTooLarge(lenp)
×
UNCOV
361
        }
×
362

363
        return buf.Len() - oldByteSize, nil
4✔
364
}
365

366
// ReadMessage reads, validates, and parses the next Lightning message from r
367
// for the provided protocol version.
368
func ReadMessage(r io.Reader, pver uint32) (Message, error) {
4✔
369
        // First, we'll read out the first two bytes of the message so we can
4✔
370
        // create the proper empty message.
4✔
371
        var mType [2]byte
4✔
372
        if _, err := io.ReadFull(r, mType[:]); err != nil {
4✔
373
                return nil, err
×
374
        }
×
375

376
        msgType := MessageType(binary.BigEndian.Uint16(mType[:]))
4✔
377

4✔
378
        // Now that we know the target message type, we can create the proper
4✔
379
        // empty message type and decode the message into it.
4✔
380
        msg, err := makeEmptyMessage(msgType)
4✔
381
        if err != nil {
8✔
382
                return nil, err
4✔
383
        }
4✔
384
        if err := msg.Decode(r, pver); err != nil {
4✔
UNCOV
385
                return nil, err
×
UNCOV
386
        }
×
387

388
        return msg, nil
4✔
389
}
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