Skip to content

Commit 0e3f6b6

Browse files
TheBlueMattwaterson
authored andcommitted
Handle sign_counterparty_commitment failing during inb funding
If sign_counterparty_commitment fails (i.e. because the signer is temporarily disconnected), this really indicates that we should retry the message sending which required the signature later, rather than force-closing the channel (which probably won't even work if the signer is missing). Here we add initial handling of sign_counterparty_commitment failing during inbound channel funding, setting a flag in `ChannelContext` which indicates we should retry sending the `funding_signed` later. We don't yet add any ability to do that retry.
1 parent d86f73b commit 0e3f6b6

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

lightning/src/ln/channel.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6687,7 +6687,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
66876687
self.generate_accept_channel_message()
66886688
}
66896689

6690-
fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(CommitmentTransaction, CommitmentTransaction, Signature), ChannelError> where L::Target: Logger {
6690+
fn funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<(CommitmentTransaction, CommitmentTransaction, Option<Signature>), ChannelError> where L::Target: Logger {
66916691
let funding_script = self.context.get_funding_redeemscript();
66926692

66936693
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
@@ -6716,7 +6716,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
67166716
// TODO (arik): move match into calling method for Taproot
67176717
ChannelSignerType::Ecdsa(ecdsa) => {
67186718
let counterparty_signature = ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
6719-
.map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0;
6719+
.map(|(sig, _)| sig).ok();
67206720

67216721
// We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
67226722
Ok((counterparty_initial_commitment_tx, initial_commitment_tx, counterparty_signature))
@@ -6726,7 +6726,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
67266726

67276727
pub fn funding_created<L: Deref>(
67286728
mut self, msg: &msgs::FundingCreated, best_block: BestBlock, signer_provider: &SP, logger: &L
6729-
) -> Result<(Channel<SP>, msgs::FundingSigned, ChannelMonitor<<SP::Target as SignerProvider>::Signer>), (Self, ChannelError)>
6729+
) -> Result<(Channel<SP>, Option<msgs::FundingSigned>, ChannelMonitor<<SP::Target as SignerProvider>::Signer>), (Self, ChannelError)>
67306730
where
67316731
L::Target: Logger
67326732
{
@@ -6751,7 +6751,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
67516751
// funding_created_signature may fail.
67526752
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
67536753

6754-
let (counterparty_initial_commitment_tx, initial_commitment_tx, signature) = match self.funding_created_signature(&msg.signature, logger) {
6754+
let (counterparty_initial_commitment_tx, initial_commitment_tx, sig_opt) = match self.funding_created_signature(&msg.signature, logger) {
67556755
Ok(res) => res,
67566756
Err(ChannelError::Close(e)) => {
67576757
self.context.channel_transaction_parameters.funding_outpoint = None;
@@ -6815,12 +6815,19 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
68156815
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
68166816
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
68176817

6818-
Ok((channel, msgs::FundingSigned {
6819-
channel_id,
6820-
signature,
6821-
#[cfg(taproot)]
6822-
partial_signature_with_nonce: None,
6823-
}, channel_monitor))
6818+
let funding_signed = if let Some(signature) = sig_opt {
6819+
Some(msgs::FundingSigned {
6820+
channel_id,
6821+
signature,
6822+
#[cfg(taproot)]
6823+
partial_signature_with_nonce: None,
6824+
})
6825+
} else {
6826+
channel.context.signer_pending_funding = true;
6827+
None
6828+
};
6829+
6830+
Ok((channel, funding_signed, channel_monitor))
68246831
}
68256832
}
68266833

@@ -7908,7 +7915,7 @@ mod tests {
79087915
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg.unwrap(), best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
79097916

79107917
// Node B --> Node A: funding signed
7911-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7918+
let _ = node_a_chan.funding_signed(&funding_signed_msg.unwrap(), best_block, &&keys_provider, &&logger).unwrap();
79127919

79137920
// Put some inbound and outbound HTLCs in A's channel.
79147921
let htlc_amount_msat = 11_092_000; // put an amount below A's effective dust limit but above B's.
@@ -8035,7 +8042,7 @@ mod tests {
80358042
let (mut node_b_chan, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg.unwrap(), best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
80368043

80378044
// Node B --> Node A: funding signed
8038-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
8045+
let _ = node_a_chan.funding_signed(&funding_signed_msg.unwrap(), best_block, &&keys_provider, &&logger).unwrap();
80398046

80408047
// Now disconnect the two nodes and check that the commitment point in
80418048
// Node B's channel_reestablish message is sane.
@@ -8223,7 +8230,7 @@ mod tests {
82238230
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg.unwrap(), best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
82248231

82258232
// Node B --> Node A: funding signed
8226-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
8233+
let _ = node_a_chan.funding_signed(&funding_signed_msg.unwrap(), best_block, &&keys_provider, &&logger).unwrap();
82278234

82288235
// Make sure that receiving a channel update will update the Channel as expected.
82298236
let update = ChannelUpdate {
@@ -9308,7 +9315,7 @@ mod tests {
93089315
// Receive funding_signed, but the channel will be configured to hold sending channel_ready and
93099316
// broadcasting the funding transaction until the batch is ready.
93109317
let _ = node_a_chan.funding_signed(
9311-
&funding_signed_msg,
9318+
&funding_signed_msg.unwrap(),
93129319
best_block,
93139320
&&keys_provider,
93149321
&&logger,

lightning/src/ln/channelmanager.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6231,7 +6231,7 @@ where
62316231

62326232
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
62336233
let peer_state = &mut *peer_state_lock;
6234-
let (chan, funding_msg, monitor) =
6234+
let (chan, funding_msg_opt, monitor) =
62356235
match peer_state.channel_by_id.remove(&msg.temporary_channel_id) {
62366236
Some(ChannelPhase::UnfundedInboundV1(inbound_chan)) => {
62376237
match inbound_chan.funding_created(msg, best_block, &self.signer_provider, &self.logger) {
@@ -6254,17 +6254,20 @@ where
62546254
None => return 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.temporary_channel_id))
62556255
};
62566256

6257-
match peer_state.channel_by_id.entry(funding_msg.channel_id) {
6257+
match peer_state.channel_by_id.entry(chan.context.channel_id()) {
62586258
hash_map::Entry::Occupied(_) => {
6259-
Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id".to_owned(), funding_msg.channel_id))
6259+
Err(MsgHandleErrInternal::send_err_msg_no_close(
6260+
"Already had channel with the new channel_id".to_owned(),
6261+
chan.context.channel_id()
6262+
))
62606263
},
62616264
hash_map::Entry::Vacant(e) => {
62626265
let mut id_to_peer_lock = self.id_to_peer.lock().unwrap();
62636266
match id_to_peer_lock.entry(chan.context.channel_id()) {
62646267
hash_map::Entry::Occupied(_) => {
62656268
return Err(MsgHandleErrInternal::send_err_msg_no_close(
62666269
"The funding_created message had the same funding_txid as an existing channel - funding is not possible".to_owned(),
6267-
funding_msg.channel_id))
6270+
chan.context.channel_id()))
62686271
},
62696272
hash_map::Entry::Vacant(i_e) => {
62706273
let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
@@ -6276,10 +6279,12 @@ where
62766279
// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
62776280
// accepted payment from yet. We do, however, need to wait to send our channel_ready
62786281
// until we have persisted our monitor.
6279-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
6280-
node_id: counterparty_node_id.clone(),
6281-
msg: funding_msg,
6282-
});
6282+
if let Some(msg) = funding_msg_opt {
6283+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
6284+
node_id: counterparty_node_id.clone(),
6285+
msg,
6286+
});
6287+
}
62836288

62846289
if let ChannelPhase::Funded(chan) = e.insert(ChannelPhase::Funded(chan)) {
62856290
handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
@@ -6290,9 +6295,13 @@ where
62906295
Ok(())
62916296
} else {
62926297
log_error!(self.logger, "Persisting initial ChannelMonitor failed, implying the funding outpoint was duplicated");
6298+
let channel_id = match funding_msg_opt {
6299+
Some(msg) => msg.channel_id,
6300+
None => chan.context.channel_id(),
6301+
};
62936302
return Err(MsgHandleErrInternal::send_err_msg_no_close(
62946303
"The funding_created message had the same funding_txid as an existing channel - funding is not possible".to_owned(),
6295-
funding_msg.channel_id));
6304+
channel_id));
62966305
}
62976306
}
62986307
}

0 commit comments

Comments
 (0)