Skip to content

Commit c88b707

Browse files
committed
Drop ChannelManager::block_disconnected() entirely
It is now entirely redundant with ChannelManager::update_best_block and is still accessible via `Listen::block_disconnected`.
1 parent a15c854 commit c88b707

File tree

3 files changed

+14
-50
lines changed

3 files changed

+14
-50
lines changed

fuzz/src/full_stack.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
2727
use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
2828

2929
use lightning::chain;
30+
use lightning::chain::Listen;
3031
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
3132
use lightning::chain::chainmonitor;
3233
use lightning::chain::transaction::OutPoint;
@@ -217,7 +218,7 @@ impl<'a> MoneyLossDetector<'a> {
217218
fn disconnect_block(&mut self) {
218219
if self.height > 0 && (self.max_height < 6 || self.height >= self.max_height - 6) {
219220
let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height - 1].0, merkle_root: Default::default(), time: self.header_hashes[self.height].1, bits: 42, nonce: 42 };
220-
self.manager.block_disconnected(&header);
221+
self.manager.block_disconnected(&header, self.height as u32);
221222
self.monitor.block_disconnected(&header, self.height as u32);
222223
self.height -= 1;
223224
let removal_height = self.height;

lightning/src/ln/channelmanager.rs

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3302,8 +3302,17 @@ where
33023302
self.update_best_block(&block.header, height);
33033303
}
33043304

3305-
fn block_disconnected(&self, header: &BlockHeader, _height: u32) {
3306-
ChannelManager::block_disconnected(self, header);
3305+
fn block_disconnected(&self, header: &BlockHeader, height: u32) {
3306+
assert_eq!(*self.last_block_hash.read().unwrap(), header.block_hash(),
3307+
"Blocks must be disconnected in chain-order - the disconnected header must be the last connected header");
3308+
3309+
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3310+
let new_height = self.latest_block_height.fetch_sub(1, Ordering::AcqRel) as u32 - 1;
3311+
assert_eq!(new_height, height - 1,
3312+
"Blocks must be disconnected in chain-order - the disconnected block must have the correct height");
3313+
*self.last_block_hash.write().unwrap() = header.prev_blockhash;
3314+
3315+
self.do_chain_event(new_height, |channel| channel.update_best_block(new_height, header.time));
33073316
}
33083317
}
33093318

@@ -3472,52 +3481,6 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34723481
}
34733482
}
34743483

3475-
/// Updates channel state based on a disconnected block.
3476-
///
3477-
/// If necessary, the channel may be force-closed without letting the counterparty participate
3478-
/// in the shutdown.
3479-
pub fn block_disconnected(&self, header: &BlockHeader) {
3480-
// Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
3481-
// during initialization prior to the chain_monitor being fully configured in some cases.
3482-
// See the docs for `ChannelManagerReadArgs` for more.
3483-
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3484-
3485-
assert_eq!(*self.last_block_hash.read().unwrap(), header.block_hash(),
3486-
"Blocks must be disconnected in chain-order - the disconnected header must be the last connected header");
3487-
let new_height = self.latest_block_height.fetch_sub(1, Ordering::AcqRel) as u32 - 1;
3488-
*self.last_block_hash.write().unwrap() = header.prev_blockhash;
3489-
3490-
let mut failed_channels = Vec::new();
3491-
{
3492-
let mut channel_lock = self.channel_state.lock().unwrap();
3493-
let channel_state = &mut *channel_lock;
3494-
let short_to_id = &mut channel_state.short_to_id;
3495-
let pending_msg_events = &mut channel_state.pending_msg_events;
3496-
channel_state.by_id.retain(|_, v| {
3497-
if let Err(err_msg) = v.update_best_block(new_height, header.time) {
3498-
if let Some(short_id) = v.get_short_channel_id() {
3499-
short_to_id.remove(&short_id);
3500-
}
3501-
failed_channels.push(v.force_shutdown(true));
3502-
if let Ok(update) = self.get_channel_update(&v) {
3503-
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
3504-
msg: update
3505-
});
3506-
}
3507-
pending_msg_events.push(events::MessageSendEvent::HandleError {
3508-
node_id: v.get_counterparty_node_id(),
3509-
action: msgs::ErrorAction::SendErrorMessage { msg: err_msg },
3510-
});
3511-
false
3512-
} else {
3513-
true
3514-
}
3515-
});
3516-
}
3517-
3518-
self.handle_init_event_channel_failures(failed_channels);
3519-
}
3520-
35213484
/// Blocks until ChannelManager needs to be persisted or a timeout is reached. It returns a bool
35223485
/// indicating whether persistence is necessary. Only one listener on
35233486
/// `await_persistable_update` or `await_persistable_update_timeout` is guaranteed to be woken

lightning/src/ln/functional_test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32)
112112
let orig_header = node.blocks.borrow_mut().pop().unwrap();
113113
assert!(orig_header.1 > 0); // Cannot disconnect genesis
114114
node.chain_monitor.chain_monitor.block_disconnected(&orig_header.0, orig_header.1);
115-
node.node.block_disconnected(&orig_header.0);
115+
node.node.block_disconnected(&orig_header.0, orig_header.1);
116116
}
117117
}
118118

0 commit comments

Comments
 (0)