Skip to content

Commit 205214f

Browse files
committed
Electrum interface for ChainMonitor
Add an interface to ChainMonitor for Electrum users, delegating to the corresponding methods in each ChannelMonitor.
1 parent 476bb09 commit 205214f

File tree

1 file changed

+90
-2
lines changed

1 file changed

+90
-2
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
//! servicing [`ChannelMonitor`] updates from the client.
2525
2626
use bitcoin::blockdata::block::{Block, BlockHeader};
27+
use bitcoin::hash_types::Txid;
28+
use bitcoin::blockdata::transaction::TxOut;
2729

2830
use chain;
2931
use chain::{Filter, WatchedOutput};
@@ -82,10 +84,66 @@ where C::Target: chain::Filter,
8284
/// descendants of such transactions. It is not necessary to re-fetch the block to obtain
8385
/// updated `txdata`.
8486
pub fn block_connected(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
87+
self.process_transactions(header, txdata, |monitor, txdata| {
88+
monitor.block_connected(
89+
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
90+
});
91+
}
92+
93+
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
94+
/// of a channel and reacting accordingly to newly confirmed transactions. For details, see
95+
/// [`ChannelMonitor::transactions_confirmed`].
96+
///
97+
/// Used instead of [`block_connected`] by clients that are notified of transactions rather than
98+
/// blocks. May be called before or after [`update_best_block`] for transactions in the
99+
/// corresponding block. See [`update_best_block`] for further calling expectations.
100+
///
101+
/// [`block_connected`]: Self::block_connected
102+
/// [`update_best_block`]: Self::update_best_block
103+
pub fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
104+
self.process_transactions(header, txdata, |monitor, txdata| {
105+
monitor.transactions_confirmed(
106+
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
107+
});
108+
}
109+
110+
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
111+
/// of a channel and reacting accordingly based on the new chain tip. For details, see
112+
/// [`ChannelMonitor::update_best_block`].
113+
///
114+
/// Used instead of [`block_connected`] by clients that are notified of transactions rather than
115+
/// blocks. May be called before or after [`transactions_confirmed`] for the corresponding
116+
/// block.
117+
///
118+
/// Must be called after new blocks become available for the most recent block. Intermediary
119+
/// blocks, however, may be safely skipped. For a chain reorganization, this must be called at
120+
/// least at the fork point and then again for the most recent block; intermediary blocks may be
121+
/// skipped as before.
122+
///
123+
/// [`block_connected`]: Self::block_connected
124+
/// [`transactions_confirmed`]: Self::transactions_confirmed
125+
pub fn update_best_block(&self, header: &BlockHeader, height: u32) {
126+
self.process_transactions(header, &[], |monitor, txdata| {
127+
// Recursive calls can supply txdata when chain::Filter::register_output returns a
128+
// transaction.
129+
if txdata.is_empty() {
130+
monitor.update_best_block(
131+
header, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
132+
} else {
133+
monitor.transactions_confirmed(
134+
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
135+
}
136+
});
137+
}
138+
139+
fn process_transactions<FN>(&self, header: &BlockHeader, txdata: &TransactionData, process: FN)
140+
where
141+
FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<(Txid, Vec<(u32, TxOut)>)>
142+
{
85143
let mut dependent_txdata = Vec::new();
86144
let monitors = self.monitors.read().unwrap();
87145
for monitor in monitors.values() {
88-
let mut txn_outputs = monitor.block_connected(header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
146+
let mut txn_outputs = process(monitor, txdata);
89147

90148
// Register any new outputs with the chain source for filtering, storing any dependent
91149
// transactions from within the block that previously had not been included in txdata.
@@ -114,7 +172,7 @@ where C::Target: chain::Filter,
114172
dependent_txdata.sort_unstable_by_key(|(index, _tx)| *index);
115173
dependent_txdata.dedup_by_key(|(index, _tx)| *index);
116174
let txdata: Vec<_> = dependent_txdata.iter().map(|(index, tx)| (*index, tx)).collect();
117-
self.block_connected(header, &txdata, height);
175+
self.process_transactions(header, &txdata, process);
118176
}
119177
}
120178

@@ -128,6 +186,36 @@ where C::Target: chain::Filter,
128186
}
129187
}
130188

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

0 commit comments

Comments
 (0)