Put test utilities behind a feature flag.
[rust-lightning] / lightning / src / ln / functional_test_utils.rs
index 366627b6595a815ce518c230fe0e2253ecf502dc..871854ce63e06ae405c0c859f286214c8adde245 100644 (file)
@@ -263,6 +263,7 @@ macro_rules! get_event_msg {
        }
 }
 
+#[cfg(test)]
 macro_rules! get_htlc_update_msgs {
        ($node: expr, $node_id: expr) => {
                {
@@ -279,6 +280,7 @@ macro_rules! get_htlc_update_msgs {
        }
 }
 
+#[cfg(test)]
 macro_rules! get_feerate {
        ($node: expr, $channel_id: expr) => {
                {
@@ -289,6 +291,7 @@ macro_rules! get_feerate {
        }
 }
 
+#[cfg(test)]
 macro_rules! get_local_commitment_txn {
        ($node: expr, $channel_id: expr) => {
                {
@@ -327,6 +330,8 @@ macro_rules! unwrap_send_err {
        }
 }
 
+/// Check whether N channel monitor(s) have been added.
+#[macro_export]
 macro_rules! check_added_monitors {
        ($node: expr, $count: expr) => {
                {
@@ -505,14 +510,26 @@ pub fn create_announced_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(nodes: &'a
 macro_rules! check_spends {
        ($tx: expr, $($spends_txn: expr),*) => {
                {
-                       $tx.verify(|out_point| {
+                       let get_output = |out_point: &bitcoin::blockdata::transaction::OutPoint| {
                                $(
                                        if out_point.txid == $spends_txn.txid() {
                                                return $spends_txn.output.get(out_point.vout as usize).cloned()
                                        }
                                )*
                                None
-                       }).unwrap();
+                       };
+                       let mut total_value_in = 0;
+                       for input in $tx.input.iter() {
+                               total_value_in += get_output(&input.previous_output).unwrap().value;
+                       }
+                       let mut total_value_out = 0;
+                       for output in $tx.output.iter() {
+                               total_value_out += output.value;
+                       }
+                       let min_fee = ($tx.get_weight() 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
+                       assert!(total_value_out + min_fee <= total_value_in);
+                       $tx.verify(get_output).unwrap();
                }
        }
 }
@@ -541,6 +558,9 @@ macro_rules! get_closing_signed_broadcast {
        }
 }
 
+/// Check that a channel's closing channel update has been broadcasted, and optionally
+/// check whether an error message event has occurred.
+#[macro_export]
 macro_rules! check_closed_broadcast {
        ($node: expr, $with_error_msg: expr) => {{
                let events = $node.node.get_and_clear_pending_msg_events();
@@ -767,6 +787,7 @@ macro_rules! expect_pending_htlcs_forwardable {
        }}
 }
 
+#[cfg(test)]
 macro_rules! expect_payment_received {
        ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
                let events = $node.node.get_and_clear_pending_events();
@@ -795,6 +816,7 @@ macro_rules! expect_payment_sent {
        }
 }
 
+#[cfg(test)]
 macro_rules! expect_payment_failed {
        ($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr $(, $expected_error_code: expr, $expected_error_data: expr)*) => {
                let events = $node.node.get_and_clear_pending_events();
@@ -1272,6 +1294,7 @@ pub fn get_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec<Node<'a, 'b,
        }
 }
 
+#[cfg(test)]
 macro_rules! get_channel_value_stat {
        ($node: expr, $channel_id: expr) => {{
                let chan_lock = $node.node.channel_state.lock().unwrap();