@@ -649,11 +649,9 @@ pub(crate) enum ChannelMonitorUpdateStep {
649649 to_broadcaster_value_sat : Option < u64 > ,
650650 to_countersignatory_value_sat : Option < u64 > ,
651651 } ,
652- LatestCounterpartyCommitmentTX {
653- // The dust and non-dust htlcs for that commitment
654- htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
655- // Contains only the non-dust htlcs
656- commitment_tx : CommitmentTransaction ,
652+ LatestCounterpartyCommitment {
653+ commitment_txs : Vec < CommitmentTransaction > ,
654+ htlc_data : CommitmentHTLCData ,
657655 } ,
658656 PaymentPreimage {
659657 payment_preimage : PaymentPreimage ,
@@ -689,7 +687,7 @@ impl ChannelMonitorUpdateStep {
689687 ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { .. } => "LatestHolderCommitmentTXInfo" ,
690688 ChannelMonitorUpdateStep :: LatestHolderCommitment { .. } => "LatestHolderCommitment" ,
691689 ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. } => "LatestCounterpartyCommitmentTXInfo" ,
692- ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { .. } => "LatestCounterpartyCommitmentTX " ,
690+ ChannelMonitorUpdateStep :: LatestCounterpartyCommitment { .. } => "LatestCounterpartyCommitment " ,
693691 ChannelMonitorUpdateStep :: PaymentPreimage { .. } => "PaymentPreimage" ,
694692 ChannelMonitorUpdateStep :: CommitmentSecret { .. } => "CommitmentSecret" ,
695693 ChannelMonitorUpdateStep :: ChannelForceClosed { .. } => "ChannelForceClosed" ,
@@ -729,9 +727,9 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
729727 ( 5 , ShutdownScript ) => {
730728 ( 0 , scriptpubkey, required) ,
731729 } ,
732- ( 6 , LatestCounterpartyCommitmentTX ) => {
733- ( 0 , htlc_outputs , required_vec) ,
734- ( 2 , commitment_tx , required) ,
730+ ( 6 , LatestCounterpartyCommitment ) => {
731+ ( 1 , commitment_txs , required_vec) ,
732+ ( 3 , htlc_data , required) ,
735733 } ,
736734 ( 8 , LatestHolderCommitment ) => {
737735 ( 1 , commitment_txs, required_vec) ,
@@ -1847,34 +1845,29 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
18471845 ///
18481846 /// This is used to provide the counterparty commitment transaction directly to the monitor
18491847 /// before the initial persistence of a new channel.
1850- pub ( crate ) fn provide_initial_counterparty_commitment_tx < L : Deref > (
1851- & self , commitment_tx : CommitmentTransaction , logger : & L ,
1852- ) where
1853- L :: Target : Logger ,
1854- {
1848+ pub ( crate ) fn provide_initial_counterparty_commitment_tx (
1849+ & self , commitment_tx : CommitmentTransaction ,
1850+ ) {
18551851 let mut inner = self . inner . lock ( ) . unwrap ( ) ;
1856- let logger = WithChannelMonitor :: from_impl ( logger, & * inner, None ) ;
1857- inner. provide_initial_counterparty_commitment_tx ( commitment_tx, & logger) ;
1852+ inner. provide_initial_counterparty_commitment_tx ( commitment_tx) ;
18581853 }
18591854
18601855 /// Informs this monitor of the latest counterparty (ie non-broadcastable) commitment transaction.
18611856 /// The monitor watches for it to be broadcasted and then uses the HTLC information (and
18621857 /// possibly future revocation/preimage information) to claim outputs where possible.
18631858 /// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
18641859 #[ cfg( test) ]
1865- #[ rustfmt:: skip]
1866- fn provide_latest_counterparty_commitment_tx < L : Deref > (
1867- & self ,
1868- txid : Txid ,
1869- htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
1870- commitment_number : u64 ,
1871- their_per_commitment_point : PublicKey ,
1872- logger : & L ,
1873- ) where L :: Target : Logger {
1860+ fn provide_latest_counterparty_commitment_tx (
1861+ & self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
1862+ commitment_number : u64 , their_per_commitment_point : PublicKey ,
1863+ ) {
18741864 let mut inner = self . inner . lock ( ) . unwrap ( ) ;
1875- let logger = WithChannelMonitor :: from_impl ( logger, & * inner, None ) ;
18761865 inner. provide_latest_counterparty_commitment_tx (
1877- txid, htlc_outputs, commitment_number, their_per_commitment_point, & logger)
1866+ txid,
1867+ htlc_outputs,
1868+ commitment_number,
1869+ their_per_commitment_point,
1870+ )
18781871 }
18791872
18801873 #[ cfg( test) ]
@@ -3261,9 +3254,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32613254 }
32623255
32633256 #[ rustfmt:: skip]
3264- fn provide_initial_counterparty_commitment_tx < L : Deref > (
3265- & mut self , commitment_tx : CommitmentTransaction , logger : & WithChannelMonitor < L > ,
3266- ) where L :: Target : Logger {
3257+ fn provide_initial_counterparty_commitment_tx (
3258+ & mut self , commitment_tx : CommitmentTransaction ,
3259+ ) {
32673260 // We populate this field for downgrades
32683261 self . initial_counterparty_commitment_info = Some ( ( commitment_tx. per_commitment_point ( ) ,
32693262 commitment_tx. feerate_per_kw ( ) , commitment_tx. to_broadcaster_value_sat ( ) , commitment_tx. to_countersignatory_value_sat ( ) ) ) ;
@@ -3274,17 +3267,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32743267 }
32753268
32763269 self . provide_latest_counterparty_commitment_tx ( commitment_tx. trust ( ) . txid ( ) , Vec :: new ( ) , commitment_tx. commitment_number ( ) ,
3277- commitment_tx. per_commitment_point ( ) , logger ) ;
3270+ commitment_tx. per_commitment_point ( ) ) ;
32783271 // Soon, we will only populate this field
32793272 self . initial_counterparty_commitment_tx = Some ( commitment_tx) ;
32803273 }
32813274
32823275 #[ rustfmt:: skip]
3283- fn provide_latest_counterparty_commitment_tx < L : Deref > (
3284- & mut self , txid : Txid ,
3285- htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
3286- commitment_number : u64 , their_per_commitment_point : PublicKey , logger : & WithChannelMonitor < L > ,
3287- ) where L :: Target : Logger {
3276+ fn provide_latest_counterparty_commitment_tx (
3277+ & mut self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
3278+ commitment_number : u64 , their_per_commitment_point : PublicKey ,
3279+ ) {
32883280 // TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
32893281 // so that a remote monitor doesn't learn anything unless there is a malicious close.
32903282 // (only maybe, sadly we cant do the same for local info, as we need to be aware of
@@ -3293,11 +3285,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32933285 self . counterparty_hash_commitment_number . insert ( htlc. payment_hash , commitment_number) ;
32943286 }
32953287
3296- log_trace ! ( logger, "Tracking new counterparty commitment transaction with txid {} at commitment number {} with {} HTLC outputs" , txid, commitment_number, htlc_outputs. len( ) ) ;
32973288 self . funding . prev_counterparty_commitment_txid = self . funding . current_counterparty_commitment_txid . take ( ) ;
32983289 self . funding . current_counterparty_commitment_txid = Some ( txid) ;
3299- self . funding . counterparty_claimable_outpoints . insert ( txid, htlc_outputs. clone ( ) ) ;
3290+ self . funding . counterparty_claimable_outpoints . insert ( txid, htlc_outputs) ;
33003291 self . current_counterparty_commitment_number = commitment_number;
3292+
33013293 //TODO: Merge this into the other per-counterparty-transaction output storage stuff
33023294 match self . their_cur_per_commitment_points {
33033295 Some ( old_points) => {
@@ -3319,6 +3311,57 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33193311 }
33203312 }
33213313
3314+ fn update_counterparty_commitment_data (
3315+ & mut self , commitment_txs : & [ CommitmentTransaction ] , htlc_data : & CommitmentHTLCData ,
3316+ ) -> Result < ( ) , & ' static str > {
3317+ self . verify_matching_commitment_transactions ( commitment_txs. iter ( ) ) ?;
3318+
3319+ let htlcs_for_commitment = |commitment : & CommitmentTransaction | {
3320+ debug_assert ! ( htlc_data. nondust_htlc_sources. len( ) <= commitment. nondust_htlcs( ) . len( ) ) ;
3321+ let mut nondust_htlcs = commitment. nondust_htlcs ( ) . iter ( ) ;
3322+ let mut sources = htlc_data. nondust_htlc_sources . iter ( ) ;
3323+ let nondust_htlcs = core:: iter:: from_fn ( move || {
3324+ let htlc = nondust_htlcs. next ( ) ?. clone ( ) ;
3325+ let source = ( !htlc. offered ) . then ( || {
3326+ let source = sources
3327+ . next ( )
3328+ . expect ( "Every inbound non-dust HTLC should have a corresponding source" )
3329+ . clone ( ) ;
3330+ Box :: new ( source)
3331+ } ) ;
3332+ Some ( ( htlc, source) )
3333+ } ) ;
3334+
3335+ let dust_htlcs = htlc_data. dust_htlcs . iter ( ) . map ( |( htlc, source) | {
3336+ ( htlc. clone ( ) , source. as_ref ( ) . map ( |source| Box :: new ( source. clone ( ) ) ) )
3337+ } ) ;
3338+
3339+ nondust_htlcs. chain ( dust_htlcs) . collect :: < Vec < _ > > ( )
3340+ } ;
3341+
3342+ let current_funding_commitment_tx = commitment_txs. first ( ) . unwrap ( ) ;
3343+ self . provide_latest_counterparty_commitment_tx (
3344+ current_funding_commitment_tx. trust ( ) . txid ( ) ,
3345+ htlcs_for_commitment ( current_funding_commitment_tx) ,
3346+ current_funding_commitment_tx. commitment_number ( ) ,
3347+ current_funding_commitment_tx. per_commitment_point ( ) ,
3348+ ) ;
3349+
3350+ for ( pending_funding, commitment_tx) in
3351+ self . pending_funding . iter_mut ( ) . zip ( commitment_txs. iter ( ) . skip ( 1 ) )
3352+ {
3353+ let commitment_txid = commitment_tx. trust ( ) . txid ( ) ;
3354+ pending_funding. prev_counterparty_commitment_txid =
3355+ pending_funding. current_counterparty_commitment_txid . take ( ) ;
3356+ pending_funding. current_counterparty_commitment_txid = Some ( commitment_txid) ;
3357+ pending_funding
3358+ . counterparty_claimable_outpoints
3359+ . insert ( commitment_txid, htlcs_for_commitment ( commitment_tx) ) ;
3360+ }
3361+
3362+ Ok ( ( ) )
3363+ }
3364+
33223365 /// Informs this monitor of the latest holder (ie broadcastable) commitment transaction. The
33233366 /// monitor watches for timeouts and may broadcast it if we approach such a timeout. Thus, it
33243367 /// is important that any clones of this channel monitor (including remote clones) by kept
@@ -3851,14 +3894,24 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
38513894 } ,
38523895 // Soon we will drop the `LatestCounterpartyCommitmentTXInfo` variant in favor of `LatestCounterpartyCommitment`.
38533896 // For now we just add the code to handle the new updates.
3854- // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitmentTX ` variant.
3897+ // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitment ` variant.
38553898 ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point, .. } => {
38563899 log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
3857- self . provide_latest_counterparty_commitment_tx ( * commitment_txid, htlc_outputs. clone ( ) , * commitment_number, * their_per_commitment_point, logger)
3900+ if self . pending_funding . is_empty ( ) {
3901+ self . provide_latest_counterparty_commitment_tx ( * commitment_txid, htlc_outputs. clone ( ) , * commitment_number, * their_per_commitment_point)
3902+ } else {
3903+ log_error ! ( logger, "Received unexpected non-splice counterparty commitment monitor update" ) ;
3904+ ret = Err ( ( ) ) ;
3905+ }
38583906 } ,
3859- ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { htlc_outputs, commitment_tx } => {
3860- log_trace ! ( logger, "Updating ChannelMonitor with latest counterparty commitment transaction info" ) ;
3861- self . provide_latest_counterparty_commitment_tx ( commitment_tx. trust ( ) . txid ( ) , htlc_outputs. clone ( ) , commitment_tx. commitment_number ( ) , commitment_tx. per_commitment_point ( ) , logger)
3907+ ChannelMonitorUpdateStep :: LatestCounterpartyCommitment {
3908+ commitment_txs, htlc_data,
3909+ } => {
3910+ log_trace ! ( logger, "Updating ChannelMonitor with {} latest counterparty commitments" , commitment_txs. len( ) ) ;
3911+ if let Err ( e) = self . update_counterparty_commitment_data ( commitment_txs, htlc_data) {
3912+ log_error ! ( logger, "Failed updating latest counterparty commitment state: {}" , e) ;
3913+ ret = Err ( ( ) ) ;
3914+ }
38623915 } ,
38633916 ChannelMonitorUpdateStep :: PaymentPreimage { payment_preimage, payment_info } => {
38643917 log_trace ! ( logger, "Updating ChannelMonitor with payment preimage" ) ;
@@ -3934,7 +3987,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
39343987 ChannelMonitorUpdateStep :: LatestHolderCommitmentTXInfo { .. }
39353988 |ChannelMonitorUpdateStep :: LatestHolderCommitment { .. }
39363989 |ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. }
3937- |ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { .. }
3990+ |ChannelMonitorUpdateStep :: LatestCounterpartyCommitment { .. }
39383991 |ChannelMonitorUpdateStep :: ShutdownScript { .. }
39393992 |ChannelMonitorUpdateStep :: CommitmentSecret { .. }
39403993 |ChannelMonitorUpdateStep :: RenegotiatedFunding { .. } =>
@@ -4094,7 +4147,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40944147 update. updates . iter ( ) . filter_map ( |update| {
40954148 // Soon we will drop the first branch here in favor of the second.
40964149 // In preparation, we just add the second branch without deleting the first.
4097- // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitmentTX ` variant.
4150+ // Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitment ` variant.
40984151 match update {
40994152 & ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { commitment_txid,
41004153 ref htlc_outputs, commitment_number, their_per_commitment_point,
@@ -4112,19 +4165,17 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
41124165
41134166 debug_assert_eq ! ( commitment_tx. trust( ) . txid( ) , commitment_txid) ;
41144167
4115- Some ( commitment_tx)
4168+ Some ( vec ! [ commitment_tx] )
41164169 } ,
4117- & ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX {
4118- htlc_outputs : _, ref commitment_tx,
4119- } => {
4120- Some ( commitment_tx. clone ( ) )
4170+ & ChannelMonitorUpdateStep :: LatestCounterpartyCommitment { ref commitment_txs, .. } => {
4171+ Some ( commitment_txs. clone ( ) )
41214172 } ,
41224173 & ChannelMonitorUpdateStep :: RenegotiatedFunding { ref counterparty_commitment_tx, .. } => {
4123- Some ( counterparty_commitment_tx. clone ( ) )
4174+ Some ( vec ! [ counterparty_commitment_tx. clone( ) ] )
41244175 } ,
41254176 _ => None ,
41264177 }
4127- } ) . collect ( )
4178+ } ) . flatten ( ) . collect ( )
41284179 }
41294180
41304181 #[ rustfmt:: skip]
@@ -6248,9 +6299,9 @@ mod tests {
62486299 monitor. provide_latest_holder_commitment_tx ( dummy_commitment_tx. clone ( ) ,
62496300 & nondust_htlcs. iter ( ) . map ( |htlc| ( htlc. clone ( ) , Some ( dummy_sig) , Some ( dummy_source. clone ( ) ) ) ) . collect :: < Vec < _ > > ( ) ) ;
62506301 monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"1" ) . to_byte_array ( ) ) ,
6251- preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key, & logger ) ;
6302+ preimages_slice_to_htlc_outputs ! ( preimages[ 5 ..15 ] ) , 281474976710655 , dummy_key) ;
62526303 monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"2" ) . to_byte_array ( ) ) ,
6253- preimages_slice_to_htlc_outputs ! ( preimages[ 15 ..20 ] ) , 281474976710654 , dummy_key, & logger ) ;
6304+ preimages_slice_to_htlc_outputs ! ( preimages[ 15 ..20 ] ) , 281474976710654 , dummy_key) ;
62546305 for & ( ref preimage, ref hash) in preimages. iter ( ) {
62556306 let bounded_fee_estimator = LowerBoundedFeeEstimator :: new ( & fee_estimator) ;
62566307 monitor. provide_payment_preimage_unsafe_legacy (
@@ -6267,7 +6318,7 @@ mod tests {
62676318 test_preimages_exist ! ( & preimages[ 15 ..20 ] , monitor) ;
62686319
62696320 monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"3" ) . to_byte_array ( ) ) ,
6270- preimages_slice_to_htlc_outputs ! ( preimages[ 17 ..20 ] ) , 281474976710653 , dummy_key, & logger ) ;
6321+ preimages_slice_to_htlc_outputs ! ( preimages[ 17 ..20 ] ) , 281474976710653 , dummy_key) ;
62716322
62726323 // Now provide a further secret, pruning preimages 15-17
62736324 secret[ 0 ..32 ] . clone_from_slice ( & <Vec < u8 > >:: from_hex ( "c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964" ) . unwrap ( ) ) ;
@@ -6277,7 +6328,7 @@ mod tests {
62776328 test_preimages_exist ! ( & preimages[ 17 ..20 ] , monitor) ;
62786329
62796330 monitor. provide_latest_counterparty_commitment_tx ( Txid :: from_byte_array ( Sha256 :: hash ( b"4" ) . to_byte_array ( ) ) ,
6280- preimages_slice_to_htlc_outputs ! ( preimages[ 18 ..20 ] ) , 281474976710652 , dummy_key, & logger ) ;
6331+ preimages_slice_to_htlc_outputs ! ( preimages[ 18 ..20 ] ) , 281474976710652 , dummy_key) ;
62816332
62826333 // Now update holder commitment tx info, pruning only element 18 as we still care about the
62836334 // previous commitment tx's preimages too
0 commit comments