@@ -282,11 +282,14 @@ pub(super) struct ChannelHolder<ChanSigner: ChannelKeys> {
282
282
/// guarantees are made about the existence of a channel with the short id here, nor the short
283
283
/// ids in the PendingHTLCInfo!
284
284
pub ( super ) forward_htlcs : HashMap < u64 , Vec < HTLCForwardInfo > > ,
285
- /// Tracks things that were to us and can be failed/claimed by the user
285
+ /// (payment_hash, payment_secret) -> Vec<HTLCs> for tracking things that
286
+ /// were to us and can be failed/claimed by the user
286
287
/// Note that while this is held in the same mutex as the channels themselves, no consistency
287
288
/// guarantees are made about the channels given here actually existing anymore by the time you
288
289
/// go to read them!
289
- claimable_htlcs : HashMap < PaymentHash , Vec < ClaimableHTLC > > ,
290
+ /// TODO: We need to time out HTLCs sitting here which are waiting on other AMP HTLCs to
291
+ /// arrive.
292
+ claimable_htlcs : HashMap < ( PaymentHash , Option < [ u8 ; 32 ] > ) , Vec < ClaimableHTLC > > ,
290
293
/// Messages to send to peers - pushed to in the same lock that they are generated in (except
291
294
/// for broadcast messages, where ordering isn't as strict).
292
295
pub ( super ) pending_msg_events : Vec < events:: MessageSendEvent > ,
@@ -295,7 +298,7 @@ pub(super) struct MutChannelHolder<'a, ChanSigner: ChannelKeys + 'a> {
295
298
pub ( super ) by_id : & ' a mut HashMap < [ u8 ; 32 ] , Channel < ChanSigner > > ,
296
299
pub ( super ) short_to_id : & ' a mut HashMap < u64 , [ u8 ; 32 ] > ,
297
300
pub ( super ) forward_htlcs : & ' a mut HashMap < u64 , Vec < HTLCForwardInfo > > ,
298
- claimable_htlcs : & ' a mut HashMap < PaymentHash , Vec < ClaimableHTLC > > ,
301
+ claimable_htlcs : & ' a mut HashMap < ( PaymentHash , Option < [ u8 ; 32 ] > ) , Vec < ClaimableHTLC > > ,
299
302
pub ( super ) pending_msg_events : & ' a mut Vec < events:: MessageSendEvent > ,
300
303
}
301
304
impl < ChanSigner : ChannelKeys > ChannelHolder < ChanSigner > {
@@ -1195,7 +1198,14 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1195
1198
/// In case of APIError::MonitorUpdateFailed, the commitment update has been irrevocably
1196
1199
/// committed on our end and we're just waiting for a monitor update to send it. Do NOT retry
1197
1200
/// the payment via a different route unless you intend to pay twice!
1198
- pub fn send_payment ( & self , route : Route , payment_hash : PaymentHash ) -> Result < ( ) , APIError > {
1201
+ ///
1202
+ /// payment_secret is unrelated to payment_hash (or PaymentPreimage) and exists to authenticate
1203
+ /// the sender to the recipient and prevent payment-probing (deanonymization) attacks. For
1204
+ /// newer nodes, it will be provided to you in the invoice. If you do not have one, the Route
1205
+ /// must not contain multiple paths as otherwise the multipath data cannot be sent.
1206
+ /// If a payment_secret *is* provided, we assume that the invoice had the basic_mpp feature bit
1207
+ /// set (either as required or as available).
1208
+ pub fn send_payment ( & self , route : Route , payment_hash : PaymentHash , payment_secret : Option < & [ u8 ; 32 ] > ) -> Result < ( ) , APIError > {
1199
1209
if route. hops . len ( ) < 1 || route. hops . len ( ) > 20 {
1200
1210
return Err ( APIError :: RouteError { err : "Route didn't go anywhere/had bogus size" } ) ;
1201
1211
}
@@ -1212,7 +1222,7 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1212
1222
1213
1223
let onion_keys = secp_call ! ( onion_utils:: construct_onion_keys( & self . secp_ctx, & route, & session_priv) ,
1214
1224
APIError :: RouteError { err: "Pubkey along hop was maliciously selected" } ) ;
1215
- let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( & route, cur_height) ?;
1225
+ let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( & route, payment_secret , cur_height) ?;
1216
1226
if onion_utils:: route_size_insane ( & onion_payloads) {
1217
1227
return Err ( APIError :: RouteError { err : "Route had too large size once" } ) ;
1218
1228
}
@@ -1423,7 +1433,9 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1423
1433
htlc_id : prev_htlc_id,
1424
1434
incoming_packet_shared_secret : forward_info. incoming_shared_secret ,
1425
1435
} ) ;
1426
- failed_forwards. push ( ( htlc_source, forward_info. payment_hash , 0x4000 | 10 , None ) ) ;
1436
+ failed_forwards. push ( ( htlc_source, forward_info. payment_hash ,
1437
+ HTLCFailReason :: Reason { failure_code : 0x1000 | 7 , data : Vec :: new ( ) }
1438
+ ) ) ;
1427
1439
} ,
1428
1440
HTLCForwardInfo :: FailHTLC { .. } => {
1429
1441
// Channel went away before we could fail it. This implies
@@ -1459,7 +1471,9 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1459
1471
panic ! ( "Stated return value requirements in send_htlc() were not met" ) ;
1460
1472
}
1461
1473
let chan_update = self . get_channel_update ( chan. get ( ) ) . unwrap ( ) ;
1462
- failed_forwards. push ( ( htlc_source, payment_hash, 0x1000 | 7 , Some ( chan_update) ) ) ;
1474
+ failed_forwards. push ( ( htlc_source, payment_hash,
1475
+ HTLCFailReason :: Reason { failure_code : 0x1000 | 7 , data : chan_update. encode_with_len ( ) }
1476
+ ) ) ;
1463
1477
continue ;
1464
1478
} ,
1465
1479
Ok ( update_add) => {
@@ -1568,15 +1582,48 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1568
1582
htlc_id : prev_htlc_id,
1569
1583
incoming_packet_shared_secret : incoming_shared_secret,
1570
1584
} ;
1571
- channel_state. claimable_htlcs . entry ( payment_hash) . or_insert ( Vec :: new ( ) ) . push ( ClaimableHTLC {
1585
+
1586
+ let mut total_value = 0 ;
1587
+ let htlcs = channel_state. claimable_htlcs . entry ( ( payment_hash, if let & Some ( ref data) = & payment_data {
1588
+ Some ( data. payment_secret . clone ( ) ) } else { None } ) )
1589
+ . or_insert ( Vec :: new ( ) ) ;
1590
+ htlcs. push ( ClaimableHTLC {
1572
1591
src : prev_hop_data,
1573
1592
value : amt_to_forward,
1574
- payment_data,
1575
- } ) ;
1576
- new_events. push ( events:: Event :: PaymentReceived {
1577
- payment_hash : payment_hash,
1578
- amt : amt_to_forward,
1593
+ payment_data : payment_data. clone ( ) ,
1579
1594
} ) ;
1595
+ if let & Some ( ref data) = & payment_data {
1596
+ for htlc in htlcs. iter ( ) {
1597
+ total_value += htlc. value ;
1598
+ if htlc. payment_data . as_ref ( ) . unwrap ( ) . total_msat != data. total_msat {
1599
+ total_value = msgs:: MAX_VALUE_MSAT ;
1600
+ }
1601
+ if total_value >= msgs:: MAX_VALUE_MSAT { break ; }
1602
+ }
1603
+ if total_value >= msgs:: MAX_VALUE_MSAT {
1604
+ for htlc in htlcs. iter ( ) {
1605
+ failed_forwards. push ( ( HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1606
+ short_channel_id : htlc. src . short_channel_id ,
1607
+ htlc_id : htlc. src . htlc_id ,
1608
+ incoming_packet_shared_secret : htlc. src . incoming_packet_shared_secret ,
1609
+ } ) , payment_hash,
1610
+ HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) }
1611
+ ) ) ;
1612
+ }
1613
+ } else if total_value >= data. total_msat {
1614
+ new_events. push ( events:: Event :: PaymentReceived {
1615
+ payment_hash : payment_hash,
1616
+ payment_secret : Some ( data. payment_secret ) ,
1617
+ amt : total_value,
1618
+ } ) ;
1619
+ }
1620
+ } else {
1621
+ new_events. push ( events:: Event :: PaymentReceived {
1622
+ payment_hash : payment_hash,
1623
+ payment_secret : None ,
1624
+ amt : amt_to_forward,
1625
+ } ) ;
1626
+ }
1580
1627
} ,
1581
1628
HTLCForwardInfo :: AddHTLC { .. } => {
1582
1629
panic ! ( "short_channel_id == 0 should imply any pending_forward entries are of type Receive" ) ;
@@ -1590,11 +1637,8 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1590
1637
}
1591
1638
}
1592
1639
1593
- for ( htlc_source, payment_hash, failure_code, update) in failed_forwards. drain ( ..) {
1594
- match update {
1595
- None => self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source, & payment_hash, HTLCFailReason :: Reason { failure_code, data : Vec :: new ( ) } ) ,
1596
- 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 ( ) } ) ,
1597
- } ;
1640
+ for ( htlc_source, payment_hash, failure_reason) in failed_forwards. drain ( ..) {
1641
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source, & payment_hash, failure_reason) ;
1598
1642
}
1599
1643
1600
1644
if handle_errors. len ( ) > 0 {
@@ -1639,11 +1683,11 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1639
1683
/// along the path (including in our own channel on which we received it).
1640
1684
/// Returns false if no payment was found to fail backwards, true if the process of failing the
1641
1685
/// HTLC backwards has been started.
1642
- pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash ) -> bool {
1686
+ pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash , payment_secret : & Option < [ u8 ; 32 ] > ) -> bool {
1643
1687
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1644
1688
1645
1689
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1646
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( payment_hash) ;
1690
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( * payment_hash, * payment_secret ) ) ;
1647
1691
if let Some ( mut sources) = removed_source {
1648
1692
for htlc in sources. drain ( ..) {
1649
1693
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
@@ -1765,13 +1809,13 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1765
1809
/// motivated attackers.
1766
1810
///
1767
1811
/// May panic if called except in response to a PaymentReceived event.
1768
- pub fn claim_funds ( & self , payment_preimage : PaymentPreimage , expected_amount : u64 ) -> bool {
1812
+ pub fn claim_funds ( & self , payment_preimage : PaymentPreimage , payment_secret : & Option < [ u8 ; 32 ] > , expected_amount : u64 ) -> bool {
1769
1813
let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 ) . into_inner ( ) ) ;
1770
1814
1771
1815
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1772
1816
1773
1817
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1774
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
1818
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( payment_hash, * payment_secret ) ) ;
1775
1819
if let Some ( mut sources) = removed_source {
1776
1820
for htlc in sources. drain ( ..) {
1777
1821
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
0 commit comments