@@ -924,7 +924,7 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
924
924
/// preimages that are not included in any unrevoked local commitment transaction or unrevoked
925
925
/// remote commitment transactions are automatically removed when commitment transactions are
926
926
/// revoked.
927
- payment_preimages : HashMap < PaymentHash , PaymentPreimage > ,
927
+ payment_preimages : HashMap < PaymentHash , ( PaymentPreimage , Vec < PaymentClaimDetails > ) > ,
928
928
929
929
// Note that `MonitorEvent`s MUST NOT be generated during update processing, only generated
930
930
// during chain data processing. This prevents a race in `ChainMonitor::update_channel` (and
@@ -1150,7 +1150,7 @@ impl<Signer: EcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signer> {
1150
1150
writer. write_all ( & byte_utils:: be48_to_array ( self . current_holder_commitment_number ) ) ?;
1151
1151
1152
1152
writer. write_all ( & ( self . payment_preimages . len ( ) as u64 ) . to_be_bytes ( ) ) ?;
1153
- for payment_preimage in self . payment_preimages . values ( ) {
1153
+ for ( payment_preimage, _ ) in self . payment_preimages . values ( ) {
1154
1154
writer. write_all ( & payment_preimage. 0 [ ..] ) ?;
1155
1155
}
1156
1156
@@ -1228,6 +1228,7 @@ impl<Signer: EcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signer> {
1228
1228
( 19 , self . channel_id, required) ,
1229
1229
( 21 , self . balances_empty_height, option) ,
1230
1230
( 23 , self . holder_pays_commitment_tx_fee, option) ,
1231
+ ( 25 , self . payment_preimages, required) ,
1231
1232
} ) ;
1232
1233
1233
1234
Ok ( ( ) )
@@ -2201,7 +2202,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2201
2202
outbound_payment,
2202
2203
} ) ;
2203
2204
}
2204
- } else if let Some ( payment_preimage) = self . payment_preimages . get ( & htlc. payment_hash ) {
2205
+ } else if let Some ( ( payment_preimage, _ ) ) = self . payment_preimages . get ( & htlc. payment_hash ) {
2205
2206
// Otherwise (the payment was inbound), only expose it as claimable if
2206
2207
// we know the preimage.
2207
2208
// Note that if there is a pending claim, but it did not use the
@@ -2422,7 +2423,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
2422
2423
outbound_payment,
2423
2424
} ) ;
2424
2425
}
2425
- } else if us. payment_preimages . get ( & htlc. payment_hash ) . is_some ( ) {
2426
+ } else if us. payment_preimages . contains_key ( & htlc. payment_hash ) {
2426
2427
inbound_claiming_htlc_rounded_msat += rounded_value_msat;
2427
2428
if htlc. transaction_output_index . is_some ( ) {
2428
2429
claimable_inbound_htlc_value_sat += htlc. amount_msat / 1000 ;
@@ -2577,7 +2578,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
2577
2578
res
2578
2579
}
2579
2580
2580
- pub ( crate ) fn get_stored_preimages ( & self ) -> HashMap < PaymentHash , PaymentPreimage > {
2581
+ pub ( crate ) fn get_stored_preimages ( & self ) -> HashMap < PaymentHash , ( PaymentPreimage , Vec < PaymentClaimDetails > ) > {
2581
2582
self . inner . lock ( ) . unwrap ( ) . payment_preimages . clone ( )
2582
2583
}
2583
2584
}
@@ -2936,6 +2937,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2936
2937
2937
2938
/// Provides a payment_hash->payment_preimage mapping. Will be automatically pruned when all
2938
2939
/// commitment_tx_infos which contain the payment hash have been revoked.
2940
+ ///
2941
+ /// Note that this is often called multiple times for the same payment and must be idempotent.
2939
2942
fn provide_payment_preimage < B : Deref , F : Deref , L : Deref > (
2940
2943
& mut self , payment_hash : & PaymentHash , payment_preimage : & PaymentPreimage ,
2941
2944
payment_info : & Option < PaymentClaimDetails > , broadcaster : & B ,
@@ -2944,8 +2947,17 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2944
2947
F :: Target : FeeEstimator ,
2945
2948
L :: Target : Logger ,
2946
2949
{
2947
- // TODO: Store payment_info (but do not override any existing values)
2948
- self . payment_preimages . insert ( payment_hash. clone ( ) , payment_preimage. clone ( ) ) ;
2950
+ self . payment_preimages . entry ( payment_hash. clone ( ) )
2951
+ . and_modify ( |( _, payment_infos) | {
2952
+ if let Some ( payment_info) = payment_info {
2953
+ if !payment_infos. contains ( & payment_info) {
2954
+ payment_infos. push ( payment_info. clone ( ) ) ;
2955
+ }
2956
+ }
2957
+ } )
2958
+ . or_insert_with ( || {
2959
+ ( payment_preimage. clone ( ) , payment_info. clone ( ) . into_iter ( ) . collect ( ) )
2960
+ } ) ;
2949
2961
2950
2962
let confirmed_spend_txid = self . funding_spend_confirmed . or_else ( || {
2951
2963
self . onchain_events_awaiting_threshold_conf . iter ( ) . find_map ( |event| match event. event {
@@ -3602,7 +3614,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3602
3614
return ( claimable_outpoints, to_counterparty_output_info) ;
3603
3615
}
3604
3616
}
3605
- let preimage = if htlc. offered { if let Some ( p ) = self . payment_preimages . get ( & htlc. payment_hash ) { Some ( * p) } else { None } } else { None } ;
3617
+ let preimage = if htlc. offered { if let Some ( ( p , _ ) ) = self . payment_preimages . get ( & htlc. payment_hash ) { Some ( * p) } else { None } } else { None } ;
3606
3618
if preimage. is_some ( ) || !htlc. offered {
3607
3619
let counterparty_htlc_outp = if htlc. offered {
3608
3620
PackageSolvingData :: CounterpartyOfferedHTLCOutput (
@@ -3690,7 +3702,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3690
3702
) ;
3691
3703
( htlc_output, conf_height)
3692
3704
} else {
3693
- let payment_preimage = if let Some ( preimage) = self . payment_preimages . get ( & htlc. payment_hash ) {
3705
+ let payment_preimage = if let Some ( ( preimage, _ ) ) = self . payment_preimages . get ( & htlc. payment_hash ) {
3694
3706
preimage. clone ( )
3695
3707
} else {
3696
3708
// We can't build an HTLC-Success transaction without the preimage
@@ -3844,7 +3856,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3844
3856
for htlc in self . current_holder_commitment_tx . htlc_outputs . iter ( ) {
3845
3857
if let Some ( vout) = htlc. 0 . transaction_output_index {
3846
3858
let preimage = if !htlc. 0 . offered {
3847
- if let Some ( preimage) = self . payment_preimages . get ( & htlc. 0 . payment_hash ) { Some ( preimage. clone ( ) ) } else {
3859
+ if let Some ( ( preimage, _ ) ) = self . payment_preimages . get ( & htlc. 0 . payment_hash ) { Some ( preimage. clone ( ) ) } else {
3848
3860
// We can't build an HTLC-Success transaction without the preimage
3849
3861
continue ;
3850
3862
}
@@ -4817,7 +4829,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4817
4829
for _ in 0 ..payment_preimages_len {
4818
4830
let preimage: PaymentPreimage = Readable :: read ( reader) ?;
4819
4831
let hash = PaymentHash ( Sha256 :: hash ( & preimage. 0 [ ..] ) . to_byte_array ( ) ) ;
4820
- if let Some ( _) = payment_preimages. insert ( hash, preimage) {
4832
+ if let Some ( _) = payment_preimages. insert ( hash, ( preimage, Vec :: new ( ) ) ) {
4821
4833
return Err ( DecodeError :: InvalidValue ) ;
4822
4834
}
4823
4835
}
@@ -4900,6 +4912,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4900
4912
let mut balances_empty_height = None ;
4901
4913
let mut channel_id = None ;
4902
4914
let mut holder_pays_commitment_tx_fee = None ;
4915
+ let mut payment_preimages_with_info: Option < HashMap < _ , _ > > = None ;
4903
4916
read_tlv_fields ! ( reader, {
4904
4917
( 1 , funding_spend_confirmed, option) ,
4905
4918
( 3 , htlcs_resolved_on_chain, optional_vec) ,
@@ -4913,7 +4926,24 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4913
4926
( 19 , channel_id, option) ,
4914
4927
( 21 , balances_empty_height, option) ,
4915
4928
( 23 , holder_pays_commitment_tx_fee, option) ,
4929
+ ( 25 , payment_preimages_with_info, option) ,
4916
4930
} ) ;
4931
+ if let Some ( payment_preimages_with_info) = payment_preimages_with_info {
4932
+ if payment_preimages_with_info. len ( ) != payment_preimages. len ( ) {
4933
+ return Err ( DecodeError :: InvalidValue ) ;
4934
+ }
4935
+ for ( payment_hash, ( payment_preimage, _) ) in payment_preimages. iter ( ) {
4936
+ // Note that because `payment_preimages` is built back from preimages directly,
4937
+ // checking that the two maps have the same hash -> preimage pairs also checks that
4938
+ // the payment hashes in `payment_preimages_with_info`'s preimages match its
4939
+ // hashes.
4940
+ let new_preimage = payment_preimages_with_info. get ( payment_hash) . map ( |( p, _) | p) ;
4941
+ if new_preimage != Some ( payment_preimage) {
4942
+ return Err ( DecodeError :: InvalidValue ) ;
4943
+ }
4944
+ }
4945
+ payment_preimages = payment_preimages_with_info;
4946
+ }
4917
4947
4918
4948
// `HolderForceClosedWithInfo` replaced `HolderForceClosed` in v0.0.122. If we have both
4919
4949
// events, we can remove the `HolderForceClosed` event and just keep the `HolderForceClosedWithInfo`.
0 commit comments