@@ -49,6 +49,7 @@ use crate::ln::features::InvoiceFeatures;
4949use crate :: routing:: router:: { InFlightHtlcs , PaymentParameters , Route , RouteHop , RoutePath , RouteParameters } ;
5050use crate :: ln:: msgs;
5151use crate :: ln:: onion_utils;
52+ use crate :: ln:: onion_utils:: HTLCFailReason ;
5253use crate :: ln:: msgs:: { ChannelMessageHandler , DecodeError , LightningError , MAX_VALUE_MSAT } ;
5354use crate :: ln:: wire:: Encode ;
5455use crate :: chain:: keysinterface:: { Sign , KeysInterface , KeysManager , Recipient } ;
@@ -276,82 +277,6 @@ impl HTLCSource {
276277 }
277278}
278279
279- #[ derive( Clone ) ] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
280- pub ( super ) enum HTLCFailReason {
281- LightningError {
282- err : msgs:: OnionErrorPacket ,
283- } ,
284- Reason {
285- failure_code : u16 ,
286- data : Vec < u8 > ,
287- }
288- }
289-
290- impl core:: fmt:: Debug for HTLCFailReason {
291- fn fmt ( & self , f : & mut core:: fmt:: Formatter ) -> Result < ( ) , core:: fmt:: Error > {
292- match self {
293- HTLCFailReason :: Reason { ref failure_code, .. } => {
294- write ! ( f, "HTLC error code {}" , failure_code)
295- } ,
296- HTLCFailReason :: LightningError { .. } => {
297- write ! ( f, "pre-built LightningError" )
298- }
299- }
300- }
301- }
302-
303- impl HTLCFailReason {
304- pub ( super ) fn reason ( failure_code : u16 , data : Vec < u8 > ) -> Self {
305- Self :: Reason { failure_code, data }
306- }
307-
308- pub ( super ) fn from_failure_code ( failure_code : u16 ) -> Self {
309- Self :: Reason { failure_code, data : Vec :: new ( ) }
310- }
311-
312- pub ( super ) fn from_msg ( msg : & msgs:: UpdateFailHTLC ) -> Self {
313- Self :: LightningError { err : msg. reason . clone ( ) }
314- }
315-
316- fn get_encrypted_failure_packet ( & self , incoming_packet_shared_secret : & [ u8 ; 32 ] , phantom_shared_secret : & Option < [ u8 ; 32 ] > ) -> msgs:: OnionErrorPacket {
317- match self {
318- HTLCFailReason :: Reason { ref failure_code, ref data } => {
319- if let Some ( phantom_ss) = phantom_shared_secret {
320- let phantom_packet = onion_utils:: build_failure_packet ( phantom_ss, * failure_code, & data[ ..] ) . encode ( ) ;
321- let encrypted_phantom_packet = onion_utils:: encrypt_failure_packet ( phantom_ss, & phantom_packet) ;
322- onion_utils:: encrypt_failure_packet ( incoming_packet_shared_secret, & encrypted_phantom_packet. data [ ..] )
323- } else {
324- let packet = onion_utils:: build_failure_packet ( incoming_packet_shared_secret, * failure_code, & data[ ..] ) . encode ( ) ;
325- onion_utils:: encrypt_failure_packet ( incoming_packet_shared_secret, & packet)
326- }
327- } ,
328- HTLCFailReason :: LightningError { err } => {
329- onion_utils:: encrypt_failure_packet ( incoming_packet_shared_secret, & err. data )
330- }
331- }
332- }
333-
334- 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 {
335- match self {
336- HTLCFailReason :: LightningError { ref err } => {
337- onion_utils:: process_onion_failure ( secp_ctx, logger, & htlc_source, err. data . clone ( ) )
338- } ,
339- HTLCFailReason :: Reason { ref failure_code, ref data, .. } => {
340- // we get a fail_malformed_htlc from the first hop
341- // TODO: We'd like to generate a NetworkUpdate for temporary
342- // failures here, but that would be insufficient as find_route
343- // generally ignores its view of our own channels as we provide them via
344- // ChannelDetails.
345- // TODO: For non-temporary failures, we really should be closing the
346- // channel here as we apparently can't relay through them anyway.
347- if let & HTLCSource :: OutboundRoute { ref path, .. } = htlc_source {
348- ( None , Some ( path. first ( ) . unwrap ( ) . short_channel_id ) , true , Some ( * failure_code) , Some ( data. clone ( ) ) )
349- } else { unreachable ! ( ) ; }
350- }
351- }
352- }
353- }
354-
355280struct ReceiveError {
356281 err_code : u16 ,
357282 err_data : Vec < u8 > ,
@@ -7031,16 +6956,6 @@ impl Writeable for HTLCSource {
70316956 }
70326957}
70336958
7034- impl_writeable_tlv_based_enum ! ( HTLCFailReason ,
7035- ( 0 , LightningError ) => {
7036- ( 0 , err, required) ,
7037- } ,
7038- ( 1 , Reason ) => {
7039- ( 0 , failure_code, required) ,
7040- ( 2 , data, vec_type) ,
7041- } ,
7042- ; ) ;
7043-
70446959impl_writeable_tlv_based ! ( PendingAddHTLCInfo , {
70456960 ( 0 , forward_info, required) ,
70466961 ( 1 , prev_user_channel_id, ( default_value, 0 ) ) ,
0 commit comments