X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Freload_tests.rs;h=909f76e0624785ae9f271411a96919b4a3271554;hb=f24830719af671f2998a42c3ce248147e94fe0d2;hp=fa08fba99fabda640739f714dd9eb6818d2ab374;hpb=ce7463486ee1ae61e9af439c3d34d00244248ee9;p=rust-lightning diff --git a/lightning/src/ln/reload_tests.rs b/lightning/src/ln/reload_tests.rs index fa08fba9..909f76e0 100644 --- a/lightning/src/ln/reload_tests.rs +++ b/lightning/src/ln/reload_tests.rs @@ -11,7 +11,7 @@ use crate::chain::{ChannelMonitorUpdateStatus, Watch}; use crate::chain::chaininterface::LowerBoundedFeeEstimator; -use crate::chain::channelmonitor::ChannelMonitor; +use crate::chain::channelmonitor::{CLOSED_CHANNEL_UPDATE_ID, ChannelMonitor}; use crate::sign::EntropySource; use crate::chain::transaction::OutPoint; use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider}; @@ -247,7 +247,7 @@ fn test_manager_serialize_deserialize_events() { let push_msat = 10001; let node_a = nodes.remove(0); let node_b = nodes.remove(0); - node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42, None).unwrap(); + node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42, None, None).unwrap(); node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id())); node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id())); @@ -493,6 +493,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() { assert!(found_err); } +#[cfg(feature = "std")] fn do_test_data_loss_protect(reconnect_panicing: bool) { // When we get a data_loss_protect proving we're behind, we immediately panic as the // chain::Watch API requirements have been violated (e.g. the user restored from a backup). The @@ -539,8 +540,20 @@ fn do_test_data_loss_protect(reconnect_panicing: bool) { // Check that we sent the warning message when we detected that A has fallen behind, // and give the possibility for A to recover from the warning. nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]); - let warn_msg = "Peer attempted to reestablish channel with a very old local commitment transaction".to_owned(); - assert!(check_warn_msg!(nodes[1], nodes[0].node.get_our_node_id(), chan.2).contains(&warn_msg)); + let warn_msg = "Peer attempted to reestablish channel with a very old local commitment transaction: 0 (received) vs 4 (expected)".to_owned(); + + let warn_reestablish = nodes[1].node.get_and_clear_pending_msg_events(); + assert_eq!(warn_reestablish.len(), 2); + match warn_reestablish[1] { + MessageSendEvent::HandleError { action: ErrorAction::SendWarningMessage { ref msg, .. }, .. } => { + assert_eq!(msg.data, warn_msg); + }, + _ => panic!("Unexpected event"), + } + let reestablish_msg = match &warn_reestablish[0] { + MessageSendEvent::SendChannelReestablish { msg, .. } => msg.clone(), + _ => panic!("Unexpected event"), + }; { let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); @@ -548,9 +561,11 @@ fn do_test_data_loss_protect(reconnect_panicing: bool) { assert!(node_txn.is_empty()); } - let reestablish_0 = get_chan_reestablish_msgs!(nodes[1], nodes[0]); // Check A panics upon seeing proof it has fallen behind. - nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_0[0]); + assert!(std::panic::catch_unwind(|| { + nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_msg); + }).is_err()); + std::mem::forget(nodes); // Skip the `Drop` handler for `Node` return; // By this point we should have panic'ed! } @@ -566,8 +581,8 @@ fn do_test_data_loss_protect(reconnect_panicing: bool) { if let MessageSendEvent::BroadcastChannelUpdate { .. } = msg { } else if let MessageSendEvent::HandleError { ref action, .. } = msg { match action { - &ErrorAction::SendErrorMessage { ref msg } => { - assert_eq!(msg.data, "Channel force-closed"); + &ErrorAction::DisconnectPeer { ref msg } => { + assert_eq!(msg.as_ref().unwrap().data, "Channel force-closed"); }, _ => panic!("Unexpected event!"), } @@ -589,18 +604,16 @@ fn do_test_data_loss_protect(reconnect_panicing: bool) { nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &retry_reestablish[0]); let mut err_msgs_0 = Vec::with_capacity(1); - for msg in nodes[0].node.get_and_clear_pending_msg_events() { - if let MessageSendEvent::HandleError { ref action, .. } = msg { - match action { - &ErrorAction::SendErrorMessage { ref msg } => { - assert_eq!(msg.data, format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", &nodes[1].node.get_our_node_id())); - err_msgs_0.push(msg.clone()); - }, - _ => panic!("Unexpected event!"), - } - } else { - panic!("Unexpected event!"); + if let MessageSendEvent::HandleError { ref action, .. } = nodes[0].node.get_and_clear_pending_msg_events()[1] { + match action { + &ErrorAction::SendErrorMessage { ref msg } => { + assert_eq!(msg.data, format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", &nodes[1].node.get_our_node_id())); + err_msgs_0.push(msg.clone()); + }, + _ => panic!("Unexpected event!"), } + } else { + panic!("Unexpected event!"); } assert_eq!(err_msgs_0.len(), 1); nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), &err_msgs_0[0]); @@ -612,13 +625,9 @@ fn do_test_data_loss_protect(reconnect_panicing: bool) { } #[test] -#[should_panic] -fn test_data_loss_protect_showing_stale_state_panics() { +#[cfg(feature = "std")] +fn test_data_loss_protect() { do_test_data_loss_protect(true); -} - -#[test] -fn test_force_close_without_broadcast() { do_test_data_loss_protect(false); } @@ -749,8 +758,8 @@ fn do_test_partial_claim_before_restart(persist_both_monitors: bool) { assert_eq!(send_events.len(), 2); let node_1_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut send_events); let node_2_msgs = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut send_events); - do_pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_1_msgs, true, false, None); - do_pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_2_msgs, true, false, None); + do_pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_1_msgs, true, false, None, false); + do_pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_2_msgs, true, false, None, false); // Now that we have an MPP payment pending, get the latest encoded copies of nodes[3]'s // monitors and ChannelManager, for use later, if we don't want to persist both monitors. @@ -1114,3 +1123,65 @@ fn removed_payment_no_manager_persistence() { expect_payment_failed!(nodes[0], payment_hash, false); } + +#[test] +fn test_reload_partial_funding_batch() { + let chanmon_cfgs = create_chanmon_cfgs(3); + let node_cfgs = create_node_cfgs(3, &chanmon_cfgs); + let new_persister; + let new_chain_monitor; + + let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]); + let new_channel_manager; + let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); + + // Initiate channel opening and create the batch channel funding transaction. + let (tx, funding_created_msgs) = create_batch_channel_funding(&nodes[0], &[ + (&nodes[1], 100_000, 0, 42, None), + (&nodes[2], 200_000, 0, 43, None), + ]); + + // Go through the funding_created and funding_signed flow with node 1. + nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msgs[0]); + check_added_monitors(&nodes[1], 1); + expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id()); + + // The monitor is persisted when receiving funding_signed. + let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()); + nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg); + check_added_monitors(&nodes[0], 1); + + // The transaction should not have been broadcast before all channels are ready. + assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0); + + // Reload the node while a subset of the channels in the funding batch have persisted monitors. + let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id(); + let node_encoded = nodes[0].node.encode(); + let channel_monitor_1_serialized = get_monitor!(nodes[0], channel_id_1).encode(); + reload_node!(nodes[0], node_encoded, &[&channel_monitor_1_serialized], new_persister, new_chain_monitor, new_channel_manager); + + // Process monitor events. + assert!(nodes[0].node.get_and_clear_pending_events().is_empty()); + + // The monitor should become closed. + check_added_monitors(&nodes[0], 1); + { + let mut monitor_updates = nodes[0].chain_monitor.monitor_updates.lock().unwrap(); + let monitor_updates_1 = monitor_updates.get(&channel_id_1).unwrap(); + assert_eq!(monitor_updates_1.len(), 1); + assert_eq!(monitor_updates_1[0].update_id, CLOSED_CHANNEL_UPDATE_ID); + } + + // The funding transaction should not have been broadcast, but we broadcast the force-close + // transaction as part of closing the monitor. + { + let broadcasted_txs = nodes[0].tx_broadcaster.txn_broadcast(); + assert_eq!(broadcasted_txs.len(), 1); + assert!(broadcasted_txs[0].txid() != tx.txid()); + assert_eq!(broadcasted_txs[0].input.len(), 1); + assert_eq!(broadcasted_txs[0].input[0].previous_output.txid, tx.txid()); + } + + // Ensure the channels don't exist anymore. + assert!(nodes[0].node.list_channels().is_empty()); +}