Skip to content

Commit 0707e53

Browse files
committed
Promote V2 channels to Channel on initial commitment_signed receipt
Before this commit, unfunded V2 channels were promoted to `Channel`s in the `Funded` channel phase in `funding_tx_constructed`. Since a monitor is only created upon receipt of an initial `commitment_signed`, this would cause a crash if the channel was read from persisted data between those two events. Consequently, we also need to hold an `interactive_tx_signing_session` for both of our unfunded V2 channel structs.
1 parent 8d9624f commit 0707e53

File tree

2 files changed

+106
-48
lines changed

2 files changed

+106
-48
lines changed

lightning/src/ln/channel.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -8736,6 +8736,8 @@ pub(super) struct OutboundV2Channel<SP: Deref> where SP::Target: SignerProvider
87368736
pub dual_funding_context: DualFundingChannelContext,
87378737
/// The current interactive transaction construction session under negotiation.
87388738
interactive_tx_constructor: Option<InteractiveTxConstructor>,
8739+
/// The signing session created after `tx_complete` handling
8740+
pub interactive_tx_signing_session: Option<InteractiveTxSigningSession>,
87398741
}
87408742

87418743
impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
@@ -8795,6 +8797,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
87958797
our_funding_inputs: funding_inputs,
87968798
},
87978799
interactive_tx_constructor: None,
8800+
interactive_tx_signing_session: None,
87988801
};
87998802
Ok(chan)
88008803
}
@@ -8862,13 +8865,11 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
88628865
}
88638866
}
88648867

8865-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<Channel<SP>, ChannelError>{
8866-
let channel = Channel {
8868+
pub fn into_channel(self) -> Channel<SP> {
8869+
Channel {
88678870
context: self.context,
8868-
interactive_tx_signing_session: Some(signing_session),
8869-
};
8870-
8871-
Ok(channel)
8871+
interactive_tx_signing_session: self.interactive_tx_signing_session,
8872+
}
88728873
}
88738874
}
88748875

@@ -8879,6 +8880,8 @@ pub(super) struct InboundV2Channel<SP: Deref> where SP::Target: SignerProvider {
88798880
pub dual_funding_context: DualFundingChannelContext,
88808881
/// The current interactive transaction construction session under negotiation.
88818882
interactive_tx_constructor: Option<InteractiveTxConstructor>,
8883+
/// The signing session created after `tx_complete` handling
8884+
pub interactive_tx_signing_session: Option<InteractiveTxSigningSession>,
88828885
}
88838886

88848887
impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
@@ -8980,6 +8983,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
89808983
context,
89818984
dual_funding_context,
89828985
interactive_tx_constructor,
8986+
interactive_tx_signing_session: None,
89838987
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
89848988
})
89858989
}
@@ -9056,13 +9060,11 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
90569060
self.generate_accept_channel_v2_message()
90579061
}
90589062

9059-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<Channel<SP>, ChannelError>{
9060-
let channel = Channel {
9063+
pub fn into_channel(self) -> Channel<SP> {
9064+
Channel {
90619065
context: self.context,
9062-
interactive_tx_signing_session: Some(signing_session),
9063-
};
9064-
9065-
Ok(channel)
9066+
interactive_tx_signing_session: self.interactive_tx_signing_session,
9067+
}
90669068
}
90679069
}
90689070

lightning/src/ln/channelmanager.rs

+92-36
Original file line numberDiff line numberDiff line change
@@ -8305,7 +8305,7 @@ where
83058305
peer_state.pending_msg_events.push(msg_send_event);
83068306
};
83078307
if let Some(mut signing_session) = signing_session_opt {
8308-
let (commitment_signed, funding_ready_for_sig_event_opt) = match chan_phase_entry.get_mut() {
8308+
let (commitment_signed, funding_ready_for_sig_event_opt) = match channel_phase {
83098309
ChannelPhase::UnfundedOutboundV2(chan) => {
83108310
chan.funding_tx_constructed(&mut signing_session, &self.logger)
83118311
},
@@ -8316,18 +8316,17 @@ where
83168316
"Got a tx_complete message with no interactive transaction construction expected or in-progress"
83178317
.into())),
83188318
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8319-
let (channel_id, channel_phase) = chan_phase_entry.remove_entry();
8320-
let channel = match channel_phase {
8321-
ChannelPhase::UnfundedOutboundV2(chan) => chan.into_channel(signing_session),
8322-
ChannelPhase::UnfundedInboundV2(chan) => chan.into_channel(signing_session),
8319+
match channel_phase {
8320+
ChannelPhase::UnfundedOutboundV2(chan) => chan.interactive_tx_signing_session = Some(signing_session),
8321+
ChannelPhase::UnfundedInboundV2(chan) => chan.interactive_tx_signing_session = Some(signing_session),
83238322
_ => {
83248323
debug_assert!(false); // It cannot be another variant as we are in the `Ok` branch of the above match.
8325-
Err(ChannelError::Warn(
8324+
let err = ChannelError::Warn(
83268325
"Got a tx_complete message with no interactive transaction construction expected or in-progress"
8327-
.into()))
8326+
.into());
8327+
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id));
83288328
},
8329-
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8330-
peer_state.channel_by_id.insert(channel_id, ChannelPhase::Funded(channel));
8329+
}
83318330
if let Some(funding_ready_for_sig_event) = funding_ready_for_sig_event_opt {
83328331
let mut pending_events = self.pending_events.lock().unwrap();
83338332
pending_events.push_back((funding_ready_for_sig_event, None));
@@ -8840,46 +8839,103 @@ where
88408839
})?;
88418840
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
88428841
let peer_state = &mut *peer_state_lock;
8843-
match peer_state.channel_by_id.entry(msg.channel_id) {
8842+
let (channel_id, mut chan) = match peer_state.channel_by_id.entry(msg.channel_id) {
88448843
hash_map::Entry::Occupied(mut chan_phase_entry) => {
8845-
if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
8846-
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8847-
let funding_txo = chan.context.get_funding_txo();
8848-
8849-
if chan.interactive_tx_signing_session.is_some() {
8850-
let monitor = try_chan_phase_entry!(
8851-
self, peer_state, chan.commitment_signed_initial_v2(msg, best_block, &self.signer_provider, &&logger),
8852-
chan_phase_entry);
8853-
let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
8854-
if let Ok(persist_state) = monitor_res {
8855-
handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
8856-
per_peer_state, chan, INITIAL_MONITOR);
8844+
let channel_phase = chan_phase_entry.get_mut();
8845+
match channel_phase {
8846+
ChannelPhase::UnfundedOutboundV2(chan) => {
8847+
if chan.interactive_tx_signing_session.is_some() {
8848+
let (channel_id, mut channel_phase) = chan_phase_entry.remove_entry();
8849+
match channel_phase {
8850+
ChannelPhase::UnfundedOutboundV2(chan) => {
8851+
(channel_id, chan.into_channel())
8852+
}
8853+
_ => {
8854+
debug_assert!(false, "The channel phase was not UnfundedOutboundV2");
8855+
let err = ChannelError::close(
8856+
"Closing due to unexpected sender error".into());
8857+
return Err(convert_chan_phase_err!(self, peer_state, err, &mut channel_phase,
8858+
&channel_id).1)
8859+
}
8860+
}
88578861
} else {
8858-
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8859-
log_error!(logger, "Persisting initial ChannelMonitor failed, implying the funding outpoint was duplicated");
8860-
try_chan_phase_entry!(self, peer_state, Err(ChannelError::Close(
8861-
(
8862-
"Channel funding outpoint was a duplicate".to_owned(),
8863-
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
8864-
)
8865-
)), chan_phase_entry)
8862+
return try_chan_phase_entry!(self, peer_state, Err(ChannelError::close(
8863+
"Got a commitment_signed message for a V2 channel before funding transaction constructed!".into())), chan_phase_entry);
88668864
}
8867-
} else {
8865+
},
8866+
ChannelPhase::UnfundedInboundV2(chan) => {
8867+
// TODO(dual_funding): This should be somewhat DRYable when #3418 is merged.
8868+
if chan.interactive_tx_signing_session.is_some() {
8869+
let (channel_id, mut channel_phase) = chan_phase_entry.remove_entry();
8870+
match channel_phase {
8871+
ChannelPhase::UnfundedInboundV2(chan) => {
8872+
(channel_id, chan.into_channel())
8873+
}
8874+
_ => {
8875+
debug_assert!(false, "The channel phase was not UnfundedInboundV2");
8876+
let err = ChannelError::close(
8877+
"Closing due to unexpected sender error".into());
8878+
return Err(convert_chan_phase_err!(self, peer_state, err, &mut channel_phase,
8879+
&channel_id).1)
8880+
}
8881+
}
8882+
} else {
8883+
return try_chan_phase_entry!(self, peer_state, Err(ChannelError::close(
8884+
"Got a commitment_signed message for a V2 channel before funding transaction constructed!".into())), chan_phase_entry);
8885+
}
8886+
},
8887+
ChannelPhase::Funded(chan) => {
8888+
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8889+
let funding_txo = chan.context.get_funding_txo();
88688890
let monitor_update_opt = try_chan_phase_entry!(
88698891
self, peer_state, chan.commitment_signed(msg, &&logger), chan_phase_entry);
88708892
if let Some(monitor_update) = monitor_update_opt {
88718893
handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update, peer_state_lock,
88728894
peer_state, per_peer_state, chan);
88738895
}
8896+
return Ok(())
8897+
},
8898+
_ => {
8899+
return try_chan_phase_entry!(self, peer_state, Err(ChannelError::close(
8900+
"Got a commitment_signed message for an unfunded channel!".into())), chan_phase_entry);
88748901
}
8875-
Ok(())
8876-
} else {
8877-
return try_chan_phase_entry!(self, peer_state, Err(ChannelError::close(
8878-
"Got a commitment_signed message for an unfunded channel!".into())), chan_phase_entry);
88798902
}
88808903
},
8881-
hash_map::Entry::Vacant(_) => Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
8904+
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(
8905+
format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
8906+
counterparty_node_id), msg.channel_id))
8907+
};
8908+
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8909+
let monitor = match chan.commitment_signed_initial_v2(msg, best_block, &self.signer_provider, &&logger) {
8910+
Ok(monitor) => monitor,
8911+
Err(err) => return Err(convert_chan_phase_err!(self, peer_state, err, &mut ChannelPhase::Funded(chan), &channel_id).1),
8912+
};
8913+
let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
8914+
if let Ok(persist_state) = monitor_res {
8915+
let mut occupied_entry = peer_state.channel_by_id.entry(channel_id).insert(ChannelPhase::Funded(chan));
8916+
let channel_phase_entry = occupied_entry.get_mut();
8917+
match channel_phase_entry {
8918+
ChannelPhase::Funded(chan) => { handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
8919+
per_peer_state, chan, INITIAL_MONITOR); },
8920+
channel_phase => {
8921+
debug_assert!(false, "Expected a ChannelPhase::Funded");
8922+
let err = ChannelError::close(
8923+
"Closing due to unexpected sender error".into());
8924+
return Err(convert_chan_phase_err!(self, peer_state, err, channel_phase,
8925+
&channel_id).1)
8926+
},
8927+
}
8928+
} else {
8929+
log_error!(logger, "Persisting initial ChannelMonitor failed, implying the funding outpoint was duplicated");
8930+
let err = ChannelError::Close(
8931+
(
8932+
"Channel funding outpoint was a duplicate".to_owned(),
8933+
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
8934+
)
8935+
);
8936+
return Err(convert_chan_phase_err!(self, peer_state, err, &mut ChannelPhase::Funded(chan), &channel_id).1);
88828937
}
8938+
Ok(())
88838939
}
88848940

88858941
fn push_decode_update_add_htlcs(&self, mut update_add_htlcs: (u64, Vec<msgs::UpdateAddHTLC>)) {

0 commit comments

Comments
 (0)