Skip to content

Commit edffca2

Browse files
committed
Handle transition to Funded inline, without remove+readd from map
1 parent 60a3f36 commit edffca2

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

lightning/src/ln/channel.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,7 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11391139
SP::Target: SignerProvider,
11401140
<SP::Target as SignerProvider>::EcdsaSigner: ChannelSigner,
11411141
{
1142+
#[inline]
11421143
pub fn context(&'a self) -> &'a ChannelContext<SP> {
11431144
match self {
11441145
ChannelPhase::Funded(chan) => &chan.context,
@@ -1149,6 +1150,7 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11491150
}
11501151
}
11511152

1153+
#[inline]
11521154
pub fn context_mut(&'a mut self) -> &'a mut ChannelContext<SP> {
11531155
match self {
11541156
ChannelPhase::Funded(ref mut chan) => &mut chan.context,
@@ -1162,40 +1164,72 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11621164

11631165
/// A top-level channel struct, containing a channel phase
11641166
pub(super) struct ChannelWrapper<SP: Deref> where SP::Target: SignerProvider {
1165-
phase: ChannelPhase<SP>,
1167+
/// The inner channel phase
1168+
/// Option is used to facilitate in-place replacement (see e.g. move_v2_to_funded),
1169+
/// but it is never None, ensured in new() and set_phase()
1170+
phase: Option<ChannelPhase<SP>>,
11661171
}
11671172

11681173
impl<'a, SP: Deref> ChannelWrapper<SP> where SP::Target: SignerProvider {
11691174
pub fn new(phase: ChannelPhase<SP>) -> Self {
1170-
Self { phase }
1175+
Self {
1176+
phase: Some(phase),
1177+
}
11711178
}
11721179

1180+
#[inline]
11731181
pub fn phase(&self) -> &ChannelPhase<SP> {
1174-
&self.phase
1182+
self.phase.as_ref().unwrap()
11751183
}
11761184

1185+
#[inline]
11771186
pub fn phase_mut(&mut self) -> &mut ChannelPhase<SP> {
1178-
&mut self.phase
1187+
self.phase.as_mut().unwrap()
11791188
}
11801189

11811190
pub fn phase_take(self) -> ChannelPhase<SP> {
1182-
self.phase
1191+
match self.phase {
1192+
Some(phase) => phase,
1193+
None => panic!("None phase"),
1194+
}
11831195
}
11841196

11851197
pub fn get_funded_channel(&self) -> Option<&Channel<SP>> {
1186-
if let ChannelPhase::Funded(chan) = &self.phase {
1198+
if let ChannelPhase::Funded(chan) = &self.phase.as_ref().unwrap() {
11871199
Some(chan)
11881200
} else {
11891201
None
11901202
}
11911203
}
11921204

1205+
/// Change the internal phase
1206+
#[inline]
1207+
pub fn set_phase(&mut self, new_phase: ChannelPhase<SP>) {
1208+
self.phase = Some(new_phase);
1209+
}
1210+
1211+
pub fn move_v2_to_funded(&mut self, signing_session: InteractiveTxSigningSession) -> Result<(), ChannelError> {
1212+
// We need a borrow to the phase field, but self is only a mut ref
1213+
let phase_inline = self.phase.take().unwrap();
1214+
let new_phase = match phase_inline {
1215+
ChannelPhase::UnfundedOutboundV2(chan) =>
1216+
ChannelPhase::Funded(chan.into_funded_channel(signing_session)?),
1217+
ChannelPhase::UnfundedInboundV2(chan) =>
1218+
ChannelPhase::Funded(chan.into_funded_channel(signing_session)?),
1219+
_ => phase_inline,
1220+
};
1221+
self.set_phase(new_phase);
1222+
Ok(())
1223+
}
1224+
1225+
#[inline]
11931226
pub fn context(&'a self) -> &'a ChannelContext<SP> {
1194-
self.phase.context()
1227+
self.phase().context()
11951228
}
11961229

1230+
#[inline]
11971231
pub fn context_mut(&'a mut self) -> &'a mut ChannelContext<SP> {
1198-
self.phase.context_mut()
1232+
self.phase_mut().context_mut()
11991233
}
12001234
}
12011235

@@ -8896,7 +8930,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
88968930
}
88978931
}
88988932

8899-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<Channel<SP>, ChannelError>{
8933+
pub fn into_funded_channel(self, signing_session: InteractiveTxSigningSession) -> Result<Channel<SP>, ChannelError>{
89008934
let channel = Channel {
89018935
context: self.context,
89028936
interactive_tx_signing_session: Some(signing_session),
@@ -9090,7 +9124,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
90909124
self.generate_accept_channel_v2_message()
90919125
}
90929126

9093-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<Channel<SP>, ChannelError>{
9127+
pub fn into_funded_channel(self, signing_session: InteractiveTxSigningSession) -> Result<Channel<SP>, ChannelError>{
90949128
let channel = Channel {
90959129
context: self.context,
90969130
interactive_tx_signing_session: Some(signing_session),

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5012,6 +5012,7 @@ where
50125012
}
50135013
}
50145014
} else {
5015+
// put it back
50155016
peer_state.channel_by_id.insert(temporary_channel_id, ChannelWrapper::new(phase));
50165017
return Err(APIError::APIMisuseError {
50175018
err: format!(
@@ -8314,18 +8315,16 @@ where
83148315
"Got a tx_complete message with no interactive transaction construction expected or in-progress"
83158316
.into())),
83168317
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8317-
let (channel_id, channel) = chan_entry.remove_entry();
8318-
let channel = match channel.phase_take() {
8319-
ChannelPhase::UnfundedOutboundV2(chan) => chan.into_channel(signing_session),
8320-
ChannelPhase::UnfundedInboundV2(chan) => chan.into_channel(signing_session),
8321-
_ => {
8318+
8319+
// change the channel phase inline
8320+
match chan_entry.get_mut().move_v2_to_funded(signing_session) {
8321+
Ok(_) => {},
8322+
Err(err) => {
83228323
debug_assert!(false); // It cannot be another variant as we are in the `Ok` branch of the above match.
8323-
Err(ChannelError::Warn(
8324-
"Got a tx_complete message with no interactive transaction construction expected or in-progress"
8325-
.into()))
8326-
},
8327-
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8328-
peer_state.channel_by_id.insert(channel_id, ChannelWrapper::new(ChannelPhase::Funded(channel)));
8324+
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a tx_complete message, could not transition to funding, {}", err), msg.channel_id))?;
8325+
}
8326+
}
8327+
83298328
if let Some(funding_ready_for_sig_event) = funding_ready_for_sig_event_opt {
83308329
let mut pending_events = self.pending_events.lock().unwrap();
83318330
pending_events.push_back((funding_ready_for_sig_event, None));

0 commit comments

Comments
 (0)