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

lightningnetwork / lnd / 16807123789

07 Aug 2025 02:18PM UTC coverage: 54.85% (-12.1%) from 66.947%
16807123789

Pull #10140

github

web-flow
Merge d256dafec into 2269859d9
Pull Request #10140: [2/3] lnwire: fix encoding customized TLV records

192 of 207 new or added lines in 9 files covered. (92.75%)

23859 existing lines in 288 files now uncovered.

108730 of 198233 relevant lines covered (54.85%)

22096.12 hits per line

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

74.16
/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
)
10

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

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

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

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

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

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

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

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

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

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

58
// decodeClosingSigs decodes the closing sig TLV records in the passed
59
// ExtraOpaqueData.
60
func decodeClosingSigs(c *ClosingSigs, tlvRecords ExtraOpaqueData) (
61
        ExtraOpaqueData, error) {
202✔
62

202✔
63
        sig1 := c.CloserNoClosee.Zero()
202✔
64
        sig2 := c.NoCloserClosee.Zero()
202✔
65
        sig3 := c.CloserAndClosee.Zero()
202✔
66

202✔
67
        knownRecords, extraData, err := ParseAndExtractExtraData(
202✔
68
                tlvRecords, &sig1, &sig2, &sig3,
202✔
69
        )
202✔
70
        if err != nil {
202✔
NEW
71
                return nil, err
×
72
        }
×
73

74
        if _, ok := knownRecords[c.CloserNoClosee.TlvType()]; ok {
306✔
75
                c.CloserNoClosee = tlv.SomeRecordT(sig1)
104✔
76
        }
104✔
77
        if _, ok := knownRecords[c.NoCloserClosee.TlvType()]; ok {
313✔
78
                c.NoCloserClosee = tlv.SomeRecordT(sig2)
111✔
79
        }
111✔
80
        if _, ok := knownRecords[c.CloserAndClosee.TlvType()]; ok {
312✔
81
                c.CloserAndClosee = tlv.SomeRecordT(sig3)
110✔
82
        }
110✔
83

84
        return extraData, nil
202✔
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 {
160✔
90
        // First, read out all the fields that are hard coded into the message.
160✔
91
        err := ReadElements(
160✔
92
                r, &c.ChannelID, &c.CloserScript, &c.CloseeScript,
160✔
93
                &c.FeeSatoshis, &c.LockTime,
160✔
94
        )
160✔
95
        if err != nil {
219✔
96
                return err
59✔
97
        }
59✔
98

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

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

111
        c.ExtraData = extraData
101✔
112

101✔
113
        return nil
101✔
114
}
115

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

130
        return recordProducers
202✔
131
}
132

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

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

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

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

154
        // Get producers from extra data.
155
        producers, err := c.ExtraData.RecordProducers()
101✔
156
        if err != nil {
101✔
NEW
157
                return err
×
NEW
158
        }
×
159

160
        producers = append(producers, closingSigRecords(&c.ClosingSigs)...)
101✔
161

101✔
162
        // Pack all records into a new TLV stream.
101✔
163
        var tlvData ExtraOpaqueData
101✔
164
        err = tlvData.PackRecords(producers...)
101✔
165
        if err != nil {
101✔
166
                return err
×
167
        }
×
168

169
        return WriteBytes(w, tlvData)
101✔
170
}
171

172
// MsgType returns the uint32 code which uniquely identifies this message as a
173
// ClosingComplete message on the wire.
174
//
175
// This is part of the lnwire.Message interface.
176
func (c *ClosingComplete) MsgType() MessageType {
100✔
177
        return MsgClosingComplete
100✔
178
}
100✔
179

180
// SerializedSize returns the serialized size of the message in bytes.
181
//
182
// This is part of the lnwire.SizeableMessage interface.
183
func (c *ClosingComplete) SerializedSize() (uint32, error) {
×
184
        return MessageSerializedSize(c)
×
185
}
×
186

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

191
// A compile time check to ensure ClosingComplete implements the
192
// lnwire.SizeableMessage interface.
193
var _ SizeableMessage = (*ClosingComplete)(nil)
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