Skip to content

Commit 4498a78

Browse files
committed
routing: remove paymentFailureInfo
We can remove paymentFailureInfo since we can gate the result interpretation on the source index, meaning that if we don't have a source index, we deal with an unknown payment outcome because we couldn't pinpoint the failing hop.
1 parent e5c5414 commit 4498a78

File tree

2 files changed

+25
-98
lines changed

2 files changed

+25
-98
lines changed

routing/missioncontrol.go

Lines changed: 15 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -827,12 +827,6 @@ func (n *namespacedDB) purge() error {
827827
}, func() {})
828828
}
829829

830-
// paymentFailure represents the presence of a payment failure. It may or may
831-
// not include additional information about said failure.
832-
type paymentFailure struct {
833-
info tlv.OptionalRecordT[tlv.TlvType0, paymentFailureInfo]
834-
}
835-
836830
// newPaymentFailure constructs a new paymentFailure struct. If the source
837831
// index is nil, then an empty paymentFailure is returned. This represents a
838832
// failure with unknown details. Otherwise, the index and failure message are
@@ -844,7 +838,7 @@ func newPaymentFailure(sourceIdx *int,
844838
return &paymentFailure{}
845839
}
846840

847-
info := paymentFailureInfo{
841+
return &paymentFailure{
848842
sourceIdx: tlv.SomeRecordT(
849843
tlv.NewPrimitiveRecord[tlv.TlvType0](
850844
uint8(*sourceIdx),
@@ -855,77 +849,10 @@ func newPaymentFailure(sourceIdx *int,
855849
),
856850
),
857851
}
858-
859-
return &paymentFailure{
860-
info: tlv.SomeRecordT(tlv.NewRecordT[tlv.TlvType0](info)),
861-
}
862-
}
863-
864-
// Record returns a TLV record that can be used to encode/decode a
865-
// paymentFailure to/from a TLV stream.
866-
func (r *paymentFailure) Record() tlv.Record {
867-
recordSize := func() uint64 {
868-
var (
869-
b bytes.Buffer
870-
buf [8]byte
871-
)
872-
if err := encodePaymentFailure(&b, r, &buf); err != nil {
873-
panic(err)
874-
}
875-
876-
return uint64(len(b.Bytes()))
877-
}
878-
879-
return tlv.MakeDynamicRecord(
880-
0, r, recordSize, encodePaymentFailure, decodePaymentFailure,
881-
)
882-
}
883-
884-
func encodePaymentFailure(w io.Writer, val interface{}, _ *[8]byte) error {
885-
if v, ok := val.(*paymentFailure); ok {
886-
var recordProducers []tlv.RecordProducer
887-
v.info.WhenSome(
888-
func(r tlv.RecordT[tlv.TlvType0, paymentFailureInfo]) {
889-
recordProducers = append(recordProducers, &r)
890-
},
891-
)
892-
893-
return lnwire.EncodeRecordsTo(
894-
w, lnwire.ProduceRecordsSorted(recordProducers...),
895-
)
896-
}
897-
898-
return tlv.NewTypeForEncodingErr(val, "routing.paymentFailure")
899852
}
900853

901-
func decodePaymentFailure(r io.Reader, val interface{}, _ *[8]byte,
902-
l uint64) error {
903-
904-
if v, ok := val.(*paymentFailure); ok {
905-
var h paymentFailure
906-
907-
info := tlv.ZeroRecordT[tlv.TlvType0, paymentFailureInfo]()
908-
typeMap, err := lnwire.DecodeRecords(
909-
r, lnwire.ProduceRecordsSorted(&info)...,
910-
)
911-
if err != nil {
912-
return err
913-
}
914-
915-
if _, ok := typeMap[h.info.TlvType()]; ok {
916-
h.info = tlv.SomeRecordT(info)
917-
}
918-
919-
*v = h
920-
921-
return nil
922-
}
923-
924-
return tlv.NewTypeForDecodingErr(val, "routing.paymentFailure", l, l)
925-
}
926-
927-
// paymentFailureInfo holds additional information about a payment failure.
928-
type paymentFailureInfo struct {
854+
// paymentFailure holds additional information about a payment failure.
855+
type paymentFailure struct {
929856
// sourceIdx is the hop the error was reported from. In order to be able
930857
// to decrypt the error message, we need to know the source, which is
931858
// why an error message can only be present if the source is known.
@@ -938,28 +865,28 @@ type paymentFailureInfo struct {
938865
}
939866

940867
// Record returns a TLV record that can be used to encode/decode a
941-
// paymentFailureInfo to/from a TLV stream.
942-
func (r *paymentFailureInfo) Record() tlv.Record {
868+
// paymentFailure to/from a TLV stream.
869+
func (r *paymentFailure) Record() tlv.Record {
943870
recordSize := func() uint64 {
944871
var (
945872
b bytes.Buffer
946873
buf [8]byte
947874
)
948-
if err := encodePaymentFailureInfo(&b, r, &buf); err != nil {
875+
if err := encodePaymentFailure(&b, r, &buf); err != nil {
949876
panic(err)
950877
}
951878

952879
return uint64(len(b.Bytes()))
953880
}
954881

955882
return tlv.MakeDynamicRecord(
956-
0, r, recordSize, encodePaymentFailureInfo,
957-
decodePaymentFailureInfo,
883+
0, r, recordSize, encodePaymentFailure,
884+
decodePaymentFailure,
958885
)
959886
}
960887

961-
func encodePaymentFailureInfo(w io.Writer, val interface{}, _ *[8]byte) error {
962-
if v, ok := val.(*paymentFailureInfo); ok {
888+
func encodePaymentFailure(w io.Writer, val interface{}, _ *[8]byte) error {
889+
if v, ok := val.(*paymentFailure); ok {
963890
var recordProducers []tlv.RecordProducer
964891

965892
v.sourceIdx.WhenSome(
@@ -985,14 +912,14 @@ func encodePaymentFailureInfo(w io.Writer, val interface{}, _ *[8]byte) error {
985912
)
986913
}
987914

988-
return tlv.NewTypeForEncodingErr(val, "routing.paymentFailureInfo")
915+
return tlv.NewTypeForEncodingErr(val, "routing.paymentFailure")
989916
}
990917

991-
func decodePaymentFailureInfo(r io.Reader, val interface{}, _ *[8]byte,
918+
func decodePaymentFailure(r io.Reader, val interface{}, _ *[8]byte,
992919
l uint64) error {
993920

994-
if v, ok := val.(*paymentFailureInfo); ok {
995-
var h paymentFailureInfo
921+
if v, ok := val.(*paymentFailure); ok {
922+
var h paymentFailure
996923

997924
sourceIdx := tlv.ZeroRecordT[tlv.TlvType0, uint8]()
998925
msg := tlv.ZeroRecordT[tlv.TlvType1, failureMessage]()
@@ -1020,6 +947,6 @@ func decodePaymentFailureInfo(r io.Reader, val interface{}, _ *[8]byte,
1020947
}
1021948

1022949
return tlv.NewTypeForDecodingErr(
1023-
val, "routing.paymentFailureInfo", l, l,
950+
val, "routing.paymentFailure", l, l,
1024951
)
1025952
}

routing/result_interpretation.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ func (i *interpretedResult) processSuccess(route *mcRoute) {
126126

127127
// processFail processes a failed payment attempt.
128128
func (i *interpretedResult) processFail(rt *mcRoute, failure paymentFailure) {
129-
if failure.info.IsNone() {
129+
// Not having a source index means that we were unable to decrypt the
130+
// error message.
131+
if failure.sourceIdx.IsNone() {
130132
i.processPaymentOutcomeUnknown(rt)
131133
return
132134
}
@@ -136,16 +138,14 @@ func (i *interpretedResult) processFail(rt *mcRoute, failure paymentFailure) {
136138
failMsg lnwire.FailureMessage
137139
)
138140

139-
failure.info.WhenSome(
140-
func(r tlv.RecordT[tlv.TlvType0, paymentFailureInfo]) {
141-
r.Val.sourceIdx.WhenSome(
142-
func(r tlv.RecordT[tlv.TlvType0, uint8]) {
143-
idx = int(r.Val)
144-
},
145-
)
141+
failure.sourceIdx.WhenSome(
142+
func(r tlv.RecordT[tlv.TlvType0, uint8]) {
143+
idx = int(r.Val)
144+
145+
failure.msg.WhenSome(
146+
func(r tlv.RecordT[tlv.TlvType1,
147+
failureMessage]) {
146148

147-
r.Val.msg.WhenSome(
148-
func(r tlv.RecordT[tlv.TlvType1, failureMessage]) {
149149
failMsg = r.Val.FailureMessage
150150
},
151151
)

0 commit comments

Comments
 (0)