Skip to content

Commit 6267610

Browse files
committed
f - pass MonitorName as parameter
1 parent e3c9061 commit 6267610

File tree

6 files changed

+46
-40
lines changed

6 files changed

+46
-40
lines changed

fuzz/src/utils/test_persister.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ pub struct TestPersister {
1010
}
1111
impl chainmonitor::Persist<TestChannelSigner> for TestPersister {
1212
fn persist_new_channel(
13-
&self, _data: &channelmonitor::ChannelMonitor<TestChannelSigner>,
13+
&self, _monitor_name: MonitorName,
14+
_data: &channelmonitor::ChannelMonitor<TestChannelSigner>,
1415
) -> chain::ChannelMonitorUpdateStatus {
1516
self.update_ret.lock().unwrap().clone()
1617
}
1718

1819
fn update_persisted_channel(
19-
&self, _update: Option<&channelmonitor::ChannelMonitorUpdate>,
20+
&self, _monitor_name: MonitorName, _update: Option<&channelmonitor::ChannelMonitorUpdate>,
2021
_data: &channelmonitor::ChannelMonitor<TestChannelSigner>,
2122
) -> chain::ChannelMonitorUpdateStatus {
2223
self.update_ret.lock().unwrap().clone()

lightning-persister/src/fs_store.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ mod tests {
618618
perms.set_readonly(true);
619619
fs::set_permissions(path, perms).unwrap();
620620

621-
match store.persist_new_channel(&added_monitors[0].1) {
621+
let monitor_name = added_monitors[0].1.persistence_key();
622+
match store.persist_new_channel(monitor_name, &added_monitors[0].1) {
622623
ChannelMonitorUpdateStatus::UnrecoverableError => {},
623624
_ => panic!("unexpected result from persisting new channel"),
624625
}
@@ -665,7 +666,8 @@ mod tests {
665666
// handle, hence why the test is Windows-only.
666667
let store = FilesystemStore::new(":<>/".into());
667668

668-
match store.persist_new_channel(&added_monitors[0].1) {
669+
let monitor_name = added_monitors[0].1.persistence_key();
670+
match store.persist_new_channel(monitor_name, &added_monitors[0].1) {
669671
ChannelMonitorUpdateStatus::UnrecoverableError => {},
670672
_ => panic!("unexpected result from persisting new channel"),
671673
}

lightning/src/chain/chainmonitor.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ pub trait Persist<ChannelSigner: EcdsaChannelSigner> {
105105
/// Persist a new channel's data in response to a [`chain::Watch::watch_channel`] call. This is
106106
/// called by [`ChannelManager`] for new channels, or may be called directly, e.g. on startup.
107107
///
108-
/// The data can be stored any way you want, so long as [`ChannelMonitor::persistence_key`] is
109-
/// used to maintain a correct mapping with the stored channel data. Note that you **must**
110-
/// persist every new monitor to disk.
108+
/// The data can be stored any way you want, so long as `monitor_name` is used to maintain a
109+
/// correct mapping with the stored channel data. Note that you **must** persist every new
110+
/// monitor to disk.
111111
///
112112
/// The [`ChannelMonitor::get_latest_update_id`] uniquely links this call to [`ChainMonitor::channel_monitor_updated`].
113113
/// For [`Persist::persist_new_channel`], it is only necessary to call [`ChainMonitor::channel_monitor_updated`]
@@ -118,7 +118,7 @@ pub trait Persist<ChannelSigner: EcdsaChannelSigner> {
118118
///
119119
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
120120
/// [`Writeable::write`]: crate::util::ser::Writeable::write
121-
fn persist_new_channel(&self, monitor: &ChannelMonitor<ChannelSigner>) -> ChannelMonitorUpdateStatus;
121+
fn persist_new_channel(&self, monitor_name: MonitorName, monitor: &ChannelMonitor<ChannelSigner>) -> ChannelMonitorUpdateStatus;
122122

123123
/// Update one channel's data. The provided [`ChannelMonitor`] has already applied the given
124124
/// update.
@@ -157,7 +157,7 @@ pub trait Persist<ChannelSigner: EcdsaChannelSigner> {
157157
/// [`ChannelMonitorUpdateStatus`] for requirements when returning errors.
158158
///
159159
/// [`Writeable::write`]: crate::util::ser::Writeable::write
160-
fn update_persisted_channel(&self, monitor_update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>) -> ChannelMonitorUpdateStatus;
160+
fn update_persisted_channel(&self, monitor_name: MonitorName, monitor_update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>) -> ChannelMonitorUpdateStatus;
161161
/// Prevents the channel monitor from being loaded on startup.
162162
///
163163
/// Archiving the data in a backup location (rather than deleting it fully) is useful for
@@ -343,7 +343,7 @@ where C::Target: chain::Filter,
343343
// `ChannelMonitorUpdate` after a channel persist for a channel with the same
344344
// `latest_update_id`.
345345
let _pending_monitor_updates = monitor_state.pending_monitor_updates.lock().unwrap();
346-
match self.persister.update_persisted_channel(None, monitor) {
346+
match self.persister.update_persisted_channel(monitor.persistence_key(), None, monitor) {
347347
ChannelMonitorUpdateStatus::Completed =>
348348
log_trace!(logger, "Finished syncing Channel Monitor for channel {} for block-data",
349349
log_funding_info!(monitor)
@@ -642,7 +642,7 @@ where C::Target: chain::Filter,
642642
have_monitors_to_prune = true;
643643
}
644644
if needs_persistence {
645-
self.persister.update_persisted_channel(None, &monitor_holder.monitor);
645+
self.persister.update_persisted_channel(monitor_holder.monitor.persistence_key(), None, &monitor_holder.monitor);
646646
}
647647
}
648648
if have_monitors_to_prune {
@@ -769,7 +769,7 @@ where C::Target: chain::Filter,
769769
log_trace!(logger, "Got new ChannelMonitor for channel {}", log_funding_info!(monitor));
770770
let update_id = monitor.get_latest_update_id();
771771
let mut pending_monitor_updates = Vec::new();
772-
let persist_res = self.persister.persist_new_channel(&monitor);
772+
let persist_res = self.persister.persist_new_channel(monitor.persistence_key(), &monitor);
773773
match persist_res {
774774
ChannelMonitorUpdateStatus::InProgress => {
775775
log_info!(logger, "Persistence of new ChannelMonitor for channel {} in progress", log_funding_info!(monitor));
@@ -832,9 +832,9 @@ where C::Target: chain::Filter,
832832
// while reading `channel_monitor` with updates from storage. Instead, we should persist
833833
// the entire `channel_monitor` here.
834834
log_warn!(logger, "Failed to update ChannelMonitor for channel {}. Going ahead and persisting the entire ChannelMonitor", log_funding_info!(monitor));
835-
self.persister.update_persisted_channel(None, monitor)
835+
self.persister.update_persisted_channel(monitor.persistence_key(), None, monitor)
836836
} else {
837-
self.persister.update_persisted_channel(Some(update), monitor)
837+
self.persister.update_persisted_channel(monitor.persistence_key(), Some(update), monitor)
838838
};
839839
match persist_res {
840840
ChannelMonitorUpdateStatus::InProgress => {

lightning/src/chain/channelmonitor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,13 +1466,13 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
14661466
})
14671467
}
14681468

1469-
/// Returns a unique id for persisting the [`ChannelMonitor`], which may be useful as a key in a
1469+
/// Returns a unique id for persisting the [`ChannelMonitor`], which is used as a key in a
14701470
/// key-value store.
14711471
///
14721472
/// Note: Previously, the funding outpoint was used in the [`Persist`] trait. However, since the
1473-
/// outpoint may change during splicing, using this method to obtain a unique key is recommended
1474-
/// instead. For v1 channels, the funding outpoint is still used for backwards compatibility,
1475-
/// whereas v2 channels use the channel id since it is fixed.
1473+
/// outpoint may change during splicing, this method is used to obtain a unique key instead. For
1474+
/// v1 channels, the funding outpoint is still used for backwards compatibility, whereas v2
1475+
/// channels use the channel id since it is fixed.
14761476
///
14771477
/// [`Persist`]: crate::chain::chainmonitor::Persist
14781478
pub fn persistence_key(&self) -> MonitorName {

lightning/src/util/persist.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,8 @@ impl<ChannelSigner: EcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSign
261261
// just shut down the node since we're not retrying persistence!
262262

263263
fn persist_new_channel(
264-
&self, monitor: &ChannelMonitor<ChannelSigner>,
264+
&self, monitor_name: MonitorName, monitor: &ChannelMonitor<ChannelSigner>,
265265
) -> chain::ChannelMonitorUpdateStatus {
266-
let monitor_name = monitor.persistence_key();
267266
match self.write(
268267
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
269268
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
@@ -276,9 +275,9 @@ impl<ChannelSigner: EcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSign
276275
}
277276

278277
fn update_persisted_channel(
279-
&self, _update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>,
278+
&self, monitor_name: MonitorName, _update: Option<&ChannelMonitorUpdate>,
279+
monitor: &ChannelMonitor<ChannelSigner>,
280280
) -> chain::ChannelMonitorUpdateStatus {
281-
let monitor_name = monitor.persistence_key();
282281
match self.write(
283282
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
284283
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
@@ -706,10 +705,9 @@ where
706705
/// Persists a new channel. This means writing the entire monitor to the
707706
/// parametrized [`KVStore`].
708707
fn persist_new_channel(
709-
&self, monitor: &ChannelMonitor<ChannelSigner>,
708+
&self, monitor_name: MonitorName, monitor: &ChannelMonitor<ChannelSigner>,
710709
) -> chain::ChannelMonitorUpdateStatus {
711710
// Determine the proper key for this monitor
712-
let monitor_name = monitor.persistence_key();
713711
let monitor_key = monitor_name.to_string();
714712
// Serialize and write the new monitor
715713
let mut monitor_bytes = Vec::with_capacity(
@@ -748,15 +746,15 @@ where
748746
/// `update` is `None`.
749747
/// - The update is at [`u64::MAX`], indicating an update generated by pre-0.1 LDK.
750748
fn update_persisted_channel(
751-
&self, update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>,
749+
&self, monitor_name: MonitorName, update: Option<&ChannelMonitorUpdate>,
750+
monitor: &ChannelMonitor<ChannelSigner>,
752751
) -> chain::ChannelMonitorUpdateStatus {
753752
const LEGACY_CLOSED_CHANNEL_UPDATE_ID: u64 = u64::MAX;
754753
if let Some(update) = update {
755-
let monitor_name = monitor.persistence_key();
754+
let monitor_key = monitor_name.to_string();
756755
let persist_update = update.update_id != LEGACY_CLOSED_CHANNEL_UPDATE_ID
757756
&& update.update_id % self.maximum_pending_updates != 0;
758757
if persist_update {
759-
let monitor_key = monitor_name.to_string();
760758
let update_name = UpdateName::from(update.update_id);
761759
match self.kv_store.write(
762760
CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE,
@@ -778,7 +776,6 @@ where
778776
},
779777
}
780778
} else {
781-
let monitor_key = monitor_name.to_string();
782779
// In case of channel-close monitor update, we need to read old monitor before persisting
783780
// the new one in order to determine the cleanup range.
784781
let maybe_old_monitor = match monitor.get_latest_update_id() {
@@ -789,7 +786,7 @@ where
789786
};
790787

791788
// We could write this update, but it meets criteria of our design that calls for a full monitor write.
792-
let monitor_update_status = self.persist_new_channel(monitor);
789+
let monitor_update_status = self.persist_new_channel(monitor_name, monitor);
793790

794791
if let chain::ChannelMonitorUpdateStatus::Completed = monitor_update_status {
795792
let channel_closed_legacy =
@@ -820,7 +817,7 @@ where
820817
}
821818
} else {
822819
// There is no update given, so we must persist a new monitor.
823-
self.persist_new_channel(monitor)
820+
self.persist_new_channel(monitor_name, monitor)
824821
}
825822
}
826823

@@ -923,7 +920,7 @@ where
923920
/// // Using MonitorName to generate a storage key
924921
/// let storage_key = format!("channel_monitors/{}", monitor_name);
925922
/// ```
926-
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
923+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
927924
pub enum MonitorName {
928925
/// The outpoint of the channel's funding transaction.
929926
V1Channel(OutPoint),
@@ -1358,7 +1355,8 @@ mod tests {
13581355
broadcaster: node_cfgs[0].tx_broadcaster,
13591356
fee_estimator: node_cfgs[0].fee_estimator,
13601357
};
1361-
match ro_persister.persist_new_channel(&added_monitors[0].1) {
1358+
let monitor_name = added_monitors[0].1.persistence_key();
1359+
match ro_persister.persist_new_channel(monitor_name, &added_monitors[0].1) {
13621360
ChannelMonitorUpdateStatus::UnrecoverableError => {
13631361
// correct result
13641362
},
@@ -1369,7 +1367,11 @@ mod tests {
13691367
panic!("Returned InProgress when shouldn't have")
13701368
},
13711369
}
1372-
match ro_persister.update_persisted_channel(Some(cmu), &added_monitors[0].1) {
1370+
match ro_persister.update_persisted_channel(
1371+
monitor_name,
1372+
Some(cmu),
1373+
&added_monitors[0].1,
1374+
) {
13731375
ChannelMonitorUpdateStatus::UnrecoverableError => {
13741376
// correct result
13751377
},

lightning/src/util/test_utils.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,9 @@ impl WatchtowerPersister {
588588
#[cfg(test)]
589589
impl<Signer: sign::ecdsa::EcdsaChannelSigner> Persist<Signer> for WatchtowerPersister {
590590
fn persist_new_channel(
591-
&self, data: &ChannelMonitor<Signer>,
591+
&self, monitor_name: MonitorName, data: &ChannelMonitor<Signer>,
592592
) -> chain::ChannelMonitorUpdateStatus {
593-
let res = self.persister.persist_new_channel(data);
593+
let res = self.persister.persist_new_channel(monitor_name, data);
594594

595595
assert!(self
596596
.unsigned_justice_tx_data
@@ -621,9 +621,10 @@ impl<Signer: sign::ecdsa::EcdsaChannelSigner> Persist<Signer> for WatchtowerPers
621621
}
622622

623623
fn update_persisted_channel(
624-
&self, update: Option<&ChannelMonitorUpdate>, data: &ChannelMonitor<Signer>,
624+
&self, monitor_name: MonitorName, update: Option<&ChannelMonitorUpdate>,
625+
data: &ChannelMonitor<Signer>,
625626
) -> chain::ChannelMonitorUpdateStatus {
626-
let res = self.persister.update_persisted_channel(update, data);
627+
let res = self.persister.update_persisted_channel(monitor_name, update, data);
627628

628629
if let Some(update) = update {
629630
let commitment_txs = data.counterparty_commitment_txs_from_update(update);
@@ -697,7 +698,7 @@ impl TestPersister {
697698
}
698699
impl<Signer: sign::ecdsa::EcdsaChannelSigner> Persist<Signer> for TestPersister {
699700
fn persist_new_channel(
700-
&self, _data: &ChannelMonitor<Signer>,
701+
&self, _monitor_name: MonitorName, _data: &ChannelMonitor<Signer>,
701702
) -> chain::ChannelMonitorUpdateStatus {
702703
if let Some(update_ret) = self.update_rets.lock().unwrap().pop_front() {
703704
return update_ret;
@@ -706,14 +707,14 @@ impl<Signer: sign::ecdsa::EcdsaChannelSigner> Persist<Signer> for TestPersister
706707
}
707708

708709
fn update_persisted_channel(
709-
&self, update: Option<&ChannelMonitorUpdate>, data: &ChannelMonitor<Signer>,
710+
&self, monitor_name: MonitorName, update: Option<&ChannelMonitorUpdate>,
711+
_data: &ChannelMonitor<Signer>,
710712
) -> chain::ChannelMonitorUpdateStatus {
711713
let mut ret = chain::ChannelMonitorUpdateStatus::Completed;
712714
if let Some(update_ret) = self.update_rets.lock().unwrap().pop_front() {
713715
ret = update_ret;
714716
}
715717

716-
let monitor_name = data.persistence_key();
717718
if let Some(update) = update {
718719
self.offchain_monitor_updates
719720
.lock()

0 commit comments

Comments
 (0)