@@ -693,6 +693,20 @@ pub(crate) enum ChannelMonitorUpdateStep {
693
693
RenegotiatedFundingLocked {
694
694
funding_txid : Txid ,
695
695
} ,
696
+ /// When a payment is finally resolved by the user handling an [`Event::PaymentSent`] or
697
+ /// [`Event::PaymentFailed`] event, the `ChannelManager` no longer needs to hear about it on
698
+ /// startup (which would cause it to re-hydrate the payment information even though the user
699
+ /// already learned about the payment's result).
700
+ ///
701
+ /// This will remove the HTLC from [`ChannelMonitor::get_all_current_outbound_htlcs`] and
702
+ /// [`ChannelMonitor::get_onchain_failed_outbound_htlcs`].
703
+ ///
704
+ /// Note that this is only generated for closed channels as this is implicit in the
705
+ /// [`Self::CommitmentSecret`] update which clears the payment information from all un-revoked
706
+ /// counterparty commitment transactions.
707
+ ReleasePaymentComplete {
708
+ htlc : SentHTLCId ,
709
+ } ,
696
710
}
697
711
698
712
impl ChannelMonitorUpdateStep {
@@ -709,6 +723,7 @@ impl ChannelMonitorUpdateStep {
709
723
ChannelMonitorUpdateStep :: ShutdownScript { .. } => "ShutdownScript" ,
710
724
ChannelMonitorUpdateStep :: RenegotiatedFunding { .. } => "RenegotiatedFunding" ,
711
725
ChannelMonitorUpdateStep :: RenegotiatedFundingLocked { .. } => "RenegotiatedFundingLocked" ,
726
+ ChannelMonitorUpdateStep :: ReleasePaymentComplete { .. } => "ReleasePaymentComplete" ,
712
727
}
713
728
}
714
729
}
@@ -747,6 +762,9 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
747
762
( 1 , commitment_txs, required_vec) ,
748
763
( 3 , htlc_data, required) ,
749
764
} ,
765
+ ( 7 , ReleasePaymentComplete ) => {
766
+ ( 1 , htlc, required) ,
767
+ } ,
750
768
( 8 , LatestHolderCommitment ) => {
751
769
( 1 , commitment_txs, required_vec) ,
752
770
( 3 , htlc_data, required) ,
@@ -1288,6 +1306,14 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
1288
1306
/// spending CSV for revocable outputs).
1289
1307
htlcs_resolved_on_chain : Vec < IrrevocablyResolvedHTLC > ,
1290
1308
1309
+ /// When a payment is resolved through an on-chain transaction, we tell the `ChannelManager`
1310
+ /// about this via [`ChannelMonitor::get_onchain_failed_outbound_htlcs`] and
1311
+ /// [`ChannelMonitor::get_all_current_outbound_htlcs`] at startup. We'll keep repeating the
1312
+ /// same payments until they're eventually fully resolved by the user processing a
1313
+ /// `PaymentSent` or `PaymentFailed` event, at which point the `ChannelManager` will inform of
1314
+ /// this and we'll store the set of fully resolved payments here.
1315
+ htlcs_resolved_to_user : HashSet < SentHTLCId > ,
1316
+
1291
1317
/// The set of `SpendableOutput` events which we have already passed upstream to be claimed.
1292
1318
/// These are tracked explicitly to ensure that we don't generate the same events redundantly
1293
1319
/// if users duplicatively confirm old transactions. Specifically for transactions claiming a
@@ -1697,6 +1723,7 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
1697
1723
( 29 , channel_monitor. initial_counterparty_commitment_tx, option) ,
1698
1724
( 31 , channel_monitor. funding. channel_parameters, required) ,
1699
1725
( 32 , channel_monitor. pending_funding, optional_vec) ,
1726
+ ( 33 , channel_monitor. htlcs_resolved_to_user, required) ,
1700
1727
( 34 , channel_monitor. alternative_funding_confirmed, option) ,
1701
1728
} ) ;
1702
1729
@@ -1915,6 +1942,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1915
1942
funding_spend_confirmed : None ,
1916
1943
confirmed_commitment_tx_counterparty_output : None ,
1917
1944
htlcs_resolved_on_chain : Vec :: new ( ) ,
1945
+ htlcs_resolved_to_user : new_hash_set ( ) ,
1918
1946
spendable_txids_confirmed : Vec :: new ( ) ,
1919
1947
1920
1948
best_block,
@@ -3074,10 +3102,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
3074
3102
/// Gets the set of outbound HTLCs which can be (or have been) resolved by this
3075
3103
/// `ChannelMonitor`. This is used to determine if an HTLC was removed from the channel prior
3076
3104
/// to the `ChannelManager` having been persisted.
3077
- ///
3078
- /// This is similar to [`Self::get_pending_or_resolved_outbound_htlcs`] except it includes
3079
- /// HTLCs which were resolved on-chain (i.e. where the final HTLC resolution was done by an
3080
- /// event from this `ChannelMonitor`).
3081
3105
pub ( crate ) fn get_all_current_outbound_htlcs (
3082
3106
& self ,
3083
3107
) -> HashMap < HTLCSource , ( HTLCOutputInCommitment , Option < PaymentPreimage > ) > {
@@ -3090,8 +3114,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
3090
3114
for & ( ref htlc, ref source_option) in latest_outpoints. iter ( ) {
3091
3115
if let & Some ( ref source) = source_option {
3092
3116
let htlc_id = SentHTLCId :: from_source ( source) ;
3093
- let preimage_opt = us. counterparty_fulfilled_htlcs . get ( & htlc_id) . cloned ( ) ;
3094
- res. insert ( ( * * source) . clone ( ) , ( htlc. clone ( ) , preimage_opt) ) ;
3117
+ if !us. htlcs_resolved_to_user . contains ( & htlc_id) {
3118
+ let preimage_opt =
3119
+ us. counterparty_fulfilled_htlcs . get ( & htlc_id) . cloned ( ) ;
3120
+ res. insert ( ( * * source) . clone ( ) , ( htlc. clone ( ) , preimage_opt) ) ;
3121
+ }
3095
3122
}
3096
3123
}
3097
3124
}
@@ -3147,6 +3174,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
3147
3174
} else {
3148
3175
continue ;
3149
3176
} ;
3177
+ let htlc_id = SentHTLCId :: from_source( source) ;
3178
+ if us. htlcs_resolved_to_user. contains( & htlc_id) {
3179
+ continue ;
3180
+ }
3181
+
3150
3182
let confirmed = $htlc_iter. find( |( _, conf_src) | Some ( source) == * conf_src) ;
3151
3183
if let Some ( ( confirmed_htlc, _) ) = confirmed {
3152
3184
let filter = |v: &&IrrevocablyResolvedHTLC | {
@@ -3219,96 +3251,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
3219
3251
res
3220
3252
}
3221
3253
3222
- /// Gets the set of outbound HTLCs which are pending resolution in this channel or which were
3223
- /// resolved with a preimage from our counterparty.
3224
- ///
3225
- /// This is used to reconstruct pending outbound payments on restart in the ChannelManager.
3226
- ///
3227
- /// Currently, the preimage is unused, however if it is present in the relevant internal state
3228
- /// an HTLC is always included even if it has been resolved.
3229
- #[ rustfmt:: skip]
3230
- pub ( crate ) fn get_pending_or_resolved_outbound_htlcs ( & self ) -> HashMap < HTLCSource , ( HTLCOutputInCommitment , Option < PaymentPreimage > ) > {
3231
- let us = self . inner . lock ( ) . unwrap ( ) ;
3232
- // We're only concerned with the confirmation count of HTLC transactions, and don't
3233
- // actually care how many confirmations a commitment transaction may or may not have. Thus,
3234
- // we look for either a FundingSpendConfirmation event or a funding_spend_confirmed.
3235
- let confirmed_txid = us. funding_spend_confirmed . or_else ( || {
3236
- us. onchain_events_awaiting_threshold_conf . iter ( ) . find_map ( |event| {
3237
- if let OnchainEvent :: FundingSpendConfirmation { .. } = event. event {
3238
- Some ( event. txid )
3239
- } else { None }
3240
- } )
3241
- } ) ;
3242
-
3243
- if confirmed_txid. is_none ( ) {
3244
- // If we have not seen a commitment transaction on-chain (ie the channel is not yet
3245
- // closed), just get the full set.
3246
- mem:: drop ( us) ;
3247
- return self . get_all_current_outbound_htlcs ( ) ;
3248
- }
3249
-
3250
- let mut res = new_hash_map ( ) ;
3251
- macro_rules! walk_htlcs {
3252
- ( $holder_commitment: expr, $htlc_iter: expr) => {
3253
- for ( htlc, source) in $htlc_iter {
3254
- if us. htlcs_resolved_on_chain. iter( ) . any( |v| v. commitment_tx_output_idx == htlc. transaction_output_index) {
3255
- // We should assert that funding_spend_confirmed is_some() here, but we
3256
- // have some unit tests which violate HTLC transaction CSVs entirely and
3257
- // would fail.
3258
- // TODO: Once tests all connect transactions at consensus-valid times, we
3259
- // should assert here like we do in `get_claimable_balances`.
3260
- } else if htlc. offered == $holder_commitment {
3261
- // If the payment was outbound, check if there's an HTLCUpdate
3262
- // indicating we have spent this HTLC with a timeout, claiming it back
3263
- // and awaiting confirmations on it.
3264
- let htlc_update_confd = us. onchain_events_awaiting_threshold_conf. iter( ) . any( |event| {
3265
- if let OnchainEvent :: HTLCUpdate { commitment_tx_output_idx: Some ( commitment_tx_output_idx) , .. } = event. event {
3266
- // If the HTLC was timed out, we wait for ANTI_REORG_DELAY blocks
3267
- // before considering it "no longer pending" - this matches when we
3268
- // provide the ChannelManager an HTLC failure event.
3269
- Some ( commitment_tx_output_idx) == htlc. transaction_output_index &&
3270
- us. best_block. height >= event. height + ANTI_REORG_DELAY - 1
3271
- } else if let OnchainEvent :: HTLCSpendConfirmation { commitment_tx_output_idx, .. } = event. event {
3272
- // If the HTLC was fulfilled with a preimage, we consider the HTLC
3273
- // immediately non-pending, matching when we provide ChannelManager
3274
- // the preimage.
3275
- Some ( commitment_tx_output_idx) == htlc. transaction_output_index
3276
- } else { false }
3277
- } ) ;
3278
- if let Some ( source) = source {
3279
- let counterparty_resolved_preimage_opt =
3280
- us. counterparty_fulfilled_htlcs. get( & SentHTLCId :: from_source( source) ) . cloned( ) ;
3281
- if !htlc_update_confd || counterparty_resolved_preimage_opt. is_some( ) {
3282
- res. insert( source. clone( ) , ( htlc. clone( ) , counterparty_resolved_preimage_opt) ) ;
3283
- }
3284
- } else {
3285
- panic!( "Outbound HTLCs should have a source" ) ;
3286
- }
3287
- }
3288
- }
3289
- }
3290
- }
3291
-
3292
- let commitment_txid = confirmed_txid. unwrap ( ) ;
3293
- let funding_spent = get_confirmed_funding_scope ! ( us) ;
3294
-
3295
- if Some ( commitment_txid) == funding_spent. current_counterparty_commitment_txid || Some ( commitment_txid) == funding_spent. prev_counterparty_commitment_txid {
3296
- walk_htlcs ! ( false , funding_spent. counterparty_claimable_outpoints. get( & commitment_txid) . unwrap( ) . iter( ) . filter_map( |( a, b) | {
3297
- if let & Some ( ref source) = b {
3298
- Some ( ( a, Some ( & * * source) ) )
3299
- } else { None }
3300
- } ) ) ;
3301
- } else if commitment_txid == funding_spent. current_holder_commitment_tx . trust ( ) . txid ( ) {
3302
- walk_htlcs ! ( true , holder_commitment_htlcs!( us, CURRENT_WITH_SOURCES ) ) ;
3303
- } else if let Some ( prev_commitment_tx) = & funding_spent. prev_holder_commitment_tx {
3304
- if commitment_txid == prev_commitment_tx. trust ( ) . txid ( ) {
3305
- walk_htlcs ! ( true , holder_commitment_htlcs!( us, PREV_WITH_SOURCES ) . unwrap( ) ) ;
3306
- }
3307
- }
3308
-
3309
- res
3310
- }
3311
-
3312
3254
pub ( crate ) fn get_stored_preimages (
3313
3255
& self ,
3314
3256
) -> HashMap < PaymentHash , ( PaymentPreimage , Vec < PaymentClaimDetails > ) > {
@@ -4195,6 +4137,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
4195
4137
if updates. update_id == LEGACY_CLOSED_CHANNEL_UPDATE_ID || self . lockdown_from_offchain {
4196
4138
assert_eq ! ( updates. updates. len( ) , 1 ) ;
4197
4139
match updates. updates [ 0 ] {
4140
+ ChannelMonitorUpdateStep :: ReleasePaymentComplete { .. } => { } ,
4198
4141
ChannelMonitorUpdateStep :: ChannelForceClosed { .. } => { } ,
4199
4142
// We should have already seen a `ChannelForceClosed` update if we're trying to
4200
4143
// provide a preimage at this point.
@@ -4322,6 +4265,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
4322
4265
panic ! ( "Attempted to replace shutdown script {} with {}" , shutdown_script, scriptpubkey) ;
4323
4266
}
4324
4267
} ,
4268
+ ChannelMonitorUpdateStep :: ReleasePaymentComplete { htlc } => {
4269
+ log_trace ! ( logger, "HTLC {htlc:?} permanently and fully resolved" ) ;
4270
+ self . htlcs_resolved_to_user . insert ( * htlc) ;
4271
+ } ,
4325
4272
}
4326
4273
}
4327
4274
@@ -4347,11 +4294,13 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
4347
4294
|ChannelMonitorUpdateStep :: RenegotiatedFundingLocked { .. } =>
4348
4295
is_pre_close_update = true ,
4349
4296
// After a channel is closed, we don't communicate with our peer about it, so the
4350
- // only things we will update is getting a new preimage (from a different channel)
4351
- // or being told that the channel is closed. All other updates are generated while
4352
- // talking to our peer.
4297
+ // only things we will update is getting a new preimage (from a different channel),
4298
+ // being told that the channel is closed, or being told a payment which was
4299
+ // resolved on-chain has had its resolution communicated to the user. All other
4300
+ // updates are generated while talking to our peer.
4353
4301
ChannelMonitorUpdateStep :: PaymentPreimage { .. } => { } ,
4354
4302
ChannelMonitorUpdateStep :: ChannelForceClosed { .. } => { } ,
4303
+ ChannelMonitorUpdateStep :: ReleasePaymentComplete { .. } => { } ,
4355
4304
}
4356
4305
}
4357
4306
@@ -6447,6 +6396,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
6447
6396
6448
6397
let mut funding_spend_confirmed = None ;
6449
6398
let mut htlcs_resolved_on_chain = Some ( Vec :: new ( ) ) ;
6399
+ let mut htlcs_resolved_to_user = Some ( new_hash_set ( ) ) ;
6450
6400
let mut funding_spend_seen = Some ( false ) ;
6451
6401
let mut counterparty_node_id = None ;
6452
6402
let mut confirmed_commitment_tx_counterparty_output = None ;
@@ -6480,6 +6430,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
6480
6430
( 29 , initial_counterparty_commitment_tx, option) ,
6481
6431
( 31 , channel_parameters, ( option: ReadableArgs , None ) ) ,
6482
6432
( 32 , pending_funding, optional_vec) ,
6433
+ ( 33 , htlcs_resolved_to_user, option) ,
6483
6434
( 34 , alternative_funding_confirmed, option) ,
6484
6435
} ) ;
6485
6436
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
@@ -6642,6 +6593,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
6642
6593
funding_spend_confirmed,
6643
6594
confirmed_commitment_tx_counterparty_output,
6644
6595
htlcs_resolved_on_chain : htlcs_resolved_on_chain. unwrap ( ) ,
6596
+ htlcs_resolved_to_user : htlcs_resolved_to_user. unwrap ( ) ,
6645
6597
spendable_txids_confirmed : spendable_txids_confirmed. unwrap ( ) ,
6646
6598
6647
6599
best_block,
0 commit comments