@@ -331,6 +331,8 @@ pub struct ChannelManager {
331
331
channel_state : Mutex < ChannelHolder > ,
332
332
our_network_key : SecretKey ,
333
333
334
+ channel_closing_waiting_threshold_conf : Mutex < HashMap < u32 , Vec < [ u8 ; 32 ] > > > ,
335
+
334
336
pending_events : Mutex < Vec < events:: Event > > ,
335
337
/// Used when we have to take a BIG lock to make sure everything is self-consistent.
336
338
/// Essentially just when we're serializing ourselves out.
@@ -555,6 +557,8 @@ impl ChannelManager {
555
557
} ) ,
556
558
our_network_key : keys_manager. get_node_secret ( ) ,
557
559
560
+ channel_closing_waiting_threshold_conf : Mutex :: new ( HashMap :: new ( ) ) ,
561
+
558
562
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
559
563
total_consistency_lock : RwLock :: new ( ( ) ) ,
560
564
@@ -2399,11 +2403,12 @@ impl ChainListener for ChannelManager {
2399
2403
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2400
2404
let mut failed_channels = Vec :: new ( ) ;
2401
2405
{
2406
+ let mut channel_closing_lock = self . channel_closing_waiting_threshold_conf . lock ( ) . unwrap ( ) ;
2402
2407
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2403
2408
let channel_state = channel_lock. borrow_parts ( ) ;
2404
2409
let short_to_id = channel_state. short_to_id ;
2405
2410
let pending_msg_events = channel_state. pending_msg_events ;
2406
- channel_state. by_id . retain ( |_ , channel| {
2411
+ channel_state. by_id . retain ( |channel_id , channel| {
2407
2412
let chan_res = channel. block_connected ( header, height, txn_matched, indexes_of_txn_matched) ;
2408
2413
if let Ok ( Some ( funding_locked) ) = chan_res {
2409
2414
pending_msg_events. push ( events:: MessageSendEvent :: SendFundingLocked {
@@ -2428,20 +2433,24 @@ impl ChainListener for ChannelManager {
2428
2433
for tx in txn_matched {
2429
2434
for inp in tx. input . iter ( ) {
2430
2435
if inp. previous_output == funding_txo. into_bitcoin_outpoint ( ) {
2431
- log_trace ! ( self , "Detected channel-closing tx {} spending {}:{}, closing channel {}" , tx. txid( ) , inp. previous_output. txid, inp. previous_output. vout, log_bytes!( channel. channel_id( ) ) ) ;
2432
- if let Some ( short_id) = channel. get_short_channel_id ( ) {
2433
- short_to_id. remove ( & short_id) ;
2434
- }
2435
- // It looks like our counterparty went on-chain. We go ahead and
2436
- // broadcast our latest local state as well here, just in case its
2437
- // some kind of SPV attack, though we expect these to be dropped.
2438
- failed_channels. push ( channel. force_shutdown ( ) ) ;
2439
- if let Ok ( update) = self . get_channel_update ( & channel) {
2440
- pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2441
- msg : update
2442
- } ) ;
2436
+ log_trace ! ( self , "Detected channel-closing tx {} spending {}:{}, waiting until {} to close channel {}" , tx. txid( ) , inp. previous_output. txid, inp. previous_output. vout, height + HTLC_FAIL_ANTI_REORG_DELAY - 1 , log_bytes!( channel_id[ ..] ) ) ;
2437
+ match channel_closing_lock. entry ( height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) {
2438
+ hash_map:: Entry :: Occupied ( mut entry) => {
2439
+ let mut duplicate = false ;
2440
+ for id in entry. get ( ) . iter ( ) {
2441
+ if * id == * channel_id {
2442
+ duplicate = true ;
2443
+ break ;
2444
+ }
2445
+ }
2446
+ if !duplicate {
2447
+ entry. get_mut ( ) . push ( * channel_id) ;
2448
+ }
2449
+ }
2450
+ hash_map:: Entry :: Vacant ( entry) => {
2451
+ entry. insert ( vec ! [ * channel_id] ) ;
2452
+ }
2443
2453
}
2444
- return false ;
2445
2454
}
2446
2455
}
2447
2456
}
@@ -2464,6 +2473,25 @@ impl ChainListener for ChannelManager {
2464
2473
}
2465
2474
true
2466
2475
} ) ;
2476
+ if let Some ( channel_closings) = channel_closing_lock. remove ( & height) {
2477
+ for channel_id in channel_closings {
2478
+ log_trace ! ( self , "Enough confirmations for a broacast commitment tx, channel {} can be closed" , log_bytes!( & channel_id[ ..] ) ) ;
2479
+ if let Some ( mut channel) = channel_state. by_id . remove ( & channel_id) {
2480
+ if let Some ( short_id) = channel. get_short_channel_id ( ) {
2481
+ short_to_id. remove ( & short_id) ;
2482
+ }
2483
+ // It looks like our counterparty went on-chain. We go ahead and
2484
+ // broadcast our latest local state as well here, just in case its
2485
+ // some kind of SPV attack, though we expect these to be dropped.
2486
+ failed_channels. push ( channel. force_shutdown ( ) ) ;
2487
+ if let Ok ( update) = self . get_channel_update ( & channel) {
2488
+ pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2489
+ msg : update
2490
+ } ) ;
2491
+ }
2492
+ }
2493
+ }
2494
+ }
2467
2495
}
2468
2496
for failure in failed_channels. drain ( ..) {
2469
2497
self . finish_force_close_channel ( failure) ;
@@ -2473,7 +2501,7 @@ impl ChainListener for ChannelManager {
2473
2501
}
2474
2502
2475
2503
/// We force-close the channel without letting our counterparty participate in the shutdown
2476
- fn block_disconnected ( & self , header : & BlockHeader , _ : u32 ) {
2504
+ fn block_disconnected ( & self , header : & BlockHeader , height : u32 ) {
2477
2505
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2478
2506
let mut failed_channels = Vec :: new ( ) ;
2479
2507
{
@@ -2498,6 +2526,12 @@ impl ChainListener for ChannelManager {
2498
2526
}
2499
2527
} ) ;
2500
2528
}
2529
+ {
2530
+ let mut channel_closing_lock = self . channel_closing_waiting_threshold_conf . lock ( ) . unwrap ( ) ;
2531
+ if let Some ( _) = channel_closing_lock. remove ( & ( height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) ) {
2532
+ // We discard channel_closing there as brooadcast commitment tx has been disconnected, (and may be replaced by a legit closing_signed)
2533
+ }
2534
+ }
2501
2535
for failure in failed_channels. drain ( ..) {
2502
2536
self . finish_force_close_channel ( failure) ;
2503
2537
}
@@ -2935,6 +2969,15 @@ impl Writeable for ChannelManager {
2935
2969
}
2936
2970
}
2937
2971
2972
+ let channel_closing_lock = self . channel_closing_waiting_threshold_conf . lock ( ) . unwrap ( ) ;
2973
+ ( channel_closing_lock. len ( ) as u64 ) . write ( writer) ?;
2974
+ for ( confirmation_height, channel_id) in channel_closing_lock. iter ( ) {
2975
+ confirmation_height. write ( writer) ?;
2976
+ for id in channel_id {
2977
+ id. write ( writer) ?;
2978
+ }
2979
+ }
2980
+
2938
2981
Ok ( ( ) )
2939
2982
}
2940
2983
}
@@ -3072,6 +3115,21 @@ impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (S
3072
3115
claimable_htlcs. insert ( payment_hash, previous_hops) ;
3073
3116
}
3074
3117
3118
+ let channel_closing_count: u64 = Readable :: read ( reader) ?;
3119
+ let mut channel_closing: HashMap < u32 , Vec < [ u8 ; 32 ] > > = HashMap :: with_capacity ( cmp:: min ( channel_closing_count as usize , 32 ) ) ;
3120
+ for _ in 0 ..channel_closing_count {
3121
+ let confirmation_height: u32 = Readable :: read ( reader) ?;
3122
+ let channel_id: [ u8 ; 32 ] = Readable :: read ( reader) ?;
3123
+ match channel_closing. entry ( confirmation_height) {
3124
+ hash_map:: Entry :: Occupied ( mut entry) => {
3125
+ entry. get_mut ( ) . push ( channel_id) ;
3126
+ }
3127
+ hash_map:: Entry :: Vacant ( entry) => {
3128
+ entry. insert ( vec ! [ channel_id] ) ;
3129
+ }
3130
+ }
3131
+ }
3132
+
3075
3133
let channel_manager = ChannelManager {
3076
3134
genesis_hash,
3077
3135
fee_estimator : args. fee_estimator ,
@@ -3093,6 +3151,8 @@ impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (S
3093
3151
} ) ,
3094
3152
our_network_key : args. keys_manager . get_node_secret ( ) ,
3095
3153
3154
+ channel_closing_waiting_threshold_conf : Mutex :: new ( channel_closing) ,
3155
+
3096
3156
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
3097
3157
total_consistency_lock : RwLock :: new ( ( ) ) ,
3098
3158
keys_manager : args. keys_manager ,
0 commit comments