Skip to content

Commit 420001a

Browse files
committed
lnwire: add InboundFee TLV record to ChannelUpdate
1 parent 7add5bf commit 420001a

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

lnwire/channel_update.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77

88
"github.com/btcsuite/btcd/chaincfg/chainhash"
9+
"github.com/lightningnetwork/lnd/tlv"
910
)
1011

1112
// ChanUpdateMsgFlags is a bitfield that signals whether optional fields are
@@ -114,6 +115,10 @@ type ChannelUpdate1 struct {
114115
// HtlcMaximumMsat is the maximum HTLC value which will be accepted.
115116
HtlcMaximumMsat MilliSatoshi
116117

118+
// InboundFee is an optional TLV record that contains the fee
119+
// information for incoming HTLCs.
120+
InboundFee tlv.OptionalRecordT[tlv.TlvType55555, Fee]
121+
117122
// ExtraData is the set of data that was appended to this message to
118123
// fill out the full maximum transport message size. These fields can
119124
// be used to specify optional data such as custom TLV fields.
@@ -156,12 +161,27 @@ func (a *ChannelUpdate1) Decode(r io.Reader, _ uint32) error {
156161
}
157162
}
158163

159-
err = a.ExtraOpaqueData.Decode(r)
164+
var tlvRecords ExtraOpaqueData
165+
if err := ReadElements(r, &tlvRecords); err != nil {
166+
return err
167+
}
168+
169+
var inboundFee = a.InboundFee.Zero()
170+
typeMap, err := tlvRecords.ExtractRecords(&inboundFee)
160171
if err != nil {
161172
return err
162173
}
163174

164-
return a.ExtraOpaqueData.ValidateTLV()
175+
val, ok := typeMap[a.InboundFee.TlvType()]
176+
if ok && val == nil {
177+
a.InboundFee = tlv.SomeRecordT(inboundFee)
178+
}
179+
180+
if len(tlvRecords) != 0 {
181+
a.ExtraOpaqueData = tlvRecords
182+
}
183+
184+
return nil
165185
}
166186

167187
// Encode serializes the target ChannelUpdate into the passed io.Writer
@@ -218,6 +238,16 @@ func (a *ChannelUpdate1) Encode(w *bytes.Buffer, pver uint32) error {
218238
}
219239
}
220240

241+
recordProducers := make([]tlv.RecordProducer, 0, 1)
242+
a.InboundFee.WhenSome(func(fee tlv.RecordT[tlv.TlvType55555, Fee]) {
243+
recordProducers = append(recordProducers, &fee)
244+
})
245+
246+
err := EncodeMessageExtraData(&a.ExtraOpaqueData, recordProducers...)
247+
if err != nil {
248+
return err
249+
}
250+
221251
// Finally, append any extra opaque data.
222252
return WriteBytes(w, a.ExtraOpaqueData)
223253
}

lnwire/test_message.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/lightningnetwork/lnd/fn/v2"
1313
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
1414
"github.com/lightningnetwork/lnd/tlv"
15+
"github.com/stretchr/testify/require"
1516
"pgregory.net/rapid"
1617
)
1718

@@ -406,6 +407,45 @@ func (a *ChannelUpdate1) RandTestMessage(t *rapid.T) Message {
406407
maxHtlc = 0
407408
}
408409

410+
// Randomly decide if an inbound fee should be included.
411+
// By default, our extra opaque data will just be random TLV but if we
412+
// include an inbound fee, then we will also set the record in the
413+
// extra opaque data.
414+
var (
415+
customRecords, _ = RandCustomRecords(t, nil, false)
416+
inboundFee tlv.OptionalRecordT[tlv.TlvType55555, Fee]
417+
)
418+
includeInboundFee := rapid.Bool().Draw(t, "includeInboundFee")
419+
if includeInboundFee {
420+
if customRecords == nil {
421+
customRecords = make(CustomRecords)
422+
}
423+
424+
inFeeBase := int32(
425+
rapid.IntRange(-1000, 1000).Draw(t, "inFeeBase"),
426+
)
427+
inFeeProp := int32(
428+
rapid.IntRange(-1000, 1000).Draw(t, "inFeeProp"),
429+
)
430+
fee := Fee{
431+
BaseFee: inFeeBase,
432+
FeeRate: inFeeProp,
433+
}
434+
inboundFee = tlv.SomeRecordT(
435+
tlv.NewRecordT[tlv.TlvType55555, Fee](fee),
436+
)
437+
438+
var b bytes.Buffer
439+
feeRecord := fee.Record()
440+
err := feeRecord.Encode(&b)
441+
require.NoError(t, err)
442+
443+
customRecords[uint64(FeeRecordType)] = b.Bytes()
444+
}
445+
446+
extraBytes, err := customRecords.Serialize()
447+
require.NoError(t, err)
448+
409449
return &ChannelUpdate1{
410450
Signature: RandSignature(t),
411451
ChainHash: hash,
@@ -428,7 +468,8 @@ func (a *ChannelUpdate1) RandTestMessage(t *rapid.T) Message {
428468
t, "feeRate"),
429469
),
430470
HtlcMaximumMsat: maxHtlc,
431-
ExtraOpaqueData: RandExtraOpaqueData(t, nil),
471+
InboundFee: inboundFee,
472+
ExtraOpaqueData: extraBytes,
432473
}
433474
}
434475

0 commit comments

Comments
 (0)