@@ -3501,11 +3501,57 @@ impl<Signer: Sign> Channel<Signer> {
3501
3501
self . network_sync == UpdateStatus :: DisabledMarked
3502
3502
}
3503
3503
3504
+ fn check_get_funding_locked ( & mut self , height : u32 ) -> Option < msgs:: FundingLocked > {
3505
+ if self . funding_tx_confirmation_height == 0 {
3506
+ return None ;
3507
+ }
3508
+
3509
+ let funding_tx_confirmations = height as i64 - self . funding_tx_confirmation_height as i64 + 1 ;
3510
+ if funding_tx_confirmations <= 0 {
3511
+ self . funding_tx_confirmation_height = 0 ;
3512
+ }
3513
+
3514
+ if funding_tx_confirmations < self . minimum_depth as i64 {
3515
+ return None ;
3516
+ }
3517
+
3518
+ let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
3519
+ let need_commitment_update = if non_shutdown_state == ChannelState :: FundingSent as u32 {
3520
+ self . channel_state |= ChannelState :: OurFundingLocked as u32 ;
3521
+ true
3522
+ } else if non_shutdown_state == ( ChannelState :: FundingSent as u32 | ChannelState :: TheirFundingLocked as u32 ) {
3523
+ self . channel_state = ChannelState :: ChannelFunded as u32 | ( self . channel_state & MULTI_STATE_FLAGS ) ;
3524
+ self . update_time_counter += 1 ;
3525
+ true
3526
+ } else if non_shutdown_state == ( ChannelState :: FundingSent as u32 | ChannelState :: OurFundingLocked as u32 ) {
3527
+ // We got a reorg but not enough to trigger a force close, just ignore.
3528
+ false
3529
+ } else if self . channel_state < ChannelState :: ChannelFunded as u32 {
3530
+ panic ! ( "Started confirming a channel in a state pre-FundingSent?: {}" , self . channel_state) ;
3531
+ } else {
3532
+ // We got a reorg but not enough to trigger a force close, just ignore.
3533
+ false
3534
+ } ;
3535
+
3536
+ if need_commitment_update {
3537
+ if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
3538
+ let next_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
3539
+ return Some ( msgs:: FundingLocked {
3540
+ channel_id : self . channel_id ,
3541
+ next_per_commitment_point,
3542
+ } ) ;
3543
+ } else {
3544
+ self . monitor_pending_funding_locked = true ;
3545
+ }
3546
+ }
3547
+ None
3548
+ }
3549
+
3504
3550
/// When a transaction is confirmed, we check whether it is or spends the funding transaction
3505
3551
/// In the first case, we store the confirmation height and calculating the short channel id.
3506
3552
/// In the second, we simply return an Err indicating we need to be force-closed now.
3507
3553
pub fn transactions_confirmed < L : Deref > ( & mut self , block_hash : & BlockHash , height : u32 , txdata : & TransactionData , logger : & L )
3508
- -> Result < ( ) , msgs:: ErrorMessage > where L :: Target : Logger {
3554
+ -> Result < Option < msgs :: FundingLocked > , msgs:: ErrorMessage > where L :: Target : Logger {
3509
3555
let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
3510
3556
for & ( index_in_block, tx) in txdata. iter ( ) {
3511
3557
if let Some ( funding_txo) = self . get_funding_txo ( ) {
@@ -3550,6 +3596,12 @@ impl<Signer: Sign> Channel<Signer> {
3550
3596
}
3551
3597
}
3552
3598
}
3599
+ // If we allow 1-conf funding, we may need to check for funding_locked here and
3600
+ // send it immediately instead of waiting for an update_best_block call (which
3601
+ // may have already happened for this block).
3602
+ if let Some ( funding_locked) = self . check_get_funding_locked ( height) {
3603
+ return Ok ( Some ( funding_locked) ) ;
3604
+ }
3553
3605
}
3554
3606
for inp in tx. input . iter ( ) {
3555
3607
if inp. previous_output == funding_txo. into_bitcoin_outpoint ( ) {
@@ -3562,7 +3614,7 @@ impl<Signer: Sign> Channel<Signer> {
3562
3614
}
3563
3615
}
3564
3616
}
3565
- Ok ( ( ) )
3617
+ Ok ( None )
3566
3618
}
3567
3619
3568
3620
/// When a new block is connected, we check the height of the block against outbound holding
@@ -3592,59 +3644,32 @@ impl<Signer: Sign> Channel<Signer> {
3592
3644
} ) ;
3593
3645
3594
3646
self . update_time_counter = cmp:: max ( self . update_time_counter , highest_header_time) ;
3595
- if self . funding_tx_confirmation_height > 0 {
3596
- let funding_tx_confirmations = height as i64 - self . funding_tx_confirmation_height as i64 + 1 ;
3597
- if funding_tx_confirmations <= 0 {
3598
- self . funding_tx_confirmation_height = 0 ;
3647
+
3648
+ if let Some ( funding_locked) = self . check_get_funding_locked ( height) {
3649
+ return Ok ( ( Some ( funding_locked) , timed_out_htlcs) ) ;
3650
+ }
3651
+
3652
+ let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
3653
+ if non_shutdown_state >= ChannelState :: ChannelFunded as u32 ||
3654
+ ( non_shutdown_state & ChannelState :: OurFundingLocked as u32 ) == ChannelState :: OurFundingLocked as u32 {
3655
+ let mut funding_tx_confirmations = height as i64 - self . funding_tx_confirmation_height as i64 + 1 ;
3656
+ if self . funding_tx_confirmation_height == 0 {
3657
+ // Note that check_get_funding_locked may reset funding_tx_confirmation_height to
3658
+ // zero if it has been reorged out, however in either case, our state flags
3659
+ // indicate we've already sent a funding_locked
3660
+ funding_tx_confirmations = 0 ;
3599
3661
}
3600
3662
3601
- let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
3602
- if ( non_shutdown_state >= ChannelState :: ChannelFunded as u32 ||
3603
- ( non_shutdown_state & ChannelState :: OurFundingLocked as u32 ) == ChannelState :: OurFundingLocked as u32 ) &&
3604
- funding_tx_confirmations < self . minimum_depth as i64 / 2 {
3663
+ // If we've sent funding_locked (or have both sent and received funding_locked), and
3664
+ // the funding transaction's confirmation count has dipped below minimum_depth / 2,
3665
+ // close the channel and hope we can get the latest state on chain (because presumably
3666
+ // the funding transaction is at least still in the mempool of most nodes).
3667
+ if funding_tx_confirmations < self . minimum_depth as i64 / 2 {
3605
3668
return Err ( msgs:: ErrorMessage {
3606
3669
channel_id : self . channel_id ( ) ,
3607
3670
data : format ! ( "Funding transaction was un-confirmed. Locked at {} confs, now have {} confs." , self . minimum_depth, funding_tx_confirmations) ,
3608
3671
} ) ;
3609
3672
}
3610
-
3611
- if funding_tx_confirmations == self . minimum_depth as i64 {
3612
- let need_commitment_update = if non_shutdown_state == ChannelState :: FundingSent as u32 {
3613
- self . channel_state |= ChannelState :: OurFundingLocked as u32 ;
3614
- true
3615
- } else if non_shutdown_state == ( ChannelState :: FundingSent as u32 | ChannelState :: TheirFundingLocked as u32 ) {
3616
- self . channel_state = ChannelState :: ChannelFunded as u32 | ( self . channel_state & MULTI_STATE_FLAGS ) ;
3617
- self . update_time_counter += 1 ;
3618
- true
3619
- } else if non_shutdown_state == ( ChannelState :: FundingSent as u32 | ChannelState :: OurFundingLocked as u32 ) {
3620
- // We got a reorg but not enough to trigger a force close, just update
3621
- // funding_tx_confirmed_in and return.
3622
- false
3623
- } else if self . channel_state < ChannelState :: ChannelFunded as u32 {
3624
- panic ! ( "Started confirming a channel in a state pre-FundingSent?: {}" , self . channel_state) ;
3625
- } else {
3626
- // We got a reorg but not enough to trigger a force close, just update
3627
- // funding_tx_confirmed_in and return.
3628
- false
3629
- } ;
3630
-
3631
- //TODO: Note that this must be a duplicate of the previous commitment point they sent us,
3632
- //as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
3633
- //they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
3634
- //a protocol oversight, but I assume I'm just missing something.
3635
- if need_commitment_update {
3636
- if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
3637
- let next_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
3638
- return Ok ( ( Some ( msgs:: FundingLocked {
3639
- channel_id : self . channel_id ,
3640
- next_per_commitment_point,
3641
- } ) , timed_out_htlcs) ) ;
3642
- } else {
3643
- self . monitor_pending_funding_locked = true ;
3644
- return Ok ( ( None , timed_out_htlcs) ) ;
3645
- }
3646
- }
3647
- }
3648
3673
}
3649
3674
3650
3675
Ok ( ( None , timed_out_htlcs) )
0 commit comments