AsyncPaymentsMessageHandler trait for OnionMessenger
[rust-lightning] / lightning / src / ln / functional_test_utils.rs
index b9688d04348e22a6d111167e62ecdadfc390ed90..079f6345fc99bbc8a77a53f1a411db89f43e5982 100644 (file)
@@ -35,15 +35,17 @@ use crate::util::test_utils;
 use crate::util::test_utils::{panicking, TestChainMonitor, TestScorer, TestKeysInterface};
 use crate::util::ser::{ReadableArgs, Writeable};
 
+use bitcoin::amount::Amount;
 use bitcoin::blockdata::block::{Block, Header, Version};
 use bitcoin::blockdata::locktime::absolute::LockTime;
 use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut};
 use bitcoin::hash_types::{BlockHash, TxMerkleNode};
 use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::hashes::Hash as _;
-use bitcoin::network::constants::Network;
+use bitcoin::network::Network;
 use bitcoin::pow::CompactTarget;
 use bitcoin::secp256k1::{PublicKey, SecretKey};
+use bitcoin::transaction;
 
 use alloc::rc::Rc;
 use core::cell::RefCell;
@@ -95,7 +97,7 @@ pub fn mine_transaction_without_consistency_checks<'a, 'b, 'c, 'd>(node: &'a Nod
                txdata: Vec::new(),
        };
        for _ in 0..*node.network_chan_count.borrow() { // Make sure we don't end up with channels at the same short id by offsetting by chan_count
-               block.txdata.push(Transaction { version: 0, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() });
+               block.txdata.push(Transaction { version: transaction::Version(0), lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() });
        }
        block.txdata.push((*tx).clone());
        do_connect_block_without_consistency_checks(node, block, false);
@@ -113,7 +115,7 @@ pub fn confirm_transactions_at<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, txn:
        }
        let mut txdata = Vec::new();
        for _ in 0..*node.network_chan_count.borrow() { // Make sure we don't end up with channels at the same short id by offsetting by chan_count
-               txdata.push(Transaction { version: 0, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() });
+               txdata.push(Transaction { version: transaction::Version(0), lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() });
        }
        for tx in txn {
                txdata.push((*tx).clone());
@@ -419,6 +421,7 @@ type TestOnionMessenger<'chan_man, 'node_cfg, 'chan_mon_cfg> = OnionMessenger<
        &'node_cfg test_utils::TestMessageRouter<'chan_mon_cfg>,
        &'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
        IgnoringMessageHandler,
+       IgnoringMessageHandler,
 >;
 
 /// For use with [`OnionMessenger`] otherwise `test_restored_packages_retry` will fail. This is
@@ -1155,8 +1158,8 @@ fn internal_create_funding_transaction<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>,
                                Vec::new()
                        };
 
-                       let tx = Transaction { version: chan_id as i32, lock_time: LockTime::ZERO, input, output: vec![TxOut {
-                               value: *channel_value_satoshis, script_pubkey: output_script.clone(),
+                       let tx = Transaction { version: transaction::Version(chan_id as i32), lock_time: LockTime::ZERO, input, output: vec![TxOut {
+                               value: Amount::from_sat(*channel_value_satoshis), script_pubkey: output_script.clone(),
                        }]};
                        let funding_outpoint = OutPoint { txid: tx.txid(), index: 0 };
                        (*temporary_channel_id, tx, funding_outpoint)
@@ -1476,15 +1479,15 @@ pub fn update_nodes_with_chan_announce<'a, 'b, 'c, 'd>(nodes: &'a Vec<Node<'b, '
 
 pub fn do_check_spends<F: Fn(&bitcoin::blockdata::transaction::OutPoint) -> Option<TxOut>>(tx: &Transaction, get_output: F) {
        for outp in tx.output.iter() {
-               assert!(outp.value >= outp.script_pubkey.dust_value().to_sat(), "Spending tx output didn't meet dust limit");
+               assert!(outp.value >= outp.script_pubkey.dust_value(), "Spending tx output didn't meet dust limit");
        }
        let mut total_value_in = 0;
        for input in tx.input.iter() {
-               total_value_in += get_output(&input.previous_output).unwrap().value;
+               total_value_in += get_output(&input.previous_output).unwrap().value.to_sat();
        }
        let mut total_value_out = 0;
        for output in tx.output.iter() {
-               total_value_out += output.value;
+               total_value_out += output.value.to_sat();
        }
        let min_fee = (tx.weight().to_wu() as u64 + 3) / 4; // One sat per vbyte (ie per weight/4, rounded up)
        // Input amount - output amount = fee, so check that out + min_fee is smaller than input
@@ -1498,7 +1501,7 @@ macro_rules! check_spends {
                {
                        $(
                        for outp in $spends_txn.output.iter() {
-                               assert!(outp.value >= outp.script_pubkey.dust_value().to_sat(), "Input tx output didn't meet dust limit");
+                               assert!(outp.value >= outp.script_pubkey.dust_value(), "Input tx output didn't meet dust limit");
                        }
                        )*
                        let get_output = |out_point: &bitcoin::blockdata::transaction::OutPoint| {
@@ -2065,7 +2068,7 @@ macro_rules! get_payment_preimage_hash {
 /// Gets a route from the given sender to the node described in `payment_params`.
 pub fn get_route(send_node: &Node, route_params: &RouteParameters) -> Result<Route, msgs::LightningError> {
        let scorer = TestScorer::new();
-       let keys_manager = TestKeysInterface::new(&[0u8; 32], bitcoin::network::constants::Network::Testnet);
+       let keys_manager = TestKeysInterface::new(&[0u8; 32], Network::Testnet);
        let random_seed_bytes = keys_manager.get_secure_random_bytes();
        router::get_route(
                &send_node.node.get_our_node_id(), route_params, &send_node.network_graph.read_only(),
@@ -2077,7 +2080,7 @@ pub fn get_route(send_node: &Node, route_params: &RouteParameters) -> Result<Rou
 /// Like `get_route` above, but adds a random CLTV offset to the final hop.
 pub fn find_route(send_node: &Node, route_params: &RouteParameters) -> Result<Route, msgs::LightningError> {
        let scorer = TestScorer::new();
-       let keys_manager = TestKeysInterface::new(&[0u8; 32], bitcoin::network::constants::Network::Testnet);
+       let keys_manager = TestKeysInterface::new(&[0u8; 32], Network::Testnet);
        let random_seed_bytes = keys_manager.get_secure_random_bytes();
        router::find_route(
                &send_node.node.get_our_node_id(), route_params, &send_node.network_graph,
@@ -2453,18 +2456,11 @@ pub fn expect_payment_failed_conditions_event<'a, 'b, 'c, 'd, 'e>(
                        if let Some(chan_closed) = conditions.expected_blamed_chan_closed {
                                if let PathFailure::OnPath { network_update: Some(upd) } = failure {
                                        match upd {
-                                               NetworkUpdate::ChannelUpdateMessage { ref msg } if !chan_closed => {
-                                                       if let Some(scid) = conditions.expected_blamed_scid {
-                                                               assert_eq!(msg.contents.short_channel_id, scid);
-                                                       }
-                                                       const CHAN_DISABLED_FLAG: u8 = 2;
-                                                       assert_eq!(msg.contents.flags & CHAN_DISABLED_FLAG, 0);
-                                               },
-                                               NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } if chan_closed => {
+                                               NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
                                                        if let Some(scid) = conditions.expected_blamed_scid {
                                                                assert_eq!(*short_channel_id, scid);
                                                        }
-                                                       assert!(is_permanent);
+                                                       assert_eq!(*is_permanent, chan_closed);
                                                },
                                                _ => panic!("Unexpected update type"),
                                        }
@@ -3233,7 +3229,7 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
                let dedicated_entropy = DedicatedEntropy(RandomBytes::new([i as u8; 32]));
                let onion_messenger = OnionMessenger::new(
                        dedicated_entropy, cfgs[i].keys_manager, cfgs[i].logger, &chan_mgrs[i],
-                       &cfgs[i].message_router, &chan_mgrs[i], IgnoringMessageHandler {},
+                       &cfgs[i].message_router, &chan_mgrs[i], IgnoringMessageHandler {}, IgnoringMessageHandler {},
                );
                let gossip_sync = P2PGossipSync::new(cfgs[i].network_graph.as_ref(), None, cfgs[i].logger);
                let wallet_source = Arc::new(test_utils::TestWalletSource::new(SecretKey::from_slice(&[i as u8 + 1; 32]).unwrap()));
@@ -3257,30 +3253,34 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
 
        for i in 0..node_count {
                for j in (i+1)..node_count {
-                       let node_id_i = nodes[i].node.get_our_node_id();
-                       let node_id_j = nodes[j].node.get_our_node_id();
-
-                       let init_i = msgs::Init {
-                               features: nodes[i].init_features(&node_id_j),
-                               networks: None,
-                               remote_network_address: None,
-                       };
-                       let init_j = msgs::Init {
-                               features: nodes[j].init_features(&node_id_i),
-                               networks: None,
-                               remote_network_address: None,
-                       };
-
-                       nodes[i].node.peer_connected(&node_id_j, &init_j, true).unwrap();
-                       nodes[j].node.peer_connected(&node_id_i, &init_i, false).unwrap();
-                       nodes[i].onion_messenger.peer_connected(&node_id_j, &init_j, true).unwrap();
-                       nodes[j].onion_messenger.peer_connected(&node_id_i, &init_i, false).unwrap();
+                       connect_nodes(&nodes[i], &nodes[j]);
                }
        }
 
        nodes
 }
 
+fn connect_nodes<'a, 'b: 'a, 'c: 'b>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
+       let node_id_a = node_a.node.get_our_node_id();
+       let node_id_b = node_b.node.get_our_node_id();
+
+       let init_a = msgs::Init {
+               features: node_a.init_features(&node_id_b),
+               networks: None,
+               remote_network_address: None,
+       };
+       let init_b = msgs::Init {
+               features: node_b.init_features(&node_id_a),
+               networks: None,
+               remote_network_address: None,
+       };
+
+       node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
+       node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
+       node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
+       node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
+}
+
 pub fn connect_dummy_node<'a, 'b: 'a, 'c: 'b>(node: &Node<'a, 'b, 'c>) {
        let node_id_dummy = PublicKey::from_slice(&[2; 33]).unwrap();
 
@@ -3641,13 +3641,8 @@ pub fn reconnect_nodes<'a, 'b, 'c, 'd>(args: ReconnectArgs<'a, 'b, 'c, 'd>) {
                pending_cell_htlc_claims, pending_cell_htlc_fails, pending_raa,
                pending_responding_commitment_signed, pending_responding_commitment_signed_dup_monitor,
        } = args;
-       node_a.node.peer_connected(&node_b.node.get_our_node_id(), &msgs::Init {
-               features: node_b.node.init_features(), networks: None, remote_network_address: None
-       }, true).unwrap();
+       connect_nodes(node_a, node_b);
        let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
-       node_b.node.peer_connected(&node_a.node.get_our_node_id(), &msgs::Init {
-               features: node_a.node.init_features(), networks: None, remote_network_address: None
-       }, false).unwrap();
        let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
 
        if send_channel_ready.0 {
@@ -3861,7 +3856,7 @@ pub fn create_batch_channel_funding<'a, 'b, 'c>(
                                assert_eq!(channel_value_satoshis, event_channel_value_satoshis);
                                assert_eq!(user_channel_id, event_user_channel_id);
                                tx_outs.push(TxOut {
-                                       value: *channel_value_satoshis, script_pubkey: output_script.clone(),
+                                       value: Amount::from_sat(*channel_value_satoshis), script_pubkey: output_script.clone(),
                                });
                        },
                        _ => panic!("Unexpected event"),
@@ -3871,7 +3866,7 @@ pub fn create_batch_channel_funding<'a, 'b, 'c>(
 
        // Compose the batch funding transaction and give it to the ChannelManager.
        let tx = Transaction {
-               version: 2,
+               version: transaction::Version::TWO,
                lock_time: LockTime::ZERO,
                input: Vec::new(),
                output: tx_outs,