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

lightningnetwork / lnd / 11880886288

17 Nov 2024 05:35PM UTC coverage: 57.94% (-1.0%) from 58.935%
11880886288

Pull #9274

github

ziggie1984
docs: add release-notes
Pull Request #9274: Decrease outgoing htlc budget

3 of 4 new or added lines in 1 file covered. (75.0%)

19093 existing lines in 233 files now uncovered.

100208 of 172952 relevant lines covered (57.94%)

25486.44 hits per line

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

94.56
/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.
67
func ErrorEncodeMessage(err error) error {
4✔
68
        return fmt.Errorf("failed to encode message to buffer, got %w", err)
4✔
69
}
4✔
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.
78
func ErrorPayloadTooLarge(size int) error {
2✔
79
        return fmt.Errorf(
2✔
80
                "message payload is too large - encoded %d bytes, "+
2✔
81
                        "but maximum message payload is %d bytes",
2✔
82
                size, MaxMsgBody,
2✔
83
        )
2✔
84
}
2✔
85

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

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

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

318
        return msg, nil
13,739✔
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) {
12,869✔
330
        // Record the size of the bytes already written in buffer.
12,869✔
331
        oldByteSize := buf.Len()
12,869✔
332

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

341
        // Write the message type.
342
        var mType [2]byte
12,869✔
343
        binary.BigEndian.PutUint16(mType[:], uint16(msg.MsgType()))
12,869✔
344
        msgTypeBytes, err := buf.Write(mType[:])
12,869✔
345
        if err != nil {
12,869✔
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 {
12,872✔
351
                return cleanBrokenBytes(buf), ErrorEncodeMessage(err)
3✔
352
        }
3✔
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
12,866✔
359
        if lenp > MaxMsgBody {
12,867✔
360
                return cleanBrokenBytes(buf), ErrorPayloadTooLarge(lenp)
1✔
361
        }
1✔
362

363
        return buf.Len() - oldByteSize, nil
12,865✔
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) {
13,739✔
369
        // First, we'll read out the first two bytes of the message so we can
13,739✔
370
        // create the proper empty message.
13,739✔
371
        var mType [2]byte
13,739✔
372
        if _, err := io.ReadFull(r, mType[:]); err != nil {
13,739✔
373
                return nil, err
×
374
        }
×
375

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

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

388
        return msg, nil
11,977✔
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