- fn block_connected(&self, _header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) {
- let monitors = self.monitors.lock().unwrap();
- for monitor in monitors.values() {
- let txn_outputs = monitor.block_connected(txn_matched, height, &*self.broadcaster);
- for (ref txid, ref outputs) in txn_outputs {
- for (idx, output) in outputs.iter().enumerate() {
- self.chain_monitor.install_watch_outpoint((txid.clone(), idx as u32), &output.script_pubkey);
+ fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) {
+ let block_hash = header.bitcoin_hash();
+ let mut new_events: Vec<events::Event> = Vec::with_capacity(0);
+ let mut htlc_updated_infos = Vec::new();
+ {
+ let mut monitors = self.monitors.lock().unwrap();
+ for monitor in monitors.values_mut() {
+ let (txn_outputs, spendable_outputs, mut htlc_updated) = monitor.block_connected(txn_matched, height, &block_hash, &*self.broadcaster);
+ if spendable_outputs.len() > 0 {
+ new_events.push(events::Event::SpendableOutputs {
+ outputs: spendable_outputs,
+ });
+ }
+
+ for (ref txid, ref outputs) in txn_outputs {
+ for (idx, output) in outputs.iter().enumerate() {
+ self.chain_monitor.install_watch_outpoint((txid.clone(), idx as u32), &output.script_pubkey);
+ }
+ }
+ htlc_updated_infos.append(&mut htlc_updated);
+ }
+ }
+ {
+ // ChannelManager will just need to fetch pending_htlc_updated and pass state backward
+ let mut pending_htlc_updated = self.pending_htlc_updated.lock().unwrap();
+ for htlc in htlc_updated_infos.drain(..) {
+ match pending_htlc_updated.entry(htlc.2) {
+ hash_map::Entry::Occupied(mut e) => {
+ // In case of reorg we may have htlc outputs solved in a different way so
+ // we prefer to keep claims but don't store duplicate updates for a given
+ // (payment_hash, HTLCSource) pair.
+ // TODO: Note that we currently don't really use this as ChannelManager
+ // will fail/claim backwards after the first block. We really should delay
+ // a few blocks before failing backwards (but can claim backwards
+ // immediately) as long as we have a few blocks of headroom.
+ let mut existing_claim = false;
+ e.get_mut().retain(|htlc_data| {
+ if htlc.0 == htlc_data.0 {
+ if htlc_data.1.is_some() {
+ existing_claim = true;
+ true
+ } else { false }
+ } else { true }
+ });
+ if !existing_claim {
+ e.get_mut().push((htlc.0, htlc.1));
+ }
+ }
+ hash_map::Entry::Vacant(e) => {
+ e.insert(vec![(htlc.0, htlc.1)]);
+ }