|
10 | 10 | //! Tests of our shutdown and closing_signed negotiation logic.
|
11 | 11 |
|
12 | 12 | use crate::sign::{EntropySource, SignerProvider};
|
| 13 | +use crate::chain::ChannelMonitorUpdateStatus; |
13 | 14 | use crate::chain::transaction::OutPoint;
|
14 |
| -use crate::events::{MessageSendEvent, MessageSendEventsProvider, ClosureReason}; |
| 15 | +use crate::events::{MessageSendEvent, HTLCDestination, MessageSendEventsProvider, ClosureReason}; |
15 | 16 | use crate::ln::channelmanager::{self, PaymentSendFailure, PaymentId, RecipientOnionFields, ChannelShutdownState, ChannelDetails};
|
16 | 17 | use crate::routing::router::{PaymentParameters, get_route, RouteParameters};
|
17 | 18 | use crate::ln::msgs;
|
@@ -1237,3 +1238,102 @@ fn simple_target_feerate_shutdown() {
|
1237 | 1238 | check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
|
1238 | 1239 | check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
|
1239 | 1240 | }
|
| 1241 | + |
| 1242 | +fn do_outbound_update_no_early_closing_signed(use_htlc: bool) { |
| 1243 | + // Previously, if we have a pending inbound HTLC (or fee update) on a channel which has |
| 1244 | + // initiated shutdown, we'd send our initial closing_signed immediately after receiving the |
| 1245 | + // peer's last RAA to remove the HTLC/fee update, but before receiving their final |
| 1246 | + // commitment_signed for a commitment without the HTLC/with the new fee. This caused at least |
| 1247 | + // LDK peers to force-close as we initiated closing_signed prior to the channel actually being |
| 1248 | + // fully empty of pending updates/HTLCs. |
| 1249 | + let chanmon_cfgs = create_chanmon_cfgs(2); |
| 1250 | + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); |
| 1251 | + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); |
| 1252 | + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); |
| 1253 | + |
| 1254 | + let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2; |
| 1255 | + |
| 1256 | + send_payment(&nodes[0], &[&nodes[1]], 1_000_000); |
| 1257 | + let payment_hash_opt = if use_htlc { |
| 1258 | + Some(route_payment(&nodes[1], &[&nodes[0]], 10_000).1) |
| 1259 | + } else { |
| 1260 | + None |
| 1261 | + }; |
| 1262 | + |
| 1263 | + if use_htlc { |
| 1264 | + nodes[0].node.fail_htlc_backwards(&payment_hash_opt.unwrap()); |
| 1265 | + expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[0], |
| 1266 | + [HTLCDestination::FailedPayment { payment_hash: payment_hash_opt.unwrap() }]); |
| 1267 | + } else { |
| 1268 | + *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap() *= 10; |
| 1269 | + nodes[0].node.timer_tick_occurred(); |
| 1270 | + } |
| 1271 | + let updates = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id()); |
| 1272 | + check_added_monitors(&nodes[0], 1); |
| 1273 | + |
| 1274 | + nodes[1].node.close_channel(&chan_id, &nodes[0].node.get_our_node_id()).unwrap(); |
| 1275 | + let node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id()); |
| 1276 | + nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap(); |
| 1277 | + let node_1_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id()); |
| 1278 | + |
| 1279 | + nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown); |
| 1280 | + nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_1_shutdown); |
| 1281 | + |
| 1282 | + if use_htlc { |
| 1283 | + nodes[1].node.handle_update_fail_htlc(&nodes[0].node.get_our_node_id(), &updates.update_fail_htlcs[0]); |
| 1284 | + } else { |
| 1285 | + nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &updates.update_fee.unwrap()); |
| 1286 | + } |
| 1287 | + nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed); |
| 1288 | + check_added_monitors(&nodes[1], 1); |
| 1289 | + let (bs_raa, bs_cs) = get_revoke_commit_msgs(&nodes[1], &nodes[0].node.get_our_node_id()); |
| 1290 | + |
| 1291 | + nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa); |
| 1292 | + check_added_monitors(&nodes[0], 1); |
| 1293 | + |
| 1294 | + // At this point the Channel on nodes[0] has no record of any HTLCs but the latest |
| 1295 | + // broadcastable commitment does contain the HTLC (but only the ChannelMonitor knows this). |
| 1296 | + // Thus, the channel should not yet initiate closing_signed negotiation (but previously did). |
| 1297 | + assert_eq!(nodes[0].node.get_and_clear_pending_msg_events(), Vec::new()); |
| 1298 | + |
| 1299 | + chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress); |
| 1300 | + nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs); |
| 1301 | + check_added_monitors(&nodes[0], 1); |
| 1302 | + assert_eq!(nodes[0].node.get_and_clear_pending_msg_events(), Vec::new()); |
| 1303 | + |
| 1304 | + expect_channel_shutdown_state!(nodes[0], chan_id, ChannelShutdownState::ResolvingHTLCs); |
| 1305 | + assert_eq!(nodes[0].node.get_and_clear_pending_msg_events(), Vec::new()); |
| 1306 | + let (outpoint, latest_update, _) = nodes[0].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id).unwrap().clone(); |
| 1307 | + nodes[0].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update); |
| 1308 | + |
| 1309 | + let as_raa_closing_signed = nodes[0].node.get_and_clear_pending_msg_events(); |
| 1310 | + assert_eq!(as_raa_closing_signed.len(), 2); |
| 1311 | + |
| 1312 | + if let MessageSendEvent::SendRevokeAndACK { msg, .. } = &as_raa_closing_signed[0] { |
| 1313 | + nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &msg); |
| 1314 | + check_added_monitors(&nodes[1], 1); |
| 1315 | + if use_htlc { |
| 1316 | + expect_payment_failed!(nodes[1], payment_hash_opt.unwrap(), true); |
| 1317 | + } |
| 1318 | + } else { panic!("Unexpected message {:?}", as_raa_closing_signed[0]); } |
| 1319 | + |
| 1320 | + if let MessageSendEvent::SendClosingSigned { msg, .. } = &as_raa_closing_signed[1] { |
| 1321 | + nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &msg); |
| 1322 | + } else { panic!("Unexpected message {:?}", as_raa_closing_signed[1]); } |
| 1323 | + |
| 1324 | + let bs_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id()); |
| 1325 | + nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &bs_closing_signed); |
| 1326 | + let (_, as_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id()); |
| 1327 | + nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &as_2nd_closing_signed.unwrap()); |
| 1328 | + let (_, node_1_none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id()); |
| 1329 | + assert!(node_1_none.is_none()); |
| 1330 | + |
| 1331 | + check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000); |
| 1332 | + check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000); |
| 1333 | +} |
| 1334 | + |
| 1335 | +#[test] |
| 1336 | +fn outbound_update_no_early_closing_signed() { |
| 1337 | + do_outbound_update_no_early_closing_signed(true); |
| 1338 | + do_outbound_update_no_early_closing_signed(false); |
| 1339 | +} |
0 commit comments