Skip to content

Commit 40c8c67

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 92c6f77 commit 40c8c67

File tree

2 files changed

+53
-46
lines changed

2 files changed

+53
-46
lines changed

lightning/src/ln/channel.rs

+48-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,31 @@ 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+
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+
}
14281445
}
14291446

14301447
impl<SP: Deref> From<OutboundV1Channel<SP>> for Channel<SP>
@@ -2073,8 +2090,8 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20732090
}
20742091

20752092
pub fn funding_tx_constructed<L: Deref>(
2076-
&mut self, signing_session: &mut InteractiveTxSigningSession, logger: &L
2077-
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
2093+
mut self, mut signing_session: InteractiveTxSigningSession, logger: &L
2094+
) -> Result<(FundedChannel<SP>, msgs::CommitmentSigned, Option<Event>), (PendingV2Channel<SP>, ChannelError)>
20782095
where
20792096
L::Target: Logger
20802097
{
@@ -2090,7 +2107,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
20902107
(
20912108
"Multiple outputs matched the expected script and value".to_owned(),
20922109
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
2093-
)));
2110+
))).map_err(|e| (self, e));
20942111
}
20952112
output_index = Some(idx as u16);
20962113
}
@@ -2102,7 +2119,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21022119
(
21032120
"No output matched the funding script_pubkey".to_owned(),
21042121
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
2105-
)));
2122+
))).map_err(|e| (self, e));
21062123
};
21072124
self.context.channel_transaction_parameters.funding_outpoint = Some(outpoint);
21082125
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
@@ -2117,6 +2134,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21172134
Err(err) => {
21182135
self.context.channel_transaction_parameters.funding_outpoint = None;
21192136
return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })))
2137+
.map_err(|e| (self, e));
21202138
},
21212139
};
21222140

@@ -2151,7 +2169,24 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
21512169
// Clear the interactive transaction constructor
21522170
self.interactive_tx_constructor.take();
21532171

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

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

94379459
// 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)