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

lightningnetwork / lnd / 13980275562

20 Mar 2025 10:06PM UTC coverage: 58.6% (-10.2%) from 68.789%
13980275562

Pull #9623

github

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

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

26603 existing lines in 443 files now uncovered.

96807 of 165200 relevant lines covered (58.6%)

1.82 hits per line

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

46.99
/lnwire/closing_signed.go
1
package lnwire
2

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

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

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

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

32
        // Signature is for the proposed channel close transaction.
33
        Signature Sig
34

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

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

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

3✔
53
        return &ClosingSigned{
3✔
54
                ChannelID:   cid,
3✔
55
                FeeSatoshis: fs,
3✔
56
                Signature:   sig,
3✔
57
        }
3✔
58
}
3✔
59

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

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

68
// A compile time check to ensure ClosingSigned implements the
69
// lnwire.TestMessage interface.
70
var _ TestMessage = (*ClosingSigned)(nil)
71

72
// Decode deserializes a serialized ClosingSigned message stored in the passed
73
// io.Reader observing the specified protocol version.
74
//
75
// This is part of the lnwire.Message interface.
76
func (c *ClosingSigned) Decode(r io.Reader, pver uint32) error {
3✔
77
        err := ReadElements(
3✔
78
                r, &c.ChannelID, &c.FeeSatoshis, &c.Signature,
3✔
79
        )
3✔
80
        if err != nil {
3✔
UNCOV
81
                return err
×
UNCOV
82
        }
×
83

84
        var tlvRecords ExtraOpaqueData
3✔
85
        if err := ReadElements(r, &tlvRecords); err != nil {
3✔
86
                return err
×
87
        }
×
88

89
        partialSig := c.PartialSig.Zero()
3✔
90
        typeMap, err := tlvRecords.ExtractRecords(&partialSig)
3✔
91
        if err != nil {
3✔
UNCOV
92
                return err
×
UNCOV
93
        }
×
94

95
        // Set the corresponding TLV types if they were included in the stream.
96
        if val, ok := typeMap[c.PartialSig.TlvType()]; ok && val == nil {
6✔
97
                c.PartialSig = tlv.SomeRecordT(partialSig)
3✔
98
        }
3✔
99

100
        if len(tlvRecords) != 0 {
6✔
101
                c.ExtraData = tlvRecords
3✔
102
        }
3✔
103

104
        return nil
3✔
105
}
106

107
// Encode serializes the target ClosingSigned into the passed io.Writer
108
// observing the protocol version specified.
109
//
110
// This is part of the lnwire.Message interface.
111
func (c *ClosingSigned) Encode(w *bytes.Buffer, pver uint32) error {
3✔
112
        recordProducers := make([]tlv.RecordProducer, 0, 1)
3✔
113
        c.PartialSig.WhenSome(func(sig PartialSigTLV) {
6✔
114
                recordProducers = append(recordProducers, &sig)
3✔
115
        })
3✔
116
        err := EncodeMessageExtraData(&c.ExtraData, recordProducers...)
3✔
117
        if err != nil {
3✔
118
                return err
×
119
        }
×
120

121
        if err := WriteChannelID(w, c.ChannelID); err != nil {
3✔
122
                return err
×
123
        }
×
124

125
        if err := WriteSatoshi(w, c.FeeSatoshis); err != nil {
3✔
126
                return err
×
127
        }
×
128

129
        if err := WriteSig(w, c.Signature); err != nil {
3✔
130
                return err
×
131
        }
×
132

133
        return WriteBytes(w, c.ExtraData)
3✔
134
}
135

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

144
// SerializedSize returns the serialized size of the message in bytes.
145
//
146
// This is part of the lnwire.SizeableMessage interface.
NEW
147
func (c *ClosingSigned) SerializedSize() (uint32, error) {
×
NEW
148
        return MessageSerializedSize(c)
×
NEW
149
}
×
150

151
// RandTestMessage populates the message with random data suitable for testing.
152
// It uses the rapid testing framework to generate random values.
153
//
154
// This is part of the TestMessage interface.
NEW
155
func (c *ClosingSigned) RandTestMessage(t *rapid.T) Message {
×
NEW
156
        // Generate a random boolean to decide whether to include CommitSig or
×
NEW
157
        // PartialSig Since they're mutually exclusive, when one is populated,
×
NEW
158
        // the other must be blank.
×
NEW
159
        usePartialSig := rapid.Bool().Draw(t, "usePartialSig")
×
NEW
160

×
NEW
161
        msg := &ClosingSigned{
×
NEW
162
                ChannelID: RandChannelID(t),
×
NEW
163
                FeeSatoshis: btcutil.Amount(
×
NEW
164
                        rapid.Int64Range(0, 1000000).Draw(t, "feeSatoshis"),
×
NEW
165
                ),
×
NEW
166
                ExtraData: RandExtraOpaqueData(t, nil),
×
NEW
167
        }
×
NEW
168

×
NEW
169
        if usePartialSig {
×
NEW
170
                sigBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(
×
NEW
171
                        t, "sigScalar",
×
NEW
172
                )
×
NEW
173
                var s btcec.ModNScalar
×
NEW
174
                _ = s.SetByteSlice(sigBytes)
×
NEW
175

×
NEW
176
                msg.PartialSig = SomePartialSig(NewPartialSig(s))
×
NEW
177
                msg.Signature = Sig{}
×
NEW
178
        } else {
×
NEW
179
                msg.Signature = RandSignature(t)
×
NEW
180
        }
×
181

NEW
182
        return msg
×
183
}
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