From f69d9d7f3098011120b87e2ec45f0c4b675fbe86 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Fri, 26 Jun 2020 10:43:24 -0700 Subject: [PATCH] Align ChannelMonitor interface with ChainListener ChannelMonitor has block_connected and block_disconnected methods called by . Use similar parameters in ChannelMonitor such that transformations are not needed and the interface is more closely aligned with ChainListener. --- lightning/src/ln/channelmonitor.rs | 24 +++++++++++++----------- lightning/src/ln/onchaintx.rs | 4 ++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs index cca3c99f0..9fb240279 100644 --- a/lightning/src/ln/channelmonitor.rs +++ b/lightning/src/ln/channelmonitor.rs @@ -215,13 +215,12 @@ impl = matched_indexes.iter().map(|index| txdata[*index].1).collect(); + let matched_txn: Vec<_> = matched_indexes.iter().map(|index| txdata[*index]).collect(); let last_seen = self.chain_monitor.reentered(); - let block_hash = header.block_hash(); { let mut monitors = self.monitors.lock().unwrap(); for monitor in monitors.values_mut() { - let txn_outputs = monitor.block_connected(&matched_txn, height, &block_hash, &*self.broadcaster, &*self.fee_estimator, &*self.logger); + let txn_outputs = monitor.block_connected(header, &matched_txn, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger); for (ref txid, ref outputs) in txn_outputs { for (idx, output) in outputs.iter().enumerate() { @@ -235,10 +234,9 @@ impl ChannelMonitor { /// Eventually this should be pub and, roughly, implement ChainListener, however this requires /// &mut self, as well as returns new spendable outputs and outpoints to watch for spending of /// on-chain. - fn block_connected(&mut self, txn_matched: &[&Transaction], height: u32, block_hash: &BlockHash, broadcaster: B, fee_estimator: F, logger: L)-> Vec<(Txid, Vec)> + fn block_connected(&mut self, header: &BlockHeader, txn_matched: &[(usize, &Transaction)], height: u32, broadcaster: B, fee_estimator: F, logger: L)-> Vec<(Txid, Vec)> where B::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, { - for tx in txn_matched { + for &(_, tx) in txn_matched { let mut output_val = 0; for out in tx.output.iter() { if out.value > 21_000_000_0000_0000 { panic!("Value-overflowing transaction provided to block connected"); } @@ -1906,10 +1904,12 @@ impl ChannelMonitor { } } + let block_hash = header.block_hash(); log_trace!(logger, "Block {} at height {} connected with {} txn matched", block_hash, height, txn_matched.len()); + let mut watch_outputs = Vec::new(); let mut claimable_outpoints = Vec::new(); - for tx in txn_matched { + 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, @@ -1986,7 +1986,7 @@ impl ChannelMonitor { self.onchain_tx_handler.block_connected(txn_matched, claimable_outpoints, height, &*broadcaster, &*fee_estimator, &*logger); - self.last_block_hash = block_hash.clone(); + self.last_block_hash = block_hash; for &(ref txid, ref output_scripts) in watch_outputs.iter() { self.outputs_to_watch.insert(txid.clone(), output_scripts.iter().map(|o| o.script_pubkey.clone()).collect()); } @@ -1994,12 +1994,14 @@ impl ChannelMonitor { watch_outputs } - fn block_disconnected(&mut self, height: u32, block_hash: &BlockHash, broadcaster: B, fee_estimator: F, logger: L) + fn block_disconnected(&mut self, header: &BlockHeader, height: u32, broadcaster: B, fee_estimator: F, logger: L) where B::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, { + let block_hash = header.block_hash(); log_trace!(logger, "Block {} at height {} disconnected", block_hash, height); + 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 @@ -2008,7 +2010,7 @@ impl ChannelMonitor { self.onchain_tx_handler.block_disconnected(height, broadcaster, fee_estimator, logger); - self.last_block_hash = block_hash.clone(); + self.last_block_hash = block_hash; } fn would_broadcast_at_height(&self, height: u32, logger: &L) -> bool where L::Target: Logger { diff --git a/lightning/src/ln/onchaintx.rs b/lightning/src/ln/onchaintx.rs index cad5cc1bb..8d5ed6cbc 100644 --- a/lightning/src/ln/onchaintx.rs +++ b/lightning/src/ln/onchaintx.rs @@ -657,7 +657,7 @@ impl OnchainTxHandler { None } - pub(super) fn block_connected(&mut self, txn_matched: &[&Transaction], claimable_outpoints: Vec, height: u32, broadcaster: B, fee_estimator: F, logger: L) + pub(super) fn block_connected(&mut self, txn_matched: &[(usize, &Transaction)], claimable_outpoints: Vec, height: u32, broadcaster: B, fee_estimator: F, logger: L) where B::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, @@ -706,7 +706,7 @@ impl OnchainTxHandler { } let mut bump_candidates = HashMap::new(); - for tx in txn_matched { + for &(_, tx) in txn_matched { // Scan all input to verify is one of the outpoint spent is of interest for us let mut claimed_outputs_material = Vec::new(); for inp in &tx.input { -- 2.39.5