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

lightningnetwork / lnd / 12199391122

06 Dec 2024 01:10PM UTC coverage: 49.807% (-9.1%) from 58.933%
12199391122

push

github

web-flow
Merge pull request #9337 from Guayaba221/patch-1

chore: fix typo in ruby.md

100137 of 201051 relevant lines covered (49.81%)

2.07 hits per line

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

71.54
/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 {
4✔
69
        switch t {
4✔
70
        case MsgUpdateAddHTLC:
4✔
71
                return true
4✔
72
        case MsgUpdateFulfillHTLC:
4✔
73
                return true
4✔
74
        case MsgUpdateFailHTLC:
4✔
75
                return true
4✔
76
        case MsgUpdateFailMalformedHTLC:
4✔
77
                return true
4✔
78
        case MsgUpdateFee:
×
79
                return true
×
80
        default:
4✔
81
                return false
4✔
82
        }
83
}
84

85
// ErrorEncodeMessage is used when failed to encode the message payload.
86
func ErrorEncodeMessage(err error) error {
×
87
        return fmt.Errorf("failed to encode message to buffer, got %w", err)
×
88
}
×
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 {
×
98
        return fmt.Errorf(
×
99
                "message payload is too large - encoded %d bytes, "+
×
100
                        "but maximum message payload is %d bytes",
×
101
                size, MaxMsgBody,
×
102
        )
×
103
}
×
104

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

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

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

337
        return msg, nil
4✔
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) {
4✔
349
        // Record the size of the bytes already written in buffer.
4✔
350
        oldByteSize := buf.Len()
4✔
351

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

360
        // Write the message type.
361
        var mType [2]byte
4✔
362
        binary.BigEndian.PutUint16(mType[:], uint16(msg.MsgType()))
4✔
363
        msgTypeBytes, err := buf.Write(mType[:])
4✔
364
        if err != nil {
4✔
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 {
4✔
370
                return cleanBrokenBytes(buf), ErrorEncodeMessage(err)
×
371
        }
×
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
4✔
378
        if lenp > MaxMsgBody {
4✔
379
                return cleanBrokenBytes(buf), ErrorPayloadTooLarge(lenp)
×
380
        }
×
381

382
        return buf.Len() - oldByteSize, nil
4✔
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) {
4✔
388
        // First, we'll read out the first two bytes of the message so we can
4✔
389
        // create the proper empty message.
4✔
390
        var mType [2]byte
4✔
391
        if _, err := io.ReadFull(r, mType[:]); err != nil {
4✔
392
                return nil, err
×
393
        }
×
394

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

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

407
        return msg, nil
4✔
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