Skip to content

Commit 4682ae3

Browse files
committed
Electrum interface for ChainMonitor
Add transactions_confirmed, transaction_unconfirmed, and update_best_block methods to ChainMonitor, delegating to the corresponding method in each ChannelMonitor.
1 parent 0f7985e commit 4682ae3

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 45 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,43 @@ 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+
/// TODO: Write docs.
94+
pub fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
95+
self.process_transactions(header, txdata, |monitor, txdata| {
96+
monitor.transactions_confirmed(
97+
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
98+
});
99+
}
100+
101+
/// TODO: Write docs.
102+
pub fn update_best_block(&self, header: &BlockHeader, height: u32) {
103+
self.process_transactions(header, &[], |monitor, txdata| {
104+
// Recursive calls can supply txdata when chain::Filter::register_output returns a
105+
// transaction.
106+
if txdata.is_empty() {
107+
monitor.update_best_block(
108+
header, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
109+
} else {
110+
monitor.transactions_confirmed(
111+
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
112+
}
113+
});
114+
}
115+
116+
fn process_transactions<FN>(&self, header: &BlockHeader, txdata: &TransactionData, process: FN)
117+
where
118+
FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<(Txid, Vec<(u32, TxOut)>)>
119+
{
85120
let mut dependent_txdata = Vec::new();
86121
let monitors = self.monitors.read().unwrap();
87122
for monitor in monitors.values() {
88-
let mut txn_outputs = monitor.block_connected(header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
123+
let mut txn_outputs = process(monitor, txdata);
89124

90125
// Register any new outputs with the chain source for filtering, storing any dependent
91126
// transactions from within the block that previously had not been included in txdata.
@@ -114,7 +149,7 @@ where C::Target: chain::Filter,
114149
dependent_txdata.sort_unstable_by_key(|(index, _tx)| *index);
115150
dependent_txdata.dedup_by_key(|(index, _tx)| *index);
116151
let txdata: Vec<_> = dependent_txdata.iter().map(|(index, tx)| (*index, tx)).collect();
117-
self.block_connected(header, &txdata, height);
152+
self.process_transactions(header, &txdata, process);
118153
}
119154
}
120155

@@ -128,6 +163,14 @@ where C::Target: chain::Filter,
128163
}
129164
}
130165

166+
/// TODO: Write docs.
167+
pub fn transaction_unconfirmed(&self, txid: &Txid) {
168+
let monitors = self.monitors.read().unwrap();
169+
for monitor in monitors.values() {
170+
monitor.transaction_unconfirmed(txid, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
171+
}
172+
}
173+
131174
/// Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
132175
///
133176
/// When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor

0 commit comments

Comments
 (0)