@@ -42,6 +42,8 @@ use std;
42
42
use std:: default:: Default ;
43
43
use std:: { cmp, mem, fmt} ;
44
44
use std:: ops:: Deref ;
45
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
46
+ use std:: sync:: Mutex ;
45
47
use bitcoin:: hashes:: hex:: ToHex ;
46
48
47
49
#[ cfg( test) ]
@@ -386,6 +388,15 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
386
388
commitment_secrets : CounterpartyCommitmentSecrets ,
387
389
388
390
network_sync : UpdateStatus ,
391
+
392
+ // We save these values so we can make sure `next_local_commit_tx_fee_msat` and
393
+ // `next_remote_commit_tx_fee_msat` properly predict what the next commitment transaction fee will
394
+ // be, by comparing the cached values to the fee of the tranaction generated by
395
+ // `build_commitment_transaction`.
396
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
397
+ next_local_commitment_tx_fee_cached : Mutex < Option < u64 > > ,
398
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
399
+ next_remote_commitment_tx_fee_cached : Mutex < Option < u64 > > ,
389
400
}
390
401
391
402
pub const OUR_MAX_HTLCS : u16 = 50 ; //TODO
@@ -557,6 +568,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
557
568
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
558
569
559
570
network_sync : UpdateStatus :: Fresh ,
571
+
572
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
573
+ next_local_commitment_tx_fee_cached : Mutex :: new ( None ) ,
574
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
575
+ next_remote_commitment_tx_fee_cached : Mutex :: new ( None ) ,
560
576
} )
561
577
}
562
578
@@ -790,6 +806,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
790
806
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
791
807
792
808
network_sync : UpdateStatus :: Fresh ,
809
+
810
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
811
+ next_local_commitment_tx_fee_cached : Mutex :: new ( None ) ,
812
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
813
+ next_remote_commitment_tx_fee_cached : Mutex :: new ( None ) ,
793
814
} ;
794
815
795
816
Ok ( chan)
@@ -867,7 +888,21 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
867
888
InboundHTLCState :: AwaitingRemoteRevokeToAnnounce ( _) => ( !generated_by_local, "AwaitingRemoteRevokeToAnnounce" ) ,
868
889
InboundHTLCState :: AwaitingAnnouncedRemoteRevoke ( _) => ( true , "AwaitingAnnouncedRemoteRevoke" ) ,
869
890
InboundHTLCState :: Committed => ( true , "Committed" ) ,
870
- InboundHTLCState :: LocalRemoved ( _) => ( !generated_by_local, "LocalRemoved" ) ,
891
+ InboundHTLCState :: LocalRemoved ( _) => {
892
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
893
+ {
894
+ // Remote commitment transactions generated by us won't include our locally removed
895
+ // HTLCs, even though we include them when deciding whether to accept/send new HTLCs and
896
+ // setting these cached values. Therefore, set these values to None here so we don't
897
+ // assert that the transaction generated in this function has the same fee as the cached
898
+ // fee. Similar reasoning applies to LocalAnnounced and RemoveRemoved HTLCs below.
899
+ if !local && generated_by_local {
900
+ * self . next_local_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = None ;
901
+ * self . next_remote_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = None ;
902
+ }
903
+ }
904
+ ( !generated_by_local, "LocalRemoved" )
905
+ } ,
871
906
} ;
872
907
873
908
if include {
@@ -890,9 +925,27 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
890
925
891
926
for ref htlc in self . pending_outbound_htlcs . iter ( ) {
892
927
let ( include, state_name) = match htlc. state {
893
- OutboundHTLCState :: LocalAnnounced ( _) => ( generated_by_local, "LocalAnnounced" ) ,
928
+ OutboundHTLCState :: LocalAnnounced ( _) => {
929
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
930
+ {
931
+ if local && !generated_by_local {
932
+ * self . next_local_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = None ;
933
+ * self . next_remote_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = None ;
934
+ }
935
+ }
936
+ ( generated_by_local, "LocalAnnounced" )
937
+ } ,
894
938
OutboundHTLCState :: Committed => ( true , "Committed" ) ,
895
- OutboundHTLCState :: RemoteRemoved ( _) => ( generated_by_local, "RemoteRemoved" ) ,
939
+ OutboundHTLCState :: RemoteRemoved ( _) => {
940
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
941
+ {
942
+ if local && !generated_by_local {
943
+ * self . next_local_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = None ;
944
+ * self . next_remote_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = None ;
945
+ }
946
+ }
947
+ ( generated_by_local, "RemoteRemoved" )
948
+ } ,
896
949
OutboundHTLCState :: AwaitingRemoteRevokeToRemove ( _) => ( generated_by_local, "AwaitingRemoteRevokeToRemove" ) ,
897
950
OutboundHTLCState :: AwaitingRemovedRemoteRevoke ( _) => ( false , "AwaitingRemovedRemoteRevoke" ) ,
898
951
} ;
@@ -1740,7 +1793,18 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1740
1793
}
1741
1794
}
1742
1795
1743
- self . commit_tx_fee_msat ( included_htlcs + addl_htlcs)
1796
+ let num_htlcs = included_htlcs + addl_htlcs;
1797
+ let res = self . commit_tx_fee_msat ( num_htlcs) ;
1798
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
1799
+ {
1800
+ if fee_spike_buffer_htlc. is_none ( ) {
1801
+ * self . next_local_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = Some ( res) ;
1802
+ } else {
1803
+ let fee = self . commit_tx_fee_msat ( num_htlcs - 1 ) ;
1804
+ * self . next_local_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = Some ( fee) ;
1805
+ }
1806
+ }
1807
+ res
1744
1808
}
1745
1809
1746
1810
// Get the commitment tx fee for the remote's next commitment transaction based on the number of
@@ -1785,10 +1849,38 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1785
1849
}
1786
1850
}
1787
1851
1788
- self . commit_tx_fee_msat ( included_htlcs + addl_htlcs)
1852
+ let num_htlcs = included_htlcs + addl_htlcs;
1853
+ let res = self . commit_tx_fee_msat ( num_htlcs) ;
1854
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
1855
+ {
1856
+ if fee_spike_buffer_htlc. is_none ( ) {
1857
+ * self . next_remote_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = Some ( res) ;
1858
+ } else {
1859
+ let fee = self . commit_tx_fee_msat ( num_htlcs - 1 ) ;
1860
+ * self . next_remote_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = Some ( fee) ;
1861
+ }
1862
+ }
1863
+ res
1789
1864
}
1790
1865
1791
- pub fn update_add_htlc < F , L : Deref > ( & mut self , msg : & msgs:: UpdateAddHTLC , mut pending_forward_status : PendingHTLCStatus , create_pending_htlc_status : F , logger : & L ) -> Result < ( ) , ChannelError >
1866
+
1867
+ pub fn update_add_htlc < F , L : Deref > ( & mut self , msg : & msgs:: UpdateAddHTLC , pending_forward_status : PendingHTLCStatus , create_pending_htlc_status : F , logger : & L ) -> Result < ( ) , ChannelError >
1868
+ where F : for < ' a > Fn ( & ' a Self , PendingHTLCStatus , u16 ) -> PendingHTLCStatus , L :: Target : Logger {
1869
+ match self . update_add_htlc_internal ( msg, pending_forward_status, create_pending_htlc_status, logger) {
1870
+ Ok ( ( ) ) => return Ok ( ( ) ) ,
1871
+ Err ( e) => {
1872
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
1873
+ {
1874
+ // If we errored, one of these fields still might've been set, so re-set them to None.
1875
+ * self . next_local_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = None ;
1876
+ * self . next_remote_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = None ;
1877
+ }
1878
+ return Err ( e) ;
1879
+ } ,
1880
+ }
1881
+ }
1882
+
1883
+ fn update_add_htlc_internal < F , L : Deref > ( & mut self , msg : & msgs:: UpdateAddHTLC , mut pending_forward_status : PendingHTLCStatus , create_pending_htlc_status : F , logger : & L ) -> Result < ( ) , ChannelError >
1792
1884
where F : for < ' a > Fn ( & ' a Self , PendingHTLCStatus , u16 ) -> PendingHTLCStatus , L :: Target : Logger {
1793
1885
// We can't accept HTLCs sent after we've sent a shutdown.
1794
1886
let local_sent_shutdown = ( self . channel_state & ( ChannelState :: ChannelFunded as u32 | ChannelState :: LocalShutdownSent as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) ;
@@ -2019,15 +2111,24 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2019
2111
( commitment_tx. 1 , htlcs_cloned, commitment_tx. 0 , commitment_txid)
2020
2112
} ;
2021
2113
2114
+ let total_fee = feerate_per_kw as u64 * ( COMMITMENT_TX_BASE_WEIGHT + ( num_htlcs as u64 ) * COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000 ;
2022
2115
//If channel fee was updated by funder confirm funder can afford the new fee rate when applied to the current local commitment transaction
2023
2116
if update_fee {
2024
- let total_fee = feerate_per_kw as u64 * ( COMMITMENT_TX_BASE_WEIGHT + ( num_htlcs as u64 ) * COMMITMENT_TX_WEIGHT_PER_HTLC ) / 1000 ;
2025
-
2026
2117
let counterparty_reserve_we_require = Channel :: < ChanSigner > :: get_holder_selected_channel_reserve_satoshis ( self . channel_value_satoshis ) ;
2027
2118
if self . channel_value_satoshis - self . value_to_self_msat / 1000 < total_fee + counterparty_reserve_we_require {
2028
2119
return Err ( ( None , ChannelError :: Close ( "Funding remote cannot afford proposed new fee" . to_owned ( ) ) ) ) ;
2029
2120
}
2030
2121
}
2122
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
2123
+ {
2124
+ let projected_fee = match self . is_outbound ( ) {
2125
+ true => self . next_local_commitment_tx_fee_cached . lock ( ) . unwrap ( ) . take ( ) ,
2126
+ false => self . next_remote_commitment_tx_fee_cached . lock ( ) . unwrap ( ) . take ( )
2127
+ } ;
2128
+ if let Some ( fee) = projected_fee {
2129
+ assert_eq ! ( total_fee, fee / 1000 ) ;
2130
+ }
2131
+ }
2031
2132
2032
2133
if msg. htlc_signatures . len ( ) != num_htlcs {
2033
2134
return Err ( ( None , ChannelError :: Close ( format ! ( "Got wrong number of HTLC signatures ({}) from remote. It must be {}" , msg. htlc_signatures. len( ) , num_htlcs) ) ) ) ;
@@ -3726,6 +3827,28 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3726
3827
/// You MUST call send_commitment prior to any other calls on this Channel
3727
3828
/// If an Err is returned, it's a ChannelError::Ignore!
3728
3829
pub fn send_htlc ( & mut self , amount_msat : u64 , payment_hash : PaymentHash , cltv_expiry : u32 , source : HTLCSource , onion_routing_packet : msgs:: OnionPacket ) -> Result < Option < msgs:: UpdateAddHTLC > , ChannelError > {
3830
+ match self . send_htlc_internal ( amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet) {
3831
+ Ok ( res) => return Ok ( res) ,
3832
+ Err ( e) => {
3833
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
3834
+ {
3835
+ // If we errored, one of these fields still might've been set, so re-set them to None.
3836
+ * self . next_local_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = None ;
3837
+ * self . next_remote_commitment_tx_fee_cached . lock ( ) . unwrap ( ) = None ;
3838
+ }
3839
+ return Err ( e) ;
3840
+ }
3841
+ }
3842
+ }
3843
+
3844
+ /// Adds a pending outbound HTLC to this channel, note that you probably want
3845
+ /// send_htlc_and_commit instead cause you'll want both messages at once.
3846
+ /// This returns an option instead of a pure UpdateAddHTLC as we may be in a state where we are
3847
+ /// waiting on the remote peer to send us a revoke_and_ack during which time we cannot add new
3848
+ /// HTLCs on the wire or we wouldn't be able to determine what they actually ACK'ed.
3849
+ /// You MUST call send_commitment prior to any other calls on this Channel
3850
+ /// If an Err is returned, it's a ChannelError::Ignore!
3851
+ fn send_htlc_internal ( & mut self , amount_msat : u64 , payment_hash : PaymentHash , cltv_expiry : u32 , source : HTLCSource , onion_routing_packet : msgs:: OnionPacket ) -> Result < Option < msgs:: UpdateAddHTLC > , ChannelError > {
3729
3852
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 | BOTH_SIDES_SHUTDOWN_MASK ) ) != ( ChannelState :: ChannelFunded as u32 ) {
3730
3853
return Err ( ChannelError :: Ignore ( "Cannot send HTLC until channel is fully established and we haven't started shutting down" . to_owned ( ) ) ) ;
3731
3854
}
@@ -3921,6 +4044,18 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3921
4044
let counterparty_commitment_txid = counterparty_commitment_tx. 0 . trust ( ) . txid ( ) ;
3922
4045
let ( signature, htlc_signatures) ;
3923
4046
4047
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
4048
+ {
4049
+ let projected_fee = match self . is_outbound ( ) {
4050
+ true => self . next_local_commitment_tx_fee_cached . lock ( ) . unwrap ( ) . take ( ) ,
4051
+ false => self . next_remote_commitment_tx_fee_cached . lock ( ) . unwrap ( ) . take ( )
4052
+ } ;
4053
+ if let Some ( fee) = projected_fee {
4054
+ let actual_fee = self . commit_tx_fee_msat ( counterparty_commitment_tx. 1 ) ;
4055
+ assert_eq ! ( actual_fee, fee) ;
4056
+ }
4057
+ }
4058
+
3924
4059
{
3925
4060
let mut htlcs = Vec :: with_capacity ( counterparty_commitment_tx. 2 . len ( ) ) ;
3926
4061
for & ( ref htlc, _) in counterparty_commitment_tx. 2 . iter ( ) {
@@ -4511,6 +4646,11 @@ impl<'a, ChanSigner: ChannelKeys, K: Deref> ReadableArgs<&'a K> for Channel<Chan
4511
4646
commitment_secrets,
4512
4647
4513
4648
network_sync : UpdateStatus :: Fresh ,
4649
+
4650
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
4651
+ next_local_commitment_tx_fee_cached : Mutex :: new ( None ) ,
4652
+ #[ cfg( any( test, feature = "fuzztarget" ) ) ]
4653
+ next_remote_commitment_tx_fee_cached : Mutex :: new ( None ) ,
4514
4654
} )
4515
4655
}
4516
4656
}
0 commit comments