@@ -56,6 +56,7 @@ use crate::ln::channel_keys::{
5656} ;
5757use crate :: ln:: channelmanager:: { HTLCSource , PaymentClaimDetails , SentHTLCId } ;
5858use crate :: ln:: msgs:: DecodeError ;
59+ use crate :: ln:: onion_utils:: { HTLCFailReason , LocalHTLCFailureReason } ;
5960use crate :: ln:: types:: ChannelId ;
6061use crate :: sign:: {
6162 ecdsa:: EcdsaChannelSigner , ChannelDerivationParameters , DelayedPaymentOutputDescriptor ,
@@ -252,24 +253,76 @@ impl_writeable_tlv_based_enum_upgradable_legacy!(MonitorEvent,
252253 // 6 was `UpdateFailed` until LDK 0.0.117
253254) ;
254255
256+ /// The resolution of an outbound HTLC — either claimed with a preimage or failed back. Used in
257+ /// [`MonitorEvent::HTLCEvent`]s to provide resolution data to the `ChannelManager`.
258+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
259+ pub ( crate ) enum OutboundHTLCResolution {
260+ Claimed {
261+ preimage : PaymentPreimage ,
262+ skimmed_fee_msat : Option < u64 > ,
263+ } ,
264+ /// The failure resolution may come from on-chain in the [`ChannelMonitor`] or off-chain from the
265+ /// `ChannelManager`, such as from an outbound edge to provide the manager with a resolution for
266+ /// the inbound edge.
267+ Failed {
268+ reason : HTLCFailReason ,
269+ } ,
270+ }
271+
272+ impl OutboundHTLCResolution {
273+ /// Returns an on-chain timeout failure resolution.
274+ pub fn on_chain_timeout ( ) -> Self {
275+ OutboundHTLCResolution :: Failed {
276+ reason : HTLCFailReason :: from_failure_code ( LocalHTLCFailureReason :: OnChainTimeout ) ,
277+ }
278+ }
279+ }
280+
281+ impl_writeable_tlv_based_enum ! ( OutboundHTLCResolution ,
282+ ( 1 , Claimed ) => {
283+ ( 1 , preimage, required) ,
284+ ( 3 , skimmed_fee_msat, option) ,
285+ } ,
286+ ( 3 , Failed ) => {
287+ ( 1 , reason, required) ,
288+ } ,
289+ ) ;
290+
255291/// Simple structure sent back by `chain::Watch` when an HTLC from a forward channel is detected on
256292/// chain. Used to update the corresponding HTLC in the backward channel. Failing to pass the
257293/// preimage claim backward will lead to loss of funds.
258294#[ derive( Clone , PartialEq , Eq ) ]
259295pub struct HTLCUpdate {
260296 pub ( crate ) payment_hash : PaymentHash ,
261- pub ( crate ) payment_preimage : Option < PaymentPreimage > ,
262297 pub ( crate ) source : HTLCSource ,
263298 pub ( crate ) htlc_value_msat : Option < u64 > ,
264299 pub ( crate ) user_channel_id : Option < u128 > ,
300+ pub ( crate ) resolution : OutboundHTLCResolution ,
265301}
266302impl_writeable_tlv_based ! ( HTLCUpdate , {
267303 ( 0 , payment_hash, required) ,
268304 ( 1 , htlc_value_satoshis, ( legacy, u64 , |_| Ok ( ( ) ) , |us: & HTLCUpdate | us. htlc_value_msat. map( |v| v / 1000 ) ) ) ,
269305 ( 2 , source, required) ,
270- ( 4 , payment_preimage, option) ,
306+ ( 4 , _legacy_payment_preimage, ( legacy, Option <PaymentPreimage >,
307+ |v: Option <& Option <PaymentPreimage >>| {
308+ // Only use the legacy preimage if the new `resolution` TLV (9) wasn't read,
309+ // otherwise we'd clobber it (losing e.g. skimmed_fee_msat).
310+ if resolution. 0 . is_none( ) {
311+ if let Some ( Some ( preimage) ) = v {
312+ resolution = crate :: util:: ser:: RequiredWrapper ( Some (
313+ OutboundHTLCResolution :: Claimed { preimage: * preimage, skimmed_fee_msat: None }
314+ ) ) ;
315+ }
316+ }
317+ Ok ( ( ) )
318+ } ,
319+ |us: & HTLCUpdate | match & us. resolution {
320+ OutboundHTLCResolution :: Claimed { preimage, .. } => Some ( Some ( * preimage) ) ,
321+ _ => None ,
322+ } ) ) ,
271323 ( 5 , user_channel_id, option) ,
272324 ( 7 , htlc_value_msat, ( default_value, htlc_value_satoshis. map( |v: u64 | v * 1000 ) ) ) , // Added in 0.4
325+ ( 9 , resolution, ( default_value, OutboundHTLCResolution :: on_chain_timeout( ) ) ) ,
273326} ) ;
274327
275328/// If an output goes from claimable only by us to claimable by us or our counterparty within this
@@ -3910,7 +3963,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
39103963 // startup until it is explicitly acked.
39113964 self . push_monitor_event ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
39123965 payment_hash : htlc. payment_hash ,
3913- payment_preimage : Some ( claim. preimage ) ,
3966+ resolution : OutboundHTLCResolution :: Claimed {
3967+ preimage : claim. preimage ,
3968+ skimmed_fee_msat : claim. skimmed_fee_msat ,
3969+ } ,
39143970 source : * source. clone ( ) ,
39153971 htlc_value_msat : Some ( htlc. amount_msat ) ,
39163972 user_channel_id : self . user_channel_id ,
@@ -4647,7 +4703,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
46474703 & mut self . pending_monitor_events ,
46484704 MonitorEvent :: HTLCEvent ( HTLCUpdate {
46494705 payment_hash,
4650- payment_preimage : None ,
4706+ resolution : OutboundHTLCResolution :: on_chain_timeout ( ) ,
46514707 source : source. clone ( ) ,
46524708 htlc_value_msat : Some ( amount_msat) ,
46534709 user_channel_id : self . user_channel_id ,
@@ -5985,7 +6041,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
59856041 & payment_hash, entry. txid) ;
59866042 self . push_monitor_event ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
59876043 payment_hash,
5988- payment_preimage : None ,
6044+ resolution : OutboundHTLCResolution :: on_chain_timeout ( ) ,
59896045 source,
59906046 htlc_value_msat : htlc_value_satoshis. map ( |v| v * 1000 ) ,
59916047 user_channel_id : self . user_channel_id ,
@@ -6096,7 +6152,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
60966152 expires soon at {}", log_bytes!( htlc. payment_hash. 0 ) , inbound_htlc_expiry) ;
60976153 push_monitor_event ( & mut self . pending_monitor_events , MonitorEvent :: HTLCEvent ( HTLCUpdate {
60986154 source : source. clone ( ) ,
6099- payment_preimage : None ,
6155+ resolution : OutboundHTLCResolution :: on_chain_timeout ( ) ,
61006156 payment_hash : htlc. payment_hash ,
61016157 htlc_value_msat : Some ( htlc. amount_msat ) ,
61026158 user_channel_id : self . user_channel_id ,
@@ -6514,7 +6570,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
65146570 self . counterparty_fulfilled_htlcs . insert ( SentHTLCId :: from_source ( & source) , payment_preimage) ;
65156571 push_monitor_event ( & mut self . pending_monitor_events , MonitorEvent :: HTLCEvent ( HTLCUpdate {
65166572 source,
6517- payment_preimage : Some ( payment_preimage) ,
6573+ resolution : OutboundHTLCResolution :: Claimed { preimage : payment_preimage, skimmed_fee_msat : None } ,
65186574 payment_hash,
65196575 htlc_value_msat : Some ( amount_msat) ,
65206576 user_channel_id : self . user_channel_id ,
@@ -6539,7 +6595,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
65396595 self . counterparty_fulfilled_htlcs . insert ( SentHTLCId :: from_source ( & source) , payment_preimage) ;
65406596 push_monitor_event ( & mut self . pending_monitor_events , MonitorEvent :: HTLCEvent ( HTLCUpdate {
65416597 source,
6542- payment_preimage : Some ( payment_preimage) ,
6598+ resolution : OutboundHTLCResolution :: Claimed { preimage : payment_preimage, skimmed_fee_msat : None } ,
65436599 payment_hash,
65446600 htlc_value_msat : Some ( amount_msat) ,
65456601 user_channel_id : self . user_channel_id ,
0 commit comments