@@ -28,7 +28,7 @@ use secp256k1;
28
28
use chain:: chaininterface:: { BroadcasterInterface , ChainListener , FeeEstimator } ;
29
29
use chain:: transaction:: OutPoint ;
30
30
use ln:: channel:: { Channel , ChannelError } ;
31
- use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr , ManyChannelMonitor , CLTV_CLAIM_BUFFER , LATENCY_GRACE_PERIOD_BLOCKS , ANTI_REORG_DELAY } ;
31
+ use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdate , ChannelMonitorUpdateErr , ManyChannelMonitor , CLTV_CLAIM_BUFFER , LATENCY_GRACE_PERIOD_BLOCKS , ANTI_REORG_DELAY } ;
32
32
use ln:: features:: { InitFeatures , NodeFeatures } ;
33
33
use ln:: router:: Route ;
34
34
use ln:: msgs;
@@ -152,7 +152,7 @@ pub struct PaymentHash(pub [u8;32]);
152
152
#[ derive( Hash , Copy , Clone , PartialEq , Eq , Debug ) ]
153
153
pub struct PaymentPreimage ( pub [ u8 ; 32 ] ) ;
154
154
155
- type ShutdownResult = ( Vec < Transaction > , Vec < ( HTLCSource , PaymentHash ) > ) ;
155
+ type ShutdownResult = ( Option < OutPoint > , ChannelMonitorUpdate , Vec < ( HTLCSource , PaymentHash ) > ) ;
156
156
157
157
/// Error type returned across the channel_state mutex boundary. When an Err is generated for a
158
158
/// Channel, we generally end up with a ChannelError::Close for which we have to close the channel
@@ -502,8 +502,7 @@ macro_rules! break_chan_entry {
502
502
if let Some ( short_id) = chan. get_short_channel_id( ) {
503
503
$channel_state. short_to_id. remove( & short_id) ;
504
504
}
505
- break Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
506
- } ,
505
+ break Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( true ) , $self. get_channel_update( & chan) . ok( ) ) ) } ,
507
506
Err ( 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" ) ; }
508
507
}
509
508
}
@@ -522,7 +521,7 @@ macro_rules! try_chan_entry {
522
521
if let Some ( short_id) = chan. get_short_channel_id( ) {
523
522
$channel_state. short_to_id. remove( & short_id) ;
524
523
}
525
- return Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) )
524
+ return Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( true ) , $self. get_channel_update( & chan) . ok( ) ) )
526
525
} ,
527
526
Err ( ChannelError :: CloseDelayBroadcast { msg, update } ) => {
528
527
log_error!( $self, "Channel {} need to be shutdown but closing transactions not broadcast due to {}" , log_bytes!( $entry. key( ) [ ..] ) , msg) ;
@@ -540,11 +539,7 @@ macro_rules! try_chan_entry {
540
539
ChannelMonitorUpdateErr :: TemporaryFailure => { } ,
541
540
}
542
541
}
543
- let mut shutdown_res = chan. force_shutdown( ) ;
544
- if shutdown_res. 0 . len( ) >= 1 {
545
- log_error!( $self, "You have a toxic local commitment transaction {} avaible in channel monitor, read comment in ChannelMonitor::get_latest_local_commitment_txn to be informed of manual action to take" , shutdown_res. 0 [ 0 ] . txid( ) ) ;
546
- }
547
- shutdown_res. 0 . clear( ) ;
542
+ let shutdown_res = chan. force_shutdown( false ) ;
548
543
return Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, shutdown_res, $self. get_channel_update( & chan) . ok( ) ) )
549
544
}
550
545
}
@@ -572,7 +567,7 @@ macro_rules! handle_monitor_err {
572
567
// splitting hairs we'd prefer to claim payments that were to us, but we haven't
573
568
// given up the preimage yet, so might as well just wait until the payment is
574
569
// retried, avoiding the on-chain fees.
575
- let res: Result <( ) , _> = Err ( MsgHandleErrInternal :: from_finish_shutdown( "ChannelMonitor storage failure" , channel_id, chan. force_shutdown( ) , $self. get_channel_update( & chan) . ok( ) ) ) ;
570
+ let res: Result <( ) , _> = Err ( MsgHandleErrInternal :: from_finish_shutdown( "ChannelMonitor storage failure" , channel_id, chan. force_shutdown( true ) , $self. get_channel_update( & chan) . ok( ) ) ) ;
576
571
res
577
572
} ,
578
573
ChannelMonitorUpdateErr :: TemporaryFailure => {
@@ -820,14 +815,17 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
820
815
821
816
#[ inline]
822
817
fn finish_force_close_channel ( & self , shutdown_res : ShutdownResult ) {
823
- let ( local_txn , mut failed_htlcs) = shutdown_res;
824
- log_trace ! ( self , "Finishing force-closure of channel with {} transactions to broadcast and {} HTLCs to fail" , local_txn . len ( ) , failed_htlcs. len( ) ) ;
818
+ let ( funding_txo_option , monitor_update , mut failed_htlcs) = shutdown_res;
819
+ log_trace ! ( self , "Finishing force-closure of channel {} HTLCs to fail" , failed_htlcs. len( ) ) ;
825
820
for htlc_source in failed_htlcs. drain ( ..) {
826
821
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 ( ) } ) ;
827
822
}
828
- for tx in local_txn {
829
- log_trace ! ( self , "Broadcast onchain {}" , log_tx!( tx) ) ;
830
- self . tx_broadcaster . broadcast_transaction ( & tx) ;
823
+ if let Some ( funding_txo) = funding_txo_option {
824
+ // There isn't anything we can do if we get an update failure - we're already
825
+ // force-closing. The monitor update on the required in-memory copy should broadcast
826
+ // the latest local state, which is the best we can do anyway. Thus, it is safe to
827
+ // ignore the result here.
828
+ let _ = self . monitor . update_monitor ( funding_txo, monitor_update) ;
831
829
}
832
830
}
833
831
@@ -849,7 +847,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
849
847
}
850
848
} ;
851
849
log_trace ! ( self , "Force-closing channel {}" , log_bytes!( channel_id[ ..] ) ) ;
852
- self . finish_force_close_channel ( chan. force_shutdown ( ) ) ;
850
+ self . finish_force_close_channel ( chan. force_shutdown ( true ) ) ;
853
851
if let Ok ( update) = self . get_channel_update ( & chan) {
854
852
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
855
853
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
@@ -1268,7 +1266,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1268
1266
Some ( mut chan) => {
1269
1267
( chan. get_outbound_funding_created ( funding_txo)
1270
1268
. map_err ( |e| if let ChannelError :: Close ( msg) = e {
1271
- MsgHandleErrInternal :: from_finish_shutdown ( msg, chan. channel_id ( ) , chan. force_shutdown ( ) , None )
1269
+ MsgHandleErrInternal :: from_finish_shutdown ( msg, chan. channel_id ( ) , chan. force_shutdown ( true ) , None )
1272
1270
} else { unreachable ! ( ) ; } )
1273
1271
, chan)
1274
1272
} ,
@@ -1288,7 +1286,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1288
1286
ChannelMonitorUpdateErr :: PermanentFailure => {
1289
1287
{
1290
1288
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1291
- match handle_error ! ( self , Err ( MsgHandleErrInternal :: from_finish_shutdown( "ChannelMonitor storage failure" , * temporary_channel_id, chan. force_shutdown( ) , None ) ) , chan. get_their_node_id( ) , channel_state) {
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) {
1292
1290
Err ( _) => { return ; } ,
1293
1291
Ok ( ( ) ) => unreachable ! ( ) ,
1294
1292
}
@@ -1518,7 +1516,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1518
1516
if let Some ( short_id) = channel. get_short_channel_id ( ) {
1519
1517
channel_state. short_to_id . remove ( & short_id) ;
1520
1518
}
1521
- Err ( MsgHandleErrInternal :: from_finish_shutdown ( msg, channel_id, channel. force_shutdown ( ) , self . get_channel_update ( & channel) . ok ( ) ) )
1519
+ Err ( MsgHandleErrInternal :: from_finish_shutdown ( msg, channel_id, channel. force_shutdown ( true ) , self . get_channel_update ( & channel) . ok ( ) ) )
1522
1520
} ,
1523
1521
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" ) ; }
1524
1522
} ;
@@ -2021,7 +2019,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
2021
2019
// channel, not the temporary_channel_id. This is compatible with ourselves, but the
2022
2020
// spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
2023
2021
// any messages referencing a previously-closed channel anyway.
2024
- return Err ( MsgHandleErrInternal :: from_finish_shutdown ( "ChannelMonitor storage failure" , funding_msg. channel_id , chan. force_shutdown ( ) , None ) ) ;
2022
+ return Err ( MsgHandleErrInternal :: from_finish_shutdown ( "ChannelMonitor storage failure" , funding_msg. channel_id , chan. force_shutdown ( true ) , None ) ) ;
2025
2023
} ,
2026
2024
ChannelMonitorUpdateErr :: TemporaryFailure => {
2027
2025
// There's no problem signing a counterparty's funding transaction if our monitor
@@ -2741,7 +2739,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
2741
2739
// It looks like our counterparty went on-chain. We go ahead and
2742
2740
// broadcast our latest local state as well here, just in case its
2743
2741
// some kind of SPV attack, though we expect these to be dropped.
2744
- failed_channels. push ( channel. force_shutdown ( ) ) ;
2742
+ failed_channels. push ( channel. force_shutdown ( true ) ) ;
2745
2743
if let Ok ( update) = self . get_channel_update ( & channel) {
2746
2744
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2747
2745
msg : update
@@ -2756,11 +2754,10 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
2756
2754
if let Some ( short_id) = channel. get_short_channel_id ( ) {
2757
2755
short_to_id. remove ( & short_id) ;
2758
2756
}
2759
- failed_channels. push ( channel. force_shutdown ( ) ) ;
2760
2757
// If would_broadcast_at_height() is true, the channel_monitor will broadcast
2761
2758
// the latest local tx for us, so we should skip that here (it doesn't really
2762
2759
// hurt anything, but does make tests a bit simpler).
2763
- failed_channels. last_mut ( ) . unwrap ( ) . 0 = Vec :: new ( ) ;
2760
+ failed_channels. push ( channel . force_shutdown ( false ) ) ;
2764
2761
if let Ok ( update) = self . get_channel_update ( & channel) {
2765
2762
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2766
2763
msg : update
@@ -2804,7 +2801,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
2804
2801
if let Some ( short_id) = v. get_short_channel_id ( ) {
2805
2802
short_to_id. remove ( & short_id) ;
2806
2803
}
2807
- failed_channels. push ( v. force_shutdown ( ) ) ;
2804
+ failed_channels. push ( v. force_shutdown ( true ) ) ;
2808
2805
if let Ok ( update) = self . get_channel_update ( & v) {
2809
2806
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2810
2807
msg : update
@@ -2992,7 +2989,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
2992
2989
if let Some ( short_id) = chan. get_short_channel_id ( ) {
2993
2990
short_to_id. remove ( & short_id) ;
2994
2991
}
2995
- failed_channels. push ( chan. force_shutdown ( ) ) ;
2992
+ failed_channels. push ( chan. force_shutdown ( true ) ) ;
2996
2993
if let Ok ( update) = self . get_channel_update ( & chan) {
2997
2994
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2998
2995
msg : update
@@ -3458,7 +3455,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3458
3455
let latest_block_height: u32 = Readable :: read ( reader) ?;
3459
3456
let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
3460
3457
3461
- let mut closed_channels = Vec :: new ( ) ;
3458
+ let mut failed_htlcs = Vec :: new ( ) ;
3462
3459
3463
3460
let channel_count: u64 = Readable :: read ( reader) ?;
3464
3461
let mut funding_txo_set = HashSet :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
@@ -3477,9 +3474,9 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3477
3474
channel. get_revoked_remote_commitment_transaction_number ( ) != monitor. get_min_seen_secret ( ) ||
3478
3475
channel. get_cur_remote_commitment_transaction_number ( ) != monitor. get_cur_remote_commitment_number ( ) ||
3479
3476
channel. get_latest_monitor_update_id ( ) != monitor. get_latest_update_id ( ) {
3480
- let mut force_close_res = channel. force_shutdown ( ) ;
3481
- force_close_res . 0 = monitor . get_latest_local_commitment_txn ( ) ;
3482
- closed_channels . push ( force_close_res ) ;
3477
+ let ( _ , _ , mut new_failed_htlcs ) = channel. force_shutdown ( true ) ;
3478
+ failed_htlcs . append ( & mut new_failed_htlcs ) ;
3479
+ monitor . broadcast_latest_local_commitment_txn ( & args . tx_broadcaster ) ;
3483
3480
} else {
3484
3481
if let Some ( short_channel_id) = channel. get_short_channel_id ( ) {
3485
3482
short_to_id. insert ( short_channel_id, channel. channel_id ( ) ) ;
@@ -3493,7 +3490,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3493
3490
3494
3491
for ( ref funding_txo, ref mut monitor) in args. channel_monitors . iter_mut ( ) {
3495
3492
if !funding_txo_set. contains ( funding_txo) {
3496
- closed_channels . push ( ( monitor. get_latest_local_commitment_txn ( ) , Vec :: new ( ) ) ) ;
3493
+ monitor. broadcast_latest_local_commitment_txn ( & args . tx_broadcaster ) ;
3497
3494
}
3498
3495
}
3499
3496
@@ -3563,12 +3560,13 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3563
3560
default_configuration : args. default_config ,
3564
3561
} ;
3565
3562
3566
- for close_res in closed_channels. drain ( ..) {
3567
- channel_manager. finish_force_close_channel ( close_res) ;
3568
- //TODO: Broadcast channel update for closed channels, but only after we've made a
3569
- //connection or two.
3563
+ for htlc_source in failed_htlcs. drain ( ..) {
3564
+ channel_manager. fail_htlc_backwards_internal ( channel_manager. channel_state . lock ( ) . unwrap ( ) , htlc_source. 0 , & htlc_source. 1 , HTLCFailReason :: Reason { failure_code : 0x4000 | 8 , data : Vec :: new ( ) } ) ;
3570
3565
}
3571
3566
3567
+ //TODO: Broadcast channel update for closed channels, but only after we've made a
3568
+ //connection or two.
3569
+
3572
3570
Ok ( ( last_block_hash. clone ( ) , channel_manager) )
3573
3571
}
3574
3572
}
0 commit comments