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

lightningnetwork / lnd / 11357847688

16 Oct 2024 02:36AM UTC coverage: 57.864% (-0.9%) from 58.779%
11357847688

Pull #9148

github

ProofOfKeags
lnwire: change DynPropose/DynCommit TLV numbers to align with spec
Pull Request #9148: DynComms [2/n]: lnwire: add authenticated wire messages for Dyn*

350 of 644 new or added lines in 12 files covered. (54.35%)

19831 existing lines in 241 files now uncovered.

99337 of 171674 relevant lines covered (57.86%)

38595.9 hits per line

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

78.53
/lnwire/dyn_commit.go
1
package lnwire
2

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

7
        "github.com/btcsuite/btcd/btcutil"
8
        "github.com/lightningnetwork/lnd/fn"
9
        "github.com/lightningnetwork/lnd/tlv"
10
)
11

12
// DynAck is the message used to accept the parameters of a dynamic commitment
13
// negotiation. Additional optional parameters will need to be present depending
14
// on the details of the dynamic commitment upgrade.
15
type DynCommit struct {
16
        // ChanID is the ChannelID of the channel that is currently undergoing
17
        // a dynamic commitment negotiation
18
        ChanID ChannelID
19

20
        // Sig is a signature that acknowledges and approves the parameters
21
        // that were requested in the DynPropose
22
        Sig Sig
23

24
        // DustLimit, if not nil, proposes a change to the dust_limit_satoshis
25
        // for the sender's commitment transaction.
26
        DustLimit fn.Option[btcutil.Amount]
27

28
        // MaxValueInFlight, if not nil, proposes a change to the
29
        // max_htlc_value_in_flight_msat limit of the sender.
30
        MaxValueInFlight fn.Option[MilliSatoshi]
31

32
        // HtlcMinimum, if not nil, proposes a change to the htlc_minimum_msat
33
        // floor of the sender.
34
        HtlcMinimum fn.Option[MilliSatoshi]
35

36
        // ChannelReserve, if not nil, proposes a change to the
37
        // channel_reserve_satoshis requirement of the recipient.
38
        ChannelReserve fn.Option[btcutil.Amount]
39

40
        // CsvDelay, if not nil, proposes a change to the to_self_delay
41
        // requirement of the recipient.
42
        CsvDelay fn.Option[uint16]
43

44
        // MaxAcceptedHTLCs, if not nil, proposes a change to the
45
        // max_accepted_htlcs limit of the sender.
46
        MaxAcceptedHTLCs fn.Option[uint16]
47

48
        // ChannelType, if not nil, proposes a change to the channel_type
49
        // parameter.
50
        ChannelType fn.Option[ChannelType]
51

52
        // ExtraData is the set of data that was appended to this message to
53
        // fill out the full maximum transport message size. These fields can
54
        // be used to specify optional data such as custom TLV fields.
55
        ExtraData ExtraOpaqueData
56
}
57

58
// A compile time check to ensure DynAck implements the lnwire.Message
59
// interface.
60
var _ Message = (*DynCommit)(nil)
61

62
// Encode serializes the target DynAck into the passed io.Writer. Serialization
63
// will observe the rules defined by the passed protocol version.
64
//
65
// This is a part of the lnwire.Message interface.
66
func (dc *DynCommit) Encode(w *bytes.Buffer, _ uint32) error {
100✔
67
        if err := WriteChannelID(w, dc.ChanID); err != nil {
100✔
NEW
68
                return err
×
NEW
69
        }
×
70

71
        if err := WriteSig(w, dc.Sig); err != nil {
100✔
NEW
72
                return err
×
NEW
73
        }
×
74

75
        var tlvRecords []tlv.Record
100✔
76
        dc.DustLimit.WhenSome(func(dl btcutil.Amount) {
141✔
77
                protoSats := uint64(dl)
41✔
78
                tlvRecords = append(
41✔
79
                        tlvRecords, tlv.MakePrimitiveRecord(
41✔
80
                                DPDustLimitSatoshis, &protoSats,
41✔
81
                        ),
41✔
82
                )
41✔
83
        })
41✔
84
        dc.MaxValueInFlight.WhenSome(func(max MilliSatoshi) {
141✔
85
                protoSats := uint64(max)
41✔
86
                tlvRecords = append(
41✔
87
                        tlvRecords, tlv.MakePrimitiveRecord(
41✔
88
                                DPMaxHtlcValueInFlightMsat, &protoSats,
41✔
89
                        ),
41✔
90
                )
41✔
91
        })
41✔
92
        dc.HtlcMinimum.WhenSome(func(min MilliSatoshi) {
100✔
NEW
93
                protoSats := uint64(min)
×
NEW
94
                tlvRecords = append(
×
NEW
95
                        tlvRecords, tlv.MakePrimitiveRecord(
×
NEW
96
                                DPHtlcMinimumMsat, &protoSats,
×
NEW
97
                        ),
×
NEW
98
                )
×
NEW
99
        })
×
100
        dc.ChannelReserve.WhenSome(func(min btcutil.Amount) {
149✔
101
                channelReserve := uint64(min)
49✔
102
                tlvRecords = append(
49✔
103
                        tlvRecords, tlv.MakePrimitiveRecord(
49✔
104
                                DPChannelReserveSatoshis, &channelReserve,
49✔
105
                        ),
49✔
106
                )
49✔
107
        })
49✔
108
        dc.CsvDelay.WhenSome(func(wait uint16) {
147✔
109
                tlvRecords = append(
47✔
110
                        tlvRecords, tlv.MakePrimitiveRecord(
47✔
111
                                DPToSelfDelay, &wait,
47✔
112
                        ),
47✔
113
                )
47✔
114
        })
47✔
115
        dc.MaxAcceptedHTLCs.WhenSome(func(max uint16) {
151✔
116
                tlvRecords = append(
51✔
117
                        tlvRecords, tlv.MakePrimitiveRecord(
51✔
118
                                DPMaxAcceptedHtlcs, &max,
51✔
119
                        ),
51✔
120
                )
51✔
121
        })
51✔
122
        dc.ChannelType.WhenSome(func(ty ChannelType) {
151✔
123
                tlvRecords = append(
51✔
124
                        tlvRecords, tlv.MakeDynamicRecord(
51✔
125
                                DPChannelType, &ty,
51✔
126
                                ty.featureBitLen,
51✔
127
                                channelTypeEncoder, channelTypeDecoder,
51✔
128
                        ),
51✔
129
                )
51✔
130
        })
51✔
131
        tlv.SortRecords(tlvRecords)
100✔
132

100✔
133
        tlvStream, err := tlv.NewStream(tlvRecords...)
100✔
134
        if err != nil {
100✔
NEW
135
                return err
×
NEW
136
        }
×
137

138
        var extraBytesWriter bytes.Buffer
100✔
139
        if err := tlvStream.Encode(&extraBytesWriter); err != nil {
100✔
NEW
140
                return err
×
NEW
141
        }
×
142

143
        dc.ExtraData = ExtraOpaqueData(extraBytesWriter.Bytes())
100✔
144

100✔
145
        return WriteBytes(w, dc.ExtraData)
100✔
146
}
147

148
// Decode deserializes the serialized DynCommit stored in the passed io.Reader
149
// into the target DynAck using the deserialization rules defined by the passed
150
// protocol version.
151
//
152
// This is a part of the lnwire.Message interface.
153
func (dc *DynCommit) Decode(r io.Reader, _ uint32) error {
100✔
154
        // Parse out main message.
100✔
155
        if err := ReadElements(r, &dc.ChanID, &dc.Sig); err != nil {
100✔
NEW
156
                return err
×
NEW
157
        }
×
158

159
        // Parse out TLV records.
160
        var tlvRecords ExtraOpaqueData
100✔
161
        if err := ReadElement(r, &tlvRecords); err != nil {
100✔
NEW
162
                return err
×
NEW
163
        }
×
164

165
        // Prepare receiving buffers to be filled by TLV extraction.
166
        var dustLimitScratch uint64
100✔
167
        dustLimit := tlv.MakePrimitiveRecord(
100✔
168
                DPDustLimitSatoshis, &dustLimitScratch,
100✔
169
        )
100✔
170

100✔
171
        var maxValueScratch uint64
100✔
172
        maxValue := tlv.MakePrimitiveRecord(
100✔
173
                DPMaxHtlcValueInFlightMsat, &maxValueScratch,
100✔
174
        )
100✔
175

100✔
176
        var htlcMinScratch uint64
100✔
177
        htlcMin := tlv.MakePrimitiveRecord(
100✔
178
                DPHtlcMinimumMsat, &htlcMinScratch,
100✔
179
        )
100✔
180

100✔
181
        var reserveScratch uint64
100✔
182
        reserve := tlv.MakePrimitiveRecord(
100✔
183
                DPChannelReserveSatoshis, &reserveScratch,
100✔
184
        )
100✔
185

100✔
186
        var csvDelayScratch uint16
100✔
187
        csvDelay := tlv.MakePrimitiveRecord(DPToSelfDelay, &csvDelayScratch)
100✔
188

100✔
189
        var maxHtlcsScratch uint16
100✔
190
        maxHtlcs := tlv.MakePrimitiveRecord(
100✔
191
                DPMaxAcceptedHtlcs, &maxHtlcsScratch,
100✔
192
        )
100✔
193

100✔
194
        var chanTypeScratch ChannelType
100✔
195
        chanType := tlv.MakeDynamicRecord(
100✔
196
                DPChannelType, &chanTypeScratch, chanTypeScratch.featureBitLen,
100✔
197
                channelTypeEncoder, channelTypeDecoder,
100✔
198
        )
100✔
199

100✔
200
        // Create set of Records to read TLV bytestream into.
100✔
201
        records := []tlv.Record{
100✔
202
                dustLimit, maxValue, htlcMin, reserve, csvDelay, maxHtlcs,
100✔
203
                chanType,
100✔
204
        }
100✔
205
        tlv.SortRecords(records)
100✔
206

100✔
207
        // Read TLV stream into record set.
100✔
208
        extraBytesReader := bytes.NewReader(tlvRecords)
100✔
209
        tlvStream, err := tlv.NewStream(records...)
100✔
210
        if err != nil {
100✔
NEW
211
                return err
×
NEW
212
        }
×
213
        typeMap, err := tlvStream.DecodeWithParsedTypesP2P(extraBytesReader)
100✔
214
        if err != nil {
100✔
NEW
215
                return err
×
NEW
216
        }
×
217

218
        // Check the results of the TLV Stream decoding and appropriately set
219
        // message fields.
220
        if val, ok := typeMap[DPDustLimitSatoshis]; ok && val == nil {
141✔
221
                dc.DustLimit = fn.Some(btcutil.Amount(dustLimitScratch))
41✔
222
        }
41✔
223
        if val, ok := typeMap[DPMaxHtlcValueInFlightMsat]; ok && val == nil {
141✔
224
                dc.MaxValueInFlight = fn.Some(MilliSatoshi(maxValueScratch))
41✔
225
        }
41✔
226
        if val, ok := typeMap[DPHtlcMinimumMsat]; ok && val == nil {
100✔
NEW
227
                dc.HtlcMinimum = fn.Some(MilliSatoshi(htlcMinScratch))
×
NEW
228
        }
×
229
        if val, ok := typeMap[DPChannelReserveSatoshis]; ok && val == nil {
149✔
230
                dc.ChannelReserve = fn.Some(btcutil.Amount(reserveScratch))
49✔
231
        }
49✔
232
        if val, ok := typeMap[DPToSelfDelay]; ok && val == nil {
147✔
233
                dc.CsvDelay = fn.Some(csvDelayScratch)
47✔
234
        }
47✔
235
        if val, ok := typeMap[DPMaxAcceptedHtlcs]; ok && val == nil {
151✔
236
                dc.MaxAcceptedHTLCs = fn.Some(maxHtlcsScratch)
51✔
237
        }
51✔
238
        if val, ok := typeMap[DPChannelType]; ok && val == nil {
151✔
239
                dc.ChannelType = fn.Some(chanTypeScratch)
51✔
240
        }
51✔
241

242
        if len(tlvRecords) != 0 {
197✔
243
                dc.ExtraData = tlvRecords
97✔
244
        }
97✔
245

246
        return nil
100✔
247
}
248

249
// MsgType returns the MessageType code which uniquely identifies this message
250
// as a DynCommit on the wire.
251
//
252
// This is part of the lnwire.Message interface.
253
func (dc *DynCommit) MsgType() MessageType {
100✔
254
        return MsgDynCommit
100✔
255
}
100✔
256

257
// NegotiateDynCommit constructs a DynCommit message from the prior DynPropose
258
// and DynAck messages exchanged during the negotiation.
NEW
259
func NegotiateDynCommit(propose DynPropose, ack DynAck) DynCommit {
×
NEW
260
        return DynCommit{
×
NEW
261
                ChanID:           propose.ChanID,
×
NEW
262
                Sig:              ack.Sig,
×
NEW
263
                DustLimit:        propose.DustLimit,
×
NEW
264
                MaxValueInFlight: propose.MaxValueInFlight,
×
NEW
265
                HtlcMinimum:      propose.HtlcMinimum,
×
NEW
266
                ChannelReserve:   propose.ChannelReserve,
×
NEW
267
                CsvDelay:         propose.CsvDelay,
×
NEW
268
                MaxAcceptedHTLCs: propose.MaxAcceptedHTLCs,
×
NEW
269
                ChannelType:      propose.ChannelType,
×
NEW
270
        }
×
NEW
271
}
×
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