@@ -361,13 +361,10 @@ pub(super) struct Channel<Signer: Sign> {
361
361
362
362
last_sent_closing_fee : Option < ( u32 , u64 , Signature ) > , // (feerate, fee, holder_sig)
363
363
364
- /// The hash of the block in which the funding transaction reached our CONF_TARGET. We use this
365
- /// to detect unconfirmation after a serialize-unserialize roundtrip where we may not see a full
366
- /// series of block_connected/block_disconnected calls. Obviously this is not a guarantee as we
367
- /// could miss the funding_tx_confirmed_in block as well, but it serves as a useful fallback.
364
+ /// The hash of the block in which the funding transaction was included.
368
365
funding_tx_confirmed_in : Option < BlockHash > ,
366
+ funding_tx_confirmation_height : u64 ,
369
367
short_channel_id : Option < u64 > ,
370
- funding_tx_confirmations : u64 ,
371
368
372
369
counterparty_dust_limit_satoshis : u64 ,
373
370
#[ cfg( test) ]
@@ -424,10 +421,6 @@ struct CommitmentTxInfoCached {
424
421
}
425
422
426
423
pub const OUR_MAX_HTLCS : u16 = 50 ; //TODO
427
- /// Confirmation count threshold at which we close a channel. Ideally we'd keep the channel around
428
- /// on ice until the funding transaction gets more confirmations, but the LN protocol doesn't
429
- /// really allow for this, so instead we're stuck closing it out at that point.
430
- const UNCONF_THRESHOLD : u32 = 6 ;
431
424
const SPENDING_INPUT_FOR_A_OUTPUT_WEIGHT : u64 = 79 ; // prevout: 36, nSequence: 4, script len: 1, witness lengths: (3+1)/4, sig: 73/4, if-selector: 1, redeemScript: (6 ops + 2*33 pubkeys + 1*2 delay)/4
432
425
const B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT : u64 = 104 ; // prevout: 40, nSequence: 4, script len: 1, witness lengths: 3/4, sig: 73/4, pubkey: 33/4, output: 31 (TODO: Wrong? Useless?)
433
426
@@ -565,7 +558,7 @@ impl<Signer: Sign> Channel<Signer> {
565
558
566
559
funding_tx_confirmed_in : None ,
567
560
short_channel_id : None ,
568
- funding_tx_confirmations : 0 ,
561
+ funding_tx_confirmation_height : 0 ,
569
562
570
563
feerate_per_kw : feerate,
571
564
counterparty_dust_limit_satoshis : 0 ,
@@ -800,7 +793,7 @@ impl<Signer: Sign> Channel<Signer> {
800
793
801
794
funding_tx_confirmed_in : None ,
802
795
short_channel_id : None ,
803
- funding_tx_confirmations : 0 ,
796
+ funding_tx_confirmation_height : 0 ,
804
797
805
798
feerate_per_kw : msg. feerate_per_kw ,
806
799
channel_value_satoshis : msg. funding_satoshis ,
@@ -3484,38 +3477,7 @@ impl<Signer: Sign> Channel<Signer> {
3484
3477
self . network_sync == UpdateStatus :: DisabledMarked
3485
3478
}
3486
3479
3487
- /// When we receive a new block, we (a) check whether the block contains the funding
3488
- /// transaction (which would start us counting blocks until we send the funding_signed), and
3489
- /// (b) check the height of the block against outbound holding cell HTLCs in case we need to
3490
- /// give up on them prematurely and time them out. Everything else (e.g. commitment
3491
- /// transaction broadcasts, channel closure detection, HTLC transaction broadcasting, etc) is
3492
- /// handled by the ChannelMonitor.
3493
- ///
3494
- /// If we return Err, the channel may have been closed, at which point the standard
3495
- /// requirements apply - no calls may be made except those explicitly stated to be allowed
3496
- /// post-shutdown.
3497
- /// Only returns an ErrorAction of DisconnectPeer, if Err.
3498
- ///
3499
- /// May return some HTLCs (and their payment_hash) which have timed out and should be failed
3500
- /// back.
3501
- pub fn block_connected ( & mut self , header : & BlockHeader , txdata : & TransactionData , height : u32 ) -> Result < ( Option < msgs:: FundingLocked > , Vec < ( HTLCSource , PaymentHash ) > ) , msgs:: ErrorMessage > {
3502
- let mut timed_out_htlcs = Vec :: new ( ) ;
3503
- self . holding_cell_htlc_updates . retain ( |htlc_update| {
3504
- match htlc_update {
3505
- & HTLCUpdateAwaitingACK :: AddHTLC { ref payment_hash, ref source, ref cltv_expiry, .. } => {
3506
- if * cltv_expiry <= height + HTLC_FAIL_BACK_BUFFER {
3507
- timed_out_htlcs. push ( ( source. clone ( ) , payment_hash. clone ( ) ) ) ;
3508
- false
3509
- } else { true }
3510
- } ,
3511
- _ => true
3512
- }
3513
- } ) ;
3514
-
3515
- if self . funding_tx_confirmations > 0 {
3516
- self . funding_tx_confirmations += 1 ;
3517
- }
3518
-
3480
+ pub fn transactions_confirmed ( & mut self , block_hash : & BlockHash , height : u32 , txdata : & TransactionData ) -> Result < ( ) , msgs:: ErrorMessage > {
3519
3481
let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
3520
3482
if non_shutdown_state & !( ChannelState :: TheirFundingLocked as u32 ) == ChannelState :: FundingSent as u32 {
3521
3483
for & ( index_in_block, tx) in txdata. iter ( ) {
@@ -3554,18 +3516,61 @@ impl<Signer: Sign> Channel<Signer> {
3554
3516
panic ! ( "Block was bogus - either height 16 million or had > 16 million transactions" ) ;
3555
3517
}
3556
3518
assert ! ( txo_idx <= 0xffff ) ; // txo_idx is a (u16 as usize), so this is just listed here for completeness
3557
- self . funding_tx_confirmations = 1 ;
3519
+ self . funding_tx_confirmation_height = height as u64 ;
3520
+ self . funding_tx_confirmed_in = Some ( * block_hash) ;
3558
3521
self . short_channel_id = Some ( ( ( height as u64 ) << ( 5 * 8 ) ) |
3559
3522
( ( index_in_block as u64 ) << ( 2 * 8 ) ) |
3560
3523
( ( txo_idx as u64 ) << ( 0 * 8 ) ) ) ;
3561
3524
}
3562
3525
}
3563
3526
}
3564
3527
}
3528
+ Ok ( ( ) )
3529
+ }
3530
+
3531
+ /// When a new block is connected, we check the height of the block against outbound holding
3532
+ /// cell HTLCs in case we need to give up on them prematurely and time them out. Everything
3533
+ /// else (e.g. commitment transaction broadcasts, channel closure detection, HTLC transaction
3534
+ /// broadcasting, etc) is handled by the ChannelMonitor.
3535
+ ///
3536
+ /// If we return Err, the channel may have been closed, at which point the standard
3537
+ /// requirements apply - no calls may be made except those explicitly stated to be allowed
3538
+ /// post-shutdown.
3539
+ ///
3540
+ /// May return some HTLCs (and their payment_hash) which have timed out and should be failed
3541
+ /// back.
3542
+ pub fn update_best_block ( & mut self , height : u32 , highest_header_time : u32 ) -> Result < ( Option < msgs:: FundingLocked > , Vec < ( HTLCSource , PaymentHash ) > ) , msgs:: ErrorMessage > {
3543
+ let mut timed_out_htlcs = Vec :: new ( ) ;
3544
+ self . holding_cell_htlc_updates . retain ( |htlc_update| {
3545
+ match htlc_update {
3546
+ & HTLCUpdateAwaitingACK :: AddHTLC { ref payment_hash, ref source, ref cltv_expiry, .. } => {
3547
+ if * cltv_expiry <= height + HTLC_FAIL_BACK_BUFFER {
3548
+ timed_out_htlcs. push ( ( source. clone ( ) , payment_hash. clone ( ) ) ) ;
3549
+ false
3550
+ } else { true }
3551
+ } ,
3552
+ _ => true
3553
+ }
3554
+ } ) ;
3555
+
3556
+ self . update_time_counter = cmp:: max ( self . update_time_counter , highest_header_time) ;
3557
+ if self . funding_tx_confirmation_height > 0 {
3558
+ let funding_tx_confirmations = height as i64 - self . funding_tx_confirmation_height as i64 + 1 ;
3559
+ if funding_tx_confirmations <= 0 {
3560
+ self . funding_tx_confirmation_height = 0 ;
3561
+ }
3565
3562
3566
- self . update_time_counter = cmp:: max ( self . update_time_counter , header. time ) ;
3567
- if self . funding_tx_confirmations > 0 {
3568
- if self . funding_tx_confirmations == self . minimum_depth as u64 {
3563
+ let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
3564
+ if ( non_shutdown_state >= ChannelState :: ChannelFunded as u32 ||
3565
+ ( non_shutdown_state & ChannelState :: OurFundingLocked as u32 ) == ChannelState :: OurFundingLocked as u32 ) &&
3566
+ funding_tx_confirmations < self . minimum_depth as i64 / 2 {
3567
+ return Err ( msgs:: ErrorMessage {
3568
+ channel_id : self . channel_id ( ) ,
3569
+ data : format ! ( "Funding transaction was un-confirmed. Locked at {} confs, now have {} confs." , self . minimum_depth, funding_tx_confirmations) ,
3570
+ } ) ;
3571
+ }
3572
+
3573
+ if funding_tx_confirmations == self . minimum_depth as i64 {
3569
3574
let need_commitment_update = if non_shutdown_state == ChannelState :: FundingSent as u32 {
3570
3575
self . channel_state |= ChannelState :: OurFundingLocked as u32 ;
3571
3576
true
@@ -3584,7 +3589,6 @@ impl<Signer: Sign> Channel<Signer> {
3584
3589
// funding_tx_confirmed_in and return.
3585
3590
false
3586
3591
} ;
3587
- self . funding_tx_confirmed_in = Some ( header. block_hash ( ) ) ;
3588
3592
3589
3593
//TODO: Note that this must be a duplicate of the previous commitment point they sent us,
3590
3594
//as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
@@ -3604,21 +3608,35 @@ impl<Signer: Sign> Channel<Signer> {
3604
3608
}
3605
3609
}
3606
3610
}
3611
+
3607
3612
Ok ( ( None , timed_out_htlcs) )
3608
3613
}
3609
3614
3615
+ /// When we receive a new block, we (a) check whether the block contains the funding
3616
+ /// transaction (which would start us counting blocks until we send the funding_signed), and
3617
+ /// (b) check the height of the block against outbound holding cell HTLCs in case we need to
3618
+ /// give up on them prematurely and time them out. Everything else (e.g. commitment
3619
+ /// transaction broadcasts, channel closure detection, HTLC transaction broadcasting, etc) is
3620
+ /// handled by the ChannelMonitor.
3621
+ ///
3622
+ /// If we return Err, the channel may have been closed, at which point the standard
3623
+ /// requirements apply - no calls may be made except those explicitly stated to be allowed
3624
+ /// post-shutdown.
3625
+ /// Only returns an ErrorAction of DisconnectPeer, if Err.
3626
+ ///
3627
+ /// May return some HTLCs (and their payment_hash) which have timed out and should be failed
3628
+ /// back.
3629
+ pub fn block_connected ( & mut self , header : & BlockHeader , txdata : & TransactionData , height : u32 ) -> Result < ( Option < msgs:: FundingLocked > , Vec < ( HTLCSource , PaymentHash ) > ) , msgs:: ErrorMessage > {
3630
+ self . transactions_confirmed ( & header. block_hash ( ) , height, txdata) ?;
3631
+ self . update_best_block ( height, header. time )
3632
+ }
3633
+
3610
3634
/// Called by channelmanager based on chain blocks being disconnected.
3611
3635
/// Returns true if we need to close the channel now due to funding transaction
3612
3636
/// unconfirmation/reorg.
3613
- pub fn block_disconnected ( & mut self , header : & BlockHeader ) -> bool {
3614
- if self . funding_tx_confirmations > 0 {
3615
- self . funding_tx_confirmations -= 1 ;
3616
- if self . funding_tx_confirmations == UNCONF_THRESHOLD as u64 {
3617
- return true ;
3618
- }
3619
- }
3620
- if Some ( header. block_hash ( ) ) == self . funding_tx_confirmed_in {
3621
- self . funding_tx_confirmations = self . minimum_depth as u64 - 1 ;
3637
+ pub fn block_disconnected ( & mut self , header : & BlockHeader , new_height : u32 ) -> bool {
3638
+ if self . update_best_block ( new_height, header. time ) . is_err ( ) {
3639
+ return true ;
3622
3640
}
3623
3641
false
3624
3642
}
@@ -4426,7 +4444,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
4426
4444
4427
4445
self . funding_tx_confirmed_in . write ( writer) ?;
4428
4446
self . short_channel_id . write ( writer) ?;
4429
- self . funding_tx_confirmations . write ( writer) ?;
4447
+ self . funding_tx_confirmation_height . write ( writer) ?;
4430
4448
4431
4449
self . counterparty_dust_limit_satoshis . write ( writer) ?;
4432
4450
self . holder_dust_limit_satoshis . write ( writer) ?;
@@ -4586,7 +4604,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
4586
4604
4587
4605
let funding_tx_confirmed_in = Readable :: read ( reader) ?;
4588
4606
let short_channel_id = Readable :: read ( reader) ?;
4589
- let funding_tx_confirmations = Readable :: read ( reader) ?;
4607
+ let funding_tx_confirmation_height = Readable :: read ( reader) ?;
4590
4608
4591
4609
let counterparty_dust_limit_satoshis = Readable :: read ( reader) ?;
4592
4610
let holder_dust_limit_satoshis = Readable :: read ( reader) ?;
@@ -4656,7 +4674,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
4656
4674
4657
4675
funding_tx_confirmed_in,
4658
4676
short_channel_id,
4659
- funding_tx_confirmations ,
4677
+ funding_tx_confirmation_height ,
4660
4678
4661
4679
counterparty_dust_limit_satoshis,
4662
4680
holder_dust_limit_satoshis,
0 commit comments