Skip to content

Commit 6639f0c

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 65c84e4 commit 6639f0c

File tree

2 files changed

+56
-46
lines changed

2 files changed

+56
-46
lines changed

lightning/src/ln/channel.rs

+51-26
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 {
@@ -1425,6 +1417,34 @@ impl<SP: Deref> Channel<SP> where
14251417

14261418
debug_assert!(!matches!(self.phase, ChannelPhase::Undefined));
14271419
}
1420+
1421+
pub fn funding_tx_constructed<L: Deref>(
1422+
&mut self, signing_session: InteractiveTxSigningSession, logger: &L
1423+
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
1424+
where
1425+
L::Target: Logger
1426+
{
1427+
let phase = core::mem::replace(&mut self.phase, ChannelPhase::Undefined);
1428+
let result = if let ChannelPhase::UnfundedV2(chan) = phase {
1429+
let logger = WithChannelContext::from(logger, &chan.context, None);
1430+
match chan.funding_tx_constructed(signing_session, &&logger) {
1431+
Ok((chan, commitment_signed, event)) => {
1432+
self.phase = ChannelPhase::Funded(chan);
1433+
Ok((commitment_signed, event))
1434+
},
1435+
Err((chan, e)) => {
1436+
self.phase = ChannelPhase::UnfundedV2(chan);
1437+
Err(e)
1438+
},
1439+
}
1440+
} else {
1441+
self.phase = phase;
1442+
Err(ChannelError::Warn("Got a tx_complete message with no interactive transaction construction expected or in-progress".to_owned()))
1443+
};
1444+
1445+
debug_assert!(!matches!(self.phase, ChannelPhase::Undefined));
1446+
result
1447+
}
14281448
}
14291449

14301450
impl<SP: Deref> From<OutboundV1Channel<SP>> for Channel<SP>
@@ -2073,8 +2093,8 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20732093
}
20742094

20752095
pub fn funding_tx_constructed<L: Deref>(
2076-
&mut self, signing_session: &mut InteractiveTxSigningSession, logger: &L
2077-
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
2096+
mut self, mut signing_session: InteractiveTxSigningSession, logger: &L
2097+
) -> Result<(FundedChannel<SP>, msgs::CommitmentSigned, Option<Event>), (PendingV2Channel<SP>, ChannelError)>
20782098
where
20792099
L::Target: Logger
20802100
{
@@ -2090,7 +2110,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20902110
(
20912111
"Multiple outputs matched the expected script and value".to_owned(),
20922112
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
2093-
)));
2113+
))).map_err(|e| (self, e));
20942114
}
20952115
output_index = Some(idx as u16);
20962116
}
@@ -2102,7 +2122,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21022122
(
21032123
"No output matched the funding script_pubkey".to_owned(),
21042124
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
2105-
)));
2125+
))).map_err(|e| (self, e));
21062126
};
21072127
self.context.channel_transaction_parameters.funding_outpoint = Some(outpoint);
21082128
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
@@ -2117,6 +2137,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21172137
Err(err) => {
21182138
self.context.channel_transaction_parameters.funding_outpoint = None;
21192139
return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })))
2140+
.map_err(|e| (self, e));
21202141
},
21212142
};
21222143

@@ -2151,7 +2172,24 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21512172
// Clear the interactive transaction constructor
21522173
self.interactive_tx_constructor.take();
21532174

2154-
Ok((commitment_signed, funding_ready_for_sig_event))
2175+
match self.unfunded_context.holder_commitment_point {
2176+
Some(holder_commitment_point) => {
2177+
let funded_chan = FundedChannel {
2178+
context: self.context,
2179+
interactive_tx_signing_session: Some(signing_session),
2180+
holder_commitment_point,
2181+
};
2182+
Ok((funded_chan, commitment_signed, funding_ready_for_sig_event))
2183+
},
2184+
None => {
2185+
Err(ChannelError::close(
2186+
format!(
2187+
"Expected to have holder commitment points available upon finishing interactive tx construction for channel {}",
2188+
self.context.channel_id(),
2189+
)))
2190+
.map_err(|e| (self, e))
2191+
},
2192+
}
21552193
}
21562194
}
21572195

@@ -9422,19 +9460,6 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
94229460
pub fn get_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
94239461
self.generate_accept_channel_v2_message()
94249462
}
9425-
9426-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<FundedChannel<SP>, ChannelError>{
9427-
let holder_commitment_point = self.unfunded_context.holder_commitment_point.ok_or(ChannelError::close(
9428-
format!("Expected to have holder commitment points available upon finishing interactive tx construction for channel {}",
9429-
self.context.channel_id())))?;
9430-
let channel = FundedChannel {
9431-
context: self.context,
9432-
interactive_tx_signing_session: Some(signing_session),
9433-
holder_commitment_point,
9434-
};
9435-
9436-
Ok(channel)
9437-
}
94389463
}
94399464

94409465
// Unfunded channel utilities

lightning/src/ln/channelmanager.rs

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

0 commit comments

Comments
 (0)