@@ -356,12 +356,20 @@ struct OnchainEventEntry {
356
356
impl OnchainEventEntry {
357
357
fn confirmation_threshold ( & self ) -> u32 {
358
358
let mut conf_threshold = self . height + ANTI_REORG_DELAY - 1 ;
359
- if let OnchainEvent :: MaturingOutput {
360
- descriptor : SpendableOutputDescriptor :: DelayedPaymentOutput ( ref descriptor)
361
- } = self . event {
362
- // A CSV'd transaction is confirmable in block (input height) + CSV delay, which means
363
- // it's broadcastable when we see the previous block.
364
- conf_threshold = cmp:: max ( conf_threshold, self . height + descriptor. to_self_delay as u32 - 1 ) ;
359
+ match self . event {
360
+ OnchainEvent :: MaturingOutput {
361
+ descriptor : SpendableOutputDescriptor :: DelayedPaymentOutput ( ref descriptor)
362
+ } => {
363
+ // A CSV'd transaction is confirmable in block (input height) + CSV delay, which means
364
+ // it's broadcastable when we see the previous block.
365
+ conf_threshold = cmp:: max ( conf_threshold, self . height + descriptor. to_self_delay as u32 - 1 ) ;
366
+ } ,
367
+ OnchainEvent :: FundingSpendConfirmation { on_local_output_csv : Some ( csv) , .. } => {
368
+ // A CSV'd transaction is confirmable in block (input height) + CSV delay, which means
369
+ // it's broadcastable when we see the previous block.
370
+ conf_threshold = cmp:: max ( conf_threshold, self . height + csv as u32 - 1 ) ;
371
+ } ,
372
+ _ => { } ,
365
373
}
366
374
conf_threshold
367
375
}
@@ -386,6 +394,10 @@ enum OnchainEvent {
386
394
MaturingOutput {
387
395
descriptor : SpendableOutputDescriptor ,
388
396
} ,
397
+ FundingSpendConfirmation {
398
+ txid : Txid ,
399
+ on_local_output_csv : Option < u16 > ,
400
+ } ,
389
401
}
390
402
391
403
impl Writeable for OnchainEventEntry {
@@ -426,6 +438,10 @@ impl_writeable_tlv_based_enum_upgradable!(OnchainEvent,
426
438
( 1 , MaturingOutput ) => {
427
439
( 0 , descriptor, required) ,
428
440
} ,
441
+ ( 3 , FundingSpendConfirmation ) => {
442
+ ( 0 , txid, required) ,
443
+ ( 2 , on_local_output_csv, option) ,
444
+ } ,
429
445
) ;
430
446
431
447
#[ cfg_attr( any( test, feature = "fuzztarget" , feature = "_test_utils" ) , derive( PartialEq ) ) ]
@@ -598,6 +614,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: Sign> {
598
614
// remote monitor out-of-order with regards to the block view.
599
615
holder_tx_signed : bool ,
600
616
617
+ funding_spend_confirmed : Option < Txid > ,
618
+
601
619
// We simply modify best_block in Channel's block_connected so that serialization is
602
620
// consistent but hopefully the users' copy handles block_connected in a consistent way.
603
621
// (we do *not*, however, update them in update_monitor to ensure any local user copies keep
@@ -656,7 +674,8 @@ impl<Signer: Sign> PartialEq for ChannelMonitorImpl<Signer> {
656
674
self . onchain_events_awaiting_threshold_conf != other. onchain_events_awaiting_threshold_conf ||
657
675
self . outputs_to_watch != other. outputs_to_watch ||
658
676
self . lockdown_from_offchain != other. lockdown_from_offchain ||
659
- self . holder_tx_signed != other. holder_tx_signed
677
+ self . holder_tx_signed != other. holder_tx_signed ||
678
+ self . funding_spend_confirmed != other. funding_spend_confirmed
660
679
{
661
680
false
662
681
} else {
@@ -821,7 +840,9 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
821
840
self . lockdown_from_offchain . write ( writer) ?;
822
841
self . holder_tx_signed . write ( writer) ?;
823
842
824
- write_tlv_fields ! ( writer, { } ) ;
843
+ write_tlv_fields ! ( writer, {
844
+ ( 1 , self . funding_spend_confirmed, option) ,
845
+ } ) ;
825
846
826
847
Ok ( ( ) )
827
848
}
@@ -919,6 +940,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
919
940
920
941
lockdown_from_offchain : false ,
921
942
holder_tx_signed : false ,
943
+ funding_spend_confirmed : None ,
922
944
923
945
best_block,
924
946
@@ -1804,7 +1826,8 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1804
1826
/// Attempts to claim any claimable HTLCs in a commitment transaction which was not (yet)
1805
1827
/// revoked using data in holder_claimable_outpoints.
1806
1828
/// Should not be used if check_spend_revoked_transaction succeeds.
1807
- fn check_spend_holder_transaction < L : Deref > ( & mut self , tx : & Transaction , height : u32 , logger : & L ) -> ( Vec < PackageTemplate > , TransactionOutputs ) where L :: Target : Logger {
1829
+ /// Returns None unless the transaction is definitely one of our commitment transactions.
1830
+ fn check_spend_holder_transaction < L : Deref > ( & mut self , tx : & Transaction , height : u32 , logger : & L ) -> Option < ( Vec < PackageTemplate > , TransactionOutputs ) > where L :: Target : Logger {
1808
1831
let commitment_txid = tx. txid ( ) ;
1809
1832
let mut claim_requests = Vec :: new ( ) ;
1810
1833
let mut watch_outputs = Vec :: new ( ) ;
@@ -1839,9 +1862,10 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1839
1862
}
1840
1863
1841
1864
if is_holder_tx {
1865
+ Some ( ( claim_requests, ( commitment_txid, watch_outputs) ) )
1866
+ } else {
1867
+ None
1842
1868
}
1843
-
1844
- ( claim_requests, ( commitment_txid, watch_outputs) )
1845
1869
}
1846
1870
1847
1871
pub fn get_latest_holder_commitment_txn < L : Deref > ( & mut self , logger : & L ) -> Vec < Transaction > where L :: Target : Logger {
@@ -1973,20 +1997,33 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1973
1997
// filters.
1974
1998
let prevout = & tx. input [ 0 ] . previous_output ;
1975
1999
if prevout. txid == self . funding_info . 0 . txid && prevout. vout == self . funding_info . 0 . index as u32 {
2000
+ let mut balance_spendable_csv = None ;
2001
+ log_info ! ( logger, "Channel closed by funding output spend in txid {}." , log_bytes!( tx. txid( ) ) ) ;
1976
2002
if ( tx. input [ 0 ] . sequence >> 8 * 3 ) as u8 == 0x80 && ( tx. lock_time >> 8 * 3 ) as u8 == 0x20 {
1977
2003
let ( mut new_outpoints, new_outputs) = self . check_spend_counterparty_transaction ( & tx, height, & logger) ;
1978
2004
if !new_outputs. 1 . is_empty ( ) {
1979
2005
watch_outputs. push ( new_outputs) ;
1980
2006
}
2007
+ claimable_outpoints. append ( & mut new_outpoints) ;
1981
2008
if new_outpoints. is_empty ( ) {
1982
- let ( mut new_outpoints, new_outputs) = self . check_spend_holder_transaction ( & tx, height, & logger) ;
1983
- if !new_outputs. 1 . is_empty ( ) {
1984
- watch_outputs. push ( new_outputs) ;
2009
+ if let Some ( ( mut new_outpoints, new_outputs) ) = self . check_spend_holder_transaction ( & tx, height, & logger) {
2010
+ if !new_outputs. 1 . is_empty ( ) {
2011
+ watch_outputs. push ( new_outputs) ;
2012
+ }
2013
+ claimable_outpoints. append ( & mut new_outpoints) ;
2014
+ balance_spendable_csv = Some ( self . on_holder_tx_csv ) ;
1985
2015
}
1986
- claimable_outpoints. append ( & mut new_outpoints) ;
1987
2016
}
1988
- claimable_outpoints. append ( & mut new_outpoints) ;
1989
2017
}
2018
+ let txid = tx. txid ( ) ;
2019
+ self . onchain_events_awaiting_threshold_conf . push ( OnchainEventEntry {
2020
+ txid,
2021
+ height : height,
2022
+ event : OnchainEvent :: FundingSpendConfirmation {
2023
+ txid,
2024
+ on_local_output_csv : balance_spendable_csv,
2025
+ } ,
2026
+ } ) ;
1990
2027
} else {
1991
2028
if let Some ( & commitment_number) = self . counterparty_commitment_txn_on_chain . get ( & prevout. txid ) {
1992
2029
let ( mut new_outpoints, new_outputs_option) = self . check_spend_counterparty_htlc ( & tx, commitment_number, height, & logger) ;
@@ -2075,6 +2112,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2075
2112
. filter_map ( |entry| match & entry. event {
2076
2113
OnchainEvent :: HTLCUpdate { source, .. } => Some ( source) ,
2077
2114
OnchainEvent :: MaturingOutput { .. } => None ,
2115
+ OnchainEvent :: FundingSpendConfirmation { .. } => None ,
2078
2116
} )
2079
2117
. collect ( ) ;
2080
2118
#[ cfg( debug_assertions) ]
@@ -2113,7 +2151,10 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2113
2151
self . pending_events . push ( Event :: SpendableOutputs {
2114
2152
outputs : vec ! [ descriptor]
2115
2153
} ) ;
2116
- }
2154
+ } ,
2155
+ OnchainEvent :: FundingSpendConfirmation { txid, .. } => {
2156
+ self . funding_spend_confirmed = Some ( txid) ;
2157
+ } ,
2117
2158
}
2118
2159
}
2119
2160
@@ -2788,7 +2829,10 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
2788
2829
return Err ( DecodeError :: InvalidValue ) ;
2789
2830
}
2790
2831
2791
- read_tlv_fields ! ( reader, { } ) ;
2832
+ let mut funding_spend_confirmed = None ;
2833
+ read_tlv_fields ! ( reader, {
2834
+ ( 1 , funding_spend_confirmed, option) ,
2835
+ } ) ;
2792
2836
2793
2837
let mut secp_ctx = Secp256k1 :: new ( ) ;
2794
2838
secp_ctx. seeded_randomize ( & keys_manager. get_secure_random_bytes ( ) ) ;
@@ -2837,6 +2881,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
2837
2881
2838
2882
lockdown_from_offchain,
2839
2883
holder_tx_signed,
2884
+ funding_spend_confirmed,
2840
2885
2841
2886
best_block,
2842
2887
0 commit comments