@@ -1039,6 +1039,132 @@ fn fake_network_test() {
1039
1039
close_channel ( & nodes[ 1 ] , & nodes[ 3 ] , & chan_5. 2 , chan_5. 3 , false ) ;
1040
1040
}
1041
1041
1042
+ #[ test]
1043
+ fn holding_cell_htlc_counting ( ) {
1044
+ // Tests that HTLCs in the holding cell count towards the pending HTLC limits on outbound HTLCs
1045
+ // to ensure we don't end up with HTLCs sitting around in our holding cell for several
1046
+ // commitment dance rounds.
1047
+ let mut nodes = create_network ( 3 ) ;
1048
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1049
+ let chan_2 = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
1050
+
1051
+ let mut payments = Vec :: new ( ) ;
1052
+ for _ in 0 ..:: ln:: channel:: OUR_MAX_HTLCS {
1053
+ let route = nodes[ 1 ] . router . get_route ( & nodes[ 2 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 100000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1054
+ let ( payment_preimage, payment_hash) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1055
+ nodes[ 1 ] . node . send_payment ( route, payment_hash) . unwrap ( ) ;
1056
+ payments. push ( ( payment_preimage, payment_hash) ) ;
1057
+ }
1058
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1059
+
1060
+ let mut events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1061
+ assert_eq ! ( events. len( ) , 1 ) ;
1062
+ let initial_payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1063
+ assert_eq ! ( initial_payment_event. node_id, nodes[ 2 ] . node. get_our_node_id( ) ) ;
1064
+
1065
+ // There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
1066
+ // the holding cell waiting on B's RAA to send. At this point we should not be able to add
1067
+ // another HTLC.
1068
+ let route = nodes[ 1 ] . router . get_route ( & nodes[ 2 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 100000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1069
+ let ( _, payment_hash_1) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1070
+ if let APIError :: ChannelUnavailable { err } = nodes[ 1 ] . node . send_payment ( route, payment_hash_1) . unwrap_err ( ) {
1071
+ assert_eq ! ( err, "Cannot push more than their max accepted HTLCs" ) ;
1072
+ } else { panic ! ( "Unexpected event" ) ; }
1073
+
1074
+ // This should also be true if we try to forward a payment.
1075
+ let route = nodes[ 0 ] . router . get_route ( & nodes[ 2 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 100000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1076
+ let ( _, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1077
+ nodes[ 0 ] . node . send_payment ( route, payment_hash_2) . unwrap ( ) ;
1078
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1079
+
1080
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1081
+ assert_eq ! ( events. len( ) , 1 ) ;
1082
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1083
+ assert_eq ! ( payment_event. node_id, nodes[ 1 ] . node. get_our_node_id( ) ) ;
1084
+
1085
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) . unwrap ( ) ;
1086
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , payment_event. commitment_msg, false ) ;
1087
+ // We have to forward pending HTLCs twice - once tries to forward the payment forward (and
1088
+ // fails), the second will process the resulting failure and fail the HTLC backward.
1089
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1090
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1091
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1092
+
1093
+ let bs_fail_updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1094
+ nodes[ 0 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_fail_updates. update_fail_htlcs [ 0 ] ) . unwrap ( ) ;
1095
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , bs_fail_updates. commitment_signed, false , true ) ;
1096
+
1097
+ let events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1098
+ assert_eq ! ( events. len( ) , 1 ) ;
1099
+ match events[ 0 ] {
1100
+ MessageSendEvent :: PaymentFailureNetworkUpdate { update : msgs:: HTLCFailChannelUpdate :: ChannelUpdateMessage { ref msg } } => {
1101
+ assert_eq ! ( msg. contents. short_channel_id, chan_2. 0 . contents. short_channel_id) ;
1102
+ } ,
1103
+ _ => panic ! ( "Unexpected event" ) ,
1104
+ }
1105
+
1106
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1107
+ assert_eq ! ( events. len( ) , 1 ) ;
1108
+ match events[ 0 ] {
1109
+ Event :: PaymentFailed { payment_hash, rejected_by_dest, .. } => {
1110
+ assert_eq ! ( payment_hash, payment_hash_2) ;
1111
+ assert ! ( !rejected_by_dest) ;
1112
+ } ,
1113
+ _ => panic ! ( "Unexpected event" ) ,
1114
+ }
1115
+
1116
+ // Now forward all the pending HTLCs and claim them back
1117
+ nodes[ 2 ] . node . handle_update_add_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & initial_payment_event. msgs [ 0 ] ) . unwrap ( ) ;
1118
+ nodes[ 2 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & initial_payment_event. commitment_msg ) . unwrap ( ) ;
1119
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1120
+
1121
+ let ( bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1122
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 2 ] . node . get_our_node_id ( ) , & bs_revoke_and_ack) . unwrap ( ) ;
1123
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1124
+ let as_updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
1125
+
1126
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & bs_commitment_signed) . unwrap ( ) ;
1127
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1128
+ let as_raa = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 2 ] . node. get_our_node_id( ) ) ;
1129
+
1130
+ for ref update in as_updates. update_add_htlcs . iter ( ) {
1131
+ nodes[ 2 ] . node . handle_update_add_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , update) . unwrap ( ) ;
1132
+ }
1133
+ nodes[ 2 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & as_updates. commitment_signed ) . unwrap ( ) ;
1134
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1135
+ nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & as_raa) . unwrap ( ) ;
1136
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1137
+ let ( bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1138
+
1139
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 2 ] . node . get_our_node_id ( ) , & bs_revoke_and_ack) . unwrap ( ) ;
1140
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1141
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & bs_commitment_signed) . unwrap ( ) ;
1142
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1143
+ let as_final_raa = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 2 ] . node. get_our_node_id( ) ) ;
1144
+
1145
+ nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & as_final_raa) . unwrap ( ) ;
1146
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1147
+
1148
+ expect_pending_htlcs_forwardable ! ( nodes[ 2 ] ) ;
1149
+
1150
+ let events = nodes[ 2 ] . node . get_and_clear_pending_events ( ) ;
1151
+ assert_eq ! ( events. len( ) , payments. len( ) ) ;
1152
+ for ( event, & ( _, ref hash) ) in events. iter ( ) . zip ( payments. iter ( ) ) {
1153
+ match event {
1154
+ & Event :: PaymentReceived { ref payment_hash, .. } => {
1155
+ assert_eq ! ( * payment_hash, * hash) ;
1156
+ } ,
1157
+ _ => panic ! ( "Unexpected event" ) ,
1158
+ } ;
1159
+ }
1160
+
1161
+ for ( preimage, _) in payments. drain ( ..) {
1162
+ claim_payment ( & nodes[ 1 ] , & [ & nodes[ 2 ] ] , preimage) ;
1163
+ }
1164
+
1165
+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 1000000 ) ;
1166
+ }
1167
+
1042
1168
#[ test]
1043
1169
fn duplicate_htlc_test ( ) {
1044
1170
// Test that we accept duplicate payment_hash HTLCs across the network and that
@@ -1109,7 +1235,7 @@ fn do_channel_reserve_test(test_recv: bool) {
1109
1235
assert ! ( route. hops. iter( ) . rev( ) . skip( 1 ) . all( |h| h. fee_msat == feemsat) ) ;
1110
1236
let err = nodes[ 0 ] . node . send_payment ( route, our_payment_hash) . err ( ) . unwrap ( ) ;
1111
1237
match err {
1112
- APIError :: ChannelUnavailable { err} => assert_eq ! ( err, "Cannot send value that would put us over our max HTLC value in flight" ) ,
1238
+ APIError :: ChannelUnavailable { err} => assert_eq ! ( err, "Cannot send value that would put us over the max HTLC value in flight" ) ,
1113
1239
_ => panic ! ( "Unknown error variants" ) ,
1114
1240
}
1115
1241
}
@@ -1145,7 +1271,7 @@ fn do_channel_reserve_test(test_recv: bool) {
1145
1271
let ( route, our_payment_hash, _) = get_route_and_payment_hash ! ( recv_value + 1 ) ;
1146
1272
let err = nodes[ 0 ] . node . send_payment ( route. clone ( ) , our_payment_hash) . err ( ) . unwrap ( ) ;
1147
1273
match err {
1148
- APIError :: ChannelUnavailable { err} => assert_eq ! ( err, "Cannot send value that would put us over our reserve value" ) ,
1274
+ APIError :: ChannelUnavailable { err} => assert_eq ! ( err, "Cannot send value that would put us over the reserve value" ) ,
1149
1275
_ => panic ! ( "Unknown error variants" ) ,
1150
1276
}
1151
1277
}
@@ -1170,7 +1296,7 @@ fn do_channel_reserve_test(test_recv: bool) {
1170
1296
{
1171
1297
let ( route, our_payment_hash, _) = get_route_and_payment_hash ! ( recv_value_2 + 1 ) ;
1172
1298
match nodes[ 0 ] . node . send_payment ( route, our_payment_hash) . err ( ) . unwrap ( ) {
1173
- APIError :: ChannelUnavailable { err} => assert_eq ! ( err, "Cannot send value that would put us over our reserve value" ) ,
1299
+ APIError :: ChannelUnavailable { err} => assert_eq ! ( err, "Cannot send value that would put us over the reserve value" ) ,
1174
1300
_ => panic ! ( "Unknown error variants" ) ,
1175
1301
}
1176
1302
}
@@ -1233,7 +1359,7 @@ fn do_channel_reserve_test(test_recv: bool) {
1233
1359
{
1234
1360
let ( route, our_payment_hash, _) = get_route_and_payment_hash ! ( recv_value_22+1 ) ;
1235
1361
match nodes[ 0 ] . node . send_payment ( route, our_payment_hash) . err ( ) . unwrap ( ) {
1236
- APIError :: ChannelUnavailable { err} => assert_eq ! ( err, "Cannot send value that would put us over our reserve value" ) ,
1362
+ APIError :: ChannelUnavailable { err} => assert_eq ! ( err, "Cannot send value that would put us over the reserve value" ) ,
1237
1363
_ => panic ! ( "Unknown error variants" ) ,
1238
1364
}
1239
1365
}
@@ -4726,7 +4852,7 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
4726
4852
let err = nodes[ 0 ] . node . send_payment ( route, our_payment_hash) ;
4727
4853
4728
4854
if let Err ( APIError :: ChannelUnavailable { err} ) = err {
4729
- assert_eq ! ( err, "Cannot send value that would put us over our max HTLC value in flight" ) ;
4855
+ assert_eq ! ( err, "Cannot send value that would put us over the max HTLC value in flight" ) ;
4730
4856
} else {
4731
4857
assert ! ( false ) ;
4732
4858
}
0 commit comments