Skip to content

Commit b9d0475

Browse files
committed
f wip: handling async monitor persist and checks for tx_signatures
1 parent 9ad6f28 commit b9d0475

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

lightning/src/ln/channel.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@ pub(super) struct MonitorRestoreUpdates {
902902
pub funding_broadcastable: Option<Transaction>,
903903
pub channel_ready: Option<msgs::ChannelReady>,
904904
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
905+
pub tx_signatures: Option<msgs::TxSignatures>,
905906
}
906907

907908
/// The return value of `signer_maybe_unblocked`
@@ -1253,6 +1254,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
12531254
monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
12541255
monitor_pending_finalized_fulfills: Vec<HTLCSource>,
12551256
monitor_pending_update_adds: Vec<msgs::UpdateAddHTLC>,
1257+
monitor_pending_tx_signatures: Option<msgs::TxSignatures>,
12561258

12571259
/// If we went to send a revoke_and_ack but our signer was unable to give us a signature,
12581260
/// we should retry at some point in the future when the signer indicates it may have a
@@ -2092,6 +2094,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20922094
monitor_pending_failures: Vec::new(),
20932095
monitor_pending_finalized_fulfills: Vec::new(),
20942096
monitor_pending_update_adds: Vec::new(),
2097+
monitor_pending_tx_signatures: None,
20952098

20962099
signer_pending_revoke_and_ack: false,
20972100
signer_pending_commitment_update: false,
@@ -2328,6 +2331,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23282331
monitor_pending_failures: Vec::new(),
23292332
monitor_pending_finalized_fulfills: Vec::new(),
23302333
monitor_pending_update_adds: Vec::new(),
2334+
monitor_pending_tx_signatures: None,
23312335

23322336
signer_pending_revoke_and_ack: false,
23332337
signer_pending_commitment_update: false,
@@ -5663,7 +5667,13 @@ impl<SP: Deref> Channel<SP> where
56635667
}
56645668
}
56655669

5666-
pub fn tx_signatures(&mut self, msg: &msgs::TxSignatures) -> Result<(Option<msgs::TxSignatures>, Option<Transaction>), ChannelError> {
5670+
pub fn tx_signatures<L: Deref>(&mut self, msg: &msgs::TxSignatures, logger: &L) -> Result<(Option<msgs::TxSignatures>, Option<Transaction>), ChannelError>
5671+
where L::Target: Logger
5672+
{
5673+
if !matches!(self.context.channel_state, ChannelState::FundingNegotiated) {
5674+
return Err(ChannelError::close("Received tx_signatures in strange state!".to_owned()));
5675+
}
5676+
56675677
if let Some(ref mut signing_session) = self.interactive_tx_signing_session {
56685678
if msg.witnesses.len() != signing_session.remote_inputs_count() {
56695679
return Err(ChannelError::Close(
@@ -5707,13 +5717,18 @@ impl<SP: Deref> Channel<SP> where
57075717
// Clear out the signing session
57085718
self.interactive_tx_signing_session = None;
57095719

5720+
if tx_signatures_opt.is_some() && self.context.channel_state.is_monitor_update_in_progress() {
5721+
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
5722+
self.context.monitor_pending_tx_signatures = tx_signatures_opt;
5723+
return Ok((None, None));
5724+
}
5725+
57105726
Ok((tx_signatures_opt, funding_tx_opt))
57115727
} else {
5712-
return Err(ChannelError::Close(
5713-
(
5728+
Err(ChannelError::Close((
57145729
"Unexpected tx_signatures. No funding transaction awaiting signatures".to_string(),
57155730
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
5716-
)));
5731+
)))
57175732
}
57185733
}
57195734

@@ -5950,14 +5965,18 @@ impl<SP: Deref> Channel<SP> where
59505965
mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
59515966
let mut pending_update_adds = Vec::new();
59525967
mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
5968+
// For channels established with V2 establishment we won't send a `tx_signatures` when we're in
5969+
// MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
5970+
// transaction and waits for us to do it).
5971+
let tx_signatures = self.context.monitor_pending_tx_signatures.take();
59535972

59545973
if self.context.channel_state.is_peer_disconnected() {
59555974
self.context.monitor_pending_revoke_and_ack = false;
59565975
self.context.monitor_pending_commitment_signed = false;
59575976
return MonitorRestoreUpdates {
59585977
raa: None, commitment_update: None, order: RAACommitmentOrder::RevokeAndACKFirst,
59595978
accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, pending_update_adds,
5960-
funding_broadcastable, channel_ready, announcement_sigs
5979+
funding_broadcastable, channel_ready, announcement_sigs, tx_signatures
59615980
};
59625981
}
59635982

@@ -5991,7 +6010,7 @@ impl<SP: Deref> Channel<SP> where
59916010
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
59926011
MonitorRestoreUpdates {
59936012
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs,
5994-
pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs
6013+
pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs, tx_signatures
59956014
}
59966015
}
59976016

@@ -9989,6 +10008,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
998910008
monitor_pending_failures,
999010009
monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
999110010
monitor_pending_update_adds: monitor_pending_update_adds.unwrap_or_default(),
10011+
monitor_pending_tx_signatures: None,
999210012

999310013
signer_pending_revoke_and_ack: false,
999410014
signer_pending_commitment_update: false,

lightning/src/ln/channelmanager.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,7 +3108,7 @@ macro_rules! handle_monitor_update_completion {
31083108
&mut $peer_state.pending_msg_events, $chan, updates.raa,
31093109
updates.commitment_update, updates.order, updates.accepted_htlcs, updates.pending_update_adds,
31103110
updates.funding_broadcastable, updates.channel_ready,
3111-
updates.announcement_sigs);
3111+
updates.announcement_sigs, updates.tx_signatures);
31123112
if let Some(upd) = channel_update {
31133113
$peer_state.pending_msg_events.push(upd);
31143114
}
@@ -7415,17 +7415,20 @@ where
74157415
commitment_update: Option<msgs::CommitmentUpdate>, order: RAACommitmentOrder,
74167416
pending_forwards: Vec<(PendingHTLCInfo, u64)>, pending_update_adds: Vec<msgs::UpdateAddHTLC>,
74177417
funding_broadcastable: Option<Transaction>,
7418-
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>)
7419-
-> (Option<(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
7418+
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>,
7419+
tx_signatures: Option<msgs::TxSignatures>
7420+
) -> (Option<(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
74207421
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
7421-
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement",
7422+
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement, {} tx_signatures",
74227423
&channel.context.channel_id(),
74237424
if raa.is_some() { "an" } else { "no" },
74247425
if commitment_update.is_some() { "a" } else { "no" },
74257426
pending_forwards.len(), pending_update_adds.len(),
74267427
if funding_broadcastable.is_some() { "" } else { "not " },
74277428
if channel_ready.is_some() { "sending" } else { "without" },
7428-
if announcement_sigs.is_some() { "sending" } else { "without" });
7429+
if announcement_sigs.is_some() { "sending" } else { "without" },
7430+
if tx_signatures.is_some() { "sending" } else { "without" },
7431+
);
74297432

74307433
let counterparty_node_id = channel.context.get_counterparty_node_id();
74317434
let short_channel_id = channel.context.get_short_channel_id().unwrap_or(channel.context.outbound_scid_alias());
@@ -7452,6 +7455,12 @@ where
74527455
msg,
74537456
});
74547457
}
7458+
if let Some(msg) = tx_signatures {
7459+
pending_msg_events.push(events::MessageSendEvent::SendTxSignatures {
7460+
node_id: counterparty_node_id,
7461+
msg,
7462+
});
7463+
}
74557464

74567465
macro_rules! handle_cs { () => {
74577466
if let Some(update) = commitment_update {
@@ -8343,7 +8352,8 @@ where
83438352
let channel_phase = chan_phase_entry.get_mut();
83448353
match channel_phase {
83458354
ChannelPhase::Funded(chan) => {
8346-
let (tx_signatures_opt, funding_tx_opt) = try_chan_phase_entry!(self, chan.tx_signatures(msg), chan_phase_entry);
8355+
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8356+
let (tx_signatures_opt, funding_tx_opt) = try_chan_phase_entry!(self, chan.tx_signatures(msg, &&logger), chan_phase_entry);
83478357
if let Some(tx_signatures) = tx_signatures_opt {
83488358
peer_state.pending_msg_events.push(events::MessageSendEvent::SendTxSignatures {
83498359
node_id: *counterparty_node_id,
@@ -9221,7 +9231,7 @@ where
92219231
let need_lnd_workaround = chan.context.workaround_lnd_bug_4006.take();
92229232
let (htlc_forwards, decode_update_add_htlcs) = self.handle_channel_resumption(
92239233
&mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.order,
9224-
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs);
9234+
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs, None);
92259235
debug_assert!(htlc_forwards.is_none());
92269236
debug_assert!(decode_update_add_htlcs.is_none());
92279237
if let Some(upd) = channel_update {

0 commit comments

Comments
 (0)