24
24
//! servicing [`ChannelMonitor`] updates from the client.
25
25
26
26
use bitcoin:: blockdata:: block:: { Block , BlockHeader } ;
27
+ use bitcoin:: hash_types:: Txid ;
28
+ use bitcoin:: blockdata:: transaction:: TxOut ;
27
29
28
30
use chain;
29
31
use chain:: { Filter , WatchedOutput } ;
@@ -82,10 +84,68 @@ where C::Target: chain::Filter,
82
84
/// descendants of such transactions. It is not necessary to re-fetch the block to obtain
83
85
/// updated `txdata`.
84
86
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, must either be:
120
+ /// * called at at the fork point and then again for the most recent block -- intermediary
121
+ /// blocks may be skipped as before -- or
122
+ /// * not called in favor of calling [`transaction_unconfirmed`] for any affected transactions.
123
+ ///
124
+ /// [`block_connected`]: Self::block_connected
125
+ /// [`transactions_confirmed`]: Self::transactions_confirmed
126
+ /// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
127
+ pub fn update_best_block ( & self , header : & BlockHeader , height : u32 ) {
128
+ self . process_transactions ( header, & [ ] , |monitor, txdata| {
129
+ // Recursive calls can supply txdata when chain::Filter::register_output returns a
130
+ // transaction.
131
+ if txdata. is_empty ( ) {
132
+ monitor. update_best_block (
133
+ header, height, & * self . broadcaster , & * self . fee_estimator , & * self . logger )
134
+ } else {
135
+ monitor. transactions_confirmed (
136
+ header, txdata, height, & * self . broadcaster , & * self . fee_estimator , & * self . logger )
137
+ }
138
+ } ) ;
139
+ }
140
+
141
+ fn process_transactions < FN > ( & self , header : & BlockHeader , txdata : & TransactionData , process : FN )
142
+ where
143
+ FN : Fn ( & ChannelMonitor < ChannelSigner > , & TransactionData ) -> Vec < ( Txid , Vec < ( u32 , TxOut ) > ) >
144
+ {
85
145
let mut dependent_txdata = Vec :: new ( ) ;
86
146
let monitors = self . monitors . read ( ) . unwrap ( ) ;
87
147
for monitor in monitors. values ( ) {
88
- let mut txn_outputs = monitor . block_connected ( header , txdata, height , & * self . broadcaster , & * self . fee_estimator , & * self . logger ) ;
148
+ let mut txn_outputs = process ( monitor , txdata) ;
89
149
90
150
// Register any new outputs with the chain source for filtering, storing any dependent
91
151
// transactions from within the block that previously had not been included in txdata.
@@ -114,7 +174,7 @@ where C::Target: chain::Filter,
114
174
dependent_txdata. sort_unstable_by_key ( |( index, _tx) | * index) ;
115
175
dependent_txdata. dedup_by_key ( |( index, _tx) | * index) ;
116
176
let txdata: Vec < _ > = dependent_txdata. iter ( ) . map ( |( index, tx) | ( * index, tx) ) . collect ( ) ;
117
- self . block_connected ( header, & txdata, height ) ;
177
+ self . process_transactions ( header, & txdata, process ) ;
118
178
}
119
179
}
120
180
@@ -128,6 +188,37 @@ where C::Target: chain::Filter,
128
188
}
129
189
}
130
190
191
+ /// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
192
+ /// of a channel based on transactions unconfirmed as a result of a chain reorganization. See
193
+ /// [`ChannelMonitor::transaction_unconfirmed`] for details.
194
+ ///
195
+ /// Used instead of [`block_disconnected`] by clients that are notified of transactions rather
196
+ /// than blocks. May be called before or after [`update_best_block`] for transactions in the
197
+ /// corresponding block. See [`update_best_block`] for further calling expectations.
198
+ ///
199
+ /// [`block_disconnected`]: Self::block_disconnected
200
+ /// [`update_best_block`]: Self::update_best_block
201
+ pub fn transaction_unconfirmed ( & self , txid : & Txid ) {
202
+ let monitors = self . monitors . read ( ) . unwrap ( ) ;
203
+ for monitor in monitors. values ( ) {
204
+ monitor. transaction_unconfirmed ( txid, & * self . broadcaster , & * self . fee_estimator , & * self . logger ) ;
205
+ }
206
+ }
207
+
208
+ /// Returns the set of txids that should be monitored for confirmation on chain or for
209
+ /// re-organization out of the chain.
210
+ pub fn get_relevant_txids ( & self ) -> Vec < Txid > {
211
+ let mut txids = Vec :: new ( ) ;
212
+ let monitors = self . monitors . read ( ) . unwrap ( ) ;
213
+ for monitor in monitors. values ( ) {
214
+ txids. append ( & mut monitor. get_relevant_txids ( ) ) ;
215
+ }
216
+
217
+ txids. sort_unstable ( ) ;
218
+ txids. dedup ( ) ;
219
+ txids
220
+ }
221
+
131
222
/// Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
132
223
///
133
224
/// When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor
0 commit comments