Ignore HTLC txn we dont know how to claim instead of unwrap()ing
authorMatt Corallo <git@bluematt.me>
Fri, 14 Sep 2018 03:54:15 +0000 (23:54 -0400)
committerMatt Corallo <git@bluematt.me>
Fri, 14 Sep 2018 19:27:39 +0000 (15:27 -0400)
This fixes a crash introduced in 3e149b1fb6624eef99b055bde772842f36
and introduces a test which will tickle the bug.

src/ln/channelmanager.rs
src/ln/channelmonitor.rs

index a2b3b242b8aa1e47119afbed5dabeb37beb33f13..576c4f0fd3666d2956634a72e4ac0ac303cef1cb 100644 (file)
@@ -3380,6 +3380,48 @@ mod tests {
                }
        }
 
+       #[test]
+       fn test_htlc_ignore_latest_remote_commitment() {
+               // Test that HTLC transactions spending the latest remote commitment transaction are simply
+               // ignored if we cannot claim them. This originally tickled an invalid unwrap().
+               let nodes = create_network(2);
+               create_announced_chan_between_nodes(&nodes, 0, 1);
+
+               route_payment(&nodes[0], &[&nodes[1]], 10000000);
+               nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id);
+               {
+                       let events = nodes[0].node.get_and_clear_pending_events();
+                       assert_eq!(events.len(), 1);
+                       match events[0] {
+                               Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { contents: msgs::UnsignedChannelUpdate { flags, .. }, .. } } => {
+                                       assert_eq!(flags & 0b10, 0b10);
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+               }
+
+               let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
+               assert_eq!(node_txn.len(), 2);
+
+               let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+               nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0], &node_txn[1]], &[1; 2]);
+
+               {
+                       let events = nodes[1].node.get_and_clear_pending_events();
+                       assert_eq!(events.len(), 1);
+                       match events[0] {
+                               Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { contents: msgs::UnsignedChannelUpdate { flags, .. }, .. } } => {
+                                       assert_eq!(flags & 0b10, 0b10);
+                               },
+                               _ => panic!("Unexpected event"),
+                       }
+               }
+
+               // Duplicate the block_connected call since this may happen due to other listeners
+               // registering new transactions
+               nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0], &node_txn[1]], &[1; 2]);
+       }
+
        #[test]
        fn test_unconf_chan() {
                // After creating a chan between nodes, we disconnect all blocks previously seen to force a channel close on nodes[0] side
index 12c26322bf8557fa845d9aa3f482479846b527fe..55e534c826969d3b1af39c11fe32b3ff860987f2 100644 (file)
@@ -1222,7 +1222,7 @@ impl ChannelMonitor {
                        };
                }
 
-               let secret = self.get_secret(commitment_number).unwrap();
+               let secret = ignore_error!(self.get_secret(commitment_number));
                let per_commitment_key = ignore_error!(SecretKey::from_slice(&self.secp_ctx, &secret));
                let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
                let revocation_pubkey = match self.key_storage {
@@ -1269,7 +1269,6 @@ impl ChannelMonitor {
                                output: outputs,
                        };
 
-
                        let sighash_parts = bip143::SighashComponents::new(&spend_tx);
 
                        let sig = match self.key_storage {