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

lightningnetwork / lnd / 11375974525

17 Oct 2024 12:21AM UTC coverage: 57.867% (-0.9%) from 58.78%
11375974525

Pull #9148

github

ProofOfKeags
lnwire: convert DynPropose and DynCommit to use typed tlv records
Pull Request #9148: DynComms [2/n]: lnwire: add authenticated wire messages for Dyn*

336 of 477 new or added lines in 12 files covered. (70.44%)

18915 existing lines in 242 files now uncovered.

99181 of 171395 relevant lines covered (57.87%)

37360.97 hits per line

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

80.65
/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
const (
12
        // DPDustLimitSatoshis is the TLV type number that identifies the record
13
        // for DynPropose.DustLimit.
14
        DPDustLimitSatoshis tlv.Type = 0
15

16
        // DPMaxHtlcValueInFlightMsat is the TLV type number that identifies the
17
        // record for DynPropose.MaxValueInFlight.
18
        DPMaxHtlcValueInFlightMsat tlv.Type = 2
19

20
        // DPHtlcMinimumMsat is the TLV type number that identifies the record
21
        // for DynPropose.HtlcMinimum.
22
        DPHtlcMinimumMsat tlv.Type = 4
23

24
        // DPChannelReserveSatoshis is the TLV type number that identifies the
25
        // for DynPropose.ChannelReserve.
26
        DPChannelReserveSatoshis tlv.Type = 6
27

28
        // DPToSelfDelay is the TLV type number that identifies the record for
29
        // DynPropose.CsvDelay.
30
        DPToSelfDelay tlv.Type = 8
31

32
        // DPMaxAcceptedHtlcs is the TLV type number that identifies the record
33
        // for DynPropose.MaxAcceptedHTLCs.
34
        DPMaxAcceptedHtlcs tlv.Type = 10
35

36
        // DPChannelType is the TLV type number that identifies the record for
37
        // DynPropose.ChannelType.
38
        DPChannelType tlv.Type = 12
39
)
40

41
// DynPropose is a message that is sent during a dynamic commitments negotiation
42
// process. It is sent by both parties to propose new channel parameters.
43
type DynPropose struct {
44
        // ChanID identifies the channel whose parameters we are trying to
45
        // re-negotiate.
46
        ChanID ChannelID
47

48
        // DustLimit, if not nil, proposes a change to the dust_limit_satoshis
49
        // for the sender's commitment transaction.
50
        DustLimit tlv.OptionalRecordT[tlv.TlvType0, btcutil.Amount]
51

52
        // MaxValueInFlight, if not nil, proposes a change to the
53
        // max_htlc_value_in_flight_msat limit of the sender.
54
        MaxValueInFlight tlv.OptionalRecordT[tlv.TlvType2, MilliSatoshi]
55

56
        // HtlcMinimum, if not nil, proposes a change to the htlc_minimum_msat
57
        // floor of the sender.
58
        HtlcMinimum tlv.OptionalRecordT[tlv.TlvType4, MilliSatoshi]
59

60
        // ChannelReserve, if not nil, proposes a change to the
61
        // channel_reserve_satoshis requirement of the recipient.
62
        ChannelReserve tlv.OptionalRecordT[tlv.TlvType6, btcutil.Amount]
63

64
        // CsvDelay, if not nil, proposes a change to the to_self_delay
65
        // requirement of the recipient.
66
        CsvDelay tlv.OptionalRecordT[tlv.TlvType8, uint16]
67

68
        // MaxAcceptedHTLCs, if not nil, proposes a change to the
69
        // max_accepted_htlcs limit of the sender.
70
        MaxAcceptedHTLCs tlv.OptionalRecordT[tlv.TlvType10, uint16]
71

72
        // ChannelType, if not nil, proposes a change to the channel_type
73
        // parameter.
74
        ChannelType tlv.OptionalRecordT[tlv.TlvType12, ChannelType]
75

76
        // ExtraData is the set of data that was appended to this message to
77
        // fill out the full maximum transport message size. These fields can
78
        // be used to specify optional data such as custom TLV fields.
79
        //
80
        // NOTE: Since the fields in this structure are part of the TLV stream,
81
        // ExtraData will contain all TLV records _except_ the ones that are
82
        // present in earlier parts of this structure.
83
        ExtraData ExtraOpaqueData
84
}
85

86
// A compile time check to ensure DynPropose implements the lnwire.Message
87
// interface.
88
var _ Message = (*DynPropose)(nil)
89

90
// Encode serializes the target DynPropose into the passed io.Writer.
91
// Serialization will observe the rules defined by the passed protocol version.
92
//
93
// This is a part of the lnwire.Message interface.
94
func (dp *DynPropose) Encode(w *bytes.Buffer, _ uint32) error {
103✔
95
        if err := WriteChannelID(w, dp.ChanID); err != nil {
103✔
UNCOV
96
                return err
×
97
        }
×
98

99
        producers := dynProposeRecords(dp)
103✔
100

103✔
101
        err := EncodeMessageExtraData(&dp.ExtraData, producers...)
103✔
102
        if err != nil {
103✔
103
                return err
×
104
        }
×
105

106
        return WriteBytes(w, dp.ExtraData)
103✔
107
}
108

109
// Decode deserializes the serialized DynPropose stored in the passed io.Reader
110
// into the target DynPropose using the deserialization rules defined by the
111
// passed protocol version.
112
//
113
// This is a part of the lnwire.Message interface.
114
func (dp *DynPropose) Decode(r io.Reader, _ uint32) error {
208✔
115
        // Parse out the only required field.
208✔
116
        if err := ReadElements(r, &dp.ChanID); err != nil {
210✔
117
                return err
2✔
118
        }
2✔
119

120
        // Parse out TLV stream.
121
        var tlvRecords ExtraOpaqueData
206✔
122
        if err := ReadElements(r, &tlvRecords); err != nil {
206✔
123
                return err
×
124
        }
×
125

126
        // Prepare receiving buffers to be filled by TLV extraction.
127
        var dustLimit tlv.RecordT[tlv.TlvType0, uint64]
206✔
128
        var maxValue tlv.RecordT[tlv.TlvType2, uint64]
206✔
129
        var htlcMin tlv.RecordT[tlv.TlvType4, uint64]
206✔
130
        var reserve tlv.RecordT[tlv.TlvType6, uint64]
206✔
131
        csvDelay := dp.CsvDelay.Zero()
206✔
132
        maxHtlcs := dp.MaxAcceptedHTLCs.Zero()
206✔
133
        chanType := dp.ChannelType.Zero()
206✔
134

206✔
135
        typeMap, err := tlvRecords.ExtractRecords(
206✔
136
                &dustLimit, &maxValue, &htlcMin, &reserve, &csvDelay, &maxHtlcs,
206✔
137
                &chanType,
206✔
138
        )
206✔
139
        if err != nil {
306✔
140
                return err
100✔
141
        }
100✔
142

143
        // Check the results of the TLV Stream decoding and appropriately set
144
        // message fields.
145
        if val, ok := typeMap[dp.DustLimit.TlvType()]; ok && val == nil {
156✔
146
                var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount]
50✔
147
                rec.Val = btcutil.Amount(dustLimit.Val)
50✔
148
                dp.DustLimit = tlv.SomeRecordT(rec)
50✔
149
        }
50✔
150
        if val, ok := typeMap[dp.MaxValueInFlight.TlvType()]; ok && val == nil {
162✔
151
                var rec tlv.RecordT[tlv.TlvType2, MilliSatoshi]
56✔
152
                rec.Val = MilliSatoshi(maxValue.Val)
56✔
153
                dp.MaxValueInFlight = tlv.SomeRecordT(rec)
56✔
154
        }
56✔
155
        if val, ok := typeMap[dp.HtlcMinimum.TlvType()]; ok && val == nil {
106✔
NEW
156
                var rec tlv.RecordT[tlv.TlvType4, MilliSatoshi]
×
NEW
157
                rec.Val = MilliSatoshi(htlcMin.Val)
×
NEW
158
                dp.HtlcMinimum = tlv.SomeRecordT(rec)
×
UNCOV
159
        }
×
160
        if val, ok := typeMap[dp.ChannelReserve.TlvType()]; ok && val == nil {
159✔
161
                var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount]
53✔
162
                rec.Val = btcutil.Amount(reserve.Val)
53✔
163
                dp.ChannelReserve = tlv.SomeRecordT(rec)
53✔
164
        }
53✔
165
        if val, ok := typeMap[dp.CsvDelay.TlvType()]; ok && val == nil {
158✔
166
                dp.CsvDelay = tlv.SomeRecordT(csvDelay)
52✔
167
        }
52✔
168
        if val, ok := typeMap[dp.MaxAcceptedHTLCs.TlvType()]; ok && val == nil {
162✔
169
                dp.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
56✔
170
        }
56✔
171
        if val, ok := typeMap[dp.ChannelType.TlvType()]; ok && val == nil {
159✔
172
                dp.ChannelType = tlv.SomeRecordT(chanType)
53✔
173
        }
53✔
174

175
        if len(tlvRecords) != 0 {
205✔
176
                dp.ExtraData = tlvRecords
99✔
177
        }
99✔
178

179
        return nil
106✔
180
}
181

182
// MsgType returns the MessageType code which uniquely identifies this message
183
// as a DynPropose on the wire.
184
//
185
// This is part of the lnwire.Message interface.
186
func (dp *DynPropose) MsgType() MessageType {
103✔
187
        return MsgDynPropose
103✔
188
}
103✔
189

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

×
NEW
196
        var extra ExtraOpaqueData
×
NEW
197
        err := extra.PackRecords(producers...)
×
NEW
198
        if err != nil {
×
NEW
199
                return nil, err
×
NEW
200
        }
×
201

NEW
202
        return extra, nil
×
203
}
204

205
func dynProposeRecords(dp *DynPropose) []tlv.RecordProducer {
203✔
206
        recordProducers := make([]tlv.RecordProducer, 0, 7)
203✔
207

203✔
208
        dp.DustLimit.WhenSome(
203✔
209
                func(dl tlv.RecordT[tlv.TlvType0, btcutil.Amount]) {
304✔
210
                        rec := tlv.NewPrimitiveRecord[tlv.TlvType0](
101✔
211
                                uint64(dl.Val),
101✔
212
                        )
101✔
213
                        recordProducers = append(recordProducers, &rec)
101✔
214
                },
101✔
215
        )
216
        dp.MaxValueInFlight.WhenSome(
203✔
217
                func(max tlv.RecordT[tlv.TlvType2, MilliSatoshi]) {
312✔
218
                        rec := tlv.NewPrimitiveRecord[tlv.TlvType2](
109✔
219
                                uint64(max.Val),
109✔
220
                        )
109✔
221
                        recordProducers = append(recordProducers, &rec)
109✔
222
                },
109✔
223
        )
224
        dp.HtlcMinimum.WhenSome(
203✔
225
                func(min tlv.RecordT[tlv.TlvType4, MilliSatoshi]) {
203✔
NEW
226
                        rec := tlv.NewPrimitiveRecord[tlv.TlvType4](
×
NEW
227
                                uint64(min.Val),
×
NEW
228
                        )
×
NEW
229
                        recordProducers = append(recordProducers, &rec)
×
NEW
230
                },
×
231
        )
232
        dp.ChannelReserve.WhenSome(
203✔
233
                func(reserve tlv.RecordT[tlv.TlvType6, btcutil.Amount]) {
306✔
234
                        rec := tlv.NewPrimitiveRecord[tlv.TlvType6](
103✔
235
                                uint64(reserve.Val),
103✔
236
                        )
103✔
237
                        recordProducers = append(recordProducers, &rec)
103✔
238
                },
103✔
239
        )
240
        dp.CsvDelay.WhenSome(
203✔
241
                func(wait tlv.RecordT[tlv.TlvType8, uint16]) {
310✔
242
                        recordProducers = append(recordProducers, &wait)
107✔
243
                },
107✔
244
        )
245
        dp.MaxAcceptedHTLCs.WhenSome(
203✔
246
                func(max tlv.RecordT[tlv.TlvType10, uint16]) {
316✔
247
                        recordProducers = append(recordProducers, &max)
113✔
248
                },
113✔
249
        )
250
        dp.ChannelType.WhenSome(
203✔
251
                func(ty tlv.RecordT[tlv.TlvType12, ChannelType]) {
311✔
252
                        recordProducers = append(recordProducers, &ty)
108✔
253
                },
108✔
254
        )
255

256
        return recordProducers
203✔
257
}
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