Skip to content

Commit 7e23afe

Browse files
committed
Pass monitor updates by reference, not owned
In the next commit(s) we'll start holding `ChannelMonitorUpdate`s that are being persisted in `Channel`s until they're done persisting. In order to do that, switch to applying the updates by reference instead of value.
1 parent ce6bcf6 commit 7e23afe

File tree

9 files changed

+34
-34
lines changed

9 files changed

+34
-34
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ impl chain::Watch<EnforcingSigner> for TestChainMonitor {
152152
self.chain_monitor.watch_channel(funding_txo, monitor)
153153
}
154154

155-
fn update_channel(&self, funding_txo: OutPoint, update: channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
155+
fn update_channel(&self, funding_txo: OutPoint, update: &channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
156156
let mut map_lock = self.latest_monitors.lock().unwrap();
157157
let mut map_entry = match map_lock.entry(funding_txo) {
158158
hash_map::Entry::Occupied(entry) => entry,
159159
hash_map::Entry::Vacant(_) => panic!("Didn't have monitor on update call"),
160160
};
161161
let deserialized_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingSigner>)>::
162162
read(&mut Cursor::new(&map_entry.get().1), (&*self.keys, &*self.keys)).unwrap().1;
163-
deserialized_monitor.update_monitor(&update, &&TestBroadcaster{}, &FuzzEstimator { ret_val: atomic::AtomicU32::new(253) }, &self.logger).unwrap();
163+
deserialized_monitor.update_monitor(update, &&TestBroadcaster{}, &FuzzEstimator { ret_val: atomic::AtomicU32::new(253) }, &self.logger).unwrap();
164164
let mut ser = VecWriter(Vec::new());
165165
deserialized_monitor.write(&mut ser).unwrap();
166166
map_entry.insert((update.update_id, ser.0));

fuzz/src/utils/test_persister.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl chainmonitor::Persist<EnforcingSigner> for TestPersister {
1414
self.update_ret.lock().unwrap().clone()
1515
}
1616

17-
fn update_persisted_channel(&self, _funding_txo: OutPoint, _update: &Option<channelmonitor::ChannelMonitorUpdate>, _data: &channelmonitor::ChannelMonitor<EnforcingSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
17+
fn update_persisted_channel(&self, _funding_txo: OutPoint, _update: Option<&channelmonitor::ChannelMonitorUpdate>, _data: &channelmonitor::ChannelMonitor<EnforcingSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
1818
self.update_ret.lock().unwrap().clone()
1919
}
2020
}

lightning/src/chain/chainmonitor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub trait Persist<ChannelSigner: Sign> {
144144
/// [`ChannelMonitorUpdateStatus`] for requirements when returning errors.
145145
///
146146
/// [`Writeable::write`]: crate::util::ser::Writeable::write
147-
fn update_persisted_channel(&self, channel_id: OutPoint, update: &Option<ChannelMonitorUpdate>, data: &ChannelMonitor<ChannelSigner>, update_id: MonitorUpdateId) -> ChannelMonitorUpdateStatus;
147+
fn update_persisted_channel(&self, channel_id: OutPoint, update: Option<&ChannelMonitorUpdate>, data: &ChannelMonitor<ChannelSigner>, update_id: MonitorUpdateId) -> ChannelMonitorUpdateStatus;
148148
}
149149

150150
struct MonitorHolder<ChannelSigner: Sign> {
@@ -294,7 +294,7 @@ where C::Target: chain::Filter,
294294
}
295295

296296
log_trace!(self.logger, "Syncing Channel Monitor for channel {}", log_funding_info!(monitor));
297-
match self.persister.update_persisted_channel(*funding_outpoint, &None, monitor, update_id) {
297+
match self.persister.update_persisted_channel(*funding_outpoint, None, monitor, update_id) {
298298
ChannelMonitorUpdateStatus::Completed =>
299299
log_trace!(self.logger, "Finished syncing Channel Monitor for channel {}", log_funding_info!(monitor)),
300300
ChannelMonitorUpdateStatus::PermanentFailure => {
@@ -646,7 +646,7 @@ where C::Target: chain::Filter,
646646

647647
/// Note that we persist the given `ChannelMonitor` update while holding the
648648
/// `ChainMonitor` monitors lock.
649-
fn update_channel(&self, funding_txo: OutPoint, update: ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus {
649+
fn update_channel(&self, funding_txo: OutPoint, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus {
650650
// Update the monitor that watches the channel referred to by the given outpoint.
651651
let monitors = self.monitors.read().unwrap();
652652
match monitors.get(&funding_txo) {
@@ -664,15 +664,15 @@ where C::Target: chain::Filter,
664664
Some(monitor_state) => {
665665
let monitor = &monitor_state.monitor;
666666
log_trace!(self.logger, "Updating ChannelMonitor for channel {}", log_funding_info!(monitor));
667-
let update_res = monitor.update_monitor(&update, &self.broadcaster, &*self.fee_estimator, &self.logger);
667+
let update_res = monitor.update_monitor(update, &self.broadcaster, &*self.fee_estimator, &self.logger);
668668
if update_res.is_err() {
669669
log_error!(self.logger, "Failed to update ChannelMonitor for channel {}.", log_funding_info!(monitor));
670670
}
671671
// Even if updating the monitor returns an error, the monitor's state will
672672
// still be changed. So, persist the updated monitor despite the error.
673-
let update_id = MonitorUpdateId::from_monitor_update(&update);
673+
let update_id = MonitorUpdateId::from_monitor_update(update);
674674
let mut pending_monitor_updates = monitor_state.pending_monitor_updates.lock().unwrap();
675-
let persist_res = self.persister.update_persisted_channel(funding_txo, &Some(update), monitor, update_id);
675+
let persist_res = self.persister.update_persisted_channel(funding_txo, Some(update), monitor, update_id);
676676
match persist_res {
677677
ChannelMonitorUpdateStatus::InProgress => {
678678
pending_monitor_updates.push(update_id);

lightning/src/chain/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ pub trait Watch<ChannelSigner: Sign> {
312312
/// [`ChannelMonitorUpdateStatus`] for invariants around returning an error.
313313
///
314314
/// [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
315-
fn update_channel(&self, funding_txo: OutPoint, update: ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus;
315+
fn update_channel(&self, funding_txo: OutPoint, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus;
316316

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

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ fn test_monitor_and_persister_update_fail() {
147147
// Check that even though the persister is returning a InProgress,
148148
// because the update is bogus, ultimately the error that's returned
149149
// should be a PermanentFailure.
150-
if let ChannelMonitorUpdateStatus::PermanentFailure = chain_mon.chain_monitor.update_channel(outpoint, update.clone()) {} else { panic!("Expected monitor error to be permanent"); }
150+
if let ChannelMonitorUpdateStatus::PermanentFailure = chain_mon.chain_monitor.update_channel(outpoint, &update) {} else { panic!("Expected monitor error to be permanent"); }
151151
logger.assert_log_regex("lightning::chain::chainmonitor".to_string(), regex::Regex::new("Persistence of ChannelMonitorUpdate for channel [0-9a-f]* in progress").unwrap(), 1);
152-
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, update), ChannelMonitorUpdateStatus::Completed);
152+
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
153153
} else { assert!(false); }
154154
}
155155

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ where
17091709

17101710
// Update the monitor with the shutdown script if necessary.
17111711
if let Some(monitor_update) = monitor_update {
1712-
let update_res = self.chain_monitor.update_channel(chan_entry.get().get_funding_txo().unwrap(), monitor_update);
1712+
let update_res = self.chain_monitor.update_channel(chan_entry.get().get_funding_txo().unwrap(), &monitor_update);
17131713
let (result, is_permanent) =
17141714
handle_monitor_update_res!(self, update_res, chan_entry.get_mut(), RAACommitmentOrder::CommitmentFirst, chan_entry.key(), NO_UPDATE);
17151715
if is_permanent {
@@ -1807,7 +1807,7 @@ where
18071807
// force-closing. The monitor update on the required in-memory copy should broadcast
18081808
// the latest local state, which is the best we can do anyway. Thus, it is safe to
18091809
// ignore the result here.
1810-
let _ = self.chain_monitor.update_channel(funding_txo, monitor_update);
1810+
let _ = self.chain_monitor.update_channel(funding_txo, &monitor_update);
18111811
}
18121812
}
18131813

@@ -2336,7 +2336,7 @@ where
23362336
chan)
23372337
} {
23382338
Some((update_add, commitment_signed, monitor_update)) => {
2339-
let update_err = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update);
2339+
let update_err = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), &monitor_update);
23402340
let chan_id = chan.get().channel_id();
23412341
match (update_err,
23422342
handle_monitor_update_res!(self, update_err, chan,
@@ -3284,7 +3284,7 @@ where
32843284
BackgroundEvent::ClosingMonitorUpdate((funding_txo, update)) => {
32853285
// The channel has already been closed, so no use bothering to care about the
32863286
// monitor updating completing.
3287-
let _ = self.chain_monitor.update_channel(funding_txo, update);
3287+
let _ = self.chain_monitor.update_channel(funding_txo, &update);
32883288
},
32893289
}
32903290
}
@@ -3807,7 +3807,7 @@ where
38073807
match chan.get_mut().get_update_fulfill_htlc_and_commit(prev_hop.htlc_id, payment_preimage, &self.logger) {
38083808
Ok(msgs_monitor_option) => {
38093809
if let UpdateFulfillCommitFetch::NewClaim { msgs, htlc_value_msat, monitor_update } = msgs_monitor_option {
3810-
match self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
3810+
match self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), &monitor_update) {
38113811
ChannelMonitorUpdateStatus::Completed => {},
38123812
e => {
38133813
log_given_level!(self.logger, if e == ChannelMonitorUpdateStatus::PermanentFailure { Level::Error } else { Level::Debug },
@@ -3844,7 +3844,7 @@ where
38443844
}
38453845
},
38463846
Err((e, monitor_update)) => {
3847-
match self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
3847+
match self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), &monitor_update) {
38483848
ChannelMonitorUpdateStatus::Completed => {},
38493849
e => {
38503850
// TODO: This needs to be handled somehow - if we receive a monitor update
@@ -3880,7 +3880,7 @@ where
38803880
};
38813881
// We update the ChannelMonitor on the backward link, after
38823882
// receiving an `update_fulfill_htlc` from the forward link.
3883-
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, preimage_update);
3883+
let update_res = self.chain_monitor.update_channel(prev_hop.outpoint, &preimage_update);
38843884
if update_res != ChannelMonitorUpdateStatus::Completed {
38853885
// TODO: This needs to be handled somehow - if we receive a monitor update
38863886
// with a preimage we *must* somehow manage to propagate it to the upstream
@@ -4449,7 +4449,7 @@ where
44494449

44504450
// Update the monitor with the shutdown script if necessary.
44514451
if let Some(monitor_update) = monitor_update {
4452-
let update_res = self.chain_monitor.update_channel(chan_entry.get().get_funding_txo().unwrap(), monitor_update);
4452+
let update_res = self.chain_monitor.update_channel(chan_entry.get().get_funding_txo().unwrap(), &monitor_update);
44534453
let (result, is_permanent) =
44544454
handle_monitor_update_res!(self, update_res, chan_entry.get_mut(), RAACommitmentOrder::CommitmentFirst, chan_entry.key(), NO_UPDATE);
44554455
if is_permanent {
@@ -4650,13 +4650,13 @@ where
46504650
Err((None, e)) => try_chan_entry!(self, Err(e), chan),
46514651
Err((Some(update), e)) => {
46524652
assert!(chan.get().is_awaiting_monitor_update());
4653-
let _ = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), update);
4653+
let _ = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), &update);
46544654
try_chan_entry!(self, Err(e), chan);
46554655
unreachable!();
46564656
},
46574657
Ok(res) => res
46584658
};
4659-
let update_res = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update);
4659+
let update_res = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), &monitor_update);
46604660
if let Err(e) = handle_monitor_update_res!(self, update_res, chan, RAACommitmentOrder::RevokeAndACKFirst, true, commitment_signed.is_some()) {
46614661
return Err(e);
46624662
}
@@ -4792,7 +4792,7 @@ where
47924792
let raa_updates = break_chan_entry!(self,
47934793
chan.get_mut().revoke_and_ack(&msg, &self.logger), chan);
47944794
htlcs_to_fail = raa_updates.holding_cell_failed_htlcs;
4795-
let update_res = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), raa_updates.monitor_update);
4795+
let update_res = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), &raa_updates.monitor_update);
47964796
if was_paused_for_mon_update {
47974797
assert!(update_res != ChannelMonitorUpdateStatus::Completed);
47984798
assert!(raa_updates.commitment_update.is_none());
@@ -5097,7 +5097,7 @@ where
50975097
));
50985098
}
50995099
if let Some((commitment_update, monitor_update)) = commitment_opt {
5100-
match self.chain_monitor.update_channel(chan.get_funding_txo().unwrap(), monitor_update) {
5100+
match self.chain_monitor.update_channel(chan.get_funding_txo().unwrap(), &monitor_update) {
51015101
ChannelMonitorUpdateStatus::Completed => {
51025102
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
51035103
node_id: chan.get_counterparty_node_id(),

lightning/src/ln/functional_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8135,8 +8135,8 @@ fn test_update_err_monitor_lockdown() {
81358135
let mut node_0_peer_state_lock;
81368136
let mut channel = get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1.2);
81378137
if let Ok((_, _, update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
8138-
assert_eq!(watchtower.chain_monitor.update_channel(outpoint, update.clone()), ChannelMonitorUpdateStatus::PermanentFailure);
8139-
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, update), ChannelMonitorUpdateStatus::Completed);
8138+
assert_eq!(watchtower.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::PermanentFailure);
8139+
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
81408140
} else { assert!(false); }
81418141
}
81428142
// Our local monitor is in-sync and hasn't processed yet timeout
@@ -8230,9 +8230,9 @@ fn test_concurrent_monitor_claim() {
82308230
let mut channel = get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1.2);
82318231
if let Ok((_, _, update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
82328232
// Watchtower Alice should already have seen the block and reject the update
8233-
assert_eq!(watchtower_alice.chain_monitor.update_channel(outpoint, update.clone()), ChannelMonitorUpdateStatus::PermanentFailure);
8234-
assert_eq!(watchtower_bob.chain_monitor.update_channel(outpoint, update.clone()), ChannelMonitorUpdateStatus::Completed);
8235-
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, update), ChannelMonitorUpdateStatus::Completed);
8233+
assert_eq!(watchtower_alice.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::PermanentFailure);
8234+
assert_eq!(watchtower_bob.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
8235+
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
82368236
} else { assert!(false); }
82378237
}
82388238
// Our local monitor is in-sync and hasn't processed yet timeout

lightning/src/util/persist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<ChannelSigner: Sign, K: KVStorePersister> Persist<ChannelSigner> for K {
9494
}
9595
}
9696

97-
fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &Option<ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
97+
fn update_persisted_channel(&self, funding_txo: OutPoint, _update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
9898
let key = format!("monitors/{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
9999
match self.persist(&key, monitor) {
100100
Ok(()) => chain::ChannelMonitorUpdateStatus::Completed,

lightning/src/util/test_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ impl<'a> chain::Watch<EnforcingSigner> for TestChainMonitor<'a> {
184184
self.chain_monitor.watch_channel(funding_txo, new_monitor)
185185
}
186186

187-
fn update_channel(&self, funding_txo: OutPoint, update: channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
187+
fn update_channel(&self, funding_txo: OutPoint, update: &channelmonitor::ChannelMonitorUpdate) -> chain::ChannelMonitorUpdateStatus {
188188
// Every monitor update should survive roundtrip
189189
let mut w = TestVecWriter(Vec::new());
190190
update.write(&mut w).unwrap();
191191
assert!(channelmonitor::ChannelMonitorUpdate::read(
192-
&mut io::Cursor::new(&w.0)).unwrap() == update);
192+
&mut io::Cursor::new(&w.0)).unwrap() == *update);
193193

194194
self.monitor_updates.lock().unwrap().entry(funding_txo.to_channel_id()).or_insert(Vec::new()).push(update.clone());
195195

@@ -202,7 +202,7 @@ impl<'a> chain::Watch<EnforcingSigner> for TestChainMonitor<'a> {
202202
}
203203

204204
self.latest_monitor_update_id.lock().unwrap().insert(funding_txo.to_channel_id(),
205-
(funding_txo, update.update_id, MonitorUpdateId::from_monitor_update(&update)));
205+
(funding_txo, update.update_id, MonitorUpdateId::from_monitor_update(update)));
206206
let update_res = self.chain_monitor.update_channel(funding_txo, update);
207207
// At every point where we get a monitor update, we should be able to send a useful monitor
208208
// to a watchtower and disk...
@@ -254,7 +254,7 @@ impl<Signer: keysinterface::Sign> chainmonitor::Persist<Signer> for TestPersiste
254254
chain::ChannelMonitorUpdateStatus::Completed
255255
}
256256

257-
fn update_persisted_channel(&self, funding_txo: OutPoint, update: &Option<channelmonitor::ChannelMonitorUpdate>, _data: &channelmonitor::ChannelMonitor<Signer>, update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
257+
fn update_persisted_channel(&self, funding_txo: OutPoint, update: Option<&channelmonitor::ChannelMonitorUpdate>, _data: &channelmonitor::ChannelMonitor<Signer>, update_id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus {
258258
let mut ret = chain::ChannelMonitorUpdateStatus::Completed;
259259
if let Some(update_ret) = self.update_rets.lock().unwrap().pop_front() {
260260
ret = update_ret;

0 commit comments

Comments
 (0)