@@ -38,7 +38,7 @@ use crate::types::payment::{PaymentHash, PaymentPreimage};
38
38
use crate :: ln:: msgs:: DecodeError ;
39
39
use crate :: ln:: channel_keys:: { DelayedPaymentKey , DelayedPaymentBasepoint , HtlcBasepoint , HtlcKey , RevocationKey , RevocationBasepoint } ;
40
40
use crate :: ln:: chan_utils:: { self , CommitmentTransaction , CounterpartyCommitmentSecrets , HTLCOutputInCommitment , HTLCClaim , ChannelTransactionParameters , HolderCommitmentTransaction , TxCreationKeys } ;
41
- use crate :: ln:: channelmanager:: { HTLCSource , SentHTLCId } ;
41
+ use crate :: ln:: channelmanager:: { HTLCSource , SentHTLCId , PaymentClaimDetails } ;
42
42
use crate :: chain;
43
43
use crate :: chain:: { BestBlock , WatchedOutput } ;
44
44
use crate :: chain:: chaininterface:: { BroadcasterInterface , ConfirmationTarget , FeeEstimator , LowerBoundedFeeEstimator } ;
@@ -546,6 +546,9 @@ pub(crate) enum ChannelMonitorUpdateStep {
546
546
} ,
547
547
PaymentPreimage {
548
548
payment_preimage : PaymentPreimage ,
549
+ /// If this preimage was from an inbound payment claim, information about the claim should
550
+ /// be included here to enable claim replay on startup.
551
+ payment_info : Option < PaymentClaimDetails > ,
549
552
} ,
550
553
CommitmentSecret {
551
554
idx : u64 ,
@@ -594,6 +597,7 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
594
597
} ,
595
598
( 2 , PaymentPreimage ) => {
596
599
( 0 , payment_preimage, required) ,
600
+ ( 1 , payment_info, option) ,
597
601
} ,
598
602
( 3 , CommitmentSecret ) => {
599
603
( 0 , idx, required) ,
@@ -919,8 +923,16 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
919
923
/// The set of payment hashes from inbound payments for which we know the preimage. Payment
920
924
/// preimages that are not included in any unrevoked local commitment transaction or unrevoked
921
925
/// remote commitment transactions are automatically removed when commitment transactions are
922
- /// revoked.
923
- payment_preimages : HashMap < PaymentHash , PaymentPreimage > ,
926
+ /// revoked. Note that this happens one revocation after it theoretically could, leaving
927
+ /// preimages present here for the previous state even when the channel is "at rest". This is a
928
+ /// good safety buffer, but also is important as it ensures we retain payment preimages for the
929
+ /// previous local commitment transaction, which may have been broadcast already when we see
930
+ /// the revocation (in setups with redundant monitors).
931
+ ///
932
+ /// We also store [`PaymentClaimDetails`] here, tracking the payment information(s) for this
933
+ /// preimage for inbound payments. This allows us to rebuild the inbound payment information on
934
+ /// startup even if we lost our `ChannelManager`.
935
+ payment_preimages : HashMap < PaymentHash , ( PaymentPreimage , Vec < PaymentClaimDetails > ) > ,
924
936
925
937
// Note that `MonitorEvent`s MUST NOT be generated during update processing, only generated
926
938
// during chain data processing. This prevents a race in `ChainMonitor::update_channel` (and
@@ -1146,7 +1158,7 @@ impl<Signer: EcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signer> {
1146
1158
writer. write_all ( & byte_utils:: be48_to_array ( self . current_holder_commitment_number ) ) ?;
1147
1159
1148
1160
writer. write_all ( & ( self . payment_preimages . len ( ) as u64 ) . to_be_bytes ( ) ) ?;
1149
- for payment_preimage in self . payment_preimages . values ( ) {
1161
+ for ( payment_preimage, _ ) in self . payment_preimages . values ( ) {
1150
1162
writer. write_all ( & payment_preimage. 0 [ ..] ) ?;
1151
1163
}
1152
1164
@@ -1224,6 +1236,7 @@ impl<Signer: EcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signer> {
1224
1236
( 19 , self . channel_id, required) ,
1225
1237
( 21 , self . balances_empty_height, option) ,
1226
1238
( 23 , self . holder_pays_commitment_tx_fee, option) ,
1239
+ ( 25 , self . payment_preimages, required) ,
1227
1240
} ) ;
1228
1241
1229
1242
Ok ( ( ) )
@@ -1488,7 +1501,15 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1488
1501
1489
1502
/// This is used to provide payment preimage(s) out-of-band during startup without updating the
1490
1503
/// off-chain state with a new commitment transaction.
1491
- pub ( crate ) fn provide_payment_preimage < B : Deref , F : Deref , L : Deref > (
1504
+ ///
1505
+ /// It is used only for legacy (created prior to LDK 0.1) pending payments on upgrade, and the
1506
+ /// flow that uses it assumes that this [`ChannelMonitor`] is persisted prior to the
1507
+ /// [`ChannelManager`] being persisted (as the state necessary to call this method again is
1508
+ /// removed from the [`ChannelManager`] and thus a persistence inversion would imply we do not
1509
+ /// get the preimage back into this [`ChannelMonitor`] on startup).
1510
+ ///
1511
+ /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
1512
+ pub ( crate ) fn provide_payment_preimage_unsafe_legacy < B : Deref , F : Deref , L : Deref > (
1492
1513
& self ,
1493
1514
payment_hash : & PaymentHash ,
1494
1515
payment_preimage : & PaymentPreimage ,
@@ -1502,8 +1523,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1502
1523
{
1503
1524
let mut inner = self . inner . lock ( ) . unwrap ( ) ;
1504
1525
let logger = WithChannelMonitor :: from_impl ( logger, & * inner, Some ( * payment_hash) ) ;
1526
+ // Note that we don't pass any MPP claim parts here. This is generally not okay but in this
1527
+ // case is acceptable as we only call this method from `ChannelManager` deserialization in
1528
+ // cases where we are replaying a claim started on a previous version of LDK.
1505
1529
inner. provide_payment_preimage (
1506
- payment_hash, payment_preimage, broadcaster, fee_estimator, & logger)
1530
+ payment_hash, payment_preimage, & None , broadcaster, fee_estimator, & logger)
1507
1531
}
1508
1532
1509
1533
/// Updates a ChannelMonitor on the basis of some new information provided by the Channel
@@ -2194,7 +2218,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2194
2218
outbound_payment,
2195
2219
} ) ;
2196
2220
}
2197
- } else if let Some ( payment_preimage) = self . payment_preimages . get ( & htlc. payment_hash ) {
2221
+ } else if let Some ( ( payment_preimage, _ ) ) = self . payment_preimages . get ( & htlc. payment_hash ) {
2198
2222
// Otherwise (the payment was inbound), only expose it as claimable if
2199
2223
// we know the preimage.
2200
2224
// Note that if there is a pending claim, but it did not use the
@@ -2415,7 +2439,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
2415
2439
outbound_payment,
2416
2440
} ) ;
2417
2441
}
2418
- } else if us. payment_preimages . get ( & htlc. payment_hash ) . is_some ( ) {
2442
+ } else if us. payment_preimages . contains_key ( & htlc. payment_hash ) {
2419
2443
inbound_claiming_htlc_rounded_msat += rounded_value_msat;
2420
2444
if htlc. transaction_output_index . is_some ( ) {
2421
2445
claimable_inbound_htlc_value_sat += htlc. amount_msat / 1000 ;
@@ -2570,7 +2594,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
2570
2594
res
2571
2595
}
2572
2596
2573
- pub ( crate ) fn get_stored_preimages ( & self ) -> HashMap < PaymentHash , PaymentPreimage > {
2597
+ pub ( crate ) fn get_stored_preimages ( & self ) -> HashMap < PaymentHash , ( PaymentPreimage , Vec < PaymentClaimDetails > ) > {
2574
2598
self . inner . lock ( ) . unwrap ( ) . payment_preimages . clone ( )
2575
2599
}
2576
2600
}
@@ -2929,14 +2953,27 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2929
2953
2930
2954
/// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
2931
2955
/// commitment_tx_infos which contain the payment hash have been revoked.
2956
+ ///
2957
+ /// Note that this is often called multiple times for the same payment and must be idempotent.
2932
2958
fn provide_payment_preimage < B : Deref , F : Deref , L : Deref > (
2933
- & mut self , payment_hash : & PaymentHash , payment_preimage : & PaymentPreimage , broadcaster : & B ,
2959
+ & mut self , payment_hash : & PaymentHash , payment_preimage : & PaymentPreimage ,
2960
+ payment_info : & Option < PaymentClaimDetails > , broadcaster : & B ,
2934
2961
fee_estimator : & LowerBoundedFeeEstimator < F > , logger : & WithChannelMonitor < L > )
2935
2962
where B :: Target : BroadcasterInterface ,
2936
2963
F :: Target : FeeEstimator ,
2937
2964
L :: Target : Logger ,
2938
2965
{
2939
- self . payment_preimages . insert ( payment_hash. clone ( ) , payment_preimage. clone ( ) ) ;
2966
+ self . payment_preimages . entry ( payment_hash. clone ( ) )
2967
+ . and_modify ( |( _, payment_infos) | {
2968
+ if let Some ( payment_info) = payment_info {
2969
+ if !payment_infos. contains ( & payment_info) {
2970
+ payment_infos. push ( payment_info. clone ( ) ) ;
2971
+ }
2972
+ }
2973
+ } )
2974
+ . or_insert_with ( || {
2975
+ ( payment_preimage. clone ( ) , payment_info. clone ( ) . into_iter ( ) . collect ( ) )
2976
+ } ) ;
2940
2977
2941
2978
let confirmed_spend_txid = self . funding_spend_confirmed . or_else ( || {
2942
2979
self . onchain_events_awaiting_threshold_conf . iter ( ) . find_map ( |event| match event. event {
@@ -3139,9 +3176,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3139
3176
log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
3140
3177
self . provide_latest_counterparty_commitment_tx ( * commitment_txid, htlc_outputs. clone ( ) , * commitment_number, * their_per_commitment_point, logger)
3141
3178
} ,
3142
- ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage } => {
3179
+ ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage, payment_info } => {
3143
3180
log_trace ! ( logger, "Updating ChannelMonitor with payment preimage" ) ;
3144
- self . provide_payment_preimage ( & PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 [ ..] ) . to_byte_array ( ) ) , & payment_preimage, broadcaster, & bounded_fee_estimator, logger)
3181
+ self . provide_payment_preimage ( & PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 [ ..] ) . to_byte_array ( ) ) , & payment_preimage, payment_info , broadcaster, & bounded_fee_estimator, logger)
3145
3182
} ,
3146
3183
ChannelMonitorUpdateStep :: CommitmentSecret { idx, secret } => {
3147
3184
log_trace ! ( logger, "Updating ChannelMonitor with commitment secret" ) ;
@@ -3593,7 +3630,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3593
3630
return ( claimable_outpoints, to_counterparty_output_info) ;
3594
3631
}
3595
3632
}
3596
- let preimage = if htlc. offered { if let Some ( p ) = self . payment_preimages . get ( & htlc. payment_hash ) { Some ( * p) } else { None } } else { None } ;
3633
+ let preimage = if htlc. offered { if let Some ( ( p , _ ) ) = self . payment_preimages . get ( & htlc. payment_hash ) { Some ( * p) } else { None } } else { None } ;
3597
3634
if preimage. is_some ( ) || !htlc. offered {
3598
3635
let counterparty_htlc_outp = if htlc. offered {
3599
3636
PackageSolvingData :: CounterpartyOfferedHTLCOutput (
@@ -3681,7 +3718,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3681
3718
) ;
3682
3719
( htlc_output, conf_height)
3683
3720
} else {
3684
- let payment_preimage = if let Some ( preimage) = self . payment_preimages . get ( & htlc. payment_hash ) {
3721
+ let payment_preimage = if let Some ( ( preimage, _ ) ) = self . payment_preimages . get ( & htlc. payment_hash ) {
3685
3722
preimage. clone ( )
3686
3723
} else {
3687
3724
// We can't build an HTLC-Success transaction without the preimage
@@ -3835,7 +3872,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3835
3872
for htlc in self . current_holder_commitment_tx . htlc_outputs . iter ( ) {
3836
3873
if let Some ( vout) = htlc. 0 . transaction_output_index {
3837
3874
let preimage = if !htlc. 0 . offered {
3838
- if let Some ( preimage) = self . payment_preimages . get ( & htlc. 0 . payment_hash ) { Some ( preimage. clone ( ) ) } else {
3875
+ if let Some ( ( preimage, _ ) ) = self . payment_preimages . get ( & htlc. 0 . payment_hash ) { Some ( preimage. clone ( ) ) } else {
3839
3876
// We can't build an HTLC-Success transaction without the preimage
3840
3877
continue ;
3841
3878
}
@@ -4808,7 +4845,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4808
4845
for _ in 0 ..payment_preimages_len {
4809
4846
let preimage: PaymentPreimage = Readable :: read ( reader) ?;
4810
4847
let hash = PaymentHash ( Sha256 :: hash ( & preimage. 0 [ ..] ) . to_byte_array ( ) ) ;
4811
- if let Some ( _) = payment_preimages. insert ( hash, preimage) {
4848
+ if let Some ( _) = payment_preimages. insert ( hash, ( preimage, Vec :: new ( ) ) ) {
4812
4849
return Err ( DecodeError :: InvalidValue ) ;
4813
4850
}
4814
4851
}
@@ -4891,6 +4928,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4891
4928
let mut balances_empty_height = None ;
4892
4929
let mut channel_id = None ;
4893
4930
let mut holder_pays_commitment_tx_fee = None ;
4931
+ let mut payment_preimages_with_info: Option < HashMap < _ , _ > > = None ;
4894
4932
read_tlv_fields ! ( reader, {
4895
4933
( 1 , funding_spend_confirmed, option) ,
4896
4934
( 3 , htlcs_resolved_on_chain, optional_vec) ,
@@ -4904,7 +4942,24 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4904
4942
( 19 , channel_id, option) ,
4905
4943
( 21 , balances_empty_height, option) ,
4906
4944
( 23 , holder_pays_commitment_tx_fee, option) ,
4945
+ ( 25 , payment_preimages_with_info, option) ,
4907
4946
} ) ;
4947
+ if let Some ( payment_preimages_with_info) = payment_preimages_with_info {
4948
+ if payment_preimages_with_info. len ( ) != payment_preimages. len ( ) {
4949
+ return Err ( DecodeError :: InvalidValue ) ;
4950
+ }
4951
+ for ( payment_hash, ( payment_preimage, _) ) in payment_preimages. iter ( ) {
4952
+ // Note that because `payment_preimages` is built back from preimages directly,
4953
+ // checking that the two maps have the same hash -> preimage pairs also checks that
4954
+ // the payment hashes in `payment_preimages_with_info`'s preimages match its
4955
+ // hashes.
4956
+ let new_preimage = payment_preimages_with_info. get ( payment_hash) . map ( |( p, _) | p) ;
4957
+ if new_preimage != Some ( payment_preimage) {
4958
+ return Err ( DecodeError :: InvalidValue ) ;
4959
+ }
4960
+ }
4961
+ payment_preimages = payment_preimages_with_info;
4962
+ }
4908
4963
4909
4964
// `HolderForceClosedWithInfo` replaced `HolderForceClosed` in v0.0.122. If we have both
4910
4965
// events, we can remove the `HolderForceClosed` event and just keep the `HolderForceClosedWithInfo`.
@@ -5097,8 +5152,12 @@ mod tests {
5097
5152
assert_eq ! ( replay_update. updates. len( ) , 1 ) ;
5098
5153
if let ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. } = replay_update. updates [ 0 ] {
5099
5154
} else { panic ! ( ) ; }
5100
- replay_update. updates . push ( ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage : payment_preimage_1 } ) ;
5101
- replay_update. updates . push ( ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage : payment_preimage_2 } ) ;
5155
+ replay_update. updates . push ( ChannelMonitorUpdateStep :: PaymentPreimage {
5156
+ payment_preimage : payment_preimage_1, payment_info : None ,
5157
+ } ) ;
5158
+ replay_update. updates . push ( ChannelMonitorUpdateStep :: PaymentPreimage {
5159
+ payment_preimage : payment_preimage_2, payment_info : None ,
5160
+ } ) ;
5102
5161
5103
5162
let broadcaster = TestBroadcaster :: with_blocks ( Arc :: clone ( & nodes[ 1 ] . blocks ) ) ;
5104
5163
assert ! (
@@ -5228,7 +5287,9 @@ mod tests {
5228
5287
preimages_slice_to_htlc_outputs ! ( preimages[ 15 ..20 ] ) , 281474976710654 , dummy_key, & logger) ;
5229
5288
for & ( ref preimage, ref hash) in preimages. iter ( ) {
5230
5289
let bounded_fee_estimator = LowerBoundedFeeEstimator :: new ( & fee_estimator) ;
5231
- monitor. provide_payment_preimage ( hash, preimage, & broadcaster, & bounded_fee_estimator, & logger) ;
5290
+ monitor. provide_payment_preimage_unsafe_legacy (
5291
+ hash, preimage, & broadcaster, & bounded_fee_estimator, & logger
5292
+ ) ;
5232
5293
}
5233
5294
5234
5295
// Now provide a secret, pruning preimages 10-15
0 commit comments