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

lightningnetwork / lnd / 11393106485

17 Oct 2024 09:10PM UTC coverage: 57.848% (-1.0%) from 58.81%
11393106485

Pull #9148

github

ProofOfKeags
lnwire: convert DynPropose and DynCommit to use typed tlv records
Pull Request #9148: DynComms [2/n]: lnwire: add authenticated wire messages for Dyn*

142 of 177 new or added lines in 4 files covered. (80.23%)

18983 existing lines in 242 files now uncovered.

99003 of 171143 relevant lines covered (57.85%)

36968.25 hits per line

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

93.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
// 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
        MsgDynCommit                           = 117
44
        MsgUpdateAddHTLC                       = 128
45
        MsgUpdateFulfillHTLC                   = 130
46
        MsgUpdateFailHTLC                      = 131
47
        MsgCommitSig                           = 132
48
        MsgRevokeAndAck                        = 133
49
        MsgUpdateFee                           = 134
50
        MsgUpdateFailMalformedHTLC             = 135
51
        MsgChannelReestablish                  = 136
52
        MsgChannelAnnouncement                 = 256
53
        MsgNodeAnnouncement                    = 257
54
        MsgChannelUpdate                       = 258
55
        MsgAnnounceSignatures                  = 259
56
        MsgAnnounceSignatures2                 = 260
57
        MsgQueryShortChanIDs                   = 261
58
        MsgReplyShortChanIDsEnd                = 262
59
        MsgQueryChannelRange                   = 263
60
        MsgReplyChannelRange                   = 264
61
        MsgGossipTimestampRange                = 265
62
        MsgChannelAnnouncement2                = 267
63
        MsgChannelUpdate2                      = 271
64
        MsgKickoffSig                          = 777
65
)
66

67
// ErrorEncodeMessage is used when failed to encode the message payload.
68
func ErrorEncodeMessage(err error) error {
4✔
69
        return fmt.Errorf("failed to encode message to buffer, got %w", err)
4✔
70
}
4✔
71

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

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

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

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

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

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

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

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

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

216
        // TargetChanID returns the channel id of the link for which this
217
        // message is intended.
218
        TargetChanID() ChannelID
219
}
220

221
// makeEmptyMessage creates a new empty message of the proper concrete type
222
// based on the passed message type.
223
func makeEmptyMessage(msgType MessageType) (Message, error) {
22,380✔
224
        var msg Message
22,380✔
225

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

318
                msg = &Custom{
9✔
319
                        Type: msgType,
9✔
320
                }
9✔
321
        }
322

323
        return msg, nil
22,379✔
324
}
325

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

21,437✔
338
        // cleanBrokenBytes is a helper closure that helps reset the buffer to
21,437✔
339
        // its original state. It truncates all the bytes written in current
21,437✔
340
        // scope.
21,437✔
341
        var cleanBrokenBytes = func(b *bytes.Buffer) int {
21,441✔
342
                b.Truncate(oldByteSize)
4✔
343
                return 0
4✔
344
        }
4✔
345

346
        // Write the message type.
347
        var mType [2]byte
21,437✔
348
        binary.BigEndian.PutUint16(mType[:], uint16(msg.MsgType()))
21,437✔
349
        msgTypeBytes, err := buf.Write(mType[:])
21,437✔
350
        if err != nil {
21,437✔
351
                return cleanBrokenBytes(buf), ErrorWriteMessageType(err)
×
352
        }
×
353

354
        // Use the write buffer to encode our message.
355
        if err := msg.Encode(buf, pver); err != nil {
21,440✔
356
                return cleanBrokenBytes(buf), ErrorEncodeMessage(err)
3✔
357
        }
3✔
358

359
        // Enforce maximum overall message payload. The write buffer now has
360
        // the size of len(originalBytes) + len(payload) + len(type). We want
361
        // to enforce the payload here, so we subtract it by the length of the
362
        // type and old bytes.
363
        lenp := buf.Len() - oldByteSize - msgTypeBytes
21,434✔
364
        if lenp > MaxMsgBody {
21,435✔
365
                return cleanBrokenBytes(buf), ErrorPayloadTooLarge(lenp)
1✔
366
        }
1✔
367

368
        return buf.Len() - oldByteSize, nil
21,433✔
369
}
370

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

381
        msgType := MessageType(binary.BigEndian.Uint16(mType[:]))
22,379✔
382

22,379✔
383
        // Now that we know the target message type, we can create the proper
22,379✔
384
        // empty message type and decode the message into it.
22,379✔
385
        msg, err := makeEmptyMessage(msgType)
22,379✔
386
        if err != nil {
22,379✔
UNCOV
387
                return nil, err
×
UNCOV
388
        }
×
389
        if err := msg.Decode(r, pver); err != nil {
24,190✔
390
                return nil, err
1,811✔
391
        }
1,811✔
392

393
        return msg, nil
20,568✔
394
}
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