@@ -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 ) ) ) ,
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
@@ -3908,7 +3961,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
39083961 if let Some ( ( htlc, source) ) = htlc_opt {
39093962 self . push_monitor_event ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
39103963 payment_hash : htlc. payment_hash ,
3911- payment_preimage : Some ( claim. preimage ) ,
3964+ resolution : OutboundHTLCResolution :: Claimed {
3965+ preimage : claim. preimage ,
3966+ skimmed_fee_msat : claim. skimmed_fee_msat ,
3967+ } ,
39123968 source : * source. clone ( ) ,
39133969 htlc_value_msat : Some ( htlc. amount_msat ) ,
39143970 user_channel_id : self . user_channel_id ,
@@ -4645,7 +4701,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
46454701 & mut self . pending_monitor_events ,
46464702 MonitorEvent :: HTLCEvent ( HTLCUpdate {
46474703 payment_hash,
4648- payment_preimage : None ,
4704+ resolution : OutboundHTLCResolution :: on_chain_timeout ( ) ,
46494705 source : source. clone ( ) ,
46504706 htlc_value_msat : Some ( amount_msat) ,
46514707 user_channel_id : self . user_channel_id ,
@@ -5983,7 +6039,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
59836039 & payment_hash, entry. txid) ;
59846040 self . push_monitor_event ( MonitorEvent :: HTLCEvent ( HTLCUpdate {
59856041 payment_hash,
5986- payment_preimage : None ,
6042+ resolution : OutboundHTLCResolution :: on_chain_timeout ( ) ,
59876043 source,
59886044 htlc_value_msat : htlc_value_satoshis. map ( |v| v * 1000 ) ,
59896045 user_channel_id : self . user_channel_id ,
@@ -6094,7 +6150,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
60946150 expires soon at {}", log_bytes!( htlc. payment_hash. 0 ) , inbound_htlc_expiry) ;
60956151 push_monitor_event ( & mut self . pending_monitor_events , MonitorEvent :: HTLCEvent ( HTLCUpdate {
60966152 source : source. clone ( ) ,
6097- payment_preimage : None ,
6153+ resolution : OutboundHTLCResolution :: on_chain_timeout ( ) ,
60986154 payment_hash : htlc. payment_hash ,
60996155 htlc_value_msat : Some ( htlc. amount_msat ) ,
61006156 user_channel_id : self . user_channel_id ,
@@ -6512,7 +6568,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
65126568 self . counterparty_fulfilled_htlcs . insert ( SentHTLCId :: from_source ( & source) , payment_preimage) ;
65136569 push_monitor_event ( & mut self . pending_monitor_events , MonitorEvent :: HTLCEvent ( HTLCUpdate {
65146570 source,
6515- payment_preimage : Some ( payment_preimage) ,
6571+ resolution : OutboundHTLCResolution :: Claimed { preimage : payment_preimage, skimmed_fee_msat : None } ,
65166572 payment_hash,
65176573 htlc_value_msat : Some ( amount_msat) ,
65186574 user_channel_id : self . user_channel_id ,
@@ -6537,7 +6593,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
65376593 self . counterparty_fulfilled_htlcs . insert ( SentHTLCId :: from_source ( & source) , payment_preimage) ;
65386594 push_monitor_event ( & mut self . pending_monitor_events , MonitorEvent :: HTLCEvent ( HTLCUpdate {
65396595 source,
6540- payment_preimage : Some ( payment_preimage) ,
6596+ resolution : OutboundHTLCResolution :: Claimed { preimage : payment_preimage, skimmed_fee_msat : None } ,
65416597 payment_hash,
65426598 htlc_value_msat : Some ( amount_msat) ,
65436599 user_channel_id : self . user_channel_id ,
0 commit comments