Skip to content

Commit b75ca39

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 d4bd56f commit b75ca39

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

@@ -9418,19 +9456,6 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
94189456
pub fn get_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
94199457
self.generate_accept_channel_v2_message()
94209458
}
9421-
9422-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<FundedChannel<SP>, ChannelError>{
9423-
let holder_commitment_point = self.unfunded_context.holder_commitment_point.ok_or(ChannelError::close(
9424-
format!("Expected to have holder commitment points available upon finishing interactive tx construction for channel {}",
9425-
self.context.channel_id())))?;
9426-
let channel = FundedChannel {
9427-
context: self.context,
9428-
interactive_tx_signing_session: Some(signing_session),
9429-
holder_commitment_point,
9430-
};
9431-
9432-
Ok(channel)
9433-
}
94349459
}
94359460

94369461
// Unfunded channel utilities

lightning/src/ln/channelmanager.rs

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

0 commit comments

Comments
 (0)