Clean up claimable_outpoints when pending_claim_requests is cleaned
[rust-lightning] / lightning / src / ln / channelmonitor.rs
index 0084cbb26e798fc830b74b6a6cdabd47daed9b77..4b2e0e65e11375b55d2eae068103a31e711f94fd 100644 (file)
@@ -2327,6 +2327,7 @@ impl ChannelMonitor {
        }
 
        fn block_connected(&mut self, txn_matched: &[&Transaction], height: u32, block_hash: &Sha256dHash, broadcaster: &BroadcasterInterface, fee_estimator: &FeeEstimator)-> (Vec<(Sha256dHash, Vec<TxOut>)>, Vec<SpendableOutputDescriptor>, Vec<(HTLCSource, Option<PaymentPreimage>, PaymentHash)>) {
+               log_trace!(self, "Block {} at height {} connected with {} txn matched", block_hash, height, txn_matched.len());
                let mut watch_outputs = Vec::new();
                let mut spendable_outputs = Vec::new();
                let mut htlc_updated = Vec::new();
@@ -2417,10 +2418,15 @@ impl ChannelMonitor {
                                                // before we could anyway), wait for ANTI_REORG_DELAY and clean the RBF
                                                // tracking map.
                                                if set_equality {
+                                                       let new_event = OnchainEvent::Claim { claim_request: ancestor_claimable_txid.0.clone() };
                                                        match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
-                                                               hash_map::Entry::Occupied(_) => {},
+                                                               hash_map::Entry::Occupied(mut entry) => {
+                                                                       if !entry.get().contains(&new_event) {
+                                                                               entry.get_mut().push(new_event);
+                                                                       }
+                                                               },
                                                                hash_map::Entry::Vacant(entry) => {
-                                                                       entry.insert(vec![OnchainEvent::Claim { claim_request: ancestor_claimable_txid.0.clone()}]);
+                                                                       entry.insert(vec![new_event]);
                                                                }
                                                        }
                                                } else { // If false, generate new claim request with update outpoint set
@@ -2439,10 +2445,15 @@ impl ChannelMonitor {
                                }
                        }
                        for (outpoint, input_material) in claimed_outputs_material.drain(..) {
+                               let new_event = OnchainEvent::ContentiousOutpoint { outpoint, input_material };
                                match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
-                                       hash_map::Entry::Occupied(_) => {},
+                                       hash_map::Entry::Occupied(mut entry) => {
+                                               if !entry.get().contains(&new_event) {
+                                                       entry.get_mut().push(new_event);
+                                               }
+                                       },
                                        hash_map::Entry::Vacant(entry) => {
-                                               entry.insert(vec![OnchainEvent::ContentiousOutpoint { outpoint, input_material }]);
+                                               entry.insert(vec![new_event]);
                                        }
                                }
                        }
@@ -2481,8 +2492,13 @@ impl ChannelMonitor {
                        for ev in events {
                                match ev {
                                        OnchainEvent::Claim { claim_request } => {
-                                               // We may remove a whole set of claim outpoints here, as these one may have been aggregated in a single tx and claimed so atomically
-                                               self.pending_claim_requests.remove(&claim_request);
+                                               // We may remove a whole set of claim outpoints here, as these one may have
+                                               // been aggregated in a single tx and claimed so atomically
+                                               if let Some(bump_material) = self.pending_claim_requests.remove(&claim_request) {
+                                                       for outpoint in bump_material.per_input_material.keys() {
+                                                               self.claimable_outpoints.remove(&outpoint);
+                                                       }
+                                               }
                                        },
                                        OnchainEvent::HTLCUpdate { htlc_update } => {
                                                log_trace!(self, "HTLC {} failure update has got enough confirmations to be passed upstream", log_bytes!((htlc_update.1).0));