@@ -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 HTLCs that were to us and can be failed/claimed by the user
287
+ /// (payment_hash, payment_secret) -> Vec<HTLCs> for tracking HTLCs 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 > ,
@@ -1191,7 +1194,14 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1191
1194
/// In case of APIError::MonitorUpdateFailed, the commitment update has been irrevocably
1192
1195
/// committed on our end and we're just waiting for a monitor update to send it. Do NOT retry
1193
1196
/// the payment via a different route unless you intend to pay twice!
1194
- pub fn send_payment ( & self , route : Route , payment_hash : PaymentHash ) -> Result < ( ) , APIError > {
1197
+ ///
1198
+ /// payment_secret is unrelated to payment_hash (or PaymentPreimage) and exists to authenticate
1199
+ /// the sender to the recipient and prevent payment-probing (deanonymization) attacks. For
1200
+ /// newer nodes, it will be provided to you in the invoice. If you do not have one, the Route
1201
+ /// must not contain multiple paths as otherwise the multipath data cannot be sent.
1202
+ /// If a payment_secret *is* provided, we assume that the invoice had the basic_mpp feature bit
1203
+ /// set (either as required or as available).
1204
+ pub fn send_payment ( & self , route : Route , payment_hash : PaymentHash , payment_secret : Option < & [ u8 ; 32 ] > ) -> Result < ( ) , APIError > {
1195
1205
if route. hops . len ( ) < 1 || route. hops . len ( ) > 20 {
1196
1206
return Err ( APIError :: RouteError { err : "Route didn't go anywhere/had bogus size" } ) ;
1197
1207
}
@@ -1208,7 +1218,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1208
1218
1209
1219
let onion_keys = secp_call ! ( onion_utils:: construct_onion_keys( & self . secp_ctx, & route, & session_priv) ,
1210
1220
APIError :: RouteError { err: "Pubkey along hop was maliciously selected" } ) ;
1211
- let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( & route, cur_height) ?;
1221
+ let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( & route, payment_secret , cur_height) ?;
1212
1222
if onion_utils:: route_size_insane ( & onion_payloads) {
1213
1223
return Err ( APIError :: RouteError { err : "Route size too large considering onion data" } ) ;
1214
1224
}
@@ -1442,7 +1452,9 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1442
1452
htlc_id : prev_htlc_id,
1443
1453
incoming_packet_shared_secret : forward_info. incoming_shared_secret ,
1444
1454
} ) ;
1445
- failed_forwards. push ( ( htlc_source, forward_info. payment_hash , 0x4000 | 10 , None ) ) ;
1455
+ failed_forwards. push ( ( htlc_source, forward_info. payment_hash ,
1456
+ HTLCFailReason :: Reason { failure_code : 0x1000 | 7 , data : Vec :: new ( ) }
1457
+ ) ) ;
1446
1458
} ,
1447
1459
HTLCForwardInfo :: FailHTLC { .. } => {
1448
1460
// Channel went away before we could fail it. This implies
@@ -1478,7 +1490,9 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1478
1490
panic ! ( "Stated return value requirements in send_htlc() were not met" ) ;
1479
1491
}
1480
1492
let chan_update = self . get_channel_update ( chan. get ( ) ) . unwrap ( ) ;
1481
- failed_forwards. push ( ( htlc_source, payment_hash, 0x1000 | 7 , Some ( chan_update) ) ) ;
1493
+ failed_forwards. push ( ( htlc_source, payment_hash,
1494
+ HTLCFailReason :: Reason { failure_code : 0x1000 | 7 , data : chan_update. encode_with_len ( ) }
1495
+ ) ) ;
1482
1496
continue ;
1483
1497
} ,
1484
1498
Ok ( update_add) => {
@@ -1587,15 +1601,48 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1587
1601
htlc_id : prev_htlc_id,
1588
1602
incoming_packet_shared_secret : incoming_shared_secret,
1589
1603
} ;
1590
- channel_state. claimable_htlcs . entry ( payment_hash) . or_insert ( Vec :: new ( ) ) . push ( ClaimableHTLC {
1604
+
1605
+ let mut total_value = 0 ;
1606
+ let htlcs = channel_state. claimable_htlcs . entry ( ( payment_hash, if let & Some ( ref data) = & payment_data {
1607
+ Some ( data. payment_secret . clone ( ) ) } else { None } ) )
1608
+ . or_insert ( Vec :: new ( ) ) ;
1609
+ htlcs. push ( ClaimableHTLC {
1591
1610
prev_hop,
1592
1611
value : amt_to_forward,
1593
- payment_data,
1594
- } ) ;
1595
- new_events. push ( events:: Event :: PaymentReceived {
1596
- payment_hash : payment_hash,
1597
- amt : amt_to_forward,
1612
+ payment_data : payment_data. clone ( ) ,
1598
1613
} ) ;
1614
+ if let & Some ( ref data) = & payment_data {
1615
+ for htlc in htlcs. iter ( ) {
1616
+ total_value += htlc. value ;
1617
+ if htlc. payment_data . as_ref ( ) . unwrap ( ) . total_msat != data. total_msat {
1618
+ total_value = msgs:: MAX_VALUE_MSAT ;
1619
+ }
1620
+ if total_value >= msgs:: MAX_VALUE_MSAT { break ; }
1621
+ }
1622
+ if total_value >= msgs:: MAX_VALUE_MSAT {
1623
+ for htlc in htlcs. iter ( ) {
1624
+ failed_forwards. push ( ( HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1625
+ short_channel_id : htlc. prev_hop . short_channel_id ,
1626
+ htlc_id : htlc. prev_hop . htlc_id ,
1627
+ incoming_packet_shared_secret : htlc. prev_hop . incoming_packet_shared_secret ,
1628
+ } ) , payment_hash,
1629
+ HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) }
1630
+ ) ) ;
1631
+ }
1632
+ } else if total_value >= data. total_msat {
1633
+ new_events. push ( events:: Event :: PaymentReceived {
1634
+ payment_hash : payment_hash,
1635
+ payment_secret : Some ( data. payment_secret ) ,
1636
+ amt : total_value,
1637
+ } ) ;
1638
+ }
1639
+ } else {
1640
+ new_events. push ( events:: Event :: PaymentReceived {
1641
+ payment_hash : payment_hash,
1642
+ payment_secret : None ,
1643
+ amt : amt_to_forward,
1644
+ } ) ;
1645
+ }
1599
1646
} ,
1600
1647
HTLCForwardInfo :: AddHTLC { .. } => {
1601
1648
panic ! ( "short_channel_id == 0 should imply any pending_forward entries are of type Receive" ) ;
@@ -1609,11 +1656,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1609
1656
}
1610
1657
}
1611
1658
1612
- for ( htlc_source, payment_hash, failure_code, update) in failed_forwards. drain ( ..) {
1613
- match update {
1614
- None => self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source, & payment_hash, HTLCFailReason :: Reason { failure_code, data : Vec :: new ( ) } ) ,
1615
- 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 ( ) } ) ,
1616
- } ;
1659
+ for ( htlc_source, payment_hash, failure_reason) in failed_forwards. drain ( ..) {
1660
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source, & payment_hash, failure_reason) ;
1617
1661
}
1618
1662
1619
1663
if handle_errors. len ( ) > 0 {
@@ -1658,11 +1702,11 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1658
1702
/// along the path (including in our own channel on which we received it).
1659
1703
/// Returns false if no payment was found to fail backwards, true if the process of failing the
1660
1704
/// HTLC backwards has been started.
1661
- pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash ) -> bool {
1705
+ pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash , payment_secret : & Option < [ u8 ; 32 ] > ) -> bool {
1662
1706
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1663
1707
1664
1708
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1665
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( payment_hash) ;
1709
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( * payment_hash, * payment_secret ) ) ;
1666
1710
if let Some ( mut sources) = removed_source {
1667
1711
for htlc in sources. drain ( ..) {
1668
1712
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
@@ -1784,13 +1828,13 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1784
1828
/// motivated attackers.
1785
1829
///
1786
1830
/// May panic if called except in response to a PaymentReceived event.
1787
- pub fn claim_funds ( & self , payment_preimage : PaymentPreimage , expected_amount : u64 ) -> bool {
1831
+ pub fn claim_funds ( & self , payment_preimage : PaymentPreimage , payment_secret : & Option < [ u8 ; 32 ] > , expected_amount : u64 ) -> bool {
1788
1832
let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 ) . into_inner ( ) ) ;
1789
1833
1790
1834
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1791
1835
1792
1836
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1793
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
1837
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( payment_hash, * payment_secret ) ) ;
1794
1838
if let Some ( mut sources) = removed_source {
1795
1839
for htlc in sources. drain ( ..) {
1796
1840
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
0 commit comments