From addc49d7999d86210d575dcff9377d18a86ccaf7 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, 30 insertions(+), 37 deletions(-) diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index c41b11d5..2face646 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -75,7 +75,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 931f2638..251c9659 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -137,14 +137,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]>, @@ -158,7 +157,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 460af8a4..c71f435b 100644 --- a/lightning-net-tokio/src/lib.rs +++ b/lightning-net-tokio/src/lib.rs @@ -26,7 +26,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 9e0f33b7..1ed76954 100644 --- a/lightning/src/ln/channelmonitor.rs +++ b/lightning/src/ln/channelmonitor.rs @@ -44,7 +44,7 @@ use util::{byte_utils, events}; use std::collections::{HashMap, hash_map}; use std::sync::Mutex; -use std::{hash,cmp, mem}; +use std::{cmp, mem}; use std::ops::Deref; /// An update generated by the underlying Channel itself which contains some new information the @@ -154,28 +154,22 @@ 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 -pub struct ChainMonitor +/// [`ChannelManager`]: ../channelmanager/struct.ChannelManager.html +pub struct ChainMonitor where T::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, { #[cfg(test)] // Used in ChannelManager tests to manipulate channels directly - pub monitors: Mutex>>, + pub monitors: Mutex>>, #[cfg(not(test))] - monitors: Mutex>>, + monitors: Mutex>>, watch_events: Mutex, broadcaster: T, logger: L, @@ -224,8 +218,8 @@ impl WatchEventQueue { } } -impl - ChainListener for ChainMonitor +impl + ChainListener for ChainMonitor where T::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, @@ -255,14 +249,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()), @@ -272,12 +266,12 @@ impl) -> Result<(), MonitorUpdateError> { + /// Adds or updates the monitor which monitors the channel referred to by the given outpoint. + pub 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, }; { @@ -295,10 +289,10 @@ impl Result<(), MonitorUpdateError> { + /// Updates the monitor which monitors the channel referred to by the given outpoint. + pub 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) @@ -308,7 +302,7 @@ impl chain::Watch for ChainMonitor +impl chain::Watch for ChainMonitor where T::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, @@ -316,14 +310,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), } @@ -338,7 +332,7 @@ impl events::EventsProvider for ChainMonitor +impl events::EventsProvider for ChainMonitor where T::Target: BroadcasterInterface, F::Target: FeeEstimator, L::Target: Logger, @@ -352,7 +346,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 d024c219..eb1db202 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -54,7 +54,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.30.2