@@ -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,14 @@ 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
+ // XXX: Add comment for why this is OK.
825
+ let _ = self . monitor . update_monitor ( funding_txo , monitor_update ) ;
831
826
}
832
827
}
833
828
@@ -849,7 +844,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
849
844
}
850
845
} ;
851
846
log_trace ! ( self , "Force-closing channel {}" , log_bytes!( channel_id[ ..] ) ) ;
852
- self . finish_force_close_channel ( chan. force_shutdown ( ) ) ;
847
+ self . finish_force_close_channel ( chan. force_shutdown ( true ) ) ;
853
848
if let Ok ( update) = self . get_channel_update ( & chan) {
854
849
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
855
850
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
@@ -1268,7 +1263,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1268
1263
Some ( mut chan) => {
1269
1264
( chan. get_outbound_funding_created ( funding_txo)
1270
1265
. map_err ( |e| if let ChannelError :: Close ( msg) = e {
1271
- MsgHandleErrInternal :: from_finish_shutdown ( msg, chan. channel_id ( ) , chan. force_shutdown ( ) , None )
1266
+ MsgHandleErrInternal :: from_finish_shutdown ( msg, chan. channel_id ( ) , chan. force_shutdown ( true ) , None )
1272
1267
} else { unreachable ! ( ) ; } )
1273
1268
, chan)
1274
1269
} ,
@@ -1288,7 +1283,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1288
1283
ChannelMonitorUpdateErr :: PermanentFailure => {
1289
1284
{
1290
1285
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) {
1286
+ 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
1287
Err ( _) => { return ; } ,
1293
1288
Ok ( ( ) ) => unreachable ! ( ) ,
1294
1289
}
@@ -1518,7 +1513,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1518
1513
if let Some ( short_id) = channel. get_short_channel_id ( ) {
1519
1514
channel_state. short_to_id . remove ( & short_id) ;
1520
1515
}
1521
- Err ( MsgHandleErrInternal :: from_finish_shutdown ( msg, channel_id, channel. force_shutdown ( ) , self . get_channel_update ( & channel) . ok ( ) ) )
1516
+ Err ( MsgHandleErrInternal :: from_finish_shutdown ( msg, channel_id, channel. force_shutdown ( true ) , self . get_channel_update ( & channel) . ok ( ) ) )
1522
1517
} ,
1523
1518
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
1519
} ;
@@ -2021,7 +2016,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
2021
2016
// channel, not the temporary_channel_id. This is compatible with ourselves, but the
2022
2017
// spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
2023
2018
// 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 ) ) ;
2019
+ return Err ( MsgHandleErrInternal :: from_finish_shutdown ( "ChannelMonitor storage failure" , funding_msg. channel_id , chan. force_shutdown ( true ) , None ) ) ;
2025
2020
} ,
2026
2021
ChannelMonitorUpdateErr :: TemporaryFailure => {
2027
2022
// There's no problem signing a counterparty's funding transaction if our monitor
@@ -2741,7 +2736,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
2741
2736
// It looks like our counterparty went on-chain. We go ahead and
2742
2737
// broadcast our latest local state as well here, just in case its
2743
2738
// some kind of SPV attack, though we expect these to be dropped.
2744
- failed_channels. push ( channel. force_shutdown ( ) ) ;
2739
+ failed_channels. push ( channel. force_shutdown ( true ) ) ;
2745
2740
if let Ok ( update) = self . get_channel_update ( & channel) {
2746
2741
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2747
2742
msg : update
@@ -2756,11 +2751,10 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
2756
2751
if let Some ( short_id) = channel. get_short_channel_id ( ) {
2757
2752
short_to_id. remove ( & short_id) ;
2758
2753
}
2759
- failed_channels. push ( channel. force_shutdown ( ) ) ;
2760
2754
// If would_broadcast_at_height() is true, the channel_monitor will broadcast
2761
2755
// the latest local tx for us, so we should skip that here (it doesn't really
2762
2756
// hurt anything, but does make tests a bit simpler).
2763
- failed_channels. last_mut ( ) . unwrap ( ) . 0 = Vec :: new ( ) ;
2757
+ failed_channels. push ( channel . force_shutdown ( false ) ) ;
2764
2758
if let Ok ( update) = self . get_channel_update ( & channel) {
2765
2759
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2766
2760
msg : update
@@ -2804,7 +2798,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
2804
2798
if let Some ( short_id) = v. get_short_channel_id ( ) {
2805
2799
short_to_id. remove ( & short_id) ;
2806
2800
}
2807
- failed_channels. push ( v. force_shutdown ( ) ) ;
2801
+ failed_channels. push ( v. force_shutdown ( true ) ) ;
2808
2802
if let Ok ( update) = self . get_channel_update ( & v) {
2809
2803
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2810
2804
msg : update
@@ -2992,7 +2986,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
2992
2986
if let Some ( short_id) = chan. get_short_channel_id ( ) {
2993
2987
short_to_id. remove ( & short_id) ;
2994
2988
}
2995
- failed_channels. push ( chan. force_shutdown ( ) ) ;
2989
+ failed_channels. push ( chan. force_shutdown ( true ) ) ;
2996
2990
if let Ok ( update) = self . get_channel_update ( & chan) {
2997
2991
pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
2998
2992
msg : update
@@ -3458,7 +3452,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3458
3452
let latest_block_height: u32 = Readable :: read ( reader) ?;
3459
3453
let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
3460
3454
3461
- let mut closed_channels = Vec :: new ( ) ;
3455
+ let mut failed_htlcs = Vec :: new ( ) ;
3462
3456
3463
3457
let channel_count: u64 = Readable :: read ( reader) ?;
3464
3458
let mut funding_txo_set = HashSet :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
@@ -3477,9 +3471,9 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3477
3471
channel. get_revoked_remote_commitment_transaction_number ( ) != monitor. get_min_seen_secret ( ) ||
3478
3472
channel. get_cur_remote_commitment_transaction_number ( ) != monitor. get_cur_remote_commitment_number ( ) ||
3479
3473
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 ) ;
3474
+ let ( _ , _ , mut new_failed_htlcs ) = channel. force_shutdown ( true ) ;
3475
+ failed_htlcs . append ( & mut new_failed_htlcs ) ;
3476
+ monitor . broadcast_latest_local_commitment_txn ( & args . tx_broadcaster ) ;
3483
3477
} else {
3484
3478
if let Some ( short_channel_id) = channel. get_short_channel_id ( ) {
3485
3479
short_to_id. insert ( short_channel_id, channel. channel_id ( ) ) ;
@@ -3493,7 +3487,7 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3493
3487
3494
3488
for ( ref funding_txo, ref mut monitor) in args. channel_monitors . iter_mut ( ) {
3495
3489
if !funding_txo_set. contains ( funding_txo) {
3496
- closed_channels . push ( ( monitor. get_latest_local_commitment_txn ( ) , Vec :: new ( ) ) ) ;
3490
+ monitor. broadcast_latest_local_commitment_txn ( & args . tx_broadcaster ) ;
3497
3491
}
3498
3492
}
3499
3493
@@ -3563,12 +3557,13 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3563
3557
default_configuration : args. default_config ,
3564
3558
} ;
3565
3559
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.
3560
+ for htlc_source in failed_htlcs. drain ( ..) {
3561
+ 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
3562
}
3571
3563
3564
+ //TODO: Broadcast channel update for closed channels, but only after we've made a
3565
+ //connection or two.
3566
+
3572
3567
Ok ( ( last_block_hash. clone ( ) , channel_manager) )
3573
3568
}
3574
3569
}
0 commit comments