@@ -181,9 +181,9 @@ enum ChannelState {
181
181
/// "disconnected" and no updates are allowed until after we've done a channel_reestablish
182
182
/// dance.
183
183
PeerDisconnected = ( 1 << 7 ) ,
184
- /// Flag which is set on ChannelFunded and FundingSent indicating the user has told us they
185
- /// failed to update our ChannelMonitor somewhere and we should pause sending any outbound
186
- /// messages until they've managed to do so.
184
+ /// Flag which is set on ChannelFunded, FundingCreated, and FundingSent indicating the user has
185
+ /// told us they failed to update our ChannelMonitor somewhere and we should pause sending any
186
+ /// outbound messages until they've managed to do so.
187
187
MonitorUpdateFailed = ( 1 << 8 ) ,
188
188
/// Flag which implies that we have sent a commitment_signed but are awaiting the responding
189
189
/// revoke_and_ack message. During this time period, we can't generate new commitment_signed
@@ -248,6 +248,7 @@ pub(super) struct Channel {
248
248
/// send it first.
249
249
resend_order : RAACommitmentOrder ,
250
250
251
+ monitor_pending_funding_locked : bool ,
251
252
monitor_pending_revoke_and_ack : bool ,
252
253
monitor_pending_commitment_signed : bool ,
253
254
monitor_pending_forwards : Vec < ( PendingForwardHTLCInfo , u64 ) > ,
@@ -457,6 +458,7 @@ impl Channel {
457
458
458
459
resend_order : RAACommitmentOrder :: CommitmentFirst ,
459
460
461
+ monitor_pending_funding_locked : false ,
460
462
monitor_pending_revoke_and_ack : false ,
461
463
monitor_pending_commitment_signed : false ,
462
464
monitor_pending_forwards : Vec :: new ( ) ,
@@ -672,6 +674,7 @@ impl Channel {
672
674
673
675
resend_order : RAACommitmentOrder :: CommitmentFirst ,
674
676
677
+ monitor_pending_funding_locked : false ,
675
678
monitor_pending_revoke_and_ack : false ,
676
679
monitor_pending_commitment_signed : false ,
677
680
monitor_pending_forwards : Vec :: new ( ) ,
@@ -1583,10 +1586,12 @@ impl Channel {
1583
1586
} else if non_shutdown_state == ( ChannelState :: FundingSent as u32 | ChannelState :: OurFundingLocked as u32 ) {
1584
1587
self . channel_state = ChannelState :: ChannelFunded as u32 | ( self . channel_state & MULTI_STATE_FLAGS ) ;
1585
1588
self . channel_update_count += 1 ;
1586
- } else if self . channel_state & ( ChannelState :: ChannelFunded as u32 ) != 0 &&
1587
- // Note that funding_signed/funding_created will have decremented both by 1!
1588
- self . cur_local_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
1589
- self . cur_remote_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 {
1589
+ } else if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) != 0 &&
1590
+ // Note that funding_signed/funding_created will have decremented both by 1!
1591
+ self . cur_local_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
1592
+ self . cur_remote_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 ) ||
1593
+ ( self . channel_state & ( ChannelState :: FundingSent as u32 | ChannelState :: TheirFundingLocked as u32 ) ==
1594
+ ( ChannelState :: FundingSent as u32 | ChannelState :: TheirFundingLocked as u32 ) ) {
1590
1595
if self . their_cur_commitment_point != Some ( msg. next_per_commitment_point ) {
1591
1596
return Err ( ChannelError :: Close ( "Peer sent a reconnect funding_locked with a different point" ) ) ;
1592
1597
}
@@ -2349,11 +2354,28 @@ impl Channel {
2349
2354
/// Indicates that the latest ChannelMonitor update has been committed by the client
2350
2355
/// successfully and we should restore normal operation. Returns messages which should be sent
2351
2356
/// to the remote side.
2352
- pub fn monitor_updating_restored ( & mut self ) -> ( Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , RAACommitmentOrder , Vec < ( PendingForwardHTLCInfo , u64 ) > , Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > , bool ) {
2357
+ pub fn monitor_updating_restored ( & mut self ) -> ( Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , RAACommitmentOrder , Vec < ( PendingForwardHTLCInfo , u64 ) > , Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > , bool , Option < msgs :: FundingLocked > ) {
2353
2358
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 , ChannelState :: MonitorUpdateFailed as u32 ) ;
2354
2359
self . channel_state &= !( ChannelState :: MonitorUpdateFailed as u32 ) ;
2360
+
2355
2361
let needs_broadcast_safe = self . channel_state & ( ChannelState :: FundingSent as u32 ) != 0 && self . channel_outbound ;
2356
2362
2363
+ // Because we will never generate a FundingBroadcastSafe event when we're in
2364
+ // MonitorUpdateFailed, if we assume the user only broadcast the funding transaction when
2365
+ // they received the FundingBroadcastSafe event, we can only ever hit
2366
+ // monitor_pending_funding_locked when we're an inbound channel which failed to persist the
2367
+ // monitor on funding_signed, and we even got the funding transaction confirmed before the
2368
+ // monitor was persisted.
2369
+ let funding_locked = if self . monitor_pending_funding_locked {
2370
+ assert ! ( !self . channel_outbound, "Funding transaction broadcast without FundingBroadcastSafe!" ) ;
2371
+ let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
2372
+ let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
2373
+ Some ( msgs:: FundingLocked {
2374
+ channel_id : self . channel_id ( ) ,
2375
+ next_per_commitment_point : next_per_commitment_point,
2376
+ } )
2377
+ } else { None } ;
2378
+
2357
2379
let mut forwards = Vec :: new ( ) ;
2358
2380
mem:: swap ( & mut forwards, & mut self . monitor_pending_forwards ) ;
2359
2381
let mut failures = Vec :: new ( ) ;
@@ -2362,7 +2384,7 @@ impl Channel {
2362
2384
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) != 0 {
2363
2385
self . monitor_pending_revoke_and_ack = false ;
2364
2386
self . monitor_pending_commitment_signed = false ;
2365
- return ( None , None , RAACommitmentOrder :: RevokeAndACKFirst , forwards, failures, needs_broadcast_safe) ;
2387
+ return ( None , None , RAACommitmentOrder :: RevokeAndACKFirst , forwards, failures, needs_broadcast_safe, funding_locked ) ;
2366
2388
}
2367
2389
2368
2390
let raa = if self . monitor_pending_revoke_and_ack {
@@ -2380,7 +2402,7 @@ impl Channel {
2380
2402
if commitment_update. is_some( ) { "a" } else { "no" } ,
2381
2403
if raa. is_some( ) { "an" } else { "no" } ,
2382
2404
match order { RAACommitmentOrder :: CommitmentFirst => "commitment" , RAACommitmentOrder :: RevokeAndACKFirst => "RAA" } ) ;
2383
- ( raa, commitment_update, order, forwards, failures, needs_broadcast_safe)
2405
+ ( raa, commitment_update, order, forwards, failures, needs_broadcast_safe, funding_locked )
2384
2406
}
2385
2407
2386
2408
pub fn update_fee ( & mut self , fee_estimator : & FeeEstimator , msg : & msgs:: UpdateFee ) -> Result < ( ) , ChannelError > {
@@ -2490,7 +2512,9 @@ impl Channel {
2490
2512
} else { None } ;
2491
2513
2492
2514
if self . channel_state & ( ChannelState :: FundingSent as u32 ) == ChannelState :: FundingSent as u32 {
2493
- if self . channel_state & ChannelState :: OurFundingLocked as u32 == 0 {
2515
+ // If we're waiting on a monitor update, we shouldn't re-send any funding_locked's.
2516
+ if self . channel_state & ( ChannelState :: OurFundingLocked as u32 ) == 0 ||
2517
+ self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) != 0 {
2494
2518
if msg. next_remote_commitment_number != 0 {
2495
2519
return Err ( ChannelError :: Close ( "Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet" ) ) ;
2496
2520
}
@@ -2981,12 +3005,17 @@ impl Channel {
2981
3005
//they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
2982
3006
//a protocol oversight, but I assume I'm just missing something.
2983
3007
if need_commitment_update {
2984
- let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
2985
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
2986
- return Ok ( Some ( msgs:: FundingLocked {
2987
- channel_id : self . channel_id ,
2988
- next_per_commitment_point : next_per_commitment_point,
2989
- } ) ) ;
3008
+ if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
3009
+ let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
3010
+ let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
3011
+ return Ok ( Some ( msgs:: FundingLocked {
3012
+ channel_id : self . channel_id ,
3013
+ next_per_commitment_point : next_per_commitment_point,
3014
+ } ) ) ;
3015
+ } else {
3016
+ self . monitor_pending_funding_locked = true ;
3017
+ return Ok ( None ) ;
3018
+ }
2990
3019
}
2991
3020
}
2992
3021
}
@@ -3705,6 +3734,7 @@ impl Writeable for Channel {
3705
3734
RAACommitmentOrder :: RevokeAndACKFirst => 1u8 . write ( writer) ?,
3706
3735
}
3707
3736
3737
+ self . monitor_pending_funding_locked . write ( writer) ?;
3708
3738
self . monitor_pending_revoke_and_ack . write ( writer) ?;
3709
3739
self . monitor_pending_commitment_signed . write ( writer) ?;
3710
3740
@@ -3872,6 +3902,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3872
3902
_ => return Err ( DecodeError :: InvalidValue ) ,
3873
3903
} ;
3874
3904
3905
+ let monitor_pending_funding_locked = Readable :: read ( reader) ?;
3875
3906
let monitor_pending_revoke_and_ack = Readable :: read ( reader) ?;
3876
3907
let monitor_pending_commitment_signed = Readable :: read ( reader) ?;
3877
3908
@@ -3968,6 +3999,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3968
3999
3969
4000
resend_order,
3970
4001
4002
+ monitor_pending_funding_locked,
3971
4003
monitor_pending_revoke_and_ack,
3972
4004
monitor_pending_commitment_signed,
3973
4005
monitor_pending_forwards,
0 commit comments