Skip to content

Commit 14212d7

Browse files
committed
Store channel-associated events in monitor
1 parent fd85279 commit 14212d7

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-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: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4575,16 +4575,42 @@ where
45754575
}
45764576

45774577
{
4578-
let mut pending_events = self.pending_events.lock().unwrap();
4579-
pending_events.push_back((events::Event::ChannelClosed {
4578+
let closed_event = (events::Event::ChannelClosed {
45804579
channel_id: shutdown_res.channel_id,
45814580
user_channel_id: shutdown_res.user_channel_id,
45824581
reason: shutdown_res.closure_reason,
45834582
counterparty_node_id: Some(shutdown_res.counterparty_node_id),
45844583
channel_capacity_sats: Some(shutdown_res.channel_capacity_satoshis),
45854584
channel_funding_txo: shutdown_res.channel_funding_txo,
45864585
last_local_balance_msat: Some(shutdown_res.last_local_balance_msat),
4587-
}, None));
4586+
}, None);
4587+
4588+
let mut per_peer_state = self.per_peer_state.write().unwrap();
4589+
let mut peer_state_lock = per_peer_state.
4590+
get_mut(&shutdown_res.counterparty_node_id)
4591+
.expect("We must always have a peer entry for a peer with which we have channels")
4592+
.lock().unwrap();
4593+
let peer_state = &mut *peer_state_lock;
4594+
let channel = peer_state
4595+
.channel_by_id.get_mut(&shutdown_res.channel_id)
4596+
.expect("We should only be finishing the closure of a channel which existed");
4597+
let funded_chan = channel.as_funded_mut()
4598+
.expect("We should only be finishing the closure of a funded channel here");
4599+
4600+
let update_id = funded_chan.get_latest_unblocked_monitor_update_id();
4601+
let monitor_update_step = ChannelMonitorUpdateStep::EventGenerated {
4602+
event: closed_event.0.clone(),
4603+
};
4604+
let monitor_update = ChannelMonitorUpdate {
4605+
update_id,
4606+
updates: vec![monitor_update_step],
4607+
channel_id: Some(shutdown_res.channel_id),
4608+
};
4609+
handle_new_monitor_update!(self, funded_chan.funding.get_funding_txo().unwrap(),
4610+
monitor_update, peer_state_lock, peer_state, per_peer_state, funded_chan);
4611+
4612+
let mut pending_events = self.pending_events.lock().unwrap();
4613+
pending_events.push_back(closed_event);
45884614

45894615
if let Some(splice_funding_failed) = shutdown_res.splice_funding_failed.take() {
45904616
pending_events.push_back((events::Event::SpliceFailed {

0 commit comments

Comments
 (0)