Skip to content

Commit 8930f59

Browse files
committed
f include channel_id in ChannelMonitorUpdate
1 parent 32d7d55 commit 8930f59

9 files changed

+45
-29
lines changed

fuzz/src/chanmon_consistency.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
176176
self.chain_monitor.watch_channel(funding_txo, monitor)
177177
}
178178

179-
fn update_channel(&self, funding_txo: OutPoint, channel_id: ChannelId, update: &channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
179+
fn update_channel(&self, funding_txo: OutPoint, update: &channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
180180
let mut map_lock = self.latest_monitors.lock().unwrap();
181181
let mut map_entry = match map_lock.entry(funding_txo) {
182182
hash_map::Entry::Occupied(entry) => entry,
@@ -188,7 +188,7 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
188188
let mut ser = VecWriter(Vec::new());
189189
deserialized_monitor.write(&mut ser).unwrap();
190190
map_entry.insert((update.update_id, ser.0));
191-
self.chain_monitor.update_channel(funding_txo, channel_id, update)
191+
self.chain_monitor.update_channel(funding_txo, update)
192192
}
193193

194194
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)> {

lightning/src/chain/chainmonitor.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,10 @@ where C::Target: chain::Filter,
762762
Ok(persist_res)
763763
}
764764

765-
fn update_channel(&self, funding_txo: OutPoint, channel_id: ChannelId, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus {
765+
fn update_channel(&self, funding_txo: OutPoint, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus {
766+
// `ChannelMonitorUpdate`'s `channel_id` is `None` prior to 0.0.121 and all channels in those
767+
// versions are V1-established. For 0.0.121+ the `channel_id` fields is always `Some`.
768+
let channel_id = update.channel_id.unwrap_or(ChannelId::v1_from_funding_outpoint(funding_txo));
766769
// Update the monitor that watches the channel referred to by the given outpoint.
767770
let monitors = self.monitors.read().unwrap();
768771
match monitors.get(&funding_txo) {

lightning/src/chain/channelmonitor.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ pub struct ChannelMonitorUpdate {
9696
///
9797
/// [`ChannelMonitorUpdateStatus::InProgress`]: super::ChannelMonitorUpdateStatus::InProgress
9898
pub update_id: u64,
99+
/// The channel ID associated with these updates.
100+
///
101+
/// Will be `None` for `ChannelMonitorUpdate`s constructed on LDK versions prior to 0.0.121 and
102+
/// always `Some` otherwise.
103+
pub channel_id: Option<ChannelId>,
99104
}
100105

101106
/// The update ID used for a [`ChannelMonitorUpdate`] that is either:
@@ -118,6 +123,7 @@ impl Writeable for ChannelMonitorUpdate {
118123
}
119124
write_tlv_fields!(w, {
120125
(1, self.counterparty_node_id, option),
126+
(3, self.channel_id, option),
121127
});
122128
Ok(())
123129
}
@@ -134,10 +140,12 @@ impl Readable for ChannelMonitorUpdate {
134140
}
135141
}
136142
let mut counterparty_node_id = None;
143+
let mut channel_id = None;
137144
read_tlv_fields!(r, {
138145
(1, counterparty_node_id, option),
146+
(3, channel_id, option),
139147
});
140-
Ok(Self { update_id, counterparty_node_id, updates })
148+
Ok(Self { update_id, counterparty_node_id, updates, channel_id })
141149
}
142150
}
143151

lightning/src/chain/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ pub trait Watch<ChannelSigner: WriteableEcdsaChannelSigner> {
287287
/// [`ChannelMonitorUpdateStatus::UnrecoverableError`], see its documentation for more info.
288288
///
289289
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
290-
fn update_channel(&self, funding_txo: OutPoint, channel_id: ChannelId, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus;
290+
fn update_channel(&self, funding_txo: OutPoint, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus;
291291

292292
/// Returns any monitor events since the last call. Subsequent calls must only return new
293293
/// events.

lightning/src/ln/chanmon_update_fail_tests.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ fn test_monitor_and_persister_update_fail() {
5050
// Create some initial channel
5151
let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5252
let outpoint = OutPoint { txid: chan.3.txid(), index: 0 };
53-
let channel_id = chan.2;
5453

5554
// Rebalance the network to generate htlc in the two directions
5655
send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
@@ -102,12 +101,12 @@ fn test_monitor_and_persister_update_fail() {
102101
if let Ok(Some(update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
103102
// Check that the persister returns InProgress (and will never actually complete)
104103
// as the monitor update errors.
105-
if let ChannelMonitorUpdateStatus::InProgress = chain_mon.chain_monitor.update_channel(outpoint, channel_id, &update) {} else { panic!("Expected monitor paused"); }
104+
if let ChannelMonitorUpdateStatus::InProgress = chain_mon.chain_monitor.update_channel(outpoint, &update) {} else { panic!("Expected monitor paused"); }
106105
logger.assert_log_regex("lightning::chain::chainmonitor", regex::Regex::new("Failed to update ChannelMonitor for channel [0-9a-f]*.").unwrap(), 1);
107106

108107
// Apply the monitor update to the original ChainMonitor, ensuring the
109108
// ChannelManager and ChannelMonitor aren't out of sync.
110-
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, channel_id, &update),
109+
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update),
111110
ChannelMonitorUpdateStatus::Completed);
112111
} else { assert!(false); }
113112
} else {

lightning/src/ln/channel.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23982398
update_id: self.latest_monitor_update_id,
23992399
counterparty_node_id: Some(self.counterparty_node_id),
24002400
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast }],
2401+
channel_id: Some(self.channel_id()),
24012402
}))
24022403
} else { None }
24032404
} else { None };
@@ -2777,6 +2778,7 @@ impl<SP: Deref> Channel<SP> where
27772778
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
27782779
payment_preimage: payment_preimage_arg.clone(),
27792780
}],
2781+
channel_id: Some(self.context.channel_id()),
27802782
};
27812783

27822784
if self.context.channel_state.should_force_holding_cell() {
@@ -3515,7 +3517,8 @@ impl<SP: Deref> Channel<SP> where
35153517
htlc_outputs: htlcs_and_sigs,
35163518
claimed_htlcs,
35173519
nondust_htlc_sources,
3518-
}]
3520+
}],
3521+
channel_id: Some(self.context.channel_id()),
35193522
};
35203523

35213524
self.context.cur_holder_commitment_transaction_number -= 1;
@@ -3591,6 +3594,7 @@ impl<SP: Deref> Channel<SP> where
35913594
update_id: self.context.latest_monitor_update_id + 1, // We don't increment this yet!
35923595
counterparty_node_id: Some(self.context.counterparty_node_id),
35933596
updates: Vec::new(),
3597+
channel_id: Some(self.context.channel_id()),
35943598
};
35953599

35963600
let mut htlc_updates = Vec::new();
@@ -3769,6 +3773,7 @@ impl<SP: Deref> Channel<SP> where
37693773
idx: self.context.cur_counterparty_commitment_transaction_number + 1,
37703774
secret: msg.per_commitment_secret,
37713775
}],
3776+
channel_id: Some(self.context.channel_id()),
37723777
};
37733778

37743779
// Update state now that we've passed all the can-fail calls...
@@ -4826,6 +4831,7 @@ impl<SP: Deref> Channel<SP> where
48264831
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
48274832
scriptpubkey: self.get_closing_scriptpubkey(),
48284833
}],
4834+
channel_id: Some(self.context.channel_id()),
48294835
};
48304836
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
48314837
self.push_ret_blockable_mon_update(monitor_update)
@@ -5965,7 +5971,8 @@ impl<SP: Deref> Channel<SP> where
59655971
feerate_per_kw: Some(counterparty_commitment_tx.feerate_per_kw()),
59665972
to_broadcaster_value_sat: Some(counterparty_commitment_tx.to_broadcaster_value_sat()),
59675973
to_countersignatory_value_sat: Some(counterparty_commitment_tx.to_countersignatory_value_sat()),
5968-
}]
5974+
}],
5975+
channel_id: Some(self.context.channel_id()),
59695976
};
59705977
self.context.channel_state.set_awaiting_remote_revoke();
59715978
monitor_update
@@ -6159,6 +6166,7 @@ impl<SP: Deref> Channel<SP> where
61596166
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
61606167
scriptpubkey: self.get_closing_scriptpubkey(),
61616168
}],
6169+
channel_id: Some(self.context.channel_id()),
61626170
};
61636171
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
61646172
self.push_ret_blockable_mon_update(monitor_update)

lightning/src/ln/channelmanager.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -2307,7 +2307,7 @@ macro_rules! handle_new_monitor_update {
23072307
in_flight_updates.push($update);
23082308
in_flight_updates.len() - 1
23092309
});
2310-
let update_res = $self.chain_monitor.update_channel($funding_txo, $channel_id, &in_flight_updates[idx]);
2310+
let update_res = $self.chain_monitor.update_channel($funding_txo, &in_flight_updates[idx]);
23112311
handle_new_monitor_update!($self, update_res, $chan, _internal,
23122312
{
23132313
let _ = in_flight_updates.remove(idx);
@@ -2864,12 +2864,12 @@ where
28642864
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
28652865
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
28662866
}
2867-
if let Some((_, funding_txo, channel_id, monitor_update)) = shutdown_res.monitor_update {
2867+
if let Some((_, funding_txo, _channel_id, monitor_update)) = shutdown_res.monitor_update {
28682868
// There isn't anything we can do if we get an update failure - we're already
28692869
// force-closing. The monitor update on the required in-memory copy should broadcast
28702870
// the latest local state, which is the best we can do anyway. Thus, it is safe to
28712871
// ignore the result here.
2872-
let _ = self.chain_monitor.update_channel(funding_txo, channel_id, &monitor_update);
2872+
let _ = self.chain_monitor.update_channel(funding_txo, &monitor_update);
28732873
}
28742874
let mut shutdown_results = Vec::new();
28752875
if let Some(txid) = shutdown_res.unbroadcasted_batch_funding_txid {
@@ -3404,7 +3404,6 @@ where
34043404
return Err(APIError::ChannelUnavailable{err: "Peer for first hop currently disconnected".to_owned()});
34053405
}
34063406
let funding_txo = chan.context.get_funding_txo().unwrap();
3407-
let channel_id = chan.context.channel_id();
34083407
let logger = WithChannelContext::from(&self.logger, &chan.context);
34093408
let send_res = chan.send_htlc_and_commit(htlc_msat, payment_hash.clone(),
34103409
htlc_cltv, HTLCSource::OutboundRoute {
@@ -4755,10 +4754,10 @@ where
47554754

47564755
for event in background_events.drain(..) {
47574756
match event {
4758-
BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((funding_txo, channel_id, update)) => {
4757+
BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((funding_txo, _channel_id, update)) => {
47594758
// The channel has already been closed, so no use bothering to care about the
47604759
// monitor updating completing.
4761-
let _ = self.chain_monitor.update_channel(funding_txo, channel_id, &update);
4760+
let _ = self.chain_monitor.update_channel(funding_txo, &update);
47624761
},
47634762
BackgroundEvent::MonitorUpdateRegeneratedOnStartup { counterparty_node_id, funding_txo, channel_id, update } => {
47644763
let mut updated_chan = false;
@@ -4783,7 +4782,7 @@ where
47834782
}
47844783
if !updated_chan {
47854784
// TODO: Track this as in-flight even though the channel is closed.
4786-
let _ = self.chain_monitor.update_channel(funding_txo, channel_id, &update);
4785+
let _ = self.chain_monitor.update_channel(funding_txo, &update);
47874786
}
47884787
},
47894788
BackgroundEvent::MonitorUpdatesComplete { counterparty_node_id, channel_id } => {
@@ -5645,14 +5644,13 @@ where
56455644
updates: vec![ChannelMonitorUpdateStep::PaymentPreimage {
56465645
payment_preimage,
56475646
}],
5647+
channel_id: Some(prev_hop.channel_id),
56485648
};
56495649

5650-
let prev_hop_channel_id = prev_hop.channel_id;
5651-
56525650
if !during_init {
56535651
// We update the ChannelMonitor on the backward link, after
56545652
// receiving an `update_fulfill_htlc` from the forward link.
5655-
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, prev_hop_channel_id, &preimage_update);
5653+
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, &preimage_update);
56565654
if update_res != ChannelMonitorUpdateStatus::Completed {
56575655
// TODO: This needs to be handled somehow - if we receive a monitor update
56585656
// with a preimage we *must* somehow manage to propagate it to the upstream
@@ -10459,6 +10457,7 @@ where
1045910457
update_id: CLOSED_CHANNEL_UPDATE_ID,
1046010458
counterparty_node_id: None,
1046110459
updates: vec![ChannelMonitorUpdateStep::ChannelForceClosed { should_broadcast: true }],
10460+
channel_id: Some(monitor.channel_id()),
1046210461
};
1046310462
close_background_events.push(BackgroundEvent::ClosedMonitorUpdateRegeneratedOnStartup((*funding_txo, channel_id, monitor_update)));
1046410463
}

lightning/src/ln/functional_tests.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -8470,7 +8470,6 @@ fn test_update_err_monitor_lockdown() {
84708470
// Create some initial channel
84718471
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
84728472
let outpoint = OutPoint { txid: chan_1.3.txid(), index: 0 };
8473-
let channel_id = chan_1.2;
84748473

84758474
// Rebalance the network to generate htlc in the two directions
84768475
send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
@@ -8513,8 +8512,8 @@ fn test_update_err_monitor_lockdown() {
85138512
let mut node_0_peer_state_lock;
85148513
if let ChannelPhase::Funded(ref mut channel) = get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1.2) {
85158514
if let Ok(Some(update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
8516-
assert_eq!(watchtower.chain_monitor.update_channel(outpoint, channel_id, &update), ChannelMonitorUpdateStatus::InProgress);
8517-
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, channel_id, &update), ChannelMonitorUpdateStatus::Completed);
8515+
assert_eq!(watchtower.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::InProgress);
8516+
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
85188517
} else { assert!(false); }
85198518
} else {
85208519
assert!(false);
@@ -8541,7 +8540,6 @@ fn test_concurrent_monitor_claim() {
85418540
// Create some initial channel
85428541
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
85438542
let outpoint = OutPoint { txid: chan_1.3.txid(), index: 0 };
8544-
let channel_id = chan_1.2;
85458543

85468544
// Rebalance the network to generate htlc in the two directions
85478545
send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
@@ -8617,9 +8615,9 @@ fn test_concurrent_monitor_claim() {
86178615
if let ChannelPhase::Funded(ref mut channel) = get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1.2) {
86188616
if let Ok(Some(update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
86198617
// Watchtower Alice should already have seen the block and reject the update
8620-
assert_eq!(watchtower_alice.chain_monitor.update_channel(outpoint, channel_id, &update), ChannelMonitorUpdateStatus::InProgress);
8621-
assert_eq!(watchtower_bob.chain_monitor.update_channel(outpoint, channel_id, &update), ChannelMonitorUpdateStatus::Completed);
8622-
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, channel_id, &update), ChannelMonitorUpdateStatus::Completed);
8618+
assert_eq!(watchtower_alice.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::InProgress);
8619+
assert_eq!(watchtower_bob.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
8620+
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
86238621
} else { assert!(false); }
86248622
} else {
86258623
assert!(false);

lightning/src/util/test_utils.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,13 @@ impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {
347347
self.chain_monitor.watch_channel(funding_txo, new_monitor)
348348
}
349349

350-
fn update_channel(&self, funding_txo: OutPoint, channel_id: ChannelId, update: &channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
350+
fn update_channel(&self, funding_txo: OutPoint, update: &channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
351351
// Every monitor update should survive roundtrip
352352
let mut w = TestVecWriter(Vec::new());
353353
update.write(&mut w).unwrap();
354354
assert!(channelmonitor::ChannelMonitorUpdate::read(
355355
&mut io::Cursor::new(&w.0)).unwrap() == *update);
356+
let channel_id = update.channel_id.unwrap_or(ChannelId::v1_from_funding_outpoint(funding_txo));
356357

357358
self.monitor_updates.lock().unwrap().entry(channel_id).or_insert(Vec::new()).push(update.clone());
358359

@@ -366,7 +367,7 @@ impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {
366367

367368
self.latest_monitor_update_id.lock().unwrap().insert(channel_id,
368369
(funding_txo, update.update_id, MonitorUpdateId::from_monitor_update(update)));
369-
let update_res = self.chain_monitor.update_channel(funding_txo, channel_id, update);
370+
let update_res = self.chain_monitor.update_channel(funding_txo, update);
370371
// At every point where we get a monitor update, we should be able to send a useful monitor
371372
// to a watchtower and disk...
372373
let monitor = self.chain_monitor.get_monitor(funding_txo).unwrap();

0 commit comments

Comments
 (0)