Add new block `ConnectionStyle`s for `transaction_unconfirmed`
authorMatt Corallo <git@bluematt.me>
Sun, 15 May 2022 19:03:45 +0000 (19:03 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 17 May 2022 20:19:38 +0000 (20:19 +0000)
Previously `transaction_unconfirmed` was never called in tests!

lightning/src/ln/functional_test_utils.rs
lightning/src/ln/reorg_tests.rs

index cb8a02d282cc39d89ed52a6c2b161cbeb9f59bfa..b3969fc1b65d9ca0ed5117f75d1ca66cea857707 100644 (file)
@@ -79,26 +79,33 @@ pub fn confirm_transaction_at<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, tx: &T
 /// The possible ways we may notify a ChannelManager of a new block
 #[derive(Clone, Copy, PartialEq)]
 pub enum ConnectStyle {
-       /// Calls best_block_updated first, detecting transactions in the block only after receiving the
-       /// header and height information.
+       /// Calls `best_block_updated` first, detecting transactions in the block only after receiving
+       /// the header and height information.
        BestBlockFirst,
-       /// The same as BestBlockFirst, however when we have multiple blocks to connect, we only
-       /// make a single best_block_updated call.
+       /// The same as `BestBlockFirst`, however when we have multiple blocks to connect, we only
+       /// make a single `best_block_updated` call.
        BestBlockFirstSkippingBlocks,
-       /// Calls transactions_confirmed first, detecting transactions in the block before updating the
-       /// header and height information.
+       /// The same as `BestBlockFirst` when connecting blocks. During disconnection only
+       /// `transaction_unconfirmed` is called.
+       BestBlockFirstReorgsOnlyTip,
+       /// Calls `transactions_confirmed` first, detecting transactions in the block before updating
+       /// the header and height information.
        TransactionsFirst,
-       /// The same as TransactionsFirst, however when we have multiple blocks to connect, we only
-       /// make a single best_block_updated call.
+       /// The same as `TransactionsFirst`, however when we have multiple blocks to connect, we only
+       /// make a single `best_block_updated` call.
        TransactionsFirstSkippingBlocks,
-       /// Provides the full block via the chain::Listen interface. In the current code this is
-       /// equivalent to TransactionsFirst with some additional assertions.
+       /// The same as `TransactionsFirst` when connecting blocks. During disconnection only
+       /// `transaction_unconfirmed` is called.
+       TransactionsFirstReorgsOnlyTip,
+       /// Provides the full block via the `chain::Listen` interface. In the current code this is
+       /// equivalent to `TransactionsFirst` with some additional assertions.
        FullBlockViaListen,
 }
 
 pub fn connect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, depth: u32) -> BlockHash {
        let skip_intermediaries = match *node.connect_style.borrow() {
-               ConnectStyle::BestBlockFirstSkippingBlocks|ConnectStyle::TransactionsFirstSkippingBlocks => true,
+               ConnectStyle::BestBlockFirstSkippingBlocks|ConnectStyle::TransactionsFirstSkippingBlocks|
+                       ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::TransactionsFirstReorgsOnlyTip => true,
                _ => false,
        };
 
@@ -138,14 +145,14 @@ fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, sk
        if !skip_intermediaries {
                let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
                match *node.connect_style.borrow() {
-                       ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstSkippingBlocks => {
+                       ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstSkippingBlocks|ConnectStyle::BestBlockFirstReorgsOnlyTip => {
                                node.chain_monitor.chain_monitor.best_block_updated(&block.header, height);
                                call_claimable_balances(node);
                                node.chain_monitor.chain_monitor.transactions_confirmed(&block.header, &txdata, height);
                                node.node.best_block_updated(&block.header, height);
                                node.node.transactions_confirmed(&block.header, &txdata, height);
                        },
-                       ConnectStyle::TransactionsFirst|ConnectStyle::TransactionsFirstSkippingBlocks => {
+                       ConnectStyle::TransactionsFirst|ConnectStyle::TransactionsFirstSkippingBlocks|ConnectStyle::TransactionsFirstReorgsOnlyTip => {
                                node.chain_monitor.chain_monitor.transactions_confirmed(&block.header, &txdata, height);
                                call_claimable_balances(node);
                                node.chain_monitor.chain_monitor.best_block_updated(&block.header, height);
@@ -181,6 +188,12 @@ pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32)
                                        node.node.best_block_updated(&prev.0.header, prev.1);
                                }
                        },
+                       ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::TransactionsFirstReorgsOnlyTip => {
+                               for tx in orig.0.txdata {
+                                       node.chain_monitor.chain_monitor.transaction_unconfirmed(&tx.txid());
+                                       node.node.transaction_unconfirmed(&tx.txid());
+                               }
+                       },
                        _ => {
                                node.chain_monitor.chain_monitor.best_block_updated(&prev.0.header, prev.1);
                                node.node.best_block_updated(&prev.0.header, prev.1);
index 96fda526d68d2d46369b9c9ec831d96551e86112..616feb1eea1f6a14dddc7f876246976f49eaea01 100644 (file)
@@ -314,6 +314,11 @@ fn test_unconf_chan() {
        do_test_unconf_chan(false, true, false, ConnectStyle::BestBlockFirstSkippingBlocks);
        do_test_unconf_chan(true, false, false, ConnectStyle::BestBlockFirstSkippingBlocks);
        do_test_unconf_chan(false, false, false, ConnectStyle::BestBlockFirstSkippingBlocks);
+
+       do_test_unconf_chan(true, true, false, ConnectStyle::BestBlockFirstReorgsOnlyTip);
+       do_test_unconf_chan(false, true, false, ConnectStyle::BestBlockFirstReorgsOnlyTip);
+       do_test_unconf_chan(true, false, false, ConnectStyle::BestBlockFirstReorgsOnlyTip);
+       do_test_unconf_chan(false, false, false, ConnectStyle::BestBlockFirstReorgsOnlyTip);
 }
 
 #[test]
@@ -331,6 +336,11 @@ fn test_unconf_chan_via_funding_unconfirmed() {
        do_test_unconf_chan(true, false, true, ConnectStyle::BestBlockFirstSkippingBlocks);
        do_test_unconf_chan(false, false, true, ConnectStyle::BestBlockFirstSkippingBlocks);
 
+       do_test_unconf_chan(true, true, true, ConnectStyle::BestBlockFirstReorgsOnlyTip);
+       do_test_unconf_chan(false, true, true, ConnectStyle::BestBlockFirstReorgsOnlyTip);
+       do_test_unconf_chan(true, false, true, ConnectStyle::BestBlockFirstReorgsOnlyTip);
+       do_test_unconf_chan(false, false, true, ConnectStyle::BestBlockFirstReorgsOnlyTip);
+
        do_test_unconf_chan(true, true, true, ConnectStyle::FullBlockViaListen);
        do_test_unconf_chan(false, true, true, ConnectStyle::FullBlockViaListen);
        do_test_unconf_chan(true, false, true, ConnectStyle::FullBlockViaListen);
@@ -539,7 +549,9 @@ fn do_test_to_remote_after_local_detection(style: ConnectStyle) {
 fn test_to_remote_after_local_detection() {
        do_test_to_remote_after_local_detection(ConnectStyle::BestBlockFirst);
        do_test_to_remote_after_local_detection(ConnectStyle::BestBlockFirstSkippingBlocks);
+       do_test_to_remote_after_local_detection(ConnectStyle::BestBlockFirstReorgsOnlyTip);
        do_test_to_remote_after_local_detection(ConnectStyle::TransactionsFirst);
        do_test_to_remote_after_local_detection(ConnectStyle::TransactionsFirstSkippingBlocks);
+       do_test_to_remote_after_local_detection(ConnectStyle::TransactionsFirstReorgsOnlyTip);
        do_test_to_remote_after_local_detection(ConnectStyle::FullBlockViaListen);
 }