@@ -593,7 +593,10 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
593
593
}
594
594
595
595
#[ derive( Clone ) ] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
596
- pub ( super ) enum HTLCFailReason {
596
+ pub ( super ) struct HTLCFailReason ( HTLCFailReasonRepr ) ;
597
+
598
+ #[ derive( Clone ) ] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
599
+ enum HTLCFailReasonRepr {
597
600
LightningError {
598
601
err : msgs:: OnionErrorPacket ,
599
602
} ,
@@ -605,18 +608,29 @@ pub(super) enum HTLCFailReason {
605
608
606
609
impl core:: fmt:: Debug for HTLCFailReason {
607
610
fn fmt ( & self , f : & mut core:: fmt:: Formatter ) -> Result < ( ) , core:: fmt:: Error > {
608
- match self {
609
- HTLCFailReason :: Reason { ref failure_code, .. } => {
611
+ match self . 0 {
612
+ HTLCFailReasonRepr :: Reason { ref failure_code, .. } => {
610
613
write ! ( f, "HTLC error code {}" , failure_code)
611
614
} ,
612
- HTLCFailReason :: LightningError { .. } => {
615
+ HTLCFailReasonRepr :: LightningError { .. } => {
613
616
write ! ( f, "pre-built LightningError" )
614
617
}
615
618
}
616
619
}
617
620
}
618
621
619
- impl_writeable_tlv_based_enum ! ( HTLCFailReason ,
622
+ impl Writeable for HTLCFailReason {
623
+ fn write < W : crate :: util:: ser:: Writer > ( & self , writer : & mut W ) -> Result < ( ) , crate :: io:: Error > {
624
+ self . 0 . write ( writer)
625
+ }
626
+ }
627
+ impl Readable for HTLCFailReason {
628
+ fn read < R : crate :: io:: Read > ( reader : & mut R ) -> Result < Self , crate :: ln:: msgs:: DecodeError > {
629
+ Ok ( Self ( Readable :: read ( reader) ?) )
630
+ }
631
+ }
632
+
633
+ impl_writeable_tlv_based_enum ! ( HTLCFailReasonRepr ,
620
634
( 0 , LightningError ) => {
621
635
( 0 , err, required) ,
622
636
} ,
@@ -628,20 +642,20 @@ impl_writeable_tlv_based_enum!(HTLCFailReason,
628
642
629
643
impl HTLCFailReason {
630
644
pub ( super ) fn reason ( failure_code : u16 , data : Vec < u8 > ) -> Self {
631
- Self :: Reason { failure_code, data }
645
+ Self ( HTLCFailReasonRepr :: Reason { failure_code, data } )
632
646
}
633
647
634
648
pub ( super ) fn from_failure_code ( failure_code : u16 ) -> Self {
635
- Self :: Reason { failure_code, data : Vec :: new ( ) }
649
+ Self ( HTLCFailReasonRepr :: Reason { failure_code, data : Vec :: new ( ) } )
636
650
}
637
651
638
652
pub ( super ) fn from_msg ( msg : & msgs:: UpdateFailHTLC ) -> Self {
639
- Self :: LightningError { err : msg. reason . clone ( ) }
653
+ Self ( HTLCFailReasonRepr :: LightningError { err : msg. reason . clone ( ) } )
640
654
}
641
655
642
656
pub ( super ) fn get_encrypted_failure_packet ( & self , incoming_packet_shared_secret : & [ u8 ; 32 ] , phantom_shared_secret : & Option < [ u8 ; 32 ] > ) -> msgs:: OnionErrorPacket {
643
- match self {
644
- HTLCFailReason :: Reason { ref failure_code, ref data } => {
657
+ match self . 0 {
658
+ HTLCFailReasonRepr :: Reason { ref failure_code, ref data } => {
645
659
if let Some ( phantom_ss) = phantom_shared_secret {
646
660
let phantom_packet = build_failure_packet ( phantom_ss, * failure_code, & data[ ..] ) . encode ( ) ;
647
661
let encrypted_phantom_packet = encrypt_failure_packet ( phantom_ss, & phantom_packet) ;
@@ -651,18 +665,18 @@ impl HTLCFailReason {
651
665
encrypt_failure_packet ( incoming_packet_shared_secret, & packet)
652
666
}
653
667
} ,
654
- HTLCFailReason :: LightningError { err } => {
668
+ HTLCFailReasonRepr :: LightningError { ref err } => {
655
669
encrypt_failure_packet ( incoming_packet_shared_secret, & err. data )
656
670
}
657
671
}
658
672
}
659
673
660
674
pub ( super ) fn decode_onion_failure < T : secp256k1:: Signing , L : Deref > ( & self , secp_ctx : & Secp256k1 < T > , logger : & L , htlc_source : & HTLCSource ) -> ( Option < crate :: routing:: gossip:: NetworkUpdate > , Option < u64 > , bool , Option < u16 > , Option < Vec < u8 > > ) where L :: Target : Logger {
661
- match self {
662
- HTLCFailReason :: LightningError { ref err } => {
675
+ match self . 0 {
676
+ HTLCFailReasonRepr :: LightningError { ref err } => {
663
677
process_onion_failure ( secp_ctx, logger, & htlc_source, err. data . clone ( ) )
664
678
} ,
665
- HTLCFailReason :: Reason { ref failure_code, ref data, .. } => {
679
+ HTLCFailReasonRepr :: Reason { ref failure_code, ref data, .. } => {
666
680
// we get a fail_malformed_htlc from the first hop
667
681
// TODO: We'd like to generate a NetworkUpdate for temporary
668
682
// failures here, but that would be insufficient as find_route
0 commit comments