Skip to content

Commit 11c97ec

Browse files
committed
lnwire: convert DynPropose and DynCommit to use typed tlv records
1 parent c73e942 commit 11c97ec

File tree

3 files changed

+197
-395
lines changed

3 files changed

+197
-395
lines changed

lnwire/dyn_commit.go

Lines changed: 42 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"io"
66

77
"github.com/btcsuite/btcd/btcutil"
8-
"github.com/lightningnetwork/lnd/fn"
98
"github.com/lightningnetwork/lnd/tlv"
109
)
1110

@@ -43,75 +42,12 @@ func (dc *DynCommit) Encode(w *bytes.Buffer, _ uint32) error {
4342
return err
4443
}
4544

46-
var tlvRecords []tlv.Record
47-
dc.DustLimit.WhenSome(func(dl btcutil.Amount) {
48-
protoSats := uint64(dl)
49-
tlvRecords = append(
50-
tlvRecords, tlv.MakePrimitiveRecord(
51-
DPDustLimitSatoshis, &protoSats,
52-
),
53-
)
54-
})
55-
dc.MaxValueInFlight.WhenSome(func(max MilliSatoshi) {
56-
protoSats := uint64(max)
57-
tlvRecords = append(
58-
tlvRecords, tlv.MakePrimitiveRecord(
59-
DPMaxHtlcValueInFlightMsat, &protoSats,
60-
),
61-
)
62-
})
63-
dc.HtlcMinimum.WhenSome(func(min MilliSatoshi) {
64-
protoSats := uint64(min)
65-
tlvRecords = append(
66-
tlvRecords, tlv.MakePrimitiveRecord(
67-
DPHtlcMinimumMsat, &protoSats,
68-
),
69-
)
70-
})
71-
dc.ChannelReserve.WhenSome(func(min btcutil.Amount) {
72-
channelReserve := uint64(min)
73-
tlvRecords = append(
74-
tlvRecords, tlv.MakePrimitiveRecord(
75-
DPChannelReserveSatoshis, &channelReserve,
76-
),
77-
)
78-
})
79-
dc.CsvDelay.WhenSome(func(wait uint16) {
80-
tlvRecords = append(
81-
tlvRecords, tlv.MakePrimitiveRecord(
82-
DPToSelfDelay, &wait,
83-
),
84-
)
85-
})
86-
dc.MaxAcceptedHTLCs.WhenSome(func(max uint16) {
87-
tlvRecords = append(
88-
tlvRecords, tlv.MakePrimitiveRecord(
89-
DPMaxAcceptedHtlcs, &max,
90-
),
91-
)
92-
})
93-
dc.ChannelType.WhenSome(func(ty ChannelType) {
94-
tlvRecords = append(
95-
tlvRecords, tlv.MakeDynamicRecord(
96-
DPChannelType, &ty,
97-
ty.featureBitLen,
98-
channelTypeEncoder, channelTypeDecoder,
99-
),
100-
)
101-
})
102-
tlv.SortRecords(tlvRecords)
103-
104-
tlvStream, err := tlv.NewStream(tlvRecords...)
45+
var extra ExtraOpaqueData
46+
err := extra.PackRecords(dynProposeRecords(&dc.DynPropose)...)
10547
if err != nil {
10648
return err
10749
}
108-
109-
var extraBytesWriter bytes.Buffer
110-
if err := tlvStream.Encode(&extraBytesWriter); err != nil {
111-
return err
112-
}
113-
114-
dc.ExtraData = ExtraOpaqueData(extraBytesWriter.Bytes())
50+
dc.ExtraData = extra
11551

11652
return WriteBytes(w, dc.ExtraData)
11753
}
@@ -135,80 +71,52 @@ func (dc *DynCommit) Decode(r io.Reader, _ uint32) error {
13571
}
13672

13773
// Prepare receiving buffers to be filled by TLV extraction.
138-
var dustLimitScratch uint64
139-
dustLimit := tlv.MakePrimitiveRecord(
140-
DPDustLimitSatoshis, &dustLimitScratch,
141-
)
142-
143-
var maxValueScratch uint64
144-
maxValue := tlv.MakePrimitiveRecord(
145-
DPMaxHtlcValueInFlightMsat, &maxValueScratch,
146-
)
147-
148-
var htlcMinScratch uint64
149-
htlcMin := tlv.MakePrimitiveRecord(
150-
DPHtlcMinimumMsat, &htlcMinScratch,
151-
)
152-
153-
var reserveScratch uint64
154-
reserve := tlv.MakePrimitiveRecord(
155-
DPChannelReserveSatoshis, &reserveScratch,
74+
var dustLimit tlv.RecordT[tlv.TlvType0, uint64]
75+
var maxValue tlv.RecordT[tlv.TlvType2, uint64]
76+
var htlcMin tlv.RecordT[tlv.TlvType4, uint64]
77+
var reserve tlv.RecordT[tlv.TlvType6, uint64]
78+
csvDelay := dc.CsvDelay.Zero()
79+
maxHtlcs := dc.MaxAcceptedHTLCs.Zero()
80+
chanType := dc.ChannelType.Zero()
81+
82+
typeMap, err := tlvRecords.ExtractRecords(
83+
&dustLimit, &maxValue, &htlcMin, &reserve, &csvDelay, &maxHtlcs,
84+
&chanType,
15685
)
157-
158-
var csvDelayScratch uint16
159-
csvDelay := tlv.MakePrimitiveRecord(DPToSelfDelay, &csvDelayScratch)
160-
161-
var maxHtlcsScratch uint16
162-
maxHtlcs := tlv.MakePrimitiveRecord(
163-
DPMaxAcceptedHtlcs, &maxHtlcsScratch,
164-
)
165-
166-
var chanTypeScratch ChannelType
167-
chanType := tlv.MakeDynamicRecord(
168-
DPChannelType, &chanTypeScratch, chanTypeScratch.featureBitLen,
169-
channelTypeEncoder, channelTypeDecoder,
170-
)
171-
172-
// Create set of Records to read TLV bytestream into.
173-
records := []tlv.Record{
174-
dustLimit, maxValue, htlcMin, reserve, csvDelay, maxHtlcs,
175-
chanType,
176-
}
177-
tlv.SortRecords(records)
178-
179-
// Read TLV stream into record set.
180-
extraBytesReader := bytes.NewReader(tlvRecords)
181-
tlvStream, err := tlv.NewStream(records...)
182-
if err != nil {
183-
return err
184-
}
185-
typeMap, err := tlvStream.DecodeWithParsedTypesP2P(extraBytesReader)
18686
if err != nil {
18787
return err
18888
}
18989

19090
// Check the results of the TLV Stream decoding and appropriately set
19191
// message fields.
192-
if val, ok := typeMap[DPDustLimitSatoshis]; ok && val == nil {
193-
dc.DustLimit = fn.Some(btcutil.Amount(dustLimitScratch))
194-
}
195-
if val, ok := typeMap[DPMaxHtlcValueInFlightMsat]; ok && val == nil {
196-
dc.MaxValueInFlight = fn.Some(MilliSatoshi(maxValueScratch))
197-
}
198-
if val, ok := typeMap[DPHtlcMinimumMsat]; ok && val == nil {
199-
dc.HtlcMinimum = fn.Some(MilliSatoshi(htlcMinScratch))
200-
}
201-
if val, ok := typeMap[DPChannelReserveSatoshis]; ok && val == nil {
202-
dc.ChannelReserve = fn.Some(btcutil.Amount(reserveScratch))
203-
}
204-
if val, ok := typeMap[DPToSelfDelay]; ok && val == nil {
205-
dc.CsvDelay = fn.Some(csvDelayScratch)
206-
}
207-
if val, ok := typeMap[DPMaxAcceptedHtlcs]; ok && val == nil {
208-
dc.MaxAcceptedHTLCs = fn.Some(maxHtlcsScratch)
209-
}
210-
if val, ok := typeMap[DPChannelType]; ok && val == nil {
211-
dc.ChannelType = fn.Some(chanTypeScratch)
92+
if val, ok := typeMap[dc.DustLimit.TlvType()]; ok && val == nil {
93+
var rec tlv.RecordT[tlv.TlvType0, btcutil.Amount]
94+
rec.Val = btcutil.Amount(dustLimit.Val)
95+
dc.DustLimit = tlv.SomeRecordT(rec)
96+
}
97+
if val, ok := typeMap[dc.MaxValueInFlight.TlvType()]; ok && val == nil {
98+
var rec tlv.RecordT[tlv.TlvType2, MilliSatoshi]
99+
rec.Val = MilliSatoshi(maxValue.Val)
100+
dc.MaxValueInFlight = tlv.SomeRecordT(rec)
101+
}
102+
if val, ok := typeMap[dc.HtlcMinimum.TlvType()]; ok && val == nil {
103+
var rec tlv.RecordT[tlv.TlvType4, MilliSatoshi]
104+
rec.Val = MilliSatoshi(htlcMin.Val)
105+
dc.HtlcMinimum = tlv.SomeRecordT(rec)
106+
}
107+
if val, ok := typeMap[dc.ChannelReserve.TlvType()]; ok && val == nil {
108+
var rec tlv.RecordT[tlv.TlvType6, btcutil.Amount]
109+
rec.Val = btcutil.Amount(reserve.Val)
110+
dc.ChannelReserve = tlv.SomeRecordT(rec)
111+
}
112+
if val, ok := typeMap[dc.CsvDelay.TlvType()]; ok && val == nil {
113+
dc.CsvDelay = tlv.SomeRecordT(csvDelay)
114+
}
115+
if val, ok := typeMap[dc.MaxAcceptedHTLCs.TlvType()]; ok && val == nil {
116+
dc.MaxAcceptedHTLCs = tlv.SomeRecordT(maxHtlcs)
117+
}
118+
if val, ok := typeMap[dc.ChannelType.TlvType()]; ok && val == nil {
119+
dc.ChannelType = tlv.SomeRecordT(chanType)
212120
}
213121

214122
if len(tlvRecords) != 0 {

0 commit comments

Comments
 (0)