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

lightningnetwork / lnd / 16983297902

15 Aug 2025 04:58AM UTC coverage: 66.777% (+0.02%) from 66.758%
16983297902

Pull #10140

github

web-flow
Merge 33b9b55dc into 3841d5554
Pull Request #10140: [2/3] lnwire: fix encoding customized TLV records

178 of 191 new or added lines in 8 files covered. (93.19%)

79 existing lines in 13 files now uncovered.

136014 of 203685 relevant lines covered (66.78%)

21511.77 hits per line

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

75.81
/lnwire/closing_signed.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
// ClosingSigned is sent by both parties to a channel once the channel is clear
12
// of HTLCs, and is primarily concerned with negotiating fees for the close
13
// transaction. Each party provides a signature for a transaction with a fee
14
// that they believe is fair. The process terminates when both sides agree on
15
// the same fee, or when one side force closes the channel.
16
//
17
// NOTE: The responder is able to send a signature without any additional
18
// messages as all transactions are assembled observing BIP 69 which defines a
19
// canonical ordering for input/outputs. Therefore, both sides are able to
20
// arrive at an identical closure transaction as they know the order of the
21
// inputs/outputs.
22
type ClosingSigned struct {
23
        // ChannelID serves to identify which channel is to be closed.
24
        ChannelID ChannelID
25

26
        // FeeSatoshis is the total fee in satoshis that the party to the
27
        // channel would like to propose for the close transaction.
28
        FeeSatoshis btcutil.Amount
29

30
        // Signature is for the proposed channel close transaction.
31
        Signature Sig
32

33
        // PartialSig is used to transmit a musig2 extended partial signature
34
        // that signs the latest fee offer. The nonce isn't sent along side, as
35
        // that has already been sent in the initial shutdown message.
36
        //
37
        // NOTE: This field is only populated if a musig2 taproot channel is
38
        // being signed for. In this case, the above Sig type MUST be blank.
39
        PartialSig OptPartialSigTLV
40

41
        // ExtraData is the set of data that was appended to this message to
42
        // fill out the full maximum transport message size. These fields can
43
        // be used to specify optional data such as custom TLV fields.
44
        ExtraData ExtraOpaqueData
45
}
46

47
// NewClosingSigned creates a new empty ClosingSigned message.
48
func NewClosingSigned(cid ChannelID, fs btcutil.Amount,
49
        sig Sig) *ClosingSigned {
22✔
50

22✔
51
        return &ClosingSigned{
22✔
52
                ChannelID:   cid,
22✔
53
                FeeSatoshis: fs,
22✔
54
                Signature:   sig,
22✔
55
        }
22✔
56
}
22✔
57

58
// A compile time check to ensure ClosingSigned implements the lnwire.Message
59
// interface.
60
var _ Message = (*ClosingSigned)(nil)
61

62
// A compile time check to ensure ClosingSigned implements the
63
// lnwire.SizeableMessage interface.
64
var _ SizeableMessage = (*ClosingSigned)(nil)
65

66
// Decode deserializes a serialized ClosingSigned message stored in the passed
67
// io.Reader observing the specified protocol version.
68
//
69
// This is part of the lnwire.Message interface.
70
func (c *ClosingSigned) Decode(r io.Reader, pver uint32) error {
177✔
71
        err := ReadElements(
177✔
72
                r, &c.ChannelID, &c.FeeSatoshis, &c.Signature,
177✔
73
        )
177✔
74
        if err != nil {
182✔
75
                return err
5✔
76
        }
5✔
77

78
        var tlvRecords ExtraOpaqueData
172✔
79
        if err := ReadElements(r, &tlvRecords); err != nil {
172✔
80
                return err
×
81
        }
×
82

83
        partialSig := c.PartialSig.Zero()
172✔
84
        knownRecords, extraData, err := ParseAndExtractExtraData(
172✔
85
                tlvRecords, &partialSig,
172✔
86
        )
172✔
87
        if err != nil {
216✔
88
                return err
44✔
89
        }
44✔
90

91
        // Set the corresponding TLV types if they were included in the stream.
92
        if _, ok := knownRecords[c.PartialSig.TlvType()]; ok {
192✔
93
                c.PartialSig = tlv.SomeRecordT(partialSig)
64✔
94
        }
64✔
95

96
        c.ExtraData = extraData
128✔
97

128✔
98
        return nil
128✔
99
}
100

101
// Encode serializes the target ClosingSigned into the passed io.Writer
102
// observing the protocol version specified.
103
//
104
// This is part of the lnwire.Message interface.
105
func (c *ClosingSigned) Encode(w *bytes.Buffer, pver uint32) error {
116✔
106
        // Get producers from extra data.
116✔
107
        producers, err := c.ExtraData.RecordProducers()
116✔
108
        if err != nil {
116✔
NEW
109
                return err
×
NEW
110
        }
×
111

112
        c.PartialSig.WhenSome(func(sig PartialSigTLV) {
178✔
113
                producers = append(producers, &sig)
62✔
114
        })
62✔
115

116
        // Pack all records into a new TLV stream.
117
        var tlvData ExtraOpaqueData
116✔
118
        err = tlvData.PackRecords(producers...)
116✔
119
        if err != nil {
116✔
120
                return err
×
121
        }
×
122

123
        if err := WriteChannelID(w, c.ChannelID); err != nil {
116✔
124
                return err
×
125
        }
×
126

127
        if err := WriteSatoshi(w, c.FeeSatoshis); err != nil {
116✔
128
                return err
×
129
        }
×
130

131
        if err := WriteSig(w, c.Signature); err != nil {
116✔
132
                return err
×
133
        }
×
134

135
        return WriteBytes(w, tlvData)
116✔
136
}
137

138
// MsgType returns the integer uniquely identifying this message type on the
139
// wire.
140
//
141
// This is part of the lnwire.Message interface.
142
func (c *ClosingSigned) MsgType() MessageType {
115✔
143
        return MsgClosingSigned
115✔
144
}
115✔
145

146
// SerializedSize returns the serialized size of the message in bytes.
147
//
148
// This is part of the lnwire.SizeableMessage interface.
149
func (c *ClosingSigned) SerializedSize() (uint32, error) {
×
150
        return MessageSerializedSize(c)
×
151
}
×
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