Merge pull request #1846 from TheBlueMatt/2022-11-more-robust-unconfirmed
[rust-lightning] / lightning / src / ln / chanmon_update_fail_tests.rs
index 15d46b04688930d03525b6347d6b714a28713b28..9b0e303676254fc89daaabe3a90322901fa9a5f2 100644 (file)
@@ -16,29 +16,28 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
 use bitcoin::blockdata::constants::genesis_block;
 use bitcoin::hash_types::BlockHash;
 use bitcoin::network::constants::Network;
-use chain::channelmonitor::{ANTI_REORG_DELAY, ChannelMonitor};
-use chain::transaction::OutPoint;
-use chain::{ChannelMonitorUpdateStatus, Listen, Watch};
-use ln::channelmanager::{self, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure};
-use ln::channel::AnnouncementSigsState;
-use ln::msgs;
-use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler};
-use util::config::UserConfig;
-use util::enforcing_trait_impls::EnforcingSigner;
-use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose, ClosureReason, HTLCDestination};
-use util::errors::APIError;
-use util::ser::{ReadableArgs, Writeable};
-use util::test_utils::TestBroadcaster;
-
-use ln::functional_test_utils::*;
-
-use util::test_utils;
-
-use io;
+use crate::chain::channelmonitor::{ANTI_REORG_DELAY, ChannelMonitor};
+use crate::chain::transaction::OutPoint;
+use crate::chain::{ChannelMonitorUpdateStatus, Listen, Watch};
+use crate::ln::channelmanager::{self, ChannelManager, RAACommitmentOrder, PaymentSendFailure, PaymentId};
+use crate::ln::channel::AnnouncementSigsState;
+use crate::ln::msgs;
+use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler};
+use crate::util::enforcing_trait_impls::EnforcingSigner;
+use crate::util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose, ClosureReason, HTLCDestination};
+use crate::util::errors::APIError;
+use crate::util::ser::{ReadableArgs, Writeable};
+use crate::util::test_utils::TestBroadcaster;
+
+use crate::ln::functional_test_utils::*;
+
+use crate::util::test_utils;
+
+use crate::io;
 use bitcoin::hashes::Hash;
 use bitcoin::TxMerkleNode;
-use prelude::*;
-use sync::{Arc, Mutex};
+use crate::prelude::*;
+use crate::sync::{Arc, Mutex};
 
 #[test]
 fn test_simple_monitor_permanent_update_fail() {
@@ -51,7 +50,7 @@ fn test_simple_monitor_permanent_update_fail() {
 
        let (route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(&nodes[0], nodes[1], 1000000);
        chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::PermanentFailure);
-       unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)), true, APIError::ChannelUnavailable {..}, {});
+       unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), PaymentId(payment_hash_1.0)), true, APIError::ChannelUnavailable {..}, {});
        check_added_monitors!(nodes[0], 2);
 
        let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
@@ -170,7 +169,7 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
        chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
 
        {
-               unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)), false, APIError::MonitorUpdateInProgress, {});
+               unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), PaymentId(payment_hash_1.0)), false, APIError::MonitorUpdateInProgress, {});
                check_added_monitors!(nodes[0], 1);
        }
 
@@ -221,7 +220,7 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
        let (route, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(&nodes[0], nodes[1], 1000000);
        {
                chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
-               unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)), false, APIError::MonitorUpdateInProgress, {});
+               unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)), false, APIError::MonitorUpdateInProgress, {});
                check_added_monitors!(nodes[0], 1);
        }
 
@@ -285,7 +284,7 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
        let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        {
                chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
-               unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)), false, APIError::MonitorUpdateInProgress, {});
+               unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)), false, APIError::MonitorUpdateInProgress, {});
                check_added_monitors!(nodes[0], 1);
        }
 
@@ -624,7 +623,7 @@ fn test_monitor_update_fail_cs() {
 
        let (route, our_payment_hash, payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        {
-               nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
+               nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
        }
 
@@ -716,7 +715,7 @@ fn test_monitor_update_fail_no_rebroadcast() {
 
        let (route, our_payment_hash, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        {
-               nodes[0].node.send_payment(&route, our_payment_hash, &Some(payment_secret_1)).unwrap();
+               nodes[0].node.send_payment(&route, our_payment_hash, &Some(payment_secret_1), PaymentId(our_payment_hash.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
        }
 
@@ -764,14 +763,14 @@ fn test_monitor_update_raa_while_paused() {
        send_payment(&nodes[0], &[&nodes[1]], 5000000);
        let (route, our_payment_hash_1, payment_preimage_1, our_payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        {
-               nodes[0].node.send_payment(&route, our_payment_hash_1, &Some(our_payment_secret_1)).unwrap();
+               nodes[0].node.send_payment(&route, our_payment_hash_1, &Some(our_payment_secret_1), PaymentId(our_payment_hash_1.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
        }
        let send_event_1 = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
 
        let (route, our_payment_hash_2, payment_preimage_2, our_payment_secret_2) = get_route_and_payment_hash!(nodes[1], nodes[0], 1000000);
        {
-               nodes[1].node.send_payment(&route, our_payment_hash_2, &Some(our_payment_secret_2)).unwrap();
+               nodes[1].node.send_payment(&route, our_payment_hash_2, &Some(our_payment_secret_2), PaymentId(our_payment_hash_2.0)).unwrap();
                check_added_monitors!(nodes[1], 1);
        }
        let send_event_2 = SendEvent::from_event(nodes[1].node.get_and_clear_pending_msg_events().remove(0));
@@ -860,7 +859,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
        // holding cell.
        let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], 1000000);
        {
-               nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
+               nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
        }
 
@@ -885,7 +884,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
        // being paused waiting a monitor update.
        let (route, payment_hash_3, _, payment_secret_3) = get_route_and_payment_hash!(nodes[0], nodes[2], 1000000);
        {
-               nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3)).unwrap();
+               nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3), PaymentId(payment_hash_3.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
        }
 
@@ -904,7 +903,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
        let (payment_preimage_4, payment_hash_4) = if test_ignore_second_cs {
                // Try to route another payment backwards from 2 to make sure 1 holds off on responding
                let (route, payment_hash_4, payment_preimage_4, payment_secret_4) = get_route_and_payment_hash!(nodes[2], nodes[0], 1000000);
-               nodes[2].node.send_payment(&route, payment_hash_4, &Some(payment_secret_4)).unwrap();
+               nodes[2].node.send_payment(&route, payment_hash_4, &Some(payment_secret_4), PaymentId(payment_hash_4.0)).unwrap();
                check_added_monitors!(nodes[2], 1);
 
                send_event = SendEvent::from_event(nodes[2].node.get_and_clear_pending_msg_events().remove(0));
@@ -1198,9 +1197,9 @@ fn raa_no_response_awaiting_raa_state() {
        // requires only an RAA response due to AwaitingRAA) we can deliver the RAA and require the CS
        // generation during RAA while in monitor-update-failed state.
        {
-               nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
+               nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
-               nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
+               nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
                check_added_monitors!(nodes[0], 0);
        }
 
@@ -1250,7 +1249,7 @@ fn raa_no_response_awaiting_raa_state() {
        // chanmon_fail_consistency test required it to actually find the bug (by seeing out-of-sync
        // commitment transaction states) whereas here we can explicitly check for it.
        {
-               nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3)).unwrap();
+               nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3), PaymentId(payment_hash_3.0)).unwrap();
                check_added_monitors!(nodes[0], 0);
                assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
        }
@@ -1340,7 +1339,7 @@ fn claim_while_disconnected_monitor_update_fail() {
        // the monitor still failed
        let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        {
-               nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
+               nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
        }
 
@@ -1430,7 +1429,7 @@ fn monitor_failed_no_reestablish_response() {
        // on receipt).
        let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        {
-               nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
+               nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
        }
 
@@ -1503,7 +1502,7 @@ fn first_message_on_recv_ordering() {
        // can deliver it and fail the monitor update.
        let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        {
-               nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
+               nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
        }
 
@@ -1526,7 +1525,7 @@ fn first_message_on_recv_ordering() {
        // Route the second payment, generating an update_add_htlc/commitment_signed
        let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        {
-               nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
+               nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
        }
        let mut events = nodes[0].node.get_and_clear_pending_msg_events();
@@ -1610,7 +1609,7 @@ fn test_monitor_update_fail_claim() {
 
        let (route, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[2], nodes[0], 1_000_000);
        {
-               nodes[2].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
+               nodes[2].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
                check_added_monitors!(nodes[2], 1);
        }
 
@@ -1627,7 +1626,7 @@ fn test_monitor_update_fail_claim() {
        commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false, true);
 
        let (_, payment_hash_3, payment_secret_3) = get_payment_preimage_hash!(nodes[0]);
-       nodes[2].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3)).unwrap();
+       nodes[2].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3), PaymentId(payment_hash_3.0)).unwrap();
        check_added_monitors!(nodes[2], 1);
 
        let mut events = nodes[2].node.get_and_clear_pending_msg_events();
@@ -1717,7 +1716,7 @@ fn test_monitor_update_on_pending_forwards() {
 
        let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[2], nodes[0], 1000000);
        {
-               nodes[2].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
+               nodes[2].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
                check_added_monitors!(nodes[2], 1);
        }
 
@@ -1777,7 +1776,7 @@ fn monitor_update_claim_fail_no_response() {
        // Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA
        let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
        {
-               nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
+               nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
        }
 
@@ -1916,6 +1915,13 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf:
                node.gossip_sync.handle_channel_update(&bs_update).unwrap();
        }
 
+       if !restore_b_before_lock {
+               expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
+       } else {
+               expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
+       }
+
+
        send_payment(&nodes[0], &[&nodes[1]], 8000000);
        close_channel(&nodes[0], &nodes[1], &channel_id, funding_tx, true);
        check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure);
@@ -1964,7 +1970,7 @@ fn test_path_paused_mpp() {
        // Now check that we get the right return value, indicating that the first path succeeded but
        // the second got a MonitorUpdateInProgress err. This implies
        // PaymentSendFailure::PartialFailure as some paths succeeded, preventing retry.
-       if let Err(PaymentSendFailure::PartialFailure { results, ..}) = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)) {
+       if let Err(PaymentSendFailure::PartialFailure { results, ..}) = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)) {
                assert_eq!(results.len(), 2);
                if let Ok(()) = results[0] {} else { panic!(); }
                if let Err(APIError::MonitorUpdateInProgress) = results[1] {} else { panic!(); }
@@ -2009,7 +2015,7 @@ fn test_pending_update_fee_ack_on_reconnect() {
        send_payment(&nodes[0], &[&nodes[1]], 100_000_00);
 
        let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[1], nodes[0], 1_000_000);
-       nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
+       nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
        check_added_monitors!(nodes[1], 1);
        let bs_initial_send_msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
        // bs_initial_send_msgs are not delivered until they are re-generated after reconnect
@@ -2231,18 +2237,19 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let persister: test_utils::TestPersister;
        let new_chain_monitor: test_utils::TestChainMonitor;
-       let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
+       let nodes_0_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
        let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
 
        let chan_id = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 15_000_000, 7_000_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features()).2;
        let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100000);
        let (payment_preimage_2, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(&nodes[1]);
 
-       // Do a really complicated dance to get an HTLC into the holding cell, with MonitorUpdateFailed
-       // set but AwaitingRemoteRevoke unset. When this test was written, any attempts to send an HTLC
-       // while MonitorUpdateFailed is set are immediately failed-backwards. Thus, the only way to get
-       // an AddHTLC into the holding cell is to add it while AwaitingRemoteRevoke is set but
-       // MonitorUpdateFailed is unset, and then swap the flags.
+       // Do a really complicated dance to get an HTLC into the holding cell, with
+       // MonitorUpdateInProgress set but AwaitingRemoteRevoke unset. When this test was written, any
+       // attempts to send an HTLC while MonitorUpdateInProgress is set are immediately
+       // failed-backwards. Thus, the only way to get an AddHTLC into the holding cell is to add it
+       // while AwaitingRemoteRevoke is set but MonitorUpdateInProgress is unset, and then swap the
+       // flags.
        //
        // We do this by:
        //  a) routing a payment from node B to node A,
@@ -2255,16 +2262,16 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
        //  e) delivering A's commitment_signed from (b) and the resulting B revoke_and_ack message,
        //     clearing AwaitingRemoteRevoke on node A.
        //
-       // Note that because, at the end, MonitorUpdateFailed is still set, the HTLC generated in (c)
-       // will not be freed from the holding cell.
+       // Note that because, at the end, MonitorUpdateInProgress is still set, the HTLC generated in
+       // (c) will not be freed from the holding cell.
        let (payment_preimage_0, payment_hash_0, _) = route_payment(&nodes[1], &[&nodes[0]], 100_000);
 
-       nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
+       nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let send = SendEvent::from_node(&nodes[0]);
        assert_eq!(send.msgs.len(), 1);
 
-       nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
+       nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
        check_added_monitors!(nodes[0], 0);
 
        chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
@@ -2286,40 +2293,8 @@ fn do_channel_holding_cell_serialize(disconnect: bool, reload_a: bool) {
                // disconnect the peers. Note that the fuzzer originally found this issue because
                // deserializing a ChannelManager in this state causes an assertion failure.
                if reload_a {
-                       let nodes_0_serialized = nodes[0].node.encode();
-                       let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
-                       get_monitor!(nodes[0], chan_id).write(&mut chan_0_monitor_serialized).unwrap();
-
-                       persister = test_utils::TestPersister::new();
-                       let keys_manager = &chanmon_cfgs[0].keys_manager;
-                       new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), nodes[0].logger, node_cfgs[0].fee_estimator, &persister, keys_manager);
-                       nodes[0].chain_monitor = &new_chain_monitor;
-                       let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
-                       let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
-                               &mut chan_0_monitor_read, keys_manager).unwrap();
-                       assert!(chan_0_monitor_read.is_empty());
-
-                       let mut nodes_0_read = &nodes_0_serialized[..];
-                       let config = UserConfig::default();
-                       nodes_0_deserialized = {
-                               let mut channel_monitors = HashMap::new();
-                               channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
-                               <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
-                                       default_config: config,
-                                       keys_manager,
-                                       fee_estimator: node_cfgs[0].fee_estimator,
-                                       chain_monitor: nodes[0].chain_monitor,
-                                       tx_broadcaster: nodes[0].tx_broadcaster.clone(),
-                                       logger: nodes[0].logger,
-                                       channel_monitors,
-                               }).unwrap().1
-                       };
-                       nodes[0].node = &nodes_0_deserialized;
-                       assert!(nodes_0_read.is_empty());
-
-                       assert_eq!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0.clone(), chan_0_monitor),
-                               ChannelMonitorUpdateStatus::Completed);
-                       check_added_monitors!(nodes[0], 1);
+                       let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
+                       reload_node!(nodes[0], &nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized);
                } else {
                        nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
                }
@@ -2456,7 +2431,7 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
                // In order to get the HTLC claim into the holding cell at nodes[1], we need nodes[1] to be
                // awaiting a remote revoke_and_ack from nodes[0].
                let (route, second_payment_hash, _, second_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
-               nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret)).unwrap();
+               nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret), PaymentId(second_payment_hash.0)).unwrap();
                check_added_monitors!(nodes[0], 1);
 
                let send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
@@ -2745,3 +2720,180 @@ fn double_temp_error() {
        commitment_signed_dance!(nodes[0], nodes[1], commitment_signed_b2, false);
        expect_payment_sent!(nodes[0], payment_preimage_2);
 }
+
+fn do_test_outbound_reload_without_init_mon(use_0conf: bool) {
+       // Test that if the monitor update generated in funding_signed is stored async and we restart
+       // with the latest ChannelManager but the ChannelMonitor persistence never completed we happily
+       // drop the channel and move on.
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+
+       let persister: test_utils::TestPersister;
+       let new_chain_monitor: test_utils::TestChainMonitor;
+       let nodes_0_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
+
+       let mut chan_config = test_default_channel_config();
+       chan_config.manually_accept_inbound_channels = true;
+       chan_config.channel_handshake_limits.trust_own_funding_0conf = true;
+
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(chan_config), Some(chan_config)]);
+       let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 43, None).unwrap();
+       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()));
+
+       let events = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::OpenChannelRequest { temporary_channel_id, .. } => {
+                       if use_0conf {
+                               nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap();
+                       } else {
+                               nodes[1].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap();
+                       }
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
+
+       let (temporary_channel_id, funding_tx, ..) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 43);
+
+       nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), funding_tx.clone()).unwrap();
+       check_added_monitors!(nodes[0], 0);
+
+       let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
+       nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
+       check_added_monitors!(nodes[1], 1);
+
+       let bs_signed_locked = nodes[1].node.get_and_clear_pending_msg_events();
+       assert_eq!(bs_signed_locked.len(), if use_0conf { 2 } else { 1 });
+       match &bs_signed_locked[0] {
+               MessageSendEvent::SendFundingSigned { msg, .. } => {
+                       chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
+
+                       nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &msg);
+                       check_added_monitors!(nodes[0], 1);
+               }
+               _ => panic!("Unexpected event"),
+       }
+       if use_0conf {
+               match &bs_signed_locked[1] {
+                       MessageSendEvent::SendChannelReady { msg, .. } => {
+                               nodes[0].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &msg);
+                       }
+                       _ => panic!("Unexpected event"),
+               }
+       }
+
+       assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
+       assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
+       assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
+
+       // nodes[0] is now waiting on the first ChannelMonitor persistence to complete in order to
+       // broadcast the funding transaction. If nodes[0] restarts at this point with the
+       // ChannelMonitor lost, we should simply discard the channel.
+
+       // The test framework checks that watched_txn/outputs match the monitor set, which they will
+       // not, so we have to clear them here.
+       nodes[0].chain_source.watched_txn.lock().unwrap().clear();
+       nodes[0].chain_source.watched_outputs.lock().unwrap().clear();
+
+       reload_node!(nodes[0], &nodes[0].node.encode(), &[], persister, new_chain_monitor, nodes_0_deserialized);
+       check_closed_event!(nodes[0], 1, ClosureReason::DisconnectedPeer);
+       assert!(nodes[0].node.list_channels().is_empty());
+}
+
+#[test]
+fn test_outbound_reload_without_init_mon() {
+       do_test_outbound_reload_without_init_mon(true);
+       do_test_outbound_reload_without_init_mon(false);
+}
+
+fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: bool) {
+       // Test that if the monitor update generated by funding_transaction_generated is stored async
+       // and we restart with the latest ChannelManager but the ChannelMonitor persistence never
+       // completed we happily drop the channel and move on.
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+
+       let persister: test_utils::TestPersister;
+       let new_chain_monitor: test_utils::TestChainMonitor;
+       let nodes_1_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
+
+       let mut chan_config = test_default_channel_config();
+       chan_config.manually_accept_inbound_channels = true;
+       chan_config.channel_handshake_limits.trust_own_funding_0conf = true;
+
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(chan_config), Some(chan_config)]);
+       let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 43, None).unwrap();
+       nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()));
+
+       let events = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::OpenChannelRequest { temporary_channel_id, .. } => {
+                       if use_0conf {
+                               nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap();
+                       } else {
+                               nodes[1].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap();
+                       }
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
+
+       let (temporary_channel_id, funding_tx, ..) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 43);
+
+       nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), funding_tx.clone()).unwrap();
+       check_added_monitors!(nodes[0], 0);
+
+       let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
+       chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
+       nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
+       check_added_monitors!(nodes[1], 1);
+
+       // nodes[1] happily sends its funding_signed even though its awaiting the persistence of the
+       // initial ChannelMonitor, but it will decline to send its channel_ready even if the funding
+       // transaction is confirmed.
+       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);
+
+       let as_funding_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
+       if lock_commitment {
+               confirm_transaction(&nodes[0], &as_funding_tx[0]);
+               confirm_transaction(&nodes[1], &as_funding_tx[0]);
+       }
+       if use_0conf || lock_commitment {
+               let as_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
+               nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_ready);
+       }
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+       // nodes[1] is now waiting on the first ChannelMonitor persistence to complete in order to
+       // move the channel to ready (or is waiting on the funding transaction to confirm). If nodes[1]
+       // restarts at this point with the ChannelMonitor lost, we should simply discard the channel.
+
+       // The test framework checks that watched_txn/outputs match the monitor set, which they will
+       // not, so we have to clear them here.
+       nodes[1].chain_source.watched_txn.lock().unwrap().clear();
+       nodes[1].chain_source.watched_outputs.lock().unwrap().clear();
+
+       reload_node!(nodes[1], &nodes[1].node.encode(), &[], persister, new_chain_monitor, nodes_1_deserialized);
+
+       check_closed_event!(nodes[1], 1, ClosureReason::DisconnectedPeer);
+       assert!(nodes[1].node.list_channels().is_empty());
+}
+
+#[test]
+fn test_inbound_reload_without_init_mon() {
+       do_test_inbound_reload_without_init_mon(true, true);
+       do_test_inbound_reload_without_init_mon(true, false);
+       do_test_inbound_reload_without_init_mon(false, true);
+       do_test_inbound_reload_without_init_mon(false, false);
+}