@@ -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
+ prev_hop : 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 HTLCs 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 > ,
@@ -987,13 +994,19 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
987
994
return_err ! ( "Upstream node set CLTV to the wrong value" , 18 , & byte_utils:: be32_to_array( msg. cltv_expiry) ) ;
988
995
}
989
996
997
+ let payment_data = match next_hop_data. format {
998
+ msgs:: OnionHopDataFormat :: Legacy { .. } => None ,
999
+ msgs:: OnionHopDataFormat :: NonFinalNode { .. } => return_err ! ( "Got non final data with an HMAC of 0" , 0x4000 | 22 , & [ 0 ; 0 ] ) ,
1000
+ msgs:: OnionHopDataFormat :: FinalNode { payment_data } => payment_data,
1001
+ } ;
1002
+
990
1003
// Note that we could obviously respond immediately with an update_fulfill_htlc
991
1004
// message, however that would leak that we are the recipient of this payment, so
992
1005
// instead we stay symmetric with the forwarding case, only responding (after a
993
1006
// delay) once they've send us a commitment_signed!
994
1007
995
1008
PendingHTLCStatus :: Forward ( PendingHTLCInfo {
996
- forward_or_receive : PendingForwardReceiveHTLCInfo :: Receive { } ,
1009
+ forward_or_receive : PendingForwardReceiveHTLCInfo :: Receive { payment_data } ,
997
1010
payment_hash : msg. payment_hash . clone ( ) ,
998
1011
incoming_shared_secret : shared_secret,
999
1012
amt_to_forward : next_hop_data. amt_to_forward ,
@@ -1567,17 +1580,18 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1567
1580
for forward_info in pending_forwards. drain ( ..) {
1568
1581
match forward_info {
1569
1582
HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info : PendingHTLCInfo {
1570
- forward_or_receive : PendingForwardReceiveHTLCInfo :: Receive { } ,
1583
+ forward_or_receive : PendingForwardReceiveHTLCInfo :: Receive { payment_data } ,
1571
1584
incoming_shared_secret, payment_hash, amt_to_forward, .. } , } => {
1572
- let prev_hop_data = HTLCPreviousHopData {
1585
+ let prev_hop = HTLCPreviousHopData {
1573
1586
short_channel_id : prev_short_channel_id,
1574
1587
htlc_id : prev_htlc_id,
1575
1588
incoming_packet_shared_secret : incoming_shared_secret,
1576
1589
} ;
1577
- match channel_state. claimable_htlcs . entry ( payment_hash) {
1578
- hash_map:: Entry :: Occupied ( mut entry) => entry. get_mut ( ) . push ( ( amt_to_forward, prev_hop_data) ) ,
1579
- hash_map:: Entry :: Vacant ( entry) => { entry. insert ( vec ! [ ( amt_to_forward, prev_hop_data) ] ) ; } ,
1580
- } ;
1590
+ channel_state. claimable_htlcs . entry ( payment_hash) . or_insert ( Vec :: new ( ) ) . push ( ClaimableHTLC {
1591
+ prev_hop,
1592
+ value : amt_to_forward,
1593
+ payment_data,
1594
+ } ) ;
1581
1595
new_events. push ( events:: Event :: PaymentReceived {
1582
1596
payment_hash : payment_hash,
1583
1597
amt : amt_to_forward,
@@ -1650,11 +1664,11 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1650
1664
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1651
1665
let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( payment_hash) ;
1652
1666
if let Some ( mut sources) = removed_source {
1653
- for ( recvd_value , htlc_with_hash ) in sources. drain ( ..) {
1667
+ for htlc in sources. drain ( ..) {
1654
1668
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
1655
1669
self . fail_htlc_backwards_internal ( channel_state. take ( ) . unwrap ( ) ,
1656
- HTLCSource :: PreviousHopData ( htlc_with_hash ) , payment_hash,
1657
- HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( recvd_value ) . to_vec ( ) } ) ;
1670
+ HTLCSource :: PreviousHopData ( htlc . prev_hop ) , payment_hash,
1671
+ HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( htlc . value ) . to_vec ( ) } ) ;
1658
1672
}
1659
1673
true
1660
1674
} else { false }
@@ -1778,17 +1792,17 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1778
1792
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1779
1793
let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
1780
1794
if let Some ( mut sources) = removed_source {
1781
- for ( received_amount , htlc_with_hash ) in sources. drain ( ..) {
1795
+ for htlc in sources. drain ( ..) {
1782
1796
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
1783
- if received_amount < expected_amount || received_amount > expected_amount * 2 {
1784
- let mut htlc_msat_data = byte_utils:: be64_to_array ( received_amount ) . to_vec ( ) ;
1797
+ if htlc . value < expected_amount || htlc . value > expected_amount * 2 {
1798
+ let mut htlc_msat_data = byte_utils:: be64_to_array ( htlc . value ) . to_vec ( ) ;
1785
1799
let mut height_data = byte_utils:: be32_to_array ( self . latest_block_height . load ( Ordering :: Acquire ) as u32 ) . to_vec ( ) ;
1786
1800
htlc_msat_data. append ( & mut height_data) ;
1787
1801
self . fail_htlc_backwards_internal ( channel_state. take ( ) . unwrap ( ) ,
1788
- HTLCSource :: PreviousHopData ( htlc_with_hash ) , & payment_hash,
1802
+ HTLCSource :: PreviousHopData ( htlc . prev_hop ) , & payment_hash,
1789
1803
HTLCFailReason :: Reason { failure_code : 0x4000 |15 , data : htlc_msat_data } ) ;
1790
1804
} else {
1791
- self . claim_funds_internal ( channel_state. take ( ) . unwrap ( ) , HTLCSource :: PreviousHopData ( htlc_with_hash ) , payment_preimage) ;
1805
+ self . claim_funds_internal ( channel_state. take ( ) . unwrap ( ) , HTLCSource :: PreviousHopData ( htlc . prev_hop ) , payment_preimage) ;
1792
1806
}
1793
1807
}
1794
1808
true
@@ -3151,8 +3165,9 @@ impl Writeable for PendingHTLCInfo {
3151
3165
onion_packet. write ( writer) ?;
3152
3166
short_channel_id. write ( writer) ?;
3153
3167
} ,
3154
- & PendingForwardReceiveHTLCInfo :: Receive { } => {
3168
+ & PendingForwardReceiveHTLCInfo :: Receive { ref payment_data } => {
3155
3169
1u8 . write ( writer) ?;
3170
+ payment_data. write ( writer) ?;
3156
3171
} ,
3157
3172
}
3158
3173
self . incoming_shared_secret . write ( writer) ?;
@@ -3171,7 +3186,9 @@ impl Readable for PendingHTLCInfo {
3171
3186
onion_packet : Readable :: read ( reader) ?,
3172
3187
short_channel_id : Readable :: read ( reader) ?,
3173
3188
} ,
3174
- 1u8 => PendingForwardReceiveHTLCInfo :: Receive { } ,
3189
+ 1u8 => PendingForwardReceiveHTLCInfo :: Receive {
3190
+ payment_data : Readable :: read ( reader) ?,
3191
+ } ,
3175
3192
_ => return Err ( DecodeError :: InvalidValue ) ,
3176
3193
} ,
3177
3194
incoming_shared_secret : Readable :: read ( reader) ?,
@@ -3381,9 +3398,10 @@ impl<ChanSigner: ChannelKeys + Writeable, M: Deref, T: Deref, K: Deref, F: Deref
3381
3398
for ( payment_hash, previous_hops) in channel_state. claimable_htlcs . iter ( ) {
3382
3399
payment_hash. write ( writer) ?;
3383
3400
( previous_hops. len ( ) as u64 ) . write ( writer) ?;
3384
- for & ( recvd_amt, ref previous_hop) in previous_hops. iter ( ) {
3385
- recvd_amt. write ( writer) ?;
3386
- previous_hop. write ( writer) ?;
3401
+ for htlc in previous_hops. iter ( ) {
3402
+ htlc. prev_hop . write ( writer) ?;
3403
+ htlc. value . write ( writer) ?;
3404
+ htlc. payment_data . write ( writer) ?;
3387
3405
}
3388
3406
}
3389
3407
@@ -3553,7 +3571,11 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3553
3571
let previous_hops_len: u64 = Readable :: read ( reader) ?;
3554
3572
let mut previous_hops = Vec :: with_capacity ( cmp:: min ( previous_hops_len as usize , 2 ) ) ;
3555
3573
for _ in 0 ..previous_hops_len {
3556
- previous_hops. push ( ( Readable :: read ( reader) ?, Readable :: read ( reader) ?) ) ;
3574
+ previous_hops. push ( ClaimableHTLC {
3575
+ prev_hop : Readable :: read ( reader) ?,
3576
+ value : Readable :: read ( reader) ?,
3577
+ payment_data : Readable :: read ( reader) ?,
3578
+ } ) ;
3557
3579
}
3558
3580
claimable_htlcs. insert ( payment_hash, previous_hops) ;
3559
3581
}
0 commit comments