Skip to content

Commit aa915b6

Browse files
committed
Store channel-associated events in monitor
1 parent 593d6aa commit aa915b6

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,9 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
635635

636636
#[derive(Clone, Debug, PartialEq, Eq)]
637637
pub(crate) enum ChannelMonitorUpdateStep {
638+
EventGenerated {
639+
event: Event,
640+
},
638641
LatestHolderCommitmentTXInfo {
639642
commitment_tx: HolderCommitmentTransaction,
640643
/// Note that LDK after 0.0.115 supports this only containing dust HTLCs (implying the
@@ -724,6 +727,7 @@ impl ChannelMonitorUpdateStep {
724727
ChannelMonitorUpdateStep::RenegotiatedFunding { .. } => "RenegotiatedFunding",
725728
ChannelMonitorUpdateStep::RenegotiatedFundingLocked { .. } => "RenegotiatedFundingLocked",
726729
ChannelMonitorUpdateStep::ReleasePaymentComplete { .. } => "ReleasePaymentComplete",
730+
ChannelMonitorUpdateStep::EventGenerated { .. } => "EventGenerated",
727731
}
728732
}
729733
}
@@ -778,6 +782,9 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
778782
(12, RenegotiatedFundingLocked) => {
779783
(1, funding_txid, required),
780784
},
785+
(13, EventGenerated) => {
786+
(0, event, upgradable_required),
787+
}
781788
);
782789

783790
/// Indicates whether the balance is derived from a cooperative close, a force-close
@@ -1283,6 +1290,7 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
12831290
pending_monitor_events: Vec<MonitorEvent>,
12841291

12851292
pub(super) pending_events: Vec<Event>,
1293+
pub(super) pending_mgr_events: Vec<Event>,
12861294
pub(super) is_processing_pending_events: bool,
12871295

12881296
// Used to track on-chain events (i.e., transactions part of channels confirmed on chain) on
@@ -1961,6 +1969,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
19611969

19621970
payment_preimages: new_hash_map(),
19631971
pending_monitor_events: Vec::new(),
1972+
pending_mgr_events: Vec::new(),
19641973
pending_events: Vec::new(),
19651974
is_processing_pending_events: false,
19661975

@@ -4370,6 +4379,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
43704379
log_trace!(logger, "HTLC {htlc:?} permanently and fully resolved");
43714380
self.htlcs_resolved_to_user.insert(*htlc);
43724381
},
4382+
ChannelMonitorUpdateStep::EventGenerated { event } => {
4383+
log_trace!(logger, "Updating ChannelMonitor with channel manager event: {:?}", event);
4384+
self.pending_mgr_events.push(event.clone());
4385+
},
43734386
}
43744387
}
43754388

@@ -4402,6 +4415,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
44024415
ChannelMonitorUpdateStep::PaymentPreimage { .. } => {},
44034416
ChannelMonitorUpdateStep::ChannelForceClosed { .. } => {},
44044417
ChannelMonitorUpdateStep::ReleasePaymentComplete { .. } => {},
4418+
ChannelMonitorUpdateStep::EventGenerated { .. } => {},
44054419
}
44064420
}
44074421

@@ -6644,6 +6658,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
66446658
let mut alternative_funding_confirmed = None;
66456659
let mut is_manual_broadcast = RequiredWrapper(None);
66466660
let mut funding_seen_onchain = RequiredWrapper(None);
6661+
let mut pending_mgr_events = Some(Vec::new());
66476662
read_tlv_fields!(reader, {
66486663
(1, funding_spend_confirmed, option),
66496664
(3, htlcs_resolved_on_chain, optional_vec),
@@ -6666,6 +6681,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
66666681
(34, alternative_funding_confirmed, option),
66676682
(35, is_manual_broadcast, (default_value, false)),
66686683
(37, funding_seen_onchain, (default_value, true)),
6684+
(38, pending_mgr_events, optional_vec),
66696685
});
66706686
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
66716687
// we can use it to determine if this monitor was last written by LDK 0.1 or later.
@@ -6812,6 +6828,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
68126828

68136829
payment_preimages,
68146830
pending_monitor_events: pending_monitor_events.unwrap(),
6831+
pending_mgr_events: pending_mgr_events.unwrap(),
68156832
pending_events,
68166833
is_processing_pending_events: false,
68176834

lightning/src/ln/channelmanager.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4686,8 +4686,7 @@ where
46864686
}
46874687

46884688
{
4689-
let mut pending_events = self.pending_events.lock().unwrap();
4690-
pending_events.push_back((
4689+
let closed_event = (
46914690
events::Event::ChannelClosed {
46924691
channel_id: shutdown_res.channel_id,
46934692
user_channel_id: shutdown_res.user_channel_id,
@@ -4698,7 +4697,43 @@ where
46984697
last_local_balance_msat: Some(shutdown_res.last_local_balance_msat),
46994698
},
47004699
None,
4701-
));
4700+
);
4701+
4702+
let mut per_peer_state = self.per_peer_state.write().unwrap();
4703+
let mut peer_state_lock = per_peer_state
4704+
.get_mut(&shutdown_res.counterparty_node_id)
4705+
.expect("We must always have a peer entry for a peer with which we have channels")
4706+
.lock()
4707+
.unwrap();
4708+
let peer_state = &mut *peer_state_lock;
4709+
let channel = peer_state
4710+
.channel_by_id
4711+
.get_mut(&shutdown_res.channel_id)
4712+
.expect("We should only be finishing the closure of a channel which existed");
4713+
let funded_chan = channel
4714+
.as_funded_mut()
4715+
.expect("We should only be finishing the closure of a funded channel here");
4716+
4717+
let update_id = funded_chan.get_latest_unblocked_monitor_update_id();
4718+
let monitor_update_step =
4719+
ChannelMonitorUpdateStep::EventGenerated { event: closed_event.0.clone() };
4720+
let monitor_update = ChannelMonitorUpdate {
4721+
update_id,
4722+
updates: vec![monitor_update_step],
4723+
channel_id: Some(shutdown_res.channel_id),
4724+
};
4725+
handle_new_monitor_update!(
4726+
self,
4727+
funded_chan.funding.get_funding_txo().unwrap(),
4728+
monitor_update,
4729+
peer_state_lock,
4730+
peer_state,
4731+
per_peer_state,
4732+
funded_chan
4733+
);
4734+
4735+
let mut pending_events = self.pending_events.lock().unwrap();
4736+
pending_events.push_back(closed_event);
47024737

47034738
if let Some(splice_funding_failed) = shutdown_res.splice_funding_failed.take() {
47044739
pending_events.push_back((

0 commit comments

Comments
 (0)