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