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

lightningnetwork / lnd / 16177133893

09 Jul 2025 06:21PM UTC coverage: 67.465% (+9.9%) from 57.611%
16177133893

Pull #10027

github

web-flow
Merge e170af2f3 into 0e830da9d
Pull Request #10027: Fix `ExtraData` field and use `BigSize` encodine

153 of 159 new or added lines in 5 files covered. (96.23%)

38 existing lines in 7 files now uncovered.

135346 of 200618 relevant lines covered (67.46%)

21844.4 hits per line

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

82.76
/lnwire/dyn_propose.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
// DynPropose is a message that is sent during a dynamic commitments negotiation
12
// process. It is sent by both parties to propose new channel parameters.
13
type DynPropose struct {
14
        // ChanID identifies the channel whose parameters we are trying to
15
        // re-negotiate.
16
        ChanID ChannelID
17

18
        // DustLimit, if not nil, proposes a change to the dust_limit_satoshis
19
        // for the sender's commitment transaction.
20
        DustLimit tlv.OptionalRecordT[
21
                tlv.TlvType0, tlv.BigSizeT[btcutil.Amount],
22
        ]
23

24
        // MaxValueInFlight, if not nil, proposes a change to the
25
        // max_htlc_value_in_flight_msat limit of the sender.
26
        MaxValueInFlight tlv.OptionalRecordT[tlv.TlvType2, MilliSatoshi]
27

28
        // HtlcMinimum, if not nil, proposes a change to the htlc_minimum_msat
29
        // floor of the sender.
30
        HtlcMinimum tlv.OptionalRecordT[tlv.TlvType4, MilliSatoshi]
31

32
        // ChannelReserve, if not nil, proposes a change to the
33
        // channel_reserve_satoshis requirement of the recipient.
34
        ChannelReserve tlv.OptionalRecordT[
35
                tlv.TlvType6, tlv.BigSizeT[btcutil.Amount],
36
        ]
37

38
        // CsvDelay, if not nil, proposes a change to the to_self_delay
39
        // requirement of the recipient.
40
        CsvDelay tlv.OptionalRecordT[tlv.TlvType8, uint16]
41

42
        // MaxAcceptedHTLCs, if not nil, proposes a change to the
43
        // max_accepted_htlcs limit of the sender.
44
        MaxAcceptedHTLCs tlv.OptionalRecordT[tlv.TlvType10, uint16]
45

46
        // ChannelType, if not nil, proposes a change to the channel_type
47
        // parameter.
48
        ChannelType tlv.OptionalRecordT[tlv.TlvType12, ChannelType]
49

50
        // ExtraData is the set of data that was appended to this message to
51
        // fill out the full maximum transport message size. These fields can
52
        // be used to specify optional data such as custom TLV fields.
53
        //
54
        // NOTE: Since the fields in this structure are part of the TLV stream,
55
        // ExtraData will contain all TLV records _except_ the ones that are
56
        // present in earlier parts of this structure.
57
        ExtraData ExtraOpaqueData
58
}
59

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

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

68
// Encode serializes the target DynPropose into the passed io.Writer.
69
// Serialization will observe the rules defined by the passed protocol version.
70
//
71
// This is a part of the lnwire.Message interface.
72
func (dp *DynPropose) Encode(w *bytes.Buffer, _ uint32) error {
104✔
73
        if err := WriteChannelID(w, dp.ChanID); err != nil {
104✔
74
                return err
×
75
        }
×
76

77
        // Create extra data records.
78
        producers, err := dp.ExtraData.RecordProducers()
104✔
79
        if err != nil {
104✔
NEW
80
                return err
×
NEW
81
        }
×
82

83
        // Append the known records.
84
        producers = append(producers, dynProposeRecords(dp)...)
104✔
85

104✔
86
        // Encode all records.
104✔
87
        var tlvData ExtraOpaqueData
104✔
88
        err = tlvData.PackRecords(producers...)
104✔
89
        if err != nil {
104✔
90
                return err
×
91
        }
×
92

93
        // Write the records.
94
        return WriteBytes(w, tlvData)
104✔
95
}
96

97
// Decode deserializes the serialized DynPropose stored in the passed io.Reader
98
// into the target DynPropose using the deserialization rules defined by the
99
// passed protocol version.
100
//
101
// This is a part of the lnwire.Message interface.
102
func (dp *DynPropose) Decode(r io.Reader, _ uint32) error {
209✔
103
        // Parse out the only required field.
209✔
104
        if err := ReadElements(r, &dp.ChanID); err != nil {
211✔
105
                return err
2✔
106
        }
2✔
107

108
        // Parse out TLV stream.
109
        var tlvRecords ExtraOpaqueData
207✔
110
        if err := ReadElements(r, &tlvRecords); err != nil {
207✔
111
                return err
×
112
        }
×
113

114
        // Prepare receiving buffers to be filled by TLV extraction.
115
        var dustLimit tlv.RecordT[tlv.TlvType0, tlv.BigSizeT[btcutil.Amount]]
207✔
116
        var maxValue tlv.RecordT[tlv.TlvType2, MilliSatoshi]
207✔
117
        var htlcMin tlv.RecordT[tlv.TlvType4, MilliSatoshi]
207✔
118
        var reserve tlv.RecordT[tlv.TlvType6, tlv.BigSizeT[btcutil.Amount]]
207✔
119
        csvDelay := dp.CsvDelay.Zero()
207✔
120
        maxHtlcs := dp.MaxAcceptedHTLCs.Zero()
207✔
121
        chanType := dp.ChannelType.Zero()
207✔
122

207✔
123
        knownRecords, extraData, err := ParseAndExtractExtraData(
207✔
124
                tlvRecords, &dustLimit, &maxValue, &htlcMin, &reserve,
207✔
125
                &csvDelay, &maxHtlcs, &chanType,
207✔
126
        )
207✔
127
        if err != nil {
307✔
128
                return err
100✔
129
        }
100✔
130

131
        // Check the results of the TLV Stream decoding and appropriately set
132
        // message fields.
133
        if _, ok := knownRecords[dp.DustLimit.TlvType()]; ok {
165✔
134
                dp.DustLimit = tlv.SomeRecordT(dustLimit)
58✔
135
        }
58✔
136

137
        if _, ok := knownRecords[dp.MaxValueInFlight.TlvType()]; ok {
155✔
138
                dp.MaxValueInFlight = tlv.SomeRecordT(maxValue)
48✔
139
        }
48✔
140

141
        if _, ok := knownRecords[dp.HtlcMinimum.TlvType()]; ok {
108✔
142
                dp.HtlcMinimum = tlv.SomeRecordT(htlcMin)
1✔
143
        }
1✔
144

145
        if _, ok := knownRecords[dp.ChannelReserve.TlvType()]; ok {
150✔
146
                dp.ChannelReserve = tlv.SomeRecordT(reserve)
43✔
147
        }
43✔
148

149
        if _, ok := knownRecords[dp.CsvDelay.TlvType()]; ok {
155✔
150
                dp.CsvDelay = tlv.SomeRecordT(csvDelay)
48✔
151
        }
48✔
152

153
        if _, ok := knownRecords[dp.MaxAcceptedHTLCs.TlvType()]; ok {
155✔
154
                dp.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
48✔
155
        }
48✔
156

157
        if _, ok := knownRecords[dp.ChannelType.TlvType()]; ok {
157✔
158
                dp.ChannelType = tlv.SomeRecordT(chanType)
50✔
159
        }
50✔
160

161
        dp.ExtraData = extraData
107✔
162

107✔
163
        return nil
107✔
164
}
165

166
// MsgType returns the MessageType code which uniquely identifies this message
167
// as a DynPropose on the wire.
168
//
169
// This is part of the lnwire.Message interface.
170
func (dp *DynPropose) MsgType() MessageType {
103✔
171
        return MsgDynPropose
103✔
172
}
103✔
173

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

181
// SerializeTlvData takes just the TLV data of DynPropose (which covers all of
182
// the parameters on deck for changing) and serializes just this component. The
183
// main purpose of this is to make it easier to validate the DynAck signature.
184
func (dp *DynPropose) SerializeTlvData() ([]byte, error) {
×
185
        producers := dynProposeRecords(dp)
×
186

×
187
        var extra ExtraOpaqueData
×
188
        err := extra.PackRecords(producers...)
×
189
        if err != nil {
×
190
                return nil, err
×
191
        }
×
192

193
        return extra, nil
×
194
}
195

196
func dynProposeRecords(dp *DynPropose) []tlv.RecordProducer {
205✔
197
        recordProducers := make([]tlv.RecordProducer, 0, 7)
205✔
198

205✔
199
        dp.DustLimit.WhenSome(
205✔
200
                func(dl tlv.RecordT[tlv.TlvType0,
205✔
201
                        tlv.BigSizeT[btcutil.Amount]]) {
311✔
202

106✔
203
                        recordProducers = append(recordProducers, &dl)
106✔
204
                },
106✔
205
        )
206
        dp.MaxValueInFlight.WhenSome(
205✔
207
                func(mvif tlv.RecordT[tlv.TlvType2, MilliSatoshi]) {
297✔
208
                        recordProducers = append(recordProducers, &mvif)
92✔
209
                },
92✔
210
        )
211
        dp.HtlcMinimum.WhenSome(
205✔
212
                func(hm tlv.RecordT[tlv.TlvType4, MilliSatoshi]) {
207✔
213
                        recordProducers = append(recordProducers, &hm)
2✔
214
                },
2✔
215
        )
216
        dp.ChannelReserve.WhenSome(
205✔
217
                func(reserve tlv.RecordT[tlv.TlvType6,
205✔
218
                        tlv.BigSizeT[btcutil.Amount]]) {
295✔
219

90✔
220
                        recordProducers = append(recordProducers, &reserve)
90✔
221
                },
90✔
222
        )
223
        dp.CsvDelay.WhenSome(
205✔
224
                func(wait tlv.RecordT[tlv.TlvType8, uint16]) {
297✔
225
                        recordProducers = append(recordProducers, &wait)
92✔
226
                },
92✔
227
        )
228
        dp.MaxAcceptedHTLCs.WhenSome(
205✔
229
                func(mah tlv.RecordT[tlv.TlvType10, uint16]) {
304✔
230
                        recordProducers = append(recordProducers, &mah)
99✔
231
                },
99✔
232
        )
233
        dp.ChannelType.WhenSome(
205✔
234
                func(ty tlv.RecordT[tlv.TlvType12, ChannelType]) {
302✔
235
                        recordProducers = append(recordProducers, &ty)
97✔
236
                },
97✔
237
        )
238

239
        return recordProducers
205✔
240
}
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