Skip to content

Commit f5e87d8

Browse files
authored
Merge pull request #2795 from TheBlueMatt/2023-11-robuster-chan-to-peer
Move channel -> peer tracking to OutPoints from Channel IDs
2 parents b9797eb + e9452c7 commit f5e87d8

File tree

6 files changed

+209
-88
lines changed

6 files changed

+209
-88
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::sign::ecdsa::WriteableEcdsaChannelSigner;
3535
use crate::events;
3636
use crate::events::{Event, EventHandler};
3737
use crate::util::atomic_counter::AtomicCounter;
38-
use crate::util::logger::Logger;
38+
use crate::util::logger::{Logger, WithContext};
3939
use crate::util::errors::APIError;
4040
use crate::util::wakers::{Future, Notifier};
4141
use crate::ln::channelmanager::ChannelDetails;
@@ -757,7 +757,8 @@ where C::Target: chain::Filter,
757757
let monitors = self.monitors.read().unwrap();
758758
match monitors.get(&funding_txo) {
759759
None => {
760-
log_error!(self.logger, "Failed to update channel monitor: no such monitor registered");
760+
let logger = WithContext::from(&self.logger, update.counterparty_node_id, Some(funding_txo.to_channel_id()));
761+
log_error!(logger, "Failed to update channel monitor: no such monitor registered");
761762

762763
// We should never ever trigger this from within ChannelManager. Technically a
763764
// user could use this object with some proxying in between which makes this

lightning/src/chain/channelmonitor.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ use crate::sync::{Mutex, LockTestExt};
7171
#[must_use]
7272
pub struct ChannelMonitorUpdate {
7373
pub(crate) updates: Vec<ChannelMonitorUpdateStep>,
74+
/// Historically, [`ChannelMonitor`]s didn't know their counterparty node id. However,
75+
/// `ChannelManager` really wants to know it so that it can easily look up the corresponding
76+
/// channel. For now, this results in a temporary map in `ChannelManager` to look up channels
77+
/// by only the funding outpoint.
78+
///
79+
/// To eventually remove that, we repeat the counterparty node id here so that we can upgrade
80+
/// `ChannelMonitor`s to become aware of the counterparty node id if they were generated prior
81+
/// to when it was stored directly in them.
82+
pub(crate) counterparty_node_id: Option<PublicKey>,
7483
/// The sequence number of this update. Updates *must* be replayed in-order according to this
7584
/// sequence number (and updates may panic if they are not). The update_id values are strictly
7685
/// increasing and increase by one for each new update, with two exceptions specified below.
@@ -107,7 +116,9 @@ impl Writeable for ChannelMonitorUpdate {
107116
for update_step in self.updates.iter() {
108117
update_step.write(w)?;
109118
}
110-
write_tlv_fields!(w, {});
119+
write_tlv_fields!(w, {
120+
(1, self.counterparty_node_id, option),
121+
});
111122
Ok(())
112123
}
113124
}
@@ -122,8 +133,11 @@ impl Readable for ChannelMonitorUpdate {
122133
updates.push(upd);
123134
}
124135
}
125-
read_tlv_fields!(r, {});
126-
Ok(Self { update_id, updates })
136+
let mut counterparty_node_id = None;
137+
read_tlv_fields!(r, {
138+
(1, counterparty_node_id, option),
139+
});
140+
Ok(Self { update_id, counterparty_node_id, updates })
127141
}
128142
}
129143

@@ -2738,6 +2752,15 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
27382752
log_info!(logger, "Applying update to monitor {}, bringing update_id from {} to {} with {} change(s).",
27392753
log_funding_info!(self), self.latest_update_id, updates.update_id, updates.updates.len());
27402754
}
2755+
2756+
if updates.counterparty_node_id.is_some() {
2757+
if self.counterparty_node_id.is_none() {
2758+
self.counterparty_node_id = updates.counterparty_node_id;
2759+
} else {
2760+
debug_assert_eq!(self.counterparty_node_id, updates.counterparty_node_id);
2761+
}
2762+
}
2763+
27412764
// ChannelMonitor updates may be applied after force close if we receive a preimage for a
27422765
// broadcasted commitment transaction HTLC output that we'd like to claim on-chain. If this
27432766
// is the case, we no longer have guaranteed access to the monitor's update ID, so we use a

lightning/src/ln/channel.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,6 +2394,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23942394
self.latest_monitor_update_id = CLOSED_CHANNEL_UPDATE_ID;
23952395
Some((self.get_counterparty_node_id(), funding_txo, ChannelMonitorUpdate {
23962396
update_id: self.latest_monitor_update_id,
2397+
counterparty_node_id: Some(self.counterparty_node_id),
23972398
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
23982399
}))
23992400
} else { None }
@@ -2766,6 +2767,7 @@ impl<SP: Deref> Channel<SP> where
27662767
self.context.latest_monitor_update_id += 1;
27672768
let monitor_update = ChannelMonitorUpdate {
27682769
update_id: self.context.latest_monitor_update_id,
2770+
counterparty_node_id: Some(self.context.counterparty_node_id),
27692771
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
27702772
payment_preimage: payment_preimage_arg.clone(),
27712773
}],
@@ -2997,6 +2999,20 @@ impl<SP: Deref> Channel<SP> where
29972999
self.context.channel_state.clear_waiting_for_batch();
29983000
}
29993001

3002+
/// Unsets the existing funding information.
3003+
///
3004+
/// This must only be used if the channel has not yet completed funding and has not been used.
3005+
///
3006+
/// Further, the channel must be immediately shut down after this with a call to
3007+
/// [`ChannelContext::force_shutdown`].
3008+
pub fn unset_funding_info(&mut self, temporary_channel_id: ChannelId) {
3009+
debug_assert!(matches!(
3010+
self.context.channel_state, ChannelState::AwaitingChannelReady(_)
3011+
));
3012+
self.context.channel_transaction_parameters.funding_outpoint = None;
3013+
self.context.channel_id = temporary_channel_id;
3014+
}
3015+
30003016
/// Handles a channel_ready message from our peer. If we've already sent our channel_ready
30013017
/// and the channel is now usable (and public), this may generate an announcement_signatures to
30023018
/// reply with.
@@ -3487,6 +3503,7 @@ impl<SP: Deref> Channel<SP> where
34873503
self.context.latest_monitor_update_id += 1;
34883504
let mut monitor_update = ChannelMonitorUpdate {
34893505
update_id: self.context.latest_monitor_update_id,
3506+
counterparty_node_id: Some(self.context.counterparty_node_id),
34903507
updates: vec![ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo {
34913508
commitment_tx: holder_commitment_tx,
34923509
htlc_outputs: htlcs_and_sigs,
@@ -3566,6 +3583,7 @@ impl<SP: Deref> Channel<SP> where
35663583

35673584
let mut monitor_update = ChannelMonitorUpdate {
35683585
update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
3586+
counterparty_node_id: Some(self.context.counterparty_node_id),
35693587
updates: Vec::new(),
35703588
};
35713589

@@ -3746,6 +3764,7 @@ impl<SP: Deref> Channel<SP> where
37463764
self.context.latest_monitor_update_id += 1;
37473765
let mut monitor_update = ChannelMonitorUpdate {
37483766
update_id: self.context.latest_monitor_update_id,
3767+
counterparty_node_id: Some(self.context.counterparty_node_id),
37493768
updates: vec![ChannelMonitorUpdateStep::CommitmentSecret {
37503769
idx: self.context.cur_counterparty_commitment_transaction_number + 1,
37513770
secret: msg.per_commitment_secret,
@@ -4803,6 +4822,7 @@ impl<SP: Deref> Channel<SP> where
48034822
self.context.latest_monitor_update_id += 1;
48044823
let monitor_update = ChannelMonitorUpdate {
48054824
update_id: self.context.latest_monitor_update_id,
4825+
counterparty_node_id: Some(self.context.counterparty_node_id),
48064826
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
48074827
scriptpubkey: self.get_closing_scriptpubkey(),
48084828
}],
@@ -5926,6 +5946,7 @@ impl<SP: Deref> Channel<SP> where
59265946
self.context.latest_monitor_update_id += 1;
59275947
let monitor_update = ChannelMonitorUpdate {
59285948
update_id: self.context.latest_monitor_update_id,
5949+
counterparty_node_id: Some(self.context.counterparty_node_id),
59295950
updates: vec![ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo {
59305951
commitment_txid: counterparty_commitment_txid,
59315952
htlc_outputs: htlcs.clone(),
@@ -6124,6 +6145,7 @@ impl<SP: Deref> Channel<SP> where
61246145
self.context.latest_monitor_update_id += 1;
61256146
let monitor_update = ChannelMonitorUpdate {
61266147
update_id: self.context.latest_monitor_update_id,
6148+
counterparty_node_id: Some(self.context.counterparty_node_id),
61276149
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
61286150
scriptpubkey: self.get_closing_scriptpubkey(),
61296151
}],

0 commit comments

Comments
 (0)