@@ -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
}
@@ -323,6 +332,7 @@ pub struct ChannelManager {
323
332
total_consistency_lock : RwLock < ( ) > ,
324
333
325
334
keys_manager : Arc < KeysInterface > ,
335
+ channels_awaiting_claiming : HashMap < [ u8 ; 32 ] , Vec < bitcoin:: Transaction > > ,
326
336
327
337
logger : Arc < Logger > ,
328
338
}
@@ -392,7 +402,7 @@ macro_rules! handle_error {
392
402
Ok ( msg) => Ok ( msg) ,
393
403
Err ( MsgHandleErrInternal { err, shutdown_finish } ) => {
394
404
if let Some ( ( shutdown_res, update_option) ) = shutdown_finish {
395
- $self. finish_force_close_channel( shutdown_res) ;
405
+ $self. finish_force_close_channel( shutdown_res, None ) ;
396
406
if let Some ( update) = update_option {
397
407
let mut channel_state = $self. channel_state. lock( ) . unwrap( ) ;
398
408
channel_state. pending_msg_events. push( events:: MessageSendEvent :: BroadcastChannelUpdate {
@@ -420,6 +430,13 @@ macro_rules! break_chan_entry {
420
430
}
421
431
break Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
422
432
} ,
433
+ Err ( ChannelError :: CloseNoPublish ( msg) ) => {
434
+ let ( channel_id, mut chan) = $entry. remove_entry( ) ;
435
+ if let Some ( short_id) = chan. get_short_channel_id( ) {
436
+ $channel_state. short_to_id. remove( & short_id) ;
437
+ }
438
+ break Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
439
+ } ,
423
440
}
424
441
}
425
442
}
@@ -438,6 +455,13 @@ macro_rules! try_chan_entry {
438
455
}
439
456
return Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
440
457
} ,
458
+ Err ( ChannelError :: CloseNoPublish ( msg) ) => {
459
+ let ( channel_id, mut chan) = $entry. remove_entry( ) ;
460
+ if let Some ( short_id) = chan. get_short_channel_id( ) {
461
+ $channel_state. short_to_id. remove( & short_id) ;
462
+ }
463
+ return Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
464
+ } ,
441
465
}
442
466
}
443
467
}
@@ -478,7 +502,7 @@ impl ChannelManager {
478
502
479
503
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
480
504
total_consistency_lock : RwLock :: new ( ( ) ) ,
481
-
505
+ channels_awaiting_claiming : HashMap :: new ( ) ,
482
506
keys_manager,
483
507
484
508
logger,
@@ -615,14 +639,18 @@ impl ChannelManager {
615
639
}
616
640
617
641
#[ inline]
618
- fn finish_force_close_channel ( & self , shutdown_res : ShutdownResult ) {
642
+ fn finish_force_close_channel ( & mut self , shutdown_res : ShutdownResult , dont_claim_from_chain : Option < [ u8 ; 32 ] > ) {
619
643
let ( local_txn, mut failed_htlcs) = shutdown_res;
620
644
for htlc_source in failed_htlcs. drain ( ..) {
621
645
// unknown_next_peer...I dunno who that is anymore....
622
646
self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source. 0 , & htlc_source. 1 , HTLCFailReason :: Reason { failure_code : 0x4000 | 10 , data : Vec :: new ( ) } ) ;
623
647
}
624
- for tx in local_txn {
625
- self . tx_broadcaster . broadcast_transaction ( & tx) ;
648
+ if dont_claim_from_chain == None {
649
+ for tx in local_txn {
650
+ self . tx_broadcaster . broadcast_transaction ( & tx) ;
651
+ }
652
+ } else {
653
+ self . channels_awaiting_claiming . insert ( dont_claim_from_chain. unwrap ( ) , local_txn) ;
626
654
}
627
655
//TODO: We need to have a way where outbound HTLC claims can result in us claiming the
628
656
//now-on-chain HTLC output for ourselves (and, thereafter, passing the HTLC backwards).
@@ -635,7 +663,7 @@ impl ChannelManager {
635
663
636
664
/// Force closes a channel, immediately broadcasting the latest local commitment transaction to
637
665
/// the chain and rejecting new HTLCs on the given channel.
638
- pub fn force_close_channel ( & self , channel_id : & [ u8 ; 32 ] ) {
666
+ pub fn force_close_channel ( & mut self , channel_id : & [ u8 ; 32 ] , store_closed : bool ) {
639
667
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
640
668
641
669
let mut chan = {
@@ -650,7 +678,12 @@ impl ChannelManager {
650
678
return ;
651
679
}
652
680
} ;
653
- self . finish_force_close_channel ( chan. force_shutdown ( ) ) ;
681
+ if store_closed{
682
+ self . finish_force_close_channel ( chan. force_shutdown ( ) , Some ( chan. get_channel_id ( ) ) ) ;
683
+ } else
684
+ {
685
+ self . finish_force_close_channel ( chan. force_shutdown ( ) , None ) ;
686
+ }
654
687
if let Ok ( update) = self . get_channel_update ( & chan) {
655
688
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
656
689
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
@@ -661,13 +694,13 @@ impl ChannelManager {
661
694
662
695
/// Force close all channels, immediately broadcasting the latest local commitment transaction
663
696
/// for each to the chain and rejecting new HTLCs on each.
664
- pub fn force_close_all_channels ( & self ) {
697
+ pub fn force_close_all_channels ( & mut self ) {
665
698
for chan in self . list_channels ( ) {
666
- self . force_close_channel ( & chan. channel_id ) ;
699
+ self . force_close_channel ( & chan. channel_id , false ) ;
667
700
}
668
701
}
669
702
670
- fn handle_monitor_update_fail ( & self , mut channel_state_lock : MutexGuard < ChannelHolder > , channel_id : & [ u8 ; 32 ] , err : ChannelMonitorUpdateErr , reason : RAACommitmentOrder ) {
703
+ fn handle_monitor_update_fail ( & mut self , mut channel_state_lock : MutexGuard < ChannelHolder > , channel_id : & [ u8 ; 32 ] , err : ChannelMonitorUpdateErr , reason : RAACommitmentOrder ) {
671
704
match err {
672
705
ChannelMonitorUpdateErr :: PermanentFailure => {
673
706
let mut chan = {
@@ -679,7 +712,7 @@ impl ChannelManager {
679
712
chan
680
713
} ;
681
714
mem:: drop ( channel_state_lock) ;
682
- self . finish_force_close_channel ( chan. force_shutdown ( ) ) ;
715
+ self . finish_force_close_channel ( chan. force_shutdown ( ) , None ) ;
683
716
if let Ok ( update) = self . get_channel_update ( & chan) {
684
717
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
685
718
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
@@ -1194,7 +1227,7 @@ impl ChannelManager {
1194
1227
///
1195
1228
/// Raises APIError::RoutError when invalid route or forward parameter
1196
1229
/// (cltv_delta, fee, node public key) is specified
1197
- pub fn send_payment ( & self , route : Route , payment_hash : [ u8 ; 32 ] ) -> Result < ( ) , APIError > {
1230
+ pub fn send_payment ( & mut self , route : Route , payment_hash : [ u8 ; 32 ] ) -> Result < ( ) , APIError > {
1198
1231
if route. hops . len ( ) < 1 || route. hops . len ( ) > 20 {
1199
1232
return Err ( APIError :: RouteError { err : "Route didn't go anywhere/had bogus size" } ) ;
1200
1233
}
@@ -1737,7 +1770,7 @@ impl ChannelManager {
1737
1770
self . forward_htlcs ( & mut htlc_forwards[ ..] ) ;
1738
1771
1739
1772
for res in close_results. drain ( ..) {
1740
- self . finish_force_close_channel ( res) ;
1773
+ self . finish_force_close_channel ( res, None ) ;
1741
1774
}
1742
1775
}
1743
1776
@@ -2659,7 +2692,7 @@ impl ChainListener for ChannelManager {
2659
2692
} ) ;
2660
2693
}
2661
2694
for failure in failed_channels. drain ( ..) {
2662
- self . finish_force_close_channel ( failure) ;
2695
+ self . finish_force_close_channel ( failure, None ) ;
2663
2696
}
2664
2697
self . latest_block_height . store ( height as usize , Ordering :: Release ) ;
2665
2698
* self . last_block_hash . try_lock ( ) . expect ( "block_(dis)connected must not be called in parallel" ) = header. bitcoin_hash ( ) ;
@@ -2692,7 +2725,7 @@ impl ChainListener for ChannelManager {
2692
2725
} ) ;
2693
2726
}
2694
2727
for failure in failed_channels. drain ( ..) {
2695
- self . finish_force_close_channel ( failure) ;
2728
+ self . finish_force_close_channel ( failure, None ) ;
2696
2729
}
2697
2730
self . latest_block_height . fetch_sub ( 1 , Ordering :: AcqRel ) ;
2698
2731
* self . last_block_hash . try_lock ( ) . expect ( "block_(dis)connected must not be called in parallel" ) = header. bitcoin_hash ( ) ;
@@ -2781,7 +2814,7 @@ impl ChannelMessageHandler for ChannelManager {
2781
2814
handle_error ! ( self , self . internal_channel_reestablish( their_node_id, msg) , their_node_id)
2782
2815
}
2783
2816
2784
- fn peer_disconnected ( & self , their_node_id : & PublicKey , no_connection_possible : bool ) {
2817
+ fn peer_disconnected ( & mut self , their_node_id : & PublicKey , no_connection_possible : bool ) {
2785
2818
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2786
2819
let mut failed_channels = Vec :: new ( ) ;
2787
2820
let mut failed_payments = Vec :: new ( ) ;
@@ -2830,7 +2863,7 @@ impl ChannelMessageHandler for ChannelManager {
2830
2863
}
2831
2864
}
2832
2865
for failure in failed_channels. drain ( ..) {
2833
- self . finish_force_close_channel ( failure) ;
2866
+ self . finish_force_close_channel ( failure, None ) ;
2834
2867
}
2835
2868
for ( chan_update, mut htlc_sources) in failed_payments {
2836
2869
for ( htlc_source, payment_hash) in htlc_sources. drain ( ..) {
@@ -2866,17 +2899,17 @@ impl ChannelMessageHandler for ChannelManager {
2866
2899
//TODO: Also re-broadcast announcement_signatures
2867
2900
}
2868
2901
2869
- fn handle_error ( & self , their_node_id : & PublicKey , msg : & msgs:: ErrorMessage ) {
2902
+ fn handle_error ( & mut self , their_node_id : & PublicKey , msg : & msgs:: ErrorMessage ) {
2870
2903
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2871
2904
2872
2905
if msg. channel_id == [ 0 ; 32 ] {
2873
2906
for chan in self . list_channels ( ) {
2874
2907
if chan. remote_network_id == * their_node_id {
2875
- self . force_close_channel ( & chan. channel_id ) ;
2908
+ self . force_close_channel ( & chan. channel_id , false ) ;
2876
2909
}
2877
2910
}
2878
2911
} else {
2879
- self . force_close_channel ( & msg. channel_id ) ;
2912
+ self . force_close_channel ( & msg. channel_id , false ) ;
2880
2913
}
2881
2914
}
2882
2915
}
@@ -3225,7 +3258,7 @@ impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (S
3225
3258
claimable_htlcs. insert ( payment_hash, previous_hops) ;
3226
3259
}
3227
3260
3228
- let channel_manager = ChannelManager {
3261
+ let mut channel_manager = ChannelManager {
3229
3262
genesis_hash,
3230
3263
fee_estimator : args. fee_estimator ,
3231
3264
monitor : args. monitor ,
@@ -3250,11 +3283,12 @@ impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (S
3250
3283
total_consistency_lock : RwLock :: new ( ( ) ) ,
3251
3284
keys_manager : args. keys_manager ,
3252
3285
logger : args. logger ,
3286
+ channels_awaiting_claiming : HashMap :: new ( ) ,
3253
3287
default_configuration : args. default_config ,
3254
3288
} ;
3255
3289
3256
3290
for close_res in closed_channels. drain ( ..) {
3257
- channel_manager. finish_force_close_channel ( close_res) ;
3291
+ channel_manager. finish_force_close_channel ( close_res, None ) ;
3258
3292
//TODO: Broadcast channel update for closed channels, but only after we've made a
3259
3293
//connection or two.
3260
3294
}
0 commit comments