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

lightningnetwork / lnd / 12986279612

27 Jan 2025 09:51AM UTC coverage: 57.652% (-1.1%) from 58.788%
12986279612

Pull #9447

github

yyforyongyu
sweep: rename methods for clarity

We now rename "third party" to "unknown" as the inputs can be spent via
an older sweeping tx, a third party (anchor), or a remote party (pin).
In fee bumper we don't have the info to distinguish the above cases, and
leave them to be further handled by the sweeper as it has more context.
Pull Request #9447: sweep: start tracking input spending status in the fee bumper

83 of 87 new or added lines in 2 files covered. (95.4%)

19578 existing lines in 256 files now uncovered.

103448 of 179434 relevant lines covered (57.65%)

24884.58 hits per line

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

94.07
/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 {
3,696✔
69
        switch t {
3,696✔
70
        case MsgUpdateAddHTLC:
930✔
71
                return true
930✔
72
        case MsgUpdateFulfillHTLC:
250✔
73
                return true
250✔
74
        case MsgUpdateFailHTLC:
138✔
75
                return true
138✔
76
        case MsgUpdateFailMalformedHTLC:
3✔
77
                return true
3✔
78
        case MsgUpdateFee:
3✔
79
                return true
3✔
80
        default:
2,372✔
81
                return false
2,372✔
82
        }
83
}
84

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

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

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

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

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

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

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

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

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

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

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