@@ -74,7 +74,9 @@ enum PendingForwardReceiveHTLCInfo {
74
74
onion_packet : msgs:: OnionPacket ,
75
75
short_channel_id : u64 , // This should be NonZero<u64> eventually
76
76
} ,
77
- Receive { } ,
77
+ Receive {
78
+ payment_data : Option < msgs:: FinalOnionHopData > ,
79
+ } ,
78
80
}
79
81
80
82
#[ derive( Clone ) ] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
@@ -119,6 +121,12 @@ pub(super) struct HTLCPreviousHopData {
119
121
incoming_packet_shared_secret : [ u8 ; 32 ] ,
120
122
}
121
123
124
+ struct ClaimableHTLC {
125
+ src : HTLCPreviousHopData ,
126
+ value : u64 ,
127
+ payment_data : Option < msgs:: FinalOnionHopData > ,
128
+ }
129
+
122
130
/// Tracks the inbound corresponding to an outbound HTLC
123
131
#[ derive( Clone , PartialEq ) ]
124
132
pub ( super ) enum HTLCSource {
@@ -276,12 +284,11 @@ pub(super) struct ChannelHolder<ChanSigner: ChannelKeys> {
276
284
/// guarantees are made about the existence of a channel with the short id here, nor the short
277
285
/// ids in the PendingHTLCInfo!
278
286
pub ( super ) forward_htlcs : HashMap < u64 , Vec < HTLCForwardInfo > > ,
279
- /// payment_hash -> Vec<(amount_received, htlc_source)> for tracking things that were to us and
280
- /// can be failed/claimed by the user
287
+ /// Tracks things that were to us and can be failed/claimed by the user
281
288
/// Note that while this is held in the same mutex as the channels themselves, no consistency
282
289
/// guarantees are made about the channels given here actually existing anymore by the time you
283
290
/// go to read them!
284
- pub ( super ) claimable_htlcs : HashMap < PaymentHash , Vec < ( u64 , HTLCPreviousHopData ) > > ,
291
+ claimable_htlcs : HashMap < PaymentHash , Vec < ClaimableHTLC > > ,
285
292
/// Messages to send to peers - pushed to in the same lock that they are generated in (except
286
293
/// for broadcast messages, where ordering isn't as strict).
287
294
pub ( super ) pending_msg_events : Vec < events:: MessageSendEvent > ,
@@ -976,13 +983,19 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
976
983
return_err ! ( "Upstream node set CLTV to the wrong value" , 18 , & byte_utils:: be32_to_array( msg. cltv_expiry) ) ;
977
984
}
978
985
986
+ let payment_data = match next_hop_data. format {
987
+ msgs:: OnionHopDataFormat :: Legacy { .. } => None ,
988
+ msgs:: OnionHopDataFormat :: NonFinalNode { .. } => return_err ! ( "Got non final data with an HMAC of 0" , 0x4000 | 22 , & [ 0 ; 0 ] ) ,
989
+ msgs:: OnionHopDataFormat :: FinalNode { payment_data } => payment_data,
990
+ } ;
991
+
979
992
// Note that we could obviously respond immediately with an update_fulfill_htlc
980
993
// message, however that would leak that we are the recipient of this payment, so
981
994
// instead we stay symmetric with the forwarding case, only responding (after a
982
995
// delay) once they've send us a commitment_signed!
983
996
984
997
PendingHTLCStatus :: Forward ( PendingHTLCInfo {
985
- type_data : PendingForwardReceiveHTLCInfo :: Receive { } ,
998
+ type_data : PendingForwardReceiveHTLCInfo :: Receive { payment_data } ,
986
999
payment_hash : msg. payment_hash . clone ( ) ,
987
1000
incoming_shared_secret : shared_secret,
988
1001
amt_to_forward : next_hop_data. amt_to_forward ,
@@ -1538,17 +1551,18 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1538
1551
for forward_info in pending_forwards. drain ( ..) {
1539
1552
match forward_info {
1540
1553
HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info : PendingHTLCInfo {
1541
- type_data : PendingForwardReceiveHTLCInfo :: Receive { } ,
1554
+ type_data : PendingForwardReceiveHTLCInfo :: Receive { payment_data } ,
1542
1555
incoming_shared_secret, payment_hash, amt_to_forward, .. } , } => {
1543
1556
let prev_hop_data = HTLCPreviousHopData {
1544
1557
short_channel_id : prev_short_channel_id,
1545
1558
htlc_id : prev_htlc_id,
1546
1559
incoming_packet_shared_secret : incoming_shared_secret,
1547
1560
} ;
1548
- match channel_state. claimable_htlcs . entry ( payment_hash) {
1549
- hash_map:: Entry :: Occupied ( mut entry) => entry. get_mut ( ) . push ( ( amt_to_forward, prev_hop_data) ) ,
1550
- hash_map:: Entry :: Vacant ( entry) => { entry. insert ( vec ! [ ( amt_to_forward, prev_hop_data) ] ) ; } ,
1551
- } ;
1561
+ channel_state. claimable_htlcs . entry ( payment_hash) . or_insert ( Vec :: new ( ) ) . push ( ClaimableHTLC {
1562
+ src : prev_hop_data,
1563
+ value : amt_to_forward,
1564
+ payment_data,
1565
+ } ) ;
1552
1566
new_events. push ( events:: Event :: PaymentReceived {
1553
1567
payment_hash : payment_hash,
1554
1568
amt : amt_to_forward,
@@ -1621,11 +1635,11 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1621
1635
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1622
1636
let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( payment_hash) ;
1623
1637
if let Some ( mut sources) = removed_source {
1624
- for ( recvd_value , htlc_with_hash ) in sources. drain ( ..) {
1638
+ for htlc in sources. drain ( ..) {
1625
1639
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
1626
1640
self . fail_htlc_backwards_internal ( channel_state. take ( ) . unwrap ( ) ,
1627
- HTLCSource :: PreviousHopData ( htlc_with_hash ) , payment_hash,
1628
- HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( recvd_value ) . to_vec ( ) } ) ;
1641
+ HTLCSource :: PreviousHopData ( htlc . src ) , payment_hash,
1642
+ HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( htlc . value ) . to_vec ( ) } ) ;
1629
1643
}
1630
1644
true
1631
1645
} else { false }
@@ -1749,17 +1763,17 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
1749
1763
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1750
1764
let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
1751
1765
if let Some ( mut sources) = removed_source {
1752
- for ( received_amount , htlc_with_hash ) in sources. drain ( ..) {
1766
+ for htlc in sources. drain ( ..) {
1753
1767
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
1754
- if received_amount < expected_amount || received_amount > expected_amount * 2 {
1755
- let mut htlc_msat_data = byte_utils:: be64_to_array ( received_amount ) . to_vec ( ) ;
1768
+ if htlc . value < expected_amount || htlc . value > expected_amount * 2 {
1769
+ let mut htlc_msat_data = byte_utils:: be64_to_array ( htlc . value ) . to_vec ( ) ;
1756
1770
let mut height_data = byte_utils:: be32_to_array ( self . latest_block_height . load ( Ordering :: Acquire ) as u32 ) . to_vec ( ) ;
1757
1771
htlc_msat_data. append ( & mut height_data) ;
1758
1772
self . fail_htlc_backwards_internal ( channel_state. take ( ) . unwrap ( ) ,
1759
- HTLCSource :: PreviousHopData ( htlc_with_hash ) , & payment_hash,
1773
+ HTLCSource :: PreviousHopData ( htlc . src ) , & payment_hash,
1760
1774
HTLCFailReason :: Reason { failure_code : 0x4000 |15 , data : htlc_msat_data } ) ;
1761
1775
} else {
1762
- self . claim_funds_internal ( channel_state. take ( ) . unwrap ( ) , HTLCSource :: PreviousHopData ( htlc_with_hash ) , payment_preimage) ;
1776
+ self . claim_funds_internal ( channel_state. take ( ) . unwrap ( ) , HTLCSource :: PreviousHopData ( htlc . src ) , payment_preimage) ;
1763
1777
}
1764
1778
}
1765
1779
true
@@ -3076,8 +3090,9 @@ impl Writeable for PendingHTLCInfo {
3076
3090
onion_packet. write ( writer) ?;
3077
3091
short_channel_id. write ( writer) ?;
3078
3092
} ,
3079
- & PendingForwardReceiveHTLCInfo :: Receive { } => {
3093
+ & PendingForwardReceiveHTLCInfo :: Receive { ref payment_data } => {
3080
3094
1u8 . write ( writer) ?;
3095
+ payment_data. write ( writer) ?;
3081
3096
} ,
3082
3097
}
3083
3098
self . incoming_shared_secret . write ( writer) ?;
@@ -3096,7 +3111,9 @@ impl<R: ::std::io::Read> Readable<R> for PendingHTLCInfo {
3096
3111
onion_packet : Readable :: read ( reader) ?,
3097
3112
short_channel_id : Readable :: read ( reader) ?,
3098
3113
} ,
3099
- 1u8 => PendingForwardReceiveHTLCInfo :: Receive { } ,
3114
+ 1u8 => PendingForwardReceiveHTLCInfo :: Receive {
3115
+ payment_data : Readable :: read ( reader) ?,
3116
+ } ,
3100
3117
_ => return Err ( DecodeError :: InvalidValue ) ,
3101
3118
} ,
3102
3119
incoming_shared_secret : Readable :: read ( reader) ?,
@@ -3301,9 +3318,10 @@ impl<ChanSigner: ChannelKeys + Writeable, M: Deref> Writeable for ChannelManager
3301
3318
for ( payment_hash, previous_hops) in channel_state. claimable_htlcs . iter ( ) {
3302
3319
payment_hash. write ( writer) ?;
3303
3320
( previous_hops. len ( ) as u64 ) . write ( writer) ?;
3304
- for & ( recvd_amt, ref previous_hop) in previous_hops. iter ( ) {
3305
- recvd_amt. write ( writer) ?;
3306
- previous_hop. write ( writer) ?;
3321
+ for htlc in previous_hops. iter ( ) {
3322
+ htlc. src . write ( writer) ?;
3323
+ htlc. value . write ( writer) ?;
3324
+ htlc. payment_data . write ( writer) ?;
3307
3325
}
3308
3326
}
3309
3327
@@ -3445,7 +3463,11 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> R
3445
3463
let previous_hops_len: u64 = Readable :: read ( reader) ?;
3446
3464
let mut previous_hops = Vec :: with_capacity ( cmp:: min ( previous_hops_len as usize , 2 ) ) ;
3447
3465
for _ in 0 ..previous_hops_len {
3448
- previous_hops. push ( ( Readable :: read ( reader) ?, Readable :: read ( reader) ?) ) ;
3466
+ previous_hops. push ( ClaimableHTLC {
3467
+ src : Readable :: read ( reader) ?,
3468
+ value : Readable :: read ( reader) ?,
3469
+ payment_data : Readable :: read ( reader) ?,
3470
+ } ) ;
3449
3471
}
3450
3472
claimable_htlcs. insert ( payment_hash, previous_hops) ;
3451
3473
}
0 commit comments