@@ -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 ( ) ,
@@ -1538,7 +1541,7 @@ impl Channel {
1538
1541
if !self . channel_outbound {
1539
1542
return Err ( ChannelError :: Close ( "Received funding_signed for an inbound channel?" ) ) ;
1540
1543
}
1541
- if self . channel_state != ChannelState :: FundingCreated as u32 {
1544
+ if self . channel_state & ! ( ChannelState :: MonitorUpdateFailed as u32 ) != ChannelState :: FundingCreated as u32 {
1542
1545
return Err ( ChannelError :: Close ( "Received funding_signed in strange state!" ) ) ;
1543
1546
}
1544
1547
if self . channel_monitor . get_min_seen_secret ( ) != ( 1 << 48 ) ||
@@ -1559,10 +1562,14 @@ impl Channel {
1559
1562
self . sign_commitment_transaction ( & mut local_initial_commitment_tx, & msg. signature ) ;
1560
1563
self . channel_monitor . provide_latest_local_commitment_tx_info ( local_initial_commitment_tx. clone ( ) , local_keys, self . feerate_per_kw , Vec :: new ( ) ) ;
1561
1564
self . last_local_commitment_txn = vec ! [ local_initial_commitment_tx] ;
1562
- self . channel_state = ChannelState :: FundingSent as u32 ;
1565
+ self . channel_state = ChannelState :: FundingSent as u32 | ( self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) ) ;
1563
1566
self . cur_local_commitment_transaction_number -= 1 ;
1564
1567
1565
- Ok ( self . channel_monitor . clone ( ) )
1568
+ if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
1569
+ Ok ( self . channel_monitor . clone ( ) )
1570
+ } else {
1571
+ Err ( ChannelError :: Ignore ( "Previous monitor update failure prevented funding_signed from allowing funding broadcast" ) )
1572
+ }
1566
1573
}
1567
1574
1568
1575
pub fn funding_locked ( & mut self , msg : & msgs:: FundingLocked ) -> Result < ( ) , ChannelError > {
@@ -1577,10 +1584,13 @@ impl Channel {
1577
1584
} else if non_shutdown_state == ( ChannelState :: FundingSent as u32 | ChannelState :: OurFundingLocked as u32 ) {
1578
1585
self . channel_state = ChannelState :: ChannelFunded as u32 | ( self . channel_state & MULTI_STATE_FLAGS ) ;
1579
1586
self . channel_update_count += 1 ;
1580
- } else if self . channel_state & ( ChannelState :: ChannelFunded as u32 ) != 0 &&
1581
- // Note that funding_signed/funding_created will have decremented both by 1!
1582
- self . cur_local_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
1583
- self . cur_remote_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 {
1587
+ } else if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) != 0 &&
1588
+ // Note that funding_signed/funding_created will have decremented both by 1!
1589
+ self . cur_local_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 &&
1590
+ self . cur_remote_commitment_transaction_number == INITIAL_COMMITMENT_NUMBER - 1 ) ||
1591
+ // If we reconnected before sending our funding locked they may still resend theirs:
1592
+ ( self . channel_state & ( ChannelState :: FundingSent as u32 | ChannelState :: TheirFundingLocked as u32 ) ==
1593
+ ( ChannelState :: FundingSent as u32 | ChannelState :: TheirFundingLocked as u32 ) ) {
1584
1594
if self . their_cur_commitment_point != Some ( msg. next_per_commitment_point ) {
1585
1595
return Err ( ChannelError :: Close ( "Peer sent a reconnect funding_locked with a different point" ) ) ;
1586
1596
}
@@ -2343,10 +2353,29 @@ impl Channel {
2343
2353
/// Indicates that the latest ChannelMonitor update has been committed by the client
2344
2354
/// successfully and we should restore normal operation. Returns messages which should be sent
2345
2355
/// to the remote side.
2346
- pub fn monitor_updating_restored ( & mut self ) -> ( Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , RAACommitmentOrder , Vec < ( PendingForwardHTLCInfo , u64 ) > , Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ) {
2356
+ 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 > ) {
2347
2357
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 , ChannelState :: MonitorUpdateFailed as u32 ) ;
2348
2358
self . channel_state &= !( ChannelState :: MonitorUpdateFailed as u32 ) ;
2349
2359
2360
+ let needs_broadcast_safe = self . channel_state & ( ChannelState :: FundingSent as u32 ) != 0 && self . channel_outbound ;
2361
+
2362
+ // Because we will never generate a FundingBroadcastSafe event when we're in
2363
+ // MonitorUpdateFailed, if we assume the user only broadcast the funding transaction when
2364
+ // they received the FundingBroadcastSafe event, we can only ever hit
2365
+ // monitor_pending_funding_locked when we're an inbound channel which failed to persist the
2366
+ // monitor on funding_created, and we even got the funding transaction confirmed before the
2367
+ // monitor was persisted.
2368
+ let funding_locked = if self . monitor_pending_funding_locked {
2369
+ assert ! ( !self . channel_outbound, "Funding transaction broadcast without FundingBroadcastSafe!" ) ;
2370
+ self . monitor_pending_funding_locked = false ;
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
+
2350
2379
let mut forwards = Vec :: new ( ) ;
2351
2380
mem:: swap ( & mut forwards, & mut self . monitor_pending_forwards ) ;
2352
2381
let mut failures = Vec :: new ( ) ;
@@ -2355,7 +2384,7 @@ impl Channel {
2355
2384
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) != 0 {
2356
2385
self . monitor_pending_revoke_and_ack = false ;
2357
2386
self . monitor_pending_commitment_signed = false ;
2358
- return ( None , None , RAACommitmentOrder :: RevokeAndACKFirst , forwards, failures) ;
2387
+ return ( None , None , RAACommitmentOrder :: RevokeAndACKFirst , forwards, failures, needs_broadcast_safe , funding_locked ) ;
2359
2388
}
2360
2389
2361
2390
let raa = if self . monitor_pending_revoke_and_ack {
@@ -2368,11 +2397,12 @@ impl Channel {
2368
2397
self . monitor_pending_revoke_and_ack = false ;
2369
2398
self . monitor_pending_commitment_signed = false ;
2370
2399
let order = self . resend_order . clone ( ) ;
2371
- log_trace ! ( self , "Restored monitor updating resulting in {} commitment update and {} RAA, with {} first" ,
2400
+ log_trace ! ( self , "Restored monitor updating resulting in {}{} commitment update and {} RAA, with {} first" ,
2401
+ if needs_broadcast_safe { "a funding broadcast safe, " } else { "" } ,
2372
2402
if commitment_update. is_some( ) { "a" } else { "no" } ,
2373
2403
if raa. is_some( ) { "an" } else { "no" } ,
2374
2404
match order { RAACommitmentOrder :: CommitmentFirst => "commitment" , RAACommitmentOrder :: RevokeAndACKFirst => "RAA" } ) ;
2375
- ( raa, commitment_update, order, forwards, failures)
2405
+ ( raa, commitment_update, order, forwards, failures, needs_broadcast_safe , funding_locked )
2376
2406
}
2377
2407
2378
2408
pub fn update_fee ( & mut self , fee_estimator : & FeeEstimator , msg : & msgs:: UpdateFee ) -> Result < ( ) , ChannelError > {
@@ -2482,7 +2512,9 @@ impl Channel {
2482
2512
} else { None } ;
2483
2513
2484
2514
if self . channel_state & ( ChannelState :: FundingSent as u32 ) == ChannelState :: FundingSent as u32 {
2485
- 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 {
2486
2518
if msg. next_remote_commitment_number != 0 {
2487
2519
return Err ( ChannelError :: Close ( "Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet" ) ) ;
2488
2520
}
@@ -2972,12 +3004,17 @@ impl Channel {
2972
3004
//they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
2973
3005
//a protocol oversight, but I assume I'm just missing something.
2974
3006
if need_commitment_update {
2975
- let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
2976
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
2977
- return Ok ( Some ( msgs:: FundingLocked {
2978
- channel_id : self . channel_id ,
2979
- next_per_commitment_point : next_per_commitment_point,
2980
- } ) ) ;
3007
+ if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
3008
+ let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
3009
+ let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
3010
+ return Ok ( Some ( msgs:: FundingLocked {
3011
+ channel_id : self . channel_id ,
3012
+ next_per_commitment_point : next_per_commitment_point,
3013
+ } ) ) ;
3014
+ } else {
3015
+ self . monitor_pending_funding_locked = true ;
3016
+ return Ok ( None ) ;
3017
+ }
2981
3018
}
2982
3019
}
2983
3020
}
@@ -3696,6 +3733,7 @@ impl Writeable for Channel {
3696
3733
RAACommitmentOrder :: RevokeAndACKFirst => 1u8 . write ( writer) ?,
3697
3734
}
3698
3735
3736
+ self . monitor_pending_funding_locked . write ( writer) ?;
3699
3737
self . monitor_pending_revoke_and_ack . write ( writer) ?;
3700
3738
self . monitor_pending_commitment_signed . write ( writer) ?;
3701
3739
@@ -3863,6 +3901,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3863
3901
_ => return Err ( DecodeError :: InvalidValue ) ,
3864
3902
} ;
3865
3903
3904
+ let monitor_pending_funding_locked = Readable :: read ( reader) ?;
3866
3905
let monitor_pending_revoke_and_ack = Readable :: read ( reader) ?;
3867
3906
let monitor_pending_commitment_signed = Readable :: read ( reader) ?;
3868
3907
@@ -3959,6 +3998,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
3959
3998
3960
3999
resend_order,
3961
4000
4001
+ monitor_pending_funding_locked,
3962
4002
monitor_pending_revoke_and_ack,
3963
4003
monitor_pending_commitment_signed,
3964
4004
monitor_pending_forwards,
0 commit comments