@@ -66,12 +66,20 @@ use std::time::Duration;
66
66
// Alternatively, we can fill an outbound HTLC with a HTLCSource::OutboundRoute indicating this is
67
67
// our payment, which we can use to decode errors or inform the user that the payment was sent.
68
68
69
+ #[ derive( Clone ) ] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
70
+ enum PendingForwardReceiveHTLCInfo {
71
+ Forward {
72
+ onion_packet : msgs:: OnionPacket ,
73
+ short_channel_id : u64 , // This should be NonZero<u64> eventually
74
+ } ,
75
+ Receive { } ,
76
+ }
77
+
69
78
#[ derive( Clone ) ] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
70
79
pub ( super ) struct PendingHTLCInfo {
71
- onion_packet : Option < msgs :: OnionPacket > ,
80
+ type_data : PendingForwardReceiveHTLCInfo ,
72
81
incoming_shared_secret : [ u8 ; 32 ] ,
73
82
payment_hash : PaymentHash ,
74
- short_channel_id : u64 ,
75
83
pub ( super ) amt_to_forward : u64 ,
76
84
pub ( super ) outgoing_cltv_value : u32 ,
77
85
}
@@ -991,9 +999,8 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
991
999
// delay) once they've send us a commitment_signed!
992
1000
993
1001
PendingHTLCStatus :: Forward ( PendingHTLCInfo {
994
- onion_packet : None ,
1002
+ type_data : PendingForwardReceiveHTLCInfo :: Receive { } ,
995
1003
payment_hash : msg. payment_hash . clone ( ) ,
996
- short_channel_id : 0 ,
997
1004
incoming_shared_secret : shared_secret,
998
1005
amt_to_forward : next_hop_data. amt_to_forward ,
999
1006
outgoing_cltv_value : next_hop_data. outgoing_cltv_value ,
@@ -1035,24 +1042,29 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1035
1042
let short_channel_id = match next_hop_data. format {
1036
1043
msgs:: OnionHopDataFormat :: Legacy { short_channel_id } => short_channel_id,
1037
1044
msgs:: OnionHopDataFormat :: NonFinalNode { short_channel_id } => short_channel_id,
1038
- msgs:: OnionHopDataFormat :: FinalNode => {
1045
+ msgs:: OnionHopDataFormat :: FinalNode { .. } => {
1039
1046
return_err ! ( "Final Node OnionHopData provided for us as an intermediary node" , 0x4000 | 22 , & [ 0 ; 0 ] ) ;
1040
1047
} ,
1041
1048
} ;
1042
1049
1043
1050
PendingHTLCStatus :: Forward ( PendingHTLCInfo {
1044
- onion_packet : Some ( outgoing_packet) ,
1051
+ type_data : PendingForwardReceiveHTLCInfo :: Forward {
1052
+ onion_packet : outgoing_packet,
1053
+ short_channel_id : short_channel_id,
1054
+ } ,
1045
1055
payment_hash : msg. payment_hash . clone ( ) ,
1046
- short_channel_id : short_channel_id,
1047
1056
incoming_shared_secret : shared_secret,
1048
1057
amt_to_forward : next_hop_data. amt_to_forward ,
1049
1058
outgoing_cltv_value : next_hop_data. outgoing_cltv_value ,
1050
1059
} )
1051
1060
} ;
1052
1061
1053
1062
channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1054
- if let & PendingHTLCStatus :: Forward ( PendingHTLCInfo { ref onion_packet, ref short_channel_id, ref amt_to_forward, ref outgoing_cltv_value, .. } ) = & pending_forward_info {
1055
- if onion_packet. is_some ( ) { // If short_channel_id is 0 here, we'll reject them in the body here
1063
+ if let & PendingHTLCStatus :: Forward ( PendingHTLCInfo { ref type_data, ref amt_to_forward, ref outgoing_cltv_value, .. } ) = & pending_forward_info {
1064
+ // If short_channel_id is 0 here, we'll reject them in the body here (which is
1065
+ // important as various things later assume we are a ::Receive if short_channel_id is
1066
+ // non-0.
1067
+ if let & PendingForwardReceiveHTLCInfo :: Forward { ref short_channel_id, .. } = type_data {
1056
1068
let id_option = channel_state. as_ref ( ) . unwrap ( ) . short_to_id . get ( & short_channel_id) . cloned ( ) ;
1057
1069
let forwarding_id = match id_option {
1058
1070
None => { // unknown_next_peer
@@ -1416,22 +1428,25 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1416
1428
let mut fail_htlc_msgs = Vec :: new ( ) ;
1417
1429
for forward_info in pending_forwards. drain ( ..) {
1418
1430
match forward_info {
1419
- HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
1420
- log_trace ! ( self , "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay" , log_bytes!( forward_info. payment_hash. 0 ) , prev_short_channel_id, short_chan_id) ;
1431
+ HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info : PendingHTLCInfo {
1432
+ type_data : PendingForwardReceiveHTLCInfo :: Forward {
1433
+ onion_packet, ..
1434
+ } , incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value } , } => {
1435
+ log_trace ! ( self , "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay" , log_bytes!( payment_hash. 0 ) , prev_short_channel_id, short_chan_id) ;
1421
1436
let htlc_source = HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1422
1437
short_channel_id : prev_short_channel_id,
1423
1438
htlc_id : prev_htlc_id,
1424
- incoming_packet_shared_secret : forward_info . incoming_shared_secret ,
1439
+ incoming_packet_shared_secret : incoming_shared_secret,
1425
1440
} ) ;
1426
- match chan. get_mut ( ) . send_htlc ( forward_info . amt_to_forward , forward_info . payment_hash , forward_info . outgoing_cltv_value , htlc_source. clone ( ) , forward_info . onion_packet . unwrap ( ) ) {
1441
+ match chan. get_mut ( ) . send_htlc ( amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source. clone ( ) , onion_packet) {
1427
1442
Err ( e) => {
1428
1443
if let ChannelError :: Ignore ( msg) = e {
1429
- log_trace ! ( self , "Failed to forward HTLC with payment_hash {}: {}" , log_bytes!( forward_info . payment_hash. 0 ) , msg) ;
1444
+ log_trace ! ( self , "Failed to forward HTLC with payment_hash {}: {}" , log_bytes!( payment_hash. 0 ) , msg) ;
1430
1445
} else {
1431
1446
panic ! ( "Stated return value requirements in send_htlc() were not met" ) ;
1432
1447
}
1433
1448
let chan_update = self . get_channel_update ( chan. get ( ) ) . unwrap ( ) ;
1434
- failed_forwards. push ( ( htlc_source, forward_info . payment_hash , 0x1000 | 7 , Some ( chan_update) ) ) ;
1449
+ failed_forwards. push ( ( htlc_source, payment_hash, 0x1000 | 7 , Some ( chan_update) ) ) ;
1435
1450
continue ;
1436
1451
} ,
1437
1452
Ok ( update_add) => {
@@ -1450,6 +1465,9 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1450
1465
}
1451
1466
}
1452
1467
} ,
1468
+ HTLCForwardInfo :: AddHTLC { .. } => {
1469
+ panic ! ( "short_channel_id != 0 should imply any pending_forward entries are of type Forward" ) ;
1470
+ } ,
1453
1471
HTLCForwardInfo :: FailHTLC { htlc_id, err_packet } => {
1454
1472
log_trace ! ( self , "Failing HTLC back to channel with short id {} after delay" , short_chan_id) ;
1455
1473
match chan. get_mut ( ) . get_update_fail_htlc ( htlc_id, err_packet) {
@@ -1529,21 +1547,26 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
1529
1547
} else {
1530
1548
for forward_info in pending_forwards. drain ( ..) {
1531
1549
match forward_info {
1532
- HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } => {
1550
+ HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info : PendingHTLCInfo {
1551
+ type_data : PendingForwardReceiveHTLCInfo :: Receive { } ,
1552
+ incoming_shared_secret, payment_hash, amt_to_forward, .. } , } => {
1533
1553
let prev_hop_data = HTLCPreviousHopData {
1534
1554
short_channel_id : prev_short_channel_id,
1535
1555
htlc_id : prev_htlc_id,
1536
- incoming_packet_shared_secret : forward_info . incoming_shared_secret ,
1556
+ incoming_packet_shared_secret : incoming_shared_secret,
1537
1557
} ;
1538
- match channel_state. claimable_htlcs . entry ( forward_info . payment_hash ) {
1539
- hash_map:: Entry :: Occupied ( mut entry) => entry. get_mut ( ) . push ( ( forward_info . amt_to_forward , prev_hop_data) ) ,
1540
- hash_map:: Entry :: Vacant ( entry) => { entry. insert ( vec ! [ ( forward_info . amt_to_forward, prev_hop_data) ] ) ; } ,
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) ] ) ; } ,
1541
1561
} ;
1542
1562
new_events. push ( events:: Event :: PaymentReceived {
1543
- payment_hash : forward_info . payment_hash ,
1544
- amt : forward_info . amt_to_forward ,
1563
+ payment_hash : payment_hash,
1564
+ amt : amt_to_forward,
1545
1565
} ) ;
1546
1566
} ,
1567
+ HTLCForwardInfo :: AddHTLC { .. } => {
1568
+ panic ! ( "short_channel_id == 0 should imply any pending_forward entries are of type Receive" ) ;
1569
+ } ,
1547
1570
HTLCForwardInfo :: FailHTLC { .. } => {
1548
1571
panic ! ( "Got pending fail of our own HTLC" ) ;
1549
1572
}
@@ -2343,7 +2366,10 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
2343
2366
forward_event = Some ( Duration :: from_millis ( MIN_HTLC_RELAY_HOLDING_CELL_MILLIS ) )
2344
2367
}
2345
2368
for ( forward_info, prev_htlc_id) in pending_forwards. drain ( ..) {
2346
- match channel_state. forward_htlcs . entry ( forward_info. short_channel_id ) {
2369
+ match channel_state. forward_htlcs . entry ( match forward_info. type_data {
2370
+ PendingForwardReceiveHTLCInfo :: Forward { short_channel_id, .. } => short_channel_id,
2371
+ PendingForwardReceiveHTLCInfo :: Receive { .. } => 0 ,
2372
+ } ) {
2347
2373
hash_map:: Entry :: Occupied ( mut entry) => {
2348
2374
entry. get_mut ( ) . push ( HTLCForwardInfo :: AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info } ) ;
2349
2375
} ,
@@ -3047,10 +3073,18 @@ const MIN_SERIALIZATION_VERSION: u8 = 1;
3047
3073
3048
3074
impl Writeable for PendingHTLCInfo {
3049
3075
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
3050
- self . onion_packet . write ( writer) ?;
3076
+ match & self . type_data {
3077
+ & PendingForwardReceiveHTLCInfo :: Forward { ref onion_packet, ref short_channel_id } => {
3078
+ 0u8 . write ( writer) ?;
3079
+ onion_packet. write ( writer) ?;
3080
+ short_channel_id. write ( writer) ?;
3081
+ } ,
3082
+ & PendingForwardReceiveHTLCInfo :: Receive { } => {
3083
+ 1u8 . write ( writer) ?;
3084
+ } ,
3085
+ }
3051
3086
self . incoming_shared_secret . write ( writer) ?;
3052
3087
self . payment_hash . write ( writer) ?;
3053
- self . short_channel_id . write ( writer) ?;
3054
3088
self . amt_to_forward . write ( writer) ?;
3055
3089
self . outgoing_cltv_value . write ( writer) ?;
3056
3090
Ok ( ( ) )
@@ -3060,10 +3094,16 @@ impl Writeable for PendingHTLCInfo {
3060
3094
impl < R : :: std:: io:: Read > Readable < R > for PendingHTLCInfo {
3061
3095
fn read ( reader : & mut R ) -> Result < PendingHTLCInfo , DecodeError > {
3062
3096
Ok ( PendingHTLCInfo {
3063
- onion_packet : Readable :: read ( reader) ?,
3097
+ type_data : match Readable :: read ( reader) ? {
3098
+ 0u8 => PendingForwardReceiveHTLCInfo :: Forward {
3099
+ onion_packet : Readable :: read ( reader) ?,
3100
+ short_channel_id : Readable :: read ( reader) ?,
3101
+ } ,
3102
+ 1u8 => PendingForwardReceiveHTLCInfo :: Receive { } ,
3103
+ _ => return Err ( DecodeError :: InvalidValue ) ,
3104
+ } ,
3064
3105
incoming_shared_secret : Readable :: read ( reader) ?,
3065
3106
payment_hash : Readable :: read ( reader) ?,
3066
- short_channel_id : Readable :: read ( reader) ?,
3067
3107
amt_to_forward : Readable :: read ( reader) ?,
3068
3108
outgoing_cltv_value : Readable :: read ( reader) ?,
3069
3109
} )
0 commit comments