Skip to content

Commit 4269120

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 624cfd0 commit 4269120

File tree

2 files changed

+106
-48
lines changed

2 files changed

+106
-48
lines changed

lightning/src/ln/channel.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8746,6 +8746,8 @@ pub(super) struct OutboundV2Channel<SP: Deref> where SP::Target: SignerProvider
87468746
pub dual_funding_context: DualFundingChannelContext,
87478747
/// The current interactive transaction construction session under negotiation.
87488748
interactive_tx_constructor: Option<InteractiveTxConstructor>,
8749+
/// The signing session created after `tx_complete` handling
8750+
pub interactive_tx_signing_session: Option<InteractiveTxSigningSession>,
87498751
}
87508752

87518753
impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
@@ -8805,6 +8807,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
88058807
our_funding_inputs: funding_inputs,
88068808
},
88078809
interactive_tx_constructor: None,
8810+
interactive_tx_signing_session: None,
88088811
};
88098812
Ok(chan)
88108813
}
@@ -8872,13 +8875,11 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
88728875
}
88738876
}
88748877

8875-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<Channel<SP>, ChannelError>{
8876-
let channel = Channel {
8878+
pub fn into_channel(self) -> Channel<SP> {
8879+
Channel {
88778880
context: self.context,
8878-
interactive_tx_signing_session: Some(signing_session),
8879-
};
8880-
8881-
Ok(channel)
8881+
interactive_tx_signing_session: self.interactive_tx_signing_session,
8882+
}
88828883
}
88838884
}
88848885

@@ -8889,6 +8890,8 @@ pub(super) struct InboundV2Channel<SP: Deref> where SP::Target: SignerProvider {
88898890
pub dual_funding_context: DualFundingChannelContext,
88908891
/// The current interactive transaction construction session under negotiation.
88918892
interactive_tx_constructor: Option<InteractiveTxConstructor>,
8893+
/// The signing session created after `tx_complete` handling
8894+
pub interactive_tx_signing_session: Option<InteractiveTxSigningSession>,
88928895
}
88938896

88948897
impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
@@ -8990,6 +8993,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
89908993
context,
89918994
dual_funding_context,
89928995
interactive_tx_constructor,
8996+
interactive_tx_signing_session: None,
89938997
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
89948998
})
89958999
}
@@ -9066,13 +9070,11 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
90669070
self.generate_accept_channel_v2_message()
90679071
}
90689072

9069-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<Channel<SP>, ChannelError>{
9070-
let channel = Channel {
9073+
pub fn into_channel(self) -> Channel<SP> {
9074+
Channel {
90719075
context: self.context,
9072-
interactive_tx_signing_session: Some(signing_session),
9073-
};
9074-
9075-
Ok(channel)
9076+
interactive_tx_signing_session: self.interactive_tx_signing_session,
9077+
}
90769078
}
90779079
}
90789080

lightning/src/ln/channelmanager.rs

Lines changed: 92 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8246,7 +8246,7 @@ where
82468246
peer_state.pending_msg_events.push(msg_send_event);
82478247
};
82488248
if let Some(mut signing_session) = signing_session_opt {
8249-
let (commitment_signed, funding_ready_for_sig_event_opt) = match chan_phase_entry.get_mut() {
8249+
let (commitment_signed, funding_ready_for_sig_event_opt) = match channel_phase {
82508250
ChannelPhase::UnfundedOutboundV2(chan) => {
82518251
chan.funding_tx_constructed(&mut signing_session, &self.logger)
82528252
},
@@ -8257,18 +8257,17 @@ where
82578257
"Got a tx_complete message with no interactive transaction construction expected or in-progress"
82588258
.into())),
82598259
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8260-
let (channel_id, channel_phase) = chan_phase_entry.remove_entry();
8261-
let channel = match channel_phase {
8262-
ChannelPhase::UnfundedOutboundV2(chan) => chan.into_channel(signing_session),
8263-
ChannelPhase::UnfundedInboundV2(chan) => chan.into_channel(signing_session),
8260+
match channel_phase {
8261+
ChannelPhase::UnfundedOutboundV2(chan) => chan.interactive_tx_signing_session = Some(signing_session),
8262+
ChannelPhase::UnfundedInboundV2(chan) => chan.interactive_tx_signing_session = Some(signing_session),
82648263
_ => {
82658264
debug_assert!(false); // It cannot be another variant as we are in the `Ok` branch of the above match.
8266-
Err(ChannelError::Warn(
8265+
let err = ChannelError::Warn(
82678266
"Got a tx_complete message with no interactive transaction construction expected or in-progress"
8268-
.into()))
8267+
.into());
8268+
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id));
82698269
},
8270-
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8271-
peer_state.channel_by_id.insert(channel_id, ChannelPhase::Funded(channel));
8270+
}
82728271
if let Some(funding_ready_for_sig_event) = funding_ready_for_sig_event_opt {
82738272
let mut pending_events = self.pending_events.lock().unwrap();
82748273
pending_events.push_back((funding_ready_for_sig_event, None));
@@ -8781,46 +8780,103 @@ where
87818780
})?;
87828781
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
87838782
let peer_state = &mut *peer_state_lock;
8784-
match peer_state.channel_by_id.entry(msg.channel_id) {
8783+
let (channel_id, mut chan) = match peer_state.channel_by_id.entry(msg.channel_id) {
87858784
hash_map::Entry::Occupied(mut chan_phase_entry) => {
8786-
if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
8787-
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8788-
let funding_txo = chan.context.get_funding_txo();
8789-
8790-
if chan.interactive_tx_signing_session.is_some() {
8791-
let monitor = try_chan_phase_entry!(
8792-
self, peer_state, chan.commitment_signed_initial_v2(msg, best_block, &self.signer_provider, &&logger),
8793-
chan_phase_entry);
8794-
let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
8795-
if let Ok(persist_state) = monitor_res {
8796-
handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
8797-
per_peer_state, chan, INITIAL_MONITOR);
8785+
let channel_phase = chan_phase_entry.get_mut();
8786+
match channel_phase {
8787+
ChannelPhase::UnfundedOutboundV2(chan) => {
8788+
if chan.interactive_tx_signing_session.is_some() {
8789+
let (channel_id, mut channel_phase) = chan_phase_entry.remove_entry();
8790+
match channel_phase {
8791+
ChannelPhase::UnfundedOutboundV2(chan) => {
8792+
(channel_id, chan.into_channel())
8793+
}
8794+
_ => {
8795+
debug_assert!(false, "The channel phase was not UnfundedOutboundV2");
8796+
let err = ChannelError::close(
8797+
"Closing due to unexpected sender error".into());
8798+
return Err(convert_chan_phase_err!(self, peer_state, err, &mut channel_phase,
8799+
&channel_id).1)
8800+
}
8801+
}
87988802
} else {
8799-
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8800-
log_error!(logger, "Persisting initial ChannelMonitor failed, implying the funding outpoint was duplicated");
8801-
try_chan_phase_entry!(self, peer_state, Err(ChannelError::Close(
8802-
(
8803-
"Channel funding outpoint was a duplicate".to_owned(),
8804-
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
8805-
)
8806-
)), chan_phase_entry)
8803+
return try_chan_phase_entry!(self, peer_state, Err(ChannelError::close(
8804+
"Got a commitment_signed message for a V2 channel before funding transaction constructed!".into())), chan_phase_entry);
88078805
}
8808-
} else {
8806+
},
8807+
ChannelPhase::UnfundedInboundV2(chan) => {
8808+
// TODO(dual_funding): This should be somewhat DRYable when #3418 is merged.
8809+
if chan.interactive_tx_signing_session.is_some() {
8810+
let (channel_id, mut channel_phase) = chan_phase_entry.remove_entry();
8811+
match channel_phase {
8812+
ChannelPhase::UnfundedInboundV2(chan) => {
8813+
(channel_id, chan.into_channel())
8814+
}
8815+
_ => {
8816+
debug_assert!(false, "The channel phase was not UnfundedInboundV2");
8817+
let err = ChannelError::close(
8818+
"Closing due to unexpected sender error".into());
8819+
return Err(convert_chan_phase_err!(self, peer_state, err, &mut channel_phase,
8820+
&channel_id).1)
8821+
}
8822+
}
8823+
} else {
8824+
return try_chan_phase_entry!(self, peer_state, Err(ChannelError::close(
8825+
"Got a commitment_signed message for a V2 channel before funding transaction constructed!".into())), chan_phase_entry);
8826+
}
8827+
},
8828+
ChannelPhase::Funded(chan) => {
8829+
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8830+
let funding_txo = chan.context.get_funding_txo();
88098831
let monitor_update_opt = try_chan_phase_entry!(
88108832
self, peer_state, chan.commitment_signed(msg, &&logger), chan_phase_entry);
88118833
if let Some(monitor_update) = monitor_update_opt {
88128834
handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update, peer_state_lock,
88138835
peer_state, per_peer_state, chan);
88148836
}
8837+
return Ok(())
8838+
},
8839+
_ => {
8840+
return try_chan_phase_entry!(self, peer_state, Err(ChannelError::close(
8841+
"Got a commitment_signed message for an unfunded channel!".into())), chan_phase_entry);
88158842
}
8816-
Ok(())
8817-
} else {
8818-
return try_chan_phase_entry!(self, peer_state, Err(ChannelError::close(
8819-
"Got a commitment_signed message for an unfunded channel!".into())), chan_phase_entry);
88208843
}
88218844
},
8822-
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))
8845+
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(
8846+
format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
8847+
counterparty_node_id), msg.channel_id))
8848+
};
8849+
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8850+
let monitor = match chan.commitment_signed_initial_v2(msg, best_block, &self.signer_provider, &&logger) {
8851+
Ok(monitor) => monitor,
8852+
Err(err) => return Err(convert_chan_phase_err!(self, peer_state, err, &mut ChannelPhase::Funded(chan), &channel_id).1),
8853+
};
8854+
let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
8855+
if let Ok(persist_state) = monitor_res {
8856+
let mut occupied_entry = peer_state.channel_by_id.entry(channel_id).insert(ChannelPhase::Funded(chan));
8857+
let channel_phase_entry = occupied_entry.get_mut();
8858+
match channel_phase_entry {
8859+
ChannelPhase::Funded(chan) => { handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
8860+
per_peer_state, chan, INITIAL_MONITOR); },
8861+
channel_phase => {
8862+
debug_assert!(false, "Expected a ChannelPhase::Funded");
8863+
let err = ChannelError::close(
8864+
"Closing due to unexpected sender error".into());
8865+
return Err(convert_chan_phase_err!(self, peer_state, err, channel_phase,
8866+
&channel_id).1)
8867+
},
8868+
}
8869+
} else {
8870+
log_error!(logger, "Persisting initial ChannelMonitor failed, implying the funding outpoint was duplicated");
8871+
let err = ChannelError::Close(
8872+
(
8873+
"Channel funding outpoint was a duplicate".to_owned(),
8874+
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
8875+
)
8876+
);
8877+
return Err(convert_chan_phase_err!(self, peer_state, err, &mut ChannelPhase::Funded(chan), &channel_id).1);
88238878
}
8879+
Ok(())
88248880
}
88258881

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

0 commit comments

Comments
 (0)