@@ -1098,3 +1098,194 @@ fn raa_no_response_awaiting_raa_state() {
1098
1098
claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_2) ;
1099
1099
claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_3) ;
1100
1100
}
1101
+
1102
+ #[ test]
1103
+ fn test_monitor_update_fail_claim ( ) {
1104
+ // Basic test for monitor update failures when processing claim_funds calls.
1105
+ // We set up a simple 3-node network, sending a payment from A to B and failing B's monitor
1106
+ // update to claim the payment. We then send a payment C->B->A, making the forward of this
1107
+ // payment from B to A fail due to the paused channel. Finally, we restore the channel monitor
1108
+ // updating and claim the payment on B.
1109
+ let mut nodes = create_network ( 3 ) ;
1110
+ let chan_1 = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1111
+ create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
1112
+
1113
+ // Rebalance a bit so that we can send backwards from 3 to 2.
1114
+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 5000000 ) ;
1115
+
1116
+ let ( payment_preimage_1, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1000000 ) ;
1117
+
1118
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Err ( ChannelMonitorUpdateErr :: TemporaryFailure ) ;
1119
+ assert ! ( nodes[ 1 ] . node. claim_funds( payment_preimage_1) ) ;
1120
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1121
+
1122
+ let route = nodes[ 2 ] . router . get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 1000000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1123
+ let ( _, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1124
+ nodes[ 2 ] . node . send_payment ( route, payment_hash_2) . unwrap ( ) ;
1125
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1126
+
1127
+ // Successfully update the monitor on the 1<->2 channel, but the 0<->1 channel should still be
1128
+ // paused, so forward shouldn't succeed until we call test_restore_channel_monitor().
1129
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Ok ( ( ) ) ;
1130
+
1131
+ let mut events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
1132
+ assert_eq ! ( events. len( ) , 1 ) ;
1133
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1134
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) . unwrap ( ) ;
1135
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 2 ] , payment_event. commitment_msg, false , true ) ;
1136
+
1137
+ let bs_fail_update = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
1138
+ nodes[ 2 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_fail_update. update_fail_htlcs [ 0 ] ) . unwrap ( ) ;
1139
+ commitment_signed_dance ! ( nodes[ 2 ] , nodes[ 1 ] , bs_fail_update. commitment_signed, false , true ) ;
1140
+
1141
+ let msg_events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
1142
+ assert_eq ! ( msg_events. len( ) , 1 ) ;
1143
+ match msg_events[ 0 ] {
1144
+ MessageSendEvent :: PaymentFailureNetworkUpdate { update : msgs:: HTLCFailChannelUpdate :: ChannelUpdateMessage { ref msg } } => {
1145
+ assert_eq ! ( msg. contents. short_channel_id, chan_1. 0 . contents. short_channel_id) ;
1146
+ assert_eq ! ( msg. contents. flags & 2 , 2 ) ; // temp disabled
1147
+ } ,
1148
+ _ => panic ! ( "Unexpected event" ) ,
1149
+ }
1150
+
1151
+ let events = nodes[ 2 ] . node . get_and_clear_pending_events ( ) ;
1152
+ assert_eq ! ( events. len( ) , 1 ) ;
1153
+ if let Event :: PaymentFailed { payment_hash, rejected_by_dest, .. } = events[ 0 ] {
1154
+ assert_eq ! ( payment_hash, payment_hash_2) ;
1155
+ assert ! ( !rejected_by_dest) ;
1156
+ } else { panic ! ( "Unexpected event!" ) ; }
1157
+
1158
+ // Now restore monitor updating on the 0<->1 channel and claim the funds on B.
1159
+ nodes[ 1 ] . node . test_restore_channel_monitor ( ) ;
1160
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1161
+
1162
+ let bs_fulfill_update = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1163
+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_fulfill_update. update_fulfill_htlcs [ 0 ] ) . unwrap ( ) ;
1164
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , bs_fulfill_update. commitment_signed, false ) ;
1165
+
1166
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1167
+ assert_eq ! ( events. len( ) , 1 ) ;
1168
+ if let Event :: PaymentSent { payment_preimage, .. } = events[ 0 ] {
1169
+ assert_eq ! ( payment_preimage, payment_preimage_1) ;
1170
+ } else { panic ! ( "Unexpected event!" ) ; }
1171
+ }
1172
+
1173
+ #[ test]
1174
+ fn test_monitor_update_on_pending_forwards ( ) {
1175
+ // Basic test for monitor update failures when processing pending HTLC fail/add forwards.
1176
+ // We do this with a simple 3-node network, sending a payment from A to C and one from C to A.
1177
+ // The payment from A to C will be failed by C and pending a back-fail to A, while the payment
1178
+ // from C to A will be pending a forward to A.
1179
+ let mut nodes = create_network ( 3 ) ;
1180
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1181
+ create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
1182
+
1183
+ // Rebalance a bit so that we can send backwards from 3 to 1.
1184
+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 5000000 ) ;
1185
+
1186
+ let ( _, payment_hash_1) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 1000000 ) ;
1187
+ assert ! ( nodes[ 2 ] . node. fail_htlc_backwards( & payment_hash_1, 1000000 ) ) ;
1188
+ expect_pending_htlcs_forwardable ! ( nodes[ 2 ] ) ;
1189
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1190
+
1191
+ let cs_fail_update = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1192
+ nodes[ 1 ] . node . handle_update_fail_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_fail_update. update_fail_htlcs [ 0 ] ) . unwrap ( ) ;
1193
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 2 ] , cs_fail_update. commitment_signed, true , true ) ;
1194
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1195
+
1196
+ let route = nodes[ 2 ] . router . get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 1000000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1197
+ let ( payment_preimage_2, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1198
+ nodes[ 2 ] . node . send_payment ( route, payment_hash_2) . unwrap ( ) ;
1199
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1200
+
1201
+ let mut events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
1202
+ assert_eq ! ( events. len( ) , 1 ) ;
1203
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1204
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) . unwrap ( ) ;
1205
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 2 ] , payment_event. commitment_msg, false ) ;
1206
+
1207
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Err ( ChannelMonitorUpdateErr :: TemporaryFailure ) ;
1208
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1209
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1210
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1211
+
1212
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Ok ( ( ) ) ;
1213
+ nodes[ 1 ] . node . test_restore_channel_monitor ( ) ;
1214
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1215
+
1216
+ let bs_updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1217
+ nodes[ 0 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fail_htlcs [ 0 ] ) . unwrap ( ) ;
1218
+ nodes[ 0 ] . node . handle_update_add_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_add_htlcs [ 0 ] ) . unwrap ( ) ;
1219
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , bs_updates. commitment_signed, false , true ) ;
1220
+
1221
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1222
+ assert_eq ! ( events. len( ) , 2 ) ;
1223
+ if let Event :: PaymentFailed { payment_hash, rejected_by_dest, .. } = events[ 0 ] {
1224
+ assert_eq ! ( payment_hash, payment_hash_1) ;
1225
+ assert ! ( rejected_by_dest) ;
1226
+ } else { panic ! ( "Unexpected event!" ) ; }
1227
+ match events[ 1 ] {
1228
+ Event :: PendingHTLCsForwardable { .. } => { } ,
1229
+ _ => panic ! ( "Unexpected event" ) ,
1230
+ } ;
1231
+ nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) . next_forward = Instant :: now ( ) ;
1232
+ nodes[ 0 ] . node . process_pending_htlc_forwards ( ) ;
1233
+ expect_payment_received ! ( nodes[ 0 ] , payment_hash_2) ;
1234
+
1235
+ claim_payment ( & nodes[ 2 ] , & [ & nodes[ 1 ] , & nodes[ 0 ] ] , payment_preimage_2) ;
1236
+ }
1237
+
1238
+ #[ test]
1239
+ fn monitor_update_claim_fail_no_response ( ) {
1240
+ // Test for claim_funds resulting in both a monitor update failure and no message response (due
1241
+ // to channel being AwaitingRAA).
1242
+ // Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
1243
+ // code was broken.
1244
+ let mut nodes = create_network ( 2 ) ;
1245
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1246
+
1247
+ // Forward a payment for B to claim
1248
+ let ( payment_preimage_1, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1000000 ) ;
1249
+
1250
+ // Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA
1251
+ let route = nodes[ 0 ] . router . get_route ( & nodes[ 1 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 1000000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1252
+ let ( payment_preimage_2, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1253
+ nodes[ 0 ] . node . send_payment ( route, payment_hash_2) . unwrap ( ) ;
1254
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1255
+
1256
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1257
+ assert_eq ! ( events. len( ) , 1 ) ;
1258
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1259
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) . unwrap ( ) ;
1260
+ let as_raa = commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , payment_event. commitment_msg, false , true , false , true ) ;
1261
+
1262
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Err ( ChannelMonitorUpdateErr :: TemporaryFailure ) ;
1263
+ assert ! ( nodes[ 1 ] . node. claim_funds( payment_preimage_1) ) ;
1264
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1265
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1266
+
1267
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Ok ( ( ) ) ;
1268
+ nodes[ 1 ] . node . test_restore_channel_monitor ( ) ;
1269
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1270
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1271
+
1272
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_raa) . unwrap ( ) ;
1273
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1274
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1275
+ expect_payment_received ! ( nodes[ 1 ] , payment_hash_2) ;
1276
+
1277
+ let bs_updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1278
+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fulfill_htlcs [ 0 ] ) . unwrap ( ) ;
1279
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , bs_updates. commitment_signed, false ) ;
1280
+
1281
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1282
+ assert_eq ! ( events. len( ) , 1 ) ;
1283
+ match events[ 0 ] {
1284
+ Event :: PaymentSent { ref payment_preimage } => {
1285
+ assert_eq ! ( * payment_preimage, payment_preimage_1) ;
1286
+ } ,
1287
+ _ => panic ! ( "Unexpected event" ) ,
1288
+ }
1289
+
1290
+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_2) ;
1291
+ }
0 commit comments