Skip to content

Commit 7bc39f9

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 7d03d51 commit 7bc39f9

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 {
@@ -1427,6 +1419,31 @@ impl<SP: Deref> Channel<SP> where
14271419

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

14321449
impl<SP: Deref> From<OutboundV1Channel<SP>> for Channel<SP>
@@ -2075,8 +2092,8 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20752092
}
20762093

20772094
pub fn funding_tx_constructed<L: Deref>(
2078-
&mut self, signing_session: &mut InteractiveTxSigningSession, logger: &L
2079-
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
2095+
mut self, mut signing_session: InteractiveTxSigningSession, logger: &L
2096+
) -> Result<(FundedChannel<SP>, msgs::CommitmentSigned, Option<Event>), (PendingV2Channel<SP>, ChannelError)>
20802097
where
20812098
L::Target: Logger
20822099
{
@@ -2092,7 +2109,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20922109
(
20932110
"Multiple outputs matched the expected script and value".to_owned(),
20942111
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
2095-
)));
2112+
))).map_err(|e| (self, e));
20962113
}
20972114
output_index = Some(idx as u16);
20982115
}
@@ -2104,7 +2121,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21042121
(
21052122
"No output matched the funding script_pubkey".to_owned(),
21062123
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
2107-
)));
2124+
))).map_err(|e| (self, e));
21082125
};
21092126
self.context.channel_transaction_parameters.funding_outpoint = Some(outpoint);
21102127
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
@@ -2119,6 +2136,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21192136
Err(err) => {
21202137
self.context.channel_transaction_parameters.funding_outpoint = None;
21212138
return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })))
2139+
.map_err(|e| (self, e));
21222140
},
21232141
};
21242142

@@ -2153,7 +2171,24 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21532171
// Clear the interactive transaction constructor
21542172
self.interactive_tx_constructor.take();
21552173

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

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

94399461
// Unfunded channel utilities

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 20 deletions
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)