@@ -72,7 +72,9 @@ enum PendingForwardReceiveHTLCInfo {
72
72
onion_packet : msgs:: OnionPacket ,
73
73
short_channel_id : u64 , // This should be NonZero<u64> eventually
74
74
} ,
75
- Receive { } ,
75
+ Receive {
76
+ payment_data : Option < msgs:: FinalOnionHopData > ,
77
+ } ,
76
78
}
77
79
78
80
#[ derive( Clone ) ] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
@@ -117,6 +119,12 @@ pub(super) struct HTLCPreviousHopData {
117
119
incoming_packet_shared_secret : [ u8 ; 32 ] ,
118
120
}
119
121
122
+ struct ClaimableHTLC {
123
+ src : HTLCPreviousHopData ,
124
+ value : u64 ,
125
+ payment_data : Option < msgs:: FinalOnionHopData > ,
126
+ }
127
+
120
128
/// Tracks the inbound corresponding to an outbound HTLC
121
129
#[ derive( Clone , PartialEq ) ]
122
130
pub ( super ) enum HTLCSource {
@@ -274,12 +282,11 @@ pub(super) struct ChannelHolder<ChanSigner: ChannelKeys> {
274
282
/// guarantees are made about the existence of a channel with the short id here, nor the short
275
283
/// ids in the PendingHTLCInfo!
276
284
pub ( super ) forward_htlcs : HashMap < u64 , Vec < HTLCForwardInfo > > ,
277
- /// payment_hash -> Vec<(amount_received, htlc_source)> for tracking things that were to us and
278
- /// can be failed/claimed by the user
285
+ /// Tracks things that were to us and can be failed/claimed by the user
279
286
/// Note that while this is held in the same mutex as the channels themselves, no consistency
280
287
/// guarantees are made about the channels given here actually existing anymore by the time you
281
288
/// go to read them!
282
- pub ( super ) claimable_htlcs : HashMap < PaymentHash , Vec < ( u64 , HTLCPreviousHopData ) > > ,
289
+ claimable_htlcs : HashMap < PaymentHash , Vec < ClaimableHTLC > > ,
283
290
/// Messages to send to peers - pushed to in the same lock that they are generated in (except
284
291
/// for broadcast messages, where ordering isn't as strict).
285
292
pub ( super ) pending_msg_events : Vec < events:: MessageSendEvent > ,
@@ -288,7 +295,7 @@ pub(super) struct MutChannelHolder<'a, ChanSigner: ChannelKeys + 'a> {
288
295
pub ( super ) by_id : & ' a mut HashMap < [ u8 ; 32 ] , Channel < ChanSigner > > ,
289
296
pub ( super ) short_to_id : & ' a mut HashMap < u64 , [ u8 ; 32 ] > ,
290
297
pub ( super ) forward_htlcs : & ' a mut HashMap < u64 , Vec < HTLCForwardInfo > > ,
291
- pub ( super ) claimable_htlcs : & ' a mut HashMap < PaymentHash , Vec < ( u64 , HTLCPreviousHopData ) > > ,
298
+ claimable_htlcs : & ' a mut HashMap < PaymentHash , Vec < ClaimableHTLC > > ,
292
299
pub ( super ) pending_msg_events : & ' a mut Vec < events:: MessageSendEvent > ,
293
300
}
294
301
impl < ChanSigner : ChannelKeys > ChannelHolder < ChanSigner > {
@@ -993,13 +1000,19 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
993
1000
return_err ! ( "Upstream node set CLTV to the wrong value" , 18 , & byte_utils:: be32_to_array( msg. cltv_expiry) ) ;
994
1001
}
995
1002
1003
+ let payment_data = match next_hop_data. format {
1004
+ msgs:: OnionHopDataFormat :: Legacy { .. } => None ,
1005
+ msgs:: OnionHopDataFormat :: NonFinalNode { .. } => return_err ! ( "Got non final data with an HMAC of 0" , 0x4000 | 22 , & [ 0 ; 0 ] ) ,
1006
+ msgs:: OnionHopDataFormat :: FinalNode { payment_data } => payment_data,
1007
+ } ;
1008
+
996
1009
// Note that we could obviously respond immediately with an update_fulfill_htlc
997
1010
// message, however that would leak that we are the recipient of this payment, so
998
1011
// instead we stay symmetric with the forwarding case, only responding (after a
999
1012
// delay) once they've send us a commitment_signed!
1000
1013
1001
1014
PendingHTLCStatus :: Forward ( PendingHTLCInfo {
1002
- type_data : PendingForwardReceiveHTLCInfo :: Receive { } ,
1015
+ type_data : PendingForwardReceiveHTLCInfo :: Receive { payment_data } ,
1003
1016
payment_hash : msg. payment_hash . clone ( ) ,
1004
1017
incoming_shared_secret : shared_secret,
1005
1018
amt_to_forward : next_hop_data. amt_to_forward ,
@@ -1548,17 +1561,18 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1548
1561
for forward_info in pending_forwards. drain ( ..) {
1549
1562
match forward_info {
1550
1563
HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info : PendingHTLCInfo {
1551
- type_data : PendingForwardReceiveHTLCInfo :: Receive { } ,
1564
+ type_data : PendingForwardReceiveHTLCInfo :: Receive { payment_data } ,
1552
1565
incoming_shared_secret, payment_hash, amt_to_forward, .. } , } => {
1553
1566
let prev_hop_data = HTLCPreviousHopData {
1554
1567
short_channel_id : prev_short_channel_id,
1555
1568
htlc_id : prev_htlc_id,
1556
1569
incoming_packet_shared_secret : incoming_shared_secret,
1557
1570
} ;
1558
- match channel_state. claimable_htlcs . entry ( payment_hash) {
1559
- hash_map:: Entry :: Occupied ( mut entry) => entry. get_mut ( ) . push ( ( amt_to_forward, prev_hop_data) ) ,
1560
- hash_map:: Entry :: Vacant ( entry) => { entry. insert ( vec ! [ ( amt_to_forward, prev_hop_data) ] ) ; } ,
1561
- } ;
1571
+ channel_state. claimable_htlcs . entry ( payment_hash) . or_insert ( Vec :: new ( ) ) . push ( ClaimableHTLC {
1572
+ src : prev_hop_data,
1573
+ value : amt_to_forward,
1574
+ payment_data,
1575
+ } ) ;
1562
1576
new_events. push ( events:: Event :: PaymentReceived {
1563
1577
payment_hash : payment_hash,
1564
1578
amt : amt_to_forward,
@@ -1631,11 +1645,11 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1631
1645
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1632
1646
let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( payment_hash) ;
1633
1647
if let Some ( mut sources) = removed_source {
1634
- for ( recvd_value , htlc_with_hash ) in sources. drain ( ..) {
1648
+ for htlc in sources. drain ( ..) {
1635
1649
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
1636
1650
self . fail_htlc_backwards_internal ( channel_state. take ( ) . unwrap ( ) ,
1637
- HTLCSource :: PreviousHopData ( htlc_with_hash ) , payment_hash,
1638
- HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( recvd_value ) . to_vec ( ) } ) ;
1651
+ HTLCSource :: PreviousHopData ( htlc . src ) , payment_hash,
1652
+ HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( htlc . value ) . to_vec ( ) } ) ;
1639
1653
}
1640
1654
true
1641
1655
} else { false }
@@ -1759,17 +1773,17 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1759
1773
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1760
1774
let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
1761
1775
if let Some ( mut sources) = removed_source {
1762
- for ( received_amount , htlc_with_hash ) in sources. drain ( ..) {
1776
+ for htlc in sources. drain ( ..) {
1763
1777
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
1764
- if received_amount < expected_amount || received_amount > expected_amount * 2 {
1765
- let mut htlc_msat_data = byte_utils:: be64_to_array ( received_amount ) . to_vec ( ) ;
1778
+ if htlc . value < expected_amount || htlc . value > expected_amount * 2 {
1779
+ let mut htlc_msat_data = byte_utils:: be64_to_array ( htlc . value ) . to_vec ( ) ;
1766
1780
let mut height_data = byte_utils:: be32_to_array ( self . latest_block_height . load ( Ordering :: Acquire ) as u32 ) . to_vec ( ) ;
1767
1781
htlc_msat_data. append ( & mut height_data) ;
1768
1782
self . fail_htlc_backwards_internal ( channel_state. take ( ) . unwrap ( ) ,
1769
- HTLCSource :: PreviousHopData ( htlc_with_hash ) , & payment_hash,
1783
+ HTLCSource :: PreviousHopData ( htlc . src ) , & payment_hash,
1770
1784
HTLCFailReason :: Reason { failure_code : 0x4000 |15 , data : htlc_msat_data } ) ;
1771
1785
} else {
1772
- self . claim_funds_internal ( channel_state. take ( ) . unwrap ( ) , HTLCSource :: PreviousHopData ( htlc_with_hash ) , payment_preimage) ;
1786
+ self . claim_funds_internal ( channel_state. take ( ) . unwrap ( ) , HTLCSource :: PreviousHopData ( htlc . src ) , payment_preimage) ;
1773
1787
}
1774
1788
}
1775
1789
true
@@ -3079,8 +3093,9 @@ impl Writeable for PendingHTLCInfo {
3079
3093
onion_packet. write ( writer) ?;
3080
3094
short_channel_id. write ( writer) ?;
3081
3095
} ,
3082
- & PendingForwardReceiveHTLCInfo :: Receive { } => {
3096
+ & PendingForwardReceiveHTLCInfo :: Receive { ref payment_data } => {
3083
3097
1u8 . write ( writer) ?;
3098
+ payment_data. write ( writer) ?;
3084
3099
} ,
3085
3100
}
3086
3101
self . incoming_shared_secret . write ( writer) ?;
@@ -3099,7 +3114,9 @@ impl<R: ::std::io::Read> Readable<R> for PendingHTLCInfo {
3099
3114
onion_packet : Readable :: read ( reader) ?,
3100
3115
short_channel_id : Readable :: read ( reader) ?,
3101
3116
} ,
3102
- 1u8 => PendingForwardReceiveHTLCInfo :: Receive { } ,
3117
+ 1u8 => PendingForwardReceiveHTLCInfo :: Receive {
3118
+ payment_data : Readable :: read ( reader) ?,
3119
+ } ,
3103
3120
_ => return Err ( DecodeError :: InvalidValue ) ,
3104
3121
} ,
3105
3122
incoming_shared_secret : Readable :: read ( reader) ?,
@@ -3304,9 +3321,10 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for ChannelManager<ChanSigne
3304
3321
for ( payment_hash, previous_hops) in channel_state. claimable_htlcs . iter ( ) {
3305
3322
payment_hash. write ( writer) ?;
3306
3323
( previous_hops. len ( ) as u64 ) . write ( writer) ?;
3307
- for & ( recvd_amt, ref previous_hop) in previous_hops. iter ( ) {
3308
- recvd_amt. write ( writer) ?;
3309
- previous_hop. write ( writer) ?;
3324
+ for htlc in previous_hops. iter ( ) {
3325
+ htlc. src . write ( writer) ?;
3326
+ htlc. value . write ( writer) ?;
3327
+ htlc. payment_data . write ( writer) ?;
3310
3328
}
3311
3329
}
3312
3330
@@ -3448,7 +3466,11 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArg
3448
3466
let previous_hops_len: u64 = Readable :: read ( reader) ?;
3449
3467
let mut previous_hops = Vec :: with_capacity ( cmp:: min ( previous_hops_len as usize , 2 ) ) ;
3450
3468
for _ in 0 ..previous_hops_len {
3451
- previous_hops. push ( ( Readable :: read ( reader) ?, Readable :: read ( reader) ?) ) ;
3469
+ previous_hops. push ( ClaimableHTLC {
3470
+ src : Readable :: read ( reader) ?,
3471
+ value : Readable :: read ( reader) ?,
3472
+ payment_data : Readable :: read ( reader) ?,
3473
+ } ) ;
3452
3474
}
3453
3475
claimable_htlcs. insert ( payment_hash, previous_hops) ;
3454
3476
}
0 commit comments