@@ -376,13 +376,10 @@ pub(super) struct Channel<Signer: Sign> {
376
376
377
377
last_sent_closing_fee : Option < ( u32 , u64 , Signature ) > , // (feerate, fee, holder_sig)
378
378
379
- /// The hash of the block in which the funding transaction reached our CONF_TARGET. We use this
380
- /// to detect unconfirmation after a serialize-unserialize roundtrip where we may not see a full
381
- /// series of block_connected/block_disconnected calls. Obviously this is not a guarantee as we
382
- /// could miss the funding_tx_confirmed_in block as well, but it serves as a useful fallback.
379
+ /// The hash of the block in which the funding transaction was included.
383
380
funding_tx_confirmed_in : Option < BlockHash > ,
381
+ funding_tx_confirmation_height : u64 ,
384
382
short_channel_id : Option < u64 > ,
385
- funding_tx_confirmations : u64 ,
386
383
387
384
counterparty_dust_limit_satoshis : u64 ,
388
385
#[ cfg( test) ]
@@ -441,10 +438,6 @@ struct CommitmentTxInfoCached {
441
438
}
442
439
443
440
pub const OUR_MAX_HTLCS : u16 = 50 ; //TODO
444
- /// Confirmation count threshold at which we close a channel. Ideally we'd keep the channel around
445
- /// on ice until the funding transaction gets more confirmations, but the LN protocol doesn't
446
- /// really allow for this, so instead we're stuck closing it out at that point.
447
- const UNCONF_THRESHOLD : u32 = 6 ;
448
441
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
449
442
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?)
450
443
@@ -581,8 +574,8 @@ impl<Signer: Sign> Channel<Signer> {
581
574
last_sent_closing_fee : None ,
582
575
583
576
funding_tx_confirmed_in : None ,
577
+ funding_tx_confirmation_height : 0 ,
584
578
short_channel_id : None ,
585
- funding_tx_confirmations : 0 ,
586
579
587
580
feerate_per_kw : feerate,
588
581
counterparty_dust_limit_satoshis : 0 ,
@@ -818,8 +811,8 @@ impl<Signer: Sign> Channel<Signer> {
818
811
last_sent_closing_fee : None ,
819
812
820
813
funding_tx_confirmed_in : None ,
814
+ funding_tx_confirmation_height : 0 ,
821
815
short_channel_id : None ,
822
- funding_tx_confirmations : 0 ,
823
816
824
817
feerate_per_kw : msg. feerate_per_kw ,
825
818
channel_value_satoshis : msg. funding_satoshis ,
@@ -3537,10 +3530,6 @@ impl<Signer: Sign> Channel<Signer> {
3537
3530
}
3538
3531
} ) ;
3539
3532
3540
- if self . funding_tx_confirmations > 0 {
3541
- self . funding_tx_confirmations += 1 ;
3542
- }
3543
-
3544
3533
let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
3545
3534
if non_shutdown_state & !( ChannelState :: TheirFundingLocked as u32 ) == ChannelState :: FundingSent as u32 {
3546
3535
for & ( index_in_block, tx) in txdata. iter ( ) {
@@ -3575,7 +3564,8 @@ impl<Signer: Sign> Channel<Signer> {
3575
3564
}
3576
3565
}
3577
3566
}
3578
- self . funding_tx_confirmations = 1 ;
3567
+ self . funding_tx_confirmation_height = height as u64 ;
3568
+ self . funding_tx_confirmed_in = Some ( header. block_hash ( ) ) ;
3579
3569
self . short_channel_id = match scid_from_parts ( height as u64 , index_in_block as u64 , txo_idx as u64 ) {
3580
3570
Ok ( scid) => Some ( scid) ,
3581
3571
Err ( _) => panic ! ( "Block was bogus - either height was > 16 million, had > 16 million transactions, or had > 65k outputs" ) ,
@@ -3585,9 +3575,11 @@ impl<Signer: Sign> Channel<Signer> {
3585
3575
}
3586
3576
}
3587
3577
3578
+
3588
3579
self . update_time_counter = cmp:: max ( self . update_time_counter , header. time ) ;
3589
- if self . funding_tx_confirmations > 0 {
3590
- if self . funding_tx_confirmations == self . minimum_depth as u64 {
3580
+ if self . funding_tx_confirmation_height > 0 {
3581
+ let funding_tx_confirmations = height as i64 - self . funding_tx_confirmation_height as i64 + 1 ;
3582
+ if funding_tx_confirmations == self . minimum_depth as i64 {
3591
3583
let need_commitment_update = if non_shutdown_state == ChannelState :: FundingSent as u32 {
3592
3584
self . channel_state |= ChannelState :: OurFundingLocked as u32 ;
3593
3585
true
@@ -3606,7 +3598,6 @@ impl<Signer: Sign> Channel<Signer> {
3606
3598
// funding_tx_confirmed_in and return.
3607
3599
false
3608
3600
} ;
3609
- self . funding_tx_confirmed_in = Some ( header. block_hash ( ) ) ;
3610
3601
3611
3602
//TODO: Note that this must be a duplicate of the previous commitment point they sent us,
3612
3603
//as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
@@ -3632,16 +3623,20 @@ impl<Signer: Sign> Channel<Signer> {
3632
3623
/// Called by channelmanager based on chain blocks being disconnected.
3633
3624
/// Returns true if we need to close the channel now due to funding transaction
3634
3625
/// unconfirmation/reorg.
3635
- pub fn block_disconnected ( & mut self , header : & BlockHeader ) -> bool {
3636
- if self . funding_tx_confirmations > 0 {
3637
- self . funding_tx_confirmations -= 1 ;
3638
- if self . funding_tx_confirmations == UNCONF_THRESHOLD as u64 {
3626
+ pub fn block_disconnected ( & mut self , header : & BlockHeader , height : u32 ) -> bool {
3627
+ if self . funding_tx_confirmation_height > 0 {
3628
+ let funding_tx_confirmations = height as i64 - self . funding_tx_confirmation_height as i64 + 1 ;
3629
+ if funding_tx_confirmations <= 0 {
3630
+ self . funding_tx_confirmation_height = 0 ;
3631
+ }
3632
+
3633
+ let non_shutdown_state = self . channel_state & ( !MULTI_STATE_FLAGS ) ;
3634
+ if ( non_shutdown_state >= ChannelState :: ChannelFunded as u32 ||
3635
+ ( non_shutdown_state & ChannelState :: OurFundingLocked as u32 ) == ChannelState :: OurFundingLocked as u32 ) &&
3636
+ funding_tx_confirmations < self . minimum_depth as i64 / 2 {
3639
3637
return true ;
3640
3638
}
3641
3639
}
3642
- if Some ( header. block_hash ( ) ) == self . funding_tx_confirmed_in {
3643
- self . funding_tx_confirmations = self . minimum_depth as u64 - 1 ;
3644
- }
3645
3640
false
3646
3641
}
3647
3642
@@ -4466,8 +4461,8 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
4466
4461
}
4467
4462
4468
4463
self . funding_tx_confirmed_in . write ( writer) ?;
4464
+ self . funding_tx_confirmation_height . write ( writer) ?;
4469
4465
self . short_channel_id . write ( writer) ?;
4470
- self . funding_tx_confirmations . write ( writer) ?;
4471
4466
4472
4467
self . counterparty_dust_limit_satoshis . write ( writer) ?;
4473
4468
self . holder_dust_limit_satoshis . write ( writer) ?;
@@ -4636,8 +4631,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
4636
4631
} ;
4637
4632
4638
4633
let funding_tx_confirmed_in = Readable :: read ( reader) ?;
4634
+ let funding_tx_confirmation_height = Readable :: read ( reader) ?;
4639
4635
let short_channel_id = Readable :: read ( reader) ?;
4640
- let funding_tx_confirmations = Readable :: read ( reader) ?;
4641
4636
4642
4637
let counterparty_dust_limit_satoshis = Readable :: read ( reader) ?;
4643
4638
let holder_dust_limit_satoshis = Readable :: read ( reader) ?;
@@ -4716,8 +4711,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
4716
4711
last_sent_closing_fee,
4717
4712
4718
4713
funding_tx_confirmed_in,
4714
+ funding_tx_confirmation_height,
4719
4715
short_channel_id,
4720
- funding_tx_confirmations,
4721
4716
4722
4717
counterparty_dust_limit_satoshis,
4723
4718
holder_dust_limit_satoshis,
0 commit comments