Skip to content

Commit 93d20ff

Browse files
committed
Implement chain::Confirm for ChainMonitor
1 parent 23c4c8b commit 93d20ff

File tree

1 file changed

+47
-73
lines changed

1 file changed

+47
-73
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 47 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -89,49 +89,6 @@ where C::Target: chain::Filter,
8989
});
9090
}
9191

92-
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
93-
/// of a channel and reacting accordingly to newly confirmed transactions. For details, see
94-
/// [`ChannelMonitor::transactions_confirmed`].
95-
///
96-
/// Used instead of [`block_connected`] by clients that are notified of transactions rather than
97-
/// blocks. May be called before or after [`best_block_updated`] for transactions in the
98-
/// corresponding block. See [`best_block_updated`] for further calling expectations.
99-
///
100-
/// [`block_connected`]: Self::block_connected
101-
/// [`best_block_updated`]: Self::best_block_updated
102-
pub fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
103-
self.process_chain_data(header, txdata, |monitor, txdata| {
104-
monitor.transactions_confirmed(
105-
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
106-
});
107-
}
108-
109-
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
110-
/// of a channel and reacting accordingly based on the new chain tip. For details, see
111-
/// [`ChannelMonitor::best_block_updated`].
112-
///
113-
/// Used instead of [`block_connected`] by clients that are notified of transactions rather than
114-
/// blocks. May be called before or after [`transactions_confirmed`] for the corresponding
115-
/// block.
116-
///
117-
/// Must be called after new blocks become available for the most recent block. Intermediary
118-
/// blocks, however, may be safely skipped. In the event of a chain re-organization, this only
119-
/// needs to be called for the most recent block assuming `transaction_unconfirmed` is called
120-
/// for any affected transactions.
121-
///
122-
/// [`block_connected`]: Self::block_connected
123-
/// [`transactions_confirmed`]: Self::transactions_confirmed
124-
/// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
125-
pub fn best_block_updated(&self, header: &BlockHeader, height: u32) {
126-
self.process_chain_data(header, &[], |monitor, txdata| {
127-
// While in practice there shouldn't be any recursive calls when given empty txdata,
128-
// it's still possible if a chain::Filter implementation returns a transaction.
129-
debug_assert!(txdata.is_empty());
130-
monitor.best_block_updated(
131-
header, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
132-
});
133-
}
134-
13592
fn process_chain_data<FN>(&self, header: &BlockHeader, txdata: &TransactionData, process: FN)
13693
where
13794
FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<TransactionOutputs>
@@ -182,36 +139,6 @@ where C::Target: chain::Filter,
182139
}
183140
}
184141

185-
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
186-
/// of a channel based on transactions unconfirmed as a result of a chain reorganization. See
187-
/// [`ChannelMonitor::transaction_unconfirmed`] for details.
188-
///
189-
/// Used instead of [`block_disconnected`] by clients that are notified of transactions rather
190-
/// than blocks. May be called before or after [`best_block_updated`] for transactions in the
191-
/// corresponding block. See [`best_block_updated`] for further calling expectations.
192-
///
193-
/// [`block_disconnected`]: Self::block_disconnected
194-
/// [`best_block_updated`]: Self::best_block_updated
195-
pub fn transaction_unconfirmed(&self, txid: &Txid) {
196-
let monitors = self.monitors.read().unwrap();
197-
for monitor in monitors.values() {
198-
monitor.transaction_unconfirmed(txid, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
199-
}
200-
}
201-
202-
/// Returns the set of txids that should be monitored for re-organization out of the chain.
203-
pub fn get_relevant_txids(&self) -> Vec<Txid> {
204-
let mut txids = Vec::new();
205-
let monitors = self.monitors.read().unwrap();
206-
for monitor in monitors.values() {
207-
txids.append(&mut monitor.get_relevant_txids());
208-
}
209-
210-
txids.sort_unstable();
211-
txids.dedup();
212-
txids
213-
}
214-
215142
/// Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
216143
///
217144
/// When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor
@@ -251,6 +178,53 @@ where
251178
}
252179
}
253180

181+
impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
182+
chain::Confirm for ChainMonitor<ChannelSigner, C, T, F, L, P>
183+
where
184+
ChannelSigner: Sign,
185+
C::Target: chain::Filter,
186+
T::Target: BroadcasterInterface,
187+
F::Target: FeeEstimator,
188+
L::Target: Logger,
189+
P::Target: channelmonitor::Persist<ChannelSigner>,
190+
{
191+
fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
192+
self.process_chain_data(header, txdata, |monitor, txdata| {
193+
monitor.transactions_confirmed(
194+
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
195+
});
196+
}
197+
198+
fn transaction_unconfirmed(&self, txid: &Txid) {
199+
let monitors = self.monitors.read().unwrap();
200+
for monitor in monitors.values() {
201+
monitor.transaction_unconfirmed(txid, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
202+
}
203+
}
204+
205+
fn best_block_updated(&self, header: &BlockHeader, height: u32) {
206+
self.process_chain_data(header, &[], |monitor, txdata| {
207+
// While in practice there shouldn't be any recursive calls when given empty txdata,
208+
// it's still possible if a chain::Filter implementation returns a transaction.
209+
debug_assert!(txdata.is_empty());
210+
monitor.best_block_updated(
211+
header, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
212+
});
213+
}
214+
215+
fn get_relevant_txids(&self) -> Vec<Txid> {
216+
let mut txids = Vec::new();
217+
let monitors = self.monitors.read().unwrap();
218+
for monitor in monitors.values() {
219+
txids.append(&mut monitor.get_relevant_txids());
220+
}
221+
222+
txids.sort_unstable();
223+
txids.dedup();
224+
txids
225+
}
226+
}
227+
254228
impl<ChannelSigner: Sign, C: Deref + Sync + Send, T: Deref + Sync + Send, F: Deref + Sync + Send, L: Deref + Sync + Send, P: Deref + Sync + Send>
255229
chain::Watch<ChannelSigner> for ChainMonitor<ChannelSigner, C, T, F, L, P>
256230
where C::Target: chain::Filter,

0 commit comments

Comments
 (0)