Skip to content

Commit 8ac4804

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 baac0a3 commit 8ac4804

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
@@ -6654,7 +6654,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
66546654
self.generate_accept_channel_message()
66556655
}
66566656

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

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

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

66946694
pub fn funding_created<L: Deref>(
66956695
mut self, msg: &msgs::FundingCreated, best_block: BestBlock, signer_provider: &SP, logger: &L
6696-
) -> Result<(Channel<SP>, msgs::FundingSigned, ChannelMonitor<<SP::Target as SignerProvider>::Signer>), (Self, ChannelError)>
6696+
) -> Result<(Channel<SP>, Option<msgs::FundingSigned>, ChannelMonitor<<SP::Target as SignerProvider>::Signer>), (Self, ChannelError)>
66976697
where
66986698
L::Target: Logger
66996699
{
@@ -6718,7 +6718,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
67186718
// funding_created_signature may fail.
67196719
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
67206720

6721-
let (counterparty_initial_commitment_tx, initial_commitment_tx, signature) = match self.funding_created_signature(&msg.signature, logger) {
6721+
let (counterparty_initial_commitment_tx, initial_commitment_tx, sig_opt) = match self.funding_created_signature(&msg.signature, logger) {
67226722
Ok(res) => res,
67236723
Err(ChannelError::Close(e)) => {
67246724
self.context.channel_transaction_parameters.funding_outpoint = None;
@@ -6782,12 +6782,19 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
67826782
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
67836783
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
67846784

6785-
Ok((channel, msgs::FundingSigned {
6786-
channel_id,
6787-
signature,
6788-
#[cfg(taproot)]
6789-
partial_signature_with_nonce: None,
6790-
}, channel_monitor))
6785+
let funding_signed = if let Some(signature) = sig_opt {
6786+
Some(msgs::FundingSigned {
6787+
channel_id,
6788+
signature,
6789+
#[cfg(taproot)]
6790+
partial_signature_with_nonce: None,
6791+
})
6792+
} else {
6793+
channel.context.signer_pending_funding = true;
6794+
None
6795+
};
6796+
6797+
Ok((channel, funding_signed, channel_monitor))
67916798
}
67926799
}
67936800

@@ -7875,7 +7882,7 @@ mod tests {
78757882
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg.unwrap(), best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
78767883

78777884
// Node B --> Node A: funding signed
7878-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
7885+
let _ = node_a_chan.funding_signed(&funding_signed_msg.unwrap(), best_block, &&keys_provider, &&logger).unwrap();
78797886

78807887
// Put some inbound and outbound HTLCs in A's channel.
78817888
let htlc_amount_msat = 11_092_000; // put an amount below A's effective dust limit but above B's.
@@ -8002,7 +8009,7 @@ mod tests {
80028009
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();
80038010

80048011
// Node B --> Node A: funding signed
8005-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
8012+
let _ = node_a_chan.funding_signed(&funding_signed_msg.unwrap(), best_block, &&keys_provider, &&logger).unwrap();
80068013

80078014
// Now disconnect the two nodes and check that the commitment point in
80088015
// Node B's channel_reestablish message is sane.
@@ -8190,7 +8197,7 @@ mod tests {
81908197
let (_, funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg.unwrap(), best_block, &&keys_provider, &&logger).map_err(|_| ()).unwrap();
81918198

81928199
// Node B --> Node A: funding signed
8193-
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger).unwrap();
8200+
let _ = node_a_chan.funding_signed(&funding_signed_msg.unwrap(), best_block, &&keys_provider, &&logger).unwrap();
81948201

81958202
// Make sure that receiving a channel update will update the Channel as expected.
81968203
let update = ChannelUpdate {
@@ -9275,7 +9282,7 @@ mod tests {
92759282
// Receive funding_signed, but the channel will be configured to hold sending channel_ready and
92769283
// broadcasting the funding transaction until the batch is ready.
92779284
let _ = node_a_chan.funding_signed(
9278-
&funding_signed_msg,
9285+
&funding_signed_msg.unwrap(),
92799286
best_block,
92809287
&&keys_provider,
92819288
&&logger,

lightning/src/ln/channelmanager.rs

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

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

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

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

0 commit comments

Comments
 (0)