• 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

52.38
/lnwire/commit_sig.go
1
package lnwire
2

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

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

11
// CommitSig is sent by either side to stage any pending HTLC's in the
12
// receiver's pending set into a new commitment state. Implicitly, the new
13
// commitment transaction constructed which has been signed by CommitSig
14
// includes all HTLC's in the remote node's pending set. A CommitSig message
15
// may be sent after a series of UpdateAddHTLC/UpdateFulfillHTLC messages in
16
// order to batch add several HTLC's with a single signature covering all
17
// implicitly accepted HTLC's.
18
type CommitSig struct {
19
        // ChanID uniquely identifies to which currently active channel this
20
        // CommitSig applies to.
21
        ChanID ChannelID
22

23
        // CommitSig is Alice's signature for Bob's new commitment transaction.
24
        // Alice is able to send this signature without requesting any
25
        // additional data due to the piggybacking of Bob's next revocation
26
        // hash in his prior RevokeAndAck message, as well as the canonical
27
        // ordering used for all inputs/outputs within commitment transactions.
28
        // If initiating a new commitment state, this signature should ONLY
29
        // cover all of the sending party's pending log updates, and the log
30
        // updates of the remote party that have been ACK'd.
31
        CommitSig Sig
32

33
        // HtlcSigs is a signature for each relevant HTLC output within the
34
        // created commitment. The order of the signatures is expected to be
35
        // identical to the placement of the HTLC's within the BIP 69 sorted
36
        // commitment transaction. For each outgoing HTLC (from the PoV of the
37
        // sender of this message), a signature for an HTLC timeout transaction
38
        // should be signed, for each incoming HTLC the HTLC timeout
39
        // transaction should be signed.
40
        HtlcSigs []Sig
41

42
        // PartialSig is used to transmit a musig2 extended partial signature
43
        // that also carries along the public nonce of the signer.
44
        //
45
        // NOTE: This field is only populated if a musig2 taproot channel is
46
        // being signed for. In this case, the above Sig type MUST be blank.
47
        PartialSig OptPartialSigWithNonceTLV
48

49
        // CustomRecords maps TLV types to byte slices, storing arbitrary data
50
        // intended for inclusion in the ExtraData field.
51
        CustomRecords CustomRecords
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
// NewCommitSig creates a new empty CommitSig message.
UNCOV
60
func NewCommitSig() *CommitSig {
×
UNCOV
61
        return &CommitSig{}
×
UNCOV
62
}
×
63

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

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

72
// A compile time check to ensure CommitSig implements the lnwire.TestMessage
73
// interface.
74
var _ TestMessage = (*CommitSig)(nil)
75

76
// Decode deserializes a serialized CommitSig message stored in the
77
// passed io.Reader observing the specified protocol version.
78
//
79
// This is part of the lnwire.Message interface.
80
func (c *CommitSig) Decode(r io.Reader, pver uint32) error {
3✔
81
        // msgExtraData is a temporary variable used to read the message extra
3✔
82
        // data field from the reader.
3✔
83
        var msgExtraData ExtraOpaqueData
3✔
84

3✔
85
        err := ReadElements(r,
3✔
86
                &c.ChanID,
3✔
87
                &c.CommitSig,
3✔
88
                &c.HtlcSigs,
3✔
89
                &msgExtraData,
3✔
90
        )
3✔
91
        if err != nil {
3✔
UNCOV
92
                return err
×
UNCOV
93
        }
×
94

95
        // Extract TLV records from the extra data field.
96
        partialSig := c.PartialSig.Zero()
3✔
97

3✔
98
        customRecords, parsed, extraData, err := ParseAndExtractCustomRecords(
3✔
99
                msgExtraData, &partialSig,
3✔
100
        )
3✔
101
        if err != nil {
3✔
UNCOV
102
                return err
×
UNCOV
103
        }
×
104

105
        // Set the corresponding TLV types if they were included in the stream.
106
        if _, ok := parsed[partialSig.TlvType()]; ok {
6✔
107
                c.PartialSig = tlv.SomeRecordT(partialSig)
3✔
108
        }
3✔
109

110
        c.CustomRecords = customRecords
3✔
111
        c.ExtraData = extraData
3✔
112

3✔
113
        return nil
3✔
114
}
115

116
// Encode serializes the target CommitSig into the passed io.Writer
117
// observing the protocol version specified.
118
//
119
// This is part of the lnwire.Message interface.
120
func (c *CommitSig) Encode(w *bytes.Buffer, pver uint32) error {
3✔
121
        recordProducers := make([]tlv.RecordProducer, 0, 1)
3✔
122
        c.PartialSig.WhenSome(func(sig PartialSigWithNonceTLV) {
6✔
123
                recordProducers = append(recordProducers, &sig)
3✔
124
        })
3✔
125

126
        extraData, err := MergeAndEncode(
3✔
127
                recordProducers, c.ExtraData, c.CustomRecords,
3✔
128
        )
3✔
129
        if err != nil {
3✔
UNCOV
130
                return err
×
UNCOV
131
        }
×
132

133
        if err := WriteChannelID(w, c.ChanID); err != nil {
3✔
134
                return err
×
135
        }
×
136

137
        if err := WriteSig(w, c.CommitSig); err != nil {
3✔
138
                return err
×
139
        }
×
140

141
        if err := WriteSigs(w, c.HtlcSigs); err != nil {
3✔
142
                return err
×
143
        }
×
144

145
        return WriteBytes(w, extraData)
3✔
146
}
147

148
// MsgType returns the integer uniquely identifying this message type on the
149
// wire.
150
//
151
// This is part of the lnwire.Message interface.
152
func (c *CommitSig) MsgType() MessageType {
3✔
153
        return MsgCommitSig
3✔
154
}
3✔
155

156
// TargetChanID returns the channel id of the link for which this message is
157
// intended.
158
//
159
// NOTE: Part of peer.LinkUpdater interface.
160
func (c *CommitSig) TargetChanID() ChannelID {
3✔
161
        return c.ChanID
3✔
162
}
3✔
163

164
// SerializedSize returns the serialized size of the message in bytes.
165
//
166
// This is part of the lnwire.SizeableMessage interface.
NEW
167
func (c *CommitSig) SerializedSize() (uint32, error) {
×
NEW
168
        return MessageSerializedSize(c)
×
NEW
169
}
×
170

171
// RandTestMessage populates the message with random data suitable for testing.
172
// It uses the rapid testing framework to generate random values.
173
//
174
// This is part of the TestMessage interface.
NEW
175
func (c *CommitSig) RandTestMessage(t *rapid.T) Message {
×
NEW
176
        cr, _ := RandCustomRecords(t, nil, true)
×
NEW
177
        sig := &CommitSig{
×
NEW
178
                ChanID:        RandChannelID(t),
×
NEW
179
                CommitSig:     RandSignature(t),
×
NEW
180
                CustomRecords: cr,
×
NEW
181
        }
×
NEW
182

×
NEW
183
        numHtlcSigs := rapid.IntRange(0, 20).Draw(t, "numHtlcSigs")
×
NEW
184
        htlcSigs := make([]Sig, numHtlcSigs)
×
NEW
185
        for i := 0; i < numHtlcSigs; i++ {
×
NEW
186
                htlcSigs[i] = RandSignature(t)
×
NEW
187
        }
×
188

NEW
189
        if len(htlcSigs) > 0 {
×
NEW
190
                sig.HtlcSigs = htlcSigs
×
NEW
191
        }
×
192

NEW
193
        includePartialSig := rapid.Bool().Draw(t, "includePartialSig")
×
NEW
194
        if includePartialSig {
×
NEW
195
                sigWithNonce := RandPartialSigWithNonce(t)
×
NEW
196
                sig.PartialSig = MaybePartialSigWithNonce(sigWithNonce)
×
NEW
197
        }
×
198

NEW
199
        return sig
×
200
}
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