21
21
//! ChannelMonitors to get out of the HSM and onto monitoring devices.
22
22
23
23
use bitcoin:: blockdata:: block:: BlockHeader ;
24
- use bitcoin:: blockdata:: transaction:: { TxOut , Transaction } ;
25
- use bitcoin:: blockdata:: transaction:: OutPoint as BitcoinOutPoint ;
24
+ use bitcoin:: blockdata:: transaction:: { OutPoint as BitcoinOutPoint , TxOut , Transaction } ;
26
25
use bitcoin:: blockdata:: script:: { Script , Builder } ;
27
26
use bitcoin:: blockdata:: opcodes;
28
27
@@ -44,13 +43,17 @@ use chain::{BestBlock, WatchedOutput};
44
43
use chain:: chaininterface:: { BroadcasterInterface , FeeEstimator , LowerBoundedFeeEstimator } ;
45
44
use chain:: transaction:: { OutPoint , TransactionData } ;
46
45
use chain:: keysinterface:: { SpendableOutputDescriptor , StaticPaymentOutputDescriptor , DelayedPaymentOutputDescriptor , Sign , KeysInterface } ;
46
+ #[ cfg( anchors) ]
47
+ use chain:: onchaintx:: ClaimEvent ;
47
48
use chain:: onchaintx:: OnchainTxHandler ;
48
49
use chain:: package:: { CounterpartyOfferedHTLCOutput , CounterpartyReceivedHTLCOutput , HolderFundingOutput , HolderHTLCOutput , PackageSolvingData , PackageTemplate , RevokedOutput , RevokedHTLCOutput } ;
49
50
use chain:: Filter ;
50
51
use util:: logger:: Logger ;
51
52
use util:: ser:: { Readable , ReadableArgs , MaybeReadable , Writer , Writeable , U48 , OptionDeserWrapper } ;
52
53
use util:: byte_utils;
53
54
use util:: events:: Event ;
55
+ #[ cfg( anchors) ]
56
+ use util:: events:: { AnchorDescriptor , BumpTransactionEvent } ;
54
57
55
58
use prelude:: * ;
56
59
use core:: { cmp, mem} ;
@@ -263,6 +266,20 @@ impl_writeable_tlv_based!(HolderSignedTx, {
263
266
( 14 , htlc_outputs, vec_type)
264
267
} ) ;
265
268
269
+ #[ cfg( anchors) ]
270
+ impl HolderSignedTx {
271
+ fn non_dust_htlcs ( & self ) -> Vec < HTLCOutputInCommitment > {
272
+ self . htlc_outputs . iter ( ) . filter_map ( |( htlc, _, _) | {
273
+ if let Some ( _) = htlc. transaction_output_index {
274
+ Some ( htlc. clone ( ) )
275
+ } else {
276
+ None
277
+ }
278
+ } )
279
+ . collect ( )
280
+ }
281
+ }
282
+
266
283
/// We use this to track static counterparty commitment transaction data and to generate any
267
284
/// justice or 2nd-stage preimage/timeout transactions.
268
285
#[ derive( PartialEq , Eq ) ]
@@ -1221,7 +1238,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1221
1238
B :: Target : BroadcasterInterface ,
1222
1239
L :: Target : Logger ,
1223
1240
{
1224
- self . inner . lock ( ) . unwrap ( ) . broadcast_latest_holder_commitment_txn ( broadcaster, logger)
1241
+ self . inner . lock ( ) . unwrap ( ) . broadcast_latest_holder_commitment_txn ( broadcaster, logger) ;
1225
1242
}
1226
1243
1227
1244
/// Updates a ChannelMonitor on the basis of some new information provided by the Channel
@@ -2222,6 +2239,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2222
2239
panic ! ( "Attempted to apply ChannelMonitorUpdates out of order, check the update_id before passing an update to update_monitor!" ) ;
2223
2240
}
2224
2241
let mut ret = Ok ( ( ) ) ;
2242
+ let bounded_fee_estimator = LowerBoundedFeeEstimator :: new ( & * fee_estimator) ;
2225
2243
for update in updates. updates . iter ( ) {
2226
2244
match update {
2227
2245
ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs } => {
@@ -2239,7 +2257,6 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2239
2257
} ,
2240
2258
ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage } => {
2241
2259
log_trace ! ( logger, "Updating ChannelMonitor with payment preimage" ) ;
2242
- let bounded_fee_estimator = LowerBoundedFeeEstimator :: new ( & * fee_estimator) ;
2243
2260
self . provide_payment_preimage ( & PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 [ ..] ) . into_inner ( ) ) , & payment_preimage, broadcaster, & bounded_fee_estimator, logger)
2244
2261
} ,
2245
2262
ChannelMonitorUpdateStep :: CommitmentSecret { idx, secret } => {
@@ -2255,6 +2272,25 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2255
2272
self . lockdown_from_offchain = true ;
2256
2273
if * should_broadcast {
2257
2274
self . broadcast_latest_holder_commitment_txn ( broadcaster, logger) ;
2275
+ // If the channel supports anchor outputs, we'll need to emit an external
2276
+ // event to be consumed such that a child transaction is broadcast with a
2277
+ // high enough feerate for the parent commitment transaction to confirm.
2278
+ if self . onchain_tx_handler . opt_anchors ( ) {
2279
+ let funding_output = HolderFundingOutput :: build (
2280
+ self . funding_redeemscript . clone ( ) , self . channel_value_satoshis ,
2281
+ self . onchain_tx_handler . opt_anchors ( ) ,
2282
+ ) ;
2283
+ let best_block_height = self . best_block . height ( ) ;
2284
+ let commitment_package = PackageTemplate :: build_package (
2285
+ self . funding_info . 0 . txid . clone ( ) , self . funding_info . 0 . index as u32 ,
2286
+ PackageSolvingData :: HolderFundingOutput ( funding_output) ,
2287
+ best_block_height, false , best_block_height,
2288
+ ) ;
2289
+ self . onchain_tx_handler . update_claims_view (
2290
+ & [ ] , vec ! [ commitment_package] , best_block_height, best_block_height,
2291
+ broadcaster, & bounded_fee_estimator, logger,
2292
+ ) ;
2293
+ }
2258
2294
} else if !self . holder_tx_signed {
2259
2295
log_error ! ( logger, "WARNING: You have a potentially-unsafe holder commitment transaction available to broadcast" ) ;
2260
2296
log_error ! ( logger, " in channel monitor for channel {}!" , log_bytes!( self . funding_info. 0 . to_channel_id( ) ) ) ;
@@ -2309,6 +2345,34 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2309
2345
pub fn get_and_clear_pending_events ( & mut self ) -> Vec < Event > {
2310
2346
let mut ret = Vec :: new ( ) ;
2311
2347
mem:: swap ( & mut ret, & mut self . pending_events ) ;
2348
+ #[ cfg( anchors) ]
2349
+ for claim_event in self . onchain_tx_handler . get_and_clear_pending_claim_events ( ) . drain ( ..) {
2350
+ match claim_event {
2351
+ ClaimEvent :: BumpCommitment {
2352
+ package_target_feerate_sat_per_1000_weight, commitment_tx, anchor_output_idx,
2353
+ } => {
2354
+ let commitment_txid = commitment_tx. txid ( ) ;
2355
+ debug_assert_eq ! ( self . current_holder_commitment_tx. txid, commitment_txid) ;
2356
+ let pending_htlcs = self . current_holder_commitment_tx . non_dust_htlcs ( ) ;
2357
+ let commitment_tx_fee_satoshis = self . channel_value_satoshis -
2358
+ commitment_tx. output . iter ( ) . fold ( 0u64 , |sum, output| sum + output. value ) ;
2359
+ ret. push ( Event :: BumpTransaction ( BumpTransactionEvent :: ChannelClose {
2360
+ package_target_feerate_sat_per_1000_weight,
2361
+ commitment_tx,
2362
+ commitment_tx_fee_satoshis,
2363
+ anchor_descriptor : AnchorDescriptor {
2364
+ channel_keys_id : self . channel_keys_id ,
2365
+ channel_value_satoshis : self . channel_value_satoshis ,
2366
+ outpoint : BitcoinOutPoint {
2367
+ txid : commitment_txid,
2368
+ vout : anchor_output_idx,
2369
+ } ,
2370
+ } ,
2371
+ pending_htlcs,
2372
+ } ) ) ;
2373
+ } ,
2374
+ }
2375
+ }
2312
2376
ret
2313
2377
}
2314
2378
@@ -2890,15 +2954,20 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2890
2954
self . pending_monitor_events . push ( MonitorEvent :: CommitmentTxConfirmed ( self . funding_info . 0 ) ) ;
2891
2955
let commitment_tx = self . onchain_tx_handler . get_fully_signed_holder_tx ( & self . funding_redeemscript ) ;
2892
2956
self . holder_tx_signed = true ;
2893
- // Because we're broadcasting a commitment transaction, we should construct the package
2894
- // assuming it gets confirmed in the next block. Sadly, we have code which considers
2895
- // "not yet confirmed" things as discardable, so we cannot do that here.
2896
- let ( mut new_outpoints, _) = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx , self . best_block . height ( ) ) ;
2897
- let new_outputs = self . get_broadcasted_holder_watch_outputs ( & self . current_holder_commitment_tx , & commitment_tx) ;
2898
- if !new_outputs. is_empty ( ) {
2899
- watch_outputs. push ( ( self . current_holder_commitment_tx . txid . clone ( ) , new_outputs) ) ;
2957
+ // We can't broadcast our HTLC transactions while the commitment transaction is
2958
+ // unconfirmed. We'll delay doing so until we detect the confirmed commitment in
2959
+ // `transactions_confirmed`.
2960
+ if !self . onchain_tx_handler . opt_anchors ( ) {
2961
+ // Because we're broadcasting a commitment transaction, we should construct the package
2962
+ // assuming it gets confirmed in the next block. Sadly, we have code which considers
2963
+ // "not yet confirmed" things as discardable, so we cannot do that here.
2964
+ let ( mut new_outpoints, _) = self . get_broadcasted_holder_claims ( & self . current_holder_commitment_tx , self . best_block . height ( ) ) ;
2965
+ let new_outputs = self . get_broadcasted_holder_watch_outputs ( & self . current_holder_commitment_tx , & commitment_tx) ;
2966
+ if !new_outputs. is_empty ( ) {
2967
+ watch_outputs. push ( ( self . current_holder_commitment_tx . txid . clone ( ) , new_outputs) ) ;
2968
+ }
2969
+ claimable_outpoints. append ( & mut new_outpoints) ;
2900
2970
}
2901
- claimable_outpoints. append ( & mut new_outpoints) ;
2902
2971
}
2903
2972
2904
2973
// Find which on-chain events have reached their confirmation threshold.
0 commit comments