Skip to content

Commit cbe8421

Browse files
committed
Split commitment_signed handling by check-accept
When handling commitment_signed messages, a number of checks are performed before a ChannelMonitorUpdate is created and returned. Once splicing is added, these checks need to be performed on the primary FundingScope and any pending scopes that resulted from splicing or RBF. This commit splits the handling into a check and accept methods, taking &self and &mut self, respectively. This ensures that the ChannelContext is not modified between checks. Once all funding scopes have been checked successfully, the accept portion of the code can then execute.
1 parent 5bc9ffa commit cbe8421

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
531531

532532
#[derive(Clone, Debug, PartialEq, Eq)]
533533
pub(crate) enum ChannelMonitorUpdateStep {
534+
// Update LatestHolderCommitmentTXInfo in channel.rs if adding new fields to this variant.
534535
LatestHolderCommitmentTXInfo {
535536
commitment_tx: HolderCommitmentTransaction,
536537
/// Note that LDK after 0.0.115 supports this only containing dust HTLCs (implying the

lightning/src/ln/channel.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4707,6 +4707,14 @@ struct CommitmentTxInfoCached {
47074707
feerate: u32,
47084708
}
47094709

4710+
/// Partial data from ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo used to simplify the
4711+
/// return type of `FundedChannel::validate_commitment_signed`.
4712+
struct LatestHolderCommitmentTXInfo {
4713+
pub commitment_tx: HolderCommitmentTransaction,
4714+
pub htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
4715+
pub nondust_htlc_sources: Vec<HTLCSource>,
4716+
}
4717+
47104718
/// Contents of a wire message that fails an HTLC backwards. Useful for [`FundedChannel::fail_htlc`] to
47114719
/// fail with either [`msgs::UpdateFailMalformedHTLC`] or [`msgs::UpdateFailHTLC`] as needed.
47124720
trait FailHTLCContents {
@@ -5495,22 +5503,9 @@ impl<SP: Deref> FundedChannel<SP> where
54955503
Ok(channel_monitor)
54965504
}
54975505

5498-
pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
5506+
fn validate_commitment_signed<L: Deref>(&self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<LatestHolderCommitmentTXInfo, ChannelError>
54995507
where L::Target: Logger
55005508
{
5501-
if self.context.channel_state.is_quiescent() {
5502-
return Err(ChannelError::WarnAndDisconnect("Got commitment_signed message while quiescent".to_owned()));
5503-
}
5504-
if !matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
5505-
return Err(ChannelError::close("Got commitment signed message when channel was not in an operational state".to_owned()));
5506-
}
5507-
if self.context.channel_state.is_peer_disconnected() {
5508-
return Err(ChannelError::close("Peer sent commitment_signed when we needed a channel_reestablish".to_owned()));
5509-
}
5510-
if self.context.channel_state.is_both_sides_shutdown() && self.context.last_sent_closing_fee.is_some() {
5511-
return Err(ChannelError::close("Peer sent commitment_signed after we'd started exchanging closing_signeds".to_owned()));
5512-
}
5513-
55145509
let funding_script = self.funding.get_funding_redeemscript();
55155510

55165511
let keys = self.context.build_holder_transaction_keys(&self.funding, self.holder_commitment_point.current_point());
@@ -5623,6 +5618,31 @@ impl<SP: Deref> FundedChannel<SP> where
56235618
self.context.holder_signer.as_ref().validate_holder_commitment(&holder_commitment_tx, commitment_stats.outbound_htlc_preimages)
56245619
.map_err(|_| ChannelError::close("Failed to validate our commitment".to_owned()))?;
56255620

5621+
Ok(LatestHolderCommitmentTXInfo {
5622+
commitment_tx: holder_commitment_tx,
5623+
htlc_outputs: htlcs_and_sigs,
5624+
nondust_htlc_sources,
5625+
})
5626+
}
5627+
5628+
pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
5629+
where L::Target: Logger
5630+
{
5631+
if self.context.channel_state.is_quiescent() {
5632+
return Err(ChannelError::WarnAndDisconnect("Got commitment_signed message while quiescent".to_owned()));
5633+
}
5634+
if !matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
5635+
return Err(ChannelError::close("Got commitment signed message when channel was not in an operational state".to_owned()));
5636+
}
5637+
if self.context.channel_state.is_peer_disconnected() {
5638+
return Err(ChannelError::close("Peer sent commitment_signed when we needed a channel_reestablish".to_owned()));
5639+
}
5640+
if self.context.channel_state.is_both_sides_shutdown() && self.context.last_sent_closing_fee.is_some() {
5641+
return Err(ChannelError::close("Peer sent commitment_signed after we'd started exchanging closing_signeds".to_owned()));
5642+
}
5643+
5644+
let commitment_tx_info = self.validate_commitment_signed(msg, logger)?;
5645+
56265646
// Update state now that we've passed all the can-fail calls...
56275647
let mut need_commitment = false;
56285648
if let &mut Some((_, ref mut update_state)) = &mut self.context.pending_update_fee {
@@ -5662,13 +5682,16 @@ impl<SP: Deref> FundedChannel<SP> where
56625682
}
56635683
}
56645684

5685+
let LatestHolderCommitmentTXInfo {
5686+
commitment_tx, htlc_outputs, nondust_htlc_sources,
5687+
} = commitment_tx_info;
56655688
self.context.latest_monitor_update_id += 1;
56665689
let mut monitor_update = ChannelMonitorUpdate {
56675690
update_id: self.context.latest_monitor_update_id,
56685691
counterparty_node_id: Some(self.context.counterparty_node_id),
56695692
updates: vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
5670-
commitment_tx: holder_commitment_tx,
5671-
htlc_outputs: htlcs_and_sigs,
5693+
commitment_tx,
5694+
htlc_outputs,
56725695
claimed_htlcs,
56735696
nondust_htlc_sources,
56745697
}],

0 commit comments

Comments
 (0)