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

lightningnetwork / lnd / 16028568263

02 Jul 2025 02:55PM UTC coverage: 67.579% (+0.04%) from 67.54%
16028568263

Pull #10027

github

web-flow
Merge c4f2c13b4 into b5c84eab1
Pull Request #10027: Fix `ExtraData` field and use `BigSize` encodine

137 of 143 new or added lines in 5 files covered. (95.8%)

56 existing lines in 17 files now uncovered.

135172 of 200021 relevant lines covered (67.58%)

21907.18 hits per line

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

82.61
/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
        producers := dynProposeRecords(dp)
104✔
78

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

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

91
        // Encode ExtraData.
92
        return WriteBytes(w, dp.ExtraData)
104✔
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 {
209✔
101
        // Parse out the only required field.
209✔
102
        if err := ReadElements(r, &dp.ChanID); err != nil {
211✔
103
                return err
2✔
104
        }
2✔
105

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

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

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

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

135
        if _, ok := knownRecords[dp.MaxValueInFlight.TlvType()]; ok {
159✔
136
                dp.MaxValueInFlight = tlv.SomeRecordT(maxValue)
52✔
137
        }
52✔
138

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

143
        if _, ok := knownRecords[dp.ChannelReserve.TlvType()]; ok {
159✔
144
                dp.ChannelReserve = tlv.SomeRecordT(reserve)
52✔
145
        }
52✔
146

147
        if _, ok := knownRecords[dp.CsvDelay.TlvType()]; ok {
166✔
148
                dp.CsvDelay = tlv.SomeRecordT(csvDelay)
59✔
149
        }
59✔
150

151
        if _, ok := knownRecords[dp.MaxAcceptedHTLCs.TlvType()]; ok {
161✔
152
                dp.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
54✔
153
        }
54✔
154

155
        if _, ok := knownRecords[dp.ChannelType.TlvType()]; ok {
162✔
156
                dp.ChannelType = tlv.SomeRecordT(chanType)
55✔
157
        }
55✔
158

159
        dp.ExtraData = extraData
107✔
160

107✔
161
        return nil
107✔
162
}
163

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

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

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

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

191
        return extra, nil
×
192
}
193

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

205✔
197
        dp.DustLimit.WhenSome(
205✔
198
                func(dl tlv.RecordT[tlv.TlvType0,
205✔
199
                        tlv.BigSizeT[btcutil.Amount]]) {
305✔
200

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

103✔
218
                        recordProducers = append(recordProducers, &reserve)
103✔
219
                },
103✔
220
        )
221
        dp.CsvDelay.WhenSome(
205✔
222
                func(wait tlv.RecordT[tlv.TlvType8, uint16]) {
318✔
223
                        recordProducers = append(recordProducers, &wait)
113✔
224
                },
113✔
225
        )
226
        dp.MaxAcceptedHTLCs.WhenSome(
205✔
227
                func(mah tlv.RecordT[tlv.TlvType10, uint16]) {
311✔
228
                        recordProducers = append(recordProducers, &mah)
106✔
229
                },
106✔
230
        )
231
        dp.ChannelType.WhenSome(
205✔
232
                func(ty tlv.RecordT[tlv.TlvType12, ChannelType]) {
305✔
233
                        recordProducers = append(recordProducers, &ty)
100✔
234
                },
100✔
235
        )
236

237
        return recordProducers
205✔
238
}
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