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,66 @@ 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, 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
+ {
85
143
let mut dependent_txdata = Vec :: new ( ) ;
86
144
let monitors = self . monitors . read ( ) . unwrap ( ) ;
87
145
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) ;
89
147
90
148
// Register any new outputs with the chain source for filtering, storing any dependent
91
149
// transactions from within the block that previously had not been included in txdata.
@@ -114,7 +172,7 @@ where C::Target: chain::Filter,
114
172
dependent_txdata. sort_unstable_by_key ( |( index, _tx) | * index) ;
115
173
dependent_txdata. dedup_by_key ( |( index, _tx) | * index) ;
116
174
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 ) ;
118
176
}
119
177
}
120
178
@@ -128,6 +186,36 @@ where C::Target: chain::Filter,
128
186
}
129
187
}
130
188
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
+
131
219
/// Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
132
220
///
133
221
/// When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor
0 commit comments