@@ -284,11 +284,14 @@ pub(super) struct ChannelHolder<ChanSigner: ChannelKeys> {
284
284
/// guarantees are made about the existence of a channel with the short id here, nor the short
285
285
/// ids in the PendingHTLCInfo!
286
286
pub ( super ) forward_htlcs : HashMap < u64 , Vec < HTLCForwardInfo > > ,
287
- /// Tracks things that were to us and can be failed/claimed by the user
287
+ /// (payment_hash, payment_secret) -> Vec<HTLCs> for tracking things that
288
+ /// were to us and can be failed/claimed by the user
288
289
/// Note that while this is held in the same mutex as the channels themselves, no consistency
289
290
/// guarantees are made about the channels given here actually existing anymore by the time you
290
291
/// go to read them!
291
- claimable_htlcs : HashMap < PaymentHash , Vec < ClaimableHTLC > > ,
292
+ /// TODO: We need to time out HTLCs sitting here which are waiting on other AMP HTLCs to
293
+ /// arrive.
294
+ claimable_htlcs : HashMap < ( PaymentHash , Option < [ u8 ; 32 ] > ) , Vec < ClaimableHTLC > > ,
292
295
/// Messages to send to peers - pushed to in the same lock that they are generated in (except
293
296
/// for broadcast messages, where ordering isn't as strict).
294
297
pub ( super ) pending_msg_events : Vec < events:: MessageSendEvent > ,
@@ -1180,7 +1183,14 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1180
1183
/// In case of APIError::MonitorUpdateFailed, the commitment update has been irrevocably
1181
1184
/// committed on our end and we're just waiting for a monitor update to send it. Do NOT retry
1182
1185
/// the payment via a different route unless you intend to pay twice!
1183
- pub fn send_payment ( & self , route : Route , payment_hash : PaymentHash ) -> Result < ( ) , APIError > {
1186
+ ///
1187
+ /// payment_secret is unrelated to payment_hash (or PaymentPreimage) and exists to authenticate
1188
+ /// the sender to the recipient and prevent payment-probing (deanonymization) attacks. For
1189
+ /// newer nodes, it will be provided to you in the invoice. If you do not have one, the Route
1190
+ /// must not contain multiple paths as otherwise the multipath data cannot be sent.
1191
+ /// If a payment_secret *is* provided, we assume that the invoice had the basic_mpp feature bit
1192
+ /// set (either as required or as available).
1193
+ pub fn send_payment ( & self , route : Route , payment_hash : PaymentHash , payment_secret : Option < & [ u8 ; 32 ] > ) -> Result < ( ) , APIError > {
1184
1194
if route. hops . len ( ) < 1 || route. hops . len ( ) > 20 {
1185
1195
return Err ( APIError :: RouteError { err : "Route didn't go anywhere/had bogus size" } ) ;
1186
1196
}
@@ -1197,7 +1207,7 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1197
1207
1198
1208
let onion_keys = secp_call ! ( onion_utils:: construct_onion_keys( & self . secp_ctx, & route, & session_priv) ,
1199
1209
APIError :: RouteError { err: "Pubkey along hop was maliciously selected" } ) ;
1200
- let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( & route, cur_height) ?;
1210
+ let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( & route, payment_secret , cur_height) ?;
1201
1211
if onion_utils:: route_size_insane ( & onion_payloads) {
1202
1212
return Err ( APIError :: RouteError { err : "Route size too large considering onion data" } ) ;
1203
1213
}
@@ -1413,7 +1423,9 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1413
1423
htlc_id : prev_htlc_id,
1414
1424
incoming_packet_shared_secret : forward_info. incoming_shared_secret ,
1415
1425
} ) ;
1416
- failed_forwards. push ( ( htlc_source, forward_info. payment_hash , 0x4000 | 10 , None ) ) ;
1426
+ failed_forwards. push ( ( htlc_source, forward_info. payment_hash ,
1427
+ HTLCFailReason :: Reason { failure_code : 0x1000 | 7 , data : Vec :: new ( ) }
1428
+ ) ) ;
1417
1429
} ,
1418
1430
HTLCForwardInfo :: FailHTLC { .. } => {
1419
1431
// Channel went away before we could fail it. This implies
@@ -1449,7 +1461,9 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1449
1461
panic ! ( "Stated return value requirements in send_htlc() were not met" ) ;
1450
1462
}
1451
1463
let chan_update = self . get_channel_update ( chan. get ( ) ) . unwrap ( ) ;
1452
- failed_forwards. push ( ( htlc_source, payment_hash, 0x1000 | 7 , Some ( chan_update) ) ) ;
1464
+ failed_forwards. push ( ( htlc_source, payment_hash,
1465
+ HTLCFailReason :: Reason { failure_code : 0x1000 | 7 , data : chan_update. encode_with_len ( ) }
1466
+ ) ) ;
1453
1467
continue ;
1454
1468
} ,
1455
1469
Ok ( update_add) => {
@@ -1558,15 +1572,48 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1558
1572
htlc_id : prev_htlc_id,
1559
1573
incoming_packet_shared_secret : incoming_shared_secret,
1560
1574
} ;
1561
- channel_state. claimable_htlcs . entry ( payment_hash) . or_insert ( Vec :: new ( ) ) . push ( ClaimableHTLC {
1575
+
1576
+ let mut total_value = 0 ;
1577
+ let htlcs = channel_state. claimable_htlcs . entry ( ( payment_hash, if let & Some ( ref data) = & payment_data {
1578
+ Some ( data. payment_secret . clone ( ) ) } else { None } ) )
1579
+ . or_insert ( Vec :: new ( ) ) ;
1580
+ htlcs. push ( ClaimableHTLC {
1562
1581
src : prev_hop_data,
1563
1582
value : amt_to_forward,
1564
- payment_data,
1565
- } ) ;
1566
- new_events. push ( events:: Event :: PaymentReceived {
1567
- payment_hash : payment_hash,
1568
- amt : amt_to_forward,
1583
+ payment_data : payment_data. clone ( ) ,
1569
1584
} ) ;
1585
+ if let & Some ( ref data) = & payment_data {
1586
+ for htlc in htlcs. iter ( ) {
1587
+ total_value += htlc. value ;
1588
+ if htlc. payment_data . as_ref ( ) . unwrap ( ) . total_msat != data. total_msat {
1589
+ total_value = msgs:: MAX_VALUE_MSAT ;
1590
+ }
1591
+ if total_value >= msgs:: MAX_VALUE_MSAT { break ; }
1592
+ }
1593
+ if total_value >= msgs:: MAX_VALUE_MSAT {
1594
+ for htlc in htlcs. iter ( ) {
1595
+ failed_forwards. push ( ( HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1596
+ short_channel_id : htlc. src . short_channel_id ,
1597
+ htlc_id : htlc. src . htlc_id ,
1598
+ incoming_packet_shared_secret : htlc. src . incoming_packet_shared_secret ,
1599
+ } ) , payment_hash,
1600
+ HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) }
1601
+ ) ) ;
1602
+ }
1603
+ } else if total_value >= data. total_msat {
1604
+ new_events. push ( events:: Event :: PaymentReceived {
1605
+ payment_hash : payment_hash,
1606
+ payment_secret : Some ( data. payment_secret ) ,
1607
+ amt : total_value,
1608
+ } ) ;
1609
+ }
1610
+ } else {
1611
+ new_events. push ( events:: Event :: PaymentReceived {
1612
+ payment_hash : payment_hash,
1613
+ payment_secret : None ,
1614
+ amt : amt_to_forward,
1615
+ } ) ;
1616
+ }
1570
1617
} ,
1571
1618
HTLCForwardInfo :: AddHTLC { .. } => {
1572
1619
panic ! ( "short_channel_id == 0 should imply any pending_forward entries are of type Receive" ) ;
@@ -1580,11 +1627,8 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1580
1627
}
1581
1628
}
1582
1629
1583
- for ( htlc_source, payment_hash, failure_code, update) in failed_forwards. drain ( ..) {
1584
- match update {
1585
- None => self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source, & payment_hash, HTLCFailReason :: Reason { failure_code, data : Vec :: new ( ) } ) ,
1586
- Some ( chan_update) => self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source, & payment_hash, HTLCFailReason :: Reason { failure_code, data : chan_update. encode_with_len ( ) } ) ,
1587
- } ;
1630
+ for ( htlc_source, payment_hash, failure_reason) in failed_forwards. drain ( ..) {
1631
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source, & payment_hash, failure_reason) ;
1588
1632
}
1589
1633
1590
1634
if handle_errors. len ( ) > 0 {
@@ -1629,11 +1673,11 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1629
1673
/// along the path (including in our own channel on which we received it).
1630
1674
/// Returns false if no payment was found to fail backwards, true if the process of failing the
1631
1675
/// HTLC backwards has been started.
1632
- pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash ) -> bool {
1676
+ pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash , payment_secret : & Option < [ u8 ; 32 ] > ) -> bool {
1633
1677
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1634
1678
1635
1679
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1636
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( payment_hash) ;
1680
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( * payment_hash, * payment_secret ) ) ;
1637
1681
if let Some ( mut sources) = removed_source {
1638
1682
for htlc in sources. drain ( ..) {
1639
1683
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
@@ -1755,13 +1799,13 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1755
1799
/// motivated attackers.
1756
1800
///
1757
1801
/// May panic if called except in response to a PaymentReceived event.
1758
- pub fn claim_funds ( & self , payment_preimage : PaymentPreimage , expected_amount : u64 ) -> bool {
1802
+ pub fn claim_funds ( & self , payment_preimage : PaymentPreimage , payment_secret : & Option < [ u8 ; 32 ] > , expected_amount : u64 ) -> bool {
1759
1803
let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 ) . into_inner ( ) ) ;
1760
1804
1761
1805
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1762
1806
1763
1807
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1764
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
1808
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( payment_hash, * payment_secret ) ) ;
1765
1809
if let Some ( mut sources) = removed_source {
1766
1810
for htlc in sources. drain ( ..) {
1767
1811
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
0 commit comments