Renamed script_for_p2wpkh to get_p2wpkh_redeemscript to match convention
[rust-lightning] / lightning / src / ln / functional_test_utils.rs
index da334b45da8c465e0151a299bc5ffbce0effd599..0f188ac38eea4d4eadc2349ceb29af16c6db01d3 100644 (file)
@@ -275,10 +275,9 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
                        let feeest = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
                        let mut deserialized_monitors = Vec::new();
                        {
-                               let old_monitors = self.chain_monitor.chain_monitor.monitors.read().unwrap();
-                               for (_, old_monitor) in old_monitors.iter() {
+                               for outpoint in self.chain_monitor.chain_monitor.list_monitors() {
                                        let mut w = test_utils::TestVecWriter(Vec::new());
-                                       old_monitor.write(&mut w).unwrap();
+                                       self.chain_monitor.chain_monitor.get_monitor(outpoint).unwrap().write(&mut w).unwrap();
                                        let (_, deserialized_monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
                                                &mut io::Cursor::new(&w.0), self.keys_manager).unwrap();
                                        deserialized_monitors.push(deserialized_monitor);
@@ -438,20 +437,35 @@ macro_rules! get_feerate {
        }
 }
 
-/// Returns any local commitment transactions for the channel.
+/// Returns a channel monitor given a channel id, making some naive assumptions
 #[macro_export]
-macro_rules! get_local_commitment_txn {
+macro_rules! get_monitor {
        ($node: expr, $channel_id: expr) => {
                {
-                       let monitors = $node.chain_monitor.chain_monitor.monitors.read().unwrap();
-                       let mut commitment_txn = None;
-                       for (funding_txo, monitor) in monitors.iter() {
-                               if funding_txo.to_channel_id() == $channel_id {
-                                       commitment_txn = Some(monitor.unsafe_get_latest_holder_commitment_txn(&$node.logger));
+                       use bitcoin::hashes::Hash;
+                       let mut monitor = None;
+                       // Assume funding vout is either 0 or 1 blindly
+                       for index in 0..2 {
+                               if let Ok(mon) = $node.chain_monitor.chain_monitor.get_monitor(
+                                       $crate::chain::transaction::OutPoint {
+                                               txid: bitcoin::Txid::from_slice(&$channel_id[..]).unwrap(), index
+                                       })
+                               {
+                                       monitor = Some(mon);
                                        break;
                                }
                        }
-                       commitment_txn.unwrap()
+                       monitor.unwrap()
+               }
+       }
+}
+
+/// Returns any local commitment transactions for the channel.
+#[macro_export]
+macro_rules! get_local_commitment_txn {
+       ($node: expr, $channel_id: expr) => {
+               {
+                       $crate::get_monitor!($node, $channel_id).unsafe_get_latest_holder_commitment_txn(&$node.logger)
                }
        }
 }
@@ -513,11 +527,12 @@ pub fn create_funding_transaction<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, expected_
 }
 
 pub fn create_chan_between_nodes_with_value_init<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>, channel_value: u64, push_msat: u64, a_flags: InitFeatures, b_flags: InitFeatures) -> Transaction {
-       node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42, None).unwrap();
+       let create_chan_id = node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42, None).unwrap();
        node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), a_flags, &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id()));
        node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), b_flags, &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id()));
 
        let (temporary_channel_id, tx, funding_output) = create_funding_transaction(node_a, channel_value, 42);
+       assert_eq!(temporary_channel_id, create_chan_id);
 
        node_a.node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
        check_added_monitors!(node_a, 0);