@@ -196,6 +196,15 @@ impl MsgHandleErrInternal {
196
196
} ,
197
197
} ) ,
198
198
} ,
199
+ ChannelError :: CloseNoPublish ( msg) => HandleError {
200
+ err : msg,
201
+ action : Some ( msgs:: ErrorAction :: SendErrorMessage {
202
+ msg : msgs:: ErrorMessage {
203
+ channel_id,
204
+ data : msg. to_string ( )
205
+ } ,
206
+ } ) ,
207
+ } ,
199
208
} ,
200
209
shutdown_finish : None ,
201
210
}
@@ -326,6 +335,7 @@ pub struct ChannelManager {
326
335
total_consistency_lock : RwLock < ( ) > ,
327
336
328
337
keys_manager : Arc < KeysInterface > ,
338
+ channels_awaiting_claiming : Mutex < HashMap < [ u8 ; 32 ] , Vec < bitcoin:: Transaction > > > ,
329
339
330
340
logger : Arc < Logger > ,
331
341
}
@@ -386,7 +396,7 @@ macro_rules! handle_error {
386
396
Ok ( msg) => Ok ( msg) ,
387
397
Err ( MsgHandleErrInternal { err, shutdown_finish } ) => {
388
398
if let Some ( ( shutdown_res, update_option) ) = shutdown_finish {
389
- $self. finish_force_close_channel( shutdown_res) ;
399
+ $self. finish_force_close_channel( shutdown_res, None ) ;
390
400
if let Some ( update) = update_option {
391
401
let mut channel_state = $self. channel_state. lock( ) . unwrap( ) ;
392
402
channel_state. pending_msg_events. push( events:: MessageSendEvent :: BroadcastChannelUpdate {
@@ -415,6 +425,14 @@ macro_rules! break_chan_entry {
415
425
}
416
426
break Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
417
427
} ,
428
+ Err ( ChannelError :: CloseNoPublish ( msg) ) => {
429
+ log_trace!( $self, "Closing channel {} due to Close-No_publish_required error: {}" , log_bytes!( $entry. key( ) [ ..] ) , msg) ;
430
+ let ( channel_id, mut chan) = $entry. remove_entry( ) ;
431
+ if let Some ( short_id) = chan. get_short_channel_id( ) {
432
+ $channel_state. short_to_id. remove( & short_id) ;
433
+ }
434
+ break Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
435
+ } ,
418
436
}
419
437
}
420
438
}
@@ -434,6 +452,13 @@ macro_rules! try_chan_entry {
434
452
}
435
453
return Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
436
454
} ,
455
+ Err ( ChannelError :: CloseNoPublish ( msg) ) => {
456
+ let ( channel_id, mut chan) = $entry. remove_entry( ) ;
457
+ if let Some ( short_id) = chan. get_short_channel_id( ) {
458
+ $channel_state. short_to_id. remove( & short_id) ;
459
+ }
460
+ return Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
461
+ } ,
437
462
}
438
463
}
439
464
}
@@ -529,7 +554,7 @@ impl ChannelManager {
529
554
530
555
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
531
556
total_consistency_lock : RwLock :: new ( ( ) ) ,
532
-
557
+ channels_awaiting_claiming : Mutex :: new ( HashMap :: new ( ) ) ,
533
558
keys_manager,
534
559
535
560
logger,
@@ -551,12 +576,12 @@ impl ChannelManager {
551
576
///
552
577
/// Raises APIError::APIMisuseError when channel_value_satoshis > 2**24 or push_msat is
553
578
/// greater than channel_value_satoshis * 1k or channel_value_satoshis is < 1000.
554
- pub fn create_channel ( & self , their_network_key : PublicKey , channel_value_satoshis : u64 , push_msat : u64 , user_id : u64 ) -> Result < ( ) , APIError > {
579
+ pub fn create_channel ( & self , their_network_key : PublicKey , channel_value_satoshis : u64 , push_msat : u64 , user_id : u64 , local_feature_flags : & Option < msgs :: LocalFeatures > ) -> Result < ( ) , APIError > {
555
580
if channel_value_satoshis < 1000 {
556
581
return Err ( APIError :: APIMisuseError { err : "channel_value must be at least 1000 satoshis" } ) ;
557
582
}
558
583
559
- let channel = Channel :: new_outbound ( & * self . fee_estimator , & self . keys_manager , their_network_key, channel_value_satoshis, push_msat, user_id, Arc :: clone ( & self . logger ) , & self . default_configuration ) ?;
584
+ let channel = Channel :: new_outbound ( & * self . fee_estimator , & self . keys_manager , their_network_key, channel_value_satoshis, push_msat, user_id, Arc :: clone ( & self . logger ) , & self . default_configuration , local_feature_flags ) ?;
560
585
let res = channel. get_open_channel ( self . genesis_hash . clone ( ) , & * self . fee_estimator ) ;
561
586
562
587
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
@@ -665,20 +690,25 @@ impl ChannelManager {
665
690
}
666
691
667
692
#[ inline]
668
- fn finish_force_close_channel ( & self , shutdown_res : ShutdownResult ) {
693
+ fn finish_force_close_channel ( & self , shutdown_res : ShutdownResult , dont_claim_from_chain : Option < [ u8 ; 32 ] > ) {
669
694
let ( local_txn, mut failed_htlcs) = shutdown_res;
670
695
log_trace ! ( self , "Finishing force-closure of channel with {} transactions to broadcast and {} HTLCs to fail" , local_txn. len( ) , failed_htlcs. len( ) ) ;
671
696
for htlc_source in failed_htlcs. drain ( ..) {
672
697
self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source. 0 , & htlc_source. 1 , HTLCFailReason :: Reason { failure_code : 0x4000 | 8 , data : Vec :: new ( ) } ) ;
673
698
}
674
- for tx in local_txn {
675
- self . tx_broadcaster . broadcast_transaction ( & tx) ;
699
+ if dont_claim_from_chain == None {
700
+ for tx in local_txn {
701
+ self . tx_broadcaster . broadcast_transaction ( & tx) ;
702
+ }
703
+ } else {
704
+ let mut hashmap = self . channels_awaiting_claiming . lock ( ) . unwrap ( ) ;
705
+ hashmap. insert ( dont_claim_from_chain. unwrap ( ) , local_txn) ;
676
706
}
677
707
}
678
708
679
709
/// Force closes a channel, immediately broadcasting the latest local commitment transaction to
680
710
/// the chain and rejecting new HTLCs on the given channel.
681
- pub fn force_close_channel ( & self , channel_id : & [ u8 ; 32 ] ) {
711
+ pub fn force_close_channel ( & self , channel_id : & [ u8 ; 32 ] , store_closed : bool ) {
682
712
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
683
713
684
714
let mut chan = {
@@ -694,7 +724,12 @@ impl ChannelManager {
694
724
}
695
725
} ;
696
726
log_trace ! ( self , "Force-closing channel {}" , log_bytes!( channel_id[ ..] ) ) ;
697
- self . finish_force_close_channel ( chan. force_shutdown ( ) ) ;
727
+ if store_closed{
728
+ self . finish_force_close_channel ( chan. force_shutdown ( ) , Some ( chan. get_channel_id ( ) ) ) ;
729
+ } else
730
+ {
731
+ self . finish_force_close_channel ( chan. force_shutdown ( ) , None ) ;
732
+ }
698
733
if let Ok ( update) = self . get_channel_update ( & chan) {
699
734
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
700
735
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
@@ -705,9 +740,9 @@ impl ChannelManager {
705
740
706
741
/// Force close all channels, immediately broadcasting the latest local commitment transaction
707
742
/// for each to the chain and rejecting new HTLCs on each.
708
- pub fn force_close_all_channels ( & self ) {
743
+ pub fn force_close_all_channels ( & mut self ) {
709
744
for chan in self . list_channels ( ) {
710
- self . force_close_channel ( & chan. channel_id ) ;
745
+ self . force_close_channel ( & chan. channel_id , false ) ;
711
746
}
712
747
}
713
748
@@ -1605,16 +1640,16 @@ impl ChannelManager {
1605
1640
self . forward_htlcs ( & mut htlc_forwards[ ..] ) ;
1606
1641
1607
1642
for res in close_results. drain ( ..) {
1608
- self . finish_force_close_channel ( res) ;
1643
+ self . finish_force_close_channel ( res, None ) ;
1609
1644
}
1610
1645
}
1611
1646
1612
- fn internal_open_channel ( & self , their_node_id : & PublicKey , msg : & msgs:: OpenChannel ) -> Result < ( ) , MsgHandleErrInternal > {
1647
+ fn internal_open_channel ( & self , their_node_id : & PublicKey , msg : & msgs:: OpenChannel , local_feature_flags : & Option < msgs :: LocalFeatures > ) -> Result < ( ) , MsgHandleErrInternal > {
1613
1648
if msg. chain_hash != self . genesis_hash {
1614
1649
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Unknown genesis block hash" , msg. temporary_channel_id . clone ( ) ) ) ;
1615
1650
}
1616
1651
1617
- let channel = Channel :: new_from_req ( & * self . fee_estimator , & self . keys_manager , their_node_id. clone ( ) , msg, 0 , Arc :: clone ( & self . logger ) , & self . default_configuration )
1652
+ let channel = Channel :: new_from_req ( & * self . fee_estimator , & self . keys_manager , their_node_id. clone ( ) , msg, 0 , Arc :: clone ( & self . logger ) , & self . default_configuration , local_feature_flags )
1618
1653
. map_err ( |e| MsgHandleErrInternal :: from_chan_no_close ( e, msg. temporary_channel_id ) ) ?;
1619
1654
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1620
1655
let channel_state = channel_state_lock. borrow_parts ( ) ;
@@ -2385,7 +2420,7 @@ impl ChainListener for ChannelManager {
2385
2420
} ) ;
2386
2421
}
2387
2422
for failure in failed_channels. drain ( ..) {
2388
- self . finish_force_close_channel ( failure) ;
2423
+ self . finish_force_close_channel ( failure, None ) ;
2389
2424
}
2390
2425
self . latest_block_height . store ( height as usize , Ordering :: Release ) ;
2391
2426
* self . last_block_hash . try_lock ( ) . expect ( "block_(dis)connected must not be called in parallel" ) = header_hash;
@@ -2418,7 +2453,7 @@ impl ChainListener for ChannelManager {
2418
2453
} ) ;
2419
2454
}
2420
2455
for failure in failed_channels. drain ( ..) {
2421
- self . finish_force_close_channel ( failure) ;
2456
+ self . finish_force_close_channel ( failure, None ) ;
2422
2457
}
2423
2458
self . latest_block_height . fetch_sub ( 1 , Ordering :: AcqRel ) ;
2424
2459
* self . last_block_hash . try_lock ( ) . expect ( "block_(dis)connected must not be called in parallel" ) = header. bitcoin_hash ( ) ;
@@ -2427,9 +2462,9 @@ impl ChainListener for ChannelManager {
2427
2462
2428
2463
impl ChannelMessageHandler for ChannelManager {
2429
2464
//TODO: Handle errors and close channel (or so)
2430
- fn handle_open_channel ( & self , their_node_id : & PublicKey , msg : & msgs:: OpenChannel ) -> Result < ( ) , HandleError > {
2465
+ fn handle_open_channel ( & self , their_node_id : & PublicKey , msg : & msgs:: OpenChannel , local_feature_flags : & Option < msgs :: LocalFeatures > ) -> Result < ( ) , HandleError > {
2431
2466
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2432
- handle_error ! ( self , self . internal_open_channel( their_node_id, msg) , their_node_id)
2467
+ handle_error ! ( self , self . internal_open_channel( their_node_id, msg, local_feature_flags ) , their_node_id)
2433
2468
}
2434
2469
2435
2470
fn handle_accept_channel ( & self , their_node_id : & PublicKey , msg : & msgs:: AcceptChannel ) -> Result < ( ) , HandleError > {
@@ -2556,7 +2591,7 @@ impl ChannelMessageHandler for ChannelManager {
2556
2591
}
2557
2592
}
2558
2593
for failure in failed_channels. drain ( ..) {
2559
- self . finish_force_close_channel ( failure) ;
2594
+ self . finish_force_close_channel ( failure, None ) ;
2560
2595
}
2561
2596
for ( chan_update, mut htlc_sources) in failed_payments {
2562
2597
for ( htlc_source, payment_hash) in htlc_sources. drain ( ..) {
@@ -2598,11 +2633,11 @@ impl ChannelMessageHandler for ChannelManager {
2598
2633
if msg. channel_id == [ 0 ; 32 ] {
2599
2634
for chan in self . list_channels ( ) {
2600
2635
if chan. remote_network_id == * their_node_id {
2601
- self . force_close_channel ( & chan. channel_id ) ;
2636
+ self . force_close_channel ( & chan. channel_id , false ) ;
2602
2637
}
2603
2638
}
2604
2639
} else {
2605
- self . force_close_channel ( & msg. channel_id ) ;
2640
+ self . force_close_channel ( & msg. channel_id , false ) ;
2606
2641
}
2607
2642
}
2608
2643
}
@@ -2981,7 +3016,7 @@ impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (S
2981
3016
claimable_htlcs. insert ( payment_hash, previous_hops) ;
2982
3017
}
2983
3018
2984
- let channel_manager = ChannelManager {
3019
+ let mut channel_manager = ChannelManager {
2985
3020
genesis_hash,
2986
3021
fee_estimator : args. fee_estimator ,
2987
3022
monitor : args. monitor ,
@@ -3006,11 +3041,12 @@ impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (S
3006
3041
total_consistency_lock : RwLock :: new ( ( ) ) ,
3007
3042
keys_manager : args. keys_manager ,
3008
3043
logger : args. logger ,
3044
+ channels_awaiting_claiming : Mutex :: new ( HashMap :: new ( ) ) ,
3009
3045
default_configuration : args. default_config ,
3010
3046
} ;
3011
3047
3012
3048
for close_res in closed_channels. drain ( ..) {
3013
- channel_manager. finish_force_close_channel ( close_res) ;
3049
+ channel_manager. finish_force_close_channel ( close_res, None ) ;
3014
3050
//TODO: Broadcast channel update for closed channels, but only after we've made a
3015
3051
//connection or two.
3016
3052
}
0 commit comments