@@ -113,6 +113,37 @@ pub struct ChannelMonitorUpdate {
113
113
pub channel_id : Option < ChannelId > ,
114
114
}
115
115
116
+ impl ChannelMonitorUpdate {
117
+ fn internal_renegotiated_funding_data (
118
+ & self ,
119
+ ) -> impl Iterator < Item = ( OutPoint , ScriptBuf ) > + ' _ {
120
+ self . updates . iter ( ) . filter_map ( |update| match update {
121
+ ChannelMonitorUpdateStep :: RenegotiatedFunding { channel_parameters, .. } => {
122
+ let funding_outpoint = channel_parameters
123
+ . funding_outpoint
124
+ . expect ( "Renegotiated funding must always have known outpoint" ) ;
125
+ let funding_script = channel_parameters. make_funding_redeemscript ( ) . to_p2wsh ( ) ;
126
+ Some ( ( funding_outpoint, funding_script) )
127
+ } ,
128
+ _ => None ,
129
+ } )
130
+ }
131
+
132
+ /// Returns an iterator of new (funding outpoint, funding script) to monitor the chain for as
133
+ /// a result of a renegotiated funding transaction.
134
+ #[ cfg( c_bindings) ]
135
+ pub fn renegotiated_funding_data ( & self ) -> Vec < ( OutPoint , ScriptBuf ) > {
136
+ self . internal_renegotiated_funding_data ( ) . collect ( )
137
+ }
138
+
139
+ /// Returns an iterator of new (funding outpoint, funding script) to monitor the chain for as
140
+ /// a result of a renegotiated funding transaction.
141
+ #[ cfg( not( c_bindings) ) ]
142
+ pub fn renegotiated_funding_data ( & self ) -> impl Iterator < Item = ( OutPoint , ScriptBuf ) > + ' _ {
143
+ self . internal_renegotiated_funding_data ( )
144
+ }
145
+ }
146
+
116
147
/// LDK prior to 0.1 used this constant as the [`ChannelMonitorUpdate::update_id`] for any
117
148
/// [`ChannelMonitorUpdate`]s which were generated after the channel was closed.
118
149
const LEGACY_CLOSED_CHANNEL_UPDATE_ID : u64 = u64:: MAX ;
@@ -640,6 +671,11 @@ pub(crate) enum ChannelMonitorUpdateStep {
640
671
ShutdownScript {
641
672
scriptpubkey : ScriptBuf ,
642
673
} ,
674
+ RenegotiatedFunding {
675
+ channel_parameters : ChannelTransactionParameters ,
676
+ holder_commitment_tx : HolderCommitmentTransaction ,
677
+ counterparty_commitment_tx : CommitmentTransaction ,
678
+ } ,
643
679
}
644
680
645
681
impl ChannelMonitorUpdateStep {
@@ -653,6 +689,7 @@ impl ChannelMonitorUpdateStep {
653
689
ChannelMonitorUpdateStep :: CommitmentSecret { .. } => "CommitmentSecret" ,
654
690
ChannelMonitorUpdateStep :: ChannelForceClosed { .. } => "ChannelForceClosed" ,
655
691
ChannelMonitorUpdateStep :: ShutdownScript { .. } => "ShutdownScript" ,
692
+ ChannelMonitorUpdateStep :: RenegotiatedFunding { .. } => "RenegotiatedFunding" ,
656
693
}
657
694
}
658
695
}
@@ -691,6 +728,11 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
691
728
( 0 , htlc_outputs, required_vec) ,
692
729
( 2 , commitment_tx, required) ,
693
730
} ,
731
+ ( 10 , RenegotiatedFunding ) => {
732
+ ( 1 , channel_parameters, ( required: ReadableArgs , None ) ) ,
733
+ ( 3 , holder_commitment_tx, required) ,
734
+ ( 5 , counterparty_commitment_tx, required) ,
735
+ } ,
694
736
) ;
695
737
696
738
/// Indicates whether the balance is derived from a cooperative close, a force-close
@@ -1024,9 +1066,69 @@ struct FundingScope {
1024
1066
prev_holder_commitment_tx : Option < HolderCommitmentTransaction > ,
1025
1067
}
1026
1068
1069
+ impl FundingScope {
1070
+ fn funding_outpoint ( & self ) -> OutPoint {
1071
+ let funding_outpoint = self . channel_parameters . funding_outpoint . as_ref ( ) ;
1072
+ * funding_outpoint. expect ( "Funding outpoint must be set for active monitor" )
1073
+ }
1074
+
1075
+ fn funding_txid ( & self ) -> Txid {
1076
+ self . funding_outpoint ( ) . txid
1077
+ }
1078
+ }
1079
+
1080
+ impl Writeable for FundingScope {
1081
+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
1082
+ write_tlv_fields ! ( w, {
1083
+ ( 1 , self . channel_parameters, ( required: ReadableArgs , None ) ) ,
1084
+ ( 3 , self . current_counterparty_commitment_txid, required) ,
1085
+ ( 5 , self . prev_counterparty_commitment_txid, option) ,
1086
+ ( 7 , self . current_holder_commitment_tx, required) ,
1087
+ ( 9 , self . prev_holder_commitment_tx, option) ,
1088
+ ( 11 , self . counterparty_claimable_outpoints, required) ,
1089
+ } ) ;
1090
+ Ok ( ( ) )
1091
+ }
1092
+ }
1093
+
1094
+ impl Readable for FundingScope {
1095
+ fn read < R : io:: Read > ( r : & mut R ) -> Result < Self , DecodeError > {
1096
+ let mut channel_parameters = RequiredWrapper ( None ) ;
1097
+ let mut current_counterparty_commitment_txid = RequiredWrapper ( None ) ;
1098
+ let mut prev_counterparty_commitment_txid = None ;
1099
+ let mut current_holder_commitment_tx = RequiredWrapper ( None ) ;
1100
+ let mut prev_holder_commitment_tx = None ;
1101
+ let mut counterparty_claimable_outpoints = RequiredWrapper ( None ) ;
1102
+
1103
+ read_tlv_fields ! ( r, {
1104
+ ( 1 , channel_parameters, ( required: ReadableArgs , None ) ) ,
1105
+ ( 3 , current_counterparty_commitment_txid, required) ,
1106
+ ( 5 , prev_counterparty_commitment_txid, option) ,
1107
+ ( 7 , current_holder_commitment_tx, required) ,
1108
+ ( 9 , prev_holder_commitment_tx, option) ,
1109
+ ( 11 , counterparty_claimable_outpoints, required) ,
1110
+ } ) ;
1111
+
1112
+ let channel_parameters: ChannelTransactionParameters = channel_parameters. 0 . unwrap ( ) ;
1113
+ let redeem_script = channel_parameters. make_funding_redeemscript ( ) ;
1114
+
1115
+ Ok ( Self {
1116
+ script_pubkey : redeem_script. to_p2wsh ( ) ,
1117
+ redeem_script,
1118
+ channel_parameters,
1119
+ current_counterparty_commitment_txid : current_counterparty_commitment_txid. 0 . unwrap ( ) ,
1120
+ prev_counterparty_commitment_txid,
1121
+ current_holder_commitment_tx : current_holder_commitment_tx. 0 . unwrap ( ) ,
1122
+ prev_holder_commitment_tx,
1123
+ counterparty_claimable_outpoints : counterparty_claimable_outpoints. 0 . unwrap ( ) ,
1124
+ } )
1125
+ }
1126
+ }
1127
+
1027
1128
#[ derive( Clone , PartialEq ) ]
1028
1129
pub ( crate ) struct ChannelMonitorImpl < Signer : EcdsaChannelSigner > {
1029
1130
funding : FundingScope ,
1131
+ pending_funding : Vec < FundingScope > ,
1030
1132
1031
1133
latest_update_id : u64 ,
1032
1134
commitment_transaction_number_obscure_factor : u64 ,
@@ -1467,6 +1569,7 @@ impl<Signer: EcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signer> {
1467
1569
( 27 , self . first_confirmed_funding_txo, required) ,
1468
1570
( 29 , self . initial_counterparty_commitment_tx, option) ,
1469
1571
( 31 , self . funding. channel_parameters, required) ,
1572
+ ( 32 , self . pending_funding, optional_vec) ,
1470
1573
} ) ;
1471
1574
1472
1575
Ok ( ( ) )
@@ -1636,6 +1739,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1636
1739
current_holder_commitment_tx : initial_holder_commitment_tx,
1637
1740
prev_holder_commitment_tx : None ,
1638
1741
} ,
1742
+ pending_funding : vec ! [ ] ,
1639
1743
1640
1744
latest_update_id : 0 ,
1641
1745
commitment_transaction_number_obscure_factor,
@@ -1862,14 +1966,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
1862
1966
{
1863
1967
let lock = self . inner . lock ( ) . unwrap ( ) ;
1864
1968
let logger = WithChannelMonitor :: from_impl ( logger, & * lock, None ) ;
1865
- log_trace ! ( & logger, "Registering funding outpoint {}" , & lock. get_funding_txo( ) ) ;
1866
- let funding_outpoint = lock. get_funding_txo ( ) ;
1867
- filter. register_tx ( & funding_outpoint. txid , & lock. funding . script_pubkey ) ;
1969
+ for funding in core:: iter:: once ( & lock. funding ) . chain ( & lock. pending_funding ) {
1970
+ let funding_outpoint = funding. funding_outpoint ( ) ;
1971
+ log_trace ! ( & logger, "Registering funding outpoint {} with the filter to monitor confirmations" , & funding_outpoint) ;
1972
+ filter. register_tx ( & funding_outpoint. txid , & funding. script_pubkey ) ;
1973
+ }
1868
1974
for ( txid, outputs) in lock. get_outputs_to_watch ( ) . iter ( ) {
1869
1975
for ( index, script_pubkey) in outputs. iter ( ) {
1870
1976
assert ! ( * index <= u16 :: MAX as u32 ) ;
1871
1977
let outpoint = OutPoint { txid : * txid, index : * index as u16 } ;
1872
- log_trace ! ( logger, "Registering outpoint {} with the filter for monitoring spends " , outpoint) ;
1978
+ log_trace ! ( logger, "Registering outpoint {} with the filter to monitor spend " , outpoint) ;
1873
1979
filter. register_output ( WatchedOutput {
1874
1980
block_hash : None ,
1875
1981
outpoint,
@@ -3453,6 +3559,128 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3453
3559
) ;
3454
3560
}
3455
3561
3562
+ fn renegotiated_funding < L : Deref > (
3563
+ & mut self , logger : & WithChannelMonitor < L > ,
3564
+ channel_parameters : & ChannelTransactionParameters ,
3565
+ alternative_holder_commitment_tx : & HolderCommitmentTransaction ,
3566
+ alternative_counterparty_commitment_tx : & CommitmentTransaction ,
3567
+ ) -> Result < ( ) , ( ) >
3568
+ where
3569
+ L :: Target : Logger ,
3570
+ {
3571
+ let redeem_script = channel_parameters. make_funding_redeemscript ( ) ;
3572
+ let script_pubkey = redeem_script. to_p2wsh ( ) ;
3573
+ let alternative_counterparty_commitment_txid =
3574
+ alternative_counterparty_commitment_tx. trust ( ) . txid ( ) ;
3575
+
3576
+ // Both the current counterparty commitment and the alternative one share the same set of
3577
+ // non-dust and dust HTLCs in the same order, though the index of each non-dust HTLC may be
3578
+ // different.
3579
+ //
3580
+ // We clone all HTLCs and their sources to use in the alternative funding scope, and update
3581
+ // each non-dust HTLC with their corresponding index in the alternative counterparty
3582
+ // commitment.
3583
+ let current_counterparty_commitment_htlcs =
3584
+ if let Some ( txid) = & self . funding . current_counterparty_commitment_txid {
3585
+ self . funding . counterparty_claimable_outpoints . get ( txid) . unwrap ( )
3586
+ } else {
3587
+ debug_assert ! ( false ) ;
3588
+ log_error ! (
3589
+ logger,
3590
+ "Received funding renegotiation while initial funding negotiation is still pending"
3591
+ ) ;
3592
+ return Err ( ( ) ) ;
3593
+ } ;
3594
+ let mut htlcs_with_sources = current_counterparty_commitment_htlcs. clone ( ) ;
3595
+ let alternative_htlcs = alternative_counterparty_commitment_tx. nondust_htlcs ( ) ;
3596
+
3597
+ let expected_non_dust_htlc_count = htlcs_with_sources
3598
+ . iter ( )
3599
+ // Non-dust HTLCs always come first, so the position of the first dust HTLC is equal to
3600
+ // our non-dust HTLC count.
3601
+ . position ( |( htlc, _) | htlc. transaction_output_index . is_none ( ) )
3602
+ . unwrap_or ( htlcs_with_sources. len ( ) ) ;
3603
+ if alternative_htlcs. len ( ) != expected_non_dust_htlc_count {
3604
+ log_error ! (
3605
+ logger,
3606
+ "Received alternative counterparty commitment with HTLC count mismatch"
3607
+ ) ;
3608
+ return Err ( ( ) ) ;
3609
+ }
3610
+
3611
+ for ( alternative_htlc, ( htlc, _) ) in
3612
+ alternative_htlcs. iter ( ) . zip ( htlcs_with_sources. iter_mut ( ) )
3613
+ {
3614
+ debug_assert ! ( htlc. transaction_output_index. is_some( ) ) ;
3615
+ debug_assert ! ( alternative_htlc. transaction_output_index. is_some( ) ) ;
3616
+ if !alternative_htlc. is_data_equal ( htlc) {
3617
+ log_error ! (
3618
+ logger,
3619
+ "Received alternative counterparty commitment with non-dust HTLC mismatch"
3620
+ ) ;
3621
+ return Err ( ( ) ) ;
3622
+ }
3623
+ htlc. transaction_output_index = alternative_htlc. transaction_output_index ;
3624
+ }
3625
+
3626
+ let mut counterparty_claimable_outpoints = new_hash_map ( ) ;
3627
+ counterparty_claimable_outpoints
3628
+ . insert ( alternative_counterparty_commitment_txid, htlcs_with_sources) ;
3629
+
3630
+ // TODO(splicing): Enforce any necessary RBF validity checks.
3631
+ let alternative_funding = FundingScope {
3632
+ script_pubkey : script_pubkey. clone ( ) ,
3633
+ redeem_script,
3634
+ channel_parameters : channel_parameters. clone ( ) ,
3635
+ current_counterparty_commitment_txid : Some ( alternative_counterparty_commitment_txid) ,
3636
+ prev_counterparty_commitment_txid : None ,
3637
+ counterparty_claimable_outpoints,
3638
+ current_holder_commitment_tx : alternative_holder_commitment_tx. clone ( ) ,
3639
+ prev_holder_commitment_tx : None ,
3640
+ } ;
3641
+ let alternative_funding_outpoint = alternative_funding. funding_outpoint ( ) ;
3642
+
3643
+ if self
3644
+ . pending_funding
3645
+ . iter ( )
3646
+ . any ( |funding| funding. funding_txid ( ) == alternative_funding_outpoint. txid )
3647
+ {
3648
+ log_error ! (
3649
+ logger,
3650
+ "Renegotiated funding transaction with a duplicate funding txid {}" ,
3651
+ alternative_funding_outpoint. txid
3652
+ ) ;
3653
+ return Err ( ( ) ) ;
3654
+ }
3655
+
3656
+ if let Some ( parent_funding_txid) = channel_parameters. splice_parent_funding_txid . as_ref ( ) {
3657
+ // Only one splice can be negotiated at a time after we've exchanged `channel_ready`
3658
+ // (implying our funding is confirmed) that spends our currently locked funding.
3659
+ if !self . pending_funding . is_empty ( ) {
3660
+ log_error ! (
3661
+ logger,
3662
+ "Negotiated splice while channel is pending channel_ready/splice_locked"
3663
+ ) ;
3664
+ return Err ( ( ) ) ;
3665
+ }
3666
+ if * parent_funding_txid != self . funding . funding_txid ( ) {
3667
+ log_error ! (
3668
+ logger,
3669
+ "Negotiated splice that does not spend currently locked funding transaction"
3670
+ ) ;
3671
+ return Err ( ( ) ) ;
3672
+ }
3673
+ }
3674
+
3675
+ self . outputs_to_watch . insert (
3676
+ alternative_funding_outpoint. txid ,
3677
+ vec ! [ ( alternative_funding_outpoint. index as u32 , script_pubkey) ] ,
3678
+ ) ;
3679
+ self . pending_funding . push ( alternative_funding) ;
3680
+
3681
+ Ok ( ( ) )
3682
+ }
3683
+
3456
3684
#[ rustfmt:: skip]
3457
3685
fn update_monitor < B : Deref , F : Deref , L : Deref > (
3458
3686
& mut self , updates : & ChannelMonitorUpdate , broadcaster : & B , fee_estimator : & F , logger : & WithChannelMonitor < L >
@@ -3532,6 +3760,17 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3532
3760
ret = Err ( ( ) ) ;
3533
3761
}
3534
3762
} ,
3763
+ ChannelMonitorUpdateStep :: RenegotiatedFunding {
3764
+ channel_parameters, holder_commitment_tx, counterparty_commitment_tx,
3765
+ } => {
3766
+ log_trace ! ( logger, "Updating ChannelMonitor with alternative holder and counterparty commitment transactions for funding txid {}" ,
3767
+ channel_parameters. funding_outpoint. unwrap( ) . txid) ;
3768
+ if let Err ( _) = self . renegotiated_funding (
3769
+ logger, channel_parameters, holder_commitment_tx, counterparty_commitment_tx,
3770
+ ) {
3771
+ ret = Err ( ( ) ) ;
3772
+ }
3773
+ } ,
3535
3774
ChannelMonitorUpdateStep :: ChannelForceClosed { should_broadcast } => {
3536
3775
log_trace ! ( logger, "Updating ChannelMonitor: channel force closed, should broadcast: {}" , should_broadcast) ;
3537
3776
self . lockdown_from_offchain = true ;
@@ -3583,7 +3822,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3583
3822
|ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTXInfo { .. }
3584
3823
|ChannelMonitorUpdateStep :: LatestCounterpartyCommitmentTX { .. }
3585
3824
|ChannelMonitorUpdateStep :: ShutdownScript { .. }
3586
- |ChannelMonitorUpdateStep :: CommitmentSecret { .. } =>
3825
+ |ChannelMonitorUpdateStep :: CommitmentSecret { .. }
3826
+ |ChannelMonitorUpdateStep :: RenegotiatedFunding { .. } =>
3587
3827
is_pre_close_update = true ,
3588
3828
// After a channel is closed, we don't communicate with our peer about it, so the
3589
3829
// only things we will update is getting a new preimage (from a different channel)
@@ -3765,6 +4005,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
3765
4005
} => {
3766
4006
Some ( commitment_tx. clone ( ) )
3767
4007
} ,
4008
+ & ChannelMonitorUpdateStep :: RenegotiatedFunding { ref counterparty_commitment_tx, .. } => {
4009
+ Some ( counterparty_commitment_tx. clone ( ) )
4010
+ } ,
3768
4011
_ => None ,
3769
4012
}
3770
4013
} ) . collect ( )
@@ -5438,6 +5681,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
5438
5681
let mut payment_preimages_with_info: Option < HashMap < _ , _ > > = None ;
5439
5682
let mut first_confirmed_funding_txo = RequiredWrapper ( None ) ;
5440
5683
let mut channel_parameters = None ;
5684
+ let mut pending_funding = None ;
5441
5685
read_tlv_fields ! ( reader, {
5442
5686
( 1 , funding_spend_confirmed, option) ,
5443
5687
( 3 , htlcs_resolved_on_chain, optional_vec) ,
@@ -5455,6 +5699,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
5455
5699
( 27 , first_confirmed_funding_txo, ( default_value, outpoint) ) ,
5456
5700
( 29 , initial_counterparty_commitment_tx, option) ,
5457
5701
( 31 , channel_parameters, ( option: ReadableArgs , None ) ) ,
5702
+ ( 32 , pending_funding, optional_vec) ,
5458
5703
} ) ;
5459
5704
if let Some ( payment_preimages_with_info) = payment_preimages_with_info {
5460
5705
if payment_preimages_with_info. len ( ) != payment_preimages. len ( ) {
@@ -5570,6 +5815,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
5570
5815
current_holder_commitment_tx,
5571
5816
prev_holder_commitment_tx,
5572
5817
} ,
5818
+ pending_funding : pending_funding. unwrap_or ( vec ! [ ] ) ,
5573
5819
5574
5820
latest_update_id,
5575
5821
commitment_transaction_number_obscure_factor,
0 commit comments