Skip to content

Commit df28592

Browse files
committed
rustfmt chainmonitor selectively
1 parent 8fc49b5 commit df28592

File tree

1 file changed

+138
-57
lines changed

1 file changed

+138
-57
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 138 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ pub trait Persist<ChannelSigner: EcdsaChannelSigner> {
126126
///
127127
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
128128
/// [`Writeable::write`]: crate::util::ser::Writeable::write
129-
#[rustfmt::skip]
130-
fn persist_new_channel(&self, monitor_name: MonitorName, monitor: &ChannelMonitor<ChannelSigner>) -> ChannelMonitorUpdateStatus;
129+
fn persist_new_channel(
130+
&self, monitor_name: MonitorName, monitor: &ChannelMonitor<ChannelSigner>,
131+
) -> ChannelMonitorUpdateStatus;
131132

132133
/// Update one channel's data. The provided [`ChannelMonitor`] has already applied the given
133134
/// update.
@@ -166,8 +167,10 @@ pub trait Persist<ChannelSigner: EcdsaChannelSigner> {
166167
/// [`ChannelMonitorUpdateStatus`] for requirements when returning errors.
167168
///
168169
/// [`Writeable::write`]: crate::util::ser::Writeable::write
169-
#[rustfmt::skip]
170-
fn update_persisted_channel(&self, monitor_name: MonitorName, monitor_update: Option<&ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>) -> ChannelMonitorUpdateStatus;
170+
fn update_persisted_channel(
171+
&self, monitor_name: MonitorName, monitor_update: Option<&ChannelMonitorUpdate>,
172+
monitor: &ChannelMonitor<ChannelSigner>,
173+
) -> ChannelMonitorUpdateStatus;
171174
/// Prevents the channel monitor from being loaded on startup.
172175
///
173176
/// Archiving the data in a backup location (rather than deleting it fully) is useful for
@@ -350,19 +353,26 @@ where
350353
}
351354
}
352355

353-
#[rustfmt::skip]
354356
fn update_monitor_with_chain_data<FN>(
355-
&self, header: &Header, best_height: Option<u32>, txdata: &TransactionData, process: FN, channel_id: &ChannelId,
356-
monitor_state: &MonitorHolder<ChannelSigner>, channel_count: usize,
357-
) -> Result<(), ()> where FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<TransactionOutputs> {
357+
&self, header: &Header, best_height: Option<u32>, txdata: &TransactionData, process: FN,
358+
channel_id: &ChannelId, monitor_state: &MonitorHolder<ChannelSigner>, channel_count: usize,
359+
) -> Result<(), ()>
360+
where
361+
FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<TransactionOutputs>,
362+
{
358363
let monitor = &monitor_state.monitor;
359364
let logger = WithChannelMonitor::from(&self.logger, &monitor, None);
360365

361366
let mut txn_outputs = process(monitor, txdata);
362367

363368
let get_partition_key = |channel_id: &ChannelId| {
364369
let channel_id_bytes = channel_id.0;
365-
let channel_id_u32 = u32::from_be_bytes([channel_id_bytes[0], channel_id_bytes[1], channel_id_bytes[2], channel_id_bytes[3]]);
370+
let channel_id_u32 = u32::from_be_bytes([
371+
channel_id_bytes[0],
372+
channel_id_bytes[1],
373+
channel_id_bytes[2],
374+
channel_id_bytes[3],
375+
]);
366376
channel_id_u32.wrapping_add(best_height.unwrap_or_default())
367377
};
368378

@@ -374,23 +384,33 @@ where
374384

375385
let has_pending_claims = monitor_state.monitor.has_pending_claims();
376386
if has_pending_claims || get_partition_key(channel_id) % partition_factor == 0 {
377-
log_trace!(logger, "Syncing Channel Monitor for channel {}", log_funding_info!(monitor));
387+
log_trace!(
388+
logger,
389+
"Syncing Channel Monitor for channel {}",
390+
log_funding_info!(monitor)
391+
);
378392
// Even though we don't track monitor updates from chain-sync as pending, we still want
379393
// updates per-channel to be well-ordered so that users don't see a
380394
// `ChannelMonitorUpdate` after a channel persist for a channel with the same
381395
// `latest_update_id`.
382396
let _pending_monitor_updates = monitor_state.pending_monitor_updates.lock().unwrap();
383-
match self.persister.update_persisted_channel(monitor.persistence_key(), None, monitor) {
384-
ChannelMonitorUpdateStatus::Completed =>
385-
log_trace!(logger, "Finished syncing Channel Monitor for channel {} for block-data",
386-
log_funding_info!(monitor)
387-
),
397+
match self.persister.update_persisted_channel(monitor.persistence_key(), None, monitor)
398+
{
399+
ChannelMonitorUpdateStatus::Completed => log_trace!(
400+
logger,
401+
"Finished syncing Channel Monitor for channel {} for block-data",
402+
log_funding_info!(monitor)
403+
),
388404
ChannelMonitorUpdateStatus::InProgress => {
389-
log_trace!(logger, "Channel Monitor sync for channel {} in progress.", log_funding_info!(monitor));
390-
}
405+
log_trace!(
406+
logger,
407+
"Channel Monitor sync for channel {} in progress.",
408+
log_funding_info!(monitor)
409+
);
410+
},
391411
ChannelMonitorUpdateStatus::UnrecoverableError => {
392412
return Err(());
393-
}
413+
},
394414
}
395415
}
396416

@@ -406,7 +426,11 @@ where
406426
outpoint: OutPoint { txid, index: idx as u16 },
407427
script_pubkey: output.script_pubkey,
408428
};
409-
log_trace!(logger, "Adding monitoring for spends of outpoint {} to the filter", output.outpoint);
429+
log_trace!(
430+
logger,
431+
"Adding monitoring for spends of outpoint {} to the filter",
432+
output.outpoint
433+
);
410434
chain_source.register_output(output);
411435
}
412436
}
@@ -433,8 +457,10 @@ where
433457
/// [`NodeSigner`]: crate::sign::NodeSigner
434458
/// [`NodeSigner::get_peer_storage_key`]: crate::sign::NodeSigner::get_peer_storage_key
435459
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
436-
#[rustfmt::skip]
437-
pub fn new(chain_source: Option<C>, broadcaster: T, logger: L, feeest: F, persister: P, entropy_source: ES, our_peerstorage_encryption_key: PeerStorageKey) -> Self {
460+
pub fn new(
461+
chain_source: Option<C>, broadcaster: T, logger: L, feeest: F, persister: P,
462+
entropy_source: ES, our_peerstorage_encryption_key: PeerStorageKey,
463+
) -> Self {
438464
Self {
439465
monitors: RwLock::new(new_hash_map()),
440466
chain_source,
@@ -447,7 +473,7 @@ where
447473
highest_chain_height: AtomicUsize::new(0),
448474
event_notifier: Notifier::new(),
449475
pending_send_only_events: Mutex::new(Vec::new()),
450-
our_peerstorage_encryption_key
476+
our_peerstorage_encryption_key,
451477
}
452478
}
453479

@@ -547,37 +573,50 @@ where
547573
///
548574
/// Returns an [`APIError::APIMisuseError`] if `funding_txo` does not match any currently
549575
/// registered [`ChannelMonitor`]s.
550-
#[rustfmt::skip]
551-
pub fn channel_monitor_updated(&self, channel_id: ChannelId, completed_update_id: u64) -> Result<(), APIError> {
576+
pub fn channel_monitor_updated(
577+
&self, channel_id: ChannelId, completed_update_id: u64,
578+
) -> Result<(), APIError> {
552579
let monitors = self.monitors.read().unwrap();
553-
let monitor_data = if let Some(mon) = monitors.get(&channel_id) { mon } else {
554-
return Err(APIError::APIMisuseError { err: format!("No ChannelMonitor matching channel ID {} found", channel_id) });
580+
let monitor_data = if let Some(mon) = monitors.get(&channel_id) {
581+
mon
582+
} else {
583+
return Err(APIError::APIMisuseError {
584+
err: format!("No ChannelMonitor matching channel ID {} found", channel_id),
585+
});
555586
};
556587
let mut pending_monitor_updates = monitor_data.pending_monitor_updates.lock().unwrap();
557588
pending_monitor_updates.retain(|update_id| *update_id != completed_update_id);
558589

559590
// Note that we only check for pending non-chainsync monitor updates and we don't track monitor
560591
// updates resulting from chainsync in `pending_monitor_updates`.
561592
let monitor_is_pending_updates = monitor_data.has_pending_updates(&pending_monitor_updates);
562-
log_debug!(self.logger, "Completed off-chain monitor update {} for channel with channel ID {}, {}",
593+
log_debug!(
594+
self.logger,
595+
"Completed off-chain monitor update {} for channel with channel ID {}, {}",
563596
completed_update_id,
564597
channel_id,
565598
if monitor_is_pending_updates {
566599
"still have pending off-chain updates"
567600
} else {
568601
"all off-chain updates complete, returning a MonitorEvent"
569-
});
602+
}
603+
);
570604
if monitor_is_pending_updates {
571605
// If there are still monitor updates pending, we cannot yet construct a
572606
// Completed event.
573607
return Ok(());
574608
}
575609
let funding_txo = monitor_data.monitor.get_funding_txo();
576-
self.pending_monitor_events.lock().unwrap().push((funding_txo, channel_id, vec![MonitorEvent::Completed {
610+
self.pending_monitor_events.lock().unwrap().push((
577611
funding_txo,
578612
channel_id,
579-
monitor_update_id: monitor_data.monitor.get_latest_update_id(),
580-
}], monitor_data.monitor.get_counterparty_node_id()));
613+
vec![MonitorEvent::Completed {
614+
funding_txo,
615+
channel_id,
616+
monitor_update_id: monitor_data.monitor.get_latest_update_id(),
617+
}],
618+
monitor_data.monitor.get_counterparty_node_id(),
619+
));
581620

582621
self.event_notifier.notify();
583622
Ok(())
@@ -693,30 +732,37 @@ where
693732
///
694733
/// Depending on the implementation of [`Persist::archive_persisted_channel`] the monitor
695734
/// data could be moved to an archive location or removed entirely.
696-
#[rustfmt::skip]
697735
pub fn archive_fully_resolved_channel_monitors(&self) {
698736
let mut have_monitors_to_prune = false;
699737
for monitor_holder in self.monitors.read().unwrap().values() {
700738
let logger = WithChannelMonitor::from(&self.logger, &monitor_holder.monitor, None);
701-
let (is_fully_resolved, needs_persistence) = monitor_holder.monitor.check_and_update_full_resolution_status(&logger);
739+
let (is_fully_resolved, needs_persistence) =
740+
monitor_holder.monitor.check_and_update_full_resolution_status(&logger);
702741
if is_fully_resolved {
703742
have_monitors_to_prune = true;
704743
}
705744
if needs_persistence {
706-
self.persister.update_persisted_channel(monitor_holder.monitor.persistence_key(), None, &monitor_holder.monitor);
745+
self.persister.update_persisted_channel(
746+
monitor_holder.monitor.persistence_key(),
747+
None,
748+
&monitor_holder.monitor,
749+
);
707750
}
708751
}
709752
if have_monitors_to_prune {
710753
let mut monitors = self.monitors.write().unwrap();
711754
monitors.retain(|channel_id, monitor_holder| {
712755
let logger = WithChannelMonitor::from(&self.logger, &monitor_holder.monitor, None);
713-
let (is_fully_resolved, _) = monitor_holder.monitor.check_and_update_full_resolution_status(&logger);
756+
let (is_fully_resolved, _) =
757+
monitor_holder.monitor.check_and_update_full_resolution_status(&logger);
714758
if is_fully_resolved {
715-
log_info!(logger,
759+
log_info!(
760+
logger,
716761
"Archiving fully resolved ChannelMonitor for channel ID {}",
717762
channel_id
718763
);
719-
self.persister.archive_persisted_channel(monitor_holder.monitor.persistence_key());
764+
self.persister
765+
.archive_persisted_channel(monitor_holder.monitor.persistence_key());
720766
false
721767
} else {
722768
true
@@ -786,8 +832,11 @@ where
786832
InitFeatures::empty()
787833
}
788834

789-
#[rustfmt::skip]
790-
fn peer_connected(&self, _their_node_id: PublicKey, _msg: &Init, _inbound: bool) -> Result<(), ()> { Ok(()) }
835+
fn peer_connected(
836+
&self, _their_node_id: PublicKey, _msg: &Init, _inbound: bool,
837+
) -> Result<(), ()> {
838+
Ok(())
839+
}
791840
}
792841

793842
impl<
@@ -923,8 +972,9 @@ where
923972
P::Target: Persist<ChannelSigner>,
924973
ES::Target: EntropySource,
925974
{
926-
#[rustfmt::skip]
927-
fn watch_channel(&self, channel_id: ChannelId, monitor: ChannelMonitor<ChannelSigner>) -> Result<ChannelMonitorUpdateStatus, ()> {
975+
fn watch_channel(
976+
&self, channel_id: ChannelId, monitor: ChannelMonitor<ChannelSigner>,
977+
) -> Result<ChannelMonitorUpdateStatus, ()> {
928978
let logger = WithChannelMonitor::from(&self.logger, &monitor, None);
929979
let mut monitors = self.monitors.write().unwrap();
930980
let entry = match monitors.entry(channel_id) {
@@ -940,11 +990,19 @@ where
940990
let persist_res = self.persister.persist_new_channel(monitor.persistence_key(), &monitor);
941991
match persist_res {
942992
ChannelMonitorUpdateStatus::InProgress => {
943-
log_info!(logger, "Persistence of new ChannelMonitor for channel {} in progress", log_funding_info!(monitor));
993+
log_info!(
994+
logger,
995+
"Persistence of new ChannelMonitor for channel {} in progress",
996+
log_funding_info!(monitor)
997+
);
944998
pending_monitor_updates.push(update_id);
945999
},
9461000
ChannelMonitorUpdateStatus::Completed => {
947-
log_info!(logger, "Persistence of new ChannelMonitor for channel {} completed", log_funding_info!(monitor));
1001+
log_info!(
1002+
logger,
1003+
"Persistence of new ChannelMonitor for channel {} completed",
1004+
log_funding_info!(monitor)
1005+
);
9481006
},
9491007
ChannelMonitorUpdateStatus::UnrecoverableError => {
9501008
let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down.";
@@ -953,7 +1011,7 @@ where
9531011
},
9541012
}
9551013
if let Some(ref chain_source) = self.chain_source {
956-
monitor.load_outputs_to_watch(chain_source , &self.logger);
1014+
monitor.load_outputs_to_watch(chain_source, &self.logger);
9571015
}
9581016
entry.insert(MonitorHolder {
9591017
monitor,
@@ -962,8 +1020,9 @@ where
9621020
Ok(persist_res)
9631021
}
9641022

965-
#[rustfmt::skip]
966-
fn update_channel(&self, channel_id: ChannelId, update: &ChannelMonitorUpdate) -> ChannelMonitorUpdateStatus {
1023+
fn update_channel(
1024+
&self, channel_id: ChannelId, update: &ChannelMonitorUpdate,
1025+
) -> ChannelMonitorUpdateStatus {
9671026
// `ChannelMonitorUpdate`'s `channel_id` is `None` prior to 0.0.121 and all channels in those
9681027
// versions are V1-established. For 0.0.121+ the `channel_id` fields is always `Some`.
9691028
debug_assert_eq!(update.channel_id.unwrap(), channel_id);
@@ -985,13 +1044,24 @@ where
9851044
Some(monitor_state) => {
9861045
let monitor = &monitor_state.monitor;
9871046
let logger = WithChannelMonitor::from(&self.logger, &monitor, None);
988-
log_trace!(logger, "Updating ChannelMonitor to id {} for channel {}", update.update_id, log_funding_info!(monitor));
1047+
log_trace!(
1048+
logger,
1049+
"Updating ChannelMonitor to id {} for channel {}",
1050+
update.update_id,
1051+
log_funding_info!(monitor)
1052+
);
9891053

9901054
// We hold a `pending_monitor_updates` lock through `update_monitor` to ensure we
9911055
// have well-ordered updates from the users' point of view. See the
9921056
// `pending_monitor_updates` docs for more.
993-
let mut pending_monitor_updates = monitor_state.pending_monitor_updates.lock().unwrap();
994-
let update_res = monitor.update_monitor(update, &self.broadcaster, &self.fee_estimator, &self.logger);
1057+
let mut pending_monitor_updates =
1058+
monitor_state.pending_monitor_updates.lock().unwrap();
1059+
let update_res = monitor.update_monitor(
1060+
update,
1061+
&self.broadcaster,
1062+
&self.fee_estimator,
1063+
&self.logger,
1064+
);
9951065

9961066
let update_id = update.update_id;
9971067
let persist_res = if update_res.is_err() {
@@ -1001,9 +1071,17 @@ where
10011071
// while reading `channel_monitor` with updates from storage. Instead, we should persist
10021072
// the entire `channel_monitor` here.
10031073
log_warn!(logger, "Failed to update ChannelMonitor for channel {}. Going ahead and persisting the entire ChannelMonitor", log_funding_info!(monitor));
1004-
self.persister.update_persisted_channel(monitor.persistence_key(), None, monitor)
1074+
self.persister.update_persisted_channel(
1075+
monitor.persistence_key(),
1076+
None,
1077+
monitor,
1078+
)
10051079
} else {
1006-
self.persister.update_persisted_channel(monitor.persistence_key(), Some(update), monitor)
1080+
self.persister.update_persisted_channel(
1081+
monitor.persistence_key(),
1082+
Some(update),
1083+
monitor,
1084+
)
10071085
};
10081086
match persist_res {
10091087
ChannelMonitorUpdateStatus::InProgress => {
@@ -1015,7 +1093,8 @@ where
10151093
);
10161094
},
10171095
ChannelMonitorUpdateStatus::Completed => {
1018-
log_debug!(logger,
1096+
log_debug!(
1097+
logger,
10191098
"Persistence of ChannelMonitorUpdate id {:?} for channel {} completed",
10201099
update_id,
10211100
log_funding_info!(monitor)
@@ -1037,7 +1116,7 @@ where
10371116
} else {
10381117
persist_res
10391118
}
1040-
}
1119+
},
10411120
}
10421121
}
10431122

@@ -1087,14 +1166,16 @@ where
10871166
///
10881167
/// [`SpendableOutputs`]: events::Event::SpendableOutputs
10891168
/// [`BumpTransaction`]: events::Event::BumpTransaction
1090-
#[rustfmt::skip]
1091-
fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {
1169+
fn process_pending_events<H: Deref>(&self, handler: H)
1170+
where
1171+
H::Target: EventHandler,
1172+
{
10921173
for monitor_state in self.monitors.read().unwrap().values() {
10931174
match monitor_state.monitor.process_pending_events(&handler, &self.logger) {
10941175
Ok(()) => {},
1095-
Err(ReplayEvent ()) => {
1176+
Err(ReplayEvent()) => {
10961177
self.event_notifier.notify();
1097-
}
1178+
},
10981179
}
10991180
}
11001181
}

0 commit comments

Comments
 (0)