Compute InflightHtlcs from available information in ChannelManager
[rust-lightning] / lightning / src / ln / priv_short_conf_tests.rs
index db8c11e4c1eab56a39858375447e8a9f30593291..42835ed8035637948ae89b067d229c52e9c71efc 100644 (file)
 //! other behavior that exists only on private channels or with a semi-trusted counterparty (eg
 //! LSP).
 
-use chain::{ChannelMonitorUpdateErr, Watch};
-use chain::channelmonitor::ChannelMonitor;
-use chain::keysinterface::{Recipient, KeysInterface};
-use ln::channelmanager::{self, ChannelManager, ChannelManagerReadArgs, MIN_CLTV_EXPIRY_DELTA};
-use routing::gossip::RoutingFees;
-use routing::router::{PaymentParameters, RouteHint, RouteHintHop};
-use ln::features::ChannelTypeFeatures;
-use ln::msgs;
-use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ChannelUpdate, ErrorAction};
-use ln::wire::Encode;
-use util::enforcing_trait_impls::EnforcingSigner;
-use util::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider};
-use util::config::UserConfig;
-use util::ser::{Writeable, ReadableArgs};
-use util::test_utils;
-
-use prelude::*;
+use crate::chain::ChannelMonitorUpdateStatus;
+use crate::chain::keysinterface::{Recipient, KeysInterface};
+use crate::ln::channelmanager::{self, ChannelManager, MIN_CLTV_EXPIRY_DELTA, PaymentId};
+use crate::routing::gossip::RoutingFees;
+use crate::routing::router::{PaymentParameters, RouteHint, RouteHintHop};
+use crate::ln::features::ChannelTypeFeatures;
+use crate::ln::msgs;
+use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ChannelUpdate, ErrorAction};
+use crate::ln::wire::Encode;
+use crate::util::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider};
+use crate::util::config::UserConfig;
+use crate::util::ser::Writeable;
+use crate::util::test_utils;
+
+use crate::prelude::*;
 use core::default::Default;
 
-use ln::functional_test_utils::*;
+use crate::ln::functional_test_utils::*;
 
 use bitcoin::blockdata::constants::genesis_block;
-use bitcoin::hash_types::BlockHash;
 use bitcoin::hashes::Hash;
 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
 use bitcoin::network::constants::Network;
@@ -51,7 +48,7 @@ fn test_priv_forwarding_rejection() {
        let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]);
        let persister: test_utils::TestPersister;
        let new_chain_monitor: test_utils::TestChainMonitor;
-       let nodes_1_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
+       let nodes_1_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
 
        let chan_id_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features()).2;
@@ -77,7 +74,7 @@ fn test_priv_forwarding_rejection() {
                .with_route_hints(last_hops);
        let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000, TEST_FINAL_CLTV);
 
-       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);
        let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
        nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
@@ -100,47 +97,11 @@ fn test_priv_forwarding_rejection() {
        nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
 
        let nodes_1_serialized = nodes[1].node.encode();
-       let mut monitor_a_serialized = test_utils::TestVecWriter(Vec::new());
-       let mut monitor_b_serialized = test_utils::TestVecWriter(Vec::new());
-       get_monitor!(nodes[1], chan_id_1).write(&mut monitor_a_serialized).unwrap();
-       get_monitor!(nodes[1], chan_id_2).write(&mut monitor_b_serialized).unwrap();
-
-       persister = test_utils::TestPersister::new();
-       let keys_manager = &chanmon_cfgs[1].keys_manager;
-       new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[1].chain_source), nodes[1].tx_broadcaster.clone(), nodes[1].logger, node_cfgs[1].fee_estimator, &persister, keys_manager);
-       nodes[1].chain_monitor = &new_chain_monitor;
-
-       let mut monitor_a_read = &monitor_a_serialized.0[..];
-       let mut monitor_b_read = &monitor_b_serialized.0[..];
-       let (_, mut monitor_a) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut monitor_a_read, keys_manager).unwrap();
-       let (_, mut monitor_b) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut monitor_b_read, keys_manager).unwrap();
-       assert!(monitor_a_read.is_empty());
-       assert!(monitor_b_read.is_empty());
+       let monitor_a_serialized = get_monitor!(nodes[1], chan_id_1).encode();
+       let monitor_b_serialized = get_monitor!(nodes[1], chan_id_2).encode();
 
        no_announce_cfg.accept_forwards_to_priv_channels = true;
-
-       let mut nodes_1_read = &nodes_1_serialized[..];
-       let (_, nodes_1_deserialized_tmp) = {
-               let mut channel_monitors = HashMap::new();
-               channel_monitors.insert(monitor_a.get_funding_txo().0, &mut monitor_a);
-               channel_monitors.insert(monitor_b.get_funding_txo().0, &mut monitor_b);
-               <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_1_read, ChannelManagerReadArgs {
-                       default_config: no_announce_cfg,
-                       keys_manager,
-                       fee_estimator: node_cfgs[1].fee_estimator,
-                       chain_monitor: nodes[1].chain_monitor,
-                       tx_broadcaster: nodes[1].tx_broadcaster.clone(),
-                       logger: nodes[1].logger,
-                       channel_monitors,
-               }).unwrap()
-       };
-       assert!(nodes_1_read.is_empty());
-       nodes_1_deserialized = nodes_1_deserialized_tmp;
-
-       assert!(nodes[1].chain_monitor.watch_channel(monitor_a.get_funding_txo().0, monitor_a).is_ok());
-       assert!(nodes[1].chain_monitor.watch_channel(monitor_b.get_funding_txo().0, monitor_b).is_ok());
-       check_added_monitors!(nodes[1], 2);
-       nodes[1].node = &nodes_1_deserialized;
+       reload_node!(nodes[1], no_announce_cfg, &nodes_1_serialized, &[&monitor_a_serialized, &monitor_b_serialized], persister, new_chain_monitor, nodes_1_deserialized);
 
        nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap();
        nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap();
@@ -160,7 +121,7 @@ fn test_priv_forwarding_rejection() {
        get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
        get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
 
-       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);
        pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 10_000, our_payment_hash, our_payment_secret);
        claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], our_payment_preimage);
@@ -200,6 +161,8 @@ fn do_test_1_conf_open(connect_style: ConnectStyle) {
        } else { panic!("Unexpected event"); }
 
        nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
+       expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
+       expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
        let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events();
        assert_eq!(bs_msg_events.len(), 1);
        if let MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } = bs_msg_events[0] {
@@ -278,7 +241,7 @@ fn test_routed_scid_alias() {
                .with_route_hints(hop_hints);
        let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42);
        assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
-       nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
+       nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
        check_added_monitors!(nodes[0], 1);
 
        pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 100_000, payment_hash, payment_secret);
@@ -405,8 +368,10 @@ fn test_inbound_scid_privacy() {
        connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1);
        let bs_channel_ready = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[2].node.get_our_node_id());
        nodes[1].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &get_event_msg!(nodes[2], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id()));
+       expect_channel_ready_event(&nodes[1], &nodes[2].node.get_our_node_id());
        let bs_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
        nodes[2].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &bs_channel_ready);
+       expect_channel_ready_event(&nodes[2], &nodes[1].node.get_our_node_id());
        let cs_update = get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
 
        nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &cs_update);
@@ -431,7 +396,7 @@ fn test_inbound_scid_privacy() {
                .with_route_hints(hop_hints.clone());
        let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42);
        assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
-       nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
+       nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
        check_added_monitors!(nodes[0], 1);
 
        pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 100_000, payment_hash, payment_secret);
@@ -446,7 +411,7 @@ fn test_inbound_scid_privacy() {
                .with_route_hints(hop_hints);
        let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params_2, 100_000, 42);
        assert_eq!(route_2.paths[0][1].short_channel_id, last_hop[0].short_channel_id.unwrap());
-       nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2)).unwrap();
+       nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
        check_added_monitors!(nodes[0], 1);
 
        let payment_event = SendEvent::from_node(&nodes[0]);
@@ -501,7 +466,7 @@ fn test_scid_alias_returned() {
        route.paths[0][1].fee_msat = 10_000_000; // Overshoot the last channel's value
 
        // Route the HTLC through to the destination.
-       nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
+       nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
        nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]);
@@ -545,7 +510,7 @@ fn test_scid_alias_returned() {
        route.paths[0][0].fee_msat = 0; // But set fee paid to the middle hop to 0
 
        // Route the HTLC through to the destination.
-       nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
+       nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
        check_added_monitors!(nodes[0], 1);
        let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
        nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]);
@@ -624,7 +589,7 @@ fn test_0conf_channel_with_async_monitor() {
        nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
        let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
 
-       chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
+       chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
        nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
        check_added_monitors!(nodes[1], 1);
        assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
@@ -634,7 +599,7 @@ fn test_0conf_channel_with_async_monitor() {
 
        let bs_signed_locked = nodes[1].node.get_and_clear_pending_msg_events();
        assert_eq!(bs_signed_locked.len(), 2);
-       chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
+       chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
 
        match &bs_signed_locked[0] {
                MessageSendEvent::SendFundingSigned { node_id, msg } => {
@@ -670,6 +635,9 @@ fn test_0conf_channel_with_async_monitor() {
                }
                _ => panic!("Unexpected event"),
        }
+       expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
+       expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
+
        let bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
 
        let as_channel_update = match &as_locked_update[1] {
@@ -680,8 +648,8 @@ fn test_0conf_channel_with_async_monitor() {
                _ => panic!("Unexpected event"),
        };
 
-       chanmon_cfgs[0].persister.set_update_ret(Ok(()));
-       chanmon_cfgs[1].persister.set_update_ret(Ok(()));
+       chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
+       chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
 
        nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_channel_update);
        nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &as_channel_update);
@@ -695,7 +663,7 @@ fn test_0conf_channel_with_async_monitor() {
        // failure before we've ever confirmed the funding transaction. This previously caused a panic.
        let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
 
-       nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
+       nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap();
        check_added_monitors!(nodes[0], 1);
 
        let as_send = SendEvent::from_node(&nodes[0]);
@@ -710,12 +678,12 @@ fn test_0conf_channel_with_async_monitor() {
        nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed);
        check_added_monitors!(nodes[0], 1);
 
-       chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
+       chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
        nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id()));
        check_added_monitors!(nodes[1], 1);
        assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
 
-       chanmon_cfgs[1].persister.set_update_ret(Ok(()));
+       chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed);
        let (outpoint, _, latest_update) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&bs_raa.channel_id).unwrap().clone();
        nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(outpoint, latest_update).unwrap();
        check_added_monitors!(nodes[1], 0);