Skip to content

Commit 58108f1

Browse files
Aditya SharmaAditya Sharma
Aditya Sharma
authored and
Aditya Sharma
committed
chainwatch: Add 'watch_dummy' to handle ChannelMonitor created from YourPeerStorage.
1 parent 6514152 commit 58108f1

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
186186
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)> {
187187
return self.chain_monitor.release_pending_monitor_events();
188188
}
189+
190+
fn watch_dummy(&self, funding_outpoint: OutPoint, monitor: ChannelMonitor<ChannelSigner>) -> Result<ChannelMonitorUpdateStatus, ()> {
191+
return self.chain_monitor.watch_dummy(funding_outpoint, monitor);
192+
}
189193
}
190194

191195
struct KeyProvider {

lightning/src/chain/chainmonitor.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,47 @@ where C::Target: chain::Filter,
816816
L::Target: Logger,
817817
P::Target: Persist<ChannelSigner>,
818818
{
819+
fn watch_dummy(&self, funding_outpoint: OutPoint, monitor: ChannelMonitor<ChannelSigner>) -> Result<ChannelMonitorUpdateStatus, ()> {
820+
let logger = WithChannelMonitor::from(&self.logger, &monitor);
821+
let mut monitors = self.monitors.write().unwrap();
822+
let entry = match monitors.entry(funding_outpoint) {
823+
hash_map::Entry::Occupied(_) => {
824+
log_trace!(logger, "Channel already present {}", monitor.channel_id());
825+
return Err(());
826+
},
827+
hash_map::Entry::Vacant(e) => e,
828+
};
829+
log_trace!(logger, "Got new dummy ChannelMonitor for channel {}", monitor.channel_id());
830+
let update_id = MonitorUpdateId::from_new_monitor(&monitor);
831+
let mut pending_monitor_updates = Vec::new();
832+
let persist_res = self.persister.persist_new_channel(funding_outpoint, &monitor, update_id);
833+
834+
match persist_res {
835+
ChannelMonitorUpdateStatus::InProgress => {
836+
log_info!(logger, "Persistence of new ChannelMonitor for channel {} in progress", log_funding_info!(monitor));
837+
pending_monitor_updates.push(update_id);
838+
},
839+
ChannelMonitorUpdateStatus::Completed => {
840+
log_info!(logger, "Persistence of new ChannelMonitor for channel {} completed", log_funding_info!(monitor));
841+
},
842+
ChannelMonitorUpdateStatus::UnrecoverableError => {
843+
let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down.";
844+
log_error!(logger, "{}", err_str);
845+
panic!("{}", err_str);
846+
},
847+
}
848+
if let Some(ref chain_source) = self.chain_source {
849+
monitor.load_outputs_to_watch(chain_source , &self.logger);
850+
}
851+
entry.insert(MonitorHolder {
852+
monitor,
853+
pending_monitor_updates: Mutex::new(pending_monitor_updates),
854+
last_chain_persist_height: AtomicUsize::new(self.highest_chain_height.load(Ordering::Acquire)),
855+
});
856+
857+
Ok(persist_res)
858+
}
859+
819860
fn watch_channel(&self, funding_outpoint: OutPoint, monitor: ChannelMonitor<ChannelSigner>) -> Result<ChannelMonitorUpdateStatus, ()> {
820861
let logger = WithChannelMonitor::from(&self.logger, &monitor);
821862
let mut monitors = self.monitors.write().unwrap();

lightning/src/chain/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ pub trait Watch<ChannelSigner: WriteableEcdsaChannelSigner> {
302302
/// For details on asynchronous [`ChannelMonitor`] updating and returning
303303
/// [`MonitorEvent::Completed`] here, see [`ChannelMonitorUpdateStatus::InProgress`].
304304
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)>;
305+
306+
/// Watches a dummy channel identified by `funding_txo` using `monitor`.
307+
/// This is called when we receive a peer storage and finds an unknown channel in it.
308+
///
309+
/// A return of `Err(())` indicates that the channel is already being tracked and there is no
310+
/// need to take any action.
311+
///
312+
/// If the given `funding_txo` has previously been registered via `watch_channel`, `Err(())`
313+
/// must be returned.
314+
fn watch_dummy(&self, funding_outpoint: OutPoint, monitor: ChannelMonitor<ChannelSigner>) -> Result<ChannelMonitorUpdateStatus, ()>;
305315
}
306316

307317
/// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to

lightning/src/util/test_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {
396396
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)> {
397397
return self.chain_monitor.release_pending_monitor_events();
398398
}
399+
400+
fn watch_dummy(&self, funding_outpoint: OutPoint, monitor: channelmonitor::ChannelMonitor<TestChannelSigner>) -> Result<chain::ChannelMonitorUpdateStatus, ()> {
401+
return self.chain_monitor.watch_dummy(funding_outpoint, monitor);
402+
}
399403
}
400404

401405
#[cfg(test)]

0 commit comments

Comments
 (0)