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

lightningnetwork / lnd / 13980885714

20 Mar 2025 10:53PM UTC coverage: 58.613% (-10.2%) from 68.789%
13980885714

Pull #9623

github

web-flow
Merge 9eaec1f7a into 09b674508
Pull Request #9623: Size msg test msg

0 of 1572 new or added lines in 42 files covered. (0.0%)

27755 existing lines in 442 files now uncovered.

96886 of 165299 relevant lines covered (58.61%)

1.82 hits per line

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

40.77
/lnwire/closing_complete.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "io"
6

7
        "github.com/btcsuite/btcd/btcutil"
8
        "github.com/lightningnetwork/lnd/tlv"
9
        "pgregory.net/rapid"
10
)
11

12
// ClosingSigs houses the 3 possible signatures that can be sent when
13
// attempting to complete a cooperative channel closure. A signature will
14
// either include both outputs, or only one of the outputs from either side.
15
type ClosingSigs struct {
16
        // CloserNoClosee is a signature that excludes the output of the
17
        // clsoee.
18
        CloserNoClosee tlv.OptionalRecordT[tlv.TlvType1, Sig]
19

20
        // NoCloserClosee is a signature that excludes the output of the
21
        // closer.
22
        NoCloserClosee tlv.OptionalRecordT[tlv.TlvType2, Sig]
23

24
        // CloserAndClosee is a signature that includes both outputs.
25
        CloserAndClosee tlv.OptionalRecordT[tlv.TlvType3, Sig]
26
}
27

28
// ClosingComplete is sent by either side to kick off the process of obtaining
29
// a valid signature on a c o-operative channel closure of their choice.
30
type ClosingComplete struct {
31
        // ChannelID serves to identify which channel is to be closed.
32
        ChannelID ChannelID
33

34
        // CloserScript is the script to which the channel funds will be paid
35
        // for the closer (the person sending the ClosingComplete) message.
36
        CloserScript DeliveryAddress
37

38
        // CloseeScript is the script to which the channel funds will be paid
39
        // (the person receiving the ClosingComplete message).
40
        CloseeScript DeliveryAddress
41

42
        // FeeSatoshis is the total fee in satoshis that the party to the
43
        // channel would like to propose for the close transaction.
44
        FeeSatoshis btcutil.Amount
45

46
        // LockTime is the locktime number to be used in the input spending the
47
        // funding transaction.
48
        LockTime uint32
49

50
        // ClosingSigs houses the 3 possible signatures that can be sent.
51
        ClosingSigs
52

53
        // ExtraData is the set of data that was appended to this message to
54
        // fill out the full maximum transport message size. These fields can
55
        // be used to specify optional data such as custom TLV fields.
56
        ExtraData ExtraOpaqueData
57
}
58

59
// decodeClosingSigs decodes the closing sig TLV records in the passed
60
// ExtraOpaqueData.
61
func decodeClosingSigs(c *ClosingSigs, tlvRecords ExtraOpaqueData) error {
3✔
62
        sig1 := c.CloserNoClosee.Zero()
3✔
63
        sig2 := c.NoCloserClosee.Zero()
3✔
64
        sig3 := c.CloserAndClosee.Zero()
3✔
65

3✔
66
        typeMap, err := tlvRecords.ExtractRecords(&sig1, &sig2, &sig3)
3✔
67
        if err != nil {
3✔
68
                return err
×
69
        }
×
70

71
        // TODO(roasbeef): helper func to made decode of the optional vals
72
        // easier?
73

74
        if val, ok := typeMap[c.CloserNoClosee.TlvType()]; ok && val == nil {
6✔
75
                c.CloserNoClosee = tlv.SomeRecordT(sig1)
3✔
76
        }
3✔
77
        if val, ok := typeMap[c.NoCloserClosee.TlvType()]; ok && val == nil {
3✔
UNCOV
78
                c.NoCloserClosee = tlv.SomeRecordT(sig2)
×
UNCOV
79
        }
×
80
        if val, ok := typeMap[c.CloserAndClosee.TlvType()]; ok && val == nil {
6✔
81
                c.CloserAndClosee = tlv.SomeRecordT(sig3)
3✔
82
        }
3✔
83

84
        return nil
3✔
85
}
86

87
// Decode deserializes a serialized ClosingComplete message stored in the
88
// passed io.Reader.
89
func (c *ClosingComplete) Decode(r io.Reader, _ uint32) error {
3✔
90
        // First, read out all the fields that are hard coded into the message.
3✔
91
        err := ReadElements(
3✔
92
                r, &c.ChannelID, &c.CloserScript, &c.CloseeScript,
3✔
93
                &c.FeeSatoshis, &c.LockTime,
3✔
94
        )
3✔
95
        if err != nil {
3✔
UNCOV
96
                return err
×
UNCOV
97
        }
×
98

99
        // With the hard coded messages read, we'll now read out the TLV fields
100
        // of the message.
101
        var tlvRecords ExtraOpaqueData
3✔
102
        if err := ReadElements(r, &tlvRecords); err != nil {
3✔
103
                return err
×
104
        }
×
105

106
        if err := decodeClosingSigs(&c.ClosingSigs, tlvRecords); err != nil {
3✔
107
                return err
×
108
        }
×
109

110
        if len(tlvRecords) != 0 {
6✔
111
                c.ExtraData = tlvRecords
3✔
112
        }
3✔
113

114
        return nil
3✔
115
}
116

117
// closingSigRecords returns the set of records that encode the closing sigs,
118
// if present.
119
func closingSigRecords(c *ClosingSigs) []tlv.RecordProducer {
3✔
120
        recordProducers := make([]tlv.RecordProducer, 0, 3)
3✔
121
        c.CloserNoClosee.WhenSome(func(sig tlv.RecordT[tlv.TlvType1, Sig]) {
6✔
122
                recordProducers = append(recordProducers, &sig)
3✔
123
        })
3✔
124
        c.NoCloserClosee.WhenSome(func(sig tlv.RecordT[tlv.TlvType2, Sig]) {
3✔
UNCOV
125
                recordProducers = append(recordProducers, &sig)
×
UNCOV
126
        })
×
127
        c.CloserAndClosee.WhenSome(func(sig tlv.RecordT[tlv.TlvType3, Sig]) {
6✔
128
                recordProducers = append(recordProducers, &sig)
3✔
129
        })
3✔
130

131
        return recordProducers
3✔
132
}
133

134
// Encode serializes the target ClosingComplete into the passed io.Writer.
135
func (c *ClosingComplete) Encode(w *bytes.Buffer, _ uint32) error {
3✔
136
        if err := WriteChannelID(w, c.ChannelID); err != nil {
3✔
137
                return err
×
138
        }
×
139

140
        if err := WriteDeliveryAddress(w, c.CloserScript); err != nil {
3✔
141
                return err
×
142
        }
×
143
        if err := WriteDeliveryAddress(w, c.CloseeScript); err != nil {
3✔
144
                return err
×
145
        }
×
146

147
        if err := WriteSatoshi(w, c.FeeSatoshis); err != nil {
3✔
148
                return err
×
149
        }
×
150

151
        if err := WriteUint32(w, c.LockTime); err != nil {
3✔
152
                return err
×
153
        }
×
154

155
        recordProducers := closingSigRecords(&c.ClosingSigs)
3✔
156

3✔
157
        err := EncodeMessageExtraData(&c.ExtraData, recordProducers...)
3✔
158
        if err != nil {
3✔
159
                return err
×
160
        }
×
161

162
        return WriteBytes(w, c.ExtraData)
3✔
163
}
164

165
// MsgType returns the uint32 code which uniquely identifies this message as a
166
// ClosingComplete message on the wire.
167
//
168
// This is part of the lnwire.Message interface.
169
func (c *ClosingComplete) MsgType() MessageType {
3✔
170
        return MsgClosingComplete
3✔
171
}
3✔
172

173
// SerializedSize returns the serialized size of the message in bytes.
174
//
175
// This is part of the lnwire.SizeableMessage interface.
NEW
176
func (c *ClosingComplete) SerializedSize() (uint32, error) {
×
NEW
177
        return MessageSerializedSize(c)
×
NEW
178
}
×
179

180
// A compile time check to ensure ClosingComplete implements the lnwire.Message
181
// interface.
182
var _ Message = (*ClosingComplete)(nil)
183

184
// A compile time check to ensure ClosingComplete implements the
185
// lnwire.SizeableMessage interface.
186
var _ SizeableMessage = (*ClosingComplete)(nil)
187

188
// A compile time check to ensure ClosingComplete implements the
189
// lnwire.TestMessage interface.
190
var _ TestMessage = (*ClosingComplete)(nil)
191

192
// RandTestMessage populates the message with random data suitable for testing.
193
// It uses the rapid testing framework to generate random values.
194
//
195
// This is part of the TestMessage interface.
NEW
196
func (c *ClosingComplete) RandTestMessage(t *rapid.T) Message {
×
NEW
197
        msg := &ClosingComplete{
×
NEW
198
                ChannelID: RandChannelID(t),
×
NEW
199
                FeeSatoshis: btcutil.Amount(rapid.Int64Range(0, 1000000).Draw(
×
NEW
200
                        t, "feeSatoshis"),
×
NEW
201
                ),
×
NEW
202
                LockTime: rapid.Uint32Range(0, 0xffffffff).Draw(
×
NEW
203
                        t, "lockTime",
×
NEW
204
                ),
×
NEW
205
                CloseeScript: RandDeliveryAddress(t),
×
NEW
206
                CloserScript: RandDeliveryAddress(t),
×
NEW
207
                ExtraData:    RandExtraOpaqueData(t, nil),
×
NEW
208
        }
×
NEW
209

×
NEW
210
        includeCloserNoClosee := rapid.Bool().Draw(t, "includeCloserNoClosee")
×
NEW
211
        includeNoCloserClosee := rapid.Bool().Draw(t, "includeNoCloserClosee")
×
NEW
212
        includeCloserAndClosee := rapid.Bool().Draw(t, "includeCloserAndClosee")
×
NEW
213

×
NEW
214
        // Ensure at least one signature is present.
×
NEW
215
        if !includeCloserNoClosee && !includeNoCloserClosee &&
×
NEW
216
                !includeCloserAndClosee {
×
NEW
217

×
NEW
218
                // If all are false, enable at least one randomly.
×
NEW
219
                choice := rapid.IntRange(0, 2).Draw(t, "sigChoice")
×
NEW
220
                switch choice {
×
NEW
221
                case 0:
×
NEW
222
                        includeCloserNoClosee = true
×
NEW
223
                case 1:
×
NEW
224
                        includeNoCloserClosee = true
×
NEW
225
                case 2:
×
NEW
226
                        includeCloserAndClosee = true
×
227
                }
228
        }
229

NEW
230
        if includeCloserNoClosee {
×
NEW
231
                sig := RandSignature(t)
×
NEW
232
                msg.CloserNoClosee = tlv.SomeRecordT(
×
NEW
233
                        tlv.NewRecordT[tlv.TlvType1, Sig](sig),
×
NEW
234
                )
×
NEW
235
        }
×
236

NEW
237
        if includeNoCloserClosee {
×
NEW
238
                sig := RandSignature(t)
×
NEW
239
                msg.NoCloserClosee = tlv.SomeRecordT(
×
NEW
240
                        tlv.NewRecordT[tlv.TlvType2, Sig](sig),
×
NEW
241
                )
×
NEW
242
        }
×
243

NEW
244
        if includeCloserAndClosee {
×
NEW
245
                sig := RandSignature(t)
×
NEW
246
                msg.CloserAndClosee = tlv.SomeRecordT(
×
NEW
247
                        tlv.NewRecordT[tlv.TlvType3, Sig](sig),
×
NEW
248
                )
×
NEW
249
        }
×
250

NEW
251
        return msg
×
252
}
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