Skip to content

Commit e5c5414

Browse files
committed
routing: make msg and index optional
This is later used to handle their nil values.
1 parent bc4229b commit e5c5414

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

routing/missioncontrol.go

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,15 @@ func newPaymentFailure(sourceIdx *int,
845845
}
846846

847847
info := paymentFailureInfo{
848-
sourceIdx: tlv.NewPrimitiveRecord[tlv.TlvType0](
849-
uint8(*sourceIdx),
848+
sourceIdx: tlv.SomeRecordT(
849+
tlv.NewPrimitiveRecord[tlv.TlvType0](
850+
uint8(*sourceIdx),
851+
)),
852+
msg: tlv.SomeRecordT(
853+
tlv.NewRecordT[tlv.TlvType1](
854+
failureMessage{failureMsg},
855+
),
850856
),
851-
msg: tlv.NewRecordT[tlv.TlvType1](failureMessage{failureMsg}),
852857
}
853858

854859
return &paymentFailure{
@@ -921,8 +926,15 @@ func decodePaymentFailure(r io.Reader, val interface{}, _ *[8]byte,
921926

922927
// paymentFailureInfo holds additional information about a payment failure.
923928
type paymentFailureInfo struct {
924-
sourceIdx tlv.RecordT[tlv.TlvType0, uint8]
925-
msg tlv.RecordT[tlv.TlvType1, failureMessage]
929+
// sourceIdx is the hop the error was reported from. In order to be able
930+
// to decrypt the error message, we need to know the source, which is
931+
// why an error message can only be present if the source is known.
932+
sourceIdx tlv.OptionalRecordT[tlv.TlvType0, uint8]
933+
934+
// msg is the error why a payment failed. If we identify the failure of
935+
// a certain hop at the above index, but aren't able to decode the
936+
// failure message we indicate this by not setting this field.
937+
msg tlv.OptionalRecordT[tlv.TlvType1, failureMessage]
926938
}
927939

928940
// Record returns a TLV record that can be used to encode/decode a
@@ -948,9 +960,27 @@ func (r *paymentFailureInfo) Record() tlv.Record {
948960

949961
func encodePaymentFailureInfo(w io.Writer, val interface{}, _ *[8]byte) error {
950962
if v, ok := val.(*paymentFailureInfo); ok {
963+
var recordProducers []tlv.RecordProducer
964+
965+
v.sourceIdx.WhenSome(
966+
func(r tlv.RecordT[tlv.TlvType0, uint8]) {
967+
recordProducers = append(
968+
recordProducers, &r,
969+
)
970+
},
971+
)
972+
973+
v.msg.WhenSome(
974+
func(r tlv.RecordT[tlv.TlvType1, failureMessage]) {
975+
recordProducers = append(
976+
recordProducers, &r,
977+
)
978+
},
979+
)
980+
951981
return lnwire.EncodeRecordsTo(
952982
w, lnwire.ProduceRecordsSorted(
953-
&v.sourceIdx, &v.msg,
983+
recordProducers...,
954984
),
955985
)
956986
}
@@ -964,14 +994,26 @@ func decodePaymentFailureInfo(r io.Reader, val interface{}, _ *[8]byte,
964994
if v, ok := val.(*paymentFailureInfo); ok {
965995
var h paymentFailureInfo
966996

967-
_, err := lnwire.DecodeRecords(
997+
sourceIdx := tlv.ZeroRecordT[tlv.TlvType0, uint8]()
998+
msg := tlv.ZeroRecordT[tlv.TlvType1, failureMessage]()
999+
1000+
typeMap, err := lnwire.DecodeRecords(
9681001
r,
969-
lnwire.ProduceRecordsSorted(&h.sourceIdx, &h.msg)...,
1002+
lnwire.ProduceRecordsSorted(&sourceIdx, &msg)...,
9701003
)
1004+
9711005
if err != nil {
9721006
return err
9731007
}
9741008

1009+
if _, ok := typeMap[h.sourceIdx.TlvType()]; ok {
1010+
h.sourceIdx = tlv.SomeRecordT(sourceIdx)
1011+
}
1012+
1013+
if _, ok := typeMap[h.msg.TlvType()]; ok {
1014+
h.msg = tlv.SomeRecordT(msg)
1015+
}
1016+
9751017
*v = h
9761018

9771019
return nil

routing/result_interpretation.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,17 @@ func (i *interpretedResult) processFail(rt *mcRoute, failure paymentFailure) {
138138

139139
failure.info.WhenSome(
140140
func(r tlv.RecordT[tlv.TlvType0, paymentFailureInfo]) {
141-
idx = int(r.Val.sourceIdx.Val)
142-
failMsg = r.Val.msg.Val.FailureMessage
141+
r.Val.sourceIdx.WhenSome(
142+
func(r tlv.RecordT[tlv.TlvType0, uint8]) {
143+
idx = int(r.Val)
144+
},
145+
)
146+
147+
r.Val.msg.WhenSome(
148+
func(r tlv.RecordT[tlv.TlvType1, failureMessage]) {
149+
failMsg = r.Val.FailureMessage
150+
},
151+
)
143152
},
144153
)
145154

0 commit comments

Comments
 (0)