Skip to content

Commit da3632b

Browse files
Add helper to push monitor events
Cleans up the next commit
1 parent f76f166 commit da3632b

2 files changed

Lines changed: 32 additions & 47 deletions

File tree

lightning/src/chain/chainmonitor.rs

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,6 @@ pub struct ChainMonitor<
366366
fee_estimator: F,
367367
persister: P,
368368
_entropy_source: ES,
369-
/// "User-provided" (ie persistence-completion/-failed) [`MonitorEvent`]s. These came directly
370-
/// from the user and not from a [`ChannelMonitor`].
371-
pending_monitor_events: Mutex<Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, PublicKey)>>,
372369
/// The best block height seen, used as a proxy for the passage of time.
373370
highest_chain_height: AtomicUsize,
374371

@@ -436,7 +433,6 @@ where
436433
logger,
437434
fee_estimator: feeest,
438435
_entropy_source,
439-
pending_monitor_events: Mutex::new(Vec::new()),
440436
highest_chain_height: AtomicUsize::new(0),
441437
event_notifier: Arc::clone(&event_notifier),
442438
persister: AsyncPersister { persister, event_notifier },
@@ -657,7 +653,6 @@ where
657653
fee_estimator: feeest,
658654
persister,
659655
_entropy_source,
660-
pending_monitor_events: Mutex::new(Vec::new()),
661656
highest_chain_height: AtomicUsize::new(0),
662657
event_notifier: Arc::new(Notifier::new()),
663658
pending_send_only_events: Mutex::new(Vec::new()),
@@ -802,16 +797,11 @@ where
802797
return Ok(());
803798
}
804799
let funding_txo = monitor_data.monitor.get_funding_txo();
805-
self.pending_monitor_events.lock().unwrap().push((
800+
monitor_data.monitor.push_monitor_event(MonitorEvent::Completed {
806801
funding_txo,
807802
channel_id,
808-
vec![MonitorEvent::Completed {
809-
funding_txo,
810-
channel_id,
811-
monitor_update_id: monitor_data.monitor.get_latest_update_id(),
812-
}],
813-
monitor_data.monitor.get_counterparty_node_id(),
814-
));
803+
monitor_update_id: monitor_data.monitor.get_latest_update_id(),
804+
});
815805

816806
self.event_notifier.notify();
817807
Ok(())
@@ -824,14 +814,11 @@ where
824814
pub fn force_channel_monitor_updated(&self, channel_id: ChannelId, monitor_update_id: u64) {
825815
let monitors = self.monitors.read().unwrap();
826816
let monitor = &monitors.get(&channel_id).unwrap().monitor;
827-
let counterparty_node_id = monitor.get_counterparty_node_id();
828-
let funding_txo = monitor.get_funding_txo();
829-
self.pending_monitor_events.lock().unwrap().push((
830-
funding_txo,
817+
monitor.push_monitor_event(MonitorEvent::Completed {
818+
funding_txo: monitor.get_funding_txo(),
831819
channel_id,
832-
vec![MonitorEvent::Completed { funding_txo, channel_id, monitor_update_id }],
833-
counterparty_node_id,
834-
));
820+
monitor_update_id,
821+
});
835822
self.event_notifier.notify();
836823
}
837824

@@ -1266,21 +1253,13 @@ where
12661253
// The channel is post-close (funding spend seen, lockdown, or
12671254
// holder tx signed). Return InProgress so ChannelManager freezes
12681255
// the channel until the force-close MonitorEvents are processed.
1269-
// Push a Completed event into pending_monitor_events so it gets
1270-
// picked up after the per-monitor events in the next
1271-
// release_pending_monitor_events call.
1272-
let funding_txo = monitor.get_funding_txo();
1273-
let channel_id = monitor.channel_id();
1274-
self.pending_monitor_events.lock().unwrap().push((
1275-
funding_txo,
1276-
channel_id,
1277-
vec![MonitorEvent::Completed {
1278-
funding_txo,
1279-
channel_id,
1280-
monitor_update_id: monitor.get_latest_update_id(),
1281-
}],
1282-
monitor.get_counterparty_node_id(),
1283-
));
1256+
// Push a Completed event into the monitor so it gets picked up
1257+
// in the next release_pending_monitor_events call.
1258+
monitor.push_monitor_event(MonitorEvent::Completed {
1259+
funding_txo: monitor.get_funding_txo(),
1260+
channel_id: monitor.channel_id(),
1261+
monitor_update_id: monitor.get_latest_update_id(),
1262+
});
12841263
log_debug!(
12851264
logger,
12861265
"Deferring completion of ChannelMonitorUpdate id {:?} (channel is post-close)",
@@ -1665,10 +1644,6 @@ where
16651644
));
16661645
}
16671646
}
1668-
// Drain pending_monitor_events (which includes deferred post-close
1669-
// completions) after per-monitor events so that force-close
1670-
// MonitorEvents are processed by ChannelManager first.
1671-
pending_monitor_events.extend(self.pending_monitor_events.lock().unwrap().split_off(0));
16721647
pending_monitor_events
16731648
}
16741649
}

lightning/src/chain/channelmonitor.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ impl Readable for ChannelMonitorUpdate {
183183
}
184184
}
185185

186+
fn push_monitor_event(pending_monitor_events: &mut Vec<MonitorEvent>, event: MonitorEvent) {
187+
pending_monitor_events.push(event);
188+
}
189+
186190
/// An event to be processed by the ChannelManager.
187191
#[derive(Clone, PartialEq, Eq)]
188192
pub enum MonitorEvent {
@@ -226,8 +230,6 @@ pub enum MonitorEvent {
226230
},
227231
}
228232
impl_writeable_tlv_based_enum_upgradable_legacy!(MonitorEvent,
229-
// Note that Completed is currently never serialized to disk as it is generated only in
230-
// ChainMonitor.
231233
(0, Completed) => {
232234
(0, funding_txo, required),
233235
(2, monitor_update_id, required),
@@ -2166,6 +2168,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
21662168
self.inner.lock().unwrap().get_and_clear_pending_monitor_events()
21672169
}
21682170

2171+
pub(super) fn push_monitor_event(&self, event: MonitorEvent) {
2172+
self.inner.lock().unwrap().push_monitor_event(event);
2173+
}
2174+
21692175
/// Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
21702176
///
21712177
/// For channels featuring anchor outputs, this method will also process [`BumpTransaction`]
@@ -3891,7 +3897,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
38913897
outpoint: funding_outpoint,
38923898
channel_id: self.channel_id,
38933899
};
3894-
self.pending_monitor_events.push(event);
3900+
push_monitor_event(&mut self.pending_monitor_events, event);
38953901
}
38963902

38973903
// Although we aren't signing the transaction directly here, the transaction will be signed
@@ -4552,6 +4558,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
45524558
&self.outputs_to_watch
45534559
}
45544560

4561+
fn push_monitor_event(&mut self, event: MonitorEvent) {
4562+
push_monitor_event(&mut self.pending_monitor_events, event);
4563+
}
4564+
45554565
fn get_and_clear_pending_monitor_events(&mut self) -> Vec<MonitorEvent> {
45564566
let mut ret = Vec::new();
45574567
mem::swap(&mut ret, &mut self.pending_monitor_events);
@@ -5611,7 +5621,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
56115621
);
56125622
log_info!(logger, "Channel closed by funding output spend in txid {txid}");
56135623
if !self.funding_spend_seen {
5614-
self.pending_monitor_events.push(MonitorEvent::CommitmentTxConfirmed(()));
5624+
self.push_monitor_event(MonitorEvent::CommitmentTxConfirmed(()));
56155625
}
56165626
self.funding_spend_seen = true;
56175627

@@ -5786,7 +5796,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
57865796

57875797
log_debug!(logger, "HTLC {} failure update in {} has got enough confirmations to be passed upstream",
57885798
&payment_hash, entry.txid);
5789-
self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
5799+
self.push_monitor_event(MonitorEvent::HTLCEvent(HTLCUpdate {
57905800
payment_hash,
57915801
payment_preimage: None,
57925802
source,
@@ -5896,7 +5906,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
58965906
log_error!(logger, "Failing back HTLC {} upstream to preserve the \
58975907
channel as the forward HTLC hasn't resolved and our backward HTLC \
58985908
expires soon at {}", log_bytes!(htlc.payment_hash.0), inbound_htlc_expiry);
5899-
self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
5909+
push_monitor_event(&mut self.pending_monitor_events, MonitorEvent::HTLCEvent(HTLCUpdate {
59005910
source: source.clone(),
59015911
payment_preimage: None,
59025912
payment_hash: htlc.payment_hash,
@@ -6313,7 +6323,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
63136323
},
63146324
});
63156325
self.counterparty_fulfilled_htlcs.insert(SentHTLCId::from_source(&source), payment_preimage);
6316-
self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
6326+
push_monitor_event(&mut self.pending_monitor_events, MonitorEvent::HTLCEvent(HTLCUpdate {
63176327
source,
63186328
payment_preimage: Some(payment_preimage),
63196329
payment_hash,
@@ -6337,7 +6347,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
63376347
},
63386348
});
63396349
self.counterparty_fulfilled_htlcs.insert(SentHTLCId::from_source(&source), payment_preimage);
6340-
self.pending_monitor_events.push(MonitorEvent::HTLCEvent(HTLCUpdate {
6350+
push_monitor_event(&mut self.pending_monitor_events, MonitorEvent::HTLCEvent(HTLCUpdate {
63416351
source,
63426352
payment_preimage: Some(payment_preimage),
63436353
payment_hash,

0 commit comments

Comments
 (0)