Correctly update the last block hash on disconnect
authorJeffrey Czyz <jkczyz@gmail.com>
Thu, 4 Mar 2021 02:33:54 +0000 (18:33 -0800)
committerJeffrey Czyz <jkczyz@gmail.com>
Fri, 5 Mar 2021 23:45:13 +0000 (15:45 -0800)
When a block is disconnected, the hash of the disconnected block was
used to update the last connected block. However, this amounts to a
no-op because these hashes should be equal. Successive disconnections
would update the hash but leave it one block off.

Normally, this not a problem because the last block_disconnected should
be followed by block_connected since the former is triggered by a chain
re-org. However, this assumes the user calls the API correctly and that
no failure occurs that would prevent block_connected from being called
(e.g., if fetching the connected block fails).

Instead, update the last block hash with the disconnected block's
previous block hash.

lightning/src/chain/channelmonitor.rs
lightning/src/ln/channelmanager.rs

index 8a8a5b96d66fbbf9c99105a1dc7c1fadd574ad38..2352661271f46d8f48bb35703b6e1579951381dd 100644 (file)
@@ -2090,8 +2090,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
                      F::Target: FeeEstimator,
                      L::Target: Logger,
        {
-               let block_hash = header.block_hash();
-               log_trace!(logger, "Block {} at height {} disconnected", block_hash, height);
+               log_trace!(logger, "Block {} at height {} disconnected", header.block_hash(), height);
 
                if let Some(_) = self.onchain_events_waiting_threshold_conf.remove(&(height + ANTI_REORG_DELAY - 1)) {
                        //We may discard:
@@ -2101,7 +2100,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
 
                self.onchain_tx_handler.block_disconnected(height, broadcaster, fee_estimator, logger);
 
-               self.last_block_hash = block_hash;
+               self.last_block_hash = header.prev_blockhash;
        }
 
        /// Filters a block's `txdata` for transactions spending watched outputs or for any child
index 450d014033a895656d39cec81f7daf0caeb142ab..6e46d79fb08a76624e57bb2b25bac77834941be3 100644 (file)
@@ -3377,7 +3377,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
                let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
 
                self.latest_block_height.fetch_sub(1, Ordering::AcqRel);
-               *self.last_block_hash.write().unwrap() = header.block_hash();
+               *self.last_block_hash.write().unwrap() = header.prev_blockhash;
 
                let mut failed_channels = Vec::new();
                {