Skip to content

Commit addc49d

Browse files
committed
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.
1 parent a784109 commit addc49d

File tree

5 files changed

+30
-37
lines changed

5 files changed

+30
-37
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Writer for VecWriter {
7575

7676
struct TestChainMonitor {
7777
pub logger: Arc<dyn Logger>,
78-
pub chain_monitor: Arc<channelmonitor::ChainMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
78+
pub chain_monitor: Arc<channelmonitor::ChainMonitor<EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
7979
pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
8080
// If we reload a node with an old copy of ChannelMonitors, the ChannelManager deserialization
8181
// logic will automatically force-close our channels for us (as we don't have an up-to-date

fuzz/src/full_stack.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,13 @@ impl<'a> std::hash::Hash for Peer<'a> {
137137

138138
type ChannelMan = ChannelManager<
139139
EnforcingChannelKeys,
140-
Arc<channelmonitor::ChainMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
140+
Arc<channelmonitor::ChainMonitor<EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
141141
Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>;
142142
type PeerMan<'a> = PeerManager<Peer<'a>, Arc<ChannelMan>, Arc<NetGraphMsgHandler<Arc<dyn chain::Access>, Arc<dyn Logger>>>, Arc<dyn Logger>>;
143143

144144
struct MoneyLossDetector<'a> {
145145
manager: Arc<ChannelMan>,
146-
monitor: Arc<channelmonitor::ChainMonitor<
147-
OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
146+
monitor: Arc<channelmonitor::ChainMonitor<EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
148147
handler: PeerMan<'a>,
149148

150149
peers: &'a RefCell<[bool; 256]>,
@@ -158,7 +157,7 @@ struct MoneyLossDetector<'a> {
158157
impl<'a> MoneyLossDetector<'a> {
159158
pub fn new(peers: &'a RefCell<[bool; 256]>,
160159
manager: Arc<ChannelMan>,
161-
monitor: Arc<channelmonitor::ChainMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
160+
monitor: Arc<channelmonitor::ChainMonitor<EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
162161
handler: PeerMan<'a>) -> Self {
163162
MoneyLossDetector {
164163
manager,

lightning-net-tokio/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//! type FeeEstimator = dyn lightning::chain::chaininterface::FeeEstimator;
2727
//! type Logger = dyn lightning::util::logger::Logger;
2828
//! type ChainAccess = dyn lightning::chain::Access;
29-
//! type ChainMonitor = lightning::ln::channelmonitor::ChainMonitor<lightning::chain::transaction::OutPoint, lightning::chain::keysinterface::InMemoryChannelKeys, Arc<TxBroadcaster>, Arc<FeeEstimator>, Arc<Logger>>;
29+
//! type ChainMonitor = lightning::ln::channelmonitor::ChainMonitor<lightning::chain::keysinterface::InMemoryChannelKeys, Arc<TxBroadcaster>, Arc<FeeEstimator>, Arc<Logger>>;
3030
//! type ChannelManager = lightning::ln::channelmanager::SimpleArcChannelManager<ChainMonitor, TxBroadcaster, FeeEstimator, Logger>;
3131
//! type PeerManager = lightning::ln::peer_handler::SimpleArcPeerManager<lightning_net_tokio::SocketDescriptor, ChainMonitor, TxBroadcaster, FeeEstimator, ChainAccess, Logger>;
3232
//!

lightning/src/ln/channelmonitor.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use util::{byte_utils, events};
4444

4545
use std::collections::{HashMap, hash_map};
4646
use std::sync::Mutex;
47-
use std::{hash,cmp, mem};
47+
use std::{cmp, mem};
4848
use std::ops::Deref;
4949

5050
/// An update generated by the underlying Channel itself which contains some new information the
@@ -154,28 +154,22 @@ pub struct HTLCUpdate {
154154
}
155155
impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source });
156156

157-
/// A simple implementation of a [`chain::Watch`] and ChainListener. Can be used to create a
158-
/// watchtower or watch our own channels.
157+
/// An implementation of a [`chain::Watch`] and ChainListener.
159158
///
160-
/// Note that you must provide your own key by which to refer to channels.
161-
///
162-
/// If you're accepting remote monitors (ie are implementing a watchtower), you must verify that
163-
/// users cannot overwrite a given channel by providing a duplicate key. ie you should probably
164-
/// index by a PublicKey which is required to sign any updates.
165-
///
166-
/// If you're using this for local monitoring of your own channels, you probably want to use
167-
/// `OutPoint` as the key, which will give you a [`chain::Watch`] implementation.
159+
/// May be used in conjunction with [`ChannelManager`] to monitor channels locally or used
160+
/// independently to monitor channels remotely.
168161
///
169162
/// [`chain::Watch`]: ../../chain/trait.Watch.html
170-
pub struct ChainMonitor<Key, ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref>
163+
/// [`ChannelManager`]: ../channelmanager/struct.ChannelManager.html
164+
pub struct ChainMonitor<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref>
171165
where T::Target: BroadcasterInterface,
172166
F::Target: FeeEstimator,
173167
L::Target: Logger,
174168
{
175169
#[cfg(test)] // Used in ChannelManager tests to manipulate channels directly
176-
pub monitors: Mutex<HashMap<Key, ChannelMonitor<ChanSigner>>>,
170+
pub monitors: Mutex<HashMap<OutPoint, ChannelMonitor<ChanSigner>>>,
177171
#[cfg(not(test))]
178-
monitors: Mutex<HashMap<Key, ChannelMonitor<ChanSigner>>>,
172+
monitors: Mutex<HashMap<OutPoint, ChannelMonitor<ChanSigner>>>,
179173
watch_events: Mutex<WatchEventQueue>,
180174
broadcaster: T,
181175
logger: L,
@@ -224,8 +218,8 @@ impl WatchEventQueue {
224218
}
225219
}
226220

227-
impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L: Deref + Sync + Send>
228-
ChainListener for ChainMonitor<Key, ChanSigner, T, F, L>
221+
impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L: Deref + Sync + Send>
222+
ChainListener for ChainMonitor<ChanSigner, T, F, L>
229223
where T::Target: BroadcasterInterface,
230224
F::Target: FeeEstimator,
231225
L::Target: Logger,
@@ -255,14 +249,14 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref + Sync
255249
}
256250
}
257251

258-
impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<Key, ChanSigner, T, F, L>
252+
impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSigner, T, F, L>
259253
where T::Target: BroadcasterInterface,
260254
F::Target: FeeEstimator,
261255
L::Target: Logger,
262256
{
263257
/// Creates a new object which can be used to monitor several channels given the chain
264258
/// interface with which to register to receive notifications.
265-
pub fn new(broadcaster: T, logger: L, feeest: F) -> ChainMonitor<Key, ChanSigner, T, F, L> {
259+
pub fn new(broadcaster: T, logger: L, feeest: F) -> Self {
266260
Self {
267261
monitors: Mutex::new(HashMap::new()),
268262
watch_events: Mutex::new(WatchEventQueue::new()),
@@ -272,12 +266,12 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
272266
}
273267
}
274268

275-
/// Adds or updates the monitor which monitors the channel referred to by the given key.
276-
pub fn add_monitor_by_key(&self, key: Key, monitor: ChannelMonitor<ChanSigner>) -> Result<(), MonitorUpdateError> {
269+
/// Adds or updates the monitor which monitors the channel referred to by the given outpoint.
270+
pub fn add_monitor(&self, outpoint: OutPoint, monitor: ChannelMonitor<ChanSigner>) -> Result<(), MonitorUpdateError> {
277271
let mut watch_events = self.watch_events.lock().unwrap();
278272
let mut monitors = self.monitors.lock().unwrap();
279-
let entry = match monitors.entry(key) {
280-
hash_map::Entry::Occupied(_) => return Err(MonitorUpdateError("Channel monitor for given key is already present")),
273+
let entry = match monitors.entry(outpoint) {
274+
hash_map::Entry::Occupied(_) => return Err(MonitorUpdateError("Channel monitor for given outpoint is already present")),
281275
hash_map::Entry::Vacant(e) => e,
282276
};
283277
{
@@ -295,10 +289,10 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
295289
Ok(())
296290
}
297291

298-
/// Updates the monitor which monitors the channel referred to by the given key.
299-
pub fn update_monitor_by_key(&self, key: Key, update: ChannelMonitorUpdate) -> Result<(), MonitorUpdateError> {
292+
/// Updates the monitor which monitors the channel referred to by the given outpoint.
293+
pub fn update_monitor(&self, outpoint: OutPoint, update: ChannelMonitorUpdate) -> Result<(), MonitorUpdateError> {
300294
let mut monitors = self.monitors.lock().unwrap();
301-
match monitors.get_mut(&key) {
295+
match monitors.get_mut(&outpoint) {
302296
Some(orig_monitor) => {
303297
log_trace!(self.logger, "Updating Channel Monitor for channel {}", log_funding_info!(orig_monitor));
304298
orig_monitor.update_monitor(update, &self.broadcaster, &self.logger)
@@ -308,22 +302,22 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
308302
}
309303
}
310304

311-
impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L: Deref + Sync + Send> chain::Watch for ChainMonitor<OutPoint, ChanSigner, T, F, L>
305+
impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L: Deref + Sync + Send> chain::Watch for ChainMonitor<ChanSigner, T, F, L>
312306
where T::Target: BroadcasterInterface,
313307
F::Target: FeeEstimator,
314308
L::Target: Logger,
315309
{
316310
type Keys = ChanSigner;
317311

318312
fn watch_channel(&self, funding_txo: OutPoint, monitor: ChannelMonitor<ChanSigner>) -> Result<(), ChannelMonitorUpdateErr> {
319-
match self.add_monitor_by_key(funding_txo, monitor) {
313+
match self.add_monitor(funding_txo, monitor) {
320314
Ok(_) => Ok(()),
321315
Err(_) => Err(ChannelMonitorUpdateErr::PermanentFailure),
322316
}
323317
}
324318

325319
fn update_channel(&self, funding_txo: OutPoint, update: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr> {
326-
match self.update_monitor_by_key(funding_txo, update) {
320+
match self.update_monitor(funding_txo, update) {
327321
Ok(_) => Ok(()),
328322
Err(_) => Err(ChannelMonitorUpdateErr::PermanentFailure),
329323
}
@@ -338,7 +332,7 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
338332
}
339333
}
340334

341-
impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> events::EventsProvider for ChainMonitor<Key, ChanSigner, T, F, L>
335+
impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> events::EventsProvider for ChainMonitor<ChanSigner, T, F, L>
342336
where T::Target: BroadcasterInterface,
343337
F::Target: FeeEstimator,
344338
L::Target: Logger,
@@ -352,7 +346,7 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref, F: De
352346
}
353347
}
354348

355-
impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> chain::WatchEventProvider for ChainMonitor<Key, ChanSigner, T, F, L>
349+
impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> chain::WatchEventProvider for ChainMonitor<ChanSigner, T, F, L>
356350
where T::Target: BroadcasterInterface,
357351
F::Target: FeeEstimator,
358352
L::Target: Logger,

lightning/src/util/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl chaininterface::FeeEstimator for TestFeeEstimator {
5454
pub struct TestChainMonitor<'a> {
5555
pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>>,
5656
pub latest_monitor_update_id: Mutex<HashMap<[u8; 32], (OutPoint, u64)>>,
57-
pub chain_monitor: channelmonitor::ChainMonitor<OutPoint, EnforcingChannelKeys, &'a chaininterface::BroadcasterInterface, &'a TestFeeEstimator, &'a TestLogger>,
57+
pub chain_monitor: channelmonitor::ChainMonitor<EnforcingChannelKeys, &'a chaininterface::BroadcasterInterface, &'a TestFeeEstimator, &'a TestLogger>,
5858
pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
5959
// If this is set to Some(), after the next return, we'll always return this until update_ret
6060
// is changed:

0 commit comments

Comments
 (0)