+ 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)>) {
+ let mut watch_outputs = Vec::new();
+ let mut spendable_outputs = Vec::new();
+ let mut htlc_updated = Vec::new();
+ for tx in txn_matched {
+ if tx.input.len() == 1 {
+ // Assuming our keys were not leaked (in which case we're screwed no matter what),
+ // commitment transactions and HTLC transactions will all only ever have one input,
+ // which is an easy way to filter out any potential non-matching txn for lazy
+ // filters.
+ let prevout = &tx.input[0].previous_output;
+ let mut txn: Vec<Transaction> = Vec::new();
+ let funding_txo = match self.key_storage {
+ Storage::Local { ref funding_info, .. } => {
+ funding_info.clone()
+ }
+ Storage::Watchtower { .. } => {
+ unimplemented!();
+ }
+ };
+ if funding_txo.is_none() || (prevout.txid == funding_txo.as_ref().unwrap().0.txid && prevout.vout == funding_txo.as_ref().unwrap().0.index as u32) {
+ let (remote_txn, new_outputs, mut spendable_output) = self.check_spend_remote_transaction(tx, height, fee_estimator);
+ txn = remote_txn;
+ spendable_outputs.append(&mut spendable_output);
+ if !new_outputs.1.is_empty() {
+ watch_outputs.push(new_outputs);
+ }
+ if txn.is_empty() {
+ let (local_txn, mut spendable_output, new_outputs) = self.check_spend_local_transaction(tx, height);
+ spendable_outputs.append(&mut spendable_output);
+ txn = local_txn;
+ if !new_outputs.1.is_empty() {
+ watch_outputs.push(new_outputs);
+ }
+ }
+ if !funding_txo.is_none() && txn.is_empty() {
+ if let Some(spendable_output) = self.check_spend_closing_transaction(tx) {
+ spendable_outputs.push(spendable_output);
+ }
+ }
+ } else {
+ if let Some(&(commitment_number, _)) = self.remote_commitment_txn_on_chain.get(&prevout.txid) {
+ let (tx, spendable_output) = self.check_spend_remote_htlc(tx, commitment_number, height, fee_estimator);
+ if let Some(tx) = tx {
+ txn.push(tx);
+ }
+ if let Some(spendable_output) = spendable_output {
+ spendable_outputs.push(spendable_output);
+ }
+ }
+ }
+ for tx in txn.iter() {
+ broadcaster.broadcast_transaction(tx);
+ }
+ }
+ // While all commitment/HTLC-Success/HTLC-Timeout transactions have one input, HTLCs
+ // can also be resolved in a few other ways which can have more than one output. Thus,
+ // we call is_resolving_htlc_output here outside of the tx.input.len() == 1 check.
+ let mut updated = self.is_resolving_htlc_output(tx, height);
+ if updated.len() > 0 {
+ htlc_updated.append(&mut updated);
+ }
+ for inp in &tx.input {
+ if self.our_claim_txn_waiting_first_conf.contains_key(&inp.previous_output) {
+ match self.onchain_events_waiting_threshold_conf.entry(height + ANTI_REORG_DELAY - 1) {
+ hash_map::Entry::Occupied(mut entry) => {
+ let e = entry.get_mut();
+ e.retain(|ref event| {
+ match **event {
+ OnchainEvent::Claim { outpoint } => {
+ return outpoint != inp.previous_output
+ },
+ _ => return true
+ }
+ });
+ e.push(OnchainEvent::Claim { outpoint: inp.previous_output.clone()});
+ }
+ hash_map::Entry::Vacant(entry) => {
+ entry.insert(vec![OnchainEvent::Claim { outpoint: inp.previous_output.clone()}]);
+ }
+ }
+ }
+ }
+ }
+ let mut pending_claims = Vec::new();
+ if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
+ if self.would_broadcast_at_height(height) {
+ broadcaster.broadcast_transaction(&cur_local_tx.tx);
+ match self.key_storage {
+ Storage::Local { ref delayed_payment_base_key, ref latest_per_commitment_point, .. } => {
+ let (txs, mut spendable_output, new_outputs, mut pending_txn) = self.broadcast_by_local_state(&cur_local_tx, latest_per_commitment_point, &Some(*delayed_payment_base_key), height);
+ spendable_outputs.append(&mut spendable_output);
+ pending_claims.append(&mut pending_txn);
+ if !new_outputs.is_empty() {
+ watch_outputs.push((cur_local_tx.txid.clone(), new_outputs));
+ }
+ for tx in txs {
+ broadcaster.broadcast_transaction(&tx);
+ }
+ },
+ Storage::Watchtower { .. } => {
+ let (txs, mut spendable_output, new_outputs, mut pending_txn) = self.broadcast_by_local_state(&cur_local_tx, &None, &None, height);
+ spendable_outputs.append(&mut spendable_output);
+ pending_claims.append(&mut pending_txn);
+ if !new_outputs.is_empty() {
+ watch_outputs.push((cur_local_tx.txid.clone(), new_outputs));
+ }
+ for tx in txs {
+ broadcaster.broadcast_transaction(&tx);
+ }
+ }
+ }
+ }
+ }
+ for claim in pending_claims {
+ match self.our_claim_txn_waiting_first_conf.entry(claim.0) {
+ hash_map::Entry::Occupied(_) => {},
+ hash_map::Entry::Vacant(entry) => { entry.insert(claim.1); }
+ }
+ }
+ if let Some(events) = self.onchain_events_waiting_threshold_conf.remove(&height) {
+ for ev in events {
+ match ev {
+ OnchainEvent::Claim { outpoint } => {
+ self.our_claim_txn_waiting_first_conf.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));
+ htlc_updated.push((htlc_update.0, None, htlc_update.1));
+ },
+ }
+ }
+ }
+ //TODO: iter on buffered TxMaterial in our_claim_txn_waiting_first_conf, if block timer is expired generate a bumped claim tx (RBF or CPFP accordingly)
+ self.last_block_hash = block_hash.clone();
+ (watch_outputs, spendable_outputs, htlc_updated)
+ }
+
+ fn block_disconnected(&mut self, height: u32, block_hash: &Sha256dHash) {
+ if let Some(_) = self.onchain_events_waiting_threshold_conf.remove(&(height + ANTI_REORG_DELAY - 1)) {
+ //We may discard:
+ //- htlc update there as failure-trigger tx (revoked commitment tx, non-revoked commitment tx, HTLC-timeout tx) has been disconnected
+ //- our claim tx on a commitment tx output
+ }
+ self.our_claim_txn_waiting_first_conf.retain(|_, ref mut v| if v.3 == height { false } else { true });
+ self.last_block_hash = block_hash.clone();
+ }