From: Matt Corallo Date: Tue, 15 Nov 2022 02:43:51 +0000 (+0000) Subject: Replace manual node reloading with a macro/function in tests X-Git-Tag: v0.0.113~39^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=e359c401434f1c1554bd2eb5e0ecfc2566a05b3d;p=rust-lightning Replace manual node reloading with a macro/function in tests Fixes #1696 --- diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index 368b2e5ff..9b0e30367 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -19,11 +19,10 @@ use bitcoin::network::constants::Network; 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, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure, PaymentId}; +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::config::UserConfig; use crate::util::enforcing_trait_impls::EnforcingSigner; use crate::util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose, ClosureReason, HTLCDestination}; use crate::util::errors::APIError; @@ -2294,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)>::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<&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); } @@ -2832,28 +2799,7 @@ fn do_test_outbound_reload_without_init_mon(use_0conf: bool) { nodes[0].chain_source.watched_txn.lock().unwrap().clear(); nodes[0].chain_source.watched_outputs.lock().unwrap().clear(); - let nodes_0_serialized = nodes[0].node.encode(); - 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 nodes_0_read = &nodes_0_serialized[..]; - let config = UserConfig::default(); - nodes_0_deserialized = { - <(BlockHash, ChannelManager<&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: HashMap::new(), - }).unwrap().1 - }; - nodes[0].node = &nodes_0_deserialized; - assert!(nodes_0_read.is_empty()); - + 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()); } @@ -2938,27 +2884,7 @@ fn do_test_inbound_reload_without_init_mon(use_0conf: bool, lock_commitment: boo nodes[1].chain_source.watched_txn.lock().unwrap().clear(); nodes[1].chain_source.watched_outputs.lock().unwrap().clear(); - let nodes_1_serialized = nodes[1].node.encode(); - 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 nodes_1_read = &nodes_1_serialized[..]; - let config = UserConfig::default(); - nodes_1_deserialized = { - <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_1_read, ChannelManagerReadArgs { - default_config: config, - 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: HashMap::new(), - }).unwrap().1 - }; - nodes[1].node = &nodes_1_deserialized; - assert!(nodes_1_read.is_empty()); + 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()); diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index d6a3f35cc..72e3c34ce 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -276,6 +276,7 @@ pub struct NodeCfg<'a> { pub struct Node<'a, 'b: 'a, 'c: 'b> { pub chain_source: &'c test_utils::TestChainSource, pub tx_broadcaster: &'c test_utils::TestBroadcaster, + pub fee_estimator: &'c test_utils::TestFeeEstimator, pub chain_monitor: &'b test_utils::TestChainMonitor<'c>, pub keys_manager: &'b test_utils::TestKeysInterface, pub node: &'a ChannelManager<&'b TestChainMonitor<'c>, &'c test_utils::TestBroadcaster, &'b test_utils::TestKeysInterface, &'c test_utils::TestFeeEstimator, &'c test_utils::TestLogger>, @@ -618,6 +619,60 @@ macro_rules! check_added_monitors { } } +pub fn _reload_node<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, default_config: UserConfig, chanman_encoded: &[u8], monitors_encoded: &[&[u8]]) -> ChannelManager<&'b TestChainMonitor<'c>, &'c test_utils::TestBroadcaster, &'b test_utils::TestKeysInterface, &'c test_utils::TestFeeEstimator, &'c test_utils::TestLogger> { + let mut monitors_read = Vec::with_capacity(monitors_encoded.len()); + for encoded in monitors_encoded { + let mut monitor_read = &encoded[..]; + let (_, monitor) = <(BlockHash, ChannelMonitor)> + ::read(&mut monitor_read, node.keys_manager).unwrap(); + assert!(monitor_read.is_empty()); + monitors_read.push(monitor); + } + + let mut node_read = &chanman_encoded[..]; + let (_, node_deserialized) = { + let mut channel_monitors = HashMap::new(); + for monitor in monitors_read.iter_mut() { + assert!(channel_monitors.insert(monitor.get_funding_txo().0, monitor).is_none()); + } + <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut node_read, ChannelManagerReadArgs { + default_config, + keys_manager: node.keys_manager, + fee_estimator: node.fee_estimator, + chain_monitor: node.chain_monitor, + tx_broadcaster: node.tx_broadcaster, + logger: node.logger, + channel_monitors, + }).unwrap() + }; + assert!(node_read.is_empty()); + + for monitor in monitors_read.drain(..) { + assert_eq!(node.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor), + ChannelMonitorUpdateStatus::Completed); + check_added_monitors!(node, 1); + } + + node_deserialized +} + +#[cfg(test)] +macro_rules! reload_node { + ($node: expr, $new_config: expr, $chanman_encoded: expr, $monitors_encoded: expr, $persister: ident, $new_chain_monitor: ident, $new_channelmanager: ident) => { + let chanman_encoded = $chanman_encoded; + + $persister = test_utils::TestPersister::new(); + $new_chain_monitor = test_utils::TestChainMonitor::new(Some($node.chain_source), $node.tx_broadcaster.clone(), $node.logger, $node.fee_estimator, &$persister, &$node.keys_manager); + $node.chain_monitor = &$new_chain_monitor; + + $new_channelmanager = _reload_node(&$node, $new_config, &chanman_encoded, $monitors_encoded); + $node.node = &$new_channelmanager; + }; + ($node: expr, $chanman_encoded: expr, $monitors_encoded: expr, $persister: ident, $new_chain_monitor: ident, $new_channelmanager: ident) => { + reload_node!($node, $crate::util::config::UserConfig::default(), $chanman_encoded, $monitors_encoded, $persister, $new_chain_monitor, $new_channelmanager); + }; +} + pub fn create_funding_transaction<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, expected_counterparty_node_id: &PublicKey, expected_chan_value: u64, expected_user_chan_id: u128) -> ([u8; 32], Transaction, OutPoint) { let chan_id = *node.network_chan_count.borrow(); @@ -2126,6 +2181,7 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec)>::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 (_, nodes_0_deserialized_tmp) = { - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); - <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs { - default_config: UserConfig::default(), - 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() - }; - nodes_0_deserialized = nodes_0_deserialized_tmp; - assert!(nodes_0_read.is_empty()); - - assert_eq!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor), - ChannelMonitorUpdateStatus::Completed); - nodes[0].node = &nodes_0_deserialized; - check_added_monitors!(nodes[0], 1); + reload_node!(nodes[0], &nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized); reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false)); } @@ -4339,8 +4308,6 @@ fn test_no_txn_manager_serialize_deserialize() { let chanmon_cfgs = create_chanmon_cfgs(2); let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); - let logger: test_utils::TestLogger; - let fee_estimator: test_utils::TestFeeEstimator; 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>; @@ -4350,45 +4317,9 @@ fn test_no_txn_manager_serialize_deserialize() { nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); - let nodes_0_serialized = nodes[0].node.encode(); - let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[0], OutPoint { txid: tx.txid(), index: 0 }.to_channel_id()) - .write(&mut chan_0_monitor_serialized).unwrap(); - - logger = test_utils::TestLogger::new(); - fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }; - 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(), &logger, &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)>::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(); - let (_, nodes_0_deserialized_tmp) = { - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); - <(BlockHash, ChannelManager<&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: &fee_estimator, - chain_monitor: nodes[0].chain_monitor, - tx_broadcaster: nodes[0].tx_broadcaster.clone(), - logger: &logger, - channel_monitors, - }).unwrap() - }; - nodes_0_deserialized = nodes_0_deserialized_tmp; - assert!(nodes_0_read.is_empty()); - - assert_eq!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor), - ChannelMonitorUpdateStatus::Completed); - nodes[0].node = &nodes_0_deserialized; - assert_eq!(nodes[0].node.list_channels().len(), 1); - check_added_monitors!(nodes[0], 1); + let chan_0_monitor_serialized = + get_monitor!(nodes[0], OutPoint { txid: tx.txid(), index: 0 }.to_channel_id()).encode(); + reload_node!(nodes[0], nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_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(); let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]); @@ -4417,9 +4348,7 @@ fn test_manager_serialize_deserialize_events() { let chanmon_cfgs = create_chanmon_cfgs(2); let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); - let fee_estimator: test_utils::TestFeeEstimator; let persister: test_utils::TestPersister; - let logger: test_utils::TestLogger; 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 nodes = create_network(2, &node_cfgs, &node_chanmgrs); @@ -4462,45 +4391,11 @@ fn test_manager_serialize_deserialize_events() { nodes.push(node_b); // Start the de/seriailization process mid-channel creation to check that the channel manager will hold onto events that are serialized - let nodes_0_serialized = nodes[0].node.encode(); - let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[0], bs_funding_signed.channel_id).write(&mut chan_0_monitor_serialized).unwrap(); - - fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }; - logger = test_utils::TestLogger::new(); - 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(), &logger, &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)>::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(); - let (_, nodes_0_deserialized_tmp) = { - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); - <(BlockHash, ChannelManager<&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: &fee_estimator, - chain_monitor: nodes[0].chain_monitor, - tx_broadcaster: nodes[0].tx_broadcaster.clone(), - logger: &logger, - channel_monitors, - }).unwrap() - }; - nodes_0_deserialized = nodes_0_deserialized_tmp; - assert!(nodes_0_read.is_empty()); + let chan_0_monitor_serialized = get_monitor!(nodes[0], bs_funding_signed.channel_id).encode(); + reload_node!(nodes[0], nodes[0].node.encode(), &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized); nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); - assert_eq!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor), - ChannelMonitorUpdateStatus::Completed); - nodes[0].node = &nodes_0_deserialized; - // After deserializing, make sure the funding_transaction is still held by the channel manager let events_4 = nodes[0].node.get_and_clear_pending_events(); assert_eq!(events_4.len(), 0); @@ -4509,7 +4404,6 @@ fn test_manager_serialize_deserialize_events() { // Make sure the channel is functioning as though the de/serialization never happened assert_eq!(nodes[0].node.list_channels().len(), 1); - check_added_monitors!(nodes[0], 1); nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap(); let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]); @@ -4537,8 +4431,6 @@ fn test_simple_manager_serialize_deserialize() { let chanmon_cfgs = create_chanmon_cfgs(2); let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); - let logger: test_utils::TestLogger; - let fee_estimator: test_utils::TestFeeEstimator; 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>; @@ -4550,42 +4442,8 @@ fn test_simple_manager_serialize_deserialize() { nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); - 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(); - - logger = test_utils::TestLogger::new(); - fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }; - 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(), &logger, &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)>::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 (_, nodes_0_deserialized_tmp) = { - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); - <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs { - default_config: UserConfig::default(), - keys_manager, - fee_estimator: &fee_estimator, - chain_monitor: nodes[0].chain_monitor, - tx_broadcaster: nodes[0].tx_broadcaster.clone(), - logger: &logger, - channel_monitors, - }).unwrap() - }; - nodes_0_deserialized = nodes_0_deserialized_tmp; - assert!(nodes_0_read.is_empty()); - - assert_eq!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor), - ChannelMonitorUpdateStatus::Completed); - nodes[0].node = &nodes_0_deserialized; - 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); reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false)); @@ -4630,9 +4488,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() { // nodes[3]) let mut node_0_monitors_serialized = Vec::new(); for chan_id_iter in &[chan_id_1, chan_id_2, channel_id] { - let mut writer = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[0], chan_id_iter).write(&mut writer).unwrap(); - node_0_monitors_serialized.push(writer.0); + node_0_monitors_serialized.push(get_monitor!(nodes[0], chan_id_iter).encode()); } logger = test_utils::TestLogger::new(); @@ -7475,18 +7331,13 @@ fn do_test_data_loss_protect(reconnect_panicing: bool) { // chain::Watch API requirements have been violated (e.g. the user restored from a backup). The // panic message informs the user they should force-close without broadcasting, which is tested // if `reconnect_panicing` is not set. - let persister; - let logger; - let fee_estimator; - let tx_broadcaster; - let chain_source; let mut chanmon_cfgs = create_chanmon_cfgs(2); // We broadcast during Drop because chanmon is out of sync with chanmgr, which would cause a panic // during signing due to revoked tx chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true; - let keys_manager = &chanmon_cfgs[0].keys_manager; - let monitor; - let node_state_0; + let persister; + let new_chain_monitor; + let nodes_0_deserialized; let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); @@ -7495,8 +7346,7 @@ fn do_test_data_loss_protect(reconnect_panicing: bool) { // Cache node A state before any channel update let previous_node_state = nodes[0].node.encode(); - let mut previous_chain_monitor_state = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[0], chan.2).write(&mut previous_chain_monitor_state).unwrap(); + let previous_chain_monitor_state = get_monitor!(nodes[0], chan.2).encode(); send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000); send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000); @@ -7504,34 +7354,7 @@ fn do_test_data_loss_protect(reconnect_panicing: bool) { nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); - // Restore node A from previous state - logger = test_utils::TestLogger::with_id(format!("node {}", 0)); - let mut chain_monitor = <(BlockHash, ChannelMonitor)>::read(&mut io::Cursor::new(previous_chain_monitor_state.0), keys_manager).unwrap().1; - chain_source = test_utils::TestChainSource::new(Network::Testnet); - tx_broadcaster = test_utils::TestBroadcaster { txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new())) }; - fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }; - persister = test_utils::TestPersister::new(); - monitor = test_utils::TestChainMonitor::new(Some(&chain_source), &tx_broadcaster, &logger, &fee_estimator, &persister, keys_manager); - node_state_0 = { - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(OutPoint { txid: chan.3.txid(), index: 0 }, &mut chain_monitor); - <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut io::Cursor::new(previous_node_state), ChannelManagerReadArgs { - keys_manager: keys_manager, - fee_estimator: &fee_estimator, - chain_monitor: &monitor, - logger: &logger, - tx_broadcaster: &tx_broadcaster, - default_config: UserConfig::default(), - channel_monitors, - }).unwrap().1 - }; - nodes[0].node = &node_state_0; - assert_eq!(monitor.watch_channel(OutPoint { txid: chan.3.txid(), index: 0 }, chain_monitor), - ChannelMonitorUpdateStatus::Completed); - nodes[0].chain_monitor = &monitor; - nodes[0].chain_source = &chain_source; - - check_added_monitors!(nodes[0], 1); + reload_node!(nodes[0], previous_node_state, &[&previous_chain_monitor_state], persister, new_chain_monitor, nodes_0_deserialized); if reconnect_panicing { nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap(); @@ -9688,50 +9511,9 @@ fn test_forwardable_regen() { nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); - let nodes_1_serialized = nodes[1].node.encode(); - let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - let mut chan_1_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[1], chan_id_1).write(&mut chan_0_monitor_serialized).unwrap(); - get_monitor!(nodes[1], chan_id_2).write(&mut chan_1_monitor_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 chan_0_monitor_read = &chan_0_monitor_serialized.0[..]; - let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor)>::read( - &mut chan_0_monitor_read, keys_manager).unwrap(); - assert!(chan_0_monitor_read.is_empty()); - let mut chan_1_monitor_read = &chan_1_monitor_serialized.0[..]; - let (_, mut chan_1_monitor) = <(BlockHash, ChannelMonitor)>::read( - &mut chan_1_monitor_read, keys_manager).unwrap(); - assert!(chan_1_monitor_read.is_empty()); - - let mut nodes_1_read = &nodes_1_serialized[..]; - let (_, nodes_1_deserialized_tmp) = { - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); - channel_monitors.insert(chan_1_monitor.get_funding_txo().0, &mut chan_1_monitor); - <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_1_read, ChannelManagerReadArgs { - default_config: UserConfig::default(), - 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() - }; - nodes_1_deserialized = nodes_1_deserialized_tmp; - assert!(nodes_1_read.is_empty()); - - assert_eq!(nodes[1].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor), - ChannelMonitorUpdateStatus::Completed); - assert_eq!(nodes[1].chain_monitor.watch_channel(chan_1_monitor.get_funding_txo().0, chan_1_monitor), - ChannelMonitorUpdateStatus::Completed); - nodes[1].node = &nodes_1_deserialized; - check_added_monitors!(nodes[1], 2); + let chan_0_monitor_serialized = get_monitor!(nodes[1], chan_id_1).encode(); + let chan_1_monitor_serialized = get_monitor!(nodes[1], chan_id_2).encode(); + reload_node!(nodes[1], nodes[1].node.encode(), &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized); reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false)); // Note that nodes[1] and nodes[2] resend their channel_ready here since they haven't updated @@ -10141,8 +9923,7 @@ fn do_test_partial_claim_before_restart(persist_both_monitors: bool) { } } - let mut original_manager = test_utils::TestVecWriter(Vec::new()); - nodes[3].node.write(&mut original_manager).unwrap(); + let original_manager = nodes[3].node.encode(); expect_payment_received!(nodes[3], payment_hash, payment_secret, 15_000_000); @@ -10171,41 +9952,11 @@ fn do_test_partial_claim_before_restart(persist_both_monitors: bool) { } // Now restart nodes[3]. - persister = test_utils::TestPersister::new(); - let keys_manager = &chanmon_cfgs[3].keys_manager; - new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[3].chain_source), nodes[3].tx_broadcaster.clone(), nodes[3].logger, node_cfgs[3].fee_estimator, &persister, keys_manager); - nodes[3].chain_monitor = &new_chain_monitor; - let mut monitors = Vec::new(); - for mut monitor_data in [original_monitor, updated_monitor].iter() { - let (_, mut deserialized_monitor) = <(BlockHash, ChannelMonitor)>::read(&mut &monitor_data.0[..], keys_manager).unwrap(); - monitors.push(deserialized_monitor); - } - - let config = UserConfig::default(); - nodes_3_deserialized = { - let mut channel_monitors = HashMap::new(); - for monitor in monitors.iter_mut() { - channel_monitors.insert(monitor.get_funding_txo().0, monitor); - } - <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut &original_manager.0[..], ChannelManagerReadArgs { - default_config: config, - keys_manager, - fee_estimator: node_cfgs[3].fee_estimator, - chain_monitor: nodes[3].chain_monitor, - tx_broadcaster: nodes[3].tx_broadcaster.clone(), - logger: nodes[3].logger, - channel_monitors, - }).unwrap().1 - }; - nodes[3].node = &nodes_3_deserialized; + reload_node!(nodes[3], original_manager, &[&updated_monitor.0, &original_monitor.0], persister, new_chain_monitor, nodes_3_deserialized); - for monitor in monitors { - // On startup the preimage should have been copied into the non-persisted monitor: - assert!(monitor.get_stored_preimages().contains_key(&payment_hash)); - assert_eq!(nodes[3].chain_monitor.watch_channel(monitor.get_funding_txo().0.clone(), monitor), - ChannelMonitorUpdateStatus::Completed); - } - check_added_monitors!(nodes[3], 2); + // On startup the preimage should have been copied into the non-persisted monitor: + assert!(get_monitor!(nodes[3], chan_id_persisted).get_stored_preimages().contains_key(&payment_hash)); + assert!(get_monitor!(nodes[3], chan_id_not_persisted).get_stored_preimages().contains_key(&payment_hash)); nodes[1].node.peer_disconnected(&nodes[3].node.get_our_node_id(), false); nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id(), false); diff --git a/lightning/src/ln/onion_route_tests.rs b/lightning/src/ln/onion_route_tests.rs index b5c55ccf5..c9422a6e0 100644 --- a/lightning/src/ln/onion_route_tests.rs +++ b/lightning/src/ln/onion_route_tests.rs @@ -11,11 +11,11 @@ //! These tests work by standing up full nodes and route payments across the network, checking the //! returned errors decode to the correct thing. -use crate::chain::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS}; +use crate::chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS}; use crate::chain::keysinterface::{KeysInterface, Recipient}; use crate::ln::{PaymentHash, PaymentSecret}; use crate::ln::channel::EXPIRE_PREV_CONFIG_TICKS; -use crate::ln::channelmanager::{self, ChannelManager, ChannelManagerReadArgs, HTLCForwardInfo, CLTV_FAR_FAR_AWAY, MIN_CLTV_EXPIRY_DELTA, PendingAddHTLCInfo, PendingHTLCInfo, PendingHTLCRouting, PaymentId}; +use crate::ln::channelmanager::{self, HTLCForwardInfo, CLTV_FAR_FAR_AWAY, MIN_CLTV_EXPIRY_DELTA, PendingAddHTLCInfo, PendingHTLCInfo, PendingHTLCRouting, PaymentId}; use crate::ln::onion_utils; use crate::routing::gossip::{NetworkUpdate, RoutingFees}; use crate::routing::router::{get_route, PaymentParameters, Route, RouteHint, RouteHintHop}; @@ -24,7 +24,7 @@ use crate::ln::msgs; use crate::ln::msgs::{ChannelMessageHandler, ChannelUpdate}; use crate::ln::wire::Encode; use crate::util::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider}; -use crate::util::ser::{ReadableArgs, Writeable, Writer}; +use crate::util::ser::{Writeable, Writer}; use crate::util::{byte_utils, test_utils}; use crate::util::config::{UserConfig, ChannelConfig}; use crate::util::errors::APIError; @@ -613,6 +613,9 @@ fn do_test_onion_failure_stale_channel_update(announced_channel: bool) { config.channel_handshake_limits.force_announced_channel_preference = false; config.accept_forwards_to_priv_channels = !announced_channel; let chanmon_cfgs = create_chanmon_cfgs(3); + let persister; + let chain_monitor; + let channel_manager_1_deserialized; let node_cfgs = create_node_cfgs(3, &chanmon_cfgs); let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(config), None]); let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); @@ -761,34 +764,11 @@ fn do_test_onion_failure_stale_channel_update(announced_channel: bool) { // To test persistence of the updated config, we'll re-initialize the ChannelManager. let config_after_restart = { - let persister = test_utils::TestPersister::new(); - let 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, nodes[1].keys_manager, - ); - - let mut chanmon_1 = <(_, ChannelMonitor<_>)>::read( - &mut &get_monitor!(nodes[1], other_channel.3).encode()[..], nodes[1].keys_manager, - ).unwrap().1; - let mut chanmon_2 = <(_, ChannelMonitor<_>)>::read( - &mut &get_monitor!(nodes[1], channel_to_update.0).encode()[..], nodes[1].keys_manager, - ).unwrap().1; - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(chanmon_1.get_funding_txo().0, &mut chanmon_1); - channel_monitors.insert(chanmon_2.get_funding_txo().0, &mut chanmon_2); - - let chanmgr = <(_, ChannelManager<_, _, _, _, _>)>::read( - &mut &nodes[1].node.encode()[..], ChannelManagerReadArgs { - default_config: *nodes[1].node.get_current_default_configuration(), - keys_manager: nodes[1].keys_manager, - fee_estimator: node_cfgs[1].fee_estimator, - chain_monitor: &chain_monitor, - tx_broadcaster: nodes[1].tx_broadcaster.clone(), - logger: nodes[1].logger, - channel_monitors: channel_monitors, - }, - ).unwrap().1; - chanmgr.list_channels().iter() + let chan_1_monitor_serialized = get_monitor!(nodes[1], other_channel.3).encode(); + let chan_2_monitor_serialized = get_monitor!(nodes[1], channel_to_update.0).encode(); + reload_node!(nodes[1], *nodes[1].node.get_current_default_configuration(), &nodes[1].node.encode(), + &[&chan_1_monitor_serialized, &chan_2_monitor_serialized], persister, chain_monitor, channel_manager_1_deserialized); + nodes[1].node.list_channels().iter() .find(|channel| channel.channel_id == channel_to_update.0).unwrap() .config.unwrap() }; diff --git a/lightning/src/ln/payment_tests.rs b/lightning/src/ln/payment_tests.rs index a65da3687..ade02c48c 100644 --- a/lightning/src/ln/payment_tests.rs +++ b/lightning/src/ln/payment_tests.rs @@ -12,22 +12,20 @@ //! payments thereafter. use crate::chain::{ChannelMonitorUpdateStatus, Confirm, Listen, Watch}; -use crate::chain::channelmonitor::{ANTI_REORG_DELAY, ChannelMonitor, LATENCY_GRACE_PERIOD_BLOCKS}; +use crate::chain::channelmonitor::{ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS}; use crate::chain::transaction::OutPoint; use crate::chain::keysinterface::KeysInterface; use crate::ln::channel::EXPIRE_PREV_CONFIG_TICKS; -use crate::ln::channelmanager::{self, BREAKDOWN_TIMEOUT, ChannelManager, ChannelManagerReadArgs, MPP_TIMEOUT_TICKS, MIN_CLTV_EXPIRY_DELTA, PaymentId, PaymentSendFailure, IDEMPOTENCY_TIMEOUT_TICKS}; +use crate::ln::channelmanager::{self, BREAKDOWN_TIMEOUT, ChannelManager, MPP_TIMEOUT_TICKS, MIN_CLTV_EXPIRY_DELTA, PaymentId, PaymentSendFailure, IDEMPOTENCY_TIMEOUT_TICKS}; use crate::ln::msgs; use crate::ln::msgs::ChannelMessageHandler; use crate::routing::router::{PaymentParameters, get_route}; use crate::util::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider}; use crate::util::test_utils; use crate::util::errors::APIError; -use crate::util::enforcing_trait_impls::EnforcingSigner; -use crate::util::ser::{ReadableArgs, Writeable}; -use crate::io; +use crate::util::ser::Writeable; -use bitcoin::{Block, BlockHeader, BlockHash, TxMerkleNode}; +use bitcoin::{Block, BlockHeader, TxMerkleNode}; use bitcoin::hashes::Hash; use bitcoin::network::constants::Network; @@ -409,39 +407,8 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) { // The ChannelMonitor should always be the latest version, as we're required to persist it // during the `commitment_signed_dance!()`. - 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)>::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 (_, nodes_0_deserialized_tmp) = { - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); - <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs { - default_config: test_default_channel_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() - }; - nodes_0_deserialized = nodes_0_deserialized_tmp; - assert!(nodes_0_read.is_empty()); - - assert_eq!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor), - ChannelMonitorUpdateStatus::Completed); - nodes[0].node = &nodes_0_deserialized; - check_added_monitors!(nodes[0], 1); + let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode(); + reload_node!(nodes[0], test_default_channel_config(), &nodes_0_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized); // On reload, the ChannelManager should realize it is stale compared to the ChannelMonitor and // force-close the channel. @@ -603,64 +570,10 @@ fn do_test_completed_payment_not_retryable_on_reload(use_dust: bool) { // The ChannelMonitor should always be the latest version, as we're required to persist it // during the `commitment_signed_dance!()`. - let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[0], chan_id).write(&mut chan_0_monitor_serialized).unwrap(); - - let mut chan_1_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - - macro_rules! reload_node { - ($chain_monitor: ident, $chan_manager: ident, $persister: ident) => { { - $persister = test_utils::TestPersister::new(); - let keys_manager = &chanmon_cfgs[0].keys_manager; - $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 = &$chain_monitor; - let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..]; - let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor)>::read( - &mut chan_0_monitor_read, keys_manager).unwrap(); - assert!(chan_0_monitor_read.is_empty()); - - let mut chan_1_monitor = None; - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); - - if !chan_1_monitor_serialized.0.is_empty() { - let mut chan_1_monitor_read = &chan_1_monitor_serialized.0[..]; - chan_1_monitor = Some(<(BlockHash, ChannelMonitor)>::read( - &mut chan_1_monitor_read, keys_manager).unwrap().1); - assert!(chan_1_monitor_read.is_empty()); - channel_monitors.insert(chan_1_monitor.as_ref().unwrap().get_funding_txo().0, chan_1_monitor.as_mut().unwrap()); - } - - let mut nodes_0_read = &nodes_0_serialized[..]; - let (_, nodes_0_deserialized_tmp) = { - <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs { - default_config: test_default_channel_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() - }; - $chan_manager = nodes_0_deserialized_tmp; - assert!(nodes_0_read.is_empty()); - - assert_eq!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor), - ChannelMonitorUpdateStatus::Completed); - if !chan_1_monitor_serialized.0.is_empty() { - let funding_txo = chan_1_monitor.as_ref().unwrap().get_funding_txo().0; - assert_eq!(nodes[0].chain_monitor.watch_channel(funding_txo, chan_1_monitor.unwrap()), - ChannelMonitorUpdateStatus::Completed); - } - nodes[0].node = &$chan_manager; - check_added_monitors!(nodes[0], if !chan_1_monitor_serialized.0.is_empty() { 2 } else { 1 }); - - nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); - } } - } + let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode(); - reload_node!(first_new_chain_monitor, first_nodes_0_deserialized, first_persister); + reload_node!(nodes[0], test_default_channel_config(), nodes_0_serialized, &[&chan_0_monitor_serialized], first_persister, first_new_chain_monitor, first_nodes_0_deserialized); + nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); // On reload, the ChannelManager should realize it is stale compared to the ChannelMonitor and // force-close the channel. @@ -744,16 +657,16 @@ fn do_test_completed_payment_not_retryable_on_reload(use_dust: bool) { // We set mpp_parts_remain to avoid having abandon_payment called expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain()); - chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[0], chan_id).write(&mut chan_0_monitor_serialized).unwrap(); - chan_1_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[0], chan_id_3).write(&mut chan_1_monitor_serialized).unwrap(); + let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode(); + let chan_1_monitor_serialized = get_monitor!(nodes[0], chan_id_3).encode(); nodes_0_serialized = nodes[0].node.encode(); assert!(nodes[0].node.retry_payment(&new_route, payment_id).is_ok()); assert!(!nodes[0].node.get_and_clear_pending_msg_events().is_empty()); - reload_node!(second_new_chain_monitor, second_nodes_0_deserialized, second_persister); + reload_node!(nodes[0], test_default_channel_config(), nodes_0_serialized, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], second_persister, second_new_chain_monitor, second_nodes_0_deserialized); + nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); + reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false)); // Now resend the payment, delivering the HTLC and actually claiming it this time. This ensures @@ -766,14 +679,14 @@ fn do_test_completed_payment_not_retryable_on_reload(use_dust: bool) { assert!(nodes[0].node.retry_payment(&new_route, payment_id).is_err()); assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); - chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[0], chan_id).write(&mut chan_0_monitor_serialized).unwrap(); - chan_1_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[0], chan_id_3).write(&mut chan_1_monitor_serialized).unwrap(); + let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode(); + let chan_1_monitor_serialized = get_monitor!(nodes[0], chan_id_3).encode(); nodes_0_serialized = nodes[0].node.encode(); // Ensure that after reload we cannot retry the payment. - reload_node!(third_new_chain_monitor, third_nodes_0_deserialized, third_persister); + reload_node!(nodes[0], test_default_channel_config(), nodes_0_serialized, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], third_persister, third_new_chain_monitor, third_nodes_0_deserialized); + nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); + reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false)); assert!(nodes[0].node.retry_payment(&new_route, payment_id).is_err()); @@ -879,16 +792,15 @@ fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, co // If we persist the ChannelManager here, we should get the PaymentSent event after // deserialization. - let mut chan_manager_serialized = test_utils::TestVecWriter(Vec::new()); + let mut chan_manager_serialized = Vec::new(); if !persist_manager_post_event { - nodes[0].node.write(&mut chan_manager_serialized).unwrap(); + chan_manager_serialized = nodes[0].node.encode(); } // Now persist the ChannelMonitor and inform the ChainMonitor that we're done, generating the // payment sent event. chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed); - let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[0], chan_id).write(&mut chan_0_monitor_serialized).unwrap(); + let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode(); for update in mon_updates { nodes[0].chain_monitor.chain_monitor.channel_monitor_updated(funding_txo, update).unwrap(); } @@ -901,39 +813,11 @@ fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, co // If we persist the ChannelManager after we get the PaymentSent event, we shouldn't get it // twice. if persist_manager_post_event { - nodes[0].node.write(&mut chan_manager_serialized).unwrap(); + chan_manager_serialized = nodes[0].node.encode(); } // Now reload nodes[0]... - 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)>::read( - &mut chan_0_monitor_read, keys_manager).unwrap(); - assert!(chan_0_monitor_read.is_empty()); - - let (_, nodes_0_deserialized_tmp) = { - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); - <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)> - ::read(&mut io::Cursor::new(&chan_manager_serialized.0[..]), ChannelManagerReadArgs { - default_config: Default::default(), - 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() - }; - nodes_0_deserialized = nodes_0_deserialized_tmp; - - assert_eq!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor), - ChannelMonitorUpdateStatus::Completed); - check_added_monitors!(nodes[0], 1); - nodes[0].node = &nodes_0_deserialized; + reload_node!(nodes[0], &chan_manager_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized); if persist_manager_post_event { assert!(nodes[0].node.get_and_clear_pending_events().is_empty()); @@ -982,10 +866,8 @@ fn test_fulfill_restart_failure() { // The simplest way to get a failure after a fulfill is to reload nodes[1] from a state // pre-fulfill, which we do by serializing it here. - let mut chan_manager_serialized = test_utils::TestVecWriter(Vec::new()); - nodes[1].node.write(&mut chan_manager_serialized).unwrap(); - let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[1], chan_id).write(&mut chan_0_monitor_serialized).unwrap(); + let chan_manager_serialized = nodes[1].node.encode(); + let chan_0_monitor_serialized = get_monitor!(nodes[1], chan_id).encode(); nodes[1].node.claim_funds(payment_preimage); check_added_monitors!(nodes[1], 1); @@ -996,35 +878,7 @@ fn test_fulfill_restart_failure() { expect_payment_sent_without_paths!(nodes[0], payment_preimage); // Now reload nodes[1]... - 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 chan_0_monitor_read = &chan_0_monitor_serialized.0[..]; - let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor)>::read( - &mut chan_0_monitor_read, keys_manager).unwrap(); - assert!(chan_0_monitor_read.is_empty()); - - let (_, nodes_1_deserialized_tmp) = { - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); - <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)> - ::read(&mut io::Cursor::new(&chan_manager_serialized.0[..]), ChannelManagerReadArgs { - default_config: Default::default(), - 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() - }; - nodes_1_deserialized = nodes_1_deserialized_tmp; - - assert_eq!(nodes[1].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor), - ChannelMonitorUpdateStatus::Completed); - check_added_monitors!(nodes[1], 1); - nodes[1].node = &nodes_1_deserialized; + reload_node!(nodes[1], &chan_manager_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized); nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false)); diff --git a/lightning/src/ln/priv_short_conf_tests.rs b/lightning/src/ln/priv_short_conf_tests.rs index 6e2cf2583..42835ed80 100644 --- a/lightning/src/ln/priv_short_conf_tests.rs +++ b/lightning/src/ln/priv_short_conf_tests.rs @@ -11,20 +11,18 @@ //! other behavior that exists only on private channels or with a semi-trusted counterparty (eg //! LSP). -use crate::chain::{ChannelMonitorUpdateStatus, Watch}; -use crate::chain::channelmonitor::ChannelMonitor; +use crate::chain::ChannelMonitorUpdateStatus; use crate::chain::keysinterface::{Recipient, KeysInterface}; -use crate::ln::channelmanager::{self, ChannelManager, ChannelManagerReadArgs, MIN_CLTV_EXPIRY_DELTA, PaymentId}; +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::enforcing_trait_impls::EnforcingSigner; use crate::util::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider}; use crate::util::config::UserConfig; -use crate::util::ser::{Writeable, ReadableArgs}; +use crate::util::ser::Writeable; use crate::util::test_utils; use crate::prelude::*; @@ -33,7 +31,6 @@ use core::default::Default; 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; @@ -100,49 +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)>::read(&mut monitor_a_read, keys_manager).unwrap(); - let (_, mut monitor_b) = <(BlockHash, ChannelMonitor)>::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<&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_eq!(nodes[1].chain_monitor.watch_channel(monitor_a.get_funding_txo().0, monitor_a), - ChannelMonitorUpdateStatus::Completed); - assert_eq!(nodes[1].chain_monitor.watch_channel(monitor_b.get_funding_txo().0, monitor_b), - ChannelMonitorUpdateStatus::Completed); - 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(); diff --git a/lightning/src/ln/reorg_tests.rs b/lightning/src/ln/reorg_tests.rs index a445f420a..f6754e8bf 100644 --- a/lightning/src/ln/reorg_tests.rs +++ b/lightning/src/ln/reorg_tests.rs @@ -9,20 +9,18 @@ //! Further functional tests which test blockchain reorganizations. -use crate::chain::channelmonitor::{ANTI_REORG_DELAY, ChannelMonitor}; +use crate::chain::channelmonitor::ANTI_REORG_DELAY; use crate::chain::transaction::OutPoint; -use crate::chain::{ChannelMonitorUpdateStatus, Confirm, Watch}; -use crate::ln::channelmanager::{self, ChannelManager, ChannelManagerReadArgs}; +use crate::chain::Confirm; +use crate::ln::channelmanager::{self, ChannelManager}; use crate::ln::msgs::ChannelMessageHandler; -use crate::util::enforcing_trait_impls::EnforcingSigner; use crate::util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination}; use crate::util::test_utils; -use crate::util::ser::{ReadableArgs, Writeable}; +use crate::util::ser::Writeable; use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::script::Builder; use bitcoin::blockdata::opcodes; -use bitcoin::hash_types::BlockHash; use bitcoin::secp256k1::Secp256k1; use crate::prelude::*; @@ -315,46 +313,15 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_ // the Channel object from the ChannelManager, but still having a monitor event pending for // it when we go to deserialize, and then use the ChannelManager. let nodes_0_serialized = nodes[0].node.encode(); - let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[0], chan.2).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)>::read( - &mut chan_0_monitor_read, keys_manager).unwrap(); - assert!(chan_0_monitor_read.is_empty()); - - let mut nodes_0_read = &nodes_0_serialized[..]; - 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<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, - &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read( - &mut nodes_0_read, ChannelManagerReadArgs { - default_config: *nodes[0].node.get_current_default_configuration(), - 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()); + let chan_0_monitor_serialized = get_monitor!(nodes[0], chan.2).encode(); + + reload_node!(nodes[0], *nodes[0].node.get_current_default_configuration(), &nodes_0_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized); if !reorg_after_reload { // If the channel is already closed when we reload the node, we'll broadcast a closing // transaction via the ChannelMonitor which is missing a corresponding channel. assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1); nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear(); } - - 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); } if reorg_after_reload {