Skip to content

Check if a batch is expected for commitment_signed #3852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 75 additions & 29 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6393,27 +6393,46 @@ where
Ok(channel_monitor)
}

#[rustfmt::skip]
pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
where L::Target: Logger
pub fn commitment_signed<L: Deref>(
&mut self, msg: &msgs::CommitmentSigned, logger: &L,
) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
where
L::Target: Logger,
{
self.commitment_signed_check_state()?;

if !self.pending_funding.is_empty() {
return Err(ChannelError::close(
"Got a single commitment_signed message when expecting a batch".to_owned(),
));
}

let updates = self
.context
.validate_commitment_signed(&self.funding, &self.holder_commitment_point, msg, logger)
.map(|LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs, nondust_htlc_sources }|
vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
commitment_tx, htlc_outputs, claimed_htlcs: vec![], nondust_htlc_sources,
}]
.map(
|LatestHolderCommitmentTXInfo {
commitment_tx,
htlc_outputs,
nondust_htlc_sources,
}| {
vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
commitment_tx,
htlc_outputs,
claimed_htlcs: vec![],
nondust_htlc_sources,
}]
},
)?;

self.commitment_signed_update_monitor(updates, logger)
}

#[rustfmt::skip]
pub fn commitment_signed_batch<L: Deref>(&mut self, batch: Vec<msgs::CommitmentSigned>, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
where L::Target: Logger
pub fn commitment_signed_batch<L: Deref>(
&mut self, batch: Vec<msgs::CommitmentSigned>, logger: &L,
) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
where
L::Target: Logger,
{
self.commitment_signed_check_state()?;

Expand All @@ -6422,15 +6441,22 @@ where
let funding_txid = match msg.funding_txid {
Some(funding_txid) => funding_txid,
None => {
return Err(ChannelError::close("Peer sent batched commitment_signed without a funding_txid".to_string()));
return Err(ChannelError::close(
"Peer sent batched commitment_signed without a funding_txid".to_string(),
));
},
};

match messages.entry(funding_txid) {
btree_map::Entry::Vacant(entry) => { entry.insert(msg); },
btree_map::Entry::Vacant(entry) => {
entry.insert(msg);
},
btree_map::Entry::Occupied(_) => {
return Err(ChannelError::close(format!("Peer sent batched commitment_signed with duplicate funding_txid {}", funding_txid)));
}
return Err(ChannelError::close(format!(
"Peer sent batched commitment_signed with duplicate funding_txid {}",
funding_txid
)));
},
}
}

Expand All @@ -6440,36 +6466,56 @@ where
.chain(self.pending_funding.iter())
.map(|funding| {
let funding_txid = funding.get_funding_txo().unwrap().txid;
let msg = messages
.get(&funding_txid)
.ok_or_else(|| ChannelError::close(format!("Peer did not send a commitment_signed for pending splice transaction: {}", funding_txid)))?;
let msg = messages.get(&funding_txid).ok_or_else(|| {
ChannelError::close(format!(
"Peer did not send a commitment_signed for pending splice transaction: {}",
funding_txid
))
})?;
self.context
.validate_commitment_signed(funding, &self.holder_commitment_point, msg, logger)
.map(|LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs, nondust_htlc_sources }|
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
commitment_tx, htlc_outputs, claimed_htlcs: vec![], nondust_htlc_sources,
}
.map(
|LatestHolderCommitmentTXInfo {
commitment_tx,
htlc_outputs,
nondust_htlc_sources,
}| ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
commitment_tx,
htlc_outputs,
claimed_htlcs: vec![],
nondust_htlc_sources,
},
)
}
)
})
.collect::<Result<Vec<_>, ChannelError>>()?;

self.commitment_signed_update_monitor(updates, logger)
}

#[rustfmt::skip]
fn commitment_signed_check_state(&self) -> Result<(), ChannelError> {
if self.context.channel_state.is_quiescent() {
return Err(ChannelError::WarnAndDisconnect("Got commitment_signed message while quiescent".to_owned()));
return Err(ChannelError::WarnAndDisconnect(
"Got commitment_signed message while quiescent".to_owned(),
));
}
if !matches!(self.context.channel_state, ChannelState::ChannelReady(_)) {
return Err(ChannelError::close("Got commitment signed message when channel was not in an operational state".to_owned()));
return Err(ChannelError::close(
"Got commitment signed message when channel was not in an operational state"
.to_owned(),
));
}
if self.context.channel_state.is_peer_disconnected() {
return Err(ChannelError::close("Peer sent commitment_signed when we needed a channel_reestablish".to_owned()));
return Err(ChannelError::close(
"Peer sent commitment_signed when we needed a channel_reestablish".to_owned(),
));
}
if self.context.channel_state.is_both_sides_shutdown() && self.context.last_sent_closing_fee.is_some() {
return Err(ChannelError::close("Peer sent commitment_signed after we'd started exchanging closing_signeds".to_owned()));
if self.context.channel_state.is_both_sides_shutdown()
&& self.context.last_sent_closing_fee.is_some()
{
return Err(ChannelError::close(
"Peer sent commitment_signed after we'd started exchanging closing_signeds"
.to_owned(),
));
}

Ok(())
Expand Down
Loading