From 851283d9e54f6bdb56524c49486b03b7bae732b1 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Thu, 6 Aug 2020 16:54:13 -0700 Subject: [PATCH] Remove Key parameter from ChainMonitor ChainMonitor's template Key parameter was meant to allow supporting both local monitoring, where Key=OutPoint, and watchtowers, where Key= (PublicKey, u32). Use OutPoint directly since the watchtower case will not be supported this way. --- fuzz/src/chanmon_consistency.rs | 2 +- fuzz/src/full_stack.rs | 7 ++-- lightning-net-tokio/src/lib.rs | 2 +- lightning/src/ln/channelmonitor.rs | 54 +++++++++++++----------------- lightning/src/util/test_utils.rs | 2 +- 5 files changed, 29 insertions(+), 38 deletions(-) diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index 332940b2f..ce407f329 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -83,7 +83,7 @@ impl Writer for VecWriter { struct TestChainMonitor { pub logger: Arc, - pub chain_monitor: Arc, Arc, Arc>>, + pub chain_monitor: Arc, Arc, Arc>>, pub update_ret: Mutex>, // If we reload a node with an old copy of ChannelMonitors, the ChannelManager deserialization // logic will automatically force-close our channels for us (as we don't have an up-to-date diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 8d1c68865..03d290320 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -145,14 +145,13 @@ impl<'a> std::hash::Hash for Peer<'a> { type ChannelMan = ChannelManager< EnforcingChannelKeys, - Arc, Arc, Arc>>, + Arc, Arc, Arc>>, Arc, Arc, Arc, Arc>; type PeerMan<'a> = PeerManager, Arc, Arc, Arc>>, Arc>; struct MoneyLossDetector<'a> { manager: Arc, - monitor: Arc, Arc, Arc>>, + monitor: Arc, Arc, Arc>>, handler: PeerMan<'a>, peers: &'a RefCell<[bool; 256]>, @@ -166,7 +165,7 @@ struct MoneyLossDetector<'a> { impl<'a> MoneyLossDetector<'a> { pub fn new(peers: &'a RefCell<[bool; 256]>, manager: Arc, - monitor: Arc, Arc, Arc>>, + monitor: Arc, Arc, Arc>>, handler: PeerMan<'a>) -> Self { MoneyLossDetector { manager, diff --git a/lightning-net-tokio/src/lib.rs b/lightning-net-tokio/src/lib.rs index d30711648..2e99b71df 100644 --- a/lightning-net-tokio/src/lib.rs +++ b/lightning-net-tokio/src/lib.rs @@ -35,7 +35,7 @@ //! type FeeEstimator = dyn lightning::chain::chaininterface::FeeEstimator; //! type Logger = dyn lightning::util::logger::Logger; //! type ChainAccess = dyn lightning::chain::Access; -//! type ChainMonitor = lightning::ln::channelmonitor::ChainMonitor, Arc, Arc>; +//! type ChainMonitor = lightning::ln::channelmonitor::ChainMonitor, Arc, Arc>; //! type ChannelManager = lightning::ln::channelmanager::SimpleArcChannelManager; //! type PeerManager = lightning::ln::peer_handler::SimpleArcPeerManager; //! diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs index 3f3f06f40..b601f741e 100644 --- a/lightning/src/ln/channelmonitor.rs +++ b/lightning/src/ln/channelmonitor.rs @@ -53,7 +53,7 @@ use util::events::Event; use std::collections::{HashMap, HashSet, hash_map}; use std::sync::Mutex; -use std::{hash,cmp, mem}; +use std::{cmp, mem}; use std::ops::Deref; use std::io::Error; @@ -188,28 +188,20 @@ pub struct HTLCUpdate { } impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source }); -/// A simple implementation of a [`chain::Watch`] and ChainListener. Can be used to create a -/// watchtower or watch our own channels. +/// An implementation of a [`chain::Watch`] and ChainListener. /// -/// Note that you must provide your own key by which to refer to channels. -/// -/// If you're accepting remote monitors (ie are implementing a watchtower), you must verify that -/// users cannot overwrite a given channel by providing a duplicate key. ie you should probably -/// index by a PublicKey which is required to sign any updates. -/// -/// If you're using this for local monitoring of your own channels, you probably want to use -/// `OutPoint` as the key, which will give you a [`chain::Watch`] implementation. +/// May be used in conjunction with [`ChannelManager`] to monitor channels locally or used +/// independently to monitor channels remotely. /// /// [`chain::Watch`]: ../../chain/trait.Watch.html -/// -/// (C-not exported) due to an unconstrained generic in `Key` -pub struct ChainMonitor +/// [`ChannelManager`]: ../channelmanager/struct.ChannelManager.html +pub struct ChainMonitor where T::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, { /// The monitors - pub monitors: Mutex>>, + pub monitors: Mutex>>, watch_events: Mutex, broadcaster: T, logger: L, @@ -277,8 +269,8 @@ impl WatchEventQueue { } } -impl - ChainListener for ChainMonitor +impl + ChainListener for ChainMonitor where T::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, @@ -308,14 +300,14 @@ impl ChainMonitor +impl ChainMonitor where T::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, { /// Creates a new object which can be used to monitor several channels given the chain /// interface with which to register to receive notifications. - pub fn new(broadcaster: T, logger: L, feeest: F) -> ChainMonitor { + pub fn new(broadcaster: T, logger: L, feeest: F) -> Self { Self { monitors: Mutex::new(HashMap::new()), watch_events: Mutex::new(WatchEventQueue::new()), @@ -325,12 +317,12 @@ impl) -> Result<(), MonitorUpdateError> { + /// Adds the monitor that watches the channel referred to by the given outpoint. + fn add_monitor(&self, outpoint: OutPoint, monitor: ChannelMonitor) -> Result<(), MonitorUpdateError> { let mut watch_events = self.watch_events.lock().unwrap(); let mut monitors = self.monitors.lock().unwrap(); - let entry = match monitors.entry(key) { - hash_map::Entry::Occupied(_) => return Err(MonitorUpdateError("Channel monitor for given key is already present")), + let entry = match monitors.entry(outpoint) { + hash_map::Entry::Occupied(_) => return Err(MonitorUpdateError("Channel monitor for given outpoint is already present")), hash_map::Entry::Vacant(e) => e, }; { @@ -348,10 +340,10 @@ impl Result<(), MonitorUpdateError> { + /// Updates the monitor that watches the channel referred to by the given outpoint. + fn update_monitor(&self, outpoint: OutPoint, update: ChannelMonitorUpdate) -> Result<(), MonitorUpdateError> { let mut monitors = self.monitors.lock().unwrap(); - match monitors.get_mut(&key) { + match monitors.get_mut(&outpoint) { Some(orig_monitor) => { log_trace!(self.logger, "Updating Channel Monitor for channel {}", log_funding_info!(orig_monitor)); orig_monitor.update_monitor(update, &self.broadcaster, &self.logger) @@ -361,7 +353,7 @@ impl chain::Watch for ChainMonitor +impl chain::Watch for ChainMonitor where T::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, @@ -369,14 +361,14 @@ impl) -> Result<(), ChannelMonitorUpdateErr> { - match self.add_monitor_by_key(funding_txo, monitor) { + match self.add_monitor(funding_txo, monitor) { Ok(_) => Ok(()), Err(_) => Err(ChannelMonitorUpdateErr::PermanentFailure), } } fn update_channel(&self, funding_txo: OutPoint, update: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr> { - match self.update_monitor_by_key(funding_txo, update) { + match self.update_monitor(funding_txo, update) { Ok(_) => Ok(()), Err(_) => Err(ChannelMonitorUpdateErr::PermanentFailure), } @@ -391,7 +383,7 @@ impl events::EventsProvider for ChainMonitor +impl events::EventsProvider for ChainMonitor where T::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, @@ -405,7 +397,7 @@ impl chain::WatchEventProvider for ChainMonitor +impl chain::WatchEventProvider for ChainMonitor where T::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index e505774cc..bc084b77e 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -62,7 +62,7 @@ impl chaininterface::FeeEstimator for TestFeeEstimator { pub struct TestChainMonitor<'a> { pub added_monitors: Mutex)>>, pub latest_monitor_update_id: Mutex>, - pub chain_monitor: channelmonitor::ChainMonitor, + pub chain_monitor: channelmonitor::ChainMonitor, pub update_ret: Mutex>, // If this is set to Some(), after the next return, we'll always return this until update_ret // is changed: -- 2.39.5