@@ -3543,9 +3543,13 @@ where
3543
3543
}
3544
3544
3545
3545
macro_rules! check_total_value {
3546
- ( $payment_data: expr, $payment_preimage: expr) => { {
3546
+ ( $payment_data: expr, $payment_preimage: expr, $is_keysend : expr ) => { {
3547
3547
let mut payment_claimable_generated = false ;
3548
- let purpose = || {
3548
+ let purpose = if $is_keysend {
3549
+ events:: PaymentPurpose :: SpontaneousPayment (
3550
+ $payment_preimage. expect( "Should never call check_total_value with $is_keysend as true but no preimage" )
3551
+ )
3552
+ } else {
3549
3553
events:: PaymentPurpose :: InvoicePayment {
3550
3554
payment_preimage: $payment_preimage,
3551
3555
payment_secret: $payment_data. payment_secret,
@@ -3561,7 +3565,7 @@ where
3561
3565
. or_insert_with( || {
3562
3566
committed_to_claimable = true ;
3563
3567
ClaimablePayment {
3564
- purpose: purpose( ) , htlcs: Vec :: new( ) , onion_fields: None ,
3568
+ purpose: purpose. clone ( ) , htlcs: Vec :: new( ) , onion_fields: None ,
3565
3569
}
3566
3570
} ) ;
3567
3571
if let Some ( earlier_fields) = & mut claimable_payment. onion_fields {
@@ -3572,7 +3576,7 @@ where
3572
3576
claimable_payment. onion_fields = Some ( onion_fields) ;
3573
3577
}
3574
3578
let ref mut htlcs = & mut claimable_payment. htlcs;
3575
- if htlcs. len ( ) == 1 {
3579
+ if ! htlcs. is_empty ( ) && !$is_keysend {
3576
3580
if let OnionPayload :: Spontaneous ( _) = htlcs[ 0 ] . onion_payload {
3577
3581
log_trace!( self . logger, "Failing new HTLC with payment_hash {} as we already had an existing keysend HTLC with the same payment hash" , log_bytes!( payment_hash. 0 ) ) ;
3578
3582
fail_htlc!( claimable_htlc, payment_hash) ;
@@ -3583,17 +3587,12 @@ where
3583
3587
for htlc in htlcs. iter( ) {
3584
3588
total_value += htlc. sender_intended_value;
3585
3589
earliest_expiry = cmp:: min( earliest_expiry, htlc. cltv_expiry) ;
3586
- match & htlc. onion_payload {
3587
- OnionPayload :: Invoice { .. } => {
3588
- if htlc. total_msat != $payment_data. total_msat {
3589
- log_trace!( self . logger, "Failing HTLCs with payment_hash {} as the HTLCs had inconsistent total values (eg {} and {})" ,
3590
- log_bytes!( payment_hash. 0 ) , $payment_data. total_msat, htlc. total_msat) ;
3591
- total_value = msgs:: MAX_VALUE_MSAT ;
3592
- }
3593
- if total_value >= msgs:: MAX_VALUE_MSAT { break ; }
3594
- } ,
3595
- _ => unreachable!( ) ,
3590
+ if htlc. total_msat != $payment_data. total_msat {
3591
+ log_trace!( self . logger, "Failing HTLCs with payment_hash {} as the HTLCs had inconsistent total values (eg {} and {})" ,
3592
+ log_bytes!( payment_hash. 0 ) , $payment_data. total_msat, htlc. total_msat) ;
3593
+ total_value = msgs:: MAX_VALUE_MSAT ;
3596
3594
}
3595
+ if total_value >= msgs:: MAX_VALUE_MSAT { break ; }
3597
3596
}
3598
3597
// The condition determining whether an MPP is complete must
3599
3598
// match exactly the condition used in `timer_tick_occurred`
@@ -3614,7 +3613,7 @@ where
3614
3613
new_events. push_back( ( events:: Event :: PaymentClaimable {
3615
3614
receiver_node_id: Some ( receiver_node_id) ,
3616
3615
payment_hash,
3617
- purpose: purpose ( ) ,
3616
+ purpose,
3618
3617
amount_msat,
3619
3618
via_channel_id: Some ( prev_channel_id) ,
3620
3619
via_user_channel_id: Some ( prev_user_channel_id) ,
@@ -3662,40 +3661,44 @@ where
3662
3661
fail_htlc ! ( claimable_htlc, payment_hash) ;
3663
3662
}
3664
3663
}
3665
- check_total_value ! ( payment_data, payment_preimage) ;
3664
+ check_total_value ! ( payment_data, payment_preimage, false ) ;
3666
3665
} ,
3667
3666
OnionPayload :: Spontaneous ( preimage) => {
3668
- let mut claimable_payments = self . claimable_payments . lock ( ) . unwrap ( ) ;
3669
- if claimable_payments. pending_claiming_payments . contains_key ( & payment_hash) {
3670
- fail_htlc ! ( claimable_htlc, payment_hash) ;
3671
- }
3672
- match claimable_payments. claimable_payments . entry ( payment_hash) {
3673
- hash_map:: Entry :: Vacant ( e) => {
3674
- let amount_msat = claimable_htlc. value ;
3675
- claimable_htlc. total_value_received = Some ( amount_msat) ;
3676
- let claim_deadline = Some ( claimable_htlc. cltv_expiry - HTLC_FAIL_BACK_BUFFER ) ;
3677
- let purpose = events:: PaymentPurpose :: SpontaneousPayment ( preimage) ;
3678
- e. insert ( ClaimablePayment {
3679
- purpose : purpose. clone ( ) ,
3680
- onion_fields : Some ( onion_fields. clone ( ) ) ,
3681
- htlcs : vec ! [ claimable_htlc] ,
3682
- } ) ;
3683
- let prev_channel_id = prev_funding_outpoint. to_channel_id ( ) ;
3684
- new_events. push_back ( ( events:: Event :: PaymentClaimable {
3685
- receiver_node_id : Some ( receiver_node_id) ,
3686
- payment_hash,
3687
- amount_msat,
3688
- purpose,
3689
- via_channel_id : Some ( prev_channel_id) ,
3690
- via_user_channel_id : Some ( prev_user_channel_id) ,
3691
- claim_deadline,
3692
- onion_fields : Some ( onion_fields) ,
3693
- } , None ) ) ;
3694
- } ,
3695
- hash_map:: Entry :: Occupied ( _) => {
3696
- log_trace ! ( self . logger, "Failing new keysend HTLC with payment_hash {} for a duplicative payment hash" , log_bytes!( payment_hash. 0 ) ) ;
3667
+ if let Some ( payment_data) = payment_data {
3668
+ check_total_value ! ( payment_data, Some ( preimage) , true ) ;
3669
+ } else {
3670
+ let mut claimable_payments = self . claimable_payments . lock ( ) . unwrap ( ) ;
3671
+ if claimable_payments. pending_claiming_payments . contains_key ( & payment_hash) {
3697
3672
fail_htlc ! ( claimable_htlc, payment_hash) ;
3698
3673
}
3674
+ match claimable_payments. claimable_payments . entry ( payment_hash) {
3675
+ hash_map:: Entry :: Vacant ( e) => {
3676
+ let amount_msat = claimable_htlc. value ;
3677
+ claimable_htlc. total_value_received = Some ( amount_msat) ;
3678
+ let claim_deadline = Some ( claimable_htlc. cltv_expiry - HTLC_FAIL_BACK_BUFFER ) ;
3679
+ let purpose = events:: PaymentPurpose :: SpontaneousPayment ( preimage) ;
3680
+ e. insert ( ClaimablePayment {
3681
+ purpose : purpose. clone ( ) ,
3682
+ onion_fields : Some ( onion_fields. clone ( ) ) ,
3683
+ htlcs : vec ! [ claimable_htlc] ,
3684
+ } ) ;
3685
+ let prev_channel_id = prev_funding_outpoint. to_channel_id ( ) ;
3686
+ new_events. push_back ( ( events:: Event :: PaymentClaimable {
3687
+ receiver_node_id : Some ( receiver_node_id) ,
3688
+ payment_hash,
3689
+ amount_msat,
3690
+ purpose,
3691
+ via_channel_id : Some ( prev_channel_id) ,
3692
+ via_user_channel_id : Some ( prev_user_channel_id) ,
3693
+ claim_deadline,
3694
+ onion_fields : Some ( onion_fields) ,
3695
+ } , None ) ) ;
3696
+ } ,
3697
+ hash_map:: Entry :: Occupied ( _) => {
3698
+ log_trace ! ( self . logger, "Failing new keysend HTLC with payment_hash {} for a duplicative payment hash" , log_bytes!( payment_hash. 0 ) ) ;
3699
+ fail_htlc ! ( claimable_htlc, payment_hash) ;
3700
+ }
3701
+ }
3699
3702
}
3700
3703
}
3701
3704
}
@@ -3714,7 +3717,7 @@ where
3714
3717
log_bytes!( payment_hash. 0 ) , payment_data. total_msat, inbound_payment. get( ) . min_value_msat. unwrap( ) ) ;
3715
3718
fail_htlc ! ( claimable_htlc, payment_hash) ;
3716
3719
} else {
3717
- let payment_claimable_generated = check_total_value ! ( payment_data, inbound_payment. get( ) . payment_preimage) ;
3720
+ let payment_claimable_generated = check_total_value ! ( payment_data, inbound_payment. get( ) . payment_preimage, false ) ;
3718
3721
if payment_claimable_generated {
3719
3722
inbound_payment. remove_entry ( ) ;
3720
3723
}
@@ -4189,12 +4192,16 @@ where
4189
4192
/// event matches your expectation. If you fail to do so and call this method, you may provide
4190
4193
/// the sender "proof-of-payment" when they did not fulfill the full expected payment.
4191
4194
///
4195
+ /// To accept multi-part keysend payments you must set [`UserConfig::accept_mpp_keysend`] to
4196
+ /// true.
4197
+ ///
4192
4198
/// [`Event::PaymentClaimable`]: crate::events::Event::PaymentClaimable
4193
4199
/// [`Event::PaymentClaimable::claim_deadline`]: crate::events::Event::PaymentClaimable::claim_deadline
4194
4200
/// [`Event::PaymentClaimed`]: crate::events::Event::PaymentClaimed
4195
4201
/// [`process_pending_events`]: EventsProvider::process_pending_events
4196
4202
/// [`create_inbound_payment`]: Self::create_inbound_payment
4197
4203
/// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
4204
+ /// [`UserConfig::accept_mpp_keysend`]: crate::util::config::UserConfig::accept_mpp_keysend
4198
4205
pub fn claim_funds ( & self , payment_preimage : PaymentPreimage ) {
4199
4206
let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 ) . into_inner ( ) ) ;
4200
4207
@@ -4255,9 +4262,9 @@ where
4255
4262
expected_amt_msat = htlc. total_value_received ;
4256
4263
4257
4264
if let OnionPayload :: Spontaneous ( _) = & htlc. onion_payload {
4258
- // We don't currently support MPP for spontaneous payments, so just check
4265
+ // If the user chooses not to support MPP for spontaneous payments, just check
4259
4266
// that there's one payment here and move on.
4260
- if sources. len ( ) != 1 {
4267
+ if ! self . default_configuration . accept_mpp_keysend && sources. len ( ) != 1 {
4261
4268
log_error ! ( self . logger, "Somehow ended up with an MPP spontaneous payment - this should not be reachable!" ) ;
4262
4269
debug_assert ! ( false ) ;
4263
4270
valid_mpp = false ;
0 commit comments