Skip to content

Commit ce2cb42

Browse files
committed
f: move needs persist determination earlier
1 parent 2308073 commit ce2cb42

1 file changed

Lines changed: 15 additions & 26 deletions

File tree

lightning/src/ln/channelmanager.rs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ enum PostMonitorUpdateChanResume {
16011601
Blocked { update_actions: Vec<MonitorUpdateCompletionAction> },
16021602
/// Channel was fully unblocked and has been resumed. Contains remaining data to process.
16031603
Unblocked {
1604-
has_state_changes: bool,
1604+
needs_persist: bool,
16051605
channel_id: ChannelId,
16061606
counterparty_node_id: PublicKey,
16071607
funding_txo: OutPoint,
@@ -10281,23 +10281,17 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1028110281

1028210282
/// Handles actions which need to complete after a [`ChannelMonitorUpdate`] has been applied
1028310283
/// which can happen after the per-peer state lock has been dropped.
10284-
///
10285-
/// Returns whether the completed work mutated `ChannelManager` state in a way that should be
10286-
/// persisted before returning control to the caller.
1028710284
fn post_monitor_update_unlock(
1028810285
&self, channel_id: ChannelId, counterparty_node_id: PublicKey, funding_txo: OutPoint,
1028910286
user_channel_id: u128, unbroadcasted_batch_funding_txid: Option<Txid>,
1029010287
update_actions: Vec<MonitorUpdateCompletionAction>, htlc_forwards: Vec<PendingAddHTLCInfo>,
1029110288
finalized_claimed_htlcs: Vec<(HTLCSource, Option<AttributionData>)>,
1029210289
failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
1029310290
committed_outbound_htlc_sources: Vec<(HTLCPreviousHopData, u64)>,
10294-
) -> bool {
10295-
let mut needs_persist = false;
10296-
10291+
) {
1029710292
// If the channel belongs to a batch funding transaction, the progress of the batch
1029810293
// should be updated as we have received funding_signed and persisted the monitor.
1029910294
if let Some(txid) = unbroadcasted_batch_funding_txid {
10300-
needs_persist = true;
1030110295
let mut funding_batch_states = self.funding_batch_states.lock().unwrap();
1030210296
let mut batch_completed = false;
1030310297
if let Some(batch_state) = funding_batch_states.get_mut(&txid) {
@@ -10348,14 +10342,6 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1034810342
}
1034910343
}
1035010344

10351-
if !update_actions.is_empty()
10352-
|| !htlc_forwards.is_empty()
10353-
|| !finalized_claimed_htlcs.is_empty()
10354-
|| !failed_htlcs.is_empty()
10355-
|| !committed_outbound_htlc_sources.is_empty()
10356-
{
10357-
needs_persist = true;
10358-
}
1035910345
self.handle_monitor_update_completion_actions(update_actions);
1036010346

1036110347
self.forward_htlcs(htlc_forwards);
@@ -10377,8 +10363,6 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1037710363
user_channel_id,
1037810364
committed_outbound_htlc_sources,
1037910365
);
10380-
10381-
needs_persist
1038210366
}
1038310367

1038410368
fn handle_monitor_update_completion_actions<
@@ -10770,10 +10754,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1077010754
/// after locks are released. If blocked updates remain, only the update actions are returned
1077110755
/// and the caller should persist if any are present.
1077210756
///
10773-
/// This method only prepares the post-monitor-update work while locks are held. Any
10774-
/// persistence decision for the unblocked case is deferred until
10775-
/// [`Self::handle_post_monitor_update_chan_resume`] executes the returned work after locks are
10776-
/// released.
10757+
/// This method also determines whether the prepared work mutates `ChannelManager` state in a
10758+
/// way that should be persisted before returning control to the caller.
1077710759
///
1077810760
/// Note: This method takes individual fields from [`PeerState`] rather than the whole struct
1077910761
/// to avoid borrow checker issues when the channel is borrowed from `peer_state.channel_by_id`.
@@ -10867,9 +10849,16 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1086710849

1086810850
let unbroadcasted_batch_funding_txid =
1086910851
chan.context.unbroadcasted_batch_funding_txid(&chan.funding);
10852+
let needs_persist = has_state_changes
10853+
|| !update_actions.is_empty()
10854+
|| unbroadcasted_batch_funding_txid.is_some()
10855+
|| !htlc_forwards.is_empty()
10856+
|| !updates.finalized_claimed_htlcs.is_empty()
10857+
|| !updates.failed_htlcs.is_empty()
10858+
|| !updates.committed_outbound_htlc_sources.is_empty();
1087010859

1087110860
PostMonitorUpdateChanResume::Unblocked {
10872-
has_state_changes,
10861+
needs_persist,
1087310862
channel_id: chan_id,
1087410863
counterparty_node_id,
1087510864
funding_txo: chan.funding_outpoint(),
@@ -10978,7 +10967,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1097810967
needs_persist
1097910968
},
1098010969
PostMonitorUpdateChanResume::Unblocked {
10981-
has_state_changes,
10970+
needs_persist,
1098210971
channel_id,
1098310972
counterparty_node_id,
1098410973
funding_txo,
@@ -10990,7 +10979,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1099010979
failed_htlcs,
1099110980
committed_outbound_htlc_sources,
1099210981
} => {
10993-
let post_unlock_persist = self.post_monitor_update_unlock(
10982+
self.post_monitor_update_unlock(
1099410983
channel_id,
1099510984
counterparty_node_id,
1099610985
funding_txo,
@@ -11002,7 +10991,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1100210991
failed_htlcs,
1100310992
committed_outbound_htlc_sources,
1100410993
);
11005-
has_state_changes || post_unlock_persist
10994+
needs_persist
1100610995
},
1100710996
}
1100810997
}

0 commit comments

Comments
 (0)