@@ -1304,12 +1304,15 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1304
1304
header, height, broadcaster, fee_estimator, logger)
1305
1305
}
1306
1306
1307
- /// Processes transactions from a block with the given header and height, returning new outputs
1308
- /// to watch. See [`block_connected`] for details.
1307
+ /// Processes transactions confirmed in a block with the given header and height, returning new
1308
+ /// outputs to watch. See [`block_connected`] for details.
1309
1309
///
1310
- /// TODO: Expand docs.
1310
+ /// Used instead of [`block_connected`] by clients that are notified of transactions rather than
1311
+ /// blocks. May be called before or after [`update_best_block`] for transactions in the
1312
+ /// corresponding block. See [`update_best_block`] for further calling expectations.
1311
1313
///
1312
1314
/// [`block_connected`]: Self::block_connected
1315
+ /// [`update_best_block`]: Self::update_best_block
1313
1316
pub fn transactions_confirmed < B : Deref , F : Deref , L : Deref > (
1314
1317
& self ,
1315
1318
header : & BlockHeader ,
@@ -1327,6 +1330,35 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1327
1330
self . inner . lock ( ) . unwrap ( ) . transactions_confirmed (
1328
1331
header, txdata, height, broadcaster, fee_estimator, logger)
1329
1332
}
1333
+
1334
+ /// Updates the monitor with the current best chain tip, returning new outputs to watch. See
1335
+ /// [`block_connected`] for details.
1336
+ ///
1337
+ /// Used instead of [`block_connected`] by clients that are notified of transactions rather than
1338
+ /// blocks. May be called before or after [`transactions_confirmed`] for the corresponding
1339
+ /// block.
1340
+ ///
1341
+ /// Must be called after new blocks become available for the most recent block. Intermediary
1342
+ /// blocks, however, may be safely skipped.
1343
+ ///
1344
+ /// [`block_connected`]: Self::block_connected
1345
+ /// [`transactions_confirmed`]: Self::transactions_confirmed
1346
+ pub fn update_best_block < B : Deref , F : Deref , L : Deref > (
1347
+ & self ,
1348
+ header : & BlockHeader ,
1349
+ height : u32 ,
1350
+ broadcaster : B ,
1351
+ fee_estimator : F ,
1352
+ logger : L ,
1353
+ ) -> Vec < ( Txid , Vec < ( u32 , TxOut ) > ) >
1354
+ where
1355
+ B :: Target : BroadcasterInterface ,
1356
+ F :: Target : FeeEstimator ,
1357
+ L :: Target : Logger ,
1358
+ {
1359
+ self . inner . lock ( ) . unwrap ( ) . update_best_block (
1360
+ header, height, broadcaster, fee_estimator, logger)
1361
+ }
1330
1362
}
1331
1363
1332
1364
impl < Signer : Sign > ChannelMonitorImpl < Signer > {
@@ -2028,10 +2060,40 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2028
2060
F :: Target : FeeEstimator ,
2029
2061
L :: Target : Logger ,
2030
2062
{
2031
- self . best_block = BestBlock :: new ( header. block_hash ( ) , height) ;
2063
+ let block_hash = header. block_hash ( ) ;
2064
+ log_trace ! ( logger, "New best block {} at height {}" , block_hash, height) ;
2065
+ self . best_block = BestBlock :: new ( block_hash, height) ;
2066
+
2032
2067
self . transactions_confirmed ( header, txdata, height, broadcaster, fee_estimator, logger)
2033
2068
}
2034
2069
2070
+ fn update_best_block < B : Deref , F : Deref , L : Deref > (
2071
+ & mut self ,
2072
+ header : & BlockHeader ,
2073
+ height : u32 ,
2074
+ broadcaster : B ,
2075
+ fee_estimator : F ,
2076
+ logger : L ,
2077
+ ) -> Vec < ( Txid , Vec < ( u32 , TxOut ) > ) >
2078
+ where
2079
+ B :: Target : BroadcasterInterface ,
2080
+ F :: Target : FeeEstimator ,
2081
+ L :: Target : Logger ,
2082
+ {
2083
+ let block_hash = header. block_hash ( ) ;
2084
+ log_trace ! ( logger, "New best block {} at height {}" , block_hash, height) ;
2085
+
2086
+ if height > self . best_block . height ( ) {
2087
+ self . best_block = BestBlock :: new ( block_hash, height) ;
2088
+ self . block_confirmed ( height, vec ! [ ] , vec ! [ ] , vec ! [ ] , broadcaster, fee_estimator, logger)
2089
+ } else {
2090
+ self . best_block = BestBlock :: new ( block_hash, height) ;
2091
+ self . onchain_events_waiting_threshold_conf . retain ( |ref entry| entry. height <= height) ;
2092
+ self . onchain_tx_handler . block_disconnected ( height + 1 , broadcaster, fee_estimator, logger) ;
2093
+ Vec :: new ( )
2094
+ }
2095
+ }
2096
+
2035
2097
fn transactions_confirmed < B : Deref , F : Deref , L : Deref > (
2036
2098
& mut self ,
2037
2099
header : & BlockHeader ,
@@ -2100,11 +2162,28 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2100
2162
2101
2163
self . is_paying_spendable_output ( & tx, height, & logger) ;
2102
2164
}
2165
+
2166
+ self . block_confirmed ( height, txn_matched, watch_outputs, claimable_outpoints, broadcaster, fee_estimator, logger)
2167
+ }
2168
+
2169
+ fn block_confirmed < B : Deref , F : Deref , L : Deref > (
2170
+ & mut self ,
2171
+ height : u32 ,
2172
+ txn_matched : Vec < & Transaction > ,
2173
+ mut watch_outputs : Vec < ( Txid , Vec < ( u32 , TxOut ) > ) > ,
2174
+ mut claimable_outpoints : Vec < ClaimRequest > ,
2175
+ broadcaster : B ,
2176
+ fee_estimator : F ,
2177
+ logger : L ,
2178
+ ) -> Vec < ( Txid , Vec < ( u32 , TxOut ) > ) >
2179
+ where
2180
+ B :: Target : BroadcasterInterface ,
2181
+ F :: Target : FeeEstimator ,
2182
+ L :: Target : Logger ,
2183
+ {
2103
2184
let should_broadcast = self . would_broadcast_at_height ( height, & logger) ;
2104
2185
if should_broadcast {
2105
2186
claimable_outpoints. push ( ClaimRequest { absolute_timelock : height, aggregable : false , outpoint : BitcoinOutPoint { txid : self . funding_info . 0 . txid . clone ( ) , vout : self . funding_info . 0 . index as u32 } , witness_data : InputMaterial :: Funding { funding_redeemscript : self . funding_redeemscript . clone ( ) } } ) ;
2106
- }
2107
- if should_broadcast {
2108
2187
self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxBroadcasted ( self . funding_info . 0 ) ) ;
2109
2188
let commitment_tx = self . onchain_tx_handler . get_fully_signed_holder_tx ( & self . funding_redeemscript ) ;
2110
2189
self . holder_tx_signed = true ;
@@ -2211,7 +2290,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2211
2290
//We may discard:
2212
2291
//- htlc update there as failure-trigger tx (revoked commitment tx, non-revoked commitment tx, HTLC-timeout tx) has been disconnected
2213
2292
//- maturing spendable output has transaction paying us has been disconnected
2214
- self . onchain_events_waiting_threshold_conf . retain ( |ref entry| entry. height != height) ;
2293
+ self . onchain_events_waiting_threshold_conf . retain ( |ref entry| entry. height < height) ;
2215
2294
2216
2295
self . onchain_tx_handler . block_disconnected ( height, broadcaster, fee_estimator, logger) ;
2217
2296
0 commit comments