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

lightningnetwork / lnd / 11292787765

11 Oct 2024 12:58PM UTC coverage: 49.179% (-9.5%) from 58.716%
11292787765

push

github

web-flow
Merge pull request #9168 from feelancer21/fix-lncli-wallet-proto

lnrpc: fix lncli documentation tags in walletkit.proto

97369 of 197987 relevant lines covered (49.18%)

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

86
// String return the string representation of message type.
87
func (t MessageType) String() string {
2✔
88
        switch t {
2✔
89
        case MsgWarning:
×
90
                return "Warning"
×
91
        case MsgStfu:
×
92
                return "Stfu"
×
93
        case MsgInit:
2✔
94
                return "Init"
2✔
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:
×
110
                return "DynPropose"
×
111
        case MsgDynAck:
×
112
                return "DynAck"
×
113
        case MsgDynReject:
×
114
                return "DynReject"
×
115
        case MsgKickoffSig:
×
116
                return "KickoffSig"
×
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:
2✔
130
                return "ChannelReestablish"
2✔
131
        case MsgError:
2✔
132
                return "Error"
2✔
133
        case MsgChannelAnnouncement:
2✔
134
                return "ChannelAnnouncement"
2✔
135
        case MsgChannelUpdate:
2✔
136
                return "ChannelUpdate"
2✔
137
        case MsgNodeAnnouncement:
2✔
138
                return "NodeAnnouncement"
2✔
139
        case MsgPing:
2✔
140
                return "Ping"
2✔
141
        case MsgAnnounceSignatures:
2✔
142
                return "AnnounceSignatures"
2✔
143
        case MsgPong:
2✔
144
                return "Pong"
2✔
145
        case MsgUpdateFee:
×
146
                return "UpdateFee"
×
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:
×
158
                return "ClosingComplete"
×
159
        case MsgClosingSig:
×
160
                return "ClosingSig"
×
161
        case MsgAnnounceSignatures2:
×
162
                return "MsgAnnounceSignatures2"
×
163
        case MsgChannelAnnouncement2:
×
164
                return "ChannelAnnouncement2"
×
165
        case MsgChannelUpdate2:
×
166
                return "ChannelUpdate2"
×
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.
181
func (u *UnknownMessage) Error() string {
2✔
182
        return fmt.Sprintf("unable to parse message of unknown type: %v",
2✔
183
                u.messageType)
2✔
184
}
2✔
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) {
2✔
221
        var msg Message
2✔
222

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

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

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

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

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

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

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

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

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