@@ -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 {
@@ -1976,20 +2000,33 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
1976
2000
// filters.
1977
2001
let prevout = & tx. input [ 0 ] . previous_output ;
1978
2002
if prevout. txid == self . funding_info . 0 . txid && prevout. vout == self . funding_info . 0 . index as u32 {
2003
+ let mut balance_spendable_csv = None ;
2004
+ log_info ! ( logger, "Channel closed by funding output spend in txid {}." , log_bytes!( tx. txid( ) ) ) ;
1979
2005
if ( tx. input [ 0 ] . sequence >> 8 * 3 ) as u8 == 0x80 && ( tx. lock_time >> 8 * 3 ) as u8 == 0x20 {
1980
2006
let ( mut new_outpoints, new_outputs) = self . check_spend_counterparty_transaction ( & tx, height, & logger) ;
1981
2007
if !new_outputs. 1 . is_empty ( ) {
1982
2008
watch_outputs. push ( new_outputs) ;
1983
2009
}
2010
+ claimable_outpoints. append ( & mut new_outpoints) ;
1984
2011
if new_outpoints. is_empty ( ) {
1985
- let ( mut new_outpoints, new_outputs) = self . check_spend_holder_transaction ( & tx, height, & logger) ;
1986
- if !new_outputs. 1 . is_empty ( ) {
1987
- watch_outputs. push ( new_outputs) ;
2012
+ if let Some ( ( mut new_outpoints, new_outputs) ) = self . check_spend_holder_transaction ( & tx, height, & logger) {
2013
+ if !new_outputs. 1 . is_empty ( ) {
2014
+ watch_outputs. push ( new_outputs) ;
2015
+ }
2016
+ claimable_outpoints. append ( & mut new_outpoints) ;
2017
+ balance_spendable_csv = Some ( self . on_holder_tx_csv ) ;
1988
2018
}
1989
- claimable_outpoints. append ( & mut new_outpoints) ;
1990
2019
}
1991
- claimable_outpoints. append ( & mut new_outpoints) ;
1992
2020
}
2021
+ let txid = tx. txid ( ) ;
2022
+ self . onchain_events_awaiting_threshold_conf . push ( OnchainEventEntry {
2023
+ txid,
2024
+ height : height,
2025
+ event : OnchainEvent :: FundingSpendConfirmation {
2026
+ txid,
2027
+ on_local_output_csv : balance_spendable_csv,
2028
+ } ,
2029
+ } ) ;
1993
2030
} else {
1994
2031
if let Some ( & commitment_number) = self . counterparty_commitment_txn_on_chain . get ( & prevout. txid ) {
1995
2032
let ( mut new_outpoints, new_outputs_option) = self . check_spend_counterparty_htlc ( & tx, commitment_number, height, & logger) ;
@@ -2077,6 +2114,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2077
2114
. filter_map ( |entry| match & entry. event {
2078
2115
OnchainEvent :: HTLCUpdate { source, .. } => Some ( source) ,
2079
2116
OnchainEvent :: MaturingOutput { .. } => None ,
2117
+ OnchainEvent :: FundingSpendConfirmation { .. } => None ,
2080
2118
} )
2081
2119
. collect ( ) ;
2082
2120
#[ cfg( debug_assertions) ]
@@ -2115,7 +2153,10 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2115
2153
self . pending_events . push ( Event :: SpendableOutputs {
2116
2154
outputs : vec ! [ descriptor]
2117
2155
} ) ;
2118
- }
2156
+ } ,
2157
+ OnchainEvent :: FundingSpendConfirmation { txid, .. } => {
2158
+ self . funding_spend_confirmed = Some ( txid) ;
2159
+ } ,
2119
2160
}
2120
2161
}
2121
2162
@@ -2790,7 +2831,10 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
2790
2831
return Err ( DecodeError :: InvalidValue ) ;
2791
2832
}
2792
2833
2793
- read_tlv_fields ! ( reader, { } ) ;
2834
+ let mut funding_spend_confirmed = None ;
2835
+ read_tlv_fields ! ( reader, {
2836
+ ( 1 , funding_spend_confirmed, option) ,
2837
+ } ) ;
2794
2838
2795
2839
let mut secp_ctx = Secp256k1 :: new ( ) ;
2796
2840
secp_ctx. seeded_randomize ( & keys_manager. get_secure_random_bytes ( ) ) ;
@@ -2839,6 +2883,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
2839
2883
2840
2884
lockdown_from_offchain,
2841
2885
holder_tx_signed,
2886
+ funding_spend_confirmed,
2842
2887
2843
2888
best_block,
2844
2889
0 commit comments