Skip to content

Commit 68e6ad9

Browse files
committed
routing+migration: make payment failure msg optional
1 parent 9fa7ef2 commit 68e6ad9

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

channeldb/migration32/mission_control_store.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,14 @@ func newPaymentFailure(sourceIdx *int,
166166
sourceIdx: tlv.NewPrimitiveRecord[tlv.TlvType0](
167167
uint8(*sourceIdx),
168168
),
169-
msg: tlv.NewRecordT[tlv.TlvType1](failureMessage{failureMsg}),
169+
}
170+
171+
if failureMsg != nil {
172+
info.msg = tlv.SomeRecordT(
173+
tlv.NewRecordT[tlv.TlvType1](
174+
failureMessage{failureMsg},
175+
),
176+
)
170177
}
171178

172179
return paymentFailure{
@@ -239,8 +246,15 @@ func decodePaymentFailure(r io.Reader, val interface{}, _ *[8]byte,
239246

240247
// paymentFailureInfo holds additional information about a payment failure.
241248
type paymentFailureInfo struct {
249+
// sourceIdx is the hop the error was reported from. To have a
250+
// meaningful payment failure, we need to have a source index. Otherwise
251+
// we indicate that there was no error information.
242252
sourceIdx tlv.RecordT[tlv.TlvType0, uint8]
243-
msg tlv.RecordT[tlv.TlvType1, failureMessage]
253+
254+
// msg is the error why a payment failed. If we identify the failure of
255+
// a certain hop, but aren't able to decode the failure message, we
256+
// don't set the failure message, which is why it is an optional record.
257+
msg tlv.OptionalRecordT[tlv.TlvType1, failureMessage]
244258
}
245259

246260
// Record returns a TLV record that can be used to encode/decode a
@@ -266,10 +280,17 @@ func (r *paymentFailureInfo) Record() tlv.Record {
266280

267281
func encodePaymentFailureInfo(w io.Writer, val interface{}, _ *[8]byte) error {
268282
if v, ok := val.(*paymentFailureInfo); ok {
283+
recordProducers := []tlv.RecordProducer{
284+
&v.sourceIdx,
285+
}
286+
v.msg.WhenSome(
287+
func(r tlv.RecordT[tlv.TlvType1, failureMessage]) {
288+
recordProducers = append(recordProducers, &r)
289+
},
290+
)
291+
269292
return lnwire.EncodeRecordsTo(
270-
w, lnwire.ProduceRecordsSorted(
271-
&v.sourceIdx, &v.msg,
272-
),
293+
w, lnwire.ProduceRecordsSorted(recordProducers...),
273294
)
274295
}
275296

@@ -282,14 +303,19 @@ func decodePaymentFailureInfo(r io.Reader, val interface{}, _ *[8]byte,
282303
if v, ok := val.(*paymentFailureInfo); ok {
283304
var h paymentFailureInfo
284305

285-
_, err := lnwire.DecodeRecords(
306+
msg := tlv.ZeroRecordT[tlv.TlvType1, failureMessage]()
307+
typeMap, err := lnwire.DecodeRecords(
286308
r,
287-
lnwire.ProduceRecordsSorted(&h.sourceIdx, &h.msg)...,
309+
lnwire.ProduceRecordsSorted(&h.sourceIdx, &msg)...,
288310
)
289311
if err != nil {
290312
return err
291313
}
292314

315+
if _, ok := typeMap[h.msg.TlvType()]; ok {
316+
h.msg = tlv.SomeRecordT(msg)
317+
}
318+
293319
*v = h
294320

295321
return nil

routing/missioncontrol.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,14 @@ func newPaymentFailure(sourceIdx *int,
849849
sourceIdx: tlv.NewPrimitiveRecord[tlv.TlvType0](
850850
uint8(*sourceIdx),
851851
),
852-
msg: tlv.NewRecordT[tlv.TlvType1](failureMessage{failureMsg}),
852+
}
853+
854+
if failureMsg != nil {
855+
info.msg = tlv.SomeRecordT(
856+
tlv.NewRecordT[tlv.TlvType1](
857+
failureMessage{failureMsg},
858+
),
859+
)
853860
}
854861

855862
return paymentFailure{
@@ -922,8 +929,15 @@ func decodePaymentFailure(r io.Reader, val interface{}, _ *[8]byte,
922929

923930
// paymentFailureInfo holds additional information about a payment failure.
924931
type paymentFailureInfo struct {
932+
// sourceIdx is the hop the error was reported from. To have a
933+
// meaningful payment failure, we need to have a source index. Otherwise
934+
// we indicate that there was no error information.
925935
sourceIdx tlv.RecordT[tlv.TlvType0, uint8]
926-
msg tlv.RecordT[tlv.TlvType1, failureMessage]
936+
937+
// msg is the error why a payment failed. If we identify the failure of
938+
// a certain hop, but aren't able to decode the failure message, we
939+
// don't set the failure message, which is why it is an optional record.
940+
msg tlv.OptionalRecordT[tlv.TlvType1, failureMessage]
927941
}
928942

929943
// Record returns a TLV record that can be used to encode/decode a
@@ -949,10 +963,17 @@ func (r *paymentFailureInfo) Record() tlv.Record {
949963

950964
func encodePaymentFailureInfo(w io.Writer, val interface{}, _ *[8]byte) error {
951965
if v, ok := val.(*paymentFailureInfo); ok {
966+
recordProducers := []tlv.RecordProducer{
967+
&v.sourceIdx,
968+
}
969+
v.msg.WhenSome(
970+
func(r tlv.RecordT[tlv.TlvType1, failureMessage]) {
971+
recordProducers = append(recordProducers, &r)
972+
},
973+
)
974+
952975
return lnwire.EncodeRecordsTo(
953-
w, lnwire.ProduceRecordsSorted(
954-
&v.sourceIdx, &v.msg,
955-
),
976+
w, lnwire.ProduceRecordsSorted(recordProducers...),
956977
)
957978
}
958979

@@ -965,14 +986,19 @@ func decodePaymentFailureInfo(r io.Reader, val interface{}, _ *[8]byte,
965986
if v, ok := val.(*paymentFailureInfo); ok {
966987
var h paymentFailureInfo
967988

968-
_, err := lnwire.DecodeRecords(
989+
msg := tlv.ZeroRecordT[tlv.TlvType1, failureMessage]()
990+
typeMap, err := lnwire.DecodeRecords(
969991
r,
970-
lnwire.ProduceRecordsSorted(&h.sourceIdx, &h.msg)...,
992+
lnwire.ProduceRecordsSorted(&h.sourceIdx, &msg)...,
971993
)
972994
if err != nil {
973995
return err
974996
}
975997

998+
if _, ok := typeMap[h.msg.TlvType()]; ok {
999+
h.msg = tlv.SomeRecordT(msg)
1000+
}
1001+
9761002
*v = h
9771003

9781004
return nil

routing/result_interpretation.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,14 @@ func (i *interpretedResult) processFail(rt *mcRoute, failure paymentFailure) {
139139
failure.info.WhenSome(
140140
func(r tlv.RecordT[tlv.TlvType0, paymentFailureInfo]) {
141141
idx = int(r.Val.sourceIdx.Val)
142-
failMsg = r.Val.msg.Val.FailureMessage
142+
143+
r.Val.msg.WhenSome(
144+
func(msg tlv.RecordT[tlv.TlvType1,
145+
failureMessage]) {
146+
147+
failMsg = msg.Val.FailureMessage
148+
},
149+
)
143150
},
144151
)
145152

0 commit comments

Comments
 (0)