Skip to content

Commit f8fc32d

Browse files
committed
Make ManyChannelMonitor Keys an associated type
Instead of using a raw generic type, an associted type allows us to have explicit docs on the type, which is nice. More importantly, however, our automated bindings generator knows how to read associated types but not raw generics. Also, our bindings generator expects things which are referenced to have already been defined, so we move ManyChannelMonitor below the ChannelMonitor definition.
1 parent 8519e8f commit f8fc32d

File tree

4 files changed

+85
-73
lines changed

4 files changed

+85
-73
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ impl TestChannelMonitor {
9595
}
9696
}
9797
}
98-
impl channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMonitor {
98+
impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
99+
type Keys = EnforcingChannelKeys;
100+
99101
fn add_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<EnforcingChannelKeys>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
100102
let mut ser = VecWriter(Vec::new());
101103
monitor.write_for_disk(&mut ser).unwrap();

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, M, T, F> = ChannelManager<InMem
370370
/// SimpleArcChannelManager when you require a ChannelManager with a static lifetime, such as when
371371
/// you're using lightning-net-tokio.
372372
pub struct ChannelManager<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref>
373-
where M::Target: ManyChannelMonitor<ChanSigner>,
373+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
374374
T::Target: BroadcasterInterface,
375375
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
376376
F::Target: FeeEstimator,
@@ -682,7 +682,7 @@ macro_rules! maybe_break_monitor_err {
682682
}
683683

684684
impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelManager<ChanSigner, M, T, K, F>
685-
where M::Target: ManyChannelMonitor<ChanSigner>,
685+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
686686
T::Target: BroadcasterInterface,
687687
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
688688
F::Target: FeeEstimator,
@@ -2887,7 +2887,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
28872887
}
28882888

28892889
impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> events::MessageSendEventsProvider for ChannelManager<ChanSigner, M, T, K, F>
2890-
where M::Target: ManyChannelMonitor<ChanSigner>,
2890+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
28912891
T::Target: BroadcasterInterface,
28922892
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
28932893
F::Target: FeeEstimator,
@@ -2917,7 +2917,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> events::Me
29172917
}
29182918

29192919
impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> events::EventsProvider for ChannelManager<ChanSigner, M, T, K, F>
2920-
where M::Target: ManyChannelMonitor<ChanSigner>,
2920+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
29212921
T::Target: BroadcasterInterface,
29222922
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
29232923
F::Target: FeeEstimator,
@@ -2948,7 +2948,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> events::Ev
29482948

29492949
impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K: Deref + Sync + Send, F: Deref + Sync + Send>
29502950
ChainListener for ChannelManager<ChanSigner, M, T, K, F>
2951-
where M::Target: ManyChannelMonitor<ChanSigner>,
2951+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
29522952
T::Target: BroadcasterInterface,
29532953
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
29542954
F::Target: FeeEstimator,
@@ -3115,7 +3115,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
31153115

31163116
impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K: Deref + Sync + Send, F: Deref + Sync + Send>
31173117
ChannelMessageHandler for ChannelManager<ChanSigner, M, T, K, F>
3118-
where M::Target: ManyChannelMonitor<ChanSigner>,
3118+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
31193119
T::Target: BroadcasterInterface,
31203120
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
31213121
F::Target: FeeEstimator,
@@ -3553,7 +3553,7 @@ impl Readable for HTLCForwardInfo {
35533553
}
35543554

35553555
impl<ChanSigner: ChannelKeys + Writeable, M: Deref, T: Deref, K: Deref, F: Deref> Writeable for ChannelManager<ChanSigner, M, T, K, F>
3556-
where M::Target: ManyChannelMonitor<ChanSigner>,
3556+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
35573557
T::Target: BroadcasterInterface,
35583558
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
35593559
F::Target: FeeEstimator,
@@ -3630,7 +3630,7 @@ impl<ChanSigner: ChannelKeys + Writeable, M: Deref, T: Deref, K: Deref, F: Deref
36303630
/// 6) Disconnect/connect blocks on the ChannelManager.
36313631
/// 7) Register the new ChannelManager with your ChainWatchInterface.
36323632
pub struct ChannelManagerReadArgs<'a, ChanSigner: 'a + ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref>
3633-
where M::Target: ManyChannelMonitor<ChanSigner>,
3633+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
36343634
T::Target: BroadcasterInterface,
36353635
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
36363636
F::Target: FeeEstimator,
@@ -3679,7 +3679,7 @@ pub struct ChannelManagerReadArgs<'a, ChanSigner: 'a + ChannelKeys, M: Deref, T:
36793679
// SipmleArcChannelManager type:
36803680
impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: Deref>
36813681
ReadableArgs<ChannelManagerReadArgs<'a, ChanSigner, M, T, K, F>> for (BlockHash, Arc<ChannelManager<ChanSigner, M, T, K, F>>)
3682-
where M::Target: ManyChannelMonitor<ChanSigner>,
3682+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
36833683
T::Target: BroadcasterInterface,
36843684
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
36853685
F::Target: FeeEstimator,
@@ -3692,7 +3692,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
36923692

36933693
impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: Deref>
36943694
ReadableArgs<ChannelManagerReadArgs<'a, ChanSigner, M, T, K, F>> for (BlockHash, ChannelManager<ChanSigner, M, T, K, F>)
3695-
where M::Target: ManyChannelMonitor<ChanSigner>,
3695+
where M::Target: ManyChannelMonitor<Keys=ChanSigner>,
36963696
T::Target: BroadcasterInterface,
36973697
K::Target: KeysInterface<ChanKeySigner = ChanSigner>,
36983698
F::Target: FeeEstimator,

lightning/src/ln/channelmonitor.rs

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -149,66 +149,6 @@ pub struct HTLCUpdate {
149149
}
150150
impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source });
151151

152-
/// Simple trait indicating ability to track a set of ChannelMonitors and multiplex events between
153-
/// them. Generally should be implemented by keeping a local SimpleManyChannelMonitor and passing
154-
/// events to it, while also taking any add/update_monitor events and passing them to some remote
155-
/// server(s).
156-
///
157-
/// In general, you must always have at least one local copy in memory, which must never fail to
158-
/// update (as it is responsible for broadcasting the latest state in case the channel is closed),
159-
/// and then persist it to various on-disk locations. If, for some reason, the in-memory copy fails
160-
/// to update (eg out-of-memory or some other condition), you must immediately shut down without
161-
/// taking any further action such as writing the current state to disk. This should likely be
162-
/// accomplished via panic!() or abort().
163-
///
164-
/// Note that any updates to a channel's monitor *must* be applied to each instance of the
165-
/// channel's monitor everywhere (including remote watchtowers) *before* this function returns. If
166-
/// an update occurs and a remote watchtower is left with old state, it may broadcast transactions
167-
/// which we have revoked, allowing our counterparty to claim all funds in the channel!
168-
///
169-
/// User needs to notify implementors of ManyChannelMonitor when a new block is connected or
170-
/// disconnected using their `block_connected` and `block_disconnected` methods. However, rather
171-
/// than calling these methods directly, the user should register implementors as listeners to the
172-
/// BlockNotifier and call the BlockNotifier's `block_(dis)connected` methods, which will notify
173-
/// all registered listeners in one go.
174-
pub trait ManyChannelMonitor<ChanSigner: ChannelKeys>: Send + Sync {
175-
/// Adds a monitor for the given `funding_txo`.
176-
///
177-
/// Implementer must also ensure that the funding_txo txid *and* outpoint are registered with
178-
/// any relevant ChainWatchInterfaces such that the provided monitor receives block_connected
179-
/// callbacks with the funding transaction, or any spends of it.
180-
///
181-
/// Further, the implementer must also ensure that each output returned in
182-
/// monitor.get_outputs_to_watch() is registered to ensure that the provided monitor learns about
183-
/// any spends of any of the outputs.
184-
///
185-
/// Any spends of outputs which should have been registered which aren't passed to
186-
/// ChannelMonitors via block_connected may result in FUNDS LOSS.
187-
fn add_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor<ChanSigner>) -> Result<(), ChannelMonitorUpdateErr>;
188-
189-
/// Updates a monitor for the given `funding_txo`.
190-
///
191-
/// Implementer must also ensure that the funding_txo txid *and* outpoint are registered with
192-
/// any relevant ChainWatchInterfaces such that the provided monitor receives block_connected
193-
/// callbacks with the funding transaction, or any spends of it.
194-
///
195-
/// Further, the implementer must also ensure that each output returned in
196-
/// monitor.get_watch_outputs() is registered to ensure that the provided monitor learns about
197-
/// any spends of any of the outputs.
198-
///
199-
/// Any spends of outputs which should have been registered which aren't passed to
200-
/// ChannelMonitors via block_connected may result in FUNDS LOSS.
201-
fn update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr>;
202-
203-
/// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated
204-
/// with success or failure.
205-
///
206-
/// You should probably just call through to
207-
/// ChannelMonitor::get_and_clear_pending_htlcs_updated() for each ChannelMonitor and return
208-
/// the full list.
209-
fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate>;
210-
}
211-
212152
/// A simple implementation of a ManyChannelMonitor and ChainListener. Can be used to create a
213153
/// watchtower or watch our own channels.
214154
///
@@ -314,10 +254,12 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
314254
}
315255
}
316256

317-
impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send> ManyChannelMonitor<ChanSigner> for SimpleManyChannelMonitor<OutPoint, ChanSigner, T, F>
257+
impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send> ManyChannelMonitor for SimpleManyChannelMonitor<OutPoint, ChanSigner, T, F>
318258
where T::Target: BroadcasterInterface,
319259
F::Target: FeeEstimator
320260
{
261+
type Keys = ChanSigner;
262+
321263
fn add_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor<ChanSigner>) -> Result<(), ChannelMonitorUpdateErr> {
322264
match self.add_monitor_by_key(funding_txo, monitor) {
323265
Ok(_) => Ok(()),
@@ -794,6 +736,72 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
794736
logger: Arc<Logger>,
795737
}
796738

739+
/// Simple trait indicating ability to track a set of ChannelMonitors and multiplex events between
740+
/// them. Generally should be implemented by keeping a local SimpleManyChannelMonitor and passing
741+
/// events to it, while also taking any add/update_monitor events and passing them to some remote
742+
/// server(s).
743+
///
744+
/// In general, you must always have at least one local copy in memory, which must never fail to
745+
/// update (as it is responsible for broadcasting the latest state in case the channel is closed),
746+
/// and then persist it to various on-disk locations. If, for some reason, the in-memory copy fails
747+
/// to update (eg out-of-memory or some other condition), you must immediately shut down without
748+
/// taking any further action such as writing the current state to disk. This should likely be
749+
/// accomplished via panic!() or abort().
750+
///
751+
/// Note that any updates to a channel's monitor *must* be applied to each instance of the
752+
/// channel's monitor everywhere (including remote watchtowers) *before* this function returns. If
753+
/// an update occurs and a remote watchtower is left with old state, it may broadcast transactions
754+
/// which we have revoked, allowing our counterparty to claim all funds in the channel!
755+
///
756+
/// User needs to notify implementors of ManyChannelMonitor when a new block is connected or
757+
/// disconnected using their `block_connected` and `block_disconnected` methods. However, rather
758+
/// than calling these methods directly, the user should register implementors as listeners to the
759+
/// BlockNotifier and call the BlockNotifier's `block_(dis)connected` methods, which will notify
760+
/// all registered listeners in one go.
761+
pub trait ManyChannelMonitor: Send + Sync {
762+
/// The concrete type which signs for transactions and provides access to our channel public
763+
/// keys.
764+
type Keys: ChannelKeys;
765+
766+
/// Adds a monitor for the given `funding_txo`.
767+
///
768+
/// Implementer must also ensure that the funding_txo txid *and* outpoint are registered with
769+
/// any relevant ChainWatchInterfaces such that the provided monitor receives block_connected
770+
/// callbacks with the funding transaction, or any spends of it.
771+
///
772+
/// Further, the implementer must also ensure that each output returned in
773+
/// monitor.get_outputs_to_watch() is registered to ensure that the provided monitor learns about
774+
/// any spends of any of the outputs.
775+
///
776+
/// Any spends of outputs which should have been registered which aren't passed to
777+
/// ChannelMonitors via block_connected may result in FUNDS LOSS.
778+
fn add_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor<Self::Keys>) -> Result<(), ChannelMonitorUpdateErr>;
779+
780+
/// Updates a monitor for the given `funding_txo`.
781+
///
782+
/// Implementer must also ensure that the funding_txo txid *and* outpoint are registered with
783+
/// any relevant ChainWatchInterfaces such that the provided monitor receives block_connected
784+
/// callbacks with the funding transaction, or any spends of it.
785+
///
786+
/// Further, the implementer must also ensure that each output returned in
787+
/// monitor.get_watch_outputs() is registered to ensure that the provided monitor learns about
788+
/// any spends of any of the outputs.
789+
///
790+
/// Any spends of outputs which should have been registered which aren't passed to
791+
/// ChannelMonitors via block_connected may result in FUNDS LOSS.
792+
fn update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr>;
793+
794+
/// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated
795+
/// with success or failure.
796+
///
797+
/// You should probably just call through to
798+
/// ChannelMonitor::get_and_clear_pending_htlcs_updated() for each ChannelMonitor and return
799+
/// the full list.
800+
fn get_and_clear_pending_htlcs_updated(&self) -> Vec<HTLCUpdate>;
801+
}
802+
803+
804+
797805
#[cfg(any(test, feature = "fuzztarget"))]
798806
/// Used only in testing and fuzztarget to check serialization roundtrips don't change the
799807
/// underlying object

lightning/src/util/test_utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ impl<'a> TestChannelMonitor<'a> {
6666
}
6767
}
6868
}
69-
impl<'a> channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMonitor<'a> {
69+
impl<'a> channelmonitor::ManyChannelMonitor for TestChannelMonitor<'a> {
70+
type Keys = EnforcingChannelKeys;
71+
7072
fn add_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<EnforcingChannelKeys>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
7173
// At every point where we get a monitor update, we should be able to send a useful monitor
7274
// to a watchtower and disk...

0 commit comments

Comments
 (0)