Skip to content

Commit c8c9ab5

Browse files
committed
Do some validation in splice_ack before taking a &mut self
As much as possible, we want to only mutate state once we are done with input validation. It also helps with introducing helper validation functions that take a `&self` in the next commits.
1 parent df9232b commit c8c9ab5

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

lightning/src/ln/channel.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10831,26 +10831,17 @@ where
1083110831
ES::Target: EntropySource,
1083210832
L::Target: Logger,
1083310833
{
10834-
let pending_splice = if let Some(ref mut pending_splice) = &mut self.pending_splice {
10835-
pending_splice
10836-
} else {
10837-
return Err(ChannelError::Ignore(format!("Channel is not in pending splice")));
10838-
};
10839-
1084010834
// TODO(splicing): Add check that we are the splice (quiescence) initiator
1084110835

10842-
let funding_negotiation_context = match pending_splice.funding_negotiation.take() {
10836+
let funding_negotiation_context = match &self
10837+
.pending_splice
10838+
.as_ref()
10839+
.ok_or(ChannelError::Ignore(format!("Channel is not in pending splice")))?
10840+
.funding_negotiation
10841+
{
1084310842
Some(FundingNegotiation::AwaitingAck(context)) => context,
10844-
Some(FundingNegotiation::ConstructingTransaction(funding, constructor)) => {
10845-
pending_splice.funding_negotiation =
10846-
Some(FundingNegotiation::ConstructingTransaction(funding, constructor));
10847-
return Err(ChannelError::WarnAndDisconnect(format!(
10848-
"Got unexpected splice_ack; splice negotiation already in progress"
10849-
)));
10850-
},
10851-
Some(FundingNegotiation::AwaitingSignatures(funding)) => {
10852-
pending_splice.funding_negotiation =
10853-
Some(FundingNegotiation::AwaitingSignatures(funding));
10843+
Some(FundingNegotiation::ConstructingTransaction(_, _))
10844+
| Some(FundingNegotiation::AwaitingSignatures(_)) => {
1085410845
return Err(ChannelError::WarnAndDisconnect(format!(
1085510846
"Got unexpected splice_ack; splice negotiation already in progress"
1085610847
)));
@@ -10885,6 +10876,17 @@ where
1088510876
self.funding.get_value_satoshis(),
1088610877
);
1088710878

10879+
let pending_splice =
10880+
self.pending_splice.as_mut().expect("We should have returned an error earlier!");
10881+
// TODO: Good candidate for a let else statement once MSRV >= 1.65
10882+
let funding_negotiation_context = if let Some(FundingNegotiation::AwaitingAck(context)) =
10883+
pending_splice.funding_negotiation.take()
10884+
{
10885+
context
10886+
} else {
10887+
panic!("We should have returned an error earlier!");
10888+
};
10889+
1088810890
let mut interactive_tx_constructor = funding_negotiation_context
1088910891
.into_interactive_tx_constructor(
1089010892
&self.context,

0 commit comments

Comments
 (0)