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

lightningnetwork / lnd / 11216766535

07 Oct 2024 01:37PM UTC coverage: 57.817% (-1.0%) from 58.817%
11216766535

Pull #9148

github

ProofOfKeags
lnwire: remove kickoff feerate from propose/commit
Pull Request #9148: DynComms [2/n]: lnwire: add authenticated wire messages for Dyn*

571 of 879 new or added lines in 16 files covered. (64.96%)

23253 existing lines in 251 files now uncovered.

99022 of 171268 relevant lines covered (57.82%)

38420.67 hits per line

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

83.62
/lnwire/dyn_commit.go
1
package lnwire
2

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

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

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

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

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

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

33
        // ChannelReserve, if not nil, proposes a change to the
34
        // channel_reserve_satoshis requirement of the recipient.
35
        ChannelReserve fn.Option[btcutil.Amount]
36

37
        // CsvDelay, if not nil, proposes a change to the to_self_delay
38
        // requirement of the recipient.
39
        CsvDelay fn.Option[uint16]
40

41
        // MaxAcceptedHTLCs, if not nil, proposes a change to the
42
        // max_accepted_htlcs limit of the sender.
43
        MaxAcceptedHTLCs fn.Option[uint16]
44

45
        // FundingKey, if not nil, proposes a change to the funding_pubkey
46
        // parameter of the sender.
47
        FundingKey fn.Option[btcec.PublicKey]
48

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

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

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

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

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

76
        var tlvRecords []tlv.Record
100✔
77
        dc.DustLimit.WhenSome(func(dl btcutil.Amount) {
145✔
78
                protoSats := uint64(dl)
45✔
79
                tlvRecords = append(
45✔
80
                        tlvRecords, tlv.MakePrimitiveRecord(
45✔
81
                                DPDustLimitSatoshis, &protoSats,
45✔
82
                        ),
45✔
83
                )
45✔
84
        })
45✔
85
        dc.MaxValueInFlight.WhenSome(func(max MilliSatoshi) {
155✔
86
                protoSats := uint64(max)
55✔
87
                tlvRecords = append(
55✔
88
                        tlvRecords, tlv.MakePrimitiveRecord(
55✔
89
                                DPMaxHtlcValueInFlightMsat, &protoSats,
55✔
90
                        ),
55✔
91
                )
55✔
92
        })
55✔
93
        dc.ChannelReserve.WhenSome(func(min btcutil.Amount) {
152✔
94
                channelReserve := uint64(min)
52✔
95
                tlvRecords = append(
52✔
96
                        tlvRecords, tlv.MakePrimitiveRecord(
52✔
97
                                DPChannelReserveSatoshis, &channelReserve,
52✔
98
                        ),
52✔
99
                )
52✔
100
        })
52✔
101
        dc.CsvDelay.WhenSome(func(wait uint16) {
152✔
102
                tlvRecords = append(
52✔
103
                        tlvRecords, tlv.MakePrimitiveRecord(
52✔
104
                                DPToSelfDelay, &wait,
52✔
105
                        ),
52✔
106
                )
52✔
107
        })
52✔
108
        dc.MaxAcceptedHTLCs.WhenSome(func(max uint16) {
156✔
109
                tlvRecords = append(
56✔
110
                        tlvRecords, tlv.MakePrimitiveRecord(
56✔
111
                                DPMaxAcceptedHtlcs, &max,
56✔
112
                        ),
56✔
113
                )
56✔
114
        })
56✔
115
        dc.FundingKey.WhenSome(func(key btcec.PublicKey) {
142✔
116
                keyScratch := &key
42✔
117
                tlvRecords = append(
42✔
118
                        tlvRecords, tlv.MakePrimitiveRecord(
42✔
119
                                DPFundingPubkey, &keyScratch,
42✔
120
                        ),
42✔
121
                )
42✔
122
        })
42✔
123
        dc.ChannelType.WhenSome(func(ty ChannelType) {
152✔
124
                tlvRecords = append(
52✔
125
                        tlvRecords, tlv.MakeDynamicRecord(
52✔
126
                                DPChannelType, &ty,
52✔
127
                                ty.featureBitLen,
52✔
128
                                channelTypeEncoder, channelTypeDecoder,
52✔
129
                        ),
52✔
130
                )
52✔
131
        })
52✔
132
        tlv.SortRecords(tlvRecords)
100✔
133

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

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

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

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

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

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

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

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

100✔
177
        var reserveScratch uint64
100✔
178
        reserve := tlv.MakePrimitiveRecord(
100✔
179
                DPChannelReserveSatoshis, &reserveScratch,
100✔
180
        )
100✔
181

100✔
182
        var csvDelayScratch uint16
100✔
183
        csvDelay := tlv.MakePrimitiveRecord(DPToSelfDelay, &csvDelayScratch)
100✔
184

100✔
185
        var maxHtlcsScratch uint16
100✔
186
        maxHtlcs := tlv.MakePrimitiveRecord(
100✔
187
                DPMaxAcceptedHtlcs, &maxHtlcsScratch,
100✔
188
        )
100✔
189

100✔
190
        var fundingKeyScratch *btcec.PublicKey
100✔
191
        fundingKey := tlv.MakePrimitiveRecord(
100✔
192
                DPFundingPubkey, &fundingKeyScratch,
100✔
193
        )
100✔
194

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

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

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

219
        // Check the results of the TLV Stream decoding and appropriately set
220
        // message fields.
221
        if val, ok := typeMap[DPDustLimitSatoshis]; ok && val == nil {
145✔
222
                dc.DustLimit = fn.Some(btcutil.Amount(dustLimitScratch))
45✔
223
        }
45✔
224
        if val, ok := typeMap[DPMaxHtlcValueInFlightMsat]; ok && val == nil {
155✔
225
                dc.MaxValueInFlight = fn.Some(MilliSatoshi(maxValueScratch))
55✔
226
        }
55✔
227
        if val, ok := typeMap[DPChannelReserveSatoshis]; ok && val == nil {
152✔
228
                dc.ChannelReserve = fn.Some(btcutil.Amount(reserveScratch))
52✔
229
        }
52✔
230
        if val, ok := typeMap[DPToSelfDelay]; ok && val == nil {
152✔
231
                dc.CsvDelay = fn.Some(csvDelayScratch)
52✔
232
        }
52✔
233
        if val, ok := typeMap[DPMaxAcceptedHtlcs]; ok && val == nil {
156✔
234
                dc.MaxAcceptedHTLCs = fn.Some(maxHtlcsScratch)
56✔
235
        }
56✔
236
        if val, ok := typeMap[DPFundingPubkey]; ok && val == nil {
142✔
237
                dc.FundingKey = fn.Some(*fundingKeyScratch)
42✔
238
        }
42✔
239
        if val, ok := typeMap[DPChannelType]; ok && val == nil {
152✔
240
                dc.ChannelType = fn.Some(chanTypeScratch)
52✔
241
        }
52✔
242

243
        if len(tlvRecords) != 0 {
198✔
244
                dc.ExtraData = tlvRecords
98✔
245
        }
98✔
246

247
        return nil
100✔
248
}
249

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

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