@@ -1098,3 +1098,120 @@ 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 claim_while_disconnected_monitor_update_fail ( ) {
1104
+ // Test for claiming a payment while disconnected and then having the resulting
1105
+ // channel-update-generated monitor update fail. This kind of thing isn't a particularly
1106
+ // contrived case for nodes with network instability.
1107
+ // Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
1108
+ // code introduced a regression in this test (specifically, this caught a removal of the
1109
+ // channel_reestablish handling ensuring the order was sensical given the messages used).
1110
+ let mut nodes = create_network ( 2 ) ;
1111
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1112
+
1113
+ // Forward a payment for B to claim
1114
+ let ( payment_preimage_1, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1000000 ) ;
1115
+
1116
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
1117
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
1118
+
1119
+ assert ! ( nodes[ 1 ] . node. claim_funds( payment_preimage_1) ) ;
1120
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1121
+
1122
+ nodes[ 0 ] . node . peer_connected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
1123
+ nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
1124
+
1125
+ let as_reconnect = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendChannelReestablish , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1126
+ let bs_reconnect = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendChannelReestablish , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1127
+
1128
+ nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_reconnect) . unwrap ( ) ;
1129
+ assert ! ( nodes[ 0 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1130
+
1131
+ // Now deliver a's reestablish, freeing the claim from the holding cell, but fail the monitor
1132
+ // update.
1133
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Err ( ChannelMonitorUpdateErr :: TemporaryFailure ) ;
1134
+
1135
+ if let msgs:: HandleError { err, action : Some ( msgs:: ErrorAction :: IgnoreError ) } = nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_reconnect) . unwrap_err ( ) {
1136
+ assert_eq ! ( err, "Failed to update ChannelMonitor" ) ;
1137
+ } else { panic ! ( ) ; }
1138
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1139
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1140
+
1141
+ // Send a second payment from A to B, resulting in a commitment update that gets swallowed with
1142
+ // the monitor still failed
1143
+ let route = nodes[ 0 ] . router . get_route ( & nodes[ 1 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 1000000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1144
+ let ( payment_preimage_2, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1145
+ nodes[ 0 ] . node . send_payment ( route, payment_hash_2) . unwrap ( ) ;
1146
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1147
+
1148
+ let as_updates = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1149
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_updates. update_add_htlcs [ 0 ] ) . unwrap ( ) ;
1150
+ if let msgs:: HandleError { err, action : Some ( msgs:: ErrorAction :: IgnoreError ) } = nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_updates. commitment_signed ) . unwrap_err ( ) {
1151
+ assert_eq ! ( err, "Previous monitor update failure prevented generation of RAA" ) ;
1152
+ } else { panic ! ( ) ; }
1153
+ // Note that nodes[1] not updating monitor here is OK - it wont take action on the new HTLC
1154
+ // until we've test_restore_channel_monitor'd and updated for the new commitment transaction.
1155
+
1156
+ // Now un-fail the monitor, which will result in B sending its original commitment update,
1157
+ // receiving the commitment update from A, and the resulting commitment dances.
1158
+ * nodes[ 1 ] . chan_monitor . update_ret . lock ( ) . unwrap ( ) = Ok ( ( ) ) ;
1159
+ nodes[ 1 ] . node . test_restore_channel_monitor ( ) ;
1160
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1161
+
1162
+ let bs_msgs = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1163
+ assert_eq ! ( bs_msgs. len( ) , 2 ) ;
1164
+
1165
+ match bs_msgs[ 0 ] {
1166
+ MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
1167
+ assert_eq ! ( * node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
1168
+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & updates. update_fulfill_htlcs [ 0 ] ) . unwrap ( ) ;
1169
+ nodes[ 0 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & updates. commitment_signed ) . unwrap ( ) ;
1170
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1171
+
1172
+ let as_raa = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1173
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_raa) . unwrap ( ) ;
1174
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1175
+ } ,
1176
+ _ => panic ! ( "Unexpected event" ) ,
1177
+ }
1178
+
1179
+ match bs_msgs[ 1 ] {
1180
+ MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
1181
+ assert_eq ! ( * node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
1182
+ nodes[ 0 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , msg) . unwrap ( ) ;
1183
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1184
+ } ,
1185
+ _ => panic ! ( "Unexpected event" ) ,
1186
+ }
1187
+
1188
+ let as_commitment = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1189
+
1190
+ let bs_commitment = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1191
+ nodes[ 0 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_commitment. commitment_signed ) . unwrap ( ) ;
1192
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1193
+ let as_raa = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1194
+
1195
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_commitment. commitment_signed ) . unwrap ( ) ;
1196
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1197
+ let bs_raa = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1198
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_raa) . unwrap ( ) ;
1199
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1200
+
1201
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1202
+ expect_payment_received ! ( nodes[ 1 ] , payment_hash_2) ;
1203
+
1204
+ nodes[ 0 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_raa) . unwrap ( ) ;
1205
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1206
+
1207
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1208
+ assert_eq ! ( events. len( ) , 1 ) ;
1209
+ match events[ 0 ] {
1210
+ Event :: PaymentSent { ref payment_preimage } => {
1211
+ assert_eq ! ( * payment_preimage, payment_preimage_1) ;
1212
+ } ,
1213
+ _ => panic ! ( "Unexpected event" ) ,
1214
+ }
1215
+
1216
+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_2) ;
1217
+ }
0 commit comments