@@ -3501,11 +3501,59 @@ 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
+ let funding_tx_confirmations = height as i64 - self . funding_tx_confirmation_height as i64 + 1 ;
3507
+ if funding_tx_confirmations <= 0 {
3508
+ self . funding_tx_confirmation_height = 0 ;
3509
+ }
3510
+
3511
+ if funding_tx_confirmations >= self . minimum_depth as i64 {
3512
+ let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
3513
+ let need_commitment_update = if non_shutdown_state == ChannelState :: FundingSent as u32 {
3514
+ self . channel_state |= ChannelState :: OurFundingLocked as u32 ;
3515
+ true
3516
+ } else if non_shutdown_state == ( ChannelState :: FundingSent as u32 | ChannelState :: TheirFundingLocked as u32 ) {
3517
+ self . channel_state = ChannelState :: ChannelFunded as u32 | ( self . channel_state & MULTI_STATE_FLAGS ) ;
3518
+ self . update_time_counter += 1 ;
3519
+ true
3520
+ } else if non_shutdown_state == ( ChannelState :: FundingSent as u32 | ChannelState :: OurFundingLocked as u32 ) {
3521
+ // We got a reorg but not enough to trigger a force close, just update
3522
+ // funding_tx_confirmed_in and return.
3523
+ false
3524
+ } else if self . channel_state < ChannelState :: ChannelFunded as u32 {
3525
+ panic ! ( "Started confirming a channel in a state pre-FundingSent?: {}" , self . channel_state) ;
3526
+ } else {
3527
+ // We got a reorg but not enough to trigger a force close, just update
3528
+ // funding_tx_confirmed_in and return.
3529
+ false
3530
+ } ;
3531
+
3532
+ //TODO: Note that this must be a duplicate of the previous commitment point they sent us,
3533
+ //as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
3534
+ //they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
3535
+ //a protocol oversight, but I assume I'm just missing something.
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
+ }
3548
+ }
3549
+ None
3550
+ }
3551
+
3504
3552
/// When a transaction is confirmed, we check whether it is or spends the funding transaction
3505
3553
/// In the first case, we store the confirmation height and calculating the short channel id.
3506
3554
/// In the second, we simply return an Err indicating we need to be force-closed now.
3507
3555
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 {
3556
+ -> Result < Option < msgs :: FundingLocked > , msgs:: ErrorMessage > where L :: Target : Logger {
3509
3557
let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
3510
3558
for & ( index_in_block, tx) in txdata. iter ( ) {
3511
3559
if let Some ( funding_txo) = self . get_funding_txo ( ) {
@@ -3548,6 +3596,13 @@ impl<Signer: Sign> Channel<Signer> {
3548
3596
}
3549
3597
}
3550
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
+ // XXX: Test this case!
3603
+ if let Some ( funding_locked) = self . check_get_funding_locked ( height) {
3604
+ return Ok ( Some ( funding_locked) ) ;
3605
+ }
3551
3606
}
3552
3607
for inp in tx. input . iter ( ) {
3553
3608
if inp. previous_output == funding_txo. into_bitcoin_outpoint ( ) {
@@ -3560,7 +3615,7 @@ impl<Signer: Sign> Channel<Signer> {
3560
3615
}
3561
3616
}
3562
3617
}
3563
- Ok ( ( ) )
3618
+ Ok ( None )
3564
3619
}
3565
3620
3566
3621
/// When a new block is connected, we check the height of the block against outbound holding
@@ -3590,6 +3645,7 @@ impl<Signer: Sign> Channel<Signer> {
3590
3645
} ) ;
3591
3646
3592
3647
self . update_time_counter = cmp:: max ( self . update_time_counter , highest_header_time) ;
3648
+
3593
3649
if self . funding_tx_confirmation_height > 0 {
3594
3650
let funding_tx_confirmations = height as i64 - self . funding_tx_confirmation_height as i64 + 1 ;
3595
3651
if funding_tx_confirmations <= 0 {
@@ -3606,42 +3662,8 @@ impl<Signer: Sign> Channel<Signer> {
3606
3662
} ) ;
3607
3663
}
3608
3664
3609
- if funding_tx_confirmations == self . minimum_depth as i64 {
3610
- let need_commitment_update = if non_shutdown_state == ChannelState :: FundingSent as u32 {
3611
- self . channel_state |= ChannelState :: OurFundingLocked as u32 ;
3612
- true
3613
- } else if non_shutdown_state == ( ChannelState :: FundingSent as u32 | ChannelState :: TheirFundingLocked as u32 ) {
3614
- self . channel_state = ChannelState :: ChannelFunded as u32 | ( self . channel_state & MULTI_STATE_FLAGS ) ;
3615
- self . update_time_counter += 1 ;
3616
- true
3617
- } else if non_shutdown_state == ( ChannelState :: FundingSent as u32 | ChannelState :: OurFundingLocked as u32 ) {
3618
- // We got a reorg but not enough to trigger a force close, just update
3619
- // funding_tx_confirmed_in and return.
3620
- false
3621
- } else if self . channel_state < ChannelState :: ChannelFunded as u32 {
3622
- panic ! ( "Started confirming a channel in a state pre-FundingSent?: {}" , self . channel_state) ;
3623
- } else {
3624
- // We got a reorg but not enough to trigger a force close, just update
3625
- // funding_tx_confirmed_in and return.
3626
- false
3627
- } ;
3628
-
3629
- //TODO: Note that this must be a duplicate of the previous commitment point they sent us,
3630
- //as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
3631
- //they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
3632
- //a protocol oversight, but I assume I'm just missing something.
3633
- if need_commitment_update {
3634
- if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
3635
- let next_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
3636
- return Ok ( ( Some ( msgs:: FundingLocked {
3637
- channel_id : self . channel_id ,
3638
- next_per_commitment_point,
3639
- } ) , timed_out_htlcs) ) ;
3640
- } else {
3641
- self . monitor_pending_funding_locked = true ;
3642
- return Ok ( ( None , timed_out_htlcs) ) ;
3643
- }
3644
- }
3665
+ if let Some ( funding_locked) = self . check_get_funding_locked ( height) {
3666
+ return Ok ( ( Some ( funding_locked) , timed_out_htlcs) ) ;
3645
3667
}
3646
3668
}
3647
3669
0 commit comments