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

lightningnetwork / lnd / 11374676620

16 Oct 2024 10:12PM UTC coverage: 57.847% (-0.9%) from 58.78%
11374676620

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*

353 of 634 new or added lines in 12 files covered. (55.68%)

18908 existing lines in 242 files now uncovered.

99301 of 171661 relevant lines covered (57.85%)

37037.59 hits per line

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

85.03
/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
// DynCommit is a composite message that is used to irrefutably execute a
13
// dynamic commitment update.
14
type DynCommit struct {
15
        // DynPropose is an embedded version of the original DynPropose message
16
        // that initiated this negotiation.
17
        DynPropose
18

19
        // DynAck is an embedded version of the original DynAck message that
20
        // countersigned this negotiation.
21
        DynAck
22

23
        // ExtraData is the set of data that was appended to this message to
24
        // fill out the full maximum transport message size. These fields can
25
        // be used to specify optional data such as custom TLV fields.
26
        ExtraData ExtraOpaqueData
27
}
28

29
// A compile time check to ensure DynAck implements the lnwire.Message
30
// interface.
31
var _ Message = (*DynCommit)(nil)
32

33
// Encode serializes the target DynAck into the passed io.Writer. Serialization
34
// will observe the rules defined by the passed protocol version.
35
//
36
// This is a part of the lnwire.Message interface.
37
func (dc *DynCommit) Encode(w *bytes.Buffer, _ uint32) error {
100✔
38
        if err := WriteChannelID(w, dc.DynPropose.ChanID); err != nil {
100✔
NEW
39
                return err
×
NEW
40
        }
×
41

42
        if err := WriteSig(w, dc.Sig); err != nil {
100✔
NEW
43
                return err
×
NEW
44
        }
×
45

46
        var tlvRecords []tlv.Record
100✔
47
        dc.DustLimit.WhenSome(func(dl btcutil.Amount) {
152✔
48
                protoSats := uint64(dl)
52✔
49
                tlvRecords = append(
52✔
50
                        tlvRecords, tlv.MakePrimitiveRecord(
52✔
51
                                DPDustLimitSatoshis, &protoSats,
52✔
52
                        ),
52✔
53
                )
52✔
54
        })
52✔
55
        dc.MaxValueInFlight.WhenSome(func(max MilliSatoshi) {
143✔
56
                protoSats := uint64(max)
43✔
57
                tlvRecords = append(
43✔
58
                        tlvRecords, tlv.MakePrimitiveRecord(
43✔
59
                                DPMaxHtlcValueInFlightMsat, &protoSats,
43✔
60
                        ),
43✔
61
                )
43✔
62
        })
43✔
63
        dc.HtlcMinimum.WhenSome(func(min MilliSatoshi) {
100✔
NEW
64
                protoSats := uint64(min)
×
NEW
65
                tlvRecords = append(
×
NEW
66
                        tlvRecords, tlv.MakePrimitiveRecord(
×
NEW
67
                                DPHtlcMinimumMsat, &protoSats,
×
NEW
68
                        ),
×
NEW
69
                )
×
NEW
70
        })
×
71
        dc.ChannelReserve.WhenSome(func(min btcutil.Amount) {
155✔
72
                channelReserve := uint64(min)
55✔
73
                tlvRecords = append(
55✔
74
                        tlvRecords, tlv.MakePrimitiveRecord(
55✔
75
                                DPChannelReserveSatoshis, &channelReserve,
55✔
76
                        ),
55✔
77
                )
55✔
78
        })
55✔
79
        dc.CsvDelay.WhenSome(func(wait uint16) {
153✔
80
                tlvRecords = append(
53✔
81
                        tlvRecords, tlv.MakePrimitiveRecord(
53✔
82
                                DPToSelfDelay, &wait,
53✔
83
                        ),
53✔
84
                )
53✔
85
        })
53✔
86
        dc.MaxAcceptedHTLCs.WhenSome(func(max uint16) {
156✔
87
                tlvRecords = append(
56✔
88
                        tlvRecords, tlv.MakePrimitiveRecord(
56✔
89
                                DPMaxAcceptedHtlcs, &max,
56✔
90
                        ),
56✔
91
                )
56✔
92
        })
56✔
93
        dc.ChannelType.WhenSome(func(ty ChannelType) {
157✔
94
                tlvRecords = append(
57✔
95
                        tlvRecords, tlv.MakeDynamicRecord(
57✔
96
                                DPChannelType, &ty,
57✔
97
                                ty.featureBitLen,
57✔
98
                                channelTypeEncoder, channelTypeDecoder,
57✔
99
                        ),
57✔
100
                )
57✔
101
        })
57✔
102
        tlv.SortRecords(tlvRecords)
100✔
103

100✔
104
        tlvStream, err := tlv.NewStream(tlvRecords...)
100✔
105
        if err != nil {
100✔
NEW
106
                return err
×
NEW
107
        }
×
108

109
        var extraBytesWriter bytes.Buffer
100✔
110
        if err := tlvStream.Encode(&extraBytesWriter); err != nil {
100✔
NEW
111
                return err
×
NEW
112
        }
×
113

114
        dc.ExtraData = ExtraOpaqueData(extraBytesWriter.Bytes())
100✔
115

100✔
116
        return WriteBytes(w, dc.ExtraData)
100✔
117
}
118

119
// Decode deserializes the serialized DynCommit stored in the passed io.Reader
120
// into the target DynAck using the deserialization rules defined by the passed
121
// protocol version.
122
//
123
// This is a part of the lnwire.Message interface.
124
func (dc *DynCommit) Decode(r io.Reader, _ uint32) error {
100✔
125
        // Parse out main message.
100✔
126
        if err := ReadElements(r, &dc.DynPropose.ChanID, &dc.Sig); err != nil {
100✔
NEW
127
                return err
×
NEW
128
        }
×
129
        dc.DynAck.ChanID = dc.DynPropose.ChanID
100✔
130

100✔
131
        // Parse out TLV records.
100✔
132
        var tlvRecords ExtraOpaqueData
100✔
133
        if err := ReadElement(r, &tlvRecords); err != nil {
100✔
NEW
134
                return err
×
NEW
135
        }
×
136

137
        // Prepare receiving buffers to be filled by TLV extraction.
138
        var dustLimitScratch uint64
100✔
139
        dustLimit := tlv.MakePrimitiveRecord(
100✔
140
                DPDustLimitSatoshis, &dustLimitScratch,
100✔
141
        )
100✔
142

100✔
143
        var maxValueScratch uint64
100✔
144
        maxValue := tlv.MakePrimitiveRecord(
100✔
145
                DPMaxHtlcValueInFlightMsat, &maxValueScratch,
100✔
146
        )
100✔
147

100✔
148
        var htlcMinScratch uint64
100✔
149
        htlcMin := tlv.MakePrimitiveRecord(
100✔
150
                DPHtlcMinimumMsat, &htlcMinScratch,
100✔
151
        )
100✔
152

100✔
153
        var reserveScratch uint64
100✔
154
        reserve := tlv.MakePrimitiveRecord(
100✔
155
                DPChannelReserveSatoshis, &reserveScratch,
100✔
156
        )
100✔
157

100✔
158
        var csvDelayScratch uint16
100✔
159
        csvDelay := tlv.MakePrimitiveRecord(DPToSelfDelay, &csvDelayScratch)
100✔
160

100✔
161
        var maxHtlcsScratch uint16
100✔
162
        maxHtlcs := tlv.MakePrimitiveRecord(
100✔
163
                DPMaxAcceptedHtlcs, &maxHtlcsScratch,
100✔
164
        )
100✔
165

100✔
166
        var chanTypeScratch ChannelType
100✔
167
        chanType := tlv.MakeDynamicRecord(
100✔
168
                DPChannelType, &chanTypeScratch, chanTypeScratch.featureBitLen,
100✔
169
                channelTypeEncoder, channelTypeDecoder,
100✔
170
        )
100✔
171

100✔
172
        // Create set of Records to read TLV bytestream into.
100✔
173
        records := []tlv.Record{
100✔
174
                dustLimit, maxValue, htlcMin, reserve, csvDelay, maxHtlcs,
100✔
175
                chanType,
100✔
176
        }
100✔
177
        tlv.SortRecords(records)
100✔
178

100✔
179
        // Read TLV stream into record set.
100✔
180
        extraBytesReader := bytes.NewReader(tlvRecords)
100✔
181
        tlvStream, err := tlv.NewStream(records...)
100✔
182
        if err != nil {
100✔
NEW
183
                return err
×
NEW
184
        }
×
185
        typeMap, err := tlvStream.DecodeWithParsedTypesP2P(extraBytesReader)
100✔
186
        if err != nil {
100✔
NEW
187
                return err
×
NEW
188
        }
×
189

190
        // Check the results of the TLV Stream decoding and appropriately set
191
        // message fields.
192
        if val, ok := typeMap[DPDustLimitSatoshis]; ok && val == nil {
152✔
193
                dc.DustLimit = fn.Some(btcutil.Amount(dustLimitScratch))
52✔
194
        }
52✔
195
        if val, ok := typeMap[DPMaxHtlcValueInFlightMsat]; ok && val == nil {
143✔
196
                dc.MaxValueInFlight = fn.Some(MilliSatoshi(maxValueScratch))
43✔
197
        }
43✔
198
        if val, ok := typeMap[DPHtlcMinimumMsat]; ok && val == nil {
100✔
NEW
199
                dc.HtlcMinimum = fn.Some(MilliSatoshi(htlcMinScratch))
×
NEW
200
        }
×
201
        if val, ok := typeMap[DPChannelReserveSatoshis]; ok && val == nil {
155✔
202
                dc.ChannelReserve = fn.Some(btcutil.Amount(reserveScratch))
55✔
203
        }
55✔
204
        if val, ok := typeMap[DPToSelfDelay]; ok && val == nil {
153✔
205
                dc.CsvDelay = fn.Some(csvDelayScratch)
53✔
206
        }
53✔
207
        if val, ok := typeMap[DPMaxAcceptedHtlcs]; ok && val == nil {
156✔
208
                dc.MaxAcceptedHTLCs = fn.Some(maxHtlcsScratch)
56✔
209
        }
56✔
210
        if val, ok := typeMap[DPChannelType]; ok && val == nil {
157✔
211
                dc.ChannelType = fn.Some(chanTypeScratch)
57✔
212
        }
57✔
213

214
        if len(tlvRecords) != 0 {
196✔
215
                dc.ExtraData = tlvRecords
96✔
216
        }
96✔
217

218
        return nil
100✔
219
}
220

221
// MsgType returns the MessageType code which uniquely identifies this message
222
// as a DynCommit on the wire.
223
//
224
// This is part of the lnwire.Message interface.
225
func (dc *DynCommit) MsgType() MessageType {
100✔
226
        return MsgDynCommit
100✔
227
}
100✔
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