Skip to content

Commit 25c0a7d

Browse files
committed
Move ChannelManager to Channel's new block data API
This also moves the scanning of the block for commitment transactions into channel, unifying the error path.
1 parent 5bb177b commit 25c0a7d

File tree

2 files changed

+26
-42
lines changed

2 files changed

+26
-42
lines changed

lightning/src/ln/channel.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,7 +3477,11 @@ impl<Signer: Sign> Channel<Signer> {
34773477
self.network_sync == UpdateStatus::DisabledMarked
34783478
}
34793479

3480-
pub fn transactions_confirmed(&mut self, block_hash: &BlockHash, height: u32, txdata: &TransactionData) -> Result<(), msgs::ErrorMessage> {
3480+
/// When a transaction is confirmed, we check whether it is or spends the funding transaction
3481+
/// In the first case, we store the confirmation height and calculating the short channel id.
3482+
/// In the second, we simply return an Err indicating we need to be force-closed now.
3483+
pub fn transactions_confirmed<L: Deref>(&mut self, block_hash: &BlockHash, height: u32, txdata: &TransactionData, logger: &L)
3484+
-> Result<(), msgs::ErrorMessage> where L::Target: Logger {
34813485
let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
34823486
if non_shutdown_state & !(ChannelState::TheirFundingLocked as u32) == ChannelState::FundingSent as u32 {
34833487
for &(index_in_block, tx) in txdata.iter() {
@@ -3523,6 +3527,15 @@ impl<Signer: Sign> Channel<Signer> {
35233527
((txo_idx as u64) << (0*8)));
35243528
}
35253529
}
3530+
for inp in tx.input.iter() {
3531+
if inp.previous_output == funding_txo.into_bitcoin_outpoint() {
3532+
log_trace!(logger, "Detected channel-closing tx {} spending {}:{}, closing channel {}", tx.txid(), inp.previous_output.txid, inp.previous_output.vout, log_bytes!(self.channel_id()));
3533+
return Err(msgs::ErrorMessage {
3534+
channel_id: self.channel_id(),
3535+
data: "Commitment transaction was confirmed on chain.".to_owned()
3536+
});
3537+
}
3538+
}
35263539
}
35273540
}
35283541
Ok(())
@@ -3612,25 +3625,6 @@ impl<Signer: Sign> Channel<Signer> {
36123625
Ok((None, timed_out_htlcs))
36133626
}
36143627

3615-
/// When we receive a new block, we (a) check whether the block contains the funding
3616-
/// transaction (which would start us counting blocks until we send the funding_signed), and
3617-
/// (b) check the height of the block against outbound holding cell HTLCs in case we need to
3618-
/// give up on them prematurely and time them out. Everything else (e.g. commitment
3619-
/// transaction broadcasts, channel closure detection, HTLC transaction broadcasting, etc) is
3620-
/// handled by the ChannelMonitor.
3621-
///
3622-
/// If we return Err, the channel may have been closed, at which point the standard
3623-
/// requirements apply - no calls may be made except those explicitly stated to be allowed
3624-
/// post-shutdown.
3625-
/// Only returns an ErrorAction of DisconnectPeer, if Err.
3626-
///
3627-
/// May return some HTLCs (and their payment_hash) which have timed out and should be failed
3628-
/// back.
3629-
pub fn block_connected(&mut self, header: &BlockHeader, txdata: &TransactionData, height: u32) -> Result<(Option<msgs::FundingLocked>, Vec<(HTLCSource, PaymentHash)>), msgs::ErrorMessage> {
3630-
self.transactions_confirmed(&header.block_hash(), height, txdata)?;
3631-
self.update_best_block(height, header.time)
3632-
}
3633-
36343628
/// Called by channelmanager based on chain blocks being disconnected.
36353629
/// Returns true if we need to close the channel now due to funding transaction
36363630
/// unconfirmation/reorg.

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,7 +3262,6 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32623262

32633263
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
32643264

3265-
32663265
// This assertion should be enforced in tests, however we have a number of tests that
32673266
// were written before this requirement and do not meet it.
32683267
#[cfg(not(test))]
@@ -3283,7 +3282,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32833282
let short_to_id = &mut channel_state.short_to_id;
32843283
let pending_msg_events = &mut channel_state.pending_msg_events;
32853284
channel_state.by_id.retain(|_, channel| {
3286-
let res = channel.block_connected(header, txdata, height);
3285+
let res = channel.transactions_confirmed(&block_hash, height, txdata, &self.logger)
3286+
.and_then(|_| channel.update_best_block(height, header.time));
32873287
if let Ok((chan_res, mut timed_out_pending_htlcs)) = res {
32883288
for (source, payment_hash) in timed_out_pending_htlcs.drain(..) {
32893289
let chan_update = self.get_channel_update(&channel).map(|u| u.encode_with_len()).unwrap(); // Cannot add/recv HTLCs before we have a short_id so unwrap is safe
@@ -3309,32 +3309,22 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33093309
short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
33103310
}
33113311
} else if let Err(e) = res {
3312+
if let Some(short_id) = channel.get_short_channel_id() {
3313+
short_to_id.remove(&short_id);
3314+
}
3315+
// It looks like our counterparty went on-chain. Close the channel.
3316+
failed_channels.push(channel.force_shutdown(true));
3317+
if let Ok(update) = self.get_channel_update(&channel) {
3318+
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
3319+
msg: update
3320+
});
3321+
}
33123322
pending_msg_events.push(events::MessageSendEvent::HandleError {
33133323
node_id: channel.get_counterparty_node_id(),
33143324
action: msgs::ErrorAction::SendErrorMessage { msg: e },
33153325
});
33163326
return false;
33173327
}
3318-
if let Some(funding_txo) = channel.get_funding_txo() {
3319-
for &(_, tx) in txdata.iter() {
3320-
for inp in tx.input.iter() {
3321-
if inp.previous_output == funding_txo.into_bitcoin_outpoint() {
3322-
log_trace!(self.logger, "Detected channel-closing tx {} spending {}:{}, closing channel {}", tx.txid(), inp.previous_output.txid, inp.previous_output.vout, log_bytes!(channel.channel_id()));
3323-
if let Some(short_id) = channel.get_short_channel_id() {
3324-
short_to_id.remove(&short_id);
3325-
}
3326-
// It looks like our counterparty went on-chain. Close the channel.
3327-
failed_channels.push(channel.force_shutdown(true));
3328-
if let Ok(update) = self.get_channel_update(&channel) {
3329-
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
3330-
msg: update
3331-
});
3332-
}
3333-
return false;
3334-
}
3335-
}
3336-
}
3337-
}
33383328
true
33393329
});
33403330

0 commit comments

Comments
 (0)