Skip to content

Commit 9773f1a

Browse files
committed
Move tx_complete phase transition to Channel
Now that ChannelPhase is encapsulated in Channel, phase transitions can be moved from ChannelManager to Channel. Update the tx_complete phase transition accordingly. This allows for simpler logic in ChannelManager since the channel does not need to removed and then re-added into the channel_by_id map.
1 parent aaf6b0a commit 9773f1a

File tree

2 files changed

+53
-46
lines changed

2 files changed

+53
-46
lines changed

lightning/src/ln/channel.rs

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,14 +1243,6 @@ impl<SP: Deref> Channel<SP> where
12431243
}
12441244
}
12451245

1246-
pub fn into_unfunded_v2(self) -> Option<PendingV2Channel<SP>> {
1247-
if let ChannelPhase::UnfundedV2(channel) = self.phase {
1248-
Some(channel)
1249-
} else {
1250-
None
1251-
}
1252-
}
1253-
12541246
pub fn signer_maybe_unblocked<L: Deref>(
12551247
&mut self, chain_hash: ChainHash, logger: &L,
12561248
) -> Option<SignerResumeUpdates> where L::Target: Logger {
@@ -1400,6 +1392,31 @@ impl<SP: Deref> Channel<SP> where
14001392
Err(ChannelError::NotFound("Failed to find corresponding channel".to_owned()))
14011393
}
14021394
}
1395+
1396+
pub fn funding_tx_constructed<L: Deref>(
1397+
&mut self, signing_session: InteractiveTxSigningSession, logger: &L
1398+
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
1399+
where
1400+
L::Target: Logger
1401+
{
1402+
let phase = core::mem::replace(&mut self.phase, ChannelPhase::Intermediate);
1403+
if let ChannelPhase::UnfundedV2(chan) = phase {
1404+
let logger = WithChannelContext::from(logger, &chan.context, None);
1405+
match chan.funding_tx_constructed(signing_session, &&logger) {
1406+
Ok((chan, commitment_signed, event)) => {
1407+
self.phase = ChannelPhase::Funded(chan);
1408+
Ok((commitment_signed, event))
1409+
},
1410+
Err((chan, e)) => {
1411+
self.phase = ChannelPhase::UnfundedV2(chan);
1412+
Err(e)
1413+
},
1414+
}
1415+
} else {
1416+
self.phase = phase;
1417+
Err(ChannelError::Warn("Got a tx_complete message with no interactive transaction construction expected or in-progress".to_owned()))
1418+
}
1419+
}
14031420
}
14041421

14051422
impl<SP: Deref> From<OutboundV1Channel<SP>> for Channel<SP>
@@ -2048,8 +2065,8 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20482065
}
20492066

20502067
pub fn funding_tx_constructed<L: Deref>(
2051-
&mut self, signing_session: &mut InteractiveTxSigningSession, logger: &L
2052-
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
2068+
mut self, mut signing_session: InteractiveTxSigningSession, logger: &L
2069+
) -> Result<(FundedChannel<SP>, msgs::CommitmentSigned, Option<Event>), (PendingV2Channel<SP>, ChannelError)>
20532070
where
20542071
L::Target: Logger
20552072
{
@@ -2065,7 +2082,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20652082
(
20662083
"Multiple outputs matched the expected script and value".to_owned(),
20672084
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
2068-
)));
2085+
))).map_err(|e| (self, e));
20692086
}
20702087
output_index = Some(idx as u16);
20712088
}
@@ -2077,7 +2094,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20772094
(
20782095
"No output matched the funding script_pubkey".to_owned(),
20792096
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
2080-
)));
2097+
))).map_err(|e| (self, e));
20812098
};
20822099
self.context.channel_transaction_parameters.funding_outpoint = Some(outpoint);
20832100
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
@@ -2092,6 +2109,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20922109
Err(err) => {
20932110
self.context.channel_transaction_parameters.funding_outpoint = None;
20942111
return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })))
2112+
.map_err(|e| (self, e));
20952113
},
20962114
};
20972115

@@ -2126,7 +2144,24 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21262144
// Clear the interactive transaction constructor
21272145
self.interactive_tx_constructor.take();
21282146

2129-
Ok((commitment_signed, funding_ready_for_sig_event))
2147+
match self.unfunded_context.holder_commitment_point {
2148+
Some(holder_commitment_point) => {
2149+
let funded_chan = FundedChannel {
2150+
context: self.context,
2151+
interactive_tx_signing_session: Some(signing_session),
2152+
holder_commitment_point,
2153+
};
2154+
Ok((funded_chan, commitment_signed, funding_ready_for_sig_event))
2155+
},
2156+
None => {
2157+
Err(ChannelError::close(
2158+
format!(
2159+
"Expected to have holder commitment points available upon finishing interactive tx construction for channel {}",
2160+
self.context.channel_id(),
2161+
)))
2162+
.map_err(|e| (self, e))
2163+
},
2164+
}
21302165
}
21312166
}
21322167

@@ -9394,19 +9429,6 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
93949429
pub fn get_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
93959430
self.generate_accept_channel_v2_message()
93969431
}
9397-
9398-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<FundedChannel<SP>, ChannelError>{
9399-
let holder_commitment_point = self.unfunded_context.holder_commitment_point.ok_or(ChannelError::close(
9400-
format!("Expected to have holder commitment points available upon finishing interactive tx construction for channel {}",
9401-
self.context.channel_id())))?;
9402-
let channel = FundedChannel {
9403-
context: self.context,
9404-
interactive_tx_signing_session: Some(signing_session),
9405-
holder_commitment_point,
9406-
};
9407-
9408-
Ok(channel)
9409-
}
94109432
}
94119433

94129434
// Unfunded channel utilities

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8289,26 +8289,11 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
82898289
if let Some(msg_send_event) = msg_send_event_opt {
82908290
peer_state.pending_msg_events.push(msg_send_event);
82918291
};
8292-
if let Some(mut signing_session) = signing_session_opt {
8293-
let (commitment_signed, funding_ready_for_sig_event_opt) = match chan_entry.get_mut().as_unfunded_v2_mut() {
8294-
Some(chan) => {
8295-
chan.funding_tx_constructed(&mut signing_session, &self.logger)
8296-
},
8297-
None => Err(ChannelError::Warn(
8298-
"Got a tx_complete message with no interactive transaction construction expected or in-progress"
8299-
.into())),
8300-
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8301-
let (channel_id, channel) = chan_entry.remove_entry();
8302-
let channel = match channel.into_unfunded_v2() {
8303-
Some(chan) => chan.into_channel(signing_session),
8304-
None => {
8305-
debug_assert!(false); // It cannot be another variant as we are in the `Ok` branch of the above match.
8306-
Err(ChannelError::Warn(
8307-
"Got a tx_complete message with no interactive transaction construction expected or in-progress"
8308-
.into()))
8309-
},
8310-
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8311-
peer_state.channel_by_id.insert(channel_id, Channel::from(channel));
8292+
if let Some(signing_session) = signing_session_opt {
8293+
let (commitment_signed, funding_ready_for_sig_event_opt) = chan_entry
8294+
.get_mut()
8295+
.funding_tx_constructed(signing_session, &self.logger)
8296+
.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
83128297
if let Some(funding_ready_for_sig_event) = funding_ready_for_sig_event_opt {
83138298
let mut pending_events = self.pending_events.lock().unwrap();
83148299
pending_events.push_back((funding_ready_for_sig_event, None));

0 commit comments

Comments
 (0)