@@ -845,10 +845,15 @@ func newPaymentFailure(sourceIdx *int,
845
845
}
846
846
847
847
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
+ ),
850
856
),
851
- msg : tlv.NewRecordT [tlv.TlvType1 ](failureMessage {failureMsg }),
852
857
}
853
858
854
859
return & paymentFailure {
@@ -921,8 +926,15 @@ func decodePaymentFailure(r io.Reader, val interface{}, _ *[8]byte,
921
926
922
927
// paymentFailureInfo holds additional information about a payment failure.
923
928
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 ]
926
938
}
927
939
928
940
// Record returns a TLV record that can be used to encode/decode a
@@ -948,9 +960,27 @@ func (r *paymentFailureInfo) Record() tlv.Record {
948
960
949
961
func encodePaymentFailureInfo (w io.Writer , val interface {}, _ * [8 ]byte ) error {
950
962
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
+
951
981
return lnwire .EncodeRecordsTo (
952
982
w , lnwire .ProduceRecordsSorted (
953
- & v . sourceIdx , & v . msg ,
983
+ recordProducers ... ,
954
984
),
955
985
)
956
986
}
@@ -964,14 +994,26 @@ func decodePaymentFailureInfo(r io.Reader, val interface{}, _ *[8]byte,
964
994
if v , ok := val .(* paymentFailureInfo ); ok {
965
995
var h paymentFailureInfo
966
996
967
- _ , err := lnwire .DecodeRecords (
997
+ sourceIdx := tlv .ZeroRecordT [tlv.TlvType0 , uint8 ]()
998
+ msg := tlv .ZeroRecordT [tlv.TlvType1 , failureMessage ]()
999
+
1000
+ typeMap , err := lnwire .DecodeRecords (
968
1001
r ,
969
- lnwire .ProduceRecordsSorted (& h . sourceIdx , & h . msg )... ,
1002
+ lnwire .ProduceRecordsSorted (& sourceIdx , & msg )... ,
970
1003
)
1004
+
971
1005
if err != nil {
972
1006
return err
973
1007
}
974
1008
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
+
975
1017
* v = h
976
1018
977
1019
return nil
0 commit comments