X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fonchaintx.rs;h=8e531b501c3c0ede233154596e08d340a39aef35;hb=ecaeddca475da5fab427bed5a7bed64ef9f9d216;hp=83dc968c6ca549cdb8a4f49978445f583664490b;hpb=a89996564cdc9e8708b147b2a5e7b11b59adfc8e;p=rust-lightning diff --git a/lightning/src/ln/onchaintx.rs b/lightning/src/ln/onchaintx.rs index 83dc968c..8e531b50 100644 --- a/lightning/src/ln/onchaintx.rs +++ b/lightning/src/ln/onchaintx.rs @@ -39,11 +39,13 @@ use std::mem::replace; const MAX_ALLOC_SIZE: usize = 64*1024; -/// An entry for an [`OnchainEvent`], stating the block height when the event was observed. +/// An entry for an [`OnchainEvent`], stating the block height when the event was observed and the +/// transaction causing it. /// /// Used to determine when the on-chain event can be considered safe from a chain reorganization. #[derive(PartialEq)] struct OnchainEventEntry { + txid: Txid, height: u32, event: OnchainEvent, } @@ -54,7 +56,7 @@ impl OnchainEventEntry { } fn has_reached_confirmation_threshold(&self, height: u32) -> bool { - self.confirmation_threshold() == height + height >= self.confirmation_threshold() } } @@ -299,7 +301,7 @@ pub struct OnchainTxHandler { #[cfg(not(test))] claimable_outpoints: HashMap, - onchain_events_waiting_threshold_conf: Vec, + onchain_events_awaiting_threshold_conf: Vec, latest_height: u32, @@ -336,8 +338,9 @@ impl OnchainTxHandler { claim_and_height.1.write(writer)?; } - writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?; - for ref entry in self.onchain_events_waiting_threshold_conf.iter() { + writer.write_all(&byte_utils::be64_to_array(self.onchain_events_awaiting_threshold_conf.len() as u64))?; + for ref entry in self.onchain_events_awaiting_threshold_conf.iter() { + entry.txid.write(writer)?; writer.write_all(&byte_utils::be32_to_array(entry.height))?; match entry.event { OnchainEvent::Claim { ref claim_request } => { @@ -393,8 +396,9 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler { claimable_outpoints.insert(outpoint, (ancestor_claim_txid, height)); } let waiting_threshold_conf_len: u64 = Readable::read(reader)?; - let mut onchain_events_waiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128)); + let mut onchain_events_awaiting_threshold_conf = Vec::with_capacity(cmp::min(waiting_threshold_conf_len as usize, MAX_ALLOC_SIZE / 128)); for _ in 0..waiting_threshold_conf_len { + let txid = Readable::read(reader)?; let height = Readable::read(reader)?; let event = match ::read(reader)? { 0 => { @@ -413,7 +417,7 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler { } _ => return Err(DecodeError::InvalidValue), }; - onchain_events_waiting_threshold_conf.push(OnchainEventEntry { height, event }); + onchain_events_awaiting_threshold_conf.push(OnchainEventEntry { txid, height, event }); } let latest_height = Readable::read(reader)?; @@ -430,7 +434,7 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler { channel_transaction_parameters: channel_parameters, claimable_outpoints, pending_claim_requests, - onchain_events_waiting_threshold_conf, + onchain_events_awaiting_threshold_conf, latest_height, secp_ctx, }) @@ -449,7 +453,7 @@ impl OnchainTxHandler { channel_transaction_parameters: channel_parameters, pending_claim_requests: HashMap::new(), claimable_outpoints: HashMap::new(), - onchain_events_waiting_threshold_conf: Vec::new(), + onchain_events_awaiting_threshold_conf: Vec::new(), latest_height: 0, secp_ctx, @@ -768,11 +772,12 @@ impl OnchainTxHandler { macro_rules! clean_claim_request_after_safety_delay { () => { let entry = OnchainEventEntry { + txid: tx.txid(), height, event: OnchainEvent::Claim { claim_request: first_claim_txid_height.0.clone() } }; - if !self.onchain_events_waiting_threshold_conf.contains(&entry) { - self.onchain_events_waiting_threshold_conf.push(entry); + if !self.onchain_events_awaiting_threshold_conf.contains(&entry) { + self.onchain_events_awaiting_threshold_conf.push(entry); } } } @@ -807,19 +812,20 @@ impl OnchainTxHandler { } for (outpoint, input_material) in claimed_outputs_material.drain(..) { let entry = OnchainEventEntry { + txid: tx.txid(), height, event: OnchainEvent::ContentiousOutpoint { outpoint, input_material }, }; - if !self.onchain_events_waiting_threshold_conf.contains(&entry) { - self.onchain_events_waiting_threshold_conf.push(entry); + if !self.onchain_events_awaiting_threshold_conf.contains(&entry) { + self.onchain_events_awaiting_threshold_conf.push(entry); } } } // After security delay, either our claim tx got enough confs or outpoint is definetely out of reach - let onchain_events_waiting_threshold_conf = - self.onchain_events_waiting_threshold_conf.drain(..).collect::>(); - for entry in onchain_events_waiting_threshold_conf { + let onchain_events_awaiting_threshold_conf = + self.onchain_events_awaiting_threshold_conf.drain(..).collect::>(); + for entry in onchain_events_awaiting_threshold_conf { if entry.has_reached_confirmation_threshold(height) { match entry.event { OnchainEvent::Claim { claim_request } => { @@ -836,14 +842,14 @@ impl OnchainTxHandler { } } } else { - self.onchain_events_waiting_threshold_conf.push(entry); + self.onchain_events_awaiting_threshold_conf.push(entry); } } // Check if any pending claim request must be rescheduled for (first_claim_txid, ref claim_data) in self.pending_claim_requests.iter() { - if let Some(h) = claim_data.height_timer { - if h == height { + if let Some(height_timer) = claim_data.height_timer { + if height >= height_timer { bump_candidates.insert(*first_claim_txid, (*claim_data).clone()); } } @@ -863,16 +869,40 @@ impl OnchainTxHandler { } } + pub(crate) fn transaction_unconfirmed( + &mut self, + txid: &Txid, + broadcaster: B, + fee_estimator: F, + logger: L, + ) where + B::Target: BroadcasterInterface, + F::Target: FeeEstimator, + L::Target: Logger, + { + let mut height = None; + for entry in self.onchain_events_awaiting_threshold_conf.iter() { + if entry.txid == *txid { + height = Some(entry.height); + break; + } + } + + if let Some(height) = height { + self.block_disconnected(height, broadcaster, fee_estimator, logger); + } + } + pub(crate) fn block_disconnected(&mut self, height: u32, broadcaster: B, fee_estimator: F, logger: L) where B::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, { let mut bump_candidates = HashMap::new(); - let onchain_events_waiting_threshold_conf = - self.onchain_events_waiting_threshold_conf.drain(..).collect::>(); - for entry in onchain_events_waiting_threshold_conf { - if entry.height == height { + let onchain_events_awaiting_threshold_conf = + self.onchain_events_awaiting_threshold_conf.drain(..).collect::>(); + for entry in onchain_events_awaiting_threshold_conf { + if entry.height >= height { //- our claim tx on a commitment tx output //- resurect outpoint back in its claimable set and regenerate tx match entry.event { @@ -889,7 +919,7 @@ impl OnchainTxHandler { _ => {}, } } else { - self.onchain_events_waiting_threshold_conf.push(entry); + self.onchain_events_awaiting_threshold_conf.push(entry); } } for (_, claim_material) in bump_candidates.iter_mut() { @@ -906,7 +936,7 @@ impl OnchainTxHandler { // right now if one of the outpoint get disconnected, just erase whole pending claim request. let mut remove_request = Vec::new(); self.claimable_outpoints.retain(|_, ref v| - if v.1 == height { + if v.1 >= height { remove_request.push(v.0.clone()); false } else { true }); @@ -915,6 +945,16 @@ impl OnchainTxHandler { } } + pub(crate) fn get_relevant_txids(&self) -> Vec { + let mut txids: Vec = self.onchain_events_awaiting_threshold_conf + .iter() + .map(|entry| entry.txid) + .collect(); + txids.sort_unstable(); + txids.dedup(); + txids + } + pub(crate) fn provide_latest_holder_tx(&mut self, tx: HolderCommitmentTransaction) { self.prev_holder_commitment = Some(replace(&mut self.holder_commitment, tx)); self.holder_htlc_sigs = None;