]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Test register_output is called on dependent txn
authorJeffrey Czyz <jkczyz@gmail.com>
Sun, 21 Mar 2021 04:42:58 +0000 (00:42 -0400)
committerJeffrey Czyz <jkczyz@gmail.com>
Sat, 27 Mar 2021 22:21:05 +0000 (18:21 -0400)
chain::Filter::register_output may return an in-block dependent
transaction that spends the output. Test the scenario where the txdata
given to ChainMonitor::block_connected includes a commitment transaction
whose HTLC output is spent in the same block but not included in txdata.
Instead, it is returned by chain::Filter::register_output when given the
commitment transaction's HTLC output. This is a common scenario for
Electrum clients, which provided filtered txdata.

lightning/src/chain/chainmonitor.rs
lightning/src/ln/functional_test_utils.rs

index 65b5feb8ccedac81ce803d6c9f708dc3535b2714..0fd088019247e4e8e9adbea39adb99798e274dd4 100644 (file)
@@ -267,3 +267,56 @@ impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> even
                pending_events
        }
 }
+
+#[cfg(test)]
+mod tests {
+       use ::{check_added_monitors, get_local_commitment_txn};
+       use ln::features::InitFeatures;
+       use ln::functional_test_utils::*;
+       use util::events::EventsProvider;
+       use util::events::MessageSendEventsProvider;
+       use util::test_utils::{OnRegisterOutput, TxOutReference};
+
+       /// Tests that in-block dependent transactions are processed by `block_connected` when not
+       /// included in `txdata` but returned by [`chain::Filter::register_output`]. For instance,
+       /// a (non-anchor) commitment transaction's HTLC output may be spent in the same block as the
+       /// commitment transaction itself. An Electrum client may filter the commitment transaction but
+       /// needs to return the HTLC transaction so it can be processed.
+       #[test]
+       fn connect_block_checks_dependent_transactions() {
+               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 nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+               let channel = create_announced_chan_between_nodes(
+                       &nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
+
+               // Send a payment, saving nodes[0]'s revoked commitment and HTLC-Timeout transactions.
+               let (commitment_tx, htlc_tx) = {
+                       let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 5_000_000).0;
+                       let mut txn = get_local_commitment_txn!(nodes[0], channel.2);
+                       claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage, 5_000_000);
+
+                       assert_eq!(txn.len(), 2);
+                       (txn.remove(0), txn.remove(0))
+               };
+
+               // Set expectations on nodes[1]'s chain source to return dependent transactions.
+               let htlc_output = TxOutReference(commitment_tx.clone(), 0);
+               let to_local_output = TxOutReference(commitment_tx.clone(), 1);
+               let htlc_timeout_output = TxOutReference(htlc_tx.clone(), 0);
+               nodes[1].chain_source
+                       .expect(OnRegisterOutput { with: htlc_output, returns: Some((1, htlc_tx)) })
+                       .expect(OnRegisterOutput { with: to_local_output, returns: None })
+                       .expect(OnRegisterOutput { with: htlc_timeout_output, returns: None });
+
+               // Notify nodes[1] that nodes[0]'s revoked commitment transaction was mined. The chain
+               // source should return the dependent HTLC transaction when the HTLC output is registered.
+               mine_transaction(&nodes[1], &commitment_tx);
+
+               // Clean up so uninteresting assertions don't fail.
+               check_added_monitors!(nodes[1], 1);
+               nodes[1].node.get_and_clear_pending_msg_events();
+               nodes[1].node.get_and_clear_pending_events();
+       }
+}
index e7f61c6a3dfeefb23f845b7a47d6ab0efa5abf60..36ca7d64e5d4a2a16e3cb409214b446369ea1843 100644 (file)
@@ -341,7 +341,8 @@ macro_rules! get_feerate {
        }
 }
 
-#[cfg(test)]
+/// Returns any local commitment transactions for the channel.
+#[macro_export]
 macro_rules! get_local_commitment_txn {
        ($node: expr, $channel_id: expr) => {
                {