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

lightningnetwork / lnd / 16025893053

02 Jul 2025 01:03PM UTC coverage: 57.783% (-0.02%) from 57.803%
16025893053

Pull #10027

github

web-flow
Merge c74b17c9e into 1d2e5472b
Pull Request #10027: Fix `ExtraData` field and use `BigSize` encodine

0 of 155 new or added lines in 5 files covered. (0.0%)

49 existing lines in 10 files now uncovered.

98479 of 170428 relevant lines covered (57.78%)

1.79 hits per line

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

0.0
/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 {
×
73
        if err := WriteChannelID(w, dp.ChanID); err != nil {
×
74
                return err
×
75
        }
×
76

77
        producers := dynProposeRecords(dp)
×
78

×
NEW
79
        // Encode all known records.
×
NEW
80
        var tlvData ExtraOpaqueData
×
NEW
81
        err := tlvData.PackRecords(producers...)
×
82
        if err != nil {
×
83
                return err
×
84
        }
×
85

86
        // Write the known records.
NEW
87
        if err := WriteBytes(w, tlvData); err != nil {
×
NEW
88
                return err
×
NEW
89
        }
×
90

91
        // Encode ExtraData.
UNCOV
92
        return WriteBytes(w, dp.ExtraData)
×
93
}
94

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

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

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

×
NEW
121
        knownRecords, extraData, err := ParseAndExtractExtraData(
×
NEW
122
                tlvRecords, &dustLimit, &maxValue, &htlcMin, &reserve,
×
NEW
123
                &csvDelay, &maxHtlcs, &chanType,
×
124
        )
×
125
        if err != nil {
×
126
                return err
×
127
        }
×
128

129
        // Check the results of the TLV Stream decoding and appropriately set
130
        // message fields.
NEW
131
        if _, ok := knownRecords[dp.DustLimit.TlvType()]; ok {
×
NEW
132
                var rec tlv.RecordT[tlv.TlvType0, tlv.BigSizeT[btcutil.Amount]]
×
NEW
133
                rec.Val = tlv.NewBigSizeT(btcutil.Amount(dustLimit.Val.Int()))
×
134
                dp.DustLimit = tlv.SomeRecordT(rec)
×
135
        }
×
136

NEW
137
        if _, ok := knownRecords[dp.MaxValueInFlight.TlvType()]; ok {
×
138
                var rec tlv.RecordT[tlv.TlvType2, MilliSatoshi]
×
139
                rec.Val = MilliSatoshi(maxValue.Val)
×
140
                dp.MaxValueInFlight = tlv.SomeRecordT(rec)
×
141
        }
×
142

NEW
143
        if _, ok := knownRecords[dp.HtlcMinimum.TlvType()]; ok {
×
144
                var rec tlv.RecordT[tlv.TlvType4, MilliSatoshi]
×
145
                rec.Val = MilliSatoshi(htlcMin.Val)
×
146
                dp.HtlcMinimum = tlv.SomeRecordT(rec)
×
147
        }
×
148

NEW
149
        if _, ok := knownRecords[dp.ChannelReserve.TlvType()]; ok {
×
NEW
150
                var rec tlv.RecordT[tlv.TlvType6, tlv.BigSizeT[btcutil.Amount]]
×
NEW
151
                rec.Val = tlv.NewBigSizeT(btcutil.Amount(reserve.Val.Int()))
×
152
                dp.ChannelReserve = tlv.SomeRecordT(rec)
×
153
        }
×
154

NEW
155
        if _, ok := knownRecords[dp.CsvDelay.TlvType()]; ok {
×
156
                dp.CsvDelay = tlv.SomeRecordT(csvDelay)
×
157
        }
×
158

NEW
159
        if _, ok := knownRecords[dp.MaxAcceptedHTLCs.TlvType()]; ok {
×
160
                dp.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
×
161
        }
×
162

NEW
163
        if _, ok := knownRecords[dp.ChannelType.TlvType()]; ok {
×
164
                dp.ChannelType = tlv.SomeRecordT(chanType)
×
165
        }
×
166

NEW
167
        dp.ExtraData = extraData
×
168

×
169
        return nil
×
170
}
171

172
// MsgType returns the MessageType code which uniquely identifies this message
173
// as a DynPropose on the wire.
174
//
175
// This is part of the lnwire.Message interface.
176
func (dp *DynPropose) MsgType() MessageType {
×
177
        return MsgDynPropose
×
178
}
×
179

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

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

×
193
        var extra ExtraOpaqueData
×
194
        err := extra.PackRecords(producers...)
×
195
        if err != nil {
×
196
                return nil, err
×
197
        }
×
198

199
        return extra, nil
×
200
}
201

202
func dynProposeRecords(dp *DynPropose) []tlv.RecordProducer {
×
203
        recordProducers := make([]tlv.RecordProducer, 0, 7)
×
204

×
205
        dp.DustLimit.WhenSome(
×
NEW
206
                func(dl tlv.RecordT[tlv.TlvType0,
×
NEW
207
                        tlv.BigSizeT[btcutil.Amount]]) {
×
NEW
208

×
NEW
209
                        recordProducers = append(recordProducers, &dl)
×
UNCOV
210
                },
×
211
        )
212
        dp.MaxValueInFlight.WhenSome(
×
213
                func(mvif tlv.RecordT[tlv.TlvType2, MilliSatoshi]) {
×
NEW
214
                        recordProducers = append(recordProducers, &mvif)
×
215
                },
×
216
        )
217
        dp.HtlcMinimum.WhenSome(
×
218
                func(hm tlv.RecordT[tlv.TlvType4, MilliSatoshi]) {
×
NEW
219
                        recordProducers = append(recordProducers, &hm)
×
220
                },
×
221
        )
222
        dp.ChannelReserve.WhenSome(
×
NEW
223
                func(reserve tlv.RecordT[tlv.TlvType6,
×
NEW
224
                        tlv.BigSizeT[btcutil.Amount]]) {
×
NEW
225

×
NEW
226
                        recordProducers = append(recordProducers, &reserve)
×
UNCOV
227
                },
×
228
        )
229
        dp.CsvDelay.WhenSome(
×
230
                func(wait tlv.RecordT[tlv.TlvType8, uint16]) {
×
231
                        recordProducers = append(recordProducers, &wait)
×
232
                },
×
233
        )
234
        dp.MaxAcceptedHTLCs.WhenSome(
×
235
                func(mah tlv.RecordT[tlv.TlvType10, uint16]) {
×
236
                        recordProducers = append(recordProducers, &mah)
×
237
                },
×
238
        )
239
        dp.ChannelType.WhenSome(
×
240
                func(ty tlv.RecordT[tlv.TlvType12, ChannelType]) {
×
241
                        recordProducers = append(recordProducers, &ty)
×
242
                },
×
243
        )
244

245
        return recordProducers
×
246
}
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