@@ -467,21 +467,41 @@ pub struct ChannelDetails {
467
467
}
468
468
469
469
macro_rules! handle_error {
470
- ( $self: ident, $internal: expr, $their_node_id: expr, $locked_channel_state : expr ) => {
470
+ ( $self: ident, $internal: expr, $their_node_id: expr) => {
471
471
match $internal {
472
472
Ok ( msg) => Ok ( msg) ,
473
473
Err ( MsgHandleErrInternal { err, shutdown_finish } ) => {
474
+ #[ cfg( debug_assertions) ]
475
+ {
476
+ // In testing, ensure there are no deadlocks where the lock is already held upon
477
+ // entering the macro.
478
+ assert!( $self. channel_state. try_lock( ) . is_ok( ) ) ;
479
+ }
480
+
481
+ let mut msg_events = Vec :: with_capacity( 2 ) ;
482
+
474
483
if let Some ( ( shutdown_res, update_option) ) = shutdown_finish {
475
484
$self. finish_force_close_channel( shutdown_res) ;
476
485
if let Some ( update) = update_option {
477
- $locked_channel_state . pending_msg_events . push( events:: MessageSendEvent :: BroadcastChannelUpdate {
486
+ msg_events . push( events:: MessageSendEvent :: BroadcastChannelUpdate {
478
487
msg: update
479
488
} ) ;
480
489
}
481
490
}
491
+
482
492
log_error!( $self, "{}" , err. err) ;
483
493
if let msgs:: ErrorAction :: IgnoreError = err. action {
484
- } else { $locked_channel_state. pending_msg_events. push( events:: MessageSendEvent :: HandleError { node_id: $their_node_id, action: err. action. clone( ) } ) ; }
494
+ } else {
495
+ msg_events. push( events:: MessageSendEvent :: HandleError {
496
+ node_id: $their_node_id,
497
+ action: err. action. clone( )
498
+ } ) ;
499
+ }
500
+
501
+ if !msg_events. is_empty( ) {
502
+ $self. channel_state. lock( ) . unwrap( ) . pending_msg_events. append( & mut msg_events) ;
503
+ }
504
+
485
505
// Return error in case higher-API need one
486
506
Err ( err)
487
507
} ,
@@ -1191,9 +1211,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1191
1211
1192
1212
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1193
1213
1194
- let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1195
1214
let err: Result < ( ) , _ > = loop {
1196
-
1215
+ let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1197
1216
let id = match channel_lock. short_to_id . get ( & route. hops . first ( ) . unwrap ( ) . short_channel_id ) {
1198
1217
None => return Err ( APIError :: ChannelUnavailable { err : "No channel available with first hop!" } ) ,
1199
1218
Some ( id) => id. clone ( ) ,
@@ -1242,7 +1261,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1242
1261
return Ok ( ( ) ) ;
1243
1262
} ;
1244
1263
1245
- match handle_error ! ( self , err, route. hops. first( ) . unwrap( ) . pubkey, channel_lock ) {
1264
+ match handle_error ! ( self , err, route. hops. first( ) . unwrap( ) . pubkey) {
1246
1265
Ok ( _) => unreachable ! ( ) ,
1247
1266
Err ( e) => { Err ( APIError :: ChannelUnavailable { err : e. err } ) }
1248
1267
}
@@ -1261,8 +1280,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1261
1280
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1262
1281
1263
1282
let ( mut chan, msg, chan_monitor) = {
1264
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1265
- let ( res, chan) = match channel_state. by_id . remove ( temporary_channel_id) {
1283
+ let ( res, chan) = match self . channel_state . lock ( ) . unwrap ( ) . by_id . remove ( temporary_channel_id) {
1266
1284
Some ( mut chan) => {
1267
1285
( chan. get_outbound_funding_created ( funding_txo)
1268
1286
. map_err ( |e| if let ChannelError :: Close ( msg) = e {
@@ -1272,7 +1290,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1272
1290
} ,
1273
1291
None => return
1274
1292
} ;
1275
- match handle_error ! ( self , res, chan. get_their_node_id( ) , channel_state ) {
1293
+ match handle_error ! ( self , res, chan. get_their_node_id( ) ) {
1276
1294
Ok ( funding_msg) => {
1277
1295
( chan, funding_msg. 0 , funding_msg. 1 )
1278
1296
} ,
@@ -1284,12 +1302,9 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1284
1302
if let Err ( e) = self . monitor . add_monitor ( chan_monitor. get_funding_txo ( ) . unwrap ( ) , chan_monitor) {
1285
1303
match e {
1286
1304
ChannelMonitorUpdateErr :: PermanentFailure => {
1287
- {
1288
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1289
- match handle_error ! ( self , Err ( MsgHandleErrInternal :: from_finish_shutdown( "ChannelMonitor storage failure" , * temporary_channel_id, chan. force_shutdown( true ) , None ) ) , chan. get_their_node_id( ) , channel_state) {
1290
- Err ( _) => { return ; } ,
1291
- Ok ( ( ) ) => unreachable ! ( ) ,
1292
- }
1305
+ match handle_error ! ( self , Err ( MsgHandleErrInternal :: from_finish_shutdown( "ChannelMonitor storage failure" , * temporary_channel_id, chan. force_shutdown( true ) , None ) ) , chan. get_their_node_id( ) ) {
1306
+ Err ( _) => { return ; } ,
1307
+ Ok ( ( ) ) => unreachable ! ( ) ,
1293
1308
}
1294
1309
} ,
1295
1310
ChannelMonitorUpdateErr :: TemporaryFailure => {
@@ -1520,10 +1535,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1520
1535
} ,
1521
1536
ChannelError :: CloseDelayBroadcast { .. } => { panic ! ( "Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here" ) ; }
1522
1537
} ;
1523
- match handle_error ! ( self , err, their_node_id, channel_state) {
1524
- Ok ( _) => unreachable ! ( ) ,
1525
- Err ( _) => { continue ; } ,
1526
- }
1538
+ handle_errors. push ( ( their_node_id, err) ) ;
1539
+ continue ;
1527
1540
}
1528
1541
} ;
1529
1542
if let Err ( e) = self . monitor . update_monitor ( chan. get ( ) . get_funding_txo ( ) . unwrap ( ) , monitor_update) {
@@ -1579,11 +1592,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1579
1592
} ;
1580
1593
}
1581
1594
1582
- if handle_errors. len ( ) > 0 {
1583
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1584
- for ( their_node_id, err) in handle_errors. drain ( ..) {
1585
- let _ = handle_error ! ( self , err, their_node_id, channel_state_lock) ;
1586
- }
1595
+ for ( their_node_id, err) in handle_errors. drain ( ..) {
1596
+ let _ = handle_error ! ( self , err, their_node_id) ;
1587
1597
}
1588
1598
1589
1599
if new_events. is_empty ( ) { return }
@@ -1835,7 +1845,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1835
1845
return ;
1836
1846
} ;
1837
1847
1838
- let _ = handle_error ! ( self , err, their_node_id, channel_state_lock) ;
1848
+ mem:: drop ( channel_state_lock) ;
1849
+ let _ = handle_error ! ( self , err, their_node_id) ;
1839
1850
}
1840
1851
1841
1852
/// Gets the node_id held by this ChannelManager
@@ -2579,9 +2590,9 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
2579
2590
#[ doc( hidden) ]
2580
2591
pub fn update_fee ( & self , channel_id : [ u8 ; 32 ] , feerate_per_kw : u64 ) -> Result < ( ) , APIError > {
2581
2592
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2582
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2583
2593
let their_node_id;
2584
2594
let err: Result < ( ) , _ > = loop {
2595
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2585
2596
let channel_state = & mut * channel_state_lock;
2586
2597
2587
2598
match channel_state. by_id . entry ( channel_id) {
@@ -2620,7 +2631,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
2620
2631
return Ok ( ( ) )
2621
2632
} ;
2622
2633
2623
- match handle_error ! ( self , err, their_node_id, channel_state_lock ) {
2634
+ match handle_error ! ( self , err, their_node_id) {
2624
2635
Ok ( _) => unreachable ! ( ) ,
2625
2636
Err ( e) => { Err ( APIError :: APIMisuseError { err : e. err } ) }
2626
2637
}
@@ -2830,146 +2841,82 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
2830
2841
{
2831
2842
fn handle_open_channel ( & self , their_node_id : & PublicKey , their_features : InitFeatures , msg : & msgs:: OpenChannel ) {
2832
2843
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2833
- let res = self . internal_open_channel ( their_node_id, their_features, msg) ;
2834
- if res. is_err ( ) {
2835
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2836
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2837
- }
2844
+ let _ = handle_error ! ( self , self . internal_open_channel( their_node_id, their_features, msg) , * their_node_id) ;
2838
2845
}
2839
2846
2840
2847
fn handle_accept_channel ( & self , their_node_id : & PublicKey , their_features : InitFeatures , msg : & msgs:: AcceptChannel ) {
2841
2848
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2842
- let res = self . internal_accept_channel ( their_node_id, their_features, msg) ;
2843
- if res. is_err ( ) {
2844
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2845
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2846
- }
2849
+ let _ = handle_error ! ( self , self . internal_accept_channel( their_node_id, their_features, msg) , * their_node_id) ;
2847
2850
}
2848
2851
2849
2852
fn handle_funding_created ( & self , their_node_id : & PublicKey , msg : & msgs:: FundingCreated ) {
2850
2853
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2851
- let res = self . internal_funding_created ( their_node_id, msg) ;
2852
- if res. is_err ( ) {
2853
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2854
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2855
- }
2854
+ let _ = handle_error ! ( self , self . internal_funding_created( their_node_id, msg) , * their_node_id) ;
2856
2855
}
2857
2856
2858
2857
fn handle_funding_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: FundingSigned ) {
2859
2858
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2860
- let res = self . internal_funding_signed ( their_node_id, msg) ;
2861
- if res. is_err ( ) {
2862
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2863
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2864
- }
2859
+ let _ = handle_error ! ( self , self . internal_funding_signed( their_node_id, msg) , * their_node_id) ;
2865
2860
}
2866
2861
2867
2862
fn handle_funding_locked ( & self , their_node_id : & PublicKey , msg : & msgs:: FundingLocked ) {
2868
2863
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2869
- let res = self . internal_funding_locked ( their_node_id, msg) ;
2870
- if res. is_err ( ) {
2871
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2872
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2873
- }
2864
+ let _ = handle_error ! ( self , self . internal_funding_locked( their_node_id, msg) , * their_node_id) ;
2874
2865
}
2875
2866
2876
2867
fn handle_shutdown ( & self , their_node_id : & PublicKey , msg : & msgs:: Shutdown ) {
2877
2868
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2878
- let res = self . internal_shutdown ( their_node_id, msg) ;
2879
- if res. is_err ( ) {
2880
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2881
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2882
- }
2869
+ let _ = handle_error ! ( self , self . internal_shutdown( their_node_id, msg) , * their_node_id) ;
2883
2870
}
2884
2871
2885
2872
fn handle_closing_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: ClosingSigned ) {
2886
2873
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2887
- let res = self . internal_closing_signed ( their_node_id, msg) ;
2888
- if res. is_err ( ) {
2889
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2890
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2891
- }
2874
+ let _ = handle_error ! ( self , self . internal_closing_signed( their_node_id, msg) , * their_node_id) ;
2892
2875
}
2893
2876
2894
2877
fn handle_update_add_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateAddHTLC ) {
2895
2878
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2896
- let res = self . internal_update_add_htlc ( their_node_id, msg) ;
2897
- if res. is_err ( ) {
2898
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2899
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2900
- }
2879
+ let _ = handle_error ! ( self , self . internal_update_add_htlc( their_node_id, msg) , * their_node_id) ;
2901
2880
}
2902
2881
2903
2882
fn handle_update_fulfill_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFulfillHTLC ) {
2904
2883
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2905
- let res = self . internal_update_fulfill_htlc ( their_node_id, msg) ;
2906
- if res. is_err ( ) {
2907
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2908
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2909
- }
2884
+ let _ = handle_error ! ( self , self . internal_update_fulfill_htlc( their_node_id, msg) , * their_node_id) ;
2910
2885
}
2911
2886
2912
2887
fn handle_update_fail_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFailHTLC ) {
2913
2888
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2914
- let res = self . internal_update_fail_htlc ( their_node_id, msg) ;
2915
- if res. is_err ( ) {
2916
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2917
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2918
- }
2889
+ let _ = handle_error ! ( self , self . internal_update_fail_htlc( their_node_id, msg) , * their_node_id) ;
2919
2890
}
2920
2891
2921
2892
fn handle_update_fail_malformed_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFailMalformedHTLC ) {
2922
2893
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2923
- let res = self . internal_update_fail_malformed_htlc ( their_node_id, msg) ;
2924
- if res. is_err ( ) {
2925
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2926
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2927
- }
2894
+ let _ = handle_error ! ( self , self . internal_update_fail_malformed_htlc( their_node_id, msg) , * their_node_id) ;
2928
2895
}
2929
2896
2930
2897
fn handle_commitment_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: CommitmentSigned ) {
2931
2898
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2932
- let res = self . internal_commitment_signed ( their_node_id, msg) ;
2933
- if res. is_err ( ) {
2934
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2935
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2936
- }
2899
+ let _ = handle_error ! ( self , self . internal_commitment_signed( their_node_id, msg) , * their_node_id) ;
2937
2900
}
2938
2901
2939
2902
fn handle_revoke_and_ack ( & self , their_node_id : & PublicKey , msg : & msgs:: RevokeAndACK ) {
2940
2903
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2941
- let res = self . internal_revoke_and_ack ( their_node_id, msg) ;
2942
- if res. is_err ( ) {
2943
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2944
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2945
- }
2904
+ let _ = handle_error ! ( self , self . internal_revoke_and_ack( their_node_id, msg) , * their_node_id) ;
2946
2905
}
2947
2906
2948
2907
fn handle_update_fee ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFee ) {
2949
2908
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2950
- let res = self . internal_update_fee ( their_node_id, msg) ;
2951
- if res. is_err ( ) {
2952
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2953
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2954
- }
2909
+ let _ = handle_error ! ( self , self . internal_update_fee( their_node_id, msg) , * their_node_id) ;
2955
2910
}
2956
2911
2957
2912
fn handle_announcement_signatures ( & self , their_node_id : & PublicKey , msg : & msgs:: AnnouncementSignatures ) {
2958
2913
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2959
- let res = self . internal_announcement_signatures ( their_node_id, msg) ;
2960
- if res. is_err ( ) {
2961
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2962
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2963
- }
2914
+ let _ = handle_error ! ( self , self . internal_announcement_signatures( their_node_id, msg) , * their_node_id) ;
2964
2915
}
2965
2916
2966
2917
fn handle_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) {
2967
2918
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2968
- let res = self . internal_channel_reestablish ( their_node_id, msg) ;
2969
- if res. is_err ( ) {
2970
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2971
- let _ = handle_error ! ( self , res, * their_node_id, channel_state_lock) ;
2972
- }
2919
+ let _ = handle_error ! ( self , self . internal_channel_reestablish( their_node_id, msg) , * their_node_id) ;
2973
2920
}
2974
2921
2975
2922
fn peer_disconnected ( & self , their_node_id : & PublicKey , no_connection_possible : bool ) {
0 commit comments